@mysten/sui 1.19.0 → 1.20.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 +6 -0
- package/dist/cjs/keypairs/passkey/index.d.ts +1 -1
- package/dist/cjs/keypairs/passkey/index.js +2 -1
- package/dist/cjs/keypairs/passkey/index.js.map +2 -2
- package/dist/cjs/keypairs/passkey/keypair.d.ts +57 -3
- package/dist/cjs/keypairs/passkey/keypair.js +87 -4
- package/dist/cjs/keypairs/passkey/keypair.js.map +2 -2
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/keypairs/passkey/index.d.ts +1 -1
- package/dist/esm/keypairs/passkey/index.js +3 -2
- package/dist/esm/keypairs/passkey/index.js.map +2 -2
- package/dist/esm/keypairs/passkey/keypair.d.ts +57 -3
- package/dist/esm/keypairs/passkey/keypair.js +87 -4
- package/dist/esm/keypairs/passkey/keypair.js.map +2 -2
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/keypairs/passkey/index.ts +1 -1
- package/src/keypairs/passkey/keypair.ts +104 -4
- package/src/version.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { PasskeyKeypair, BrowserPasskeyProvider } from './keypair.js';
|
|
1
|
+
export { PasskeyKeypair, BrowserPasskeyProvider, findCommonPublicKey } from './keypair.js';
|
|
2
2
|
export type { PasskeyProvider, BrowserPasswordProviderOptions } from './keypair.js';
|
|
3
3
|
export { PasskeyPublicKey } from './publickey.js';
|
|
@@ -20,7 +20,8 @@ var passkey_exports = {};
|
|
|
20
20
|
__export(passkey_exports, {
|
|
21
21
|
BrowserPasskeyProvider: () => import_keypair.BrowserPasskeyProvider,
|
|
22
22
|
PasskeyKeypair: () => import_keypair.PasskeyKeypair,
|
|
23
|
-
PasskeyPublicKey: () => import_publickey.PasskeyPublicKey
|
|
23
|
+
PasskeyPublicKey: () => import_publickey.PasskeyPublicKey,
|
|
24
|
+
findCommonPublicKey: () => import_keypair.findCommonPublicKey
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(passkey_exports);
|
|
26
27
|
var import_keypair = require("./keypair.js");
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/keypairs/passkey/index.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nexport { PasskeyKeypair, BrowserPasskeyProvider } from './keypair.js';\nexport type { PasskeyProvider, BrowserPasswordProviderOptions } from './keypair.js';\nexport { PasskeyPublicKey } from './publickey.js';\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nexport { PasskeyKeypair, BrowserPasskeyProvider, findCommonPublicKey } from './keypair.js';\nexport type { PasskeyProvider, BrowserPasswordProviderOptions } from './keypair.js';\nexport { PasskeyPublicKey } from './publickey.js';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,qBAA4E;AAE5E,uBAAiC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -30,15 +30,27 @@ export declare class PasskeyKeypair extends Signer {
|
|
|
30
30
|
*/
|
|
31
31
|
getKeyScheme(): SignatureScheme;
|
|
32
32
|
/**
|
|
33
|
-
* Creates an instance of Passkey signer.
|
|
34
|
-
* For example:
|
|
33
|
+
* Creates an instance of Passkey signer. If no passkey wallet had created before,
|
|
34
|
+
* use `getPasskeyInstance`. For example:
|
|
35
35
|
* ```
|
|
36
|
-
*
|
|
36
|
+
* let provider = new BrowserPasskeyProvider('Sui Passkey Example',{
|
|
37
|
+
* rpName: 'Sui Passkey Example',
|
|
38
|
+
* rpId: window.location.hostname,
|
|
39
|
+
* } as BrowserPasswordProviderOptions);
|
|
40
|
+
* const signer = await PasskeyKeypair.getPasskeyInstance(provider);
|
|
37
41
|
* ```
|
|
42
|
+
*
|
|
43
|
+
* If there are existing passkey wallet, use `signAndRecover` to identify the correct
|
|
44
|
+
* public key and then initialize the instance. See usage in `signAndRecover`.
|
|
38
45
|
*/
|
|
39
46
|
constructor(publicKey: Uint8Array, provider: PasskeyProvider);
|
|
40
47
|
/**
|
|
41
48
|
* Creates an instance of Passkey signer invoking the passkey from navigator.
|
|
49
|
+
* Note that this will invoke the passkey device to create a fresh credential.
|
|
50
|
+
* Should only be called if passkey wallet is created for the first time.
|
|
51
|
+
*
|
|
52
|
+
* @param provider - the passkey provider.
|
|
53
|
+
* @returns the passkey instance.
|
|
42
54
|
*/
|
|
43
55
|
static getPasskeyInstance(provider: PasskeyProvider): Promise<PasskeyKeypair>;
|
|
44
56
|
/**
|
|
@@ -55,5 +67,47 @@ export declare class PasskeyKeypair extends Signer {
|
|
|
55
67
|
* digest of the intent message, then serialize it with the passkey flag.
|
|
56
68
|
*/
|
|
57
69
|
signWithIntent(bytes: Uint8Array, intent: IntentScope): Promise<SignatureWithBytes>;
|
|
70
|
+
/**
|
|
71
|
+
* Given a message, asks the passkey device to sign it and return all (up to 4) possible public keys.
|
|
72
|
+
* See: https://bitcoin.stackexchange.com/questions/81232/how-is-public-key-extracted-from-message-digital-signature-address
|
|
73
|
+
*
|
|
74
|
+
* This is useful if the user previously created passkey wallet with the origin, but the wallet session
|
|
75
|
+
* does not have the public key / address. By calling this method twice with two different messages, the
|
|
76
|
+
* wallet can compare the returned public keys and uniquely identify the previously created passkey wallet
|
|
77
|
+
* using `findCommonPublicKey`.
|
|
78
|
+
*
|
|
79
|
+
* Alternatively, one call can be made and all possible public keys should be checked onchain to see if
|
|
80
|
+
* there is any assets.
|
|
81
|
+
*
|
|
82
|
+
* Once the correct public key is identified, a passkey instance can then be initialized with this public key.
|
|
83
|
+
*
|
|
84
|
+
* Example usage to recover wallet with two signing calls:
|
|
85
|
+
* ```
|
|
86
|
+
* let provider = new BrowserPasskeyProvider('Sui Passkey Example',{
|
|
87
|
+
* rpName: 'Sui Passkey Example',
|
|
88
|
+
* rpId: window.location.hostname,
|
|
89
|
+
* } as BrowserPasswordProviderOptions);
|
|
90
|
+
* const testMessage = new TextEncoder().encode('Hello world!');
|
|
91
|
+
* const possiblePks = await PasskeyKeypair.signAndRecover(provider, testMessage);
|
|
92
|
+
* const testMessage2 = new TextEncoder().encode('Hello world 2!');
|
|
93
|
+
* const possiblePks2 = await PasskeyKeypair.signAndRecover(provider, testMessage2);
|
|
94
|
+
* const commonPk = findCommonPublicKey(possiblePks, possiblePks2);
|
|
95
|
+
* const signer = new PasskeyKeypair(provider, commonPk.toRawBytes());
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @param provider - the passkey provider.
|
|
99
|
+
* @param message - the message to sign.
|
|
100
|
+
* @returns all possible public keys.
|
|
101
|
+
*/
|
|
102
|
+
static signAndRecover(provider: PasskeyProvider, message: Uint8Array): Promise<PublicKey[]>;
|
|
58
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Finds the unique public key that exists in both arrays, throws error if the common
|
|
106
|
+
* pubkey does not equal to one.
|
|
107
|
+
*
|
|
108
|
+
* @param arr1 - The first pubkeys array.
|
|
109
|
+
* @param arr2 - The second pubkeys array.
|
|
110
|
+
* @returns The only common pubkey in both arrays.
|
|
111
|
+
*/
|
|
112
|
+
export declare function findCommonPublicKey(arr1: PublicKey[], arr2: PublicKey[]): PublicKey;
|
|
59
113
|
export {};
|
|
@@ -26,12 +26,14 @@ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "
|
|
|
26
26
|
var keypair_exports = {};
|
|
27
27
|
__export(keypair_exports, {
|
|
28
28
|
BrowserPasskeyProvider: () => BrowserPasskeyProvider,
|
|
29
|
-
PasskeyKeypair: () => PasskeyKeypair
|
|
29
|
+
PasskeyKeypair: () => PasskeyKeypair,
|
|
30
|
+
findCommonPublicKey: () => findCommonPublicKey
|
|
30
31
|
});
|
|
31
32
|
module.exports = __toCommonJS(keypair_exports);
|
|
32
33
|
var import_bcs = require("@mysten/bcs");
|
|
33
34
|
var import_p256 = require("@noble/curves/p256");
|
|
34
35
|
var import_blake2b = require("@noble/hashes/blake2b");
|
|
36
|
+
var import_sha256 = require("@noble/hashes/sha256");
|
|
35
37
|
var import_utils = require("@noble/hashes/utils");
|
|
36
38
|
var import_bcs2 = require("../../bcs/bcs.js");
|
|
37
39
|
var import_cryptography = require("../../cryptography/index.js");
|
|
@@ -91,11 +93,18 @@ class PasskeyKeypair extends import_cryptography.Signer {
|
|
|
91
93
|
return "Passkey";
|
|
92
94
|
}
|
|
93
95
|
/**
|
|
94
|
-
* Creates an instance of Passkey signer.
|
|
95
|
-
* For example:
|
|
96
|
+
* Creates an instance of Passkey signer. If no passkey wallet had created before,
|
|
97
|
+
* use `getPasskeyInstance`. For example:
|
|
96
98
|
* ```
|
|
97
|
-
*
|
|
99
|
+
* let provider = new BrowserPasskeyProvider('Sui Passkey Example',{
|
|
100
|
+
* rpName: 'Sui Passkey Example',
|
|
101
|
+
* rpId: window.location.hostname,
|
|
102
|
+
* } as BrowserPasswordProviderOptions);
|
|
103
|
+
* const signer = await PasskeyKeypair.getPasskeyInstance(provider);
|
|
98
104
|
* ```
|
|
105
|
+
*
|
|
106
|
+
* If there are existing passkey wallet, use `signAndRecover` to identify the correct
|
|
107
|
+
* public key and then initialize the instance. See usage in `signAndRecover`.
|
|
99
108
|
*/
|
|
100
109
|
constructor(publicKey, provider) {
|
|
101
110
|
super();
|
|
@@ -104,6 +113,11 @@ class PasskeyKeypair extends import_cryptography.Signer {
|
|
|
104
113
|
}
|
|
105
114
|
/**
|
|
106
115
|
* Creates an instance of Passkey signer invoking the passkey from navigator.
|
|
116
|
+
* Note that this will invoke the passkey device to create a fresh credential.
|
|
117
|
+
* Should only be called if passkey wallet is created for the first time.
|
|
118
|
+
*
|
|
119
|
+
* @param provider - the passkey provider.
|
|
120
|
+
* @returns the passkey instance.
|
|
107
121
|
*/
|
|
108
122
|
static async getPasskeyInstance(provider) {
|
|
109
123
|
const credential = await provider.create();
|
|
@@ -164,5 +178,74 @@ class PasskeyKeypair extends import_cryptography.Signer {
|
|
|
164
178
|
bytes: (0, import_bcs.toBase64)(bytes)
|
|
165
179
|
};
|
|
166
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Given a message, asks the passkey device to sign it and return all (up to 4) possible public keys.
|
|
183
|
+
* See: https://bitcoin.stackexchange.com/questions/81232/how-is-public-key-extracted-from-message-digital-signature-address
|
|
184
|
+
*
|
|
185
|
+
* This is useful if the user previously created passkey wallet with the origin, but the wallet session
|
|
186
|
+
* does not have the public key / address. By calling this method twice with two different messages, the
|
|
187
|
+
* wallet can compare the returned public keys and uniquely identify the previously created passkey wallet
|
|
188
|
+
* using `findCommonPublicKey`.
|
|
189
|
+
*
|
|
190
|
+
* Alternatively, one call can be made and all possible public keys should be checked onchain to see if
|
|
191
|
+
* there is any assets.
|
|
192
|
+
*
|
|
193
|
+
* Once the correct public key is identified, a passkey instance can then be initialized with this public key.
|
|
194
|
+
*
|
|
195
|
+
* Example usage to recover wallet with two signing calls:
|
|
196
|
+
* ```
|
|
197
|
+
* let provider = new BrowserPasskeyProvider('Sui Passkey Example',{
|
|
198
|
+
* rpName: 'Sui Passkey Example',
|
|
199
|
+
* rpId: window.location.hostname,
|
|
200
|
+
* } as BrowserPasswordProviderOptions);
|
|
201
|
+
* const testMessage = new TextEncoder().encode('Hello world!');
|
|
202
|
+
* const possiblePks = await PasskeyKeypair.signAndRecover(provider, testMessage);
|
|
203
|
+
* const testMessage2 = new TextEncoder().encode('Hello world 2!');
|
|
204
|
+
* const possiblePks2 = await PasskeyKeypair.signAndRecover(provider, testMessage2);
|
|
205
|
+
* const commonPk = findCommonPublicKey(possiblePks, possiblePks2);
|
|
206
|
+
* const signer = new PasskeyKeypair(provider, commonPk.toRawBytes());
|
|
207
|
+
* ```
|
|
208
|
+
*
|
|
209
|
+
* @param provider - the passkey provider.
|
|
210
|
+
* @param message - the message to sign.
|
|
211
|
+
* @returns all possible public keys.
|
|
212
|
+
*/
|
|
213
|
+
static async signAndRecover(provider, message) {
|
|
214
|
+
const credential = await provider.get(message);
|
|
215
|
+
const fullMessage = messageFromAssertionResponse(credential.response);
|
|
216
|
+
const sig = import_p256.secp256r1.Signature.fromDER(new Uint8Array(credential.response.signature));
|
|
217
|
+
const res = [];
|
|
218
|
+
for (let i = 0; i < 4; i++) {
|
|
219
|
+
const s = sig.addRecoveryBit(i);
|
|
220
|
+
try {
|
|
221
|
+
const pubkey = s.recoverPublicKey((0, import_sha256.sha256)(fullMessage));
|
|
222
|
+
const pk = new import_publickey.PasskeyPublicKey(pubkey.toRawBytes(true));
|
|
223
|
+
res.push(pk);
|
|
224
|
+
} catch {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return res;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function findCommonPublicKey(arr1, arr2) {
|
|
232
|
+
const matchingPubkeys = [];
|
|
233
|
+
for (const pubkey1 of arr1) {
|
|
234
|
+
for (const pubkey2 of arr2) {
|
|
235
|
+
if (pubkey1.equals(pubkey2)) {
|
|
236
|
+
matchingPubkeys.push(pubkey1);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (matchingPubkeys.length !== 1) {
|
|
241
|
+
throw new Error("No unique public key found");
|
|
242
|
+
}
|
|
243
|
+
return matchingPubkeys[0];
|
|
244
|
+
}
|
|
245
|
+
function messageFromAssertionResponse(response) {
|
|
246
|
+
const authenticatorData = new Uint8Array(response.authenticatorData);
|
|
247
|
+
const clientDataJSON = new Uint8Array(response.clientDataJSON);
|
|
248
|
+
const clientDataJSONDigest = (0, import_sha256.sha256)(clientDataJSON);
|
|
249
|
+
return new Uint8Array([...authenticatorData, ...clientDataJSONDigest]);
|
|
167
250
|
}
|
|
168
251
|
//# sourceMappingURL=keypair.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/keypairs/passkey/keypair.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { toBase64 } from '@mysten/bcs';\nimport { secp256r1 } from '@noble/curves/p256';\nimport { blake2b } from '@noble/hashes/blake2b';\nimport { randomBytes } from '@noble/hashes/utils';\nimport type {\n\tAuthenticationCredential,\n\tRegistrationCredential,\n} from '@simplewebauthn/typescript-types';\n\nimport { PasskeyAuthenticator } from '../../bcs/bcs.js';\nimport type { IntentScope, SignatureWithBytes } from '../../cryptography/index.js';\nimport { messageWithIntent, SIGNATURE_SCHEME_TO_FLAG, Signer } from '../../cryptography/index.js';\nimport type { PublicKey } from '../../cryptography/publickey.js';\nimport type { SignatureScheme } from '../../cryptography/signature-scheme.js';\nimport {\n\tparseDerSPKI,\n\tPASSKEY_PUBLIC_KEY_SIZE,\n\tPASSKEY_SIGNATURE_SIZE,\n\tPasskeyPublicKey,\n} from './publickey.js';\n\ntype DeepPartialConfigKeys = 'rp' | 'user' | 'authenticatorSelection';\n\ntype DeepPartial<T> = T extends object\n\t? {\n\t\t\t[P in keyof T]?: DeepPartial<T[P]>;\n\t\t}\n\t: T;\n\nexport type BrowserPasswordProviderOptions = Pick<\n\tDeepPartial<PublicKeyCredentialCreationOptions>,\n\tDeepPartialConfigKeys\n> &\n\tOmit<\n\t\tPartial<PublicKeyCredentialCreationOptions>,\n\t\tDeepPartialConfigKeys | 'pubKeyCredParams' | 'challenge'\n\t>;\n\nexport interface PasskeyProvider {\n\tcreate(): Promise<RegistrationCredential>;\n\tget(challenge: Uint8Array): Promise<AuthenticationCredential>;\n}\n\n// Default browser implementation\nexport class BrowserPasskeyProvider implements PasskeyProvider {\n\t#name: string;\n\t#options: BrowserPasswordProviderOptions;\n\n\tconstructor(name: string, options: BrowserPasswordProviderOptions) {\n\t\tthis.#name = name;\n\t\tthis.#options = options;\n\t}\n\n\tasync create(): Promise<RegistrationCredential> {\n\t\treturn (await navigator.credentials.create({\n\t\t\tpublicKey: {\n\t\t\t\ttimeout: this.#options.timeout ?? 60000,\n\t\t\t\t...this.#options,\n\t\t\t\trp: {\n\t\t\t\t\tname: this.#name,\n\t\t\t\t\t...this.#options.rp,\n\t\t\t\t},\n\t\t\t\tuser: {\n\t\t\t\t\tname: this.#name,\n\t\t\t\t\tdisplayName: this.#name,\n\t\t\t\t\t...this.#options.user,\n\t\t\t\t\tid: randomBytes(10),\n\t\t\t\t},\n\t\t\t\tchallenge: new TextEncoder().encode('Create passkey wallet on Sui'),\n\t\t\t\tpubKeyCredParams: [{ alg: -7, type: 'public-key' }],\n\t\t\t\tauthenticatorSelection: {\n\t\t\t\t\tauthenticatorAttachment: 'cross-platform',\n\t\t\t\t\tresidentKey: 'required',\n\t\t\t\t\trequireResidentKey: true,\n\t\t\t\t\tuserVerification: 'required',\n\t\t\t\t\t...this.#options.authenticatorSelection,\n\t\t\t\t},\n\t\t\t},\n\t\t})) as RegistrationCredential;\n\t}\n\n\tasync get(challenge: Uint8Array): Promise<AuthenticationCredential> {\n\t\treturn (await navigator.credentials.get({\n\t\t\tpublicKey: {\n\t\t\t\tchallenge,\n\t\t\t\tuserVerification: this.#options.authenticatorSelection?.userVerification || 'required',\n\t\t\t\ttimeout: this.#options.timeout ?? 60000,\n\t\t\t},\n\t\t})) as AuthenticationCredential;\n\t}\n}\n\n/**\n * @experimental\n * A passkey signer used for signing transactions. This is a client side implementation for [SIP-9](https://github.com/sui-foundation/sips/blob/main/sips/sip-9.md).\n */\nexport class PasskeyKeypair extends Signer {\n\tprivate publicKey: Uint8Array;\n\tprivate provider: PasskeyProvider;\n\n\t/**\n\t * Get the key scheme of passkey,\n\t */\n\tgetKeyScheme(): SignatureScheme {\n\t\treturn 'Passkey';\n\t}\n\n\t/**\n\t * Creates an instance of Passkey signer. It's expected to call the static `getPasskeyInstance` method to create an instance.\n\t * For example:\n\t * ```\n\t * const signer = await PasskeyKeypair.getPasskeyInstance();\n\t * ```\n\t */\n\tconstructor(publicKey: Uint8Array, provider: PasskeyProvider) {\n\t\tsuper();\n\t\tthis.publicKey = publicKey;\n\t\tthis.provider = provider;\n\t}\n\n\t/**\n\t * Creates an instance of Passkey signer invoking the passkey from navigator.\n\t */\n\tstatic async getPasskeyInstance(provider: PasskeyProvider): Promise<PasskeyKeypair> {\n\t\t// create a passkey secp256r1 with the provider.\n\t\tconst credential = await provider.create();\n\n\t\tif (!credential.response.getPublicKey()) {\n\t\t\tthrow new Error('Invalid credential create response');\n\t\t} else {\n\t\t\tconst derSPKI = credential.response.getPublicKey()!;\n\t\t\tconst pubkeyUncompressed = parseDerSPKI(new Uint8Array(derSPKI));\n\t\t\tconst pubkey = secp256r1.ProjectivePoint.fromHex(pubkeyUncompressed);\n\t\t\tconst pubkeyCompressed = pubkey.toRawBytes(true);\n\t\t\treturn new PasskeyKeypair(pubkeyCompressed, provider);\n\t\t}\n\t}\n\n\t/**\n\t * Return the public key for this passkey.\n\t */\n\tgetPublicKey(): PublicKey {\n\t\treturn new PasskeyPublicKey(this.publicKey);\n\t}\n\n\t/**\n\t * Return the signature for the provided data (i.e. blake2b(intent_message)).\n\t * This is sent to passkey as the challenge field.\n\t */\n\tasync sign(data: Uint8Array) {\n\t\t// sendss the passkey to sign over challenge as the data.\n\t\tconst credential = await this.provider.get(data);\n\n\t\t// parse authenticatorData (as bytes), clientDataJSON (decoded as string).\n\t\tconst authenticatorData = new Uint8Array(credential.response.authenticatorData);\n\t\tconst clientDataJSON = new Uint8Array(credential.response.clientDataJSON); // response.clientDataJSON is already UTF-8 encoded JSON\n\t\tconst decoder = new TextDecoder();\n\t\tconst clientDataJSONString: string = decoder.decode(clientDataJSON);\n\n\t\t// parse the signature from DER format, normalize and convert to compressed format (33 bytes).\n\t\tconst sig = secp256r1.Signature.fromDER(new Uint8Array(credential.response.signature));\n\t\tconst normalized = sig.normalizeS().toCompactRawBytes();\n\n\t\tif (\n\t\t\tnormalized.length !== PASSKEY_SIGNATURE_SIZE ||\n\t\t\tthis.publicKey.length !== PASSKEY_PUBLIC_KEY_SIZE\n\t\t) {\n\t\t\tthrow new Error('Invalid signature or public key length');\n\t\t}\n\n\t\t// construct userSignature as flag || sig || pubkey for the secp256r1 signature.\n\t\tconst arr = new Uint8Array(1 + normalized.length + this.publicKey.length);\n\t\tarr.set([SIGNATURE_SCHEME_TO_FLAG['Secp256r1']]);\n\t\tarr.set(normalized, 1);\n\t\tarr.set(this.publicKey, 1 + normalized.length);\n\n\t\t// serialize all fields into a passkey signature according to https://github.com/sui-foundation/sips/blob/main/sips/sip-9.md#signature-encoding\n\t\treturn PasskeyAuthenticator.serialize({\n\t\t\tauthenticatorData: authenticatorData,\n\t\t\tclientDataJson: clientDataJSONString,\n\t\t\tuserSignature: arr,\n\t\t}).toBytes();\n\t}\n\n\t/**\n\t * This overrides the base class implementation that accepts the raw bytes and signs its\n\t * digest of the intent message, then serialize it with the passkey flag.\n\t */\n\tasync signWithIntent(bytes: Uint8Array, intent: IntentScope): Promise<SignatureWithBytes> {\n\t\t// prepend it into an intent message and computes the digest.\n\t\tconst intentMessage = messageWithIntent(intent, bytes);\n\t\tconst digest = blake2b(intentMessage, { dkLen: 32 });\n\n\t\t// sign the digest.\n\t\tconst signature = await this.sign(digest);\n\n\t\t// prepend with the passkey flag.\n\t\tconst serializedSignature = new Uint8Array(1 + signature.length);\n\t\tserializedSignature.set([SIGNATURE_SCHEME_TO_FLAG[this.getKeyScheme()]]);\n\t\tserializedSignature.set(signature, 1);\n\t\treturn {\n\t\t\tsignature: toBase64(serializedSignature),\n\t\t\tbytes: toBase64(bytes),\n\t\t};\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAyB;AACzB,kBAA0B;AAC1B,qBAAwB;AACxB,mBAA4B;AAM5B,IAAAA,cAAqC;AAErC,0BAAoE;AAGpE,uBAKO;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { toBase64 } from '@mysten/bcs';\nimport { secp256r1 } from '@noble/curves/p256';\nimport { blake2b } from '@noble/hashes/blake2b';\nimport { sha256 } from '@noble/hashes/sha256';\nimport { randomBytes } from '@noble/hashes/utils';\nimport type {\n\tAuthenticationCredential,\n\tRegistrationCredential,\n} from '@simplewebauthn/typescript-types';\n\nimport { PasskeyAuthenticator } from '../../bcs/bcs.js';\nimport type { IntentScope, SignatureWithBytes } from '../../cryptography/index.js';\nimport { messageWithIntent, SIGNATURE_SCHEME_TO_FLAG, Signer } from '../../cryptography/index.js';\nimport type { PublicKey } from '../../cryptography/publickey.js';\nimport type { SignatureScheme } from '../../cryptography/signature-scheme.js';\nimport {\n\tparseDerSPKI,\n\tPASSKEY_PUBLIC_KEY_SIZE,\n\tPASSKEY_SIGNATURE_SIZE,\n\tPasskeyPublicKey,\n} from './publickey.js';\n\ntype DeepPartialConfigKeys = 'rp' | 'user' | 'authenticatorSelection';\n\ntype DeepPartial<T> = T extends object\n\t? {\n\t\t\t[P in keyof T]?: DeepPartial<T[P]>;\n\t\t}\n\t: T;\n\nexport type BrowserPasswordProviderOptions = Pick<\n\tDeepPartial<PublicKeyCredentialCreationOptions>,\n\tDeepPartialConfigKeys\n> &\n\tOmit<\n\t\tPartial<PublicKeyCredentialCreationOptions>,\n\t\tDeepPartialConfigKeys | 'pubKeyCredParams' | 'challenge'\n\t>;\n\nexport interface PasskeyProvider {\n\tcreate(): Promise<RegistrationCredential>;\n\tget(challenge: Uint8Array): Promise<AuthenticationCredential>;\n}\n\n// Default browser implementation\nexport class BrowserPasskeyProvider implements PasskeyProvider {\n\t#name: string;\n\t#options: BrowserPasswordProviderOptions;\n\n\tconstructor(name: string, options: BrowserPasswordProviderOptions) {\n\t\tthis.#name = name;\n\t\tthis.#options = options;\n\t}\n\n\tasync create(): Promise<RegistrationCredential> {\n\t\treturn (await navigator.credentials.create({\n\t\t\tpublicKey: {\n\t\t\t\ttimeout: this.#options.timeout ?? 60000,\n\t\t\t\t...this.#options,\n\t\t\t\trp: {\n\t\t\t\t\tname: this.#name,\n\t\t\t\t\t...this.#options.rp,\n\t\t\t\t},\n\t\t\t\tuser: {\n\t\t\t\t\tname: this.#name,\n\t\t\t\t\tdisplayName: this.#name,\n\t\t\t\t\t...this.#options.user,\n\t\t\t\t\tid: randomBytes(10),\n\t\t\t\t},\n\t\t\t\tchallenge: new TextEncoder().encode('Create passkey wallet on Sui'),\n\t\t\t\tpubKeyCredParams: [{ alg: -7, type: 'public-key' }],\n\t\t\t\tauthenticatorSelection: {\n\t\t\t\t\tauthenticatorAttachment: 'cross-platform',\n\t\t\t\t\tresidentKey: 'required',\n\t\t\t\t\trequireResidentKey: true,\n\t\t\t\t\tuserVerification: 'required',\n\t\t\t\t\t...this.#options.authenticatorSelection,\n\t\t\t\t},\n\t\t\t},\n\t\t})) as RegistrationCredential;\n\t}\n\n\tasync get(challenge: Uint8Array): Promise<AuthenticationCredential> {\n\t\treturn (await navigator.credentials.get({\n\t\t\tpublicKey: {\n\t\t\t\tchallenge,\n\t\t\t\tuserVerification: this.#options.authenticatorSelection?.userVerification || 'required',\n\t\t\t\ttimeout: this.#options.timeout ?? 60000,\n\t\t\t},\n\t\t})) as AuthenticationCredential;\n\t}\n}\n\n/**\n * @experimental\n * A passkey signer used for signing transactions. This is a client side implementation for [SIP-9](https://github.com/sui-foundation/sips/blob/main/sips/sip-9.md).\n */\nexport class PasskeyKeypair extends Signer {\n\tprivate publicKey: Uint8Array;\n\tprivate provider: PasskeyProvider;\n\n\t/**\n\t * Get the key scheme of passkey,\n\t */\n\tgetKeyScheme(): SignatureScheme {\n\t\treturn 'Passkey';\n\t}\n\n\t/**\n\t * Creates an instance of Passkey signer. If no passkey wallet had created before,\n\t * use `getPasskeyInstance`. For example:\n\t * ```\n\t * let provider = new BrowserPasskeyProvider('Sui Passkey Example',{\n\t * \t rpName: 'Sui Passkey Example',\n\t * \t rpId: window.location.hostname,\n\t * } as BrowserPasswordProviderOptions);\n\t * const signer = await PasskeyKeypair.getPasskeyInstance(provider);\n\t * ```\n\t *\n\t * If there are existing passkey wallet, use `signAndRecover` to identify the correct\n\t * public key and then initialize the instance. See usage in `signAndRecover`.\n\t */\n\tconstructor(publicKey: Uint8Array, provider: PasskeyProvider) {\n\t\tsuper();\n\t\tthis.publicKey = publicKey;\n\t\tthis.provider = provider;\n\t}\n\n\t/**\n\t * Creates an instance of Passkey signer invoking the passkey from navigator.\n\t * Note that this will invoke the passkey device to create a fresh credential.\n\t * Should only be called if passkey wallet is created for the first time.\n\t *\n\t * @param provider - the passkey provider.\n\t * @returns the passkey instance.\n\t */\n\tstatic async getPasskeyInstance(provider: PasskeyProvider): Promise<PasskeyKeypair> {\n\t\t// create a passkey secp256r1 with the provider.\n\t\tconst credential = await provider.create();\n\n\t\tif (!credential.response.getPublicKey()) {\n\t\t\tthrow new Error('Invalid credential create response');\n\t\t} else {\n\t\t\tconst derSPKI = credential.response.getPublicKey()!;\n\t\t\tconst pubkeyUncompressed = parseDerSPKI(new Uint8Array(derSPKI));\n\t\t\tconst pubkey = secp256r1.ProjectivePoint.fromHex(pubkeyUncompressed);\n\t\t\tconst pubkeyCompressed = pubkey.toRawBytes(true);\n\t\t\treturn new PasskeyKeypair(pubkeyCompressed, provider);\n\t\t}\n\t}\n\n\t/**\n\t * Return the public key for this passkey.\n\t */\n\tgetPublicKey(): PublicKey {\n\t\treturn new PasskeyPublicKey(this.publicKey);\n\t}\n\n\t/**\n\t * Return the signature for the provided data (i.e. blake2b(intent_message)).\n\t * This is sent to passkey as the challenge field.\n\t */\n\tasync sign(data: Uint8Array) {\n\t\t// asks the passkey to sign over challenge as the data.\n\t\tconst credential = await this.provider.get(data);\n\n\t\t// parse authenticatorData (as bytes), clientDataJSON (decoded as string).\n\t\tconst authenticatorData = new Uint8Array(credential.response.authenticatorData);\n\t\tconst clientDataJSON = new Uint8Array(credential.response.clientDataJSON); // response.clientDataJSON is already UTF-8 encoded JSON\n\t\tconst decoder = new TextDecoder();\n\t\tconst clientDataJSONString: string = decoder.decode(clientDataJSON);\n\n\t\t// parse the signature from DER format, normalize and convert to compressed format (33 bytes).\n\t\tconst sig = secp256r1.Signature.fromDER(new Uint8Array(credential.response.signature));\n\t\tconst normalized = sig.normalizeS().toCompactRawBytes();\n\n\t\tif (\n\t\t\tnormalized.length !== PASSKEY_SIGNATURE_SIZE ||\n\t\t\tthis.publicKey.length !== PASSKEY_PUBLIC_KEY_SIZE\n\t\t) {\n\t\t\tthrow new Error('Invalid signature or public key length');\n\t\t}\n\n\t\t// construct userSignature as flag || sig || pubkey for the secp256r1 signature.\n\t\tconst arr = new Uint8Array(1 + normalized.length + this.publicKey.length);\n\t\tarr.set([SIGNATURE_SCHEME_TO_FLAG['Secp256r1']]);\n\t\tarr.set(normalized, 1);\n\t\tarr.set(this.publicKey, 1 + normalized.length);\n\n\t\t// serialize all fields into a passkey signature according to https://github.com/sui-foundation/sips/blob/main/sips/sip-9.md#signature-encoding\n\t\treturn PasskeyAuthenticator.serialize({\n\t\t\tauthenticatorData: authenticatorData,\n\t\t\tclientDataJson: clientDataJSONString,\n\t\t\tuserSignature: arr,\n\t\t}).toBytes();\n\t}\n\n\t/**\n\t * This overrides the base class implementation that accepts the raw bytes and signs its\n\t * digest of the intent message, then serialize it with the passkey flag.\n\t */\n\tasync signWithIntent(bytes: Uint8Array, intent: IntentScope): Promise<SignatureWithBytes> {\n\t\t// prepend it into an intent message and computes the digest.\n\t\tconst intentMessage = messageWithIntent(intent, bytes);\n\t\tconst digest = blake2b(intentMessage, { dkLen: 32 });\n\n\t\t// sign the digest.\n\t\tconst signature = await this.sign(digest);\n\n\t\t// prepend with the passkey flag.\n\t\tconst serializedSignature = new Uint8Array(1 + signature.length);\n\t\tserializedSignature.set([SIGNATURE_SCHEME_TO_FLAG[this.getKeyScheme()]]);\n\t\tserializedSignature.set(signature, 1);\n\t\treturn {\n\t\t\tsignature: toBase64(serializedSignature),\n\t\t\tbytes: toBase64(bytes),\n\t\t};\n\t}\n\n\t/**\n\t * Given a message, asks the passkey device to sign it and return all (up to 4) possible public keys.\n\t * See: https://bitcoin.stackexchange.com/questions/81232/how-is-public-key-extracted-from-message-digital-signature-address\n\t *\n\t * This is useful if the user previously created passkey wallet with the origin, but the wallet session\n\t * does not have the public key / address. By calling this method twice with two different messages, the\n\t * wallet can compare the returned public keys and uniquely identify the previously created passkey wallet\n\t * using `findCommonPublicKey`.\n\t *\n\t * Alternatively, one call can be made and all possible public keys should be checked onchain to see if\n\t * there is any assets.\n\t *\n\t * Once the correct public key is identified, a passkey instance can then be initialized with this public key.\n\t *\n\t * Example usage to recover wallet with two signing calls:\n\t * ```\n\t * let provider = new BrowserPasskeyProvider('Sui Passkey Example',{\n\t * rpName: 'Sui Passkey Example',\n\t * \t rpId: window.location.hostname,\n\t * } as BrowserPasswordProviderOptions);\n\t * const testMessage = new TextEncoder().encode('Hello world!');\n\t * const possiblePks = await PasskeyKeypair.signAndRecover(provider, testMessage);\n\t * const testMessage2 = new TextEncoder().encode('Hello world 2!');\n\t * const possiblePks2 = await PasskeyKeypair.signAndRecover(provider, testMessage2);\n\t * const commonPk = findCommonPublicKey(possiblePks, possiblePks2);\n\t * const signer = new PasskeyKeypair(provider, commonPk.toRawBytes());\n\t * ```\n\t *\n\t * @param provider - the passkey provider.\n\t * @param message - the message to sign.\n\t * @returns all possible public keys.\n\t */\n\tstatic async signAndRecover(\n\t\tprovider: PasskeyProvider,\n\t\tmessage: Uint8Array,\n\t): Promise<PublicKey[]> {\n\t\tconst credential = await provider.get(message);\n\t\tconst fullMessage = messageFromAssertionResponse(credential.response);\n\t\tconst sig = secp256r1.Signature.fromDER(new Uint8Array(credential.response.signature));\n\n\t\tconst res = [];\n\t\tfor (let i = 0; i < 4; i++) {\n\t\t\tconst s = sig.addRecoveryBit(i);\n\t\t\ttry {\n\t\t\t\tconst pubkey = s.recoverPublicKey(sha256(fullMessage));\n\t\t\t\tconst pk = new PasskeyPublicKey(pubkey.toRawBytes(true));\n\t\t\t\tres.push(pk);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n}\n\n/**\n * Finds the unique public key that exists in both arrays, throws error if the common\n * pubkey does not equal to one.\n *\n * @param arr1 - The first pubkeys array.\n * @param arr2 - The second pubkeys array.\n * @returns The only common pubkey in both arrays.\n */\nexport function findCommonPublicKey(arr1: PublicKey[], arr2: PublicKey[]): PublicKey {\n\tconst matchingPubkeys: PublicKey[] = [];\n\tfor (const pubkey1 of arr1) {\n\t\tfor (const pubkey2 of arr2) {\n\t\t\tif (pubkey1.equals(pubkey2)) {\n\t\t\t\tmatchingPubkeys.push(pubkey1);\n\t\t\t}\n\t\t}\n\t}\n\tif (matchingPubkeys.length !== 1) {\n\t\tthrow new Error('No unique public key found');\n\t}\n\treturn matchingPubkeys[0];\n}\n\n/**\n * Constructs the message that the passkey signature is produced over as authenticatorData || sha256(clientDataJSON).\n */\nfunction messageFromAssertionResponse(response: AuthenticatorAssertionResponse): Uint8Array {\n\tconst authenticatorData = new Uint8Array(response.authenticatorData);\n\tconst clientDataJSON = new Uint8Array(response.clientDataJSON);\n\tconst clientDataJSONDigest = sha256(clientDataJSON);\n\treturn new Uint8Array([...authenticatorData, ...clientDataJSONDigest]);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA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GAAG,KAAK;AAC3B,YAAM,IAAI,IAAI,eAAe,CAAC;AAC9B,UAAI;AACH,cAAM,SAAS,EAAE,qBAAiB,sBAAO,WAAW,CAAC;AACrD,cAAM,KAAK,IAAI,kCAAiB,OAAO,WAAW,IAAI,CAAC;AACvD,YAAI,KAAK,EAAE;AAAA,MACZ,QAAQ;AACP;AAAA,MACD;AAAA,IACD;AACA,WAAO;AAAA,EACR;AACD;AAUO,SAAS,oBAAoB,MAAmB,MAA8B;AACpF,QAAM,kBAA+B,CAAC;AACtC,aAAW,WAAW,MAAM;AAC3B,eAAW,WAAW,MAAM;AAC3B,UAAI,QAAQ,OAAO,OAAO,GAAG;AAC5B,wBAAgB,KAAK,OAAO;AAAA,MAC7B;AAAA,IACD;AAAA,EACD;AACA,MAAI,gBAAgB,WAAW,GAAG;AACjC,UAAM,IAAI,MAAM,4BAA4B;AAAA,EAC7C;AACA,SAAO,gBAAgB,CAAC;AACzB;AAKA,SAAS,6BAA6B,UAAsD;AAC3F,QAAM,oBAAoB,IAAI,WAAW,SAAS,iBAAiB;AACnE,QAAM,iBAAiB,IAAI,WAAW,SAAS,cAAc;AAC7D,QAAM,2BAAuB,sBAAO,cAAc;AAClD,SAAO,IAAI,WAAW,CAAC,GAAG,mBAAmB,GAAG,oBAAoB,CAAC;AACtE;",
|
|
6
6
|
"names": ["import_bcs"]
|
|
7
7
|
}
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const PACKAGE_VERSION = "1.
|
|
1
|
+
export declare const PACKAGE_VERSION = "1.20.0";
|
|
2
2
|
export declare const TARGETED_RPC_VERSION = "1.41.0";
|
package/dist/cjs/version.js
CHANGED
|
@@ -22,6 +22,6 @@ __export(version_exports, {
|
|
|
22
22
|
TARGETED_RPC_VERSION: () => TARGETED_RPC_VERSION
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(version_exports);
|
|
25
|
-
const PACKAGE_VERSION = "1.
|
|
25
|
+
const PACKAGE_VERSION = "1.20.0";
|
|
26
26
|
const TARGETED_RPC_VERSION = "1.41.0";
|
|
27
27
|
//# sourceMappingURL=version.js.map
|
package/dist/cjs/version.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/version.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '1.
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '1.20.0';\nexport const TARGETED_RPC_VERSION = '1.41.0';\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKO,MAAM,kBAAkB;AACxB,MAAM,uBAAuB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { PasskeyKeypair, BrowserPasskeyProvider } from './keypair.js';
|
|
1
|
+
export { PasskeyKeypair, BrowserPasskeyProvider, findCommonPublicKey } from './keypair.js';
|
|
2
2
|
export type { PasskeyProvider, BrowserPasswordProviderOptions } from './keypair.js';
|
|
3
3
|
export { PasskeyPublicKey } from './publickey.js';
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { PasskeyKeypair, BrowserPasskeyProvider } from "./keypair.js";
|
|
1
|
+
import { PasskeyKeypair, BrowserPasskeyProvider, findCommonPublicKey } from "./keypair.js";
|
|
2
2
|
import { PasskeyPublicKey } from "./publickey.js";
|
|
3
3
|
export {
|
|
4
4
|
BrowserPasskeyProvider,
|
|
5
5
|
PasskeyKeypair,
|
|
6
|
-
PasskeyPublicKey
|
|
6
|
+
PasskeyPublicKey,
|
|
7
|
+
findCommonPublicKey
|
|
7
8
|
};
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/keypairs/passkey/index.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nexport { PasskeyKeypair, BrowserPasskeyProvider } from './keypair.js';\nexport type { PasskeyProvider, BrowserPasswordProviderOptions } from './keypair.js';\nexport { PasskeyPublicKey } from './publickey.js';\n"],
|
|
5
|
-
"mappings": "AAEA,SAAS,gBAAgB,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nexport { PasskeyKeypair, BrowserPasskeyProvider, findCommonPublicKey } from './keypair.js';\nexport type { PasskeyProvider, BrowserPasswordProviderOptions } from './keypair.js';\nexport { PasskeyPublicKey } from './publickey.js';\n"],
|
|
5
|
+
"mappings": "AAEA,SAAS,gBAAgB,wBAAwB,2BAA2B;AAE5E,SAAS,wBAAwB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -30,15 +30,27 @@ export declare class PasskeyKeypair extends Signer {
|
|
|
30
30
|
*/
|
|
31
31
|
getKeyScheme(): SignatureScheme;
|
|
32
32
|
/**
|
|
33
|
-
* Creates an instance of Passkey signer.
|
|
34
|
-
* For example:
|
|
33
|
+
* Creates an instance of Passkey signer. If no passkey wallet had created before,
|
|
34
|
+
* use `getPasskeyInstance`. For example:
|
|
35
35
|
* ```
|
|
36
|
-
*
|
|
36
|
+
* let provider = new BrowserPasskeyProvider('Sui Passkey Example',{
|
|
37
|
+
* rpName: 'Sui Passkey Example',
|
|
38
|
+
* rpId: window.location.hostname,
|
|
39
|
+
* } as BrowserPasswordProviderOptions);
|
|
40
|
+
* const signer = await PasskeyKeypair.getPasskeyInstance(provider);
|
|
37
41
|
* ```
|
|
42
|
+
*
|
|
43
|
+
* If there are existing passkey wallet, use `signAndRecover` to identify the correct
|
|
44
|
+
* public key and then initialize the instance. See usage in `signAndRecover`.
|
|
38
45
|
*/
|
|
39
46
|
constructor(publicKey: Uint8Array, provider: PasskeyProvider);
|
|
40
47
|
/**
|
|
41
48
|
* Creates an instance of Passkey signer invoking the passkey from navigator.
|
|
49
|
+
* Note that this will invoke the passkey device to create a fresh credential.
|
|
50
|
+
* Should only be called if passkey wallet is created for the first time.
|
|
51
|
+
*
|
|
52
|
+
* @param provider - the passkey provider.
|
|
53
|
+
* @returns the passkey instance.
|
|
42
54
|
*/
|
|
43
55
|
static getPasskeyInstance(provider: PasskeyProvider): Promise<PasskeyKeypair>;
|
|
44
56
|
/**
|
|
@@ -55,5 +67,47 @@ export declare class PasskeyKeypair extends Signer {
|
|
|
55
67
|
* digest of the intent message, then serialize it with the passkey flag.
|
|
56
68
|
*/
|
|
57
69
|
signWithIntent(bytes: Uint8Array, intent: IntentScope): Promise<SignatureWithBytes>;
|
|
70
|
+
/**
|
|
71
|
+
* Given a message, asks the passkey device to sign it and return all (up to 4) possible public keys.
|
|
72
|
+
* See: https://bitcoin.stackexchange.com/questions/81232/how-is-public-key-extracted-from-message-digital-signature-address
|
|
73
|
+
*
|
|
74
|
+
* This is useful if the user previously created passkey wallet with the origin, but the wallet session
|
|
75
|
+
* does not have the public key / address. By calling this method twice with two different messages, the
|
|
76
|
+
* wallet can compare the returned public keys and uniquely identify the previously created passkey wallet
|
|
77
|
+
* using `findCommonPublicKey`.
|
|
78
|
+
*
|
|
79
|
+
* Alternatively, one call can be made and all possible public keys should be checked onchain to see if
|
|
80
|
+
* there is any assets.
|
|
81
|
+
*
|
|
82
|
+
* Once the correct public key is identified, a passkey instance can then be initialized with this public key.
|
|
83
|
+
*
|
|
84
|
+
* Example usage to recover wallet with two signing calls:
|
|
85
|
+
* ```
|
|
86
|
+
* let provider = new BrowserPasskeyProvider('Sui Passkey Example',{
|
|
87
|
+
* rpName: 'Sui Passkey Example',
|
|
88
|
+
* rpId: window.location.hostname,
|
|
89
|
+
* } as BrowserPasswordProviderOptions);
|
|
90
|
+
* const testMessage = new TextEncoder().encode('Hello world!');
|
|
91
|
+
* const possiblePks = await PasskeyKeypair.signAndRecover(provider, testMessage);
|
|
92
|
+
* const testMessage2 = new TextEncoder().encode('Hello world 2!');
|
|
93
|
+
* const possiblePks2 = await PasskeyKeypair.signAndRecover(provider, testMessage2);
|
|
94
|
+
* const commonPk = findCommonPublicKey(possiblePks, possiblePks2);
|
|
95
|
+
* const signer = new PasskeyKeypair(provider, commonPk.toRawBytes());
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @param provider - the passkey provider.
|
|
99
|
+
* @param message - the message to sign.
|
|
100
|
+
* @returns all possible public keys.
|
|
101
|
+
*/
|
|
102
|
+
static signAndRecover(provider: PasskeyProvider, message: Uint8Array): Promise<PublicKey[]>;
|
|
58
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Finds the unique public key that exists in both arrays, throws error if the common
|
|
106
|
+
* pubkey does not equal to one.
|
|
107
|
+
*
|
|
108
|
+
* @param arr1 - The first pubkeys array.
|
|
109
|
+
* @param arr2 - The second pubkeys array.
|
|
110
|
+
* @returns The only common pubkey in both arrays.
|
|
111
|
+
*/
|
|
112
|
+
export declare function findCommonPublicKey(arr1: PublicKey[], arr2: PublicKey[]): PublicKey;
|
|
59
113
|
export {};
|
|
@@ -9,6 +9,7 @@ var _name, _options;
|
|
|
9
9
|
import { toBase64 } from "@mysten/bcs";
|
|
10
10
|
import { secp256r1 } from "@noble/curves/p256";
|
|
11
11
|
import { blake2b } from "@noble/hashes/blake2b";
|
|
12
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
12
13
|
import { randomBytes } from "@noble/hashes/utils";
|
|
13
14
|
import { PasskeyAuthenticator } from "../../bcs/bcs.js";
|
|
14
15
|
import { messageWithIntent, SIGNATURE_SCHEME_TO_FLAG, Signer } from "../../cryptography/index.js";
|
|
@@ -72,11 +73,18 @@ class PasskeyKeypair extends Signer {
|
|
|
72
73
|
return "Passkey";
|
|
73
74
|
}
|
|
74
75
|
/**
|
|
75
|
-
* Creates an instance of Passkey signer.
|
|
76
|
-
* For example:
|
|
76
|
+
* Creates an instance of Passkey signer. If no passkey wallet had created before,
|
|
77
|
+
* use `getPasskeyInstance`. For example:
|
|
77
78
|
* ```
|
|
78
|
-
*
|
|
79
|
+
* let provider = new BrowserPasskeyProvider('Sui Passkey Example',{
|
|
80
|
+
* rpName: 'Sui Passkey Example',
|
|
81
|
+
* rpId: window.location.hostname,
|
|
82
|
+
* } as BrowserPasswordProviderOptions);
|
|
83
|
+
* const signer = await PasskeyKeypair.getPasskeyInstance(provider);
|
|
79
84
|
* ```
|
|
85
|
+
*
|
|
86
|
+
* If there are existing passkey wallet, use `signAndRecover` to identify the correct
|
|
87
|
+
* public key and then initialize the instance. See usage in `signAndRecover`.
|
|
80
88
|
*/
|
|
81
89
|
constructor(publicKey, provider) {
|
|
82
90
|
super();
|
|
@@ -85,6 +93,11 @@ class PasskeyKeypair extends Signer {
|
|
|
85
93
|
}
|
|
86
94
|
/**
|
|
87
95
|
* Creates an instance of Passkey signer invoking the passkey from navigator.
|
|
96
|
+
* Note that this will invoke the passkey device to create a fresh credential.
|
|
97
|
+
* Should only be called if passkey wallet is created for the first time.
|
|
98
|
+
*
|
|
99
|
+
* @param provider - the passkey provider.
|
|
100
|
+
* @returns the passkey instance.
|
|
88
101
|
*/
|
|
89
102
|
static async getPasskeyInstance(provider) {
|
|
90
103
|
const credential = await provider.create();
|
|
@@ -145,9 +158,79 @@ class PasskeyKeypair extends Signer {
|
|
|
145
158
|
bytes: toBase64(bytes)
|
|
146
159
|
};
|
|
147
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Given a message, asks the passkey device to sign it and return all (up to 4) possible public keys.
|
|
163
|
+
* See: https://bitcoin.stackexchange.com/questions/81232/how-is-public-key-extracted-from-message-digital-signature-address
|
|
164
|
+
*
|
|
165
|
+
* This is useful if the user previously created passkey wallet with the origin, but the wallet session
|
|
166
|
+
* does not have the public key / address. By calling this method twice with two different messages, the
|
|
167
|
+
* wallet can compare the returned public keys and uniquely identify the previously created passkey wallet
|
|
168
|
+
* using `findCommonPublicKey`.
|
|
169
|
+
*
|
|
170
|
+
* Alternatively, one call can be made and all possible public keys should be checked onchain to see if
|
|
171
|
+
* there is any assets.
|
|
172
|
+
*
|
|
173
|
+
* Once the correct public key is identified, a passkey instance can then be initialized with this public key.
|
|
174
|
+
*
|
|
175
|
+
* Example usage to recover wallet with two signing calls:
|
|
176
|
+
* ```
|
|
177
|
+
* let provider = new BrowserPasskeyProvider('Sui Passkey Example',{
|
|
178
|
+
* rpName: 'Sui Passkey Example',
|
|
179
|
+
* rpId: window.location.hostname,
|
|
180
|
+
* } as BrowserPasswordProviderOptions);
|
|
181
|
+
* const testMessage = new TextEncoder().encode('Hello world!');
|
|
182
|
+
* const possiblePks = await PasskeyKeypair.signAndRecover(provider, testMessage);
|
|
183
|
+
* const testMessage2 = new TextEncoder().encode('Hello world 2!');
|
|
184
|
+
* const possiblePks2 = await PasskeyKeypair.signAndRecover(provider, testMessage2);
|
|
185
|
+
* const commonPk = findCommonPublicKey(possiblePks, possiblePks2);
|
|
186
|
+
* const signer = new PasskeyKeypair(provider, commonPk.toRawBytes());
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @param provider - the passkey provider.
|
|
190
|
+
* @param message - the message to sign.
|
|
191
|
+
* @returns all possible public keys.
|
|
192
|
+
*/
|
|
193
|
+
static async signAndRecover(provider, message) {
|
|
194
|
+
const credential = await provider.get(message);
|
|
195
|
+
const fullMessage = messageFromAssertionResponse(credential.response);
|
|
196
|
+
const sig = secp256r1.Signature.fromDER(new Uint8Array(credential.response.signature));
|
|
197
|
+
const res = [];
|
|
198
|
+
for (let i = 0; i < 4; i++) {
|
|
199
|
+
const s = sig.addRecoveryBit(i);
|
|
200
|
+
try {
|
|
201
|
+
const pubkey = s.recoverPublicKey(sha256(fullMessage));
|
|
202
|
+
const pk = new PasskeyPublicKey(pubkey.toRawBytes(true));
|
|
203
|
+
res.push(pk);
|
|
204
|
+
} catch {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return res;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function findCommonPublicKey(arr1, arr2) {
|
|
212
|
+
const matchingPubkeys = [];
|
|
213
|
+
for (const pubkey1 of arr1) {
|
|
214
|
+
for (const pubkey2 of arr2) {
|
|
215
|
+
if (pubkey1.equals(pubkey2)) {
|
|
216
|
+
matchingPubkeys.push(pubkey1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (matchingPubkeys.length !== 1) {
|
|
221
|
+
throw new Error("No unique public key found");
|
|
222
|
+
}
|
|
223
|
+
return matchingPubkeys[0];
|
|
224
|
+
}
|
|
225
|
+
function messageFromAssertionResponse(response) {
|
|
226
|
+
const authenticatorData = new Uint8Array(response.authenticatorData);
|
|
227
|
+
const clientDataJSON = new Uint8Array(response.clientDataJSON);
|
|
228
|
+
const clientDataJSONDigest = sha256(clientDataJSON);
|
|
229
|
+
return new Uint8Array([...authenticatorData, ...clientDataJSONDigest]);
|
|
148
230
|
}
|
|
149
231
|
export {
|
|
150
232
|
BrowserPasskeyProvider,
|
|
151
|
-
PasskeyKeypair
|
|
233
|
+
PasskeyKeypair,
|
|
234
|
+
findCommonPublicKey
|
|
152
235
|
};
|
|
153
236
|
//# sourceMappingURL=keypair.js.map
|