@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.
- package/CHANGELOG.md +13 -0
- package/dist/bcs/bcs.d.mts +6 -6
- package/dist/bcs/bcs.mjs +4 -2
- 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 +6 -6
- 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/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/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 +4 -0
- package/dist/multisig/publickey.d.mts.map +1 -1
- package/dist/multisig/publickey.mjs +7 -6
- 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 +4 -0
- 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/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 +7 -1
- package/src/bcs/bcs.ts +4 -0
- 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 +13 -9
- package/src/verify/verify.ts +4 -0
- 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/utils.ts +0 -83
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { poseidonHash } from "./poseidon.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/zklogin/address-seed.ts
|
|
4
|
+
const MAX_KEY_CLAIM_NAME_LENGTH = 32;
|
|
5
|
+
const MAX_KEY_CLAIM_VALUE_LENGTH = 115;
|
|
6
|
+
const MAX_AUD_VALUE_LENGTH = 145;
|
|
7
|
+
const PACK_WIDTH = 248;
|
|
8
|
+
/**
|
|
9
|
+
* Splits an array into chunks of size chunk_size. If the array is not evenly
|
|
10
|
+
* divisible by chunk_size, the first chunk will be smaller than chunk_size.
|
|
11
|
+
*
|
|
12
|
+
* E.g., arrayChunk([1, 2, 3, 4, 5], 2) => [[1], [2, 3], [4, 5]]
|
|
13
|
+
*
|
|
14
|
+
* Note: Can be made more efficient by avoiding the reverse() calls.
|
|
15
|
+
*/
|
|
16
|
+
function chunkArray(array, chunk_size) {
|
|
17
|
+
const chunks = Array(Math.ceil(array.length / chunk_size));
|
|
18
|
+
const revArray = array.reverse();
|
|
19
|
+
for (let i = 0; i < chunks.length; i++) chunks[i] = revArray.slice(i * chunk_size, (i + 1) * chunk_size).reverse();
|
|
20
|
+
return chunks.reverse();
|
|
21
|
+
}
|
|
22
|
+
function bytesBEToBigInt(bytes) {
|
|
23
|
+
const hex = bytes.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
24
|
+
if (hex.length === 0) return BigInt(0);
|
|
25
|
+
return BigInt("0x" + hex);
|
|
26
|
+
}
|
|
27
|
+
function hashASCIIStrToField(str, maxSize) {
|
|
28
|
+
if (str.length > maxSize) throw new Error(`String ${str} is longer than ${maxSize} chars`);
|
|
29
|
+
return poseidonHash(chunkArray(str.padEnd(maxSize, String.fromCharCode(0)).split("").map((c) => c.charCodeAt(0)), PACK_WIDTH / 8).map((chunk) => bytesBEToBigInt(chunk)));
|
|
30
|
+
}
|
|
31
|
+
function assertNoJsonEscape(value, label) {
|
|
32
|
+
for (let i = 0; i < value.length; i++) {
|
|
33
|
+
const c = value.charCodeAt(i);
|
|
34
|
+
if (c < 32 || c === 34 || c === 92) throw new Error(`zkLogin ${label} contains a JSON-escaped character (code ${c}); the circuit hashes raw JWT bytes, so claim values with escapes are not supported`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function genAddressSeed(salt, name, value, aud, max_name_length = MAX_KEY_CLAIM_NAME_LENGTH, max_value_length = MAX_KEY_CLAIM_VALUE_LENGTH, max_aud_length = MAX_AUD_VALUE_LENGTH) {
|
|
38
|
+
assertNoJsonEscape(name, "key claim name");
|
|
39
|
+
assertNoJsonEscape(value, "key claim value");
|
|
40
|
+
assertNoJsonEscape(aud, "aud");
|
|
41
|
+
return poseidonHash([
|
|
42
|
+
hashASCIIStrToField(name, max_name_length),
|
|
43
|
+
hashASCIIStrToField(value, max_value_length),
|
|
44
|
+
hashASCIIStrToField(aud, max_aud_length),
|
|
45
|
+
poseidonHash([BigInt(salt)])
|
|
46
|
+
]);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
export { genAddressSeed, hashASCIIStrToField };
|
|
51
|
+
//# sourceMappingURL=address-seed.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address-seed.mjs","names":[],"sources":["../../src/zklogin/address-seed.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { poseidonHash } from './poseidon.js';\n\nconst MAX_KEY_CLAIM_NAME_LENGTH = 32;\nconst MAX_KEY_CLAIM_VALUE_LENGTH = 115;\nconst MAX_AUD_VALUE_LENGTH = 145;\nconst PACK_WIDTH = 248;\n\n/**\n * Splits an array into chunks of size chunk_size. If the array is not evenly\n * divisible by chunk_size, the first chunk will be smaller than chunk_size.\n *\n * E.g., arrayChunk([1, 2, 3, 4, 5], 2) => [[1], [2, 3], [4, 5]]\n *\n * Note: Can be made more efficient by avoiding the reverse() calls.\n */\nfunction chunkArray<T>(array: T[], chunk_size: number): T[][] {\n\tconst chunks = Array(Math.ceil(array.length / chunk_size));\n\tconst revArray = array.reverse();\n\tfor (let i = 0; i < chunks.length; i++) {\n\t\tchunks[i] = revArray.slice(i * chunk_size, (i + 1) * chunk_size).reverse();\n\t}\n\treturn chunks.reverse();\n}\n\nfunction bytesBEToBigInt(bytes: number[]): bigint {\n\tconst hex = bytes.map((b) => b.toString(16).padStart(2, '0')).join('');\n\tif (hex.length === 0) {\n\t\treturn BigInt(0);\n\t}\n\treturn BigInt('0x' + hex);\n}\n\n// hashes an ASCII string to a field element\nexport function hashASCIIStrToField(str: string, maxSize: number) {\n\tif (str.length > maxSize) {\n\t\tthrow new Error(`String ${str} is longer than ${maxSize} chars`);\n\t}\n\n\t// Note: Padding with zeroes is safe because we are only using this function to map human-readable sequence of bytes.\n\t// So the ASCII values of those characters will never be zero (null character).\n\tconst strPadded = str\n\t\t.padEnd(maxSize, String.fromCharCode(0))\n\t\t.split('')\n\t\t.map((c) => c.charCodeAt(0));\n\n\tconst chunkSize = PACK_WIDTH / 8;\n\tconst packed = chunkArray(strPadded, chunkSize).map((chunk) => bytesBEToBigInt(chunk));\n\treturn poseidonHash(packed);\n}\n\n// Reject claim inputs whose decoded form reveals a JSON escape ('\"', '\\', control char):\n// the circuit hashes raw JWT bytes, so an escaped value would derive a different address.\nfunction assertNoJsonEscape(value: string, label: string) {\n\tfor (let i = 0; i < value.length; i++) {\n\t\tconst c = value.charCodeAt(i);\n\t\tif (c < 0x20 || c === 0x22 || c === 0x5c) {\n\t\t\tthrow new Error(\n\t\t\t\t`zkLogin ${label} contains a JSON-escaped character (code ${c}); the circuit ` +\n\t\t\t\t\t`hashes raw JWT bytes, so claim values with escapes are not supported`,\n\t\t\t);\n\t\t}\n\t}\n}\n\nexport function genAddressSeed(\n\tsalt: string | bigint,\n\tname: string,\n\tvalue: string,\n\taud: string,\n\tmax_name_length = MAX_KEY_CLAIM_NAME_LENGTH,\n\tmax_value_length = MAX_KEY_CLAIM_VALUE_LENGTH,\n\tmax_aud_length = MAX_AUD_VALUE_LENGTH,\n): bigint {\n\tassertNoJsonEscape(name, 'key claim name');\n\tassertNoJsonEscape(value, 'key claim value');\n\tassertNoJsonEscape(aud, 'aud');\n\treturn poseidonHash([\n\t\thashASCIIStrToField(name, max_name_length),\n\t\thashASCIIStrToField(value, max_value_length),\n\t\thashASCIIStrToField(aud, max_aud_length),\n\t\tposeidonHash([BigInt(salt)]),\n\t]);\n}\n"],"mappings":";;;AAKA,MAAM,4BAA4B;AAClC,MAAM,6BAA6B;AACnC,MAAM,uBAAuB;AAC7B,MAAM,aAAa;;;;;;;;;AAUnB,SAAS,WAAc,OAAY,YAA2B;CAC7D,MAAM,SAAS,MAAM,KAAK,KAAK,MAAM,SAAS,WAAW,CAAC;CAC1D,MAAM,WAAW,MAAM,SAAS;AAChC,MAAK,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,IAClC,QAAO,KAAK,SAAS,MAAM,IAAI,aAAa,IAAI,KAAK,WAAW,CAAC,SAAS;AAE3E,QAAO,OAAO,SAAS;;AAGxB,SAAS,gBAAgB,OAAyB;CACjD,MAAM,MAAM,MAAM,KAAK,MAAM,EAAE,SAAS,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC,KAAK,GAAG;AACtE,KAAI,IAAI,WAAW,EAClB,QAAO,OAAO,EAAE;AAEjB,QAAO,OAAO,OAAO,IAAI;;AAI1B,SAAgB,oBAAoB,KAAa,SAAiB;AACjE,KAAI,IAAI,SAAS,QAChB,OAAM,IAAI,MAAM,UAAU,IAAI,kBAAkB,QAAQ,QAAQ;AAYjE,QAAO,aADQ,WANG,IAChB,OAAO,SAAS,OAAO,aAAa,EAAE,CAAC,CACvC,MAAM,GAAG,CACT,KAAK,MAAM,EAAE,WAAW,EAAE,CAAC,EAEX,aAAa,EACgB,CAAC,KAAK,UAAU,gBAAgB,MAAM,CAAC,CAC3D;;AAK5B,SAAS,mBAAmB,OAAe,OAAe;AACzD,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;EACtC,MAAM,IAAI,MAAM,WAAW,EAAE;AAC7B,MAAI,IAAI,MAAQ,MAAM,MAAQ,MAAM,GACnC,OAAM,IAAI,MACT,WAAW,MAAM,2CAA2C,EAAE,qFAE9D;;;AAKJ,SAAgB,eACf,MACA,MACA,OACA,KACA,kBAAkB,2BAClB,mBAAmB,4BACnB,iBAAiB,sBACR;AACT,oBAAmB,MAAM,iBAAiB;AAC1C,oBAAmB,OAAO,kBAAkB;AAC5C,oBAAmB,KAAK,MAAM;AAC9B,QAAO,aAAa;EACnB,oBAAoB,MAAM,gBAAgB;EAC1C,oBAAoB,OAAO,iBAAiB;EAC5C,oBAAoB,KAAK,eAAe;EACxC,aAAa,CAAC,OAAO,KAAK,CAAC,CAAC;EAC5B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address.d.mts","names":[],"sources":["../../src/zklogin/address.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"address.d.mts","names":[],"sources":["../../src/zklogin/address.ts"],"mappings":";iBAYgB,6BAAA,CACf,WAAA,UACA,GAAA,UACA,aAAA;AAAA,iBA+Ce,YAAA,CAAa,GAAA,UAAa,QAAA,mBAA2B,aAAA;AAAA,UAmBpD,4BAAA;EAChB,SAAA;EACA,UAAA;EACA,QAAA;EACA,GAAA;EACA,GAAA;EACA,aAAA;AAAA;AAAA,iBAGe,qBAAA,CAAA;EACf,SAAA;EACA,UAAA;EACA,GAAA;EACA,GAAA;EACA,QAAA;EACA;AAAA,GACE,4BAAA"}
|
package/dist/zklogin/address.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { SUI_ADDRESS_LENGTH, normalizeSuiAddress } from "../utils/sui-types.mjs";
|
|
2
2
|
import { SIGNATURE_SCHEME_TO_FLAG } from "../cryptography/signature-scheme.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { normalizeZkLoginIssuer, toBigEndianBytes, toPaddedBigEndianBytes } from "./utils.mjs";
|
|
4
4
|
import { decodeJwt } from "./jwt-utils.mjs";
|
|
5
|
+
import { genAddressSeed } from "./address-seed.mjs";
|
|
5
6
|
import { blake2b } from "@noble/hashes/blake2.js";
|
|
6
7
|
import { bytesToHex } from "@noble/hashes/utils.js";
|
|
7
8
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address.mjs","names":[],"sources":["../../src/zklogin/address.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { blake2b } from '@noble/hashes/blake2.js';\nimport { bytesToHex } from '@noble/hashes/utils.js';\n\nimport { SIGNATURE_SCHEME_TO_FLAG } from '../cryptography/signature-scheme.js';\nimport { normalizeSuiAddress, SUI_ADDRESS_LENGTH } from '../utils/index.js';\nimport { decodeJwt } from './jwt-utils.js';\nimport {
|
|
1
|
+
{"version":3,"file":"address.mjs","names":[],"sources":["../../src/zklogin/address.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { blake2b } from '@noble/hashes/blake2.js';\nimport { bytesToHex } from '@noble/hashes/utils.js';\n\nimport { SIGNATURE_SCHEME_TO_FLAG } from '../cryptography/signature-scheme.js';\nimport { normalizeSuiAddress, SUI_ADDRESS_LENGTH } from '../utils/index.js';\nimport { genAddressSeed } from './address-seed.js';\nimport { decodeJwt } from './jwt-utils.js';\nimport { normalizeZkLoginIssuer, toBigEndianBytes, toPaddedBigEndianBytes } from './utils.js';\n\nexport function computeZkLoginAddressFromSeed(\n\taddressSeed: bigint,\n\tiss: string,\n\tlegacyAddress: boolean,\n) {\n\t// Explicitly check for people not using typescript and ignoring the major version migration guide\n\tif (legacyAddress === undefined) {\n\t\tthrow new Error('legacyAddress parameter must be specified');\n\t}\n\tconst addressSeedBytesBigEndian = legacyAddress\n\t\t? toBigEndianBytes(addressSeed, 32)\n\t\t: toPaddedBigEndianBytes(addressSeed, 32);\n\n\tconst addressParamBytes = new TextEncoder().encode(normalizeZkLoginIssuer(iss));\n\tconst tmp = new Uint8Array(2 + addressSeedBytesBigEndian.length + addressParamBytes.length);\n\n\ttmp.set([SIGNATURE_SCHEME_TO_FLAG.ZkLogin]);\n\ttmp.set([addressParamBytes.length], 1);\n\ttmp.set(addressParamBytes, 2);\n\ttmp.set(addressSeedBytesBigEndian, 2 + addressParamBytes.length);\n\n\treturn normalizeSuiAddress(\n\t\tbytesToHex(blake2b(tmp, { dkLen: 32 })).slice(0, SUI_ADDRESS_LENGTH * 2),\n\t);\n}\n\nexport const MAX_HEADER_LEN_B64 = 279;\nexport const MAX_PADDED_UNSIGNED_JWT_LEN = 64 * 30;\n\nexport function lengthChecks(jwt: string) {\n\tconst [header, payload] = jwt.split('.');\n\t/// Is the header small enough\n\tif (header.length > MAX_HEADER_LEN_B64) {\n\t\tthrow new Error(`Header is too long`);\n\t}\n\n\t/// Is the combined length of (header, payload, SHA2 padding) small enough?\n\t// unsigned_jwt = header + '.' + payload;\n\tconst L = (header.length + 1 + payload.length) * 8;\n\tconst K = (512 + 448 - ((L % 512) + 1)) % 512;\n\n\t// The SHA2 padding is 1 followed by K zeros, followed by the length of the message\n\tconst padded_unsigned_jwt_len = (L + 1 + K + 64) / 8;\n\n\t// The padded unsigned JWT must be less than the max_padded_unsigned_jwt_len\n\tif (padded_unsigned_jwt_len > MAX_PADDED_UNSIGNED_JWT_LEN) {\n\t\tthrow new Error(`JWT is too long`);\n\t}\n}\n\nexport function jwtToAddress(jwt: string, userSalt: string | bigint, legacyAddress: boolean) {\n\t// Explicitly check for people not using typescript and ignoring the major version migration guide\n\tif (legacyAddress === undefined) {\n\t\tthrow new Error('legacyAddress parameter must be specified');\n\t}\n\tlengthChecks(jwt);\n\n\tconst decodedJWT = decodeJwt(jwt);\n\n\treturn computeZkLoginAddress({\n\t\tuserSalt,\n\t\tclaimName: 'sub',\n\t\tclaimValue: decodedJWT.sub,\n\t\taud: decodedJWT.aud,\n\t\tiss: decodedJWT.iss,\n\t\tlegacyAddress,\n\t});\n}\n\nexport interface ComputeZkLoginAddressOptions {\n\tclaimName: string;\n\tclaimValue: string;\n\tuserSalt: string | bigint;\n\tiss: string;\n\taud: string;\n\tlegacyAddress: boolean;\n}\n\nexport function computeZkLoginAddress({\n\tclaimName,\n\tclaimValue,\n\tiss,\n\taud,\n\tuserSalt,\n\tlegacyAddress,\n}: ComputeZkLoginAddressOptions) {\n\t// Explicitly check for people not using typescript and ignoring the major version migration guide\n\tif (legacyAddress === undefined) {\n\t\tthrow new Error('legacyAddress parameter must be specified');\n\t}\n\treturn computeZkLoginAddressFromSeed(\n\t\tgenAddressSeed(userSalt, claimName, claimValue, aud),\n\t\tiss,\n\t\tlegacyAddress,\n\t);\n}\n"],"mappings":";;;;;;;;;AAYA,SAAgB,8BACf,aACA,KACA,eACC;AAED,KAAI,kBAAkB,OACrB,OAAM,IAAI,MAAM,4CAA4C;CAE7D,MAAM,4BAA4B,gBAC/B,iBAAiB,aAAa,GAAG,GACjC,uBAAuB,aAAa,GAAG;CAE1C,MAAM,oBAAoB,IAAI,aAAa,CAAC,OAAO,uBAAuB,IAAI,CAAC;CAC/E,MAAM,MAAM,IAAI,WAAW,IAAI,0BAA0B,SAAS,kBAAkB,OAAO;AAE3F,KAAI,IAAI,CAAC,yBAAyB,QAAQ,CAAC;AAC3C,KAAI,IAAI,CAAC,kBAAkB,OAAO,EAAE,EAAE;AACtC,KAAI,IAAI,mBAAmB,EAAE;AAC7B,KAAI,IAAI,2BAA2B,IAAI,kBAAkB,OAAO;AAEhE,QAAO,oBACN,WAAW,QAAQ,KAAK,EAAE,OAAO,IAAI,CAAC,CAAC,CAAC,MAAM,GAAG,qBAAqB,EAAE,CACxE;;AAGF,MAAa,qBAAqB;AAClC,MAAa,8BAA8B;AAE3C,SAAgB,aAAa,KAAa;CACzC,MAAM,CAAC,QAAQ,WAAW,IAAI,MAAM,IAAI;AAExC,KAAI,OAAO,SAAS,mBACnB,OAAM,IAAI,MAAM,qBAAqB;CAKtC,MAAM,KAAK,OAAO,SAAS,IAAI,QAAQ,UAAU;CACjD,MAAM,KAAK,OAAc,IAAI,MAAO,MAAM;AAM1C,MAHiC,IAAI,IAAI,IAAI,MAAM,IAGrB,4BAC7B,OAAM,IAAI,MAAM,kBAAkB;;AAIpC,SAAgB,aAAa,KAAa,UAA2B,eAAwB;AAE5F,KAAI,kBAAkB,OACrB,OAAM,IAAI,MAAM,4CAA4C;AAE7D,cAAa,IAAI;CAEjB,MAAM,aAAa,UAAU,IAAI;AAEjC,QAAO,sBAAsB;EAC5B;EACA,WAAW;EACX,YAAY,WAAW;EACvB,KAAK,WAAW;EAChB,KAAK,WAAW;EAChB;EACA,CAAC;;AAYH,SAAgB,sBAAsB,EACrC,WACA,YACA,KACA,KACA,UACA,iBACgC;AAEhC,KAAI,kBAAkB,OACrB,OAAM,IAAI,MAAM,4CAA4C;AAE7D,QAAO,8BACN,eAAe,UAAU,WAAW,YAAY,IAAI,EACpD,KACA,cACA"}
|
package/dist/zklogin/bcs.d.mts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _mysten_bcs1244 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_bcs1244.BcsStruct<{
|
|
6
|
+
inputs: _mysten_bcs1244.BcsStruct<{
|
|
7
|
+
proofPoints: _mysten_bcs1244.BcsStruct<{
|
|
8
|
+
a: _mysten_bcs1244.BcsType<string[], Iterable<string> & {
|
|
9
9
|
length: number;
|
|
10
10
|
}, string>;
|
|
11
|
-
b:
|
|
11
|
+
b: _mysten_bcs1244.BcsType<string[][], Iterable<Iterable<string> & {
|
|
12
12
|
length: number;
|
|
13
13
|
}> & {
|
|
14
14
|
length: number;
|
|
15
15
|
}, string>;
|
|
16
|
-
c:
|
|
16
|
+
c: _mysten_bcs1244.BcsType<string[], Iterable<string> & {
|
|
17
17
|
length: number;
|
|
18
18
|
}, string>;
|
|
19
19
|
}, string>;
|
|
20
|
-
issBase64Details:
|
|
21
|
-
value:
|
|
22
|
-
indexMod4:
|
|
20
|
+
issBase64Details: _mysten_bcs1244.BcsStruct<{
|
|
21
|
+
value: _mysten_bcs1244.BcsType<string, string, "string">;
|
|
22
|
+
indexMod4: _mysten_bcs1244.BcsType<number, number, "u8">;
|
|
23
23
|
}, string>;
|
|
24
|
-
headerBase64:
|
|
25
|
-
addressSeed:
|
|
24
|
+
headerBase64: _mysten_bcs1244.BcsType<string, string, "string">;
|
|
25
|
+
addressSeed: _mysten_bcs1244.BcsType<string, string, "string">;
|
|
26
26
|
}, string>;
|
|
27
|
-
maxEpoch:
|
|
28
|
-
userSignature:
|
|
27
|
+
maxEpoch: _mysten_bcs1244.BcsType<string, string | number | bigint, "u64">;
|
|
28
|
+
userSignature: _mysten_bcs1244.BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, "vector<u8>">;
|
|
29
29
|
}, string>;
|
|
30
30
|
type ZkLoginSignature = InferBcsInput<typeof zkLoginSignature>;
|
|
31
31
|
type ZkLoginSignatureInputs = ZkLoginSignature['inputs'];
|
package/dist/zklogin/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ZkLoginSignatureInputs } from "./bcs.mjs";
|
|
2
2
|
import { getZkLoginSignature, parseZkLoginSignature } from "./signature.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { getExtendedEphemeralPublicKey, toBigEndianBytes, toPaddedBigEndianBytes } from "./utils.mjs";
|
|
4
|
+
import { genAddressSeed, hashASCIIStrToField } from "./address-seed.mjs";
|
|
4
5
|
import { ComputeZkLoginAddressOptions, computeZkLoginAddress, computeZkLoginAddressFromSeed, jwtToAddress } from "./address.mjs";
|
|
5
6
|
import { ZkLoginPublicIdentifier, toZkLoginPublicIdentifier } from "./publickey.mjs";
|
|
6
7
|
import { ZkLoginSigner, ZkLoginSignerOptions } from "./signer.mjs";
|
package/dist/zklogin/index.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { genAddressSeed, getExtendedEphemeralPublicKey, hashASCIIStrToField, toBigEndianBytes, toPaddedBigEndianBytes } from "./utils.mjs";
|
|
1
|
+
import { getExtendedEphemeralPublicKey, toBigEndianBytes, toPaddedBigEndianBytes } from "./utils.mjs";
|
|
3
2
|
import { decodeJwt } from "./jwt-utils.mjs";
|
|
4
3
|
import { getZkLoginSignature, parseZkLoginSignature } from "./signature.mjs";
|
|
5
4
|
import { ZkLoginPublicIdentifier, toZkLoginPublicIdentifier } from "./publickey.mjs";
|
|
5
|
+
import { poseidonHash } from "./poseidon.mjs";
|
|
6
|
+
import { genAddressSeed, hashASCIIStrToField } from "./address-seed.mjs";
|
|
6
7
|
import { computeZkLoginAddress, computeZkLoginAddressFromSeed, jwtToAddress } from "./address.mjs";
|
|
7
8
|
import { ZkLoginSigner } from "./signer.mjs";
|
|
8
9
|
import { generateNonce, generateRandomness } from "./nonce.mjs";
|
package/dist/zklogin/nonce.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { poseidonHash } from "./poseidon.mjs";
|
|
2
1
|
import { toPaddedBigEndianBytes } from "./utils.mjs";
|
|
2
|
+
import { poseidonHash } from "./poseidon.mjs";
|
|
3
3
|
import { toHex } from "@mysten/bcs";
|
|
4
4
|
import { randomBytes } from "@noble/hashes/utils.js";
|
|
5
5
|
import { base64urlnopad } from "@scure/base";
|
package/dist/zklogin/utils.d.mts
CHANGED
|
@@ -4,8 +4,6 @@ import { PublicKey } from "../cryptography/publickey.mjs";
|
|
|
4
4
|
declare function toPaddedBigEndianBytes(num: bigint, width: number): Uint8Array;
|
|
5
5
|
declare function toBigEndianBytes(num: bigint, width: number): Uint8Array;
|
|
6
6
|
declare function getExtendedEphemeralPublicKey(publicKey: PublicKey): string;
|
|
7
|
-
declare function hashASCIIStrToField(str: string, maxSize: number): bigint;
|
|
8
|
-
declare function genAddressSeed(salt: string | bigint, name: string, value: string, aud: string, max_name_length?: number, max_value_length?: number, max_aud_length?: number): bigint;
|
|
9
7
|
//#endregion
|
|
10
|
-
export {
|
|
8
|
+
export { getExtendedEphemeralPublicKey, toBigEndianBytes, toPaddedBigEndianBytes };
|
|
11
9
|
//# sourceMappingURL=utils.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.mts","names":[],"sources":["../../src/zklogin/utils.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"utils.d.mts","names":[],"sources":["../../src/zklogin/utils.ts"],"mappings":";;;iBAkBgB,sBAAA,CAAuB,GAAA,UAAa,KAAA,WAAgB,UAAA;AAAA,iBAMpD,gBAAA,CAAiB,GAAA,UAAa,KAAA,WAAgB,UAAA;AAAA,iBAY9C,6BAAA,CAA8B,SAAA,EAAW,SAAA"}
|
package/dist/zklogin/utils.mjs
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import { poseidonHash } from "./poseidon.mjs";
|
|
2
1
|
import { hexToBytes } from "@noble/hashes/utils.js";
|
|
3
2
|
|
|
4
3
|
//#region src/zklogin/utils.ts
|
|
5
|
-
const MAX_KEY_CLAIM_NAME_LENGTH = 32;
|
|
6
|
-
const MAX_KEY_CLAIM_VALUE_LENGTH = 115;
|
|
7
|
-
const MAX_AUD_VALUE_LENGTH = 145;
|
|
8
|
-
const PACK_WIDTH = 248;
|
|
9
4
|
function findFirstNonZeroIndex(bytes) {
|
|
10
5
|
for (let i = 0; i < bytes.length; i++) if (bytes[i] !== 0) return i;
|
|
11
6
|
return -1;
|
|
@@ -22,51 +17,11 @@ function toBigEndianBytes(num, width) {
|
|
|
22
17
|
function getExtendedEphemeralPublicKey(publicKey) {
|
|
23
18
|
return publicKey.toSuiPublicKey();
|
|
24
19
|
}
|
|
25
|
-
/**
|
|
26
|
-
* Splits an array into chunks of size chunk_size. If the array is not evenly
|
|
27
|
-
* divisible by chunk_size, the first chunk will be smaller than chunk_size.
|
|
28
|
-
*
|
|
29
|
-
* E.g., arrayChunk([1, 2, 3, 4, 5], 2) => [[1], [2, 3], [4, 5]]
|
|
30
|
-
*
|
|
31
|
-
* Note: Can be made more efficient by avoiding the reverse() calls.
|
|
32
|
-
*/
|
|
33
|
-
function chunkArray(array, chunk_size) {
|
|
34
|
-
const chunks = Array(Math.ceil(array.length / chunk_size));
|
|
35
|
-
const revArray = array.reverse();
|
|
36
|
-
for (let i = 0; i < chunks.length; i++) chunks[i] = revArray.slice(i * chunk_size, (i + 1) * chunk_size).reverse();
|
|
37
|
-
return chunks.reverse();
|
|
38
|
-
}
|
|
39
|
-
function bytesBEToBigInt(bytes) {
|
|
40
|
-
const hex = bytes.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
41
|
-
if (hex.length === 0) return BigInt(0);
|
|
42
|
-
return BigInt("0x" + hex);
|
|
43
|
-
}
|
|
44
|
-
function hashASCIIStrToField(str, maxSize) {
|
|
45
|
-
if (str.length > maxSize) throw new Error(`String ${str} is longer than ${maxSize} chars`);
|
|
46
|
-
return poseidonHash(chunkArray(str.padEnd(maxSize, String.fromCharCode(0)).split("").map((c) => c.charCodeAt(0)), PACK_WIDTH / 8).map((chunk) => bytesBEToBigInt(chunk)));
|
|
47
|
-
}
|
|
48
|
-
function assertNoJsonEscape(value, label) {
|
|
49
|
-
for (let i = 0; i < value.length; i++) {
|
|
50
|
-
const c = value.charCodeAt(i);
|
|
51
|
-
if (c < 32 || c === 34 || c === 92) throw new Error(`zkLogin ${label} contains a JSON-escaped character (code ${c}); the circuit hashes raw JWT bytes, so claim values with escapes are not supported`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function genAddressSeed(salt, name, value, aud, max_name_length = MAX_KEY_CLAIM_NAME_LENGTH, max_value_length = MAX_KEY_CLAIM_VALUE_LENGTH, max_aud_length = MAX_AUD_VALUE_LENGTH) {
|
|
55
|
-
assertNoJsonEscape(name, "key claim name");
|
|
56
|
-
assertNoJsonEscape(value, "key claim value");
|
|
57
|
-
assertNoJsonEscape(aud, "aud");
|
|
58
|
-
return poseidonHash([
|
|
59
|
-
hashASCIIStrToField(name, max_name_length),
|
|
60
|
-
hashASCIIStrToField(value, max_value_length),
|
|
61
|
-
hashASCIIStrToField(aud, max_aud_length),
|
|
62
|
-
poseidonHash([BigInt(salt)])
|
|
63
|
-
]);
|
|
64
|
-
}
|
|
65
20
|
function normalizeZkLoginIssuer(iss) {
|
|
66
21
|
if (iss === "accounts.google.com") return "https://accounts.google.com";
|
|
67
22
|
return iss;
|
|
68
23
|
}
|
|
69
24
|
|
|
70
25
|
//#endregion
|
|
71
|
-
export {
|
|
26
|
+
export { getExtendedEphemeralPublicKey, normalizeZkLoginIssuer, toBigEndianBytes, toPaddedBigEndianBytes };
|
|
72
27
|
//# sourceMappingURL=utils.mjs.map
|
|
@@ -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.36.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",
|
package/src/bcs/bcs.ts
CHANGED
|
@@ -341,14 +341,18 @@ export const CompressedSignature = bcs.enum('CompressedSignature', {
|
|
|
341
341
|
Secp256r1: bcs.bytes(64),
|
|
342
342
|
ZkLogin: bcs.byteVector(),
|
|
343
343
|
Passkey: bcs.byteVector(),
|
|
344
|
+
MLDSA65: bcs.bytes(3309),
|
|
344
345
|
});
|
|
345
346
|
|
|
347
|
+
// Variant order must match Sui's `PublicKey` enum, which validators hash to
|
|
348
|
+
// derive a multisig address.
|
|
346
349
|
export const PublicKey = bcs.enum('PublicKey', {
|
|
347
350
|
ED25519: bcs.bytes(32),
|
|
348
351
|
Secp256k1: bcs.bytes(33),
|
|
349
352
|
Secp256r1: bcs.bytes(33),
|
|
350
353
|
ZkLogin: bcs.byteVector(),
|
|
351
354
|
Passkey: bcs.bytes(33),
|
|
355
|
+
MLDSA65: bcs.bytes(1952),
|
|
352
356
|
});
|
|
353
357
|
|
|
354
358
|
export const MultiSigPkMap = bcs.struct('MultiSigPkMap', {
|
|
@@ -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
|
+
}
|