@mysten/sui 2.36.0 → 2.38.0

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 (63) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/bcs/bcs.d.mts.map +1 -1
  3. package/dist/bcs/bcs.mjs +48 -14
  4. package/dist/bcs/bcs.mjs.map +1 -1
  5. package/dist/cryptography/mnemonics.d.mts +4 -1
  6. package/dist/cryptography/mnemonics.d.mts.map +1 -1
  7. package/dist/cryptography/mnemonics.mjs +5 -2
  8. package/dist/cryptography/mnemonics.mjs.map +1 -1
  9. package/dist/cryptography/slip10.d.mts +5 -0
  10. package/dist/cryptography/slip10.d.mts.map +1 -0
  11. package/dist/cryptography/slip10.mjs +55 -0
  12. package/dist/cryptography/slip10.mjs.map +1 -0
  13. package/dist/grpc/proto/sui/forking/v1alpha/forking_service.client.d.mts +4 -4
  14. package/dist/grpc/proto/sui/rpc/v2/ledger_service.client.d.mts +4 -4
  15. package/dist/grpc/proto/sui/rpc/v2/move_package_service.client.d.mts +4 -4
  16. package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
  17. package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
  18. package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +4 -4
  19. package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.client.d.mts +4 -4
  20. package/dist/keypairs/ed25519/keypair.d.mts.map +1 -1
  21. package/dist/keypairs/ed25519/keypair.mjs +6 -9
  22. package/dist/keypairs/ed25519/keypair.mjs.map +1 -1
  23. package/dist/keypairs/mldsa65/index.d.mts +3 -2
  24. package/dist/keypairs/mldsa65/index.mjs +3 -2
  25. package/dist/keypairs/mldsa65/keypair.d.mts +19 -1
  26. package/dist/keypairs/mldsa65/keypair.d.mts.map +1 -1
  27. package/dist/keypairs/mldsa65/keypair.mjs +33 -1
  28. package/dist/keypairs/mldsa65/keypair.mjs.map +1 -1
  29. package/dist/multisig/publickey.d.mts +14 -3
  30. package/dist/multisig/publickey.d.mts.map +1 -1
  31. package/dist/multisig/publickey.mjs +22 -5
  32. package/dist/multisig/publickey.mjs.map +1 -1
  33. package/dist/transactions/Transaction.d.mts +6 -6
  34. package/dist/transactions/data/internal.d.mts +133 -133
  35. package/dist/transactions/data/internal.d.mts.map +1 -1
  36. package/dist/transactions/data/v1.d.mts +239 -239
  37. package/dist/transactions/data/v1.d.mts.map +1 -1
  38. package/dist/transactions/data/v2.d.mts +16 -16
  39. package/dist/transactions/data/v2.d.mts.map +1 -1
  40. package/dist/verify/verify.d.mts.map +1 -1
  41. package/dist/verify/verify.mjs +2 -2
  42. package/dist/verify/verify.mjs.map +1 -1
  43. package/dist/version.mjs +1 -1
  44. package/dist/version.mjs.map +1 -1
  45. package/dist/zklogin/bcs.d.mts +14 -14
  46. package/dist/zklogin/publickey.d.mts.map +1 -1
  47. package/dist/zklogin/publickey.mjs +2 -2
  48. package/dist/zklogin/publickey.mjs.map +1 -1
  49. package/docs/cryptography/keypairs.md +4 -2
  50. package/package.json +7 -7
  51. package/src/bcs/bcs.ts +69 -14
  52. package/src/cryptography/mnemonics.ts +5 -2
  53. package/src/cryptography/slip10.ts +84 -0
  54. package/src/keypairs/ed25519/keypair.ts +11 -14
  55. package/src/keypairs/mldsa65/index.ts +6 -1
  56. package/src/keypairs/mldsa65/keypair.ts +46 -0
  57. package/src/multisig/publickey.ts +55 -14
  58. package/src/verify/verify.ts +4 -2
  59. package/src/version.ts +1 -1
  60. package/src/zklogin/publickey.ts +4 -4
  61. package/dist/keypairs/ed25519/ed25519-hd-key.mjs +0 -45
  62. package/dist/keypairs/ed25519/ed25519-hd-key.mjs.map +0 -1
  63. package/src/keypairs/ed25519/ed25519-hd-key.ts +0 -83
@@ -1,7 +1,7 @@
1
1
  // Copyright (c) Mysten Labs, Inc.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { toHex } from '@mysten/bcs';
4
+ import { fromHex } from '@mysten/bcs';
5
5
  import { ed25519 } from '@noble/curves/ed25519.js';
6
6
 
7
7
  import {
@@ -10,9 +10,9 @@ import {
10
10
  Keypair,
11
11
  PRIVATE_KEY_SIZE,
12
12
  } from '../../cryptography/keypair.js';
13
- import { isValidHardenedPath, mnemonicToSeedHex } from '../../cryptography/mnemonics.js';
13
+ import { isValidHardenedPath, mnemonicToSeed } from '../../cryptography/mnemonics.js';
14
14
  import type { SignatureScheme } from '../../cryptography/signature-scheme.js';
15
- import { derivePath } from './ed25519-hd-key.js';
15
+ import { derivePath, ED25519_SLIP10_MASTER_KEY } from '../../cryptography/slip10.js';
16
16
  import { Ed25519PublicKey } from './publickey.js';
17
17
 
18
18
  export const DEFAULT_ED25519_DERIVATION_PATH = "m/44'/784'/0'/0'/0'";
@@ -151,15 +151,7 @@ export class Ed25519Keypair extends Keypair {
151
151
  * be compliant to SLIP-0010 in form m/44'/784'/{account_index}'/{change_index}'/{address_index}'.
152
152
  */
153
153
  static deriveKeypair(mnemonics: string, path?: string): Ed25519Keypair {
154
- if (path == null) {
155
- path = DEFAULT_ED25519_DERIVATION_PATH;
156
- }
157
- if (!isValidHardenedPath(path)) {
158
- throw new Error('Invalid derivation path');
159
- }
160
- const { key } = derivePath(path, mnemonicToSeedHex(mnemonics));
161
-
162
- return Ed25519Keypair.fromSecretKey(key);
154
+ return Ed25519Keypair.deriveKeypairFromSeed(mnemonicToSeed(mnemonics), path);
163
155
  }
164
156
 
165
157
  /**
@@ -177,9 +169,14 @@ export class Ed25519Keypair extends Keypair {
177
169
  if (!isValidHardenedPath(path)) {
178
170
  throw new Error('Invalid derivation path');
179
171
  }
180
- const seedHex = typeof seed === 'string' ? seed : toHex(seed);
181
- const { key } = derivePath(path, seedHex);
172
+ const { key, chainCode } = derivePath(
173
+ ED25519_SLIP10_MASTER_KEY,
174
+ path,
175
+ typeof seed === 'string' ? fromHex(seed) : seed,
176
+ );
182
177
 
178
+ // The keypair keeps `key` as its secret; only the chain code is surplus.
179
+ chainCode.fill(0);
183
180
  return Ed25519Keypair.fromSecretKey(key);
184
181
  }
185
182
  }
@@ -1,5 +1,10 @@
1
1
  // Copyright (c) Mysten Labs, Inc.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- export { MLDSA65Keypair, type MLDSA65KeypairData } from './keypair.js';
4
+ export {
5
+ DEFAULT_MLDSA65_DERIVATION_PATH,
6
+ MLDSA65Keypair,
7
+ type MLDSA65KeypairData,
8
+ } from './keypair.js';
9
+ export { MLDSA65_SLIP10_MASTER_KEY } from '../../cryptography/slip10.js';
5
10
  export { MLDSA65PublicKey, MLDSA65_PUBLIC_KEY_SIZE, MLDSA65_SIGNATURE_SIZE } from './publickey.js';
@@ -1,6 +1,7 @@
1
1
  // Copyright (c) Mysten Labs, Inc.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { fromHex } from '@mysten/bcs';
4
5
  import { randomBytes } from '@noble/hashes/utils.js';
5
6
  import { ml_dsa65 } from '@noble/post-quantum/ml-dsa.js';
6
7
 
@@ -10,9 +11,15 @@ import {
10
11
  Keypair,
11
12
  PRIVATE_KEY_SIZE,
12
13
  } from '../../cryptography/keypair.js';
14
+ import { isValidHardenedPath, mnemonicToSeed } from '../../cryptography/mnemonics.js';
13
15
  import type { SignatureScheme } from '../../cryptography/signature-scheme.js';
16
+ import { derivePath, MLDSA65_SLIP10_MASTER_KEY } from '../../cryptography/slip10.js';
14
17
  import { MLDSA65PublicKey } from './publickey.js';
15
18
 
19
+ export const DEFAULT_MLDSA65_DERIVATION_PATH = "m/94'/784'/0'/0'/0'";
20
+
21
+ const MLDSA65_DERIVATION_PURPOSE = 94;
22
+
16
23
  /**
17
24
  * ML-DSA-65 Keypair data. `seed` is the 32-byte FIPS 204 seed, the only form
18
25
  * that is ever exported or stored (`suiprivkey`, keystore). `secretKey` is the
@@ -117,6 +124,45 @@ export class MLDSA65Keypair extends Keypair {
117
124
  async sign(data: Uint8Array) {
118
125
  return ml_dsa65.sign(data, this.keypair.secretKey) as Uint8Array<ArrayBuffer>;
119
126
  }
127
+
128
+ /**
129
+ * Derive ML-DSA-65 keypair from mnemonics and path. The mnemonics must be normalized
130
+ * and validated against the english wordlist.
131
+ *
132
+ * If path is none, it will default to m/94'/784'/0'/0'/0', otherwise the path must
133
+ * be compliant to SLIP-0010 in form m/94'/784'/{account_index}'/{change_index}'/{address_index}'.
134
+ */
135
+ static deriveKeypair(mnemonics: string, path?: string): MLDSA65Keypair {
136
+ return MLDSA65Keypair.deriveKeypairFromSeed(mnemonicToSeed(mnemonics), path);
137
+ }
138
+
139
+ /**
140
+ * Derive ML-DSA-65 keypair from mnemonicSeed and path.
141
+ *
142
+ * If path is none, it will default to m/94'/784'/0'/0'/0', otherwise the path must
143
+ * be compliant to SLIP-0010 in form m/94'/784'/{account_index}'/{change_index}'/{address_index}'.
144
+ *
145
+ * @param seed - The seed as a hex string or Uint8Array.
146
+ */
147
+ static deriveKeypairFromSeed(seed: string | Uint8Array, path?: string): MLDSA65Keypair {
148
+ if (path == null) {
149
+ path = DEFAULT_MLDSA65_DERIVATION_PATH;
150
+ }
151
+ if (!isValidHardenedPath(path, MLDSA65_DERIVATION_PURPOSE)) {
152
+ throw new Error('Invalid derivation path');
153
+ }
154
+ const { key, chainCode } = derivePath(
155
+ MLDSA65_SLIP10_MASTER_KEY,
156
+ path,
157
+ typeof seed === 'string' ? fromHex(seed) : seed,
158
+ );
159
+
160
+ // `fromSecretKey` copies the seed, so the node can be wiped here.
161
+ const keypair = MLDSA65Keypair.fromSecretKey(key);
162
+ key.fill(0);
163
+ chainCode.fill(0);
164
+ return keypair;
165
+ }
120
166
  }
121
167
 
122
168
  function expand(input: Uint8Array): MLDSA65KeypairData {
@@ -71,6 +71,7 @@ export class MultiSigPublicKey extends PublicKey {
71
71
  weight: number;
72
72
  publicKey: PublicKey;
73
73
  }[];
74
+ #options: { client?: ClientWithCoreApi };
74
75
  /**
75
76
  * Create a new MultiSigPublicKey object
76
77
  */
@@ -83,6 +84,8 @@ export class MultiSigPublicKey extends PublicKey {
83
84
  ) {
84
85
  super();
85
86
 
87
+ this.#options = options;
88
+
86
89
  if (typeof value === 'string') {
87
90
  this.rawBytes = fromBase64(value);
88
91
 
@@ -143,21 +146,27 @@ export class MultiSigPublicKey extends PublicKey {
143
146
  static fromPublicKeys({
144
147
  threshold,
145
148
  publicKeys,
149
+ client,
146
150
  }: {
147
151
  threshold: number;
148
152
  publicKeys: { publicKey: PublicKey; weight: number }[];
153
+ /** Client used to verify zkLogin member signatures */
154
+ client?: ClientWithCoreApi;
149
155
  }) {
150
- return new MultiSigPublicKey({
151
- pk_map: publicKeys.map(({ publicKey, weight }) => {
152
- const scheme = SIGNATURE_FLAG_TO_SCHEME[publicKey.flag() as SignatureFlag];
153
-
154
- return {
155
- pubKey: { [scheme]: publicKey.toRawBytes() } as PublicKeyEnum,
156
- weight,
157
- };
158
- }),
159
- threshold,
160
- });
156
+ return new MultiSigPublicKey(
157
+ {
158
+ pk_map: publicKeys.map(({ publicKey, weight }) => {
159
+ const scheme = SIGNATURE_FLAG_TO_SCHEME[publicKey.flag() as SignatureFlag];
160
+
161
+ return {
162
+ pubKey: { [scheme]: publicKey.toRawBytes() } as PublicKeyEnum,
163
+ weight,
164
+ };
165
+ }),
166
+ threshold,
167
+ },
168
+ { client },
169
+ );
161
170
  }
162
171
 
163
172
  /**
@@ -216,9 +225,38 @@ export class MultiSigPublicKey extends PublicKey {
216
225
  }
217
226
 
218
227
  /**
219
- * Verifies that the signature is valid for for the provided message
228
+ * Verifies that the signature is valid for the provided message
220
229
  */
221
230
  async verify(message: Uint8Array, multisigSignature: string): Promise<boolean> {
231
+ return this.#verifyMembers(multisigSignature, (publicKey, signature) =>
232
+ publicKey.verify(message, signature),
233
+ );
234
+ }
235
+
236
+ /**
237
+ * Verifies that the signature is valid for the provided PersonalMessage
238
+ */
239
+ override verifyPersonalMessage(message: Uint8Array, multisigSignature: string): Promise<boolean> {
240
+ // Each member verifies with its own intent-aware method, so members that cannot verify a
241
+ // raw digest (zkLogin, which is verified through the client) still work.
242
+ return this.#verifyMembers(multisigSignature, (publicKey, signature) =>
243
+ publicKey.verifyPersonalMessage(message, signature),
244
+ );
245
+ }
246
+
247
+ /**
248
+ * Verifies that the signature is valid for the provided Transaction
249
+ */
250
+ override verifyTransaction(transaction: Uint8Array, multisigSignature: string): Promise<boolean> {
251
+ return this.#verifyMembers(multisigSignature, (publicKey, signature) =>
252
+ publicKey.verifyTransaction(transaction, signature),
253
+ );
254
+ }
255
+
256
+ async #verifyMembers(
257
+ multisigSignature: string,
258
+ verifyMember: (publicKey: PublicKey, signature: Uint8Array) => Promise<boolean>,
259
+ ): Promise<boolean> {
222
260
  // Multisig verification only supports serialized signature
223
261
  const parsed = parseSerializedSignature(multisigSignature);
224
262
 
@@ -239,8 +277,11 @@ export class MultiSigPublicKey extends PublicKey {
239
277
  return false;
240
278
  }
241
279
 
242
- for (const { publicKey, weight, signature } of parsePartialSignatures(multisig)) {
243
- if (!(await publicKey.verify(message, signature))) {
280
+ for (const { publicKey, weight, signature } of parsePartialSignatures(
281
+ multisig,
282
+ this.#options,
283
+ )) {
284
+ if (!(await verifyMember(publicKey, signature))) {
244
285
  return false;
245
286
  }
246
287
 
@@ -111,7 +111,9 @@ function parseSignature(signature: string, options: { client?: ClientWithCoreApi
111
111
  if (parsedSignature.signatureScheme === 'MultiSig') {
112
112
  return {
113
113
  ...parsedSignature,
114
- publicKey: new MultiSigPublicKey(parsedSignature.multisig.multisig_pk),
114
+ publicKey: new MultiSigPublicKey(parsedSignature.multisig.multisig_pk, {
115
+ client: options.client,
116
+ }),
115
117
  };
116
118
  }
117
119
 
@@ -152,7 +154,7 @@ export function publicKeyFromRawBytes(
152
154
  publicKey = new Secp256r1PublicKey(bytes);
153
155
  break;
154
156
  case 'MultiSig':
155
- publicKey = new MultiSigPublicKey(bytes);
157
+ publicKey = new MultiSigPublicKey(bytes, { client: options.client });
156
158
  break;
157
159
  case 'ZkLogin':
158
160
  publicKey = ZkLoginPublicIdentifier.fromBytes(bytes, options);
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
 
4
4
  // This file is generated by genversion.mjs. Do not edit it directly.
5
5
 
6
- export const PACKAGE_VERSION = '2.36.0';
6
+ export const PACKAGE_VERSION = '2.38.0';
@@ -168,10 +168,11 @@ export class ZkLoginPublicIdentifier extends PublicKey {
168
168
  */
169
169
  verifyPersonalMessage(message: Uint8Array, signature: Uint8Array | string): Promise<boolean> {
170
170
  const parsedSignature = parseSerializedZkLoginSignature(signature);
171
- const address = new ZkLoginPublicIdentifier(parsedSignature.publicKey).toSuiAddress();
172
171
 
173
172
  return graphqlVerifyZkLoginSignature({
174
- address: address,
173
+ // Verify against this identifier's address, not one derived from the signature, so a
174
+ // signature from a different zkLogin identity is rejected (as the chain does for multisig).
175
+ address: this.toSuiAddress(),
175
176
  bytes: toBase64(message),
176
177
  signature: parsedSignature.serializedSignature,
177
178
  intentScope: 'PersonalMessage',
@@ -184,9 +185,8 @@ export class ZkLoginPublicIdentifier extends PublicKey {
184
185
  */
185
186
  verifyTransaction(transaction: Uint8Array, signature: Uint8Array | string): Promise<boolean> {
186
187
  const parsedSignature = parseSerializedZkLoginSignature(signature);
187
- const address = new ZkLoginPublicIdentifier(parsedSignature.publicKey).toSuiAddress();
188
188
  return graphqlVerifyZkLoginSignature({
189
- address: address,
189
+ address: this.toSuiAddress(),
190
190
  bytes: toBase64(transaction),
191
191
  signature: parsedSignature.serializedSignature,
192
192
  intentScope: 'TransactionData',
@@ -1,45 +0,0 @@
1
- import { fromHex } from "@mysten/bcs";
2
- import { sha512 } from "@noble/hashes/sha2.js";
3
- import { hmac } from "@noble/hashes/hmac.js";
4
-
5
- //#region src/keypairs/ed25519/ed25519-hd-key.ts
6
- const ED25519_CURVE = "ed25519 seed";
7
- const HARDENED_OFFSET = 2147483648;
8
- const pathRegex = /* @__PURE__ */ new RegExp("^m(\\/[0-9]+')+$");
9
- const replaceDerive = (val) => val.replace("'", "");
10
- const getMasterKeyFromSeed = (seed) => {
11
- const I = hmac.create(sha512, new TextEncoder().encode(ED25519_CURVE)).update(fromHex(seed)).digest();
12
- return {
13
- key: I.slice(0, 32),
14
- chainCode: I.slice(32)
15
- };
16
- };
17
- const CKDPriv = ({ key, chainCode }, index) => {
18
- const indexBuffer = /* @__PURE__ */ new ArrayBuffer(4);
19
- new DataView(indexBuffer).setUint32(0, index);
20
- const data = new Uint8Array(1 + key.length + indexBuffer.byteLength);
21
- data.set(new Uint8Array(1).fill(0));
22
- data.set(key, 1);
23
- data.set(new Uint8Array(indexBuffer, 0, indexBuffer.byteLength), key.length + 1);
24
- const I = hmac.create(sha512, chainCode).update(data).digest();
25
- return {
26
- key: I.slice(0, 32),
27
- chainCode: I.slice(32)
28
- };
29
- };
30
- const isValidPath = (path) => {
31
- if (!pathRegex.test(path)) return false;
32
- return !path.split("/").slice(1).map(replaceDerive).some(isNaN);
33
- };
34
- const derivePath = (path, seed, offset = HARDENED_OFFSET) => {
35
- if (!isValidPath(path)) throw new Error("Invalid derivation path");
36
- const { key, chainCode } = getMasterKeyFromSeed(seed);
37
- return path.split("/").slice(1).map(replaceDerive).map((el) => parseInt(el, 10)).reduce((parentKeys, segment) => CKDPriv(parentKeys, segment + offset), {
38
- key,
39
- chainCode
40
- });
41
- };
42
-
43
- //#endregion
44
- export { derivePath };
45
- //# sourceMappingURL=ed25519-hd-key.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ed25519-hd-key.mjs","names":[],"sources":["../../../src/keypairs/ed25519/ed25519-hd-key.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This is adapted from https://github.com/alepop/ed25519-hd-key replacing create-hmac\n// with @noble/hashes to be browser compatible.\n\nimport { fromHex } from '@mysten/bcs';\nimport { hmac } from '@noble/hashes/hmac.js';\nimport { sha512 } from '@noble/hashes/sha2.js';\n\ntype Hex = string;\ntype Path = string;\n\ntype Keys = {\n\tkey: Uint8Array;\n\tchainCode: Uint8Array;\n};\n\nconst ED25519_CURVE = 'ed25519 seed';\nconst HARDENED_OFFSET = 0x80000000;\n\nconst pathRegex = new RegExp(\"^m(\\\\/[0-9]+')+$\");\n\nconst replaceDerive = (val: string): string => val.replace(\"'\", '');\n\nconst getMasterKeyFromSeed = (seed: Hex): Keys => {\n\tconst h = hmac.create(sha512, new TextEncoder().encode(ED25519_CURVE));\n\tconst I = h.update(fromHex(seed)).digest();\n\tconst IL = I.slice(0, 32);\n\tconst IR = I.slice(32);\n\treturn {\n\t\tkey: IL,\n\t\tchainCode: IR,\n\t};\n};\n\nconst CKDPriv = ({ key, chainCode }: Keys, index: number): Keys => {\n\tconst indexBuffer = new ArrayBuffer(4);\n\tconst cv = new DataView(indexBuffer);\n\tcv.setUint32(0, index);\n\n\tconst data = new Uint8Array(1 + key.length + indexBuffer.byteLength);\n\tdata.set(new Uint8Array(1).fill(0));\n\tdata.set(key, 1);\n\tdata.set(new Uint8Array(indexBuffer, 0, indexBuffer.byteLength), key.length + 1);\n\n\tconst I = hmac.create(sha512, chainCode).update(data).digest();\n\tconst IL = I.slice(0, 32);\n\tconst IR = I.slice(32);\n\treturn {\n\t\tkey: IL,\n\t\tchainCode: IR,\n\t};\n};\n\nconst isValidPath = (path: string): boolean => {\n\tif (!pathRegex.test(path)) {\n\t\treturn false;\n\t}\n\treturn !path\n\t\t.split('/')\n\t\t.slice(1)\n\t\t.map(replaceDerive)\n\t\t.some(isNaN as any /* ts T_T*/);\n};\n\nexport const derivePath = (path: Path, seed: Hex, offset = HARDENED_OFFSET): Keys => {\n\tif (!isValidPath(path)) {\n\t\tthrow new Error('Invalid derivation path');\n\t}\n\n\tconst { key, chainCode } = getMasterKeyFromSeed(seed);\n\tconst segments = path\n\t\t.split('/')\n\t\t.slice(1)\n\t\t.map(replaceDerive)\n\t\t.map((el) => parseInt(el, 10));\n\n\treturn segments.reduce((parentKeys, segment) => CKDPriv(parentKeys, segment + offset), {\n\t\tkey,\n\t\tchainCode,\n\t});\n};\n"],"mappings":";;;;;AAkBA,MAAM,gBAAgB;AACtB,MAAM,kBAAkB;AAExB,MAAM,4BAAY,IAAI,OAAO,mBAAmB;AAEhD,MAAM,iBAAiB,QAAwB,IAAI,QAAQ,KAAK,GAAG;AAEnE,MAAM,wBAAwB,SAAoB;CAEjD,MAAM,IADI,KAAK,OAAO,QAAQ,IAAI,aAAa,CAAC,OAAO,cAAc,CAAC,CAC1D,OAAO,QAAQ,KAAK,CAAC,CAAC,QAAQ;AAG1C,QAAO;EACN,KAHU,EAAE,MAAM,GAAG,GAAG;EAIxB,WAHU,EAAE,MAAM,GAAG;EAIrB;;AAGF,MAAM,WAAW,EAAE,KAAK,aAAmB,UAAwB;CAClE,MAAM,8BAAc,IAAI,YAAY,EAAE;AAEtC,CADW,IAAI,SAAS,YAAY,CACjC,UAAU,GAAG,MAAM;CAEtB,MAAM,OAAO,IAAI,WAAW,IAAI,IAAI,SAAS,YAAY,WAAW;AACpE,MAAK,IAAI,IAAI,WAAW,EAAE,CAAC,KAAK,EAAE,CAAC;AACnC,MAAK,IAAI,KAAK,EAAE;AAChB,MAAK,IAAI,IAAI,WAAW,aAAa,GAAG,YAAY,WAAW,EAAE,IAAI,SAAS,EAAE;CAEhF,MAAM,IAAI,KAAK,OAAO,QAAQ,UAAU,CAAC,OAAO,KAAK,CAAC,QAAQ;AAG9D,QAAO;EACN,KAHU,EAAE,MAAM,GAAG,GAAG;EAIxB,WAHU,EAAE,MAAM,GAAG;EAIrB;;AAGF,MAAM,eAAe,SAA0B;AAC9C,KAAI,CAAC,UAAU,KAAK,KAAK,CACxB,QAAO;AAER,QAAO,CAAC,KACN,MAAM,IAAI,CACV,MAAM,EAAE,CACR,IAAI,cAAc,CAClB,KAAK,MAAyB;;AAGjC,MAAa,cAAc,MAAY,MAAW,SAAS,oBAA0B;AACpF,KAAI,CAAC,YAAY,KAAK,CACrB,OAAM,IAAI,MAAM,0BAA0B;CAG3C,MAAM,EAAE,KAAK,cAAc,qBAAqB,KAAK;AAOrD,QANiB,KACf,MAAM,IAAI,CACV,MAAM,EAAE,CACR,IAAI,cAAc,CAClB,KAAK,OAAO,SAAS,IAAI,GAAG,CAAC,CAEf,QAAQ,YAAY,YAAY,QAAQ,YAAY,UAAU,OAAO,EAAE;EACtF;EACA;EACA,CAAC"}
@@ -1,83 +0,0 @@
1
- // Copyright (c) Mysten Labs, Inc.
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- // This is adapted from https://github.com/alepop/ed25519-hd-key replacing create-hmac
5
- // with @noble/hashes to be browser compatible.
6
-
7
- import { fromHex } from '@mysten/bcs';
8
- import { hmac } from '@noble/hashes/hmac.js';
9
- import { sha512 } from '@noble/hashes/sha2.js';
10
-
11
- type Hex = string;
12
- type Path = string;
13
-
14
- type Keys = {
15
- key: Uint8Array;
16
- chainCode: Uint8Array;
17
- };
18
-
19
- const ED25519_CURVE = 'ed25519 seed';
20
- const HARDENED_OFFSET = 0x80000000;
21
-
22
- const pathRegex = new RegExp("^m(\\/[0-9]+')+$");
23
-
24
- const replaceDerive = (val: string): string => val.replace("'", '');
25
-
26
- const getMasterKeyFromSeed = (seed: Hex): Keys => {
27
- const h = hmac.create(sha512, new TextEncoder().encode(ED25519_CURVE));
28
- const I = h.update(fromHex(seed)).digest();
29
- const IL = I.slice(0, 32);
30
- const IR = I.slice(32);
31
- return {
32
- key: IL,
33
- chainCode: IR,
34
- };
35
- };
36
-
37
- const CKDPriv = ({ key, chainCode }: Keys, index: number): Keys => {
38
- const indexBuffer = new ArrayBuffer(4);
39
- const cv = new DataView(indexBuffer);
40
- cv.setUint32(0, index);
41
-
42
- const data = new Uint8Array(1 + key.length + indexBuffer.byteLength);
43
- data.set(new Uint8Array(1).fill(0));
44
- data.set(key, 1);
45
- data.set(new Uint8Array(indexBuffer, 0, indexBuffer.byteLength), key.length + 1);
46
-
47
- const I = hmac.create(sha512, chainCode).update(data).digest();
48
- const IL = I.slice(0, 32);
49
- const IR = I.slice(32);
50
- return {
51
- key: IL,
52
- chainCode: IR,
53
- };
54
- };
55
-
56
- const isValidPath = (path: string): boolean => {
57
- if (!pathRegex.test(path)) {
58
- return false;
59
- }
60
- return !path
61
- .split('/')
62
- .slice(1)
63
- .map(replaceDerive)
64
- .some(isNaN as any /* ts T_T*/);
65
- };
66
-
67
- export const derivePath = (path: Path, seed: Hex, offset = HARDENED_OFFSET): Keys => {
68
- if (!isValidPath(path)) {
69
- throw new Error('Invalid derivation path');
70
- }
71
-
72
- const { key, chainCode } = getMasterKeyFromSeed(seed);
73
- const segments = path
74
- .split('/')
75
- .slice(1)
76
- .map(replaceDerive)
77
- .map((el) => parseInt(el, 10));
78
-
79
- return segments.reduce((parentKeys, segment) => CKDPriv(parentKeys, segment + offset), {
80
- key,
81
- chainCode,
82
- });
83
- };