mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-07-21 10:25:42 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export declare function secp256k1Compress(publicKey: Uint8Array, onlyJs?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,14 @@
|
||||
import { secp256k1 } from '@noble/curves/secp256k1';
|
||||
import { hasBigInt } from '@pezkuwi/util';
|
||||
import { isReady, secp256k1Compress as wasm } from '@pezkuwi/wasm-crypto';
|
||||
export function secp256k1Compress(publicKey, onlyJs) {
|
||||
if (![33, 65].includes(publicKey.length)) {
|
||||
throw new Error(`Invalid publicKey provided, received ${publicKey.length} bytes input`);
|
||||
}
|
||||
if (publicKey.length === 33) {
|
||||
return publicKey;
|
||||
}
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? wasm(publicKey)
|
||||
: secp256k1.ProjectivePoint.fromHex(publicKey).toRawBytes(true);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export declare function secp256k1DeriveHard(seed: Uint8Array, chainCode: Uint8Array): Uint8Array;
|
||||
@@ -0,0 +1,10 @@
|
||||
import { compactAddLength, isU8a, stringToU8a, u8aConcat } from '@pezkuwi/util';
|
||||
import { blake2AsU8a } from '../blake2/asU8a.js';
|
||||
const HDKD = compactAddLength(stringToU8a('Secp256k1HDKD'));
|
||||
export function secp256k1DeriveHard(seed, chainCode) {
|
||||
if (!isU8a(chainCode) || chainCode.length !== 32) {
|
||||
throw new Error('Invalid chainCode passed to derive');
|
||||
}
|
||||
// NOTE This is specific to the Substrate HDD derivation, so always use the blake2 hasher
|
||||
return blake2AsU8a(u8aConcat(HDKD, seed, chainCode), 256);
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function secp256k1Expand(publicKey: Uint8Array, onlyJs?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,17 @@
|
||||
import { secp256k1 } from '@noble/curves/secp256k1';
|
||||
import { bnToU8a, hasBigInt, u8aConcat } from '@pezkuwi/util';
|
||||
import { isReady, secp256k1Expand as wasm } from '@pezkuwi/wasm-crypto';
|
||||
import { BN_BE_256_OPTS } from '../bn.js';
|
||||
export function secp256k1Expand(publicKey, onlyJs) {
|
||||
if (![33, 65].includes(publicKey.length)) {
|
||||
throw new Error(`Invalid publicKey provided, received ${publicKey.length} bytes input`);
|
||||
}
|
||||
if (publicKey.length === 65) {
|
||||
return publicKey.subarray(1);
|
||||
}
|
||||
if (!hasBigInt || (!onlyJs && isReady())) {
|
||||
return wasm(publicKey).subarray(1);
|
||||
}
|
||||
const { px, py } = secp256k1.ProjectivePoint.fromHex(publicKey);
|
||||
return u8aConcat(bnToU8a(px, BN_BE_256_OPTS), bnToU8a(py, BN_BE_256_OPTS));
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { HashType } from './types.js';
|
||||
export declare function hasher(hashType: HashType, data: Uint8Array | string, onlyJs?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,7 @@
|
||||
import { blake2AsU8a } from '../blake2/index.js';
|
||||
import { keccakAsU8a } from '../keccak/index.js';
|
||||
export function hasher(hashType, data, onlyJs) {
|
||||
return hashType === 'keccak'
|
||||
? keccakAsU8a(data, undefined, onlyJs)
|
||||
: blake2AsU8a(data, undefined, undefined, onlyJs);
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
export { secp256k1Compress } from './compress.js';
|
||||
export { secp256k1Expand } from './expand.js';
|
||||
export { secp256k1PairFromSeed } from './pair/fromSeed.js';
|
||||
export { secp256k1Recover } from './recover.js';
|
||||
export { secp256k1Sign } from './sign.js';
|
||||
export { secp256k1PrivateKeyTweakAdd } from './tweakAdd.js';
|
||||
export { secp256k1Verify } from './verify.js';
|
||||
@@ -0,0 +1,7 @@
|
||||
export { secp256k1Compress } from './compress.js';
|
||||
export { secp256k1Expand } from './expand.js';
|
||||
export { secp256k1PairFromSeed } from './pair/fromSeed.js';
|
||||
export { secp256k1Recover } from './recover.js';
|
||||
export { secp256k1Sign } from './sign.js';
|
||||
export { secp256k1PrivateKeyTweakAdd } from './tweakAdd.js';
|
||||
export { secp256k1Verify } from './verify.js';
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { Keypair } from '../../types.js';
|
||||
/**
|
||||
* @name secp256k1PairFromSeed
|
||||
* @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
*/
|
||||
export declare function secp256k1PairFromSeed(seed: Uint8Array, onlyJs?: boolean): Keypair;
|
||||
@@ -0,0 +1,31 @@
|
||||
import { secp256k1 } from '@noble/curves/secp256k1';
|
||||
import { hasBigInt, u8aEmpty } from '@pezkuwi/util';
|
||||
import { isReady, secp256k1FromSeed } from '@pezkuwi/wasm-crypto';
|
||||
/**
|
||||
* @name secp256k1PairFromSeed
|
||||
* @description Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed.
|
||||
*/
|
||||
export function secp256k1PairFromSeed(seed, onlyJs) {
|
||||
if (seed.length !== 32) {
|
||||
throw new Error('Expected valid 32-byte private key as a seed');
|
||||
}
|
||||
if (!hasBigInt || (!onlyJs && isReady())) {
|
||||
const full = secp256k1FromSeed(seed);
|
||||
const publicKey = full.slice(32);
|
||||
// There is an issue with the secp256k1 when running in an ASM.js environment where
|
||||
// it seems that the lazy static section yields invalid results on the _first_ run.
|
||||
// If this happens, fail outright, we cannot allow invalid return values
|
||||
// https://github.com/polkadot-js/wasm/issues/307
|
||||
if (u8aEmpty(publicKey)) {
|
||||
throw new Error('Invalid publicKey generated from WASM interface');
|
||||
}
|
||||
return {
|
||||
publicKey,
|
||||
secretKey: full.slice(0, 32)
|
||||
};
|
||||
}
|
||||
return {
|
||||
publicKey: secp256k1.getPublicKey(seed, true),
|
||||
secretKey: seed
|
||||
};
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import type { HashType } from './types.js';
|
||||
/**
|
||||
* @name secp256k1Recover
|
||||
* @description Recovers a publicKey from the supplied signature
|
||||
*/
|
||||
export declare function secp256k1Recover(msgHash: string | Uint8Array, signature: string | Uint8Array, recovery: number, hashType?: HashType, onlyJs?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,26 @@
|
||||
import { secp256k1 } from '@noble/curves/secp256k1';
|
||||
import { hasBigInt, u8aToU8a } from '@pezkuwi/util';
|
||||
import { isReady, secp256k1Recover as wasm } from '@pezkuwi/wasm-crypto';
|
||||
import { secp256k1Compress } from './compress.js';
|
||||
import { secp256k1Expand } from './expand.js';
|
||||
/**
|
||||
* @name secp256k1Recover
|
||||
* @description Recovers a publicKey from the supplied signature
|
||||
*/
|
||||
export function secp256k1Recover(msgHash, signature, recovery, hashType = 'blake2', onlyJs) {
|
||||
const sig = u8aToU8a(signature).subarray(0, 64);
|
||||
const msg = u8aToU8a(msgHash);
|
||||
const publicKey = !hasBigInt || (!onlyJs && isReady())
|
||||
? wasm(msg, sig, recovery)
|
||||
: secp256k1.Signature
|
||||
.fromCompact(sig)
|
||||
.addRecoveryBit(recovery)
|
||||
.recoverPublicKey(msg)
|
||||
.toRawBytes();
|
||||
if (!publicKey) {
|
||||
throw new Error('Unable to recover publicKey from signature');
|
||||
}
|
||||
return hashType === 'keccak'
|
||||
? secp256k1Expand(publicKey, onlyJs)
|
||||
: secp256k1Compress(publicKey, onlyJs);
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import type { Keypair } from '../types.js';
|
||||
import type { HashType } from './types.js';
|
||||
/**
|
||||
* @name secp256k1Sign
|
||||
* @description Returns message signature of `message`, using the supplied pair
|
||||
*/
|
||||
export declare function secp256k1Sign(message: Uint8Array | string, { secretKey }: Partial<Keypair>, hashType?: HashType, onlyJs?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { secp256k1 } from '@noble/curves/secp256k1';
|
||||
import { bnToU8a, hasBigInt, u8aConcat } from '@pezkuwi/util';
|
||||
import { isReady, secp256k1Sign as wasm } from '@pezkuwi/wasm-crypto';
|
||||
import { BN_BE_256_OPTS } from '../bn.js';
|
||||
import { hasher } from './hasher.js';
|
||||
/**
|
||||
* @name secp256k1Sign
|
||||
* @description Returns message signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function secp256k1Sign(message, { secretKey }, hashType = 'blake2', onlyJs) {
|
||||
if (secretKey?.length !== 32) {
|
||||
throw new Error('Expected valid secp256k1 secretKey, 32-bytes');
|
||||
}
|
||||
const data = hasher(hashType, message, onlyJs);
|
||||
if (!hasBigInt || (!onlyJs && isReady())) {
|
||||
return wasm(data, secretKey);
|
||||
}
|
||||
const signature = secp256k1.sign(data, secretKey, { lowS: true });
|
||||
return u8aConcat(bnToU8a(signature.r, BN_BE_256_OPTS), bnToU8a(signature.s, BN_BE_256_OPTS), new Uint8Array([signature.recovery || 0]));
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export declare function secp256k1PrivateKeyTweakAdd(seckey: Uint8Array, tweak: Uint8Array, onlyBn?: boolean): Uint8Array;
|
||||
@@ -0,0 +1,45 @@
|
||||
import { _0n, BN, bnToU8a, hasBigInt, isU8a, nToU8a, u8aToBigInt } from '@pezkuwi/util';
|
||||
import { BigInt } from '@pezkuwi/x-bigint';
|
||||
import { BN_BE_256_OPTS, BN_BE_OPTS } from '../bn.js';
|
||||
const N = 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141'.replace(/ /g, '');
|
||||
const N_BI = BigInt(`0x${N}`);
|
||||
const N_BN = new BN(N, 'hex');
|
||||
function addBi(seckey, tweak) {
|
||||
let res = u8aToBigInt(tweak, BN_BE_OPTS);
|
||||
if (res >= N_BI) {
|
||||
throw new Error('Tweak parameter is out of range');
|
||||
}
|
||||
res += u8aToBigInt(seckey, BN_BE_OPTS);
|
||||
if (res >= N_BI) {
|
||||
res -= N_BI;
|
||||
}
|
||||
if (res === _0n) {
|
||||
throw new Error('Invalid resulting private key');
|
||||
}
|
||||
return nToU8a(res, BN_BE_256_OPTS);
|
||||
}
|
||||
function addBn(seckey, tweak) {
|
||||
const res = new BN(tweak);
|
||||
if (res.cmp(N_BN) >= 0) {
|
||||
throw new Error('Tweak parameter is out of range');
|
||||
}
|
||||
res.iadd(new BN(seckey));
|
||||
if (res.cmp(N_BN) >= 0) {
|
||||
res.isub(N_BN);
|
||||
}
|
||||
if (res.isZero()) {
|
||||
throw new Error('Invalid resulting private key');
|
||||
}
|
||||
return bnToU8a(res, BN_BE_256_OPTS);
|
||||
}
|
||||
export function secp256k1PrivateKeyTweakAdd(seckey, tweak, onlyBn) {
|
||||
if (!isU8a(seckey) || seckey.length !== 32) {
|
||||
throw new Error('Expected seckey to be an Uint8Array with length 32');
|
||||
}
|
||||
else if (!isU8a(tweak) || tweak.length !== 32) {
|
||||
throw new Error('Expected tweak to be an Uint8Array with length 32');
|
||||
}
|
||||
return !hasBigInt || onlyBn
|
||||
? addBn(seckey, tweak)
|
||||
: addBi(seckey, tweak);
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export type HashType = 'blake2' | 'keccak';
|
||||
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import type { HashType } from './types.js';
|
||||
/**
|
||||
* @name secp256k1Verify
|
||||
* @description Verifies the signature of `message`, using the supplied pair
|
||||
*/
|
||||
export declare function secp256k1Verify(msgHash: string | Uint8Array, signature: string | Uint8Array, address: string | Uint8Array, hashType?: HashType, onlyJs?: boolean): boolean;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { u8aEq, u8aToU8a } from '@pezkuwi/util';
|
||||
import { hasher } from './hasher.js';
|
||||
import { secp256k1Recover } from './recover.js';
|
||||
/**
|
||||
* @name secp256k1Verify
|
||||
* @description Verifies the signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function secp256k1Verify(msgHash, signature, address, hashType = 'blake2', onlyJs) {
|
||||
const sig = u8aToU8a(signature);
|
||||
if (sig.length !== 65) {
|
||||
throw new Error(`Expected signature with 65 bytes, ${sig.length} found instead`);
|
||||
}
|
||||
const publicKey = secp256k1Recover(hasher(hashType, msgHash), sig, sig[64], hashType, onlyJs);
|
||||
const signerAddr = hasher(hashType, publicKey, onlyJs);
|
||||
const inputAddr = u8aToU8a(address);
|
||||
// for Ethereum (keccak) the last 20 bytes is the address
|
||||
return u8aEq(publicKey, inputAddr) || (hashType === 'keccak'
|
||||
? u8aEq(signerAddr.slice(-20), inputAddr.slice(-20))
|
||||
: u8aEq(signerAddr, inputAddr));
|
||||
}
|
||||
Reference in New Issue
Block a user