@mysten/sui 2.37.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.
- package/CHANGELOG.md +12 -0
- package/dist/cryptography/mnemonics.d.mts +4 -1
- package/dist/cryptography/mnemonics.d.mts.map +1 -1
- package/dist/cryptography/mnemonics.mjs +5 -2
- package/dist/cryptography/mnemonics.mjs.map +1 -1
- package/dist/cryptography/signature.d.mts +6 -6
- package/dist/cryptography/slip10.d.mts +5 -0
- package/dist/cryptography/slip10.d.mts.map +1 -0
- package/dist/cryptography/slip10.mjs +55 -0
- package/dist/cryptography/slip10.mjs.map +1 -0
- package/dist/grpc/proto/sui/forking/v1alpha/forking_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/ledger_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/move_package_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/name_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.client.d.mts +4 -4
- package/dist/keypairs/ed25519/keypair.d.mts.map +1 -1
- package/dist/keypairs/ed25519/keypair.mjs +6 -9
- package/dist/keypairs/ed25519/keypair.mjs.map +1 -1
- package/dist/keypairs/mldsa65/index.d.mts +3 -2
- package/dist/keypairs/mldsa65/index.mjs +3 -2
- package/dist/keypairs/mldsa65/keypair.d.mts +19 -1
- package/dist/keypairs/mldsa65/keypair.d.mts.map +1 -1
- package/dist/keypairs/mldsa65/keypair.mjs +33 -1
- package/dist/keypairs/mldsa65/keypair.mjs.map +1 -1
- package/dist/transactions/Transaction.d.mts +6 -6
- package/dist/transactions/data/internal.d.mts +133 -133
- package/dist/transactions/data/internal.d.mts.map +1 -1
- package/dist/transactions/data/v1.d.mts +239 -239
- package/dist/transactions/data/v1.d.mts.map +1 -1
- package/dist/transactions/data/v2.d.mts +16 -16
- package/dist/transactions/data/v2.d.mts.map +1 -1
- package/dist/version.mjs +1 -1
- package/dist/version.mjs.map +1 -1
- package/dist/zklogin/bcs.d.mts +14 -14
- package/docs/cryptography/keypairs.md +4 -2
- package/package.json +5 -5
- package/src/cryptography/mnemonics.ts +5 -2
- package/src/cryptography/slip10.ts +84 -0
- package/src/keypairs/ed25519/keypair.ts +11 -14
- package/src/keypairs/mldsa65/index.ts +6 -1
- package/src/keypairs/mldsa65/keypair.ts +46 -0
- package/src/version.ts +1 -1
- package/dist/keypairs/ed25519/ed25519-hd-key.mjs +0 -45
- package/dist/keypairs/ed25519/ed25519-hd-key.mjs.map +0 -1
- package/src/keypairs/ed25519/ed25519-hd-key.ts +0 -83
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { EnumInputShape } from "@mysten/bcs";
|
|
2
|
-
import * as
|
|
2
|
+
import * as valibot843 from "valibot";
|
|
3
3
|
import { GenericSchema, InferOutput } from "valibot";
|
|
4
4
|
|
|
5
5
|
//#region src/transactions/data/v2.d.ts
|
|
6
|
-
declare const SerializedTransactionDataV2Schema:
|
|
7
|
-
readonly version:
|
|
8
|
-
readonly sender:
|
|
9
|
-
readonly expiration:
|
|
6
|
+
declare const SerializedTransactionDataV2Schema: valibot843.ObjectSchema<{
|
|
7
|
+
readonly version: valibot843.LiteralSchema<2, undefined>;
|
|
8
|
+
readonly sender: valibot843.NullishSchema<valibot843.SchemaWithPipe<readonly [valibot843.StringSchema<undefined>, valibot843.TransformAction<string, string>, valibot843.CheckAction<string, undefined>]>, undefined>;
|
|
9
|
+
readonly expiration: valibot843.NullishSchema<GenericSchema<EnumInputShape<{
|
|
10
10
|
None: true;
|
|
11
11
|
Epoch: string | number;
|
|
12
12
|
ValidDuring: {
|
|
@@ -30,17 +30,17 @@ declare const SerializedTransactionDataV2Schema: valibot0.ObjectSchema<{
|
|
|
30
30
|
nonce: number;
|
|
31
31
|
};
|
|
32
32
|
}>>, undefined>;
|
|
33
|
-
readonly gasData:
|
|
34
|
-
readonly budget:
|
|
35
|
-
readonly price:
|
|
36
|
-
readonly owner:
|
|
37
|
-
readonly payment:
|
|
38
|
-
readonly objectId:
|
|
39
|
-
readonly version:
|
|
40
|
-
readonly digest:
|
|
33
|
+
readonly gasData: valibot843.ObjectSchema<{
|
|
34
|
+
readonly budget: valibot843.NullableSchema<valibot843.SchemaWithPipe<readonly [valibot843.UnionSchema<[valibot843.StringSchema<undefined>, valibot843.SchemaWithPipe<readonly [valibot843.NumberSchema<undefined>, valibot843.IntegerAction<number, undefined>]>], undefined>, valibot843.CheckAction<string | number, "Invalid u64">]>, undefined>;
|
|
35
|
+
readonly price: valibot843.NullableSchema<valibot843.SchemaWithPipe<readonly [valibot843.UnionSchema<[valibot843.StringSchema<undefined>, valibot843.SchemaWithPipe<readonly [valibot843.NumberSchema<undefined>, valibot843.IntegerAction<number, undefined>]>], undefined>, valibot843.CheckAction<string | number, "Invalid u64">]>, undefined>;
|
|
36
|
+
readonly owner: valibot843.NullableSchema<valibot843.SchemaWithPipe<readonly [valibot843.StringSchema<undefined>, valibot843.TransformAction<string, string>, valibot843.CheckAction<string, undefined>]>, undefined>;
|
|
37
|
+
readonly payment: valibot843.NullableSchema<valibot843.ArraySchema<valibot843.ObjectSchema<{
|
|
38
|
+
readonly objectId: valibot843.SchemaWithPipe<readonly [valibot843.StringSchema<undefined>, valibot843.TransformAction<string, string>, valibot843.CheckAction<string, undefined>]>;
|
|
39
|
+
readonly version: valibot843.SchemaWithPipe<readonly [valibot843.UnionSchema<[valibot843.StringSchema<undefined>, valibot843.SchemaWithPipe<readonly [valibot843.NumberSchema<undefined>, valibot843.IntegerAction<number, undefined>]>], undefined>, valibot843.CheckAction<string | number, "Invalid u64">]>;
|
|
40
|
+
readonly digest: valibot843.StringSchema<undefined>;
|
|
41
41
|
}, undefined>, undefined>, undefined>;
|
|
42
42
|
}, undefined>;
|
|
43
|
-
readonly inputs:
|
|
43
|
+
readonly inputs: valibot843.ArraySchema<GenericSchema<EnumInputShape<{
|
|
44
44
|
Object: EnumInputShape<{
|
|
45
45
|
ImmOrOwnedObject: {
|
|
46
46
|
objectId: string;
|
|
@@ -90,7 +90,7 @@ declare const SerializedTransactionDataV2Schema: valibot0.ObjectSchema<{
|
|
|
90
90
|
};
|
|
91
91
|
};
|
|
92
92
|
}>>, undefined>;
|
|
93
|
-
readonly commands:
|
|
93
|
+
readonly commands: valibot843.ArraySchema<GenericSchema<EnumInputShape<{
|
|
94
94
|
MoveCall: {
|
|
95
95
|
package: string;
|
|
96
96
|
module: string;
|
|
@@ -189,7 +189,7 @@ declare const SerializedTransactionDataV2Schema: valibot0.ObjectSchema<{
|
|
|
189
189
|
};
|
|
190
190
|
};
|
|
191
191
|
}>>, undefined>;
|
|
192
|
-
readonly digest:
|
|
192
|
+
readonly digest: valibot843.OptionalSchema<valibot843.NullableSchema<valibot843.StringSchema<undefined>, undefined>, undefined>;
|
|
193
193
|
}, undefined>;
|
|
194
194
|
type SerializedTransactionDataV2 = InferOutput<typeof SerializedTransactionDataV2Schema>;
|
|
195
195
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"v2.d.mts","names":[],"sources":["../../../src/transactions/data/v2.ts"],"mappings":";;;;;cAqJa,iCAAA,
|
|
1
|
+
{"version":3,"file":"v2.d.mts","names":[],"sources":["../../../src/transactions/data/v2.ts"],"mappings":";;;;;cAqJa,iCAAA,aAAiC,YAAA;EAAA,kBAQ5C,UAAA,CAAA,aAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAEU,2BAAA,GAA8B,WAAA,QAAmB,iCAAA"}
|
package/dist/version.mjs
CHANGED
package/dist/version.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '2.
|
|
1
|
+
{"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '2.38.0';\n"],"mappings":";AAKA,MAAa,kBAAkB"}
|
package/dist/zklogin/bcs.d.mts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _mysten_bcs1251 from "@mysten/bcs";
|
|
2
2
|
import { InferBcsInput } from "@mysten/bcs";
|
|
3
3
|
|
|
4
4
|
//#region src/zklogin/bcs.d.ts
|
|
5
|
-
declare const zkLoginSignature:
|
|
6
|
-
inputs:
|
|
7
|
-
proofPoints:
|
|
8
|
-
a:
|
|
5
|
+
declare const zkLoginSignature: _mysten_bcs1251.BcsStruct<{
|
|
6
|
+
inputs: _mysten_bcs1251.BcsStruct<{
|
|
7
|
+
proofPoints: _mysten_bcs1251.BcsStruct<{
|
|
8
|
+
a: _mysten_bcs1251.BcsType<string[], Iterable<string> & {
|
|
9
9
|
length: number;
|
|
10
10
|
}, string>;
|
|
11
|
-
b:
|
|
11
|
+
b: _mysten_bcs1251.BcsType<string[][], Iterable<Iterable<string> & {
|
|
12
12
|
length: number;
|
|
13
13
|
}> & {
|
|
14
14
|
length: number;
|
|
15
15
|
}, string>;
|
|
16
|
-
c:
|
|
16
|
+
c: _mysten_bcs1251.BcsType<string[], Iterable<string> & {
|
|
17
17
|
length: number;
|
|
18
18
|
}, string>;
|
|
19
19
|
}, string>;
|
|
20
|
-
issBase64Details:
|
|
21
|
-
value:
|
|
22
|
-
indexMod4:
|
|
20
|
+
issBase64Details: _mysten_bcs1251.BcsStruct<{
|
|
21
|
+
value: _mysten_bcs1251.BcsType<string, string, "string">;
|
|
22
|
+
indexMod4: _mysten_bcs1251.BcsType<number, number, "u8">;
|
|
23
23
|
}, string>;
|
|
24
|
-
headerBase64:
|
|
25
|
-
addressSeed:
|
|
24
|
+
headerBase64: _mysten_bcs1251.BcsType<string, string, "string">;
|
|
25
|
+
addressSeed: _mysten_bcs1251.BcsType<string, string, "string">;
|
|
26
26
|
}, string>;
|
|
27
|
-
maxEpoch:
|
|
28
|
-
userSignature:
|
|
27
|
+
maxEpoch: _mysten_bcs1251.BcsType<string, string | number | bigint, "u64">;
|
|
28
|
+
userSignature: _mysten_bcs1251.BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, "vector<u8>">;
|
|
29
29
|
}, string>;
|
|
30
30
|
type ZkLoginSignature = InferBcsInput<typeof zkLoginSignature>;
|
|
31
31
|
type ZkLoginSignatureInputs = ZkLoginSignature['inputs'];
|
|
@@ -27,13 +27,15 @@ example, to use the Ed25519 scheme, import the `Ed25519Keypair` class from
|
|
|
27
27
|
|
|
28
28
|
ML-DSA-65 is the post-quantum scheme from FIPS 204. Its secret key is a 32-byte seed, so it imports
|
|
29
29
|
and exports like the other schemes, but its public keys (1952 bytes) and signatures (3309 bytes) are
|
|
30
|
-
much larger, and every signature is randomized.
|
|
31
|
-
|
|
30
|
+
much larger, and every signature is randomized. Mnemonic derivation uses purpose `94'`, so the
|
|
31
|
+
default path is `m/94'/784'/0'/0'/0'`, hardened at every level like Ed25519. The scheme is only
|
|
32
|
+
accepted by networks where it has been enabled.
|
|
32
33
|
|
|
33
34
|
```typescript
|
|
34
35
|
|
|
35
36
|
const keypair = MLDSA65Keypair.generate();
|
|
36
37
|
const restored = MLDSA65Keypair.fromSecretKey(keypair.getSecretKey());
|
|
38
|
+
const derived = MLDSA65Keypair.deriveKeypair(mnemonic);
|
|
37
39
|
```
|
|
38
40
|
|
|
39
41
|
## Creating a key pair
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"author": "Mysten Labs <build@mystenlabs.com>",
|
|
4
4
|
"description": "Sui TypeScript API",
|
|
5
5
|
"homepage": "https://sdk.mystenlabs.com",
|
|
6
|
-
"version": "2.
|
|
6
|
+
"version": "2.38.0",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"files": [
|
|
@@ -138,7 +138,7 @@
|
|
|
138
138
|
"devDependencies": {
|
|
139
139
|
"@0no-co/graphqlsp": "^1.17.3",
|
|
140
140
|
"@graphql-codegen/add": "^7.1.0",
|
|
141
|
-
"@graphql-codegen/cli": "^7.
|
|
141
|
+
"@graphql-codegen/cli": "^7.4.6",
|
|
142
142
|
"@graphql-codegen/typed-document-node": "^7.1.0",
|
|
143
143
|
"@graphql-codegen/typescript": "6.1.0",
|
|
144
144
|
"@graphql-codegen/typescript-document-nodes": "6.1.0",
|
|
@@ -149,14 +149,14 @@
|
|
|
149
149
|
"@types/node": "^26.2.0",
|
|
150
150
|
"@types/tmp": "^0.2.6",
|
|
151
151
|
"cross-env": "^10.1.0",
|
|
152
|
-
"graphql-config": "^5.1.
|
|
152
|
+
"graphql-config": "^5.1.8",
|
|
153
153
|
"msw": "^2.15.0",
|
|
154
154
|
"tmp": "^0.2.7",
|
|
155
155
|
"ts-retry-promise": "^0.8.1",
|
|
156
156
|
"typescript": "^7.0.2",
|
|
157
|
-
"vite": "^8.2.
|
|
157
|
+
"vite": "^8.2.4",
|
|
158
158
|
"vite-tsconfig-paths": "^6.0.4",
|
|
159
|
-
"vitest": "^4.1.
|
|
159
|
+
"vitest": "^4.1.12",
|
|
160
160
|
"wait-on": "^9.1.0"
|
|
161
161
|
},
|
|
162
162
|
"dependencies": {
|
|
@@ -6,10 +6,13 @@ import { mnemonicToSeedSync as bip39MnemonicToSeedSync } from '@scure/bip39';
|
|
|
6
6
|
/**
|
|
7
7
|
* Parse and validate a path that is compliant to SLIP-0010 in form m/44'/784'/{account_index}'/{change_index}'/{address_index}'.
|
|
8
8
|
*
|
|
9
|
+
* The purpose node distinguishes signature schemes: 44 for Ed25519 (the default) and 94 for ML-DSA-65.
|
|
10
|
+
*
|
|
9
11
|
* @param path path string (e.g. `m/44'/784'/0'/0'/0'`).
|
|
12
|
+
* @param purpose purpose node of the scheme, defaults to 44.
|
|
10
13
|
*/
|
|
11
|
-
export function isValidHardenedPath(path: string): boolean {
|
|
12
|
-
if (!new RegExp(
|
|
14
|
+
export function isValidHardenedPath(path: string, purpose = 44): boolean {
|
|
15
|
+
if (!new RegExp(`^m\\/${purpose}'\\/784'\\/[0-9]+'\\/[0-9]+'\\/[0-9]+'+$`).test(path)) {
|
|
13
16
|
return false;
|
|
14
17
|
}
|
|
15
18
|
return true;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// SLIP-0010 hardened derivation for Ed25519 and ML-DSA-65. The master key string picks
|
|
5
|
+
// the tree; the HMAC-SHA512 walk below it is the same. Mirrors `fastcrypto::slip10`.
|
|
6
|
+
|
|
7
|
+
import { hmac } from '@noble/hashes/hmac.js';
|
|
8
|
+
import { sha512 } from '@noble/hashes/sha2.js';
|
|
9
|
+
|
|
10
|
+
export type Slip10Node = {
|
|
11
|
+
key: Uint8Array;
|
|
12
|
+
chainCode: Uint8Array;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const ED25519_SLIP10_MASTER_KEY = 'ed25519 seed';
|
|
16
|
+
// Proposed in https://github.com/satoshilabs/slips/pull/1968.
|
|
17
|
+
export const MLDSA65_SLIP10_MASTER_KEY = 'ML-DSA-65 seed';
|
|
18
|
+
|
|
19
|
+
const HARDENED_OFFSET = 0x80000000;
|
|
20
|
+
|
|
21
|
+
// SLIP-0010 seeds are 128 to 512 bits.
|
|
22
|
+
const MIN_SEED_LENGTH = 16;
|
|
23
|
+
const MAX_SEED_LENGTH = 64;
|
|
24
|
+
|
|
25
|
+
// Canonical decimal indices only; the range is checked in `derivePath`.
|
|
26
|
+
const pathRegex = /^m(\/(0|[1-9][0-9]*)')+$/;
|
|
27
|
+
|
|
28
|
+
const getMasterKeyFromSeed = (masterKey: string, seed: Uint8Array): Slip10Node => {
|
|
29
|
+
const I = hmac(sha512, new TextEncoder().encode(masterKey), seed);
|
|
30
|
+
return split(I);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const CKDPriv = ({ key, chainCode }: Slip10Node, index: number): Slip10Node => {
|
|
34
|
+
const data = new Uint8Array(1 + key.length + 4);
|
|
35
|
+
data.set(key, 1);
|
|
36
|
+
new DataView(data.buffer).setUint32(1 + key.length, index);
|
|
37
|
+
|
|
38
|
+
const I = hmac(sha512, chainCode, data);
|
|
39
|
+
data.fill(0);
|
|
40
|
+
return split(I);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Copies the halves out and wipes the HMAC output, so no node secret outlives its use.
|
|
44
|
+
const split = (I: Uint8Array): Slip10Node => {
|
|
45
|
+
const node = { key: I.slice(0, 32), chainCode: I.slice(32) };
|
|
46
|
+
I.fill(0);
|
|
47
|
+
return node;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const wipe = ({ key, chainCode }: Slip10Node) => {
|
|
51
|
+
key.fill(0);
|
|
52
|
+
chainCode.fill(0);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Derive the SLIP-0010 node at `path` from `seed` under `masterKey`. Every level must be
|
|
57
|
+
* hardened; neither scheme has public derivation. The node secret at the end of the path
|
|
58
|
+
* is the Ed25519 private key or the FIPS 204 keygen seed.
|
|
59
|
+
*/
|
|
60
|
+
export const derivePath = (masterKey: string, path: string, seed: Uint8Array): Slip10Node => {
|
|
61
|
+
if (!pathRegex.test(path)) {
|
|
62
|
+
throw new Error('Invalid derivation path');
|
|
63
|
+
}
|
|
64
|
+
if (seed.length < MIN_SEED_LENGTH || seed.length > MAX_SEED_LENGTH) {
|
|
65
|
+
throw new Error(`Invalid seed length: expected ${MIN_SEED_LENGTH} to ${MAX_SEED_LENGTH} bytes`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const segments = path
|
|
69
|
+
.split('/')
|
|
70
|
+
.slice(1)
|
|
71
|
+
.map((el) => parseInt(el.slice(0, -1), 10));
|
|
72
|
+
// An index of 2^31 or more would collide with a hardened index after the OR below.
|
|
73
|
+
if (segments.some((segment) => segment >= HARDENED_OFFSET)) {
|
|
74
|
+
throw new Error('Invalid derivation path');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let node = getMasterKeyFromSeed(masterKey, seed);
|
|
78
|
+
for (const segment of segments) {
|
|
79
|
+
const child = CKDPriv(node, (segment | HARDENED_OFFSET) >>> 0);
|
|
80
|
+
wipe(node);
|
|
81
|
+
node = child;
|
|
82
|
+
}
|
|
83
|
+
return node;
|
|
84
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright (c) Mysten Labs, Inc.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import {
|
|
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,
|
|
13
|
+
import { isValidHardenedPath, mnemonicToSeed } from '../../cryptography/mnemonics.js';
|
|
14
14
|
import type { SignatureScheme } from '../../cryptography/signature-scheme.js';
|
|
15
|
-
import { derivePath } from '
|
|
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
|
-
|
|
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
|
|
181
|
-
|
|
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 {
|
|
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 {
|
package/src/version.ts
CHANGED
|
@@ -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
|
-
};
|