mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-30 14:17:57 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
@@ -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,62 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.DeriveJunction = void 0;
|
||||
const util_1 = require("@pezkuwi/util");
|
||||
const asU8a_js_1 = require("../blake2/asU8a.js");
|
||||
const bn_js_1 = require("../bn.js");
|
||||
const RE_NUMBER = /^\d+$/;
|
||||
const JUNCTION_ID_LEN = 32;
|
||||
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 util_1.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 ((0, util_1.isNumber)(value) || (0, util_1.isBn)(value) || (0, util_1.isBigInt)(value)) {
|
||||
return this.soft((0, util_1.bnToU8a)(value, bn_js_1.BN_LE_256_OPTS));
|
||||
}
|
||||
else if ((0, util_1.isHex)(value)) {
|
||||
return this.soft((0, util_1.hexToU8a)(value));
|
||||
}
|
||||
else if ((0, util_1.isString)(value)) {
|
||||
return this.soft((0, util_1.compactAddLength)((0, util_1.stringToU8a)(value)));
|
||||
}
|
||||
else if (value.length > JUNCTION_ID_LEN) {
|
||||
return this.soft((0, asU8a_js_1.blake2AsU8a)(value));
|
||||
}
|
||||
this.#chainCode.fill(0);
|
||||
this.#chainCode.set(value, 0);
|
||||
return this;
|
||||
}
|
||||
soften() {
|
||||
this.#isHard = false;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
exports.DeriveJunction = DeriveJunction;
|
||||
@@ -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,26 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.keyExtractPath = keyExtractPath;
|
||||
const DeriveJunction_js_1 = require("./DeriveJunction.js");
|
||||
const RE_JUNCTION = /\/(\/?)([^/]+)/g;
|
||||
/**
|
||||
* @description Extract derivation junctions from the supplied path
|
||||
*/
|
||||
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_js_1.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,25 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.keyExtractSuri = keyExtractSuri;
|
||||
const extractPath_js_1 = require("./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).
|
||||
*/
|
||||
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 } = (0, extractPath_js_1.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,21 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.keyFromPath = keyFromPath;
|
||||
const hdkdEcdsa_js_1 = require("./hdkdEcdsa.js");
|
||||
const hdkdEd25519_js_1 = require("./hdkdEd25519.js");
|
||||
const hdkdSr25519_js_1 = require("./hdkdSr25519.js");
|
||||
const generators = {
|
||||
ecdsa: hdkdEcdsa_js_1.keyHdkdEcdsa,
|
||||
ed25519: hdkdEd25519_js_1.keyHdkdEd25519,
|
||||
// FIXME This is Substrate-compatible, not Ethereum-compatible
|
||||
ethereum: hdkdEcdsa_js_1.keyHdkdEcdsa,
|
||||
sr25519: hdkdSr25519_js_1.keyHdkdSr25519
|
||||
};
|
||||
function keyFromPath(pair, path, type) {
|
||||
const keyHdkd = generators[type];
|
||||
let result = pair;
|
||||
for (const junction of path) {
|
||||
result = keyHdkd(result, junction);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -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,11 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createSeedDeriveFn = createSeedDeriveFn;
|
||||
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,7 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.keyHdkdEcdsa = void 0;
|
||||
const deriveHard_js_1 = require("../secp256k1/deriveHard.js");
|
||||
const fromSeed_js_1 = require("../secp256k1/pair/fromSeed.js");
|
||||
const hdkdDerive_js_1 = require("./hdkdDerive.js");
|
||||
exports.keyHdkdEcdsa = (0, hdkdDerive_js_1.createSeedDeriveFn)(fromSeed_js_1.secp256k1PairFromSeed, deriveHard_js_1.secp256k1DeriveHard);
|
||||
@@ -0,0 +1 @@
|
||||
export declare const keyHdkdEd25519: (keypair: import("../types.js").Keypair, junction: import("./DeriveJunction.js").DeriveJunction) => import("../types.js").Keypair;
|
||||
@@ -0,0 +1,6 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.keyHdkdEd25519 = void 0;
|
||||
const index_js_1 = require("../ed25519/index.js");
|
||||
const hdkdDerive_js_1 = require("./hdkdDerive.js");
|
||||
exports.keyHdkdEd25519 = (0, hdkdDerive_js_1.createSeedDeriveFn)(index_js_1.ed25519PairFromSeed, index_js_1.ed25519DeriveHard);
|
||||
@@ -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,10 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.keyHdkdSr25519 = keyHdkdSr25519;
|
||||
const deriveHard_js_1 = require("../sr25519/deriveHard.js");
|
||||
const deriveSoft_js_1 = require("../sr25519/deriveSoft.js");
|
||||
function keyHdkdSr25519(keypair, { chainCode, isSoft }) {
|
||||
return isSoft
|
||||
? (0, deriveSoft_js_1.sr25519DeriveSoft)(keypair, chainCode)
|
||||
: (0, deriveHard_js_1.sr25519DeriveHard)(keypair, chainCode);
|
||||
}
|
||||
+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,18 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.keyHdkdSr25519 = exports.keyHdkdEd25519 = exports.keyHdkdEcdsa = exports.keyFromPath = exports.keyExtractSuri = exports.keyExtractPath = void 0;
|
||||
/**
|
||||
* @summary Create keys from paths, seeds and password
|
||||
*/
|
||||
var extractPath_js_1 = require("./extractPath.js");
|
||||
Object.defineProperty(exports, "keyExtractPath", { enumerable: true, get: function () { return extractPath_js_1.keyExtractPath; } });
|
||||
var extractSuri_js_1 = require("./extractSuri.js");
|
||||
Object.defineProperty(exports, "keyExtractSuri", { enumerable: true, get: function () { return extractSuri_js_1.keyExtractSuri; } });
|
||||
var fromPath_js_1 = require("./fromPath.js");
|
||||
Object.defineProperty(exports, "keyFromPath", { enumerable: true, get: function () { return fromPath_js_1.keyFromPath; } });
|
||||
var hdkdEcdsa_js_1 = require("./hdkdEcdsa.js");
|
||||
Object.defineProperty(exports, "keyHdkdEcdsa", { enumerable: true, get: function () { return hdkdEcdsa_js_1.keyHdkdEcdsa; } });
|
||||
var hdkdEd25519_js_1 = require("./hdkdEd25519.js");
|
||||
Object.defineProperty(exports, "keyHdkdEd25519", { enumerable: true, get: function () { return hdkdEd25519_js_1.keyHdkdEd25519; } });
|
||||
var hdkdSr25519_js_1 = require("./hdkdSr25519.js");
|
||||
Object.defineProperty(exports, "keyHdkdSr25519", { enumerable: true, get: function () { return hdkdSr25519_js_1.keyHdkdSr25519; } });
|
||||
Reference in New Issue
Block a user