mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-29 07:58:00 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
import { BN } from '@pezkuwi/util';
|
||||
export declare class DeriveJunction {
|
||||
#private;
|
||||
static from(value: string): DeriveJunction;
|
||||
get chainCode(): Uint8Array;
|
||||
get isHard(): boolean;
|
||||
get isSoft(): boolean;
|
||||
hard(value: number | string | bigint | BN | Uint8Array): DeriveJunction;
|
||||
harden(): DeriveJunction;
|
||||
soft(value: number | string | bigint | BN | Uint8Array): DeriveJunction;
|
||||
soften(): DeriveJunction;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { BN, bnToU8a, compactAddLength, hexToU8a, isBigInt, isBn, isHex, isNumber, isString, stringToU8a } from '@pezkuwi/util';
|
||||
import { blake2AsU8a } from '../blake2/asU8a.js';
|
||||
import { BN_LE_256_OPTS } from '../bn.js';
|
||||
const RE_NUMBER = /^\d+$/;
|
||||
const JUNCTION_ID_LEN = 32;
|
||||
export class DeriveJunction {
|
||||
#chainCode = new Uint8Array(32);
|
||||
#isHard = false;
|
||||
static from(value) {
|
||||
const result = new DeriveJunction();
|
||||
const [code, isHard] = value.startsWith('/')
|
||||
? [value.substring(1), true]
|
||||
: [value, false];
|
||||
result.soft(RE_NUMBER.test(code)
|
||||
? new BN(code, 10)
|
||||
: code);
|
||||
return isHard
|
||||
? result.harden()
|
||||
: result;
|
||||
}
|
||||
get chainCode() {
|
||||
return this.#chainCode;
|
||||
}
|
||||
get isHard() {
|
||||
return this.#isHard;
|
||||
}
|
||||
get isSoft() {
|
||||
return !this.#isHard;
|
||||
}
|
||||
hard(value) {
|
||||
return this.soft(value).harden();
|
||||
}
|
||||
harden() {
|
||||
this.#isHard = true;
|
||||
return this;
|
||||
}
|
||||
soft(value) {
|
||||
if (isNumber(value) || isBn(value) || isBigInt(value)) {
|
||||
return this.soft(bnToU8a(value, BN_LE_256_OPTS));
|
||||
}
|
||||
else if (isHex(value)) {
|
||||
return this.soft(hexToU8a(value));
|
||||
}
|
||||
else if (isString(value)) {
|
||||
return this.soft(compactAddLength(stringToU8a(value)));
|
||||
}
|
||||
else if (value.length > JUNCTION_ID_LEN) {
|
||||
return this.soft(blake2AsU8a(value));
|
||||
}
|
||||
this.#chainCode.fill(0);
|
||||
this.#chainCode.set(value, 0);
|
||||
return this;
|
||||
}
|
||||
soften() {
|
||||
this.#isHard = false;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import { DeriveJunction } from './DeriveJunction.js';
|
||||
export interface ExtractResult {
|
||||
parts: string[] | null;
|
||||
path: DeriveJunction[];
|
||||
}
|
||||
/**
|
||||
* @description Extract derivation junctions from the supplied path
|
||||
*/
|
||||
export declare function keyExtractPath(derivePath: string): ExtractResult;
|
||||
@@ -0,0 +1,23 @@
|
||||
import { DeriveJunction } from './DeriveJunction.js';
|
||||
const RE_JUNCTION = /\/(\/?)([^/]+)/g;
|
||||
/**
|
||||
* @description Extract derivation junctions from the supplied path
|
||||
*/
|
||||
export function keyExtractPath(derivePath) {
|
||||
const parts = derivePath.match(RE_JUNCTION);
|
||||
const path = [];
|
||||
let constructed = '';
|
||||
if (parts) {
|
||||
constructed = parts.join('');
|
||||
for (const p of parts) {
|
||||
path.push(DeriveJunction.from(p.substring(1)));
|
||||
}
|
||||
}
|
||||
if (constructed !== derivePath) {
|
||||
throw new Error(`Re-constructed path "${constructed}" does not match input`);
|
||||
}
|
||||
return {
|
||||
parts,
|
||||
path
|
||||
};
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import type { DeriveJunction } from './DeriveJunction.js';
|
||||
export interface ExtractResult {
|
||||
derivePath: string;
|
||||
password?: string;
|
||||
path: DeriveJunction[];
|
||||
phrase: string;
|
||||
}
|
||||
/**
|
||||
* @description Extracts the phrase, path and password from a SURI format for specifying secret keys `<secret>/<soft-key>//<hard-key>///<password>` (the `///password` may be omitted, and `/<soft-key>` and `//<hard-key>` maybe repeated and mixed).
|
||||
*/
|
||||
export declare function keyExtractSuri(suri: string): ExtractResult;
|
||||
@@ -0,0 +1,22 @@
|
||||
import { keyExtractPath } from './extractPath.js';
|
||||
const RE_CAPTURE = /^((0x[a-fA-F0-9]+|[\p{L}\d]+(?: [\p{L}\d]+)*))((\/\/?[^/]+)*)(\/\/\/(.*))?$/u;
|
||||
/**
|
||||
* @description Extracts the phrase, path and password from a SURI format for specifying secret keys `<secret>/<soft-key>//<hard-key>///<password>` (the `///password` may be omitted, and `/<soft-key>` and `//<hard-key>` maybe repeated and mixed).
|
||||
*/
|
||||
export function keyExtractSuri(suri) {
|
||||
// Normalize Unicode to NFC to avoid accent-related mismatches
|
||||
const normalizedSuri = suri.normalize('NFC');
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
|
||||
const matches = normalizedSuri.match(RE_CAPTURE);
|
||||
if (matches === null) {
|
||||
throw new Error('Unable to match provided value to a secret URI');
|
||||
}
|
||||
const [, phrase, , derivePath, , , password] = matches;
|
||||
const { path } = keyExtractPath(derivePath);
|
||||
return {
|
||||
derivePath,
|
||||
password,
|
||||
path,
|
||||
phrase
|
||||
};
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import type { Keypair, KeypairType } from '../types.js';
|
||||
import type { DeriveJunction } from './DeriveJunction.js';
|
||||
export declare function keyFromPath(pair: Keypair, path: DeriveJunction[], type: KeypairType): Keypair;
|
||||
@@ -0,0 +1,18 @@
|
||||
import { keyHdkdEcdsa } from './hdkdEcdsa.js';
|
||||
import { keyHdkdEd25519 } from './hdkdEd25519.js';
|
||||
import { keyHdkdSr25519 } from './hdkdSr25519.js';
|
||||
const generators = {
|
||||
ecdsa: keyHdkdEcdsa,
|
||||
ed25519: keyHdkdEd25519,
|
||||
// FIXME This is Substrate-compatible, not Ethereum-compatible
|
||||
ethereum: keyHdkdEcdsa,
|
||||
sr25519: keyHdkdSr25519
|
||||
};
|
||||
export function keyFromPath(pair, path, type) {
|
||||
const keyHdkd = generators[type];
|
||||
let result = pair;
|
||||
for (const junction of path) {
|
||||
result = keyHdkd(result, junction);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import type { Keypair } from '../types.js';
|
||||
import type { DeriveJunction } from './DeriveJunction.js';
|
||||
export declare function createSeedDeriveFn(fromSeed: (seed: Uint8Array) => Keypair, derive: (seed: Uint8Array, chainCode: Uint8Array) => Uint8Array): (keypair: Keypair, junction: DeriveJunction) => Keypair;
|
||||
@@ -0,0 +1,8 @@
|
||||
export function createSeedDeriveFn(fromSeed, derive) {
|
||||
return (keypair, { chainCode, isHard }) => {
|
||||
if (!isHard) {
|
||||
throw new Error('A soft key was found in the path and is not supported');
|
||||
}
|
||||
return fromSeed(derive(keypair.secretKey.subarray(0, 32), chainCode));
|
||||
};
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare const keyHdkdEcdsa: (keypair: import("../types.js").Keypair, junction: import("./DeriveJunction.js").DeriveJunction) => import("../types.js").Keypair;
|
||||
@@ -0,0 +1,4 @@
|
||||
import { secp256k1DeriveHard } from '../secp256k1/deriveHard.js';
|
||||
import { secp256k1PairFromSeed } from '../secp256k1/pair/fromSeed.js';
|
||||
import { createSeedDeriveFn } from './hdkdDerive.js';
|
||||
export const keyHdkdEcdsa = /*#__PURE__*/ createSeedDeriveFn(secp256k1PairFromSeed, secp256k1DeriveHard);
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare const keyHdkdEd25519: (keypair: import("../types.js").Keypair, junction: import("./DeriveJunction.js").DeriveJunction) => import("../types.js").Keypair;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { ed25519DeriveHard, ed25519PairFromSeed } from '../ed25519/index.js';
|
||||
import { createSeedDeriveFn } from './hdkdDerive.js';
|
||||
export const keyHdkdEd25519 = /*#__PURE__*/ createSeedDeriveFn(ed25519PairFromSeed, ed25519DeriveHard);
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import type { Keypair } from '../types.js';
|
||||
import type { DeriveJunction } from './DeriveJunction.js';
|
||||
export declare function keyHdkdSr25519(keypair: Keypair, { chainCode, isSoft }: DeriveJunction): Keypair;
|
||||
@@ -0,0 +1,7 @@
|
||||
import { sr25519DeriveHard } from '../sr25519/deriveHard.js';
|
||||
import { sr25519DeriveSoft } from '../sr25519/deriveSoft.js';
|
||||
export function keyHdkdSr25519(keypair, { chainCode, isSoft }) {
|
||||
return isSoft
|
||||
? sr25519DeriveSoft(keypair, chainCode)
|
||||
: sr25519DeriveHard(keypair, chainCode);
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @summary Create keys from paths, seeds and password
|
||||
*/
|
||||
export { keyExtractPath } from './extractPath.js';
|
||||
export { keyExtractSuri } from './extractSuri.js';
|
||||
export { keyFromPath } from './fromPath.js';
|
||||
export { keyHdkdEcdsa } from './hdkdEcdsa.js';
|
||||
export { keyHdkdEd25519 } from './hdkdEd25519.js';
|
||||
export { keyHdkdSr25519 } from './hdkdSr25519.js';
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @summary Create keys from paths, seeds and password
|
||||
*/
|
||||
export { keyExtractPath } from './extractPath.js';
|
||||
export { keyExtractSuri } from './extractSuri.js';
|
||||
export { keyFromPath } from './fromPath.js';
|
||||
export { keyHdkdEcdsa } from './hdkdEcdsa.js';
|
||||
export { keyHdkdEd25519 } from './hdkdEd25519.js';
|
||||
export { keyHdkdSr25519 } from './hdkdSr25519.js';
|
||||
Reference in New Issue
Block a user