@metamask-previews/key-tree 0.0.0-stage → 10.1.1-preview-2ad7fb9

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.
Files changed (88) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/LICENSE +21 -0
  3. package/README.md +163 -2
  4. package/dist/BIP44CoinTypeNode.d.ts +244 -0
  5. package/dist/BIP44CoinTypeNode.d.ts.map +1 -0
  6. package/dist/BIP44CoinTypeNode.js +365 -0
  7. package/dist/BIP44CoinTypeNode.js.map +1 -0
  8. package/dist/BIP44Node.d.ts +220 -0
  9. package/dist/BIP44Node.d.ts.map +1 -0
  10. package/dist/BIP44Node.js +327 -0
  11. package/dist/BIP44Node.js.map +1 -0
  12. package/dist/SLIP10Node.d.ts +310 -0
  13. package/dist/SLIP10Node.d.ts.map +1 -0
  14. package/dist/SLIP10Node.js +443 -0
  15. package/dist/SLIP10Node.js.map +1 -0
  16. package/dist/constants.d.ts +132 -0
  17. package/dist/constants.d.ts.map +1 -0
  18. package/dist/constants.js +39 -0
  19. package/dist/constants.js.map +1 -0
  20. package/dist/cryptography.d.ts +81 -0
  21. package/dist/cryptography.d.ts.map +1 -0
  22. package/dist/cryptography.js +112 -0
  23. package/dist/cryptography.js.map +1 -0
  24. package/dist/curves/curve.d.ts +40 -0
  25. package/dist/curves/curve.d.ts.map +1 -0
  26. package/dist/curves/curve.js +19 -0
  27. package/dist/curves/curve.js.map +1 -0
  28. package/dist/curves/ed25519.d.ts +14 -0
  29. package/dist/curves/ed25519.d.ts.map +1 -0
  30. package/dist/curves/ed25519.js +40 -0
  31. package/dist/curves/ed25519.js.map +1 -0
  32. package/dist/curves/ed25519Bip32.d.ts +68 -0
  33. package/dist/curves/ed25519Bip32.d.ts.map +1 -0
  34. package/dist/curves/ed25519Bip32.js +122 -0
  35. package/dist/curves/ed25519Bip32.js.map +1 -0
  36. package/dist/curves/index.d.ts +5 -0
  37. package/dist/curves/index.d.ts.map +1 -0
  38. package/dist/curves/index.js +5 -0
  39. package/dist/curves/index.js.map +1 -0
  40. package/dist/curves/secp256k1.d.ts +14 -0
  41. package/dist/curves/secp256k1.d.ts.map +1 -0
  42. package/dist/curves/secp256k1.js +51 -0
  43. package/dist/curves/secp256k1.js.map +1 -0
  44. package/dist/derivation.d.ts +65 -0
  45. package/dist/derivation.d.ts.map +1 -0
  46. package/dist/derivation.js +143 -0
  47. package/dist/derivation.js.map +1 -0
  48. package/dist/derivers/bip32.d.ts +40 -0
  49. package/dist/derivers/bip32.d.ts.map +1 -0
  50. package/dist/derivers/bip32.js +100 -0
  51. package/dist/derivers/bip32.js.map +1 -0
  52. package/dist/derivers/bip39.d.ts +93 -0
  53. package/dist/derivers/bip39.d.ts.map +1 -0
  54. package/dist/derivers/bip39.js +209 -0
  55. package/dist/derivers/bip39.js.map +1 -0
  56. package/dist/derivers/cip3.d.ts +167 -0
  57. package/dist/derivers/cip3.d.ts.map +1 -0
  58. package/dist/derivers/cip3.js +285 -0
  59. package/dist/derivers/cip3.js.map +1 -0
  60. package/dist/derivers/index.d.ts +33 -0
  61. package/dist/derivers/index.d.ts.map +1 -0
  62. package/dist/derivers/index.js +12 -0
  63. package/dist/derivers/index.js.map +1 -0
  64. package/dist/derivers/shared.d.ts +152 -0
  65. package/dist/derivers/shared.d.ts.map +1 -0
  66. package/dist/derivers/shared.js +354 -0
  67. package/dist/derivers/shared.js.map +1 -0
  68. package/dist/derivers/slip10.d.ts +14 -0
  69. package/dist/derivers/slip10.d.ts.map +1 -0
  70. package/dist/derivers/slip10.js +53 -0
  71. package/dist/derivers/slip10.js.map +1 -0
  72. package/dist/extended-keys.d.ts +40 -0
  73. package/dist/extended-keys.d.ts.map +1 -0
  74. package/dist/extended-keys.js +134 -0
  75. package/dist/extended-keys.js.map +1 -0
  76. package/dist/guard.d.ts +8 -0
  77. package/dist/guard.d.ts.map +1 -0
  78. package/dist/guard.js +8 -0
  79. package/dist/guard.js.map +1 -0
  80. package/dist/index.d.ts +14 -0
  81. package/dist/index.d.ts.map +1 -0
  82. package/dist/index.js +8 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/utils.d.ts +234 -0
  85. package/dist/utils.d.ts.map +1 -0
  86. package/dist/utils.js +379 -0
  87. package/dist/utils.js.map +1 -0
  88. package/package.json +71 -3
@@ -0,0 +1,365 @@
1
+ import { BIP44Node } from './BIP44Node.js';
2
+ import { BIP_32_HARDENED_OFFSET } from './constants.js';
3
+ import { deriveChildNode } from './SLIP10Node.js';
4
+ import { getBIP44CoinType, getBIP32NodeToken, getBIP44ChangePathString, getBIP44CoinTypePathString, getBIP44CoinTypeToAddressPathTuple, getHardenedBIP32NodeToken, getUnhardenedBIP32NodeToken, hexStringToBytes, nullableHexStringToBytes, } from './utils.js';
5
+ export const BIP_44_COIN_TYPE_DEPTH = 2;
6
+ /**
7
+ * A wrapper object for BIP-44 `coin_type` keys. `coin_type` is the index
8
+ * specifying the protocol for which deeper keys are intended. For the
9
+ * authoritative list of coin types, please see
10
+ * [SLIP-44](https://github.com/satoshilabs/slips/blob/master/slip-0044.md).
11
+ *
12
+ * Recall that a BIP-44 HD tree path consists of the following nodes:
13
+ *
14
+ * `m / 44' / coin_type' / account' / change / address_index`
15
+ *
16
+ * With the following depths:
17
+ *
18
+ * `0 / 1 / 2 / 3 / 4 / 5`
19
+ *
20
+ */
21
+ export class BIP44CoinTypeNode {
22
+ /**
23
+ * Constructs a BIP-44 `coin_type` node. `coin_type` is the index
24
+ * specifying the protocol for which deeper keys are intended. For the
25
+ * authoritative list of coin types, please see
26
+ * [SLIP-44](https://github.com/satoshilabs/slips/blob/master/slip-0044.md).
27
+ *
28
+ * Recall that a BIP-44 HD tree path consists of the following nodes:
29
+ *
30
+ * `m / 44' / coin_type' / account' / change / address_index`
31
+ *
32
+ * With the following depths:
33
+ *
34
+ * `0 / 1 / 2 / 3 / 4 / 5`
35
+ *
36
+ * @param json - The {@link JsonBIP44Node} for the key of this node.
37
+ * @param coin_type - The coin_type index of this node. Must be a non-negative
38
+ * integer.
39
+ * @param cryptographicFunctions - The cryptographic functions to use. If
40
+ * provided, these will be used instead of the built-in implementations.
41
+ * @returns A BIP44CoinType node.
42
+ */
43
+ static async fromJSON(json, coin_type, cryptographicFunctions) {
44
+ validateCoinType(coin_type);
45
+ validateCoinTypeNodeDepth(json.depth);
46
+ const node = await BIP44Node.fromExtendedKey({
47
+ depth: json.depth,
48
+ index: json.index,
49
+ network: json.network,
50
+ parentFingerprint: json.parentFingerprint,
51
+ chainCode: hexStringToBytes(json.chainCode),
52
+ privateKey: nullableHexStringToBytes(json.privateKey),
53
+ publicKey: hexStringToBytes(json.publicKey),
54
+ }, cryptographicFunctions);
55
+ return new BIP44CoinTypeNode(node, coin_type);
56
+ }
57
+ /**
58
+ * Construct a BIP-44 `coin_type` node. `coin_type` is the index
59
+ * specifying the protocol for which deeper keys are intended. For the
60
+ * authoritative list of coin types, please see
61
+ * [SLIP-44](https://github.com/satoshilabs/slips/blob/master/slip-0044.md).
62
+ *
63
+ * Recall that a BIP-44 HD tree path consists of the following nodes:
64
+ *
65
+ * `m / 44' / coin_type' / account' / change / address_index`
66
+ *
67
+ * With the following depths:
68
+ *
69
+ * `0 / 1 / 2 / 3 / 4 / 5`
70
+ *
71
+ * @param derivationPath - The derivation path for the key of this node.
72
+ * @param network - The network for the node. This is only used for extended
73
+ * keys, and defaults to `mainnet`.
74
+ * @param cryptographicFunctions - The cryptographic functions to use. If
75
+ * provided, these will be used instead of the built-in implementations.
76
+ * @returns A BIP44CoinType node.
77
+ */
78
+ static async fromDerivationPath(derivationPath, network, cryptographicFunctions) {
79
+ validateCoinTypeNodeDepth(derivationPath.length - 1);
80
+ const node = await BIP44Node.fromDerivationPath({
81
+ derivationPath,
82
+ network,
83
+ }, cryptographicFunctions);
84
+ const coinType = getBIP44CoinType(derivationPath);
85
+ return new BIP44CoinTypeNode(node, coinType);
86
+ }
87
+ /**
88
+ * Create a new BIP-44 coin type node from a BIP-39 seed. The derivation path
89
+ * must be rooted, i.e. it must begin with a BIP-39 node, given as a
90
+ * `Uint8Array` of the seed bytes.
91
+ *
92
+ * All parameters are stringently validated, and an error is thrown if
93
+ * validation fails.
94
+ *
95
+ * @param options - The options for the new node.
96
+ * @param options.derivationPath - The rooted HD tree path that will be used
97
+ * to derive the key of this node.
98
+ * @param options.network - The network for the node. This is only used for
99
+ * extended keys, and defaults to `mainnet`.
100
+ * @param cryptographicFunctions - The cryptographic functions to use. If
101
+ * provided, these will be used instead of the built-in implementations.
102
+ * @returns A new BIP-44 node.
103
+ */
104
+ static async fromSeed({ derivationPath, network }, cryptographicFunctions) {
105
+ validateCoinTypeNodeDepth(derivationPath.length - 1);
106
+ const node = await BIP44Node.fromSeed({
107
+ derivationPath,
108
+ network,
109
+ }, cryptographicFunctions);
110
+ const coinType = getBIP44CoinType(derivationPath);
111
+ return new BIP44CoinTypeNode(node, coinType);
112
+ }
113
+ /**
114
+ * Constructs a BIP-44 `coin_type` node. `coin_type` is the index
115
+ * specifying the protocol for which deeper keys are intended. For the
116
+ * authoritative list of coin types, please see
117
+ * [SLIP-44](https://github.com/satoshilabs/slips/blob/master/slip-0044.md).
118
+ *
119
+ * Recall that a BIP-44 HD tree path consists of the following nodes:
120
+ *
121
+ * `m / 44' / coin_type' / account' / change / address_index`
122
+ *
123
+ * With the following depths:
124
+ *
125
+ * `0 / 1 / 2 / 3 / 4 / 5`
126
+ *
127
+ * @param node - The {@link BIP44Node} for the key of this node.
128
+ * @param coin_type - The coin_type index of this node. Must be a non-negative
129
+ * integer.
130
+ * @returns A BIP44CoinType node.
131
+ */
132
+ static async fromNode(node, coin_type) {
133
+ if (!(node instanceof BIP44Node)) {
134
+ throw new Error('Invalid node: Expected an instance of BIP44Node.');
135
+ }
136
+ validateCoinType(coin_type);
137
+ validateCoinTypeNodeDepth(node.depth);
138
+ // TODO: Make this function not async in a future version.
139
+ return Promise.resolve(new BIP44CoinTypeNode(node, coin_type));
140
+ }
141
+ #node;
142
+ path;
143
+ coin_type;
144
+ // Constructors cannot use hash names.
145
+ // eslint-disable-next-line no-restricted-syntax
146
+ constructor(node, coin_type) {
147
+ this.#node = node;
148
+ this.coin_type = coin_type;
149
+ this.path = getBIP44CoinTypePathString(coin_type);
150
+ Object.freeze(this);
151
+ }
152
+ get depth() {
153
+ return this.#node.depth;
154
+ }
155
+ get privateKeyBytes() {
156
+ return this.#node.privateKeyBytes;
157
+ }
158
+ get publicKeyBytes() {
159
+ return this.#node.publicKeyBytes;
160
+ }
161
+ get chainCodeBytes() {
162
+ return this.#node.chainCodeBytes;
163
+ }
164
+ get privateKey() {
165
+ return this.#node.privateKey;
166
+ }
167
+ get publicKey() {
168
+ return this.#node.publicKey;
169
+ }
170
+ get compressedPublicKey() {
171
+ return this.#node.compressedPublicKey;
172
+ }
173
+ get compressedPublicKeyBytes() {
174
+ return this.#node.compressedPublicKeyBytes;
175
+ }
176
+ get chainCode() {
177
+ return this.#node.chainCode;
178
+ }
179
+ get address() {
180
+ return this.#node.address;
181
+ }
182
+ get masterFingerprint() {
183
+ return this.#node.masterFingerprint;
184
+ }
185
+ get parentFingerprint() {
186
+ return this.#node.parentFingerprint;
187
+ }
188
+ get fingerprint() {
189
+ return this.#node.fingerprint;
190
+ }
191
+ get index() {
192
+ return this.#node.index;
193
+ }
194
+ get network() {
195
+ return this.#node.network;
196
+ }
197
+ get curve() {
198
+ return this.#node.curve;
199
+ }
200
+ get extendedKey() {
201
+ return this.#node.extendedKey;
202
+ }
203
+ /**
204
+ * Derives a BIP-44 `address_index` key corresponding to the path of this
205
+ * node and the specified `account`, `change`, and `address_index` values.
206
+ * `address_index` keys are normally the keys used to generate user account
207
+ * addresses.
208
+ *
209
+ * Recall that a BIP-44 HD tree path consists of the following nodes:
210
+ *
211
+ * `m / 44' / coin_type' / account' / change / address_index`
212
+ *
213
+ * With the following depths:
214
+ *
215
+ * `0 / 1 / 2 / 3 / 4 / 5`
216
+ *
217
+ * @param indices - The BIP-44 index values to use in key derivation.
218
+ * @param indices.account - The `account` index. Default: `0`
219
+ * @param indices.change - The `change` index. Default: `0`
220
+ * @param indices.address_index - The `address_index` index.
221
+ * @returns The derived BIP-44 `address_index` node.
222
+ */
223
+ async deriveBIP44AddressKey({ account = 0, change = 0, address_index, }) {
224
+ return await this.#node.derive(getBIP44CoinTypeToAddressPathTuple({ account, change, address_index }));
225
+ }
226
+ toJSON() {
227
+ return {
228
+ ...this.#node.toJSON(),
229
+ coin_type: this.coin_type,
230
+ path: this.path,
231
+ };
232
+ }
233
+ }
234
+ /**
235
+ * Validates the depth of a `coin_type` node. Simply, ensures that it is the
236
+ * number `2`. An error is thrown if validation fails.
237
+ *
238
+ * @param depth - The depth to validate.
239
+ */
240
+ function validateCoinTypeNodeDepth(depth) {
241
+ if (depth !== BIP_44_COIN_TYPE_DEPTH) {
242
+ throw new Error(`Invalid depth: Coin type nodes must be of depth ${BIP_44_COIN_TYPE_DEPTH}. Received: "${depth}"`);
243
+ }
244
+ }
245
+ /**
246
+ * Validates that the coin type is a non-negative integer number. An error is
247
+ * thrown if validation fails.
248
+ *
249
+ * @param coin_type - The coin type to validate.
250
+ */
251
+ function validateCoinType(coin_type) {
252
+ if (typeof coin_type !== 'number' ||
253
+ !Number.isInteger(coin_type) ||
254
+ coin_type < 0) {
255
+ throw new Error('Invalid coin type: The specified coin type must be a non-negative integer number.');
256
+ }
257
+ }
258
+ /**
259
+ * Derives a BIP-44 address key corresponding to the specified derivation path,
260
+ * given either by a {@link BIP44CoinTypeNode} or derivation path tuple.
261
+ *
262
+ * Recall that a BIP-44 HD tree path consists of the following nodes:
263
+ *
264
+ * `m / 44' / coin_type' / account' / change / address_index`
265
+ *
266
+ * With the following depths:
267
+ *
268
+ * `0 / 1 / 2 / 3 / 4 / 5`
269
+ *
270
+ * @param parentKeyOrNode - The `coin_type` parent key to derive from.
271
+ * @param indices - The BIP-44 index values to use in key derivation.
272
+ * @param indices.account - The `account` index. Default: `0`.
273
+ * @param indices.change - The `change` index. Default: `0`.
274
+ * @param indices.address_index - The `address_index` index.
275
+ * @param cryptographicFunctions - The cryptographic functions to use. If
276
+ * provided, these will be used instead of the built-in implementations.
277
+ * @returns The derived `address_index` key for the specified derivation path.
278
+ */
279
+ export async function deriveBIP44AddressKey(parentKeyOrNode, { account = 0, change = 0, address_index }, cryptographicFunctions) {
280
+ const path = getBIP44CoinTypeToAddressPathTuple({
281
+ account,
282
+ change,
283
+ address_index,
284
+ });
285
+ const node = await getNode(parentKeyOrNode, cryptographicFunctions);
286
+ const childNode = await deriveChildNode({
287
+ path,
288
+ node,
289
+ }, cryptographicFunctions);
290
+ return new BIP44Node(childNode);
291
+ }
292
+ /**
293
+ * Creates a function that derives BIP-44 address keys corresponding to the
294
+ * specified derivation path, given either by a {@link BIP44CoinTypeNode} or
295
+ * derivation path tuple.
296
+ *
297
+ * Recall that a BIP-44 HD tree path consists of the following nodes:
298
+ *
299
+ * `m / 44' / coin_type' / account' / change / address_index`
300
+ *
301
+ * With the following depths:
302
+ *
303
+ * `0 / 1 / 2 / 3 / 4 / 5`
304
+ *
305
+ * @param node - The {@link BIP44CoinTypeNode} to derive address keys from.
306
+ * This node contains a BIP-44 key of depth 2, `coin_type`.
307
+ * @param accountAndChangeIndices - The `account` and `change` indices that
308
+ * will be used to derive addresses.
309
+ * @param cryptographicFunctions - The cryptographic functions to use. If
310
+ * provided, these will be used instead of the built-in implementations.
311
+ * @returns The deriver function for the derivation path specified by the
312
+ * `coin_type` node, `account`, and `change` indices.
313
+ */
314
+ export async function getBIP44AddressKeyDeriver(node, accountAndChangeIndices, cryptographicFunctions) {
315
+ const { account = 0, change = 0 } = accountAndChangeIndices ?? {};
316
+ const actualNode = await getNode(node, cryptographicFunctions);
317
+ const accountNode = getHardenedBIP32NodeToken(account);
318
+ const changeNode = getBIP32NodeToken(change);
319
+ const bip44AddressKeyDeriver = async (address_index, isHardened = false) => {
320
+ const slip10Node = await deriveChildNode({
321
+ path: [
322
+ accountNode,
323
+ changeNode,
324
+ isHardened
325
+ ? getHardenedBIP32NodeToken(address_index)
326
+ : getUnhardenedBIP32NodeToken(address_index),
327
+ ],
328
+ node: actualNode,
329
+ }, cryptographicFunctions);
330
+ return new BIP44Node(slip10Node);
331
+ };
332
+ bip44AddressKeyDeriver.coin_type = actualNode.coin_type;
333
+ bip44AddressKeyDeriver.path = getBIP44ChangePathString(actualNode.path, {
334
+ account,
335
+ change,
336
+ });
337
+ Object.freeze(bip44AddressKeyDeriver);
338
+ return bip44AddressKeyDeriver;
339
+ }
340
+ /**
341
+ * Get a BIP-44 coin type node from a JSON node or extended key string. If an existing coin type
342
+ * node is provided, the same node is returned.
343
+ *
344
+ * The depth of the node is validated to be a valid coin type node.
345
+ *
346
+ * @param node - A BIP-44 coin type node, JSON node or extended key.
347
+ * @param cryptographicFunctions - The cryptographic functions to use. If
348
+ * provided, these will be used instead of the built-in implementations. This is
349
+ * only used if the node is an extended key string or JSON object.
350
+ * @returns A BIP44CoinType node.
351
+ */
352
+ async function getNode(node, cryptographicFunctions) {
353
+ if (node instanceof BIP44CoinTypeNode) {
354
+ validateCoinTypeNodeDepth(node.depth);
355
+ return node;
356
+ }
357
+ if (typeof node === 'string') {
358
+ const bip44Node = await BIP44Node.fromExtendedKey(node, cryptographicFunctions);
359
+ const coinTypeNode = await BIP44CoinTypeNode.fromNode(bip44Node, bip44Node.index - BIP_32_HARDENED_OFFSET);
360
+ validateCoinTypeNodeDepth(coinTypeNode.depth);
361
+ return coinTypeNode;
362
+ }
363
+ return BIP44CoinTypeNode.fromJSON(node, node.coin_type, cryptographicFunctions);
364
+ }
365
+ //# sourceMappingURL=BIP44CoinTypeNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BIP44CoinTypeNode.js","sourceRoot":"","sources":["../src/BIP44CoinTypeNode.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAS3C,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAGxD,OAAO,EAA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,SAAS,CAAC;IACxD,sBAAsB,CAAC,IAAI,GAAG,wBAAwB,CAAC,UAAU,CAAC,IAAI,EAAE;QACtE,OAAO;QACP,MAAM;KACP,CAAC,CAAC;IAEH,MAAM,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC;IAEtC,OAAO,sBAAsB,CAAC;AAChC,CAAC;AAED;;;;;;;;;;;GAWG;AACH,KAAK,UAAU,OAAO,CACpB,IAAwD,EACxD,sBAA+C;IAE/C,IAAI,IAAI,YAAY,iBAAiB,EAAE,CAAC;QACtC,yBAAyB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAEtC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,MAAM,SAAS,GAAG,MAAM,SAAS,CAAC,eAAe,CAC/C,IAAI,EACJ,sBAAsB,CACvB,CAAC;QACF,MAAM,YAAY,GAAG,MAAM,iBAAiB,CAAC,QAAQ,CACnD,SAAS,EACT,SAAS,CAAC,KAAK,GAAG,sBAAsB,CACzC,CAAC;QAEF,yBAAyB,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QAE9C,OAAO,YAAY,CAAC;IACtB,CAAC;IAED,OAAO,iBAAiB,CAAC,QAAQ,CAC/B,IAAI,EACJ,IAAI,CAAC,SAAS,EACd,sBAAsB,CACvB,CAAC;AACJ,CAAC","sourcesContent":["import type { BIP44NodeInterface, JsonBIP44Node } from './BIP44Node.js';\nimport { BIP44Node } from './BIP44Node.js';\nimport type {\n BIP39Node,\n BIP44Depth,\n BIP44PurposeNodeToken,\n CoinTypeHDPathString,\n HardenedBIP32Node,\n Network,\n} from './constants.js';\nimport { BIP_32_HARDENED_OFFSET } from './constants.js';\nimport type { CryptographicFunctions } from './cryptography.js';\nimport type { SupportedCurve } from './curves/index.js';\nimport { deriveChildNode } from './SLIP10Node.js';\nimport type { CoinTypeToAddressIndices } from './utils.js';\nimport {\n getBIP44CoinType,\n getBIP32NodeToken,\n getBIP44ChangePathString,\n getBIP44CoinTypePathString,\n getBIP44CoinTypeToAddressPathTuple,\n getHardenedBIP32NodeToken,\n getUnhardenedBIP32NodeToken,\n hexStringToBytes,\n nullableHexStringToBytes,\n} from './utils.js';\n\nexport type CoinTypeHDPathTuple = [\n BIP39Node,\n typeof BIP44PurposeNodeToken,\n HardenedBIP32Node,\n];\n\nexport type CoinTypeSeedPathTuple = [\n Uint8Array,\n typeof BIP44PurposeNodeToken,\n HardenedBIP32Node,\n];\n\nexport const BIP_44_COIN_TYPE_DEPTH = 2;\n\nexport type JsonBIP44CoinTypeNode = JsonBIP44Node & {\n readonly coin_type: number;\n readonly path: CoinTypeHDPathString;\n};\n\nexport type BIP44CoinTypeNodeInterface = BIP44NodeInterface & {\n readonly coin_type: number;\n readonly path: CoinTypeHDPathString;\n};\n\nexport type BIP44CoinTypeSeedOptions = {\n readonly derivationPath: CoinTypeSeedPathTuple;\n readonly network?: Network | undefined;\n};\n\n/**\n * A wrapper object for BIP-44 `coin_type` keys. `coin_type` is the index\n * specifying the protocol for which deeper keys are intended. For the\n * authoritative list of coin types, please see\n * [SLIP-44](https://github.com/satoshilabs/slips/blob/master/slip-0044.md).\n *\n * Recall that a BIP-44 HD tree path consists of the following nodes:\n *\n * `m / 44' / coin_type' / account' / change / address_index`\n *\n * With the following depths:\n *\n * `0 / 1 / 2 / 3 / 4 / 5`\n *\n */\nexport class BIP44CoinTypeNode implements BIP44CoinTypeNodeInterface {\n /**\n * Constructs a BIP-44 `coin_type` node. `coin_type` is the index\n * specifying the protocol for which deeper keys are intended. For the\n * authoritative list of coin types, please see\n * [SLIP-44](https://github.com/satoshilabs/slips/blob/master/slip-0044.md).\n *\n * Recall that a BIP-44 HD tree path consists of the following nodes:\n *\n * `m / 44' / coin_type' / account' / change / address_index`\n *\n * With the following depths:\n *\n * `0 / 1 / 2 / 3 / 4 / 5`\n *\n * @param json - The {@link JsonBIP44Node} for the key of this node.\n * @param coin_type - The coin_type index of this node. Must be a non-negative\n * integer.\n * @param cryptographicFunctions - The cryptographic functions to use. If\n * provided, these will be used instead of the built-in implementations.\n * @returns A BIP44CoinType node.\n */\n static async fromJSON(\n json: JsonBIP44Node,\n coin_type: number,\n cryptographicFunctions?: CryptographicFunctions,\n ): Promise<BIP44CoinTypeNode> {\n validateCoinType(coin_type);\n validateCoinTypeNodeDepth(json.depth);\n\n const node = await BIP44Node.fromExtendedKey(\n {\n depth: json.depth,\n index: json.index,\n network: json.network,\n parentFingerprint: json.parentFingerprint,\n chainCode: hexStringToBytes(json.chainCode),\n privateKey: nullableHexStringToBytes(json.privateKey),\n publicKey: hexStringToBytes(json.publicKey),\n },\n cryptographicFunctions,\n );\n\n return new BIP44CoinTypeNode(node, coin_type);\n }\n\n /**\n * Construct a BIP-44 `coin_type` node. `coin_type` is the index\n * specifying the protocol for which deeper keys are intended. For the\n * authoritative list of coin types, please see\n * [SLIP-44](https://github.com/satoshilabs/slips/blob/master/slip-0044.md).\n *\n * Recall that a BIP-44 HD tree path consists of the following nodes:\n *\n * `m / 44' / coin_type' / account' / change / address_index`\n *\n * With the following depths:\n *\n * `0 / 1 / 2 / 3 / 4 / 5`\n *\n * @param derivationPath - The derivation path for the key of this node.\n * @param network - The network for the node. This is only used for extended\n * keys, and defaults to `mainnet`.\n * @param cryptographicFunctions - The cryptographic functions to use. If\n * provided, these will be used instead of the built-in implementations.\n * @returns A BIP44CoinType node.\n */\n static async fromDerivationPath(\n derivationPath: CoinTypeHDPathTuple,\n network?: Network | undefined,\n cryptographicFunctions?: CryptographicFunctions,\n ): Promise<BIP44CoinTypeNode> {\n validateCoinTypeNodeDepth(derivationPath.length - 1);\n\n const node = await BIP44Node.fromDerivationPath(\n {\n derivationPath,\n network,\n },\n cryptographicFunctions,\n );\n\n const coinType = getBIP44CoinType(derivationPath);\n return new BIP44CoinTypeNode(node, coinType);\n }\n\n /**\n * Create a new BIP-44 coin type node from a BIP-39 seed. The derivation path\n * must be rooted, i.e. it must begin with a BIP-39 node, given as a\n * `Uint8Array` of the seed bytes.\n *\n * All parameters are stringently validated, and an error is thrown if\n * validation fails.\n *\n * @param options - The options for the new node.\n * @param options.derivationPath - The rooted HD tree path that will be used\n * to derive the key of this node.\n * @param options.network - The network for the node. This is only used for\n * extended keys, and defaults to `mainnet`.\n * @param cryptographicFunctions - The cryptographic functions to use. If\n * provided, these will be used instead of the built-in implementations.\n * @returns A new BIP-44 node.\n */\n static async fromSeed(\n { derivationPath, network }: BIP44CoinTypeSeedOptions,\n cryptographicFunctions?: CryptographicFunctions,\n ): Promise<BIP44CoinTypeNode> {\n validateCoinTypeNodeDepth(derivationPath.length - 1);\n\n const node = await BIP44Node.fromSeed(\n {\n derivationPath,\n network,\n },\n cryptographicFunctions,\n );\n\n const coinType = getBIP44CoinType(derivationPath);\n return new BIP44CoinTypeNode(node, coinType);\n }\n\n /**\n * Constructs a BIP-44 `coin_type` node. `coin_type` is the index\n * specifying the protocol for which deeper keys are intended. For the\n * authoritative list of coin types, please see\n * [SLIP-44](https://github.com/satoshilabs/slips/blob/master/slip-0044.md).\n *\n * Recall that a BIP-44 HD tree path consists of the following nodes:\n *\n * `m / 44' / coin_type' / account' / change / address_index`\n *\n * With the following depths:\n *\n * `0 / 1 / 2 / 3 / 4 / 5`\n *\n * @param node - The {@link BIP44Node} for the key of this node.\n * @param coin_type - The coin_type index of this node. Must be a non-negative\n * integer.\n * @returns A BIP44CoinType node.\n */\n static async fromNode(\n node: BIP44Node,\n coin_type: number,\n ): Promise<BIP44CoinTypeNode> {\n if (!(node instanceof BIP44Node)) {\n throw new Error('Invalid node: Expected an instance of BIP44Node.');\n }\n\n validateCoinType(coin_type);\n validateCoinTypeNodeDepth(node.depth);\n\n // TODO: Make this function not async in a future version.\n return Promise.resolve(new BIP44CoinTypeNode(node, coin_type));\n }\n\n readonly #node: BIP44Node;\n\n public readonly path: CoinTypeHDPathString;\n\n public readonly coin_type: number;\n\n // Constructors cannot use hash names.\n // eslint-disable-next-line no-restricted-syntax\n private constructor(node: BIP44Node, coin_type: number) {\n this.#node = node;\n this.coin_type = coin_type;\n this.path = getBIP44CoinTypePathString(coin_type);\n\n Object.freeze(this);\n }\n\n public get depth(): BIP44Depth {\n return this.#node.depth;\n }\n\n public get privateKeyBytes(): Uint8Array | undefined {\n return this.#node.privateKeyBytes;\n }\n\n public get publicKeyBytes(): Uint8Array {\n return this.#node.publicKeyBytes;\n }\n\n public get chainCodeBytes(): Uint8Array {\n return this.#node.chainCodeBytes;\n }\n\n public get privateKey(): string | undefined {\n return this.#node.privateKey;\n }\n\n public get publicKey(): string {\n return this.#node.publicKey;\n }\n\n public get compressedPublicKey(): string {\n return this.#node.compressedPublicKey;\n }\n\n public get compressedPublicKeyBytes(): Uint8Array {\n return this.#node.compressedPublicKeyBytes;\n }\n\n public get chainCode(): string {\n return this.#node.chainCode;\n }\n\n public get address(): string {\n return this.#node.address;\n }\n\n public get masterFingerprint(): number | undefined {\n return this.#node.masterFingerprint;\n }\n\n public get parentFingerprint(): number {\n return this.#node.parentFingerprint;\n }\n\n public get fingerprint(): number {\n return this.#node.fingerprint;\n }\n\n public get index(): number {\n return this.#node.index;\n }\n\n public get network(): Network {\n return this.#node.network;\n }\n\n public get curve(): SupportedCurve {\n return this.#node.curve;\n }\n\n public get extendedKey(): string {\n return this.#node.extendedKey;\n }\n\n /**\n * Derives a BIP-44 `address_index` key corresponding to the path of this\n * node and the specified `account`, `change`, and `address_index` values.\n * `address_index` keys are normally the keys used to generate user account\n * addresses.\n *\n * Recall that a BIP-44 HD tree path consists of the following nodes:\n *\n * `m / 44' / coin_type' / account' / change / address_index`\n *\n * With the following depths:\n *\n * `0 / 1 / 2 / 3 / 4 / 5`\n *\n * @param indices - The BIP-44 index values to use in key derivation.\n * @param indices.account - The `account` index. Default: `0`\n * @param indices.change - The `change` index. Default: `0`\n * @param indices.address_index - The `address_index` index.\n * @returns The derived BIP-44 `address_index` node.\n */\n async deriveBIP44AddressKey({\n account = 0,\n change = 0,\n address_index,\n }: CoinTypeToAddressIndices): Promise<BIP44Node> {\n return await this.#node.derive(\n getBIP44CoinTypeToAddressPathTuple({ account, change, address_index }),\n );\n }\n\n toJSON(): JsonBIP44CoinTypeNode {\n return {\n ...this.#node.toJSON(),\n coin_type: this.coin_type,\n path: this.path,\n };\n }\n}\n\n/**\n * Validates the depth of a `coin_type` node. Simply, ensures that it is the\n * number `2`. An error is thrown if validation fails.\n *\n * @param depth - The depth to validate.\n */\nfunction validateCoinTypeNodeDepth(depth: number): void {\n if (depth !== BIP_44_COIN_TYPE_DEPTH) {\n throw new Error(\n `Invalid depth: Coin type nodes must be of depth ${BIP_44_COIN_TYPE_DEPTH}. Received: \"${depth}\"`,\n );\n }\n}\n\n/**\n * Validates that the coin type is a non-negative integer number. An error is\n * thrown if validation fails.\n *\n * @param coin_type - The coin type to validate.\n */\nfunction validateCoinType(coin_type: unknown): asserts coin_type is number {\n if (\n typeof coin_type !== 'number' ||\n !Number.isInteger(coin_type) ||\n coin_type < 0\n ) {\n throw new Error(\n 'Invalid coin type: The specified coin type must be a non-negative integer number.',\n );\n }\n}\n\n/**\n * Derives a BIP-44 address key corresponding to the specified derivation path,\n * given either by a {@link BIP44CoinTypeNode} or derivation path tuple.\n *\n * Recall that a BIP-44 HD tree path consists of the following nodes:\n *\n * `m / 44' / coin_type' / account' / change / address_index`\n *\n * With the following depths:\n *\n * `0 / 1 / 2 / 3 / 4 / 5`\n *\n * @param parentKeyOrNode - The `coin_type` parent key to derive from.\n * @param indices - The BIP-44 index values to use in key derivation.\n * @param indices.account - The `account` index. Default: `0`.\n * @param indices.change - The `change` index. Default: `0`.\n * @param indices.address_index - The `address_index` index.\n * @param cryptographicFunctions - The cryptographic functions to use. If\n * provided, these will be used instead of the built-in implementations.\n * @returns The derived `address_index` key for the specified derivation path.\n */\nexport async function deriveBIP44AddressKey(\n parentKeyOrNode: BIP44CoinTypeNode | JsonBIP44CoinTypeNode | string,\n { account = 0, change = 0, address_index }: CoinTypeToAddressIndices,\n cryptographicFunctions?: CryptographicFunctions,\n): Promise<BIP44Node> {\n const path = getBIP44CoinTypeToAddressPathTuple({\n account,\n change,\n address_index,\n });\n\n const node = await getNode(parentKeyOrNode, cryptographicFunctions);\n const childNode = await deriveChildNode(\n {\n path,\n node,\n },\n cryptographicFunctions,\n );\n\n return new BIP44Node(childNode);\n}\n\nexport type BIP44AddressKeyDeriver = {\n /**\n * @param address_index - The `address_index` value.\n * @param isHardened - Whether the derived index is hardened.\n * @returns The key corresponding to the path of this deriver and the\n * specified `address_index` value.\n */\n (address_index: number, isHardened?: boolean): Promise<BIP44Node>;\n\n /**\n * A human-readable representation of the derivation path of this deriver\n * function, excluding the `address_index`, which is parameterized.\n *\n * Recall that a BIP-44 HD tree path consists of the following nodes:\n *\n * `m / 44' / coin_type' / account' / change / address_index`\n *\n * With the following depths:\n *\n * `0 / 1 / 2 / 3 / 4 / 5`\n */\n path: ReturnType<typeof getBIP44ChangePathString>;\n\n /**\n * The `coin_type` index of addresses derived by this deriver function.\n */\n coin_type: number;\n};\n\n/**\n * Creates a function that derives BIP-44 address keys corresponding to the\n * specified derivation path, given either by a {@link BIP44CoinTypeNode} or\n * derivation path tuple.\n *\n * Recall that a BIP-44 HD tree path consists of the following nodes:\n *\n * `m / 44' / coin_type' / account' / change / address_index`\n *\n * With the following depths:\n *\n * `0 / 1 / 2 / 3 / 4 / 5`\n *\n * @param node - The {@link BIP44CoinTypeNode} to derive address keys from.\n * This node contains a BIP-44 key of depth 2, `coin_type`.\n * @param accountAndChangeIndices - The `account` and `change` indices that\n * will be used to derive addresses.\n * @param cryptographicFunctions - The cryptographic functions to use. If\n * provided, these will be used instead of the built-in implementations.\n * @returns The deriver function for the derivation path specified by the\n * `coin_type` node, `account`, and `change` indices.\n */\nexport async function getBIP44AddressKeyDeriver(\n node: BIP44CoinTypeNode | JsonBIP44CoinTypeNode | string,\n accountAndChangeIndices?: Omit<CoinTypeToAddressIndices, 'address_index'>,\n cryptographicFunctions?: CryptographicFunctions,\n): Promise<BIP44AddressKeyDeriver> {\n const { account = 0, change = 0 } = accountAndChangeIndices ?? {};\n\n const actualNode = await getNode(node, cryptographicFunctions);\n\n const accountNode = getHardenedBIP32NodeToken(account);\n const changeNode = getBIP32NodeToken(change);\n\n const bip44AddressKeyDeriver: BIP44AddressKeyDeriver = async (\n address_index: number,\n isHardened = false,\n ): Promise<BIP44Node> => {\n const slip10Node = await deriveChildNode(\n {\n path: [\n accountNode,\n changeNode,\n isHardened\n ? getHardenedBIP32NodeToken(address_index)\n : getUnhardenedBIP32NodeToken(address_index),\n ],\n node: actualNode,\n },\n cryptographicFunctions,\n );\n\n return new BIP44Node(slip10Node);\n };\n\n bip44AddressKeyDeriver.coin_type = actualNode.coin_type;\n bip44AddressKeyDeriver.path = getBIP44ChangePathString(actualNode.path, {\n account,\n change,\n });\n\n Object.freeze(bip44AddressKeyDeriver);\n\n return bip44AddressKeyDeriver;\n}\n\n/**\n * Get a BIP-44 coin type node from a JSON node or extended key string. If an existing coin type\n * node is provided, the same node is returned.\n *\n * The depth of the node is validated to be a valid coin type node.\n *\n * @param node - A BIP-44 coin type node, JSON node or extended key.\n * @param cryptographicFunctions - The cryptographic functions to use. If\n * provided, these will be used instead of the built-in implementations. This is\n * only used if the node is an extended key string or JSON object.\n * @returns A BIP44CoinType node.\n */\nasync function getNode(\n node: BIP44CoinTypeNode | JsonBIP44CoinTypeNode | string,\n cryptographicFunctions?: CryptographicFunctions,\n): Promise<BIP44CoinTypeNode> {\n if (node instanceof BIP44CoinTypeNode) {\n validateCoinTypeNodeDepth(node.depth);\n\n return node;\n }\n\n if (typeof node === 'string') {\n const bip44Node = await BIP44Node.fromExtendedKey(\n node,\n cryptographicFunctions,\n );\n const coinTypeNode = await BIP44CoinTypeNode.fromNode(\n bip44Node,\n bip44Node.index - BIP_32_HARDENED_OFFSET,\n );\n\n validateCoinTypeNodeDepth(coinTypeNode.depth);\n\n return coinTypeNode;\n }\n\n return BIP44CoinTypeNode.fromJSON(\n node,\n node.coin_type,\n cryptographicFunctions,\n );\n}\n"]}
@@ -0,0 +1,220 @@
1
+ import type { BIP44Depth, Network, PartialHDPathTuple, RootedSLIP10PathTuple, RootedSLIP10SeedPathTuple } from './constants.js';
2
+ import type { CryptographicFunctions } from './cryptography.js';
3
+ import type { SupportedCurve } from './curves/index.js';
4
+ import { SLIP10Node } from './SLIP10Node.js';
5
+ export type BIP44ExtendedKeyOptions = {
6
+ readonly depth: number;
7
+ readonly parentFingerprint: number;
8
+ readonly index: number;
9
+ readonly network?: Network | undefined;
10
+ readonly chainCode: Uint8Array | string;
11
+ readonly privateKey?: Uint8Array | string | undefined;
12
+ readonly publicKey?: Uint8Array | string | undefined;
13
+ };
14
+ export type BIP44DerivationPathOptions = {
15
+ readonly derivationPath: RootedSLIP10PathTuple;
16
+ readonly network?: Network | undefined;
17
+ };
18
+ export type BIP44SeedOptions = {
19
+ readonly derivationPath: RootedSLIP10SeedPathTuple;
20
+ readonly network?: Network | undefined;
21
+ };
22
+ /**
23
+ * A wrapper for BIP-44 Hierarchical Deterministic (HD) tree nodes, i.e.
24
+ * cryptographic keys used to generate keypairs and addresses for cryptocurrency
25
+ * protocols.
26
+ */
27
+ export type JsonBIP44Node = {
28
+ /**
29
+ * The 0-indexed BIP-44 path depth of this node.
30
+ *
31
+ * A BIP-44 path is of the form:
32
+ *
33
+ * `m / 44' / coin_type' / account' / change / address_index`
34
+ *
35
+ * With the following depths:
36
+ *
37
+ * `0 / 1 / 2 / 3 / 4 / 5`
38
+ */
39
+ readonly depth: BIP44Depth;
40
+ /**
41
+ * The fingerprint of the master node, i.e., the node at depth 0. May be
42
+ * undefined if this node was created from an extended key.
43
+ */
44
+ readonly masterFingerprint?: number | undefined;
45
+ /**
46
+ * The fingerprint of the parent key, or 0 if this is a master node.
47
+ */
48
+ readonly parentFingerprint: number;
49
+ /**
50
+ * The index of the node, or 0 if this is a master node.
51
+ */
52
+ readonly index: number;
53
+ /**
54
+ * The network for the node. This is only used for extended keys, and defaults
55
+ * to `mainnet`.
56
+ */
57
+ readonly network?: Network | undefined;
58
+ /**
59
+ * The hexadecimal string representation of the private key for this node.
60
+ * May be `undefined` if the node is a public node.
61
+ */
62
+ readonly privateKey?: string | undefined;
63
+ /**
64
+ * The hexadecimal string representation of the public key for this node.
65
+ */
66
+ readonly publicKey: string;
67
+ /**
68
+ * The hexadecimal string representation of the chain code for this node.
69
+ */
70
+ readonly chainCode: string;
71
+ };
72
+ export type BIP44NodeInterface = JsonBIP44Node & {
73
+ /**
74
+ * @returns A JSON-compatible representation of this node's data fields.
75
+ */
76
+ toJSON(): JsonBIP44Node;
77
+ };
78
+ /**
79
+ * A wrapper for BIP-44 Hierarchical Deterministic (HD) tree nodes, i.e.
80
+ * cryptographic keys used to generate keypairs and addresses for cryptocurrency
81
+ * protocols.
82
+ *
83
+ * This class contains methods and fields that may not serialize well. Use
84
+ * {@link BIP44Node.toJSON} to get a JSON-compatible representation.
85
+ */
86
+ export declare class BIP44Node implements BIP44NodeInterface {
87
+ #private;
88
+ /**
89
+ * Wrapper of the {@link fromExtendedKey} function. Refer to that function
90
+ * for documentation.
91
+ *
92
+ * @param json - The JSON representation of a SLIP-10 node.
93
+ * @param cryptographicFunctions - The cryptographic functions to use. If
94
+ * provided, these will be used instead of the built-in implementations.
95
+ * @returns A BIP44 node.
96
+ */
97
+ static fromJSON(json: JsonBIP44Node, cryptographicFunctions?: CryptographicFunctions): Promise<BIP44Node>;
98
+ /**
99
+ * Create a new BIP-44 node from a key and chain code. You must specify
100
+ * either a private key or a public key. When specifying a private key,
101
+ * the public key will be derived from the private key.
102
+ *
103
+ * All parameters are stringently validated, and an error is thrown if
104
+ * validation fails.
105
+ *
106
+ * @param options - An object containing the extended key, or an extended
107
+ * public (xpub) or private (xprv) key.
108
+ * @param options.depth - The depth of the node.
109
+ * @param options.network - The network for the node. This is only used for
110
+ * extended keys, and defaults to `mainnet`.
111
+ * @param options.privateKey - The private key for the node.
112
+ * @param options.publicKey - The public key for the node. If a private key is
113
+ * specified, this parameter is ignored.
114
+ * @param options.chainCode - The chain code for the node.
115
+ * @param cryptographicFunctions - The cryptographic functions to use. If
116
+ * provided, these will be used instead of the built-in implementations.
117
+ * @returns A BIP44 node.
118
+ */
119
+ static fromExtendedKey(options: BIP44ExtendedKeyOptions | string, cryptographicFunctions?: CryptographicFunctions): Promise<BIP44Node>;
120
+ /**
121
+ * Create a new BIP-44 node from a derivation path. The derivation path
122
+ * must be rooted, i.e. it must begin with a BIP-39 node, given as a string of
123
+ * the form `bip39:MNEMONIC`, where `MNEMONIC` is a space-separated list of
124
+ * BIP-39 seed phrase words.
125
+ *
126
+ * All parameters are stringently validated, and an error is thrown if
127
+ * validation fails.
128
+ *
129
+ * Recall that a BIP-44 HD tree path consists of the following nodes:
130
+ *
131
+ * `m / 44' / coin_type' / account' / change / address_index`
132
+ *
133
+ * With the following depths:
134
+ *
135
+ * `0 / 1 / 2 / 3 / 4 / 5`
136
+ *
137
+ * @param options - An object containing the derivation path.
138
+ * @param options.derivationPath - The rooted HD tree path that will be used
139
+ * to derive the key of this node.
140
+ * @param options.network - The network for the node. This is only used for
141
+ * extended keys, and defaults to `mainnet`.
142
+ * @param cryptographicFunctions - The cryptographic functions to use. If
143
+ * provided, these will be used instead of the built-in implementations.
144
+ * @returns A BIP44 node.
145
+ */
146
+ static fromDerivationPath({ derivationPath, network }: BIP44DerivationPathOptions, cryptographicFunctions?: CryptographicFunctions): Promise<BIP44Node>;
147
+ /**
148
+ * Create a new BIP-44 node from a BIP-39 seed. The derivation path must be
149
+ * rooted, i.e. it must begin with a BIP-39 node, given as a `Uint8Array` of
150
+ * the seed bytes.
151
+ *
152
+ * All parameters are stringently validated, and an error is thrown if
153
+ * validation fails.
154
+ *
155
+ * @param options - The options for the new node.
156
+ * @param options.derivationPath - The rooted HD tree path that will be used
157
+ * to derive the key of this node.
158
+ * @param options.network - The network for the node. This is only used for
159
+ * extended keys, and defaults to `mainnet`.
160
+ * @param cryptographicFunctions - The cryptographic functions to use. If
161
+ * provided, these will be used instead of the built-in implementations.
162
+ * @returns A new BIP-44 node.
163
+ */
164
+ static fromSeed({ derivationPath, network }: BIP44SeedOptions, cryptographicFunctions?: CryptographicFunctions): Promise<BIP44Node>;
165
+ get depth(): BIP44Depth;
166
+ get privateKeyBytes(): Uint8Array | undefined;
167
+ get publicKeyBytes(): Uint8Array;
168
+ get chainCodeBytes(): Uint8Array;
169
+ get privateKey(): string | undefined;
170
+ get publicKey(): string;
171
+ get compressedPublicKey(): string;
172
+ get compressedPublicKeyBytes(): Uint8Array;
173
+ get chainCode(): string;
174
+ get address(): string;
175
+ get masterFingerprint(): number | undefined;
176
+ get parentFingerprint(): number;
177
+ get fingerprint(): number;
178
+ get index(): number;
179
+ get network(): Network;
180
+ get extendedKey(): string;
181
+ get curve(): SupportedCurve;
182
+ constructor(node: SLIP10Node);
183
+ /**
184
+ * Get a neutered version of this node, i.e. a node without a private key.
185
+ *
186
+ * @returns A neutered version of this node.
187
+ */
188
+ neuter(): BIP44Node;
189
+ /**
190
+ * Derives a child of the key contains be this node and returns a new
191
+ * {@link BIP44Node} containing the child key.
192
+ *
193
+ * The specified path must be a valid HD path from this node, per BIP-44.
194
+ * At present, this means that the path must consist of no more than 5 BIP-32
195
+ * nodes, depending on the depth of this node.
196
+ *
197
+ * Recall that a BIP-44 HD tree path consists of the following nodes:
198
+ *
199
+ * `m / 44' / coin_type' / account' / change / address_index`
200
+ *
201
+ * With the following depths:
202
+ *
203
+ * `0 / 1 / 2 / 3 / 4 / 5`
204
+ *
205
+ * @param path - The partial (non-rooted) BIP-44 HD tree path will be used
206
+ * to derive a child key from the parent key contained within this node.
207
+ * @returns The {@link BIP44Node} corresponding to the derived child key.
208
+ */
209
+ derive(path: PartialHDPathTuple): Promise<BIP44Node>;
210
+ toJSON(): JsonBIP44Node;
211
+ }
212
+ /**
213
+ * Validates a BIP-44 path depth. Effectively, asserts that the depth is an
214
+ * integer `number` N such that 0 <= N <= 5. Throws an error if validation
215
+ * fails.
216
+ *
217
+ * @param depth - The depth to validate.
218
+ */
219
+ export declare function validateBIP44Depth(depth: unknown): asserts depth is BIP44Depth;
220
+ //# sourceMappingURL=BIP44Node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BIP44Node.d.ts","sourceRoot":"","sources":["../src/BIP44Node.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,UAAU,EACV,OAAO,EACP,kBAAkB,EAClB,qBAAqB,EACrB,yBAAyB,EAE1B,MAAM,gBAAgB,CAAC;AAQxB,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,EAAE,UAAU,EAAsB,MAAM,iBAAiB,CAAC;AAGjE,MAAM,MAAM,uBAAuB,GAAG;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;IACnC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACvC,QAAQ,CAAC,SAAS,EAAE,UAAU,GAAG,MAAM,CAAC;IACxC,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,SAAS,CAAC;IACtD,QAAQ,CAAC,SAAS,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,SAAS,CAAC;CACtD,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC,QAAQ,CAAC,cAAc,EAAE,qBAAqB,CAAC;IAC/C,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,CAAC,cAAc,EAAE,yBAAyB,CAAC;IACnD,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CACxC,C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