@mysten/sui 2.35.0 → 2.36.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 (76) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/bcs/bcs.d.mts +6 -6
  3. package/dist/bcs/bcs.mjs +4 -2
  4. package/dist/bcs/bcs.mjs.map +1 -1
  5. package/dist/bcs/index.d.mts +12 -3
  6. package/dist/bcs/index.d.mts.map +1 -1
  7. package/dist/cryptography/publickey.mjs +1 -0
  8. package/dist/cryptography/publickey.mjs.map +1 -1
  9. package/dist/cryptography/signature-scheme.d.mts +4 -1
  10. package/dist/cryptography/signature-scheme.d.mts.map +1 -1
  11. package/dist/cryptography/signature-scheme.mjs +6 -3
  12. package/dist/cryptography/signature-scheme.mjs.map +1 -1
  13. package/dist/cryptography/signature.d.mts +6 -6
  14. package/dist/cryptography/signature.mjs +2 -1
  15. package/dist/cryptography/signature.mjs.map +1 -1
  16. package/dist/grpc/proto/sui/forking/v1alpha/forking_service.client.d.mts +4 -4
  17. package/dist/grpc/proto/sui/rpc/v2/ledger_service.client.d.mts +4 -4
  18. package/dist/grpc/proto/sui/rpc/v2/move_package_service.client.d.mts +4 -4
  19. package/dist/grpc/proto/sui/rpc/v2/name_service.client.d.mts +4 -4
  20. package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
  21. package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
  22. package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +4 -4
  23. package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.client.d.mts +4 -4
  24. package/dist/keypairs/mldsa65/index.d.mts +3 -0
  25. package/dist/keypairs/mldsa65/index.mjs +4 -0
  26. package/dist/keypairs/mldsa65/keypair.d.mts +67 -0
  27. package/dist/keypairs/mldsa65/keypair.d.mts.map +1 -0
  28. package/dist/keypairs/mldsa65/keypair.mjs +91 -0
  29. package/dist/keypairs/mldsa65/keypair.mjs.map +1 -0
  30. package/dist/keypairs/mldsa65/publickey.d.mts +37 -0
  31. package/dist/keypairs/mldsa65/publickey.d.mts.map +1 -0
  32. package/dist/keypairs/mldsa65/publickey.mjs +64 -0
  33. package/dist/keypairs/mldsa65/publickey.mjs.map +1 -0
  34. package/dist/multisig/publickey.d.mts +4 -0
  35. package/dist/multisig/publickey.d.mts.map +1 -1
  36. package/dist/multisig/publickey.mjs +7 -6
  37. package/dist/multisig/publickey.mjs.map +1 -1
  38. package/dist/transactions/Transaction.d.mts +6 -6
  39. package/dist/verify/verify.d.mts.map +1 -1
  40. package/dist/verify/verify.mjs +4 -0
  41. package/dist/verify/verify.mjs.map +1 -1
  42. package/dist/version.mjs +1 -1
  43. package/dist/version.mjs.map +1 -1
  44. package/dist/zklogin/address-seed.d.mts +6 -0
  45. package/dist/zklogin/address-seed.d.mts.map +1 -0
  46. package/dist/zklogin/address-seed.mjs +51 -0
  47. package/dist/zklogin/address-seed.mjs.map +1 -0
  48. package/dist/zklogin/address.d.mts.map +1 -1
  49. package/dist/zklogin/address.mjs +2 -1
  50. package/dist/zklogin/address.mjs.map +1 -1
  51. package/dist/zklogin/bcs.d.mts +14 -14
  52. package/dist/zklogin/index.d.mts +2 -1
  53. package/dist/zklogin/index.mjs +3 -2
  54. package/dist/zklogin/nonce.mjs +1 -1
  55. package/dist/zklogin/utils.d.mts +1 -3
  56. package/dist/zklogin/utils.d.mts.map +1 -1
  57. package/dist/zklogin/utils.mjs +1 -46
  58. package/dist/zklogin/utils.mjs.map +1 -1
  59. package/docs/cryptography/index.md +7 -6
  60. package/docs/cryptography/keypairs.md +13 -1
  61. package/docs/llms-index.md +1 -1
  62. package/package.json +7 -1
  63. package/src/bcs/bcs.ts +4 -0
  64. package/src/cryptography/publickey.ts +1 -0
  65. package/src/cryptography/signature-scheme.ts +4 -1
  66. package/src/cryptography/signature.ts +1 -0
  67. package/src/keypairs/mldsa65/index.ts +5 -0
  68. package/src/keypairs/mldsa65/keypair.ts +128 -0
  69. package/src/keypairs/mldsa65/publickey.ts +95 -0
  70. package/src/multisig/publickey.ts +13 -9
  71. package/src/verify/verify.ts +4 -0
  72. package/src/version.ts +1 -1
  73. package/src/zklogin/address-seed.ts +86 -0
  74. package/src/zklogin/address.ts +2 -6
  75. package/src/zklogin/index.ts +1 -2
  76. package/src/zklogin/utils.ts +0 -83
@@ -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
+ }
@@ -25,14 +25,16 @@ type CompressedSignature =
25
25
  | { Secp256k1: Uint8Array }
26
26
  | { Secp256r1: Uint8Array }
27
27
  | { ZkLogin: Uint8Array }
28
- | { Passkey: Uint8Array };
28
+ | { Passkey: Uint8Array }
29
+ | { MLDSA65: Uint8Array };
29
30
 
30
31
  type PublicKeyEnum =
31
32
  | { ED25519: Uint8Array }
32
33
  | { Secp256k1: Uint8Array }
33
34
  | { Secp256r1: Uint8Array }
34
35
  | { ZkLogin: Uint8Array }
35
- | { Passkey: Uint8Array };
36
+ | { Passkey: Uint8Array }
37
+ | { MLDSA65: Uint8Array };
36
38
 
37
39
  type PubkeyEnumWeightPair = {
38
40
  pubKey: PublicKeyEnum;
@@ -188,21 +190,22 @@ export class MultiSigPublicKey extends PublicKey {
188
190
  * Return the Sui address associated with this MultiSig public key
189
191
  */
190
192
  override toSuiAddress(): string {
191
- // max length = 1 flag byte + (max pk size + max weight size (u8)) * max signer size + 2 threshold bytes (u16)
192
- const maxLength = 1 + (64 + 1) * MAX_SIGNER_IN_MULTISIG + 2;
193
- const tmp = new Uint8Array(maxLength);
193
+ const suiBytes = this.publicKeys.map(({ publicKey }) => publicKey.toSuiBytes());
194
+ // 1 flag byte + 2 threshold bytes (u16) + per member: flagged public key + 1 weight byte (u8)
195
+ const length = 3 + suiBytes.reduce((sum, bytes) => sum + bytes.length + 1, 0);
196
+ const tmp = new Uint8Array(length);
194
197
  tmp.set([SIGNATURE_SCHEME_TO_FLAG['MultiSig']]);
195
198
 
196
199
  tmp.set(bcs.u16().serialize(this.multisigPublicKey.threshold).toBytes(), 1);
197
200
  // The initial value 3 ensures that following data will be after the flag byte and threshold bytes
198
201
  let i = 3;
199
- for (const { publicKey, weight } of this.publicKeys) {
200
- const bytes = publicKey.toSuiBytes();
202
+ for (const [j, { weight }] of this.publicKeys.entries()) {
203
+ const bytes = suiBytes[j];
201
204
  tmp.set(bytes, i);
202
205
  i += bytes.length;
203
206
  tmp.set([weight], i++);
204
207
  }
205
- return normalizeSuiAddress(bytesToHex(blake2b(tmp.slice(0, i), { dkLen: 32 })));
208
+ return normalizeSuiAddress(bytesToHex(blake2b(tmp, { dkLen: 32 })));
206
209
  }
207
210
 
208
211
  /**
@@ -302,7 +305,8 @@ export class MultiSigPublicKey extends PublicKey {
302
305
  bitmap,
303
306
  multisig_pk: this.multisigPublicKey,
304
307
  };
305
- const bytes = bcs.MultiSig.serialize(multisig, { maxSize: 8192 }).toBytes();
308
+ // Ten ML-DSA-65 members are ~53 KB of signatures and public keys.
309
+ const bytes = bcs.MultiSig.serialize(multisig, { maxSize: 65536 }).toBytes();
306
310
  const tmp = new Uint8Array(bytes.length + 1);
307
311
  tmp.set([SIGNATURE_SCHEME_TO_FLAG['MultiSig']]);
308
312
  tmp.set(bytes, 1);
@@ -6,6 +6,7 @@ import { fromBase64 } from '@mysten/bcs';
6
6
  import type { PublicKey, SignatureFlag, SignatureScheme } from '../cryptography/index.js';
7
7
  import { parseSerializedSignature, SIGNATURE_FLAG_TO_SCHEME } from '../cryptography/index.js';
8
8
  import { Ed25519PublicKey } from '../keypairs/ed25519/publickey.js';
9
+ import { MLDSA65PublicKey } from '../keypairs/mldsa65/publickey.js';
9
10
  import { PasskeyPublicKey } from '../keypairs/passkey/publickey.js';
10
11
  import { Secp256k1PublicKey } from '../keypairs/secp256k1/publickey.js';
11
12
  import { Secp256r1PublicKey } from '../keypairs/secp256r1/publickey.js';
@@ -159,6 +160,9 @@ export function publicKeyFromRawBytes(
159
160
  case 'Passkey':
160
161
  publicKey = new PasskeyPublicKey(bytes);
161
162
  break;
163
+ case 'MLDSA65':
164
+ publicKey = new MLDSA65PublicKey(bytes);
165
+ break;
162
166
  default:
163
167
  throw new Error(`Unsupported signature scheme ${signatureScheme}`);
164
168
  }
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.35.0';
6
+ export const PACKAGE_VERSION = '2.36.0';
@@ -0,0 +1,86 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { poseidonHash } from './poseidon.js';
5
+
6
+ const MAX_KEY_CLAIM_NAME_LENGTH = 32;
7
+ const MAX_KEY_CLAIM_VALUE_LENGTH = 115;
8
+ const MAX_AUD_VALUE_LENGTH = 145;
9
+ const PACK_WIDTH = 248;
10
+
11
+ /**
12
+ * Splits an array into chunks of size chunk_size. If the array is not evenly
13
+ * divisible by chunk_size, the first chunk will be smaller than chunk_size.
14
+ *
15
+ * E.g., arrayChunk([1, 2, 3, 4, 5], 2) => [[1], [2, 3], [4, 5]]
16
+ *
17
+ * Note: Can be made more efficient by avoiding the reverse() calls.
18
+ */
19
+ function chunkArray<T>(array: T[], chunk_size: number): T[][] {
20
+ const chunks = Array(Math.ceil(array.length / chunk_size));
21
+ const revArray = array.reverse();
22
+ for (let i = 0; i < chunks.length; i++) {
23
+ chunks[i] = revArray.slice(i * chunk_size, (i + 1) * chunk_size).reverse();
24
+ }
25
+ return chunks.reverse();
26
+ }
27
+
28
+ function bytesBEToBigInt(bytes: number[]): bigint {
29
+ const hex = bytes.map((b) => b.toString(16).padStart(2, '0')).join('');
30
+ if (hex.length === 0) {
31
+ return BigInt(0);
32
+ }
33
+ return BigInt('0x' + hex);
34
+ }
35
+
36
+ // hashes an ASCII string to a field element
37
+ export function hashASCIIStrToField(str: string, maxSize: number) {
38
+ if (str.length > maxSize) {
39
+ throw new Error(`String ${str} is longer than ${maxSize} chars`);
40
+ }
41
+
42
+ // Note: Padding with zeroes is safe because we are only using this function to map human-readable sequence of bytes.
43
+ // So the ASCII values of those characters will never be zero (null character).
44
+ const strPadded = str
45
+ .padEnd(maxSize, String.fromCharCode(0))
46
+ .split('')
47
+ .map((c) => c.charCodeAt(0));
48
+
49
+ const chunkSize = PACK_WIDTH / 8;
50
+ const packed = chunkArray(strPadded, chunkSize).map((chunk) => bytesBEToBigInt(chunk));
51
+ return poseidonHash(packed);
52
+ }
53
+
54
+ // Reject claim inputs whose decoded form reveals a JSON escape ('"', '\', control char):
55
+ // the circuit hashes raw JWT bytes, so an escaped value would derive a different address.
56
+ function assertNoJsonEscape(value: string, label: string) {
57
+ for (let i = 0; i < value.length; i++) {
58
+ const c = value.charCodeAt(i);
59
+ if (c < 0x20 || c === 0x22 || c === 0x5c) {
60
+ throw new Error(
61
+ `zkLogin ${label} contains a JSON-escaped character (code ${c}); the circuit ` +
62
+ `hashes raw JWT bytes, so claim values with escapes are not supported`,
63
+ );
64
+ }
65
+ }
66
+ }
67
+
68
+ export function genAddressSeed(
69
+ salt: string | bigint,
70
+ name: string,
71
+ value: string,
72
+ aud: string,
73
+ max_name_length = MAX_KEY_CLAIM_NAME_LENGTH,
74
+ max_value_length = MAX_KEY_CLAIM_VALUE_LENGTH,
75
+ max_aud_length = MAX_AUD_VALUE_LENGTH,
76
+ ): bigint {
77
+ assertNoJsonEscape(name, 'key claim name');
78
+ assertNoJsonEscape(value, 'key claim value');
79
+ assertNoJsonEscape(aud, 'aud');
80
+ return poseidonHash([
81
+ hashASCIIStrToField(name, max_name_length),
82
+ hashASCIIStrToField(value, max_value_length),
83
+ hashASCIIStrToField(aud, max_aud_length),
84
+ poseidonHash([BigInt(salt)]),
85
+ ]);
86
+ }
@@ -6,13 +6,9 @@ import { bytesToHex } from '@noble/hashes/utils.js';
6
6
 
7
7
  import { SIGNATURE_SCHEME_TO_FLAG } from '../cryptography/signature-scheme.js';
8
8
  import { normalizeSuiAddress, SUI_ADDRESS_LENGTH } from '../utils/index.js';
9
+ import { genAddressSeed } from './address-seed.js';
9
10
  import { decodeJwt } from './jwt-utils.js';
10
- import {
11
- genAddressSeed,
12
- normalizeZkLoginIssuer,
13
- toBigEndianBytes,
14
- toPaddedBigEndianBytes,
15
- } from './utils.js';
11
+ import { normalizeZkLoginIssuer, toBigEndianBytes, toPaddedBigEndianBytes } from './utils.js';
16
12
 
17
13
  export function computeZkLoginAddressFromSeed(
18
14
  addressSeed: bigint,
@@ -5,10 +5,9 @@ export { getZkLoginSignature, parseZkLoginSignature } from './signature.js';
5
5
  export {
6
6
  toBigEndianBytes,
7
7
  toPaddedBigEndianBytes,
8
- hashASCIIStrToField,
9
- genAddressSeed,
10
8
  getExtendedEphemeralPublicKey,
11
9
  } from './utils.js';
10
+ export { hashASCIIStrToField, genAddressSeed } from './address-seed.js';
12
11
  export { computeZkLoginAddressFromSeed, computeZkLoginAddress, jwtToAddress } from './address.js';
13
12
  export type { ComputeZkLoginAddressOptions } from './address.js';
14
13
  export { toZkLoginPublicIdentifier, ZkLoginPublicIdentifier } from './publickey.js';
@@ -4,12 +4,6 @@
4
4
  import { hexToBytes } from '@noble/hashes/utils.js';
5
5
 
6
6
  import type { PublicKey } from '../cryptography/publickey.js';
7
- import { poseidonHash } from './poseidon.js';
8
-
9
- const MAX_KEY_CLAIM_NAME_LENGTH = 32;
10
- const MAX_KEY_CLAIM_VALUE_LENGTH = 115;
11
- const MAX_AUD_VALUE_LENGTH = 145;
12
- const PACK_WIDTH = 248;
13
7
 
14
8
  function findFirstNonZeroIndex(bytes: Uint8Array) {
15
9
  for (let i = 0; i < bytes.length; i++) {
@@ -44,83 +38,6 @@ export function getExtendedEphemeralPublicKey(publicKey: PublicKey) {
44
38
  return publicKey.toSuiPublicKey();
45
39
  }
46
40
 
47
- /**
48
- * Splits an array into chunks of size chunk_size. If the array is not evenly
49
- * divisible by chunk_size, the first chunk will be smaller than chunk_size.
50
- *
51
- * E.g., arrayChunk([1, 2, 3, 4, 5], 2) => [[1], [2, 3], [4, 5]]
52
- *
53
- * Note: Can be made more efficient by avoiding the reverse() calls.
54
- */
55
- export function chunkArray<T>(array: T[], chunk_size: number): T[][] {
56
- const chunks = Array(Math.ceil(array.length / chunk_size));
57
- const revArray = array.reverse();
58
- for (let i = 0; i < chunks.length; i++) {
59
- chunks[i] = revArray.slice(i * chunk_size, (i + 1) * chunk_size).reverse();
60
- }
61
- return chunks.reverse();
62
- }
63
-
64
- function bytesBEToBigInt(bytes: number[]): bigint {
65
- const hex = bytes.map((b) => b.toString(16).padStart(2, '0')).join('');
66
- if (hex.length === 0) {
67
- return BigInt(0);
68
- }
69
- return BigInt('0x' + hex);
70
- }
71
-
72
- // hashes an ASCII string to a field element
73
- export function hashASCIIStrToField(str: string, maxSize: number) {
74
- if (str.length > maxSize) {
75
- throw new Error(`String ${str} is longer than ${maxSize} chars`);
76
- }
77
-
78
- // Note: Padding with zeroes is safe because we are only using this function to map human-readable sequence of bytes.
79
- // So the ASCII values of those characters will never be zero (null character).
80
- const strPadded = str
81
- .padEnd(maxSize, String.fromCharCode(0))
82
- .split('')
83
- .map((c) => c.charCodeAt(0));
84
-
85
- const chunkSize = PACK_WIDTH / 8;
86
- const packed = chunkArray(strPadded, chunkSize).map((chunk) => bytesBEToBigInt(chunk));
87
- return poseidonHash(packed);
88
- }
89
-
90
- // Reject claim inputs whose decoded form reveals a JSON escape ('"', '\', control char):
91
- // the circuit hashes raw JWT bytes, so an escaped value would derive a different address.
92
- function assertNoJsonEscape(value: string, label: string) {
93
- for (let i = 0; i < value.length; i++) {
94
- const c = value.charCodeAt(i);
95
- if (c < 0x20 || c === 0x22 || c === 0x5c) {
96
- throw new Error(
97
- `zkLogin ${label} contains a JSON-escaped character (code ${c}); the circuit ` +
98
- `hashes raw JWT bytes, so claim values with escapes are not supported`,
99
- );
100
- }
101
- }
102
- }
103
-
104
- export function genAddressSeed(
105
- salt: string | bigint,
106
- name: string,
107
- value: string,
108
- aud: string,
109
- max_name_length = MAX_KEY_CLAIM_NAME_LENGTH,
110
- max_value_length = MAX_KEY_CLAIM_VALUE_LENGTH,
111
- max_aud_length = MAX_AUD_VALUE_LENGTH,
112
- ): bigint {
113
- assertNoJsonEscape(name, 'key claim name');
114
- assertNoJsonEscape(value, 'key claim value');
115
- assertNoJsonEscape(aud, 'aud');
116
- return poseidonHash([
117
- hashASCIIStrToField(name, max_name_length),
118
- hashASCIIStrToField(value, max_value_length),
119
- hashASCIIStrToField(aud, max_aud_length),
120
- poseidonHash([BigInt(salt)]),
121
- ]);
122
- }
123
-
124
41
  export function normalizeZkLoginIssuer(iss: string) {
125
42
  if (iss === 'accounts.google.com') {
126
43
  return 'https://accounts.google.com';