@leather.io/crypto 1.7.4 → 1.7.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +8 -3
- package/dist/index.mjs +12 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -5,13 +5,14 @@ declare enum DerivationPathDepth {
|
|
|
5
5
|
Purpose = 1,
|
|
6
6
|
CoinType = 2,
|
|
7
7
|
Account = 3,
|
|
8
|
-
|
|
8
|
+
Change = 4,
|
|
9
9
|
AddressIndex = 5
|
|
10
10
|
}
|
|
11
11
|
declare const extractPurposeFromPath: (path: string) => number;
|
|
12
12
|
declare const extractAccountIndexFromPath: (path: string) => number;
|
|
13
|
+
declare const extractChangeIndexFromPath: (path: string) => number;
|
|
13
14
|
declare const extractAddressIndexFromPath: (path: string) => number;
|
|
14
|
-
declare function appendAddressIndexToPath(path: string,
|
|
15
|
+
declare function appendAddressIndexToPath(path: string, change: number, addressIndex: number): string;
|
|
15
16
|
declare function extractFingerprintFromKeyOriginPath(keyOriginPath: string): string;
|
|
16
17
|
/**
|
|
17
18
|
* @description
|
|
@@ -49,6 +50,10 @@ declare function extractAccountIndexFromDescriptor(descriptor: string): number;
|
|
|
49
50
|
* @example `[0a3fd8ef/84'/0'/6']xpuba1b…2c3` -> `xpuba1b…2c3`
|
|
50
51
|
*/
|
|
51
52
|
declare function extractKeyFromDescriptor(descriptor: string): string;
|
|
53
|
+
/**
|
|
54
|
+
* @example `0a3fd8ef/84'/0'/0/0/0` -> `0a3fd8ef/84'/0'/0`
|
|
55
|
+
*/
|
|
56
|
+
declare function extractAccountPathFromFullPath(path: string): string;
|
|
52
57
|
declare function keyOriginToDerivationPath(keyOrigin: string): string;
|
|
53
58
|
declare function decomposeDescriptor(descriptor: string): {
|
|
54
59
|
descriptor: string;
|
|
@@ -83,4 +88,4 @@ interface Signer {
|
|
|
83
88
|
derivationPath: string;
|
|
84
89
|
}
|
|
85
90
|
|
|
86
|
-
export { DerivationPathDepth, type Signer, appendAddressIndexToPath, createDescriptor, createKeyOriginPath, decomposeDescriptor, deriveBip39MnemonicFromSeed, deriveBip39SeedFromMnemonic, deriveChildKeychainFromMnemnonic, deriveKeychainExtendedPublicKeyDescriptor, deriveKeychainFromXpub, deriveRootBip32Keychain, deriveRootKeychainFromMnemonic, extractAccountIndexFromDescriptor, extractAccountIndexFromPath, extractAddressIndexFromPath, extractDerivationPathFromDescriptor, extractFingerprintFromDescriptor, extractFingerprintFromKeyOriginPath, extractKeyFromDescriptor, extractKeyOriginPathFromDescriptor, extractPurposeFromPath, generateMnemonic, getMnemonicRootKeyFingerprint, isValidMnemonic, isValidMnemonicWord, keyOriginToDerivationPath, validateKeyOriginPath };
|
|
91
|
+
export { DerivationPathDepth, type Signer, appendAddressIndexToPath, createDescriptor, createKeyOriginPath, decomposeDescriptor, deriveBip39MnemonicFromSeed, deriveBip39SeedFromMnemonic, deriveChildKeychainFromMnemnonic, deriveKeychainExtendedPublicKeyDescriptor, deriveKeychainFromXpub, deriveRootBip32Keychain, deriveRootKeychainFromMnemonic, extractAccountIndexFromDescriptor, extractAccountIndexFromPath, extractAccountPathFromFullPath, extractAddressIndexFromPath, extractChangeIndexFromPath, extractDerivationPathFromDescriptor, extractFingerprintFromDescriptor, extractFingerprintFromKeyOriginPath, extractKeyFromDescriptor, extractKeyOriginPathFromDescriptor, extractPurposeFromPath, generateMnemonic, getMnemonicRootKeyFingerprint, isValidMnemonic, isValidMnemonicWord, keyOriginToDerivationPath, validateKeyOriginPath };
|
package/dist/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ var DerivationPathDepth = /* @__PURE__ */ ((DerivationPathDepth2) => {
|
|
|
5
5
|
DerivationPathDepth2[DerivationPathDepth2["Purpose"] = 1] = "Purpose";
|
|
6
6
|
DerivationPathDepth2[DerivationPathDepth2["CoinType"] = 2] = "CoinType";
|
|
7
7
|
DerivationPathDepth2[DerivationPathDepth2["Account"] = 3] = "Account";
|
|
8
|
-
DerivationPathDepth2[DerivationPathDepth2["
|
|
8
|
+
DerivationPathDepth2[DerivationPathDepth2["Change"] = 4] = "Change";
|
|
9
9
|
DerivationPathDepth2[DerivationPathDepth2["AddressIndex"] = 5] = "AddressIndex";
|
|
10
10
|
return DerivationPathDepth2;
|
|
11
11
|
})(DerivationPathDepth || {});
|
|
@@ -21,14 +21,16 @@ var extractPurposeFromPath = extractSectionFromDerivationPath(1 /* Purpose */);
|
|
|
21
21
|
var extractAccountIndexFromPath = extractSectionFromDerivationPath(
|
|
22
22
|
3 /* Account */
|
|
23
23
|
);
|
|
24
|
+
var extractChangeIndexFromPath = extractSectionFromDerivationPath(
|
|
25
|
+
4 /* Change */
|
|
26
|
+
);
|
|
24
27
|
var extractAddressIndexFromPath = extractSectionFromDerivationPath(
|
|
25
28
|
5 /* AddressIndex */
|
|
26
29
|
);
|
|
27
|
-
function appendAddressIndexToPath(path,
|
|
30
|
+
function appendAddressIndexToPath(path, change, addressIndex) {
|
|
28
31
|
const accountIndex = extractAccountIndexFromPath(path);
|
|
29
32
|
if (!Number.isInteger(accountIndex)) throw new Error("Invalid path, must have account index");
|
|
30
|
-
|
|
31
|
-
return `${path}/${assumedReceiveChangeIndex}/${index}`;
|
|
33
|
+
return `${path}/${change}/${addressIndex}`;
|
|
32
34
|
}
|
|
33
35
|
function extractFingerprintFromKeyOriginPath(keyOriginPath) {
|
|
34
36
|
const fingerprint = keyOriginPath.split("/")[0];
|
|
@@ -72,6 +74,10 @@ function extractAccountIndexFromDescriptor(descriptor) {
|
|
|
72
74
|
function extractKeyFromDescriptor(descriptor) {
|
|
73
75
|
return descriptor.split("]")[1];
|
|
74
76
|
}
|
|
77
|
+
function extractAccountPathFromFullPath(path) {
|
|
78
|
+
const segments = path.split("/");
|
|
79
|
+
return segments.slice(0, 4).join("/");
|
|
80
|
+
}
|
|
75
81
|
function keyOriginToDerivationPath(keyOrigin) {
|
|
76
82
|
const [_fingerprint, ...remainingPath] = keyOrigin.split("/");
|
|
77
83
|
return `m/${remainingPath.join("/")}`;
|
|
@@ -147,7 +153,9 @@ export {
|
|
|
147
153
|
deriveRootKeychainFromMnemonic,
|
|
148
154
|
extractAccountIndexFromDescriptor,
|
|
149
155
|
extractAccountIndexFromPath,
|
|
156
|
+
extractAccountPathFromFullPath,
|
|
150
157
|
extractAddressIndexFromPath,
|
|
158
|
+
extractChangeIndexFromPath,
|
|
151
159
|
extractDerivationPathFromDescriptor,
|
|
152
160
|
extractFingerprintFromDescriptor,
|
|
153
161
|
extractFingerprintFromKeyOriginPath,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/derivation-path-utils.ts","../src/keychain.ts"],"sourcesContent":["import { isHexString } from '@leather.io/utils';\n\nexport enum DerivationPathDepth {\n Root = 0,\n Purpose = 1,\n CoinType = 2,\n Account = 3,\n ChangeReceive = 4,\n AddressIndex = 5,\n}\n\nfunction extractSectionFromDerivationPath(depth: DerivationPathDepth) {\n return (path: string) => {\n const segments = path.split('/');\n const accountNum = parseInt(segments[depth].replaceAll(\"'\", ''), 10);\n if (isNaN(accountNum)) throw new Error(`Cannot parse ${DerivationPathDepth[depth]} from path`);\n return accountNum;\n };\n}\n\nexport const extractPurposeFromPath = extractSectionFromDerivationPath(DerivationPathDepth.Purpose);\n\nexport const extractAccountIndexFromPath = extractSectionFromDerivationPath(\n DerivationPathDepth.Account\n);\n\nexport const extractAddressIndexFromPath = extractSectionFromDerivationPath(\n DerivationPathDepth.AddressIndex\n);\n\nexport function appendAddressIndexToPath(path: string, index: number) {\n const accountIndex = extractAccountIndexFromPath(path);\n if (!Number.isInteger(accountIndex)) throw new Error('Invalid path, must have account index');\n const assumedReceiveChangeIndex = 0;\n return `${path}/${assumedReceiveChangeIndex}/${index}`;\n}\n\nexport function extractFingerprintFromKeyOriginPath(keyOriginPath: string) {\n const fingerprint = keyOriginPath.split('/')[0];\n if (!isHexString(fingerprint)) throw new Error('Fingerprint must be a hexadecimal string');\n return fingerprint;\n}\n\n/**\n * @description\n * A key origin path refers to the identifier commonly used as part of the key\n * information provided as part of a Output Descriptor described in BIP-380. It\n * replaces the `m/` part of a derivation path with the master key fingerprint to which the\n * key it describes belongs.\n * @example `0a3fd8ef/84'/0'/0'`\n */\nexport function createKeyOriginPath(fingerprint: string, path: string) {\n if (!isHexString(fingerprint)) throw new Error('Fingerprint must be a hexadecimal string');\n return `${fingerprint}/${path.replace('m/', '')}`;\n}\n\nexport function validateKeyOriginPath(keyOriginPath: string) {\n if (keyOriginPath.includes('[') || keyOriginPath.includes(']'))\n throw new Error('Key origin path should not contain square brackets');\n\n if (!keyOriginPath.includes('/'))\n throw new Error('Key origin path must contain a fingerprint and derivation path');\n\n if (!isHexString(extractFingerprintFromKeyOriginPath(keyOriginPath)))\n throw new Error('Fingerprint must be a hexadecimal string');\n\n if (keyOriginPath.split('/').length < 4)\n throw new Error('Key origin path is too short. Should describe at least to the account level');\n\n return true;\n}\n\n/**\n * @description\n * Creates a descriptor with key origin and xpub or public key\n * @returns `[0a3fd8ef/84'/0'/0']xpuba1b…2c3`\n */\nexport function createDescriptor(keyOriginPath: string, key: string) {\n validateKeyOriginPath(keyOriginPath);\n return `[${keyOriginPath}]${key}`;\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/0']xpuba1b…2c3` -> `0a3fd8ef/84'/0'/0'`\n */\nexport function extractKeyOriginPathFromDescriptor(descriptor: string) {\n const keyOriginPath = descriptor.split(']')[0].replace('[', '');\n validateKeyOriginPath(keyOriginPath);\n return keyOriginPath;\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/0']xpuba1b…2c3` -> `m/84'/0'/0'`\n */\nexport function extractDerivationPathFromDescriptor(descriptor: string) {\n const keyOriginPath = extractKeyOriginPathFromDescriptor(descriptor);\n return 'm/' + keyOriginPath.split('/').slice(1).join('/');\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/0']xpuba1b…2c3` -> `0a3fd8ef`\n */\nexport function extractFingerprintFromDescriptor(descriptor: string) {\n return extractFingerprintFromKeyOriginPath(extractKeyOriginPathFromDescriptor(descriptor));\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/6']xpuba1b…2c3` -> `6`\n */\nexport function extractAccountIndexFromDescriptor(descriptor: string) {\n return extractAccountIndexFromPath(extractKeyOriginPathFromDescriptor(descriptor));\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/6']xpuba1b…2c3` -> `xpuba1b…2c3`\n */\nexport function extractKeyFromDescriptor(descriptor: string) {\n return descriptor.split(']')[1];\n}\n\nexport function keyOriginToDerivationPath(keyOrigin: string) {\n const [_fingerprint, ...remainingPath] = keyOrigin.split('/');\n return `m/${remainingPath.join('/')}`;\n}\n\nexport function decomposeDescriptor(descriptor: string) {\n return {\n descriptor,\n keyOrigin: extractKeyOriginPathFromDescriptor(descriptor),\n fingerprint: extractFingerprintFromDescriptor(descriptor),\n derivationPath: extractDerivationPathFromDescriptor(descriptor),\n accountIndex: extractAccountIndexFromDescriptor(descriptor),\n };\n}\n","import { HDKey } from '@scure/bip32';\nimport {\n mnemonicToSeed,\n generateMnemonic as scureGenerateMnemonic,\n validateMnemonic,\n} from '@scure/bip39';\nimport { wordlist } from '@scure/bip39/wordlists/english';\nimport memoize from 'just-memoize';\n\nimport { toHexString } from '@leather.io/utils';\n\nimport {\n DerivationPathDepth,\n createDescriptor,\n createKeyOriginPath,\n keyOriginToDerivationPath,\n} from './derivation-path-utils';\n\nexport function generateMnemonic() {\n return scureGenerateMnemonic(wordlist, 256);\n}\n\nexport async function deriveBip39SeedFromMnemonic(mnemonic: string, passphrase?: string) {\n return mnemonicToSeed(mnemonic, passphrase);\n}\n/** @deprecated Inaccurately named fn, use `deriveBip39SeedFromMnemonic` */\nexport const deriveBip39MnemonicFromSeed = deriveBip39SeedFromMnemonic;\n\nexport function deriveRootBip32Keychain(seed: Uint8Array) {\n return HDKey.fromMasterSeed(seed);\n}\n\nexport async function deriveRootKeychainFromMnemonic(mnemonic: string, passphrase?: string) {\n return deriveRootBip32Keychain(await deriveBip39SeedFromMnemonic(mnemonic, passphrase));\n}\n\nexport async function deriveChildKeychainFromMnemnonic(\n path: string,\n mnemonic: string,\n passphrase?: string\n) {\n const rootKeychain = deriveRootBip32Keychain(await mnemonicToSeed(mnemonic, passphrase));\n return rootKeychain.derive(keyOriginToDerivationPath(path));\n}\n\nexport const deriveKeychainFromXpub = memoize((xpub: string) => HDKey.fromExtendedKey(xpub));\n\n/**\n * Gets keychain fingerprint directly from mnemonic. This is useful for\n * referencing a mnemonic safely by an identifier.\n */\nexport async function getMnemonicRootKeyFingerprint(mnemonic: string, passphrase?: string) {\n const keychain = deriveRootBip32Keychain(await deriveBip39SeedFromMnemonic(mnemonic, passphrase));\n return toHexString(keychain.fingerprint);\n}\n\nexport function deriveKeychainExtendedPublicKeyDescriptor(rootKeychain: HDKey, path: string) {\n const masterFingerprint = toHexString(rootKeychain.fingerprint);\n const keyOriginPath = createKeyOriginPath(masterFingerprint, path);\n\n if (rootKeychain.depth !== DerivationPathDepth.Root)\n throw new Error('Cannot derive account keychain from non-root keychain');\n\n const accountKeychain = rootKeychain.derive(path);\n return createDescriptor(keyOriginPath, accountKeychain.publicExtendedKey);\n}\n\nexport function isValidMnemonicWord(word: string): boolean {\n return wordlist.includes(word);\n}\n\nexport function isValidMnemonic(mnemonic: string): boolean {\n return validateMnemonic(mnemonic, wordlist);\n}\n"],"mappings":";AAAA,SAAS,mBAAmB;AAErB,IAAK,sBAAL,kBAAKA,yBAAL;AACL,EAAAA,0CAAA,UAAO,KAAP;AACA,EAAAA,0CAAA,aAAU,KAAV;AACA,EAAAA,0CAAA,cAAW,KAAX;AACA,EAAAA,0CAAA,aAAU,KAAV;AACA,EAAAA,0CAAA,mBAAgB,KAAhB;AACA,EAAAA,0CAAA,kBAAe,KAAf;AANU,SAAAA;AAAA,GAAA;AASZ,SAAS,iCAAiC,OAA4B;AACpE,SAAO,CAAC,SAAiB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;AAEpC,SAAS,wBAAwB,MAAkB;AACxD,SAAO,MAAM,eAAe,IAAI;AAClC;AAEA,eAAsB,+BAA+B,UAAkB,YAAqB;AAC1F,SAAO,wBAAwB,MAAM,4BAA4B,UAAU,UAAU,CAAC;AACxF;AAEA,eAAsB,iCACpB,MACA,UACA,YACA;AACA,QAAM,eAAe,wBAAwB,MAAM,eAAe,UAAU,UAAU,CAAC;AACvF,SAAO,aAAa,OAAO,0BAA0B,IAAI,CAAC;AAC5D;AAEO,IAAM,yBAAyB,QAAQ,CAAC,SAAiB,MAAM,gBAAgB,IAAI,CAAC;AAM3F,eAAsB,8BAA8B,UAAkB,YAAqB;AACzF,QAAM,WAAW,wBAAwB,MAAM,4BAA4B,UAAU,UAAU,CAAC;AAChG,SAAO,YAAY,SAAS,WAAW;AACzC;AAEO,SAAS,0CAA0C,cAAqB,MAAc;AAC3F,QAAM,oBAAoB,YAAY,aAAa,WAAW;AAC9D,QAAM,gBAAgB,oBAAoB,mBAAmB,IAAI;AAEjE,MAAI,aAAa;AACf,UAAM,IAAI,MAAM,uDAAuD;AAEzE,QAAM,kBAAkB,aAAa,OAAO,IAAI;AAChD,SAAO,iBAAiB,eAAe,gBAAgB,iBAAiB;AAC1E;AAEO,SAAS,oBAAoB,MAAuB;AACzD,SAAO,SAAS,SAAS,IAAI;AAC/B;AAEO,SAAS,gBAAgB,UAA2B;AACzD,SAAO,iBAAiB,UAAU,QAAQ;AAC5C;","names":["DerivationPathDepth"]}
|
|
1
|
+
{"version":3,"sources":["../src/derivation-path-utils.ts","../src/keychain.ts"],"sourcesContent":["import { isHexString } from '@leather.io/utils';\n\nexport enum DerivationPathDepth {\n Root = 0,\n Purpose = 1,\n CoinType = 2,\n Account = 3,\n Change = 4,\n AddressIndex = 5,\n}\n\nfunction extractSectionFromDerivationPath(depth: DerivationPathDepth) {\n return (path: string) => {\n const segments = path.split('/');\n const accountNum = parseInt(segments[depth].replaceAll(\"'\", ''), 10);\n if (isNaN(accountNum)) throw new Error(`Cannot parse ${DerivationPathDepth[depth]} from path`);\n return accountNum;\n };\n}\n\nexport const extractPurposeFromPath = extractSectionFromDerivationPath(DerivationPathDepth.Purpose);\n\nexport const extractAccountIndexFromPath = extractSectionFromDerivationPath(\n DerivationPathDepth.Account\n);\n\nexport const extractChangeIndexFromPath = extractSectionFromDerivationPath(\n DerivationPathDepth.Change\n);\n\nexport const extractAddressIndexFromPath = extractSectionFromDerivationPath(\n DerivationPathDepth.AddressIndex\n);\n\nexport function appendAddressIndexToPath(path: string, change: number, addressIndex: number) {\n const accountIndex = extractAccountIndexFromPath(path);\n if (!Number.isInteger(accountIndex)) throw new Error('Invalid path, must have account index');\n return `${path}/${change}/${addressIndex}`;\n}\n\nexport function extractFingerprintFromKeyOriginPath(keyOriginPath: string) {\n const fingerprint = keyOriginPath.split('/')[0];\n if (!isHexString(fingerprint)) throw new Error('Fingerprint must be a hexadecimal string');\n return fingerprint;\n}\n\n/**\n * @description\n * A key origin path refers to the identifier commonly used as part of the key\n * information provided as part of a Output Descriptor described in BIP-380. It\n * replaces the `m/` part of a derivation path with the master key fingerprint to which the\n * key it describes belongs.\n * @example `0a3fd8ef/84'/0'/0'`\n */\nexport function createKeyOriginPath(fingerprint: string, path: string) {\n if (!isHexString(fingerprint)) throw new Error('Fingerprint must be a hexadecimal string');\n return `${fingerprint}/${path.replace('m/', '')}`;\n}\n\nexport function validateKeyOriginPath(keyOriginPath: string) {\n if (keyOriginPath.includes('[') || keyOriginPath.includes(']'))\n throw new Error('Key origin path should not contain square brackets');\n\n if (!keyOriginPath.includes('/'))\n throw new Error('Key origin path must contain a fingerprint and derivation path');\n\n if (!isHexString(extractFingerprintFromKeyOriginPath(keyOriginPath)))\n throw new Error('Fingerprint must be a hexadecimal string');\n\n if (keyOriginPath.split('/').length < 4)\n throw new Error('Key origin path is too short. Should describe at least to the account level');\n\n return true;\n}\n\n/**\n * @description\n * Creates a descriptor with key origin and xpub or public key\n * @returns `[0a3fd8ef/84'/0'/0']xpuba1b…2c3`\n */\nexport function createDescriptor(keyOriginPath: string, key: string) {\n validateKeyOriginPath(keyOriginPath);\n return `[${keyOriginPath}]${key}`;\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/0']xpuba1b…2c3` -> `0a3fd8ef/84'/0'/0'`\n */\nexport function extractKeyOriginPathFromDescriptor(descriptor: string) {\n const keyOriginPath = descriptor.split(']')[0].replace('[', '');\n validateKeyOriginPath(keyOriginPath);\n return keyOriginPath;\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/0']xpuba1b…2c3` -> `m/84'/0'/0'`\n */\nexport function extractDerivationPathFromDescriptor(descriptor: string) {\n const keyOriginPath = extractKeyOriginPathFromDescriptor(descriptor);\n return 'm/' + keyOriginPath.split('/').slice(1).join('/');\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/0']xpuba1b…2c3` -> `0a3fd8ef`\n */\nexport function extractFingerprintFromDescriptor(descriptor: string) {\n return extractFingerprintFromKeyOriginPath(extractKeyOriginPathFromDescriptor(descriptor));\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/6']xpuba1b…2c3` -> `6`\n */\nexport function extractAccountIndexFromDescriptor(descriptor: string) {\n return extractAccountIndexFromPath(extractKeyOriginPathFromDescriptor(descriptor));\n}\n\n/**\n * @example `[0a3fd8ef/84'/0'/6']xpuba1b…2c3` -> `xpuba1b…2c3`\n */\nexport function extractKeyFromDescriptor(descriptor: string) {\n return descriptor.split(']')[1];\n}\n\n/**\n * @example `0a3fd8ef/84'/0'/0/0/0` -> `0a3fd8ef/84'/0'/0`\n */\nexport function extractAccountPathFromFullPath(path: string) {\n const segments = path.split('/');\n return segments.slice(0, 4).join('/');\n}\n\nexport function keyOriginToDerivationPath(keyOrigin: string) {\n const [_fingerprint, ...remainingPath] = keyOrigin.split('/');\n return `m/${remainingPath.join('/')}`;\n}\n\nexport function decomposeDescriptor(descriptor: string) {\n return {\n descriptor,\n keyOrigin: extractKeyOriginPathFromDescriptor(descriptor),\n fingerprint: extractFingerprintFromDescriptor(descriptor),\n derivationPath: extractDerivationPathFromDescriptor(descriptor),\n accountIndex: extractAccountIndexFromDescriptor(descriptor),\n };\n}\n","import { HDKey } from '@scure/bip32';\nimport {\n mnemonicToSeed,\n generateMnemonic as scureGenerateMnemonic,\n validateMnemonic,\n} from '@scure/bip39';\nimport { wordlist } from '@scure/bip39/wordlists/english';\nimport memoize from 'just-memoize';\n\nimport { toHexString } from '@leather.io/utils';\n\nimport {\n DerivationPathDepth,\n createDescriptor,\n createKeyOriginPath,\n keyOriginToDerivationPath,\n} from './derivation-path-utils';\n\nexport function generateMnemonic() {\n return scureGenerateMnemonic(wordlist, 256);\n}\n\nexport async function deriveBip39SeedFromMnemonic(mnemonic: string, passphrase?: string) {\n return mnemonicToSeed(mnemonic, passphrase);\n}\n/** @deprecated Inaccurately named fn, use `deriveBip39SeedFromMnemonic` */\nexport const deriveBip39MnemonicFromSeed = deriveBip39SeedFromMnemonic;\n\nexport function deriveRootBip32Keychain(seed: Uint8Array) {\n return HDKey.fromMasterSeed(seed);\n}\n\nexport async function deriveRootKeychainFromMnemonic(mnemonic: string, passphrase?: string) {\n return deriveRootBip32Keychain(await deriveBip39SeedFromMnemonic(mnemonic, passphrase));\n}\n\nexport async function deriveChildKeychainFromMnemnonic(\n path: string,\n mnemonic: string,\n passphrase?: string\n) {\n const rootKeychain = deriveRootBip32Keychain(await mnemonicToSeed(mnemonic, passphrase));\n return rootKeychain.derive(keyOriginToDerivationPath(path));\n}\n\nexport const deriveKeychainFromXpub = memoize((xpub: string) => HDKey.fromExtendedKey(xpub));\n\n/**\n * Gets keychain fingerprint directly from mnemonic. This is useful for\n * referencing a mnemonic safely by an identifier.\n */\nexport async function getMnemonicRootKeyFingerprint(mnemonic: string, passphrase?: string) {\n const keychain = deriveRootBip32Keychain(await deriveBip39SeedFromMnemonic(mnemonic, passphrase));\n return toHexString(keychain.fingerprint);\n}\n\nexport function deriveKeychainExtendedPublicKeyDescriptor(rootKeychain: HDKey, path: string) {\n const masterFingerprint = toHexString(rootKeychain.fingerprint);\n const keyOriginPath = createKeyOriginPath(masterFingerprint, path);\n\n if (rootKeychain.depth !== DerivationPathDepth.Root)\n throw new Error('Cannot derive account keychain from non-root keychain');\n\n const accountKeychain = rootKeychain.derive(path);\n return createDescriptor(keyOriginPath, accountKeychain.publicExtendedKey);\n}\n\nexport function isValidMnemonicWord(word: string): boolean {\n return wordlist.includes(word);\n}\n\nexport function isValidMnemonic(mnemonic: string): boolean {\n return validateMnemonic(mnemonic, wordlist);\n}\n"],"mappings":";AAAA,SAAS,mBAAmB;AAErB,IAAK,sBAAL,kBAAKA,yBAAL;AACL,EAAAA,0CAAA,UAAO,KAAP;AACA,EAAAA,0CAAA,aAAU,KAAV;AACA,EAAAA,0CAAA,cAAW,KAAX;AACA,EAAAA,0CAAA,aAAU,KAAV;AACA,EAAAA,0CAAA,YAAS,KAAT;AACA,EAAAA,0CAAA,kBAAe,KAAf;AANU,SAAAA;AAAA,GAAA;AASZ,SAAS,iCAAiC,OAA4B;AACpE,SAAO,CAAC,SAAiB;AACvB,UAAM,WAAW,KAAK,MAAM,GAAG;AAC/B,UAAM,aAAa,SAAS,SAAS,KAAK,EAAE,WAAW,KAAK,EAAE,GAAG,EAAE;AACnE,QAAI,MAAM,UAAU,EAAG,OAAM,IAAI,MAAM,gBAAgB,oBAAoB,KAAK,CAAC,YAAY;AAC7F,WAAO;AAAA,EACT;AACF;AAEO,IAAM,yBAAyB,iCAAiC,eAA2B;AAE3F,IAAM,8BAA8B;AAAA,EACzC;AACF;AAEO,IAAM,6BAA6B;AAAA,EACxC;AACF;AAEO,IAAM,8BAA8B;AAAA,EACzC;AACF;AAEO,SAAS,yBAAyB,MAAc,QAAgB,cAAsB;AAC3F,QAAM,eAAe,4BAA4B,IAAI;AACrD,MAAI,CAAC,OAAO,UAAU,YAAY,EAAG,OAAM,IAAI,MAAM,uCAAuC;AAC5F,SAAO,GAAG,IAAI,IAAI,MAAM,IAAI,YAAY;AAC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M,WAAW,wBAAwB,MAAM,4BAA4B,UAAU,UAAU,CAAC;AAChG,SAAO,YAAY,SAAS,WAAW;AACzC;AAEO,SAAS,0CAA0C,cAAqB,MAAc;AAC3F,QAAM,oBAAoB,YAAY,aAAa,WAAW;AAC9D,QAAM,gBAAgB,oBAAoB,mBAAmB,IAAI;AAEjE,MAAI,aAAa;AACf,UAAM,IAAI,MAAM,uDAAuD;AAEzE,QAAM,kBAAkB,aAAa,OAAO,IAAI;AAChD,SAAO,iBAAiB,eAAe,gBAAgB,iBAAiB;AAC1E;AAEO,SAAS,oBAAoB,MAAuB;AACzD,SAAO,SAAS,SAAS,IAAI;AAC/B;AAEO,SAAS,gBAAgB,UAA2B;AACzD,SAAO,iBAAiB,UAAU,QAAQ;AAC5C;","names":["DerivationPathDepth"]}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@leather.io/crypto",
|
|
3
3
|
"author": "leather.io",
|
|
4
4
|
"description": "Generic crypto utils package for Leather",
|
|
5
|
-
"version": "1.7.
|
|
5
|
+
"version": "1.7.6",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./dist/index.mjs"
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"@scure/bip32": "1.6.2",
|
|
12
12
|
"@scure/bip39": "1.5.4",
|
|
13
13
|
"just-memoize": "2.2.0",
|
|
14
|
-
"@leather.io/utils": "0.35.
|
|
14
|
+
"@leather.io/utils": "0.35.3"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"tsup": "8.4.0",
|