@mysten/sui 2.35.0 → 2.37.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 +40 -0
- package/dist/bcs/bcs.d.mts +6 -6
- package/dist/bcs/bcs.d.mts.map +1 -1
- package/dist/bcs/bcs.mjs +52 -16
- package/dist/bcs/bcs.mjs.map +1 -1
- package/dist/bcs/index.d.mts +12 -3
- package/dist/bcs/index.d.mts.map +1 -1
- package/dist/cryptography/publickey.mjs +1 -0
- package/dist/cryptography/publickey.mjs.map +1 -1
- package/dist/cryptography/signature-scheme.d.mts +4 -1
- package/dist/cryptography/signature-scheme.d.mts.map +1 -1
- package/dist/cryptography/signature-scheme.mjs +6 -3
- package/dist/cryptography/signature-scheme.mjs.map +1 -1
- package/dist/cryptography/signature.d.mts +1 -1
- package/dist/cryptography/signature.mjs +2 -1
- package/dist/cryptography/signature.mjs.map +1 -1
- 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/transaction_execution_service.client.d.mts +4 -4
- package/dist/keypairs/mldsa65/index.d.mts +3 -0
- package/dist/keypairs/mldsa65/index.mjs +4 -0
- package/dist/keypairs/mldsa65/keypair.d.mts +67 -0
- package/dist/keypairs/mldsa65/keypair.d.mts.map +1 -0
- package/dist/keypairs/mldsa65/keypair.mjs +91 -0
- package/dist/keypairs/mldsa65/keypair.mjs.map +1 -0
- package/dist/keypairs/mldsa65/publickey.d.mts +37 -0
- package/dist/keypairs/mldsa65/publickey.d.mts.map +1 -0
- package/dist/keypairs/mldsa65/publickey.mjs +64 -0
- package/dist/keypairs/mldsa65/publickey.mjs.map +1 -0
- package/dist/multisig/publickey.d.mts +18 -3
- package/dist/multisig/publickey.d.mts.map +1 -1
- package/dist/multisig/publickey.mjs +29 -11
- package/dist/multisig/publickey.mjs.map +1 -1
- package/dist/transactions/Transaction.d.mts +6 -6
- package/dist/verify/verify.d.mts.map +1 -1
- package/dist/verify/verify.mjs +6 -2
- package/dist/verify/verify.mjs.map +1 -1
- package/dist/version.mjs +1 -1
- package/dist/version.mjs.map +1 -1
- package/dist/zklogin/address-seed.d.mts +6 -0
- package/dist/zklogin/address-seed.d.mts.map +1 -0
- package/dist/zklogin/address-seed.mjs +51 -0
- package/dist/zklogin/address-seed.mjs.map +1 -0
- package/dist/zklogin/address.d.mts.map +1 -1
- package/dist/zklogin/address.mjs +2 -1
- package/dist/zklogin/address.mjs.map +1 -1
- package/dist/zklogin/bcs.d.mts +14 -14
- package/dist/zklogin/index.d.mts +2 -1
- package/dist/zklogin/index.mjs +3 -2
- package/dist/zklogin/nonce.mjs +1 -1
- package/dist/zklogin/publickey.d.mts.map +1 -1
- package/dist/zklogin/publickey.mjs +2 -2
- package/dist/zklogin/publickey.mjs.map +1 -1
- package/dist/zklogin/utils.d.mts +1 -3
- package/dist/zklogin/utils.d.mts.map +1 -1
- package/dist/zklogin/utils.mjs +1 -46
- package/dist/zklogin/utils.mjs.map +1 -1
- package/docs/cryptography/index.md +7 -6
- package/docs/cryptography/keypairs.md +13 -1
- package/docs/llms-index.md +1 -1
- package/package.json +9 -3
- package/src/bcs/bcs.ts +73 -14
- package/src/cryptography/publickey.ts +1 -0
- package/src/cryptography/signature-scheme.ts +4 -1
- package/src/cryptography/signature.ts +1 -0
- package/src/keypairs/mldsa65/index.ts +5 -0
- package/src/keypairs/mldsa65/keypair.ts +128 -0
- package/src/keypairs/mldsa65/publickey.ts +95 -0
- package/src/multisig/publickey.ts +68 -23
- package/src/verify/verify.ts +8 -2
- package/src/version.ts +1 -1
- package/src/zklogin/address-seed.ts +86 -0
- package/src/zklogin/address.ts +2 -6
- package/src/zklogin/index.ts +1 -2
- package/src/zklogin/publickey.ts +4 -4
- package/src/zklogin/utils.ts +0 -83
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/zklogin/utils.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { hexToBytes } from '@noble/hashes/utils.js';\n\nimport type { PublicKey } from '../cryptography/publickey.js';\
|
|
1
|
+
{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/zklogin/utils.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { hexToBytes } from '@noble/hashes/utils.js';\n\nimport type { PublicKey } from '../cryptography/publickey.js';\n\nfunction findFirstNonZeroIndex(bytes: Uint8Array) {\n\tfor (let i = 0; i < bytes.length; i++) {\n\t\tif (bytes[i] !== 0) {\n\t\t\treturn i;\n\t\t}\n\t}\n\n\treturn -1;\n}\n\n// Derive bytearray from num where the bytearray is padded to the left with 0s to the specified width.\nexport function toPaddedBigEndianBytes(num: bigint, width: number): Uint8Array {\n\tconst hex = num.toString(16);\n\treturn hexToBytes(hex.padStart(width * 2, '0').slice(-width * 2));\n}\n\n// Derive bytearray from num where the bytearray is not padded with 0.\nexport function toBigEndianBytes(num: bigint, width: number): Uint8Array {\n\tconst bytes = toPaddedBigEndianBytes(num, width);\n\n\tconst firstNonZeroIndex = findFirstNonZeroIndex(bytes);\n\n\tif (firstNonZeroIndex === -1) {\n\t\treturn new Uint8Array([0]);\n\t}\n\n\treturn bytes.slice(firstNonZeroIndex);\n}\n\nexport function getExtendedEphemeralPublicKey(publicKey: PublicKey) {\n\treturn publicKey.toSuiPublicKey();\n}\n\nexport function normalizeZkLoginIssuer(iss: string) {\n\tif (iss === 'accounts.google.com') {\n\t\treturn 'https://accounts.google.com';\n\t}\n\treturn iss;\n}\n"],"mappings":";;;AAOA,SAAS,sBAAsB,OAAmB;AACjD,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,IACjC,KAAI,MAAM,OAAO,EAChB,QAAO;AAIT,QAAO;;AAIR,SAAgB,uBAAuB,KAAa,OAA2B;AAE9E,QAAO,WADK,IAAI,SAAS,GAAG,CACN,SAAS,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;;AAIlE,SAAgB,iBAAiB,KAAa,OAA2B;CACxE,MAAM,QAAQ,uBAAuB,KAAK,MAAM;CAEhD,MAAM,oBAAoB,sBAAsB,MAAM;AAEtD,KAAI,sBAAsB,GACzB,QAAO,IAAI,WAAW,CAAC,EAAE,CAAC;AAG3B,QAAO,MAAM,MAAM,kBAAkB;;AAGtC,SAAgB,8BAA8B,WAAsB;AACnE,QAAO,UAAU,gBAAgB;;AAGlC,SAAgB,uBAAuB,KAAa;AACnD,KAAI,QAAQ,sBACX,QAAO;AAER,QAAO"}
|
|
@@ -11,13 +11,14 @@ implementation in detail.
|
|
|
11
11
|
## Signing schemes
|
|
12
12
|
|
|
13
13
|
A signer always produces a signature under a signature scheme, which determines the curve and
|
|
14
|
-
signature format. Sui supports
|
|
14
|
+
signature format. Sui supports four schemes, each with a corresponding `Keypair` class:
|
|
15
15
|
|
|
16
16
|
| Scheme | Class | Import |
|
|
17
17
|
| --------------- | ------------------ | -------------------------------- |
|
|
18
18
|
| Ed25519 | `Ed25519Keypair` | `@mysten/sui/keypairs/ed25519` |
|
|
19
19
|
| ECDSA Secp256k1 | `Secp256k1Keypair` | `@mysten/sui/keypairs/secp256k1` |
|
|
20
20
|
| ECDSA Secp256r1 | `Secp256r1Keypair` | `@mysten/sui/keypairs/secp256r1` |
|
|
21
|
+
| ML-DSA-65 | `MLDSA65Keypair` | `@mysten/sui/keypairs/mldsa65` |
|
|
21
22
|
|
|
22
23
|
For background on the schemes themselves, see the
|
|
23
24
|
[Signatures](https://docs.sui.io/concepts/cryptography/transaction-auth/signatures) concept topic. A
|
|
@@ -31,11 +32,11 @@ so the rest of your code doesn't need to know where the key lives.
|
|
|
31
32
|
|
|
32
33
|
Implementations provide three primitives:
|
|
33
34
|
|
|
34
|
-
| Method | Description
|
|
35
|
-
| ---------------- |
|
|
36
|
-
| `getPublicKey()` | Returns the `PublicKey` for deriving the address and verifying
|
|
37
|
-
| `getKeyScheme()` | Returns the signature scheme (`ED25519`, `Secp256k1`, `Secp256r1`) |
|
|
38
|
-
| `sign(bytes)` | Signs raw bytes and returns the signature
|
|
35
|
+
| Method | Description |
|
|
36
|
+
| ---------------- | ----------------------------------------------------------------------------- |
|
|
37
|
+
| `getPublicKey()` | Returns the `PublicKey` for deriving the address and verifying |
|
|
38
|
+
| `getKeyScheme()` | Returns the signature scheme (`ED25519`, `Secp256k1`, `Secp256r1`, `MLDSA65`) |
|
|
39
|
+
| `sign(bytes)` | Signs raw bytes and returns the signature |
|
|
39
40
|
|
|
40
41
|
On top of those, `Signer` provides the methods you typically call:
|
|
41
42
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Key pairs
|
|
2
2
|
|
|
3
|
-
> >- Create Ed25519, Secp256k1, and
|
|
3
|
+
> >- Create Ed25519, Secp256k1, Secp256r1, and ML-DSA-65 keypairs and derive them from mnemonics and secret keys
|
|
4
4
|
|
|
5
5
|
A `Keypair` holds a Sui private key in process and signs with it. It is the easiest
|
|
6
6
|
[`Signer`](/sui/cryptography) to use. It is ideal for scripts, backends, and tests where you manage
|
|
@@ -15,6 +15,7 @@ Each signing scheme has a corresponding `Keypair` class:
|
|
|
15
15
|
| Ed25519 | `Ed25519Keypair` | `@mysten/sui/keypairs/ed25519` |
|
|
16
16
|
| ECDSA Secp256k1 | `Secp256k1Keypair` | `@mysten/sui/keypairs/secp256k1` |
|
|
17
17
|
| ECDSA Secp256r1 | `Secp256r1Keypair` | `@mysten/sui/keypairs/secp256r1` |
|
|
18
|
+
| ML-DSA-65 | `MLDSA65Keypair` | `@mysten/sui/keypairs/mldsa65` |
|
|
18
19
|
|
|
19
20
|
To use, import the key pair class your project uses from the `@mysten/sui/keypairs` folder. For
|
|
20
21
|
example, to use the Ed25519 scheme, import the `Ed25519Keypair` class from
|
|
@@ -24,6 +25,17 @@ example, to use the Ed25519 scheme, import the `Ed25519Keypair` class from
|
|
|
24
25
|
|
|
25
26
|
```
|
|
26
27
|
|
|
28
|
+
ML-DSA-65 is the post-quantum scheme from FIPS 204. Its secret key is a 32-byte seed, so it imports
|
|
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. Deriving an ML-DSA-65 keypair from a mnemonic is not
|
|
31
|
+
supported yet, and the scheme is only accepted by networks where it has been enabled.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
|
|
35
|
+
const keypair = MLDSA65Keypair.generate();
|
|
36
|
+
const restored = MLDSA65Keypair.fromSecretKey(keypair.getSecretKey());
|
|
37
|
+
```
|
|
38
|
+
|
|
27
39
|
## Creating a key pair
|
|
28
40
|
|
|
29
41
|
To create a random key pair (which identifies a Sui address), instantiate a new `Keypair` class. To
|
package/docs/llms-index.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
- [Commands and Inputs Reference](./transactions/reference.md): Complete reference for transaction commands and input types
|
|
17
17
|
- [Building Offline](./transactions/offline.md): Build transactions without a network connection
|
|
18
18
|
- [Cryptography](./cryptography.md): >- Sign and verify Sui transactions and messages with keypairs and external signers
|
|
19
|
-
- [Key pairs](./cryptography/keypairs.md): >- Create Ed25519, Secp256k1, and
|
|
19
|
+
- [Key pairs](./cryptography/keypairs.md): >- Create Ed25519, Secp256k1, Secp256r1, and ML-DSA-65 keypairs and derive them from mnemonics and secret keys
|
|
20
20
|
- [Signers](./cryptography/signers.md): >- Signer implementations beyond keypairs — cloud KMS, Ledger, Web Crypto, passkeys, and multisig
|
|
21
21
|
- [AWS KMS Signer](./cryptography/signers/aws-kms.md): Sign Sui transactions with a key stored in AWS Key Management Service
|
|
22
22
|
- [GCP KMS Signer](./cryptography/signers/gcp-kms.md): Sign Sui transactions with a key stored in Google Cloud Key Management Service
|
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.37.0",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"files": [
|
|
@@ -89,6 +89,11 @@
|
|
|
89
89
|
"import": "./dist/keypairs/passkey/index.mjs",
|
|
90
90
|
"default": "./dist/keypairs/passkey/index.mjs"
|
|
91
91
|
},
|
|
92
|
+
"./keypairs/mldsa65": {
|
|
93
|
+
"types": "./dist/keypairs/mldsa65/index.d.mts",
|
|
94
|
+
"import": "./dist/keypairs/mldsa65/index.mjs",
|
|
95
|
+
"default": "./dist/keypairs/mldsa65/index.mjs"
|
|
96
|
+
},
|
|
92
97
|
"./multisig": {
|
|
93
98
|
"types": "./dist/multisig/index.d.mts",
|
|
94
99
|
"import": "./dist/multisig/index.mjs",
|
|
@@ -158,6 +163,7 @@
|
|
|
158
163
|
"@graphql-typed-document-node/core": "^3.2.0",
|
|
159
164
|
"@noble/curves": "^2.3.0",
|
|
160
165
|
"@noble/hashes": "^2.3.0",
|
|
166
|
+
"@noble/post-quantum": "^0.7.1",
|
|
161
167
|
"@protobuf-ts/grpcweb-transport": "^2.11.1",
|
|
162
168
|
"@protobuf-ts/runtime": "^2.11.1",
|
|
163
169
|
"@protobuf-ts/runtime-rpc": "^2.11.1",
|
|
@@ -168,8 +174,8 @@
|
|
|
168
174
|
"graphql": "^17.0.2",
|
|
169
175
|
"poseidon-lite": "0.3.0",
|
|
170
176
|
"valibot": "^1.4.2",
|
|
171
|
-
"@mysten/
|
|
172
|
-
"@mysten/
|
|
177
|
+
"@mysten/bcs": "^2.1.3",
|
|
178
|
+
"@mysten/utils": "^0.4.2"
|
|
173
179
|
},
|
|
174
180
|
"scripts": {
|
|
175
181
|
"clean": "rm -rf tsconfig.tsbuildinfo ./dist",
|
package/src/bcs/bcs.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import type { BcsType, BcsTypeOptions } from '@mysten/bcs';
|
|
5
|
-
import { bcs, fromBase58, fromBase64,
|
|
5
|
+
import { bcs, fromBase58, fromBase64, toBase58, toBase64, toHex } from '@mysten/bcs';
|
|
6
6
|
|
|
7
7
|
import { isValidSuiAddress, normalizeSuiAddress, SUI_ADDRESS_LENGTH } from '../utils/sui-types.js';
|
|
8
8
|
import { TypeTagSerializer } from './type-tag-serializer.js';
|
|
@@ -27,27 +27,82 @@ function optionEnum<T extends BcsType<any, any>>(type: T) {
|
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
const HEX_NIBBLES = new Int8Array(128).fill(-1);
|
|
31
|
+
for (let i = 0; i < 16; i++) {
|
|
32
|
+
HEX_NIBBLES['0123456789abcdef'.charCodeAt(i)] = i;
|
|
33
|
+
HEX_NIBBLES['0123456789ABCDEF'.charCodeAt(i)] = i;
|
|
34
|
+
}
|
|
35
|
+
function hexNibble(code: number): number {
|
|
36
|
+
return code < 128 ? HEX_NIBBLES[code] : -1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Same bytes as fromHex(normalizeSuiAddress(value)) for valid addresses, or null for invalid ones
|
|
40
|
+
function decodeAddress(value: string): Uint8Array<ArrayBuffer> | null {
|
|
41
|
+
if (!value) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const start =
|
|
45
|
+
value.length >= 2 && value[0] === '0' && (value[1] === 'x' || value[1] === 'X') ? 2 : 0;
|
|
46
|
+
const digits = value.length - start;
|
|
47
|
+
if (digits > SUI_ADDRESS_LENGTH * 2) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const bytes = new Uint8Array(SUI_ADDRESS_LENGTH);
|
|
52
|
+
// Right-align the digits, as normalizeSuiAddress left-pads with zeros
|
|
53
|
+
let byteIndex = SUI_ADDRESS_LENGTH - 1;
|
|
54
|
+
let i = value.length - 1;
|
|
55
|
+
for (; i >= start + 1; i -= 2) {
|
|
56
|
+
const low = hexNibble(value.charCodeAt(i));
|
|
57
|
+
const high = hexNibble(value.charCodeAt(i - 1));
|
|
58
|
+
if (low < 0 || high < 0) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
bytes[byteIndex--] = (high << 4) | low;
|
|
62
|
+
}
|
|
63
|
+
if (i === start) {
|
|
64
|
+
const low = hexNibble(value.charCodeAt(i));
|
|
65
|
+
if (low < 0) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
bytes[byteIndex] = low;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return bytes;
|
|
72
|
+
}
|
|
73
|
+
|
|
30
74
|
export const Address = bcs.bytes(SUI_ADDRESS_LENGTH).transform({
|
|
31
|
-
|
|
32
|
-
|
|
75
|
+
// Validation lives in input so string addresses are only decoded once per write; transform's
|
|
76
|
+
// validate runs input, so validate() still rejects invalid addresses.
|
|
77
|
+
input: (val: string | Uint8Array) => {
|
|
78
|
+
if (typeof val === 'string') {
|
|
79
|
+
const bytes = decodeAddress(val);
|
|
80
|
+
if (!bytes) {
|
|
81
|
+
throw new Error(`Invalid Sui address ${val}`);
|
|
82
|
+
}
|
|
83
|
+
return bytes;
|
|
84
|
+
}
|
|
85
|
+
const address = toHex(val);
|
|
33
86
|
if (!address || !isValidSuiAddress(normalizeSuiAddress(address))) {
|
|
34
87
|
throw new Error(`Invalid Sui address ${address}`);
|
|
35
88
|
}
|
|
89
|
+
return val;
|
|
36
90
|
},
|
|
37
|
-
|
|
38
|
-
typeof val === 'string' ? fromHex(normalizeSuiAddress(val)) : val,
|
|
39
|
-
output: (val) => normalizeSuiAddress(toHex(val)),
|
|
91
|
+
output: (val) => `0x${toHex(val)}`,
|
|
40
92
|
});
|
|
41
93
|
|
|
42
94
|
export const ObjectDigest = bcs.byteVector().transform({
|
|
43
95
|
name: 'ObjectDigest',
|
|
44
|
-
input
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
96
|
+
// The length check lives in input so the digest is only base58-decoded once per write;
|
|
97
|
+
// transform's validate runs input, so validate() still rejects digests of the wrong length.
|
|
98
|
+
input: (value: string) => {
|
|
99
|
+
const bytes = fromBase58(value);
|
|
100
|
+
if (bytes.length !== 32) {
|
|
48
101
|
throw new Error('ObjectDigest must be 32 bytes');
|
|
49
102
|
}
|
|
103
|
+
return bytes;
|
|
50
104
|
},
|
|
105
|
+
output: (value) => toBase58(value),
|
|
51
106
|
});
|
|
52
107
|
|
|
53
108
|
export const SuiObjectRef = bcs.struct('SuiObjectRef', {
|
|
@@ -113,7 +168,7 @@ export const CallArg = bcs.enum('CallArg', {
|
|
|
113
168
|
Pure: bcs.struct('Pure', {
|
|
114
169
|
bytes: bcs.byteVector().transform({
|
|
115
170
|
input: (val: string | Uint8Array) => (typeof val === 'string' ? fromBase64(val) : val),
|
|
116
|
-
output: (val) => toBase64(
|
|
171
|
+
output: (val) => toBase64(val),
|
|
117
172
|
}),
|
|
118
173
|
}),
|
|
119
174
|
Object: ObjectArg,
|
|
@@ -190,7 +245,7 @@ export const Command = bcs.enum('Command', {
|
|
|
190
245
|
modules: bcs.vector(
|
|
191
246
|
bcs.byteVector().transform({
|
|
192
247
|
input: (val: string | Uint8Array) => (typeof val === 'string' ? fromBase64(val) : val),
|
|
193
|
-
output: (val) => toBase64(
|
|
248
|
+
output: (val) => toBase64(val),
|
|
194
249
|
}),
|
|
195
250
|
),
|
|
196
251
|
dependencies: bcs.vector(Address),
|
|
@@ -218,7 +273,7 @@ export const Command = bcs.enum('Command', {
|
|
|
218
273
|
modules: bcs.vector(
|
|
219
274
|
bcs.byteVector().transform({
|
|
220
275
|
input: (val: string | Uint8Array) => (typeof val === 'string' ? fromBase64(val) : val),
|
|
221
|
-
output: (val) => toBase64(
|
|
276
|
+
output: (val) => toBase64(val),
|
|
222
277
|
}),
|
|
223
278
|
),
|
|
224
279
|
dependencies: bcs.vector(Address),
|
|
@@ -341,14 +396,18 @@ export const CompressedSignature = bcs.enum('CompressedSignature', {
|
|
|
341
396
|
Secp256r1: bcs.bytes(64),
|
|
342
397
|
ZkLogin: bcs.byteVector(),
|
|
343
398
|
Passkey: bcs.byteVector(),
|
|
399
|
+
MLDSA65: bcs.bytes(3309),
|
|
344
400
|
});
|
|
345
401
|
|
|
402
|
+
// Variant order must match Sui's `PublicKey` enum, which validators hash to
|
|
403
|
+
// derive a multisig address.
|
|
346
404
|
export const PublicKey = bcs.enum('PublicKey', {
|
|
347
405
|
ED25519: bcs.bytes(32),
|
|
348
406
|
Secp256k1: bcs.bytes(33),
|
|
349
407
|
Secp256r1: bcs.bytes(33),
|
|
350
408
|
ZkLogin: bcs.byteVector(),
|
|
351
409
|
Passkey: bcs.bytes(33),
|
|
410
|
+
MLDSA65: bcs.bytes(1952),
|
|
352
411
|
});
|
|
353
412
|
|
|
354
413
|
export const MultiSigPkMap = bcs.struct('MultiSigPkMap', {
|
|
@@ -369,7 +428,7 @@ export const MultiSig = bcs.struct('MultiSig', {
|
|
|
369
428
|
|
|
370
429
|
export const base64String = bcs.byteVector().transform({
|
|
371
430
|
input: (val: string | Uint8Array) => (typeof val === 'string' ? fromBase64(val) : val),
|
|
372
|
-
output: (val) => toBase64(
|
|
431
|
+
output: (val) => toBase64(val),
|
|
373
432
|
});
|
|
374
433
|
|
|
375
434
|
export const PasskeyAuthenticator = bcs.struct('PasskeyAuthenticator', {
|
|
@@ -150,6 +150,7 @@ export function parseSerializedKeypairSignature(serializedSignature: string) {
|
|
|
150
150
|
case 'ED25519':
|
|
151
151
|
case 'Secp256k1':
|
|
152
152
|
case 'Secp256r1':
|
|
153
|
+
case 'MLDSA65':
|
|
153
154
|
const size =
|
|
154
155
|
SIGNATURE_SCHEME_TO_SIZE[signatureScheme as keyof typeof SIGNATURE_SCHEME_TO_SIZE];
|
|
155
156
|
const signature = bytes.slice(1, bytes.length - size);
|
|
@@ -8,6 +8,7 @@ export const SIGNATURE_SCHEME_TO_FLAG = {
|
|
|
8
8
|
MultiSig: 0x03,
|
|
9
9
|
ZkLogin: 0x05,
|
|
10
10
|
Passkey: 0x06,
|
|
11
|
+
MLDSA65: 0x07,
|
|
11
12
|
} as const;
|
|
12
13
|
|
|
13
14
|
export const SIGNATURE_SCHEME_TO_SIZE = {
|
|
@@ -15,6 +16,7 @@ export const SIGNATURE_SCHEME_TO_SIZE = {
|
|
|
15
16
|
Secp256k1: 33,
|
|
16
17
|
Secp256r1: 33,
|
|
17
18
|
Passkey: 33,
|
|
19
|
+
MLDSA65: 1952,
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
export const SIGNATURE_FLAG_TO_SCHEME = {
|
|
@@ -24,9 +26,10 @@ export const SIGNATURE_FLAG_TO_SCHEME = {
|
|
|
24
26
|
0x03: 'MultiSig',
|
|
25
27
|
0x05: 'ZkLogin',
|
|
26
28
|
0x06: 'Passkey',
|
|
29
|
+
0x07: 'MLDSA65',
|
|
27
30
|
} as const;
|
|
28
31
|
|
|
29
32
|
export type SignatureScheme =
|
|
30
|
-
'ED25519' | 'Secp256k1' | 'Secp256r1' | 'MultiSig' | 'ZkLogin' | 'Passkey';
|
|
33
|
+
'ED25519' | 'Secp256k1' | 'Secp256r1' | 'MultiSig' | 'ZkLogin' | 'Passkey' | 'MLDSA65';
|
|
31
34
|
|
|
32
35
|
export type SignatureFlag = keyof typeof SIGNATURE_FLAG_TO_SCHEME;
|
|
@@ -69,6 +69,7 @@ export function parseSerializedSignature(serializedSignature: string) {
|
|
|
69
69
|
case 'ED25519':
|
|
70
70
|
case 'Secp256k1':
|
|
71
71
|
case 'Secp256r1':
|
|
72
|
+
case 'MLDSA65':
|
|
72
73
|
return parseSerializedKeypairSignature(serializedSignature);
|
|
73
74
|
default:
|
|
74
75
|
throw new Error('Unsupported signature scheme');
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { randomBytes } from '@noble/hashes/utils.js';
|
|
5
|
+
import { ml_dsa65 } from '@noble/post-quantum/ml-dsa.js';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
decodeSuiPrivateKey,
|
|
9
|
+
encodeSuiPrivateKey,
|
|
10
|
+
Keypair,
|
|
11
|
+
PRIVATE_KEY_SIZE,
|
|
12
|
+
} from '../../cryptography/keypair.js';
|
|
13
|
+
import type { SignatureScheme } from '../../cryptography/signature-scheme.js';
|
|
14
|
+
import { MLDSA65PublicKey } from './publickey.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* ML-DSA-65 Keypair data. `seed` is the 32-byte FIPS 204 seed, the only form
|
|
18
|
+
* that is ever exported or stored (`suiprivkey`, keystore). `secretKey` is the
|
|
19
|
+
* 4032-byte signing key expanded from it, kept so signing does not re-expand.
|
|
20
|
+
*/
|
|
21
|
+
export interface MLDSA65KeypairData {
|
|
22
|
+
seed: Uint8Array;
|
|
23
|
+
secretKey: Uint8Array;
|
|
24
|
+
publicKey: Uint8Array;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* An ML-DSA-65 (FIPS 204) Keypair used for signing transactions.
|
|
29
|
+
*/
|
|
30
|
+
export class MLDSA65Keypair extends Keypair {
|
|
31
|
+
private keypair: MLDSA65KeypairData;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Create a new ML-DSA-65 keypair instance.
|
|
35
|
+
* Generate random keypair if no {@link MLDSA65Keypair} is provided.
|
|
36
|
+
*
|
|
37
|
+
* @param keypair ML-DSA-65 keypair
|
|
38
|
+
*/
|
|
39
|
+
constructor(keypair?: MLDSA65KeypairData) {
|
|
40
|
+
super();
|
|
41
|
+
if (keypair) {
|
|
42
|
+
this.keypair = keypair;
|
|
43
|
+
} else {
|
|
44
|
+
this.keypair = expand(randomBytes(PRIVATE_KEY_SIZE));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Get the key scheme of the keypair MLDSA65
|
|
50
|
+
*/
|
|
51
|
+
getKeyScheme(): SignatureScheme {
|
|
52
|
+
return 'MLDSA65';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Generate a new random ML-DSA-65 keypair
|
|
57
|
+
*/
|
|
58
|
+
static generate(): MLDSA65Keypair {
|
|
59
|
+
return new MLDSA65Keypair();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Create an ML-DSA-65 keypair from its 32-byte seed.
|
|
64
|
+
*
|
|
65
|
+
* @param secretKey the 32-byte seed as a byte array or Bech32 secret key string
|
|
66
|
+
*/
|
|
67
|
+
static fromSecretKey(secretKey: Uint8Array | string): MLDSA65Keypair {
|
|
68
|
+
if (typeof secretKey === 'string') {
|
|
69
|
+
const decoded = decodeSuiPrivateKey(secretKey);
|
|
70
|
+
|
|
71
|
+
if (decoded.scheme !== 'MLDSA65') {
|
|
72
|
+
throw new Error(`Expected a MLDSA65 keypair, got ${decoded.scheme}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return this.fromSecretKey(decoded.secretKey);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (secretKey.length !== PRIVATE_KEY_SIZE) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Wrong secretKey size. Expected ${PRIVATE_KEY_SIZE} bytes, got ${secretKey.length}.`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return new MLDSA65Keypair(expand(secretKey));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Generate a keypair from a 32 byte seed. Same as {@link fromSecretKey}:
|
|
89
|
+
* for ML-DSA-65 the seed is the secret key.
|
|
90
|
+
*
|
|
91
|
+
* @param seed seed byte array
|
|
92
|
+
*/
|
|
93
|
+
static fromSeed(seed: Uint8Array): MLDSA65Keypair {
|
|
94
|
+
return MLDSA65Keypair.fromSecretKey(seed);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The public key for this ML-DSA-65 keypair
|
|
99
|
+
*/
|
|
100
|
+
getPublicKey(): MLDSA65PublicKey {
|
|
101
|
+
return new MLDSA65PublicKey(this.keypair.publicKey);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The Bech32 secret key string for this ML-DSA-65 keypair, encoding the 32-byte seed.
|
|
106
|
+
*/
|
|
107
|
+
getSecretKey(): string {
|
|
108
|
+
return encodeSuiPrivateKey(this.keypair.seed, this.getKeyScheme());
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Return the signature for the provided data using ML-DSA-65.
|
|
113
|
+
*
|
|
114
|
+
* Hedged signing: 32 fresh random bytes go into every signature, so signing the
|
|
115
|
+
* same data twice gives two different valid signatures.
|
|
116
|
+
*/
|
|
117
|
+
async sign(data: Uint8Array) {
|
|
118
|
+
return ml_dsa65.sign(data, this.keypair.secretKey) as Uint8Array<ArrayBuffer>;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function expand(input: Uint8Array): MLDSA65KeypairData {
|
|
123
|
+
// Own the seed: the caller may wipe or reuse its buffer, and the exported
|
|
124
|
+
// secret key must keep matching the signing key.
|
|
125
|
+
const seed = Uint8Array.from(input);
|
|
126
|
+
const { publicKey, secretKey } = ml_dsa65.keygen(seed);
|
|
127
|
+
return { seed, secretKey, publicKey };
|
|
128
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { fromBase64 } from '@mysten/bcs';
|
|
5
|
+
import { ml_dsa65 } from '@noble/post-quantum/ml-dsa.js';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
bytesEqual,
|
|
9
|
+
parseSerializedKeypairSignature,
|
|
10
|
+
PublicKey,
|
|
11
|
+
} from '../../cryptography/publickey.js';
|
|
12
|
+
import type { PublicKeyInitData } from '../../cryptography/publickey.js';
|
|
13
|
+
import { SIGNATURE_SCHEME_TO_FLAG } from '../../cryptography/signature-scheme.js';
|
|
14
|
+
|
|
15
|
+
export const MLDSA65_PUBLIC_KEY_SIZE = 1952;
|
|
16
|
+
export const MLDSA65_SIGNATURE_SIZE = 3309;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* An ML-DSA-65 (FIPS 204) public key
|
|
20
|
+
*/
|
|
21
|
+
export class MLDSA65PublicKey extends PublicKey {
|
|
22
|
+
static SIZE = MLDSA65_PUBLIC_KEY_SIZE;
|
|
23
|
+
private data: Uint8Array<ArrayBuffer>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Create a new MLDSA65PublicKey object
|
|
27
|
+
* @param value ML-DSA-65 public key as buffer or base-64 encoded string
|
|
28
|
+
*/
|
|
29
|
+
constructor(value: PublicKeyInitData) {
|
|
30
|
+
super();
|
|
31
|
+
|
|
32
|
+
if (typeof value === 'string') {
|
|
33
|
+
this.data = fromBase64(value);
|
|
34
|
+
} else if (value instanceof Uint8Array) {
|
|
35
|
+
this.data = value as Uint8Array<ArrayBuffer>;
|
|
36
|
+
} else {
|
|
37
|
+
this.data = Uint8Array.from(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (this.data.length !== MLDSA65_PUBLIC_KEY_SIZE) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Invalid public key input. Expected ${MLDSA65_PUBLIC_KEY_SIZE} bytes, got ${this.data.length}`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Checks if two ML-DSA-65 public keys are equal
|
|
49
|
+
*/
|
|
50
|
+
override equals(publicKey: MLDSA65PublicKey): boolean {
|
|
51
|
+
return super.equals(publicKey);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Return the byte array representation of the ML-DSA-65 public key
|
|
56
|
+
*/
|
|
57
|
+
toRawBytes(): Uint8Array<ArrayBuffer> {
|
|
58
|
+
return this.data;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Return the signature scheme flag of the ML-DSA-65 public key
|
|
63
|
+
*/
|
|
64
|
+
flag(): number {
|
|
65
|
+
return SIGNATURE_SCHEME_TO_FLAG['MLDSA65'];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Verifies that the signature is valid for for the provided message.
|
|
70
|
+
* Pure ML-DSA-65 with an empty context string.
|
|
71
|
+
*/
|
|
72
|
+
async verify(message: Uint8Array, signature: Uint8Array | string): Promise<boolean> {
|
|
73
|
+
let bytes;
|
|
74
|
+
if (typeof signature === 'string') {
|
|
75
|
+
const parsed = parseSerializedKeypairSignature(signature);
|
|
76
|
+
if (parsed.signatureScheme !== 'MLDSA65') {
|
|
77
|
+
throw new Error('Invalid signature scheme');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (!bytesEqual(this.toRawBytes(), parsed.publicKey)) {
|
|
81
|
+
throw new Error('Signature does not match public key');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
bytes = parsed.signature;
|
|
85
|
+
} else {
|
|
86
|
+
bytes = signature;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (bytes.length !== MLDSA65_SIGNATURE_SIZE) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return ml_dsa65.verify(bytes, message, this.toRawBytes());
|
|
94
|
+
}
|
|
95
|
+
}
|