@mysten/sui 2.34.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 (115) hide show
  1. package/CHANGELOG.md +21 -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/client/core-resolver.mjs +1 -1
  8. package/dist/client/types.d.mts +18 -1
  9. package/dist/client/types.d.mts.map +1 -1
  10. package/dist/client/utils.d.mts.map +1 -1
  11. package/dist/client/utils.mjs +10 -1
  12. package/dist/client/utils.mjs.map +1 -1
  13. package/dist/cryptography/publickey.mjs +1 -0
  14. package/dist/cryptography/publickey.mjs.map +1 -1
  15. package/dist/cryptography/signature-scheme.d.mts +4 -1
  16. package/dist/cryptography/signature-scheme.d.mts.map +1 -1
  17. package/dist/cryptography/signature-scheme.mjs +6 -3
  18. package/dist/cryptography/signature-scheme.mjs.map +1 -1
  19. package/dist/cryptography/signature.d.mts +6 -6
  20. package/dist/cryptography/signature.mjs +2 -1
  21. package/dist/cryptography/signature.mjs.map +1 -1
  22. package/dist/graphql/client.d.mts +4 -3
  23. package/dist/graphql/client.d.mts.map +1 -1
  24. package/dist/graphql/client.mjs.map +1 -1
  25. package/dist/graphql/core.d.mts.map +1 -1
  26. package/dist/graphql/core.mjs +7 -4
  27. package/dist/graphql/core.mjs.map +1 -1
  28. package/dist/graphql/generated/queries.mjs +7 -0
  29. package/dist/graphql/generated/queries.mjs.map +1 -1
  30. package/dist/grpc/client.d.mts +4 -3
  31. package/dist/grpc/client.d.mts.map +1 -1
  32. package/dist/grpc/client.mjs.map +1 -1
  33. package/dist/grpc/core.d.mts.map +1 -1
  34. package/dist/grpc/core.mjs +8 -4
  35. package/dist/grpc/core.mjs.map +1 -1
  36. package/dist/grpc/proto/sui/rpc/v2/name_service.client.d.mts +4 -4
  37. package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
  38. package/dist/jsonRpc/core.d.mts.map +1 -1
  39. package/dist/jsonRpc/core.mjs +7 -4
  40. package/dist/jsonRpc/core.mjs.map +1 -1
  41. package/dist/keypairs/mldsa65/index.d.mts +3 -0
  42. package/dist/keypairs/mldsa65/index.mjs +4 -0
  43. package/dist/keypairs/mldsa65/keypair.d.mts +67 -0
  44. package/dist/keypairs/mldsa65/keypair.d.mts.map +1 -0
  45. package/dist/keypairs/mldsa65/keypair.mjs +91 -0
  46. package/dist/keypairs/mldsa65/keypair.mjs.map +1 -0
  47. package/dist/keypairs/mldsa65/publickey.d.mts +37 -0
  48. package/dist/keypairs/mldsa65/publickey.d.mts.map +1 -0
  49. package/dist/keypairs/mldsa65/publickey.mjs +64 -0
  50. package/dist/keypairs/mldsa65/publickey.mjs.map +1 -0
  51. package/dist/multisig/publickey.d.mts +4 -0
  52. package/dist/multisig/publickey.d.mts.map +1 -1
  53. package/dist/multisig/publickey.mjs +7 -6
  54. package/dist/multisig/publickey.mjs.map +1 -1
  55. package/dist/transactions/Transaction.d.mts +10 -8
  56. package/dist/transactions/Transaction.d.mts.map +1 -1
  57. package/dist/transactions/Transaction.mjs +4 -2
  58. package/dist/transactions/Transaction.mjs.map +1 -1
  59. package/dist/utils/constants.d.mts +6 -1
  60. package/dist/utils/constants.d.mts.map +1 -1
  61. package/dist/utils/constants.mjs +6 -1
  62. package/dist/utils/constants.mjs.map +1 -1
  63. package/dist/utils/index.d.mts +2 -2
  64. package/dist/utils/index.mjs +2 -2
  65. package/dist/verify/verify.d.mts.map +1 -1
  66. package/dist/verify/verify.mjs +4 -0
  67. package/dist/verify/verify.mjs.map +1 -1
  68. package/dist/version.mjs +1 -1
  69. package/dist/version.mjs.map +1 -1
  70. package/dist/zklogin/address-seed.d.mts +6 -0
  71. package/dist/zklogin/address-seed.d.mts.map +1 -0
  72. package/dist/zklogin/address-seed.mjs +51 -0
  73. package/dist/zklogin/address-seed.mjs.map +1 -0
  74. package/dist/zklogin/address.d.mts.map +1 -1
  75. package/dist/zklogin/address.mjs +2 -1
  76. package/dist/zklogin/address.mjs.map +1 -1
  77. package/dist/zklogin/bcs.d.mts +14 -14
  78. package/dist/zklogin/index.d.mts +2 -1
  79. package/dist/zklogin/index.mjs +3 -2
  80. package/dist/zklogin/nonce.mjs +1 -1
  81. package/dist/zklogin/utils.d.mts +1 -3
  82. package/dist/zklogin/utils.d.mts.map +1 -1
  83. package/dist/zklogin/utils.mjs +1 -46
  84. package/dist/zklogin/utils.mjs.map +1 -1
  85. package/docs/clients/executing.md +7 -1
  86. package/docs/cryptography/index.md +7 -6
  87. package/docs/cryptography/keypairs.md +13 -1
  88. package/docs/llms-index.md +1 -1
  89. package/package.json +7 -1
  90. package/src/bcs/bcs.ts +4 -0
  91. package/src/client/types.ts +18 -1
  92. package/src/client/utils.ts +10 -0
  93. package/src/cryptography/publickey.ts +1 -0
  94. package/src/cryptography/signature-scheme.ts +4 -1
  95. package/src/cryptography/signature.ts +1 -0
  96. package/src/graphql/client.ts +4 -3
  97. package/src/graphql/core.ts +9 -3
  98. package/src/graphql/generated/queries.ts +8 -1
  99. package/src/graphql/queries/transactions.graphql +7 -0
  100. package/src/grpc/client.ts +4 -3
  101. package/src/grpc/core.ts +11 -4
  102. package/src/jsonRpc/core.ts +5 -0
  103. package/src/keypairs/mldsa65/index.ts +5 -0
  104. package/src/keypairs/mldsa65/keypair.ts +128 -0
  105. package/src/keypairs/mldsa65/publickey.ts +95 -0
  106. package/src/multisig/publickey.ts +13 -9
  107. package/src/transactions/Transaction.ts +4 -2
  108. package/src/utils/constants.ts +6 -0
  109. package/src/utils/index.ts +1 -0
  110. package/src/verify/verify.ts +4 -0
  111. package/src/version.ts +1 -1
  112. package/src/zklogin/address-seed.ts +86 -0
  113. package/src/zklogin/address.ts +2 -6
  114. package/src/zklogin/index.ts +1 -2
  115. package/src/zklogin/utils.ts +0 -83
@@ -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
+ }
@@ -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);
@@ -355,7 +355,8 @@ export class Transaction {
355
355
 
356
356
  /**
357
357
  * Creates a Coin<T> of the specified type and amount (defaults to SUI).
358
- * Sourced from address balance when available, falling back to owned coins.
358
+ * Sourced from address balance when available, falling back to owned coins. For SUI, falls back
359
+ * to the gas coin instead unless `useGasCoin` is `false`.
359
360
  * With `allowance`, spends only from the funder's address balance under that allowance.
360
361
  * Allowance IDs are resolved using the build client; app-bound allowances also require an app type and SpendPermit.
361
362
  */
@@ -370,7 +371,8 @@ export class Transaction {
370
371
 
371
372
  /**
372
373
  * Creates a Balance<T> of the specified type and amount (defaults to SUI).
373
- * Sourced from address balance when available, falling back to owned coins.
374
+ * Sourced from address balance when available, falling back to owned coins. For SUI, falls back
375
+ * to the gas coin instead unless `useGasCoin` is `false`.
374
376
  * With `allowance`, spends only from the funder's address balance under that allowance.
375
377
  * Allowance IDs are resolved using the build client; app-bound allowances also require an app type and SpendPermit.
376
378
  */
@@ -22,3 +22,9 @@ export const SUI_COIN_REGISTRY_OBJECT_ID =
22
22
  '0x000000000000000000000000000000000000000000000000000000000000000c';
23
23
  export const SUI_DENY_LIST_OBJECT_ID =
24
24
  '0x0000000000000000000000000000000000000000000000000000000000000403';
25
+ /**
26
+ * Object ID of the gas coin the node substitutes when a transaction is simulated without a gas
27
+ * payment. See `gasPaymentMocked` on `SimulateTransactionResult`.
28
+ */
29
+ export const MOCKED_GAS_OBJECT_ID =
30
+ '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
@@ -30,6 +30,7 @@ export {
30
30
  SUI_RANDOM_OBJECT_ID,
31
31
  SUI_COIN_REGISTRY_OBJECT_ID,
32
32
  SUI_DENY_LIST_OBJECT_ID,
33
+ MOCKED_GAS_OBJECT_ID,
33
34
  } from './constants.js';
34
35
 
35
36
  export { isValidNamedPackage, isValidNamedType } from './move-registry.js';
@@ -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.34.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';