mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-06-16 23:41:03 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
import type { Keypair } from '../../types.js';
|
||||
export declare function hdEthereum(seed: Uint8Array, path?: string): Keypair;
|
||||
@@ -0,0 +1,44 @@
|
||||
import { bnToU8a, stringToU8a, u8aConcat } from '@pezkuwi/util';
|
||||
import { BN_BE_32_OPTS } from '../../bn.js';
|
||||
import { hmacShaAsU8a } from '../../hmac/index.js';
|
||||
import { secp256k1PairFromSeed, secp256k1PrivateKeyTweakAdd } from '../../secp256k1/index.js';
|
||||
import { HARDENED, hdValidatePath } from '../validatePath.js';
|
||||
const MASTER_SECRET = stringToU8a('Bitcoin seed');
|
||||
function createCoded(secretKey, chainCode) {
|
||||
return {
|
||||
chainCode,
|
||||
publicKey: secp256k1PairFromSeed(secretKey).publicKey,
|
||||
secretKey
|
||||
};
|
||||
}
|
||||
function deriveChild(hd, index) {
|
||||
const indexBuffer = bnToU8a(index, BN_BE_32_OPTS);
|
||||
const data = index >= HARDENED
|
||||
? u8aConcat(new Uint8Array(1), hd.secretKey, indexBuffer)
|
||||
: u8aConcat(hd.publicKey, indexBuffer);
|
||||
try {
|
||||
const I = hmacShaAsU8a(hd.chainCode, data, 512);
|
||||
return createCoded(secp256k1PrivateKeyTweakAdd(hd.secretKey, I.slice(0, 32)), I.slice(32));
|
||||
}
|
||||
catch {
|
||||
// In case parse256(IL) >= n or ki == 0, proceed with the next value for i
|
||||
return deriveChild(hd, index + 1);
|
||||
}
|
||||
}
|
||||
export function hdEthereum(seed, path = '') {
|
||||
const I = hmacShaAsU8a(MASTER_SECRET, seed, 512);
|
||||
let hd = createCoded(I.slice(0, 32), I.slice(32));
|
||||
if (!path || path === 'm' || path === 'M' || path === "m'" || path === "M'") {
|
||||
return hd;
|
||||
}
|
||||
if (!hdValidatePath(path)) {
|
||||
throw new Error('Invalid derivation path');
|
||||
}
|
||||
const parts = path.split('/').slice(1);
|
||||
for (const p of parts) {
|
||||
hd = deriveChild(hd, parseInt(p, 10) + ((p.length > 1) && p.endsWith("'")
|
||||
? HARDENED
|
||||
: 0));
|
||||
}
|
||||
return hd;
|
||||
}
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
export { hdEthereum } from './ethereum/index.js';
|
||||
export { hdLedger } from './ledger/index.js';
|
||||
export { hdValidatePath } from './validatePath.js';
|
||||
@@ -0,0 +1,3 @@
|
||||
export { hdEthereum } from './ethereum/index.js';
|
||||
export { hdLedger } from './ledger/index.js';
|
||||
export { hdValidatePath } from './validatePath.js';
|
||||
@@ -0,0 +1 @@
|
||||
export declare function ledgerDerivePrivate(xprv: Uint8Array, index: number): Uint8Array;
|
||||
@@ -0,0 +1,12 @@
|
||||
import { BN_EIGHT, bnToU8a, u8aConcat, u8aToBn } from '@pezkuwi/util';
|
||||
import { BN_LE_32_OPTS, BN_LE_512_OPTS, BN_LE_OPTS } from '../../bn.js';
|
||||
import { hmacShaAsU8a } from '../../hmac/index.js';
|
||||
export function ledgerDerivePrivate(xprv, index) {
|
||||
const kl = xprv.subarray(0, 32);
|
||||
const kr = xprv.subarray(32, 64);
|
||||
const cc = xprv.subarray(64, 96);
|
||||
const data = u8aConcat([0], kl, kr, bnToU8a(index, BN_LE_32_OPTS));
|
||||
const z = hmacShaAsU8a(cc, data, 512);
|
||||
data[0] = 0x01;
|
||||
return u8aConcat(bnToU8a(u8aToBn(kl, BN_LE_OPTS).iadd(u8aToBn(z.subarray(0, 28), BN_LE_OPTS).imul(BN_EIGHT)), BN_LE_512_OPTS).subarray(0, 32), bnToU8a(u8aToBn(kr, BN_LE_OPTS).iadd(u8aToBn(z.subarray(32, 64), BN_LE_OPTS)), BN_LE_512_OPTS).subarray(0, 32), hmacShaAsU8a(cc, data, 512).subarray(32, 64));
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { Keypair } from '../../types.js';
|
||||
export declare function hdLedger(_mnemonic: string, path: string): Keypair;
|
||||
@@ -0,0 +1,30 @@
|
||||
import { ed25519PairFromSeed } from '../../ed25519/index.js';
|
||||
import { mnemonicValidate } from '../../mnemonic/index.js';
|
||||
import { HARDENED, hdValidatePath } from '../validatePath.js';
|
||||
import { ledgerDerivePrivate } from './derivePrivate.js';
|
||||
import { ledgerMaster } from './master.js';
|
||||
export function hdLedger(_mnemonic, path) {
|
||||
const words = _mnemonic
|
||||
.split(' ')
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => s);
|
||||
if (![12, 24, 25].includes(words.length)) {
|
||||
throw new Error('Expected a mnemonic with 24 words (or 25 including a password)');
|
||||
}
|
||||
const [mnemonic, password] = words.length === 25
|
||||
? [words.slice(0, 24).join(' '), words[24]]
|
||||
: [words.join(' '), ''];
|
||||
if (!mnemonicValidate(mnemonic)) {
|
||||
throw new Error('Invalid mnemonic passed to ledger derivation');
|
||||
}
|
||||
else if (!hdValidatePath(path)) {
|
||||
throw new Error('Invalid derivation path');
|
||||
}
|
||||
const parts = path.split('/').slice(1);
|
||||
let seed = ledgerMaster(mnemonic, password);
|
||||
for (const p of parts) {
|
||||
const n = parseInt(p.replace(/'$/, ''), 10);
|
||||
seed = ledgerDerivePrivate(seed, (n < HARDENED) ? (n + HARDENED) : n);
|
||||
}
|
||||
return ed25519PairFromSeed(seed.slice(0, 32));
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function ledgerMaster(mnemonic: string, password?: string): Uint8Array;
|
||||
@@ -0,0 +1,16 @@
|
||||
import { u8aConcat } from '@pezkuwi/util';
|
||||
import { hmacShaAsU8a } from '../../hmac/index.js';
|
||||
import { mnemonicToSeedSync } from '../../mnemonic/bip39.js';
|
||||
const ED25519_CRYPTO = 'ed25519 seed';
|
||||
export function ledgerMaster(mnemonic, password) {
|
||||
const seed = mnemonicToSeedSync(mnemonic, password);
|
||||
const chainCode = hmacShaAsU8a(ED25519_CRYPTO, new Uint8Array([1, ...seed]), 256);
|
||||
let priv;
|
||||
while (!priv || (priv[31] & 0b0010_0000)) {
|
||||
priv = hmacShaAsU8a(ED25519_CRYPTO, priv || seed, 512);
|
||||
}
|
||||
priv[0] &= 0b1111_1000;
|
||||
priv[31] &= 0b0111_1111;
|
||||
priv[31] |= 0b0100_0000;
|
||||
return u8aConcat(priv, chainCode);
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export declare const HARDENED = 2147483648;
|
||||
export declare function hdValidatePath(path: string): boolean;
|
||||
@@ -0,0 +1,16 @@
|
||||
export const HARDENED = 0x80000000;
|
||||
export function hdValidatePath(path) {
|
||||
if (!path.startsWith('m/')) {
|
||||
return false;
|
||||
}
|
||||
const parts = path.split('/').slice(1);
|
||||
for (const p of parts) {
|
||||
const n = /^\d+'?$/.test(p)
|
||||
? parseInt(p.replace(/'$/, ''), 10)
|
||||
: Number.NaN;
|
||||
if (isNaN(n) || (n >= HARDENED) || (n < 0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user