@sugarcasino/passkey-wallet 0.0.0-stage → 0.1.0-beta.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 +8 -0
- package/LICENSE +201 -0
- package/README.md +135 -2
- package/dist/constants.mjs +9 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/errors.d.mts +11 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/errors.mjs +24 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.mjs +5 -0
- package/dist/key.mjs +36 -0
- package/dist/key.mjs.map +1 -0
- package/dist/provider.mjs +91 -0
- package/dist/provider.mjs.map +1 -0
- package/dist/wallet/features.d.mts +23 -0
- package/dist/wallet/features.d.mts.map +1 -0
- package/dist/wallet/features.mjs +6 -0
- package/dist/wallet/features.mjs.map +1 -0
- package/dist/wallet/initializer.d.mts +16 -0
- package/dist/wallet/initializer.d.mts.map +1 -0
- package/dist/wallet/initializer.mjs +21 -0
- package/dist/wallet/initializer.mjs.map +1 -0
- package/dist/wallet/register.d.mts +8 -0
- package/dist/wallet/register.d.mts.map +1 -0
- package/dist/wallet/register.mjs +19 -0
- package/dist/wallet/register.mjs.map +1 -0
- package/dist/wallet/types.d.mts +45 -0
- package/dist/wallet/types.d.mts.map +1 -0
- package/dist/wallet/utils.d.mts +13 -0
- package/dist/wallet/utils.d.mts.map +1 -0
- package/dist/wallet/utils.mjs +25 -0
- package/dist/wallet/utils.mjs.map +1 -0
- package/dist/wallet/wallet.d.mts +25 -0
- package/dist/wallet/wallet.d.mts.map +1 -0
- package/dist/wallet/wallet.mjs +323 -0
- package/dist/wallet/wallet.mjs.map +1 -0
- package/package.json +80 -3
- package/skills/passkey-wallet/SKILL.md +28 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# @sugarcasino/passkey-wallet
|
|
2
|
+
|
|
3
|
+
## 0.1.0-beta.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#114](https://github.com/SugarCasino/ts-sdks/pull/114) [`9bd06f7`](https://github.com/SugarCasino/ts-sdks/commit/9bd06f7d76ca7b6452202eadd9da844d2e95ddb2) - - Add `passkeyWalletInitializer()` for dApp Kit's `walletInitializers` and `registerPasskeyWallet()`, which register a Wallet Standard wallet unlocked by a passkey. It derives a secp256r1 key with the WebAuthn PRF extension, so a synced passkey opens the same wallet on every device. The unlocked key is a non-extractable WebCrypto key kept in IndexedDB for `sessionDurationMs` (7 days by default, `0` for memory only), so it signs personal messages and transactions and executes them without further prompts, even after a reload. `webauthn.promptTimeoutMs` (60 seconds by default) sets how long a passkey prompt waits, `createPasskeyWallet(wallet, { label })` creates a passkey and names it in the password manager, and `wallet.name` and `wallet.icon` set how the wallet appears in wallet lists. Connecting with no wallet opens the wallet of the passkey the user picks, a disconnect forgets the wallet on this browser, reconnecting asks for the passkey once, and an expired key asks again at the next signature. Recoverable failures throw `PasskeyCancelledError`, `PasskeyPrfUnsupportedError` or `PasskeyWalletMismatchError`, all extending `PasskeyWalletError`. `isPasskeyWallet()` finds the wallet in dApp Kit's wallet list. `localStorage` holds only the public key and credential ID, and connection changes follow across tabs.
|
|
8
|
+
- Ship an agent skill entrypoint in `skills/passkey-wallet/SKILL.md` that loads the `passkey-wallet` skill from `SugarCasino/agent-skills`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,136 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@sugarcasino/passkey-wallet`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A self-custodial Sui wallet unlocked by a passkey. It is a [Wallet Standard](https://docs.sui.io/standards/wallet-standard) wallet, so dApp Kit lists it with the other wallets.
|
|
4
|
+
|
|
5
|
+
- The wallet key is a secp256r1 key derived from the passkey with the [WebAuthn PRF extension](https://w3c.github.io/webauthn/#prf-extension). A synced passkey, in iCloud Keychain, Google Password Manager or another password manager, opens the same wallet on every device. A passkey that was lost and never synced cannot be recovered.
|
|
6
|
+
- After an unlock, the key is a non-extractable WebCrypto key: the browser keeps it in IndexedDB for `sessionDurationMs` (7 days by default) and cannot export it. Signatures and page reloads need no prompt until it expires. After that, the next signature asks for the passkey again, even if the page stayed open. A disconnect deletes the key.
|
|
7
|
+
- `localStorage` holds only the public key and credential ID of the connected wallet. A disconnect deletes them too, so the next user of a shared browser cannot see which wallet was used.
|
|
8
|
+
- While the wallet is unlocked, a script injected into the page can sign, as it can with a zkLogin session key.
|
|
9
|
+
|
|
10
|
+
It runs only in the browser, in a secure context (`https://` or `localhost`), and needs a passkey with PRF support. Other passkeys fail with `PasskeyPrfUnsupportedError`.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @sugarcasino/passkey-wallet@beta @mysten/sui @mysten/dapp-kit-react
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createDAppKit } from '@mysten/dapp-kit-react';
|
|
22
|
+
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
23
|
+
import { passkeyWalletInitializer } from '@sugarcasino/passkey-wallet';
|
|
24
|
+
|
|
25
|
+
const GRPC_URLS = {
|
|
26
|
+
mainnet: 'https://fullnode.mainnet.sui.io:443',
|
|
27
|
+
testnet: 'https://fullnode.testnet.sui.io:443',
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const dAppKit = createDAppKit({
|
|
31
|
+
networks: ['mainnet', 'testnet'],
|
|
32
|
+
createClient: (network) => new SuiGrpcClient({ network, baseUrl: GRPC_URLS[network] }),
|
|
33
|
+
walletInitializers: [
|
|
34
|
+
passkeyWalletInitializer({ webauthn: { rpName: 'My App', rpId: 'app.example.com' } }),
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The initializer registers the wallet with one dApp Kit client per network. Without dApp Kit, register it yourself: `const { wallet, unregister } = registerPasskeyWallet({ clients, webauthn })` returns the wallet and a function that removes it.
|
|
40
|
+
|
|
41
|
+
| Option | Description |
|
|
42
|
+
| --- | --- |
|
|
43
|
+
| `clients` | `registerPasskeyWallet()` only: one client per supported network. They build and execute transactions. |
|
|
44
|
+
| `sessionDurationMs` | How long a session lasts once the passkey unlocks the wallet. Signatures and reloads need no prompt until it ends. Defaults to 7 days. `0` keeps the key in memory only, so the session ends at the next reload. |
|
|
45
|
+
| `webauthn.rpName` | Name shown in the passkey prompts. |
|
|
46
|
+
| `webauthn.rpId` | Domain the passkeys are bound to. Defaults to the current hostname. See [Choose the relying party ID](#choose-the-relying-party-id). |
|
|
47
|
+
| `webauthn.promptTimeoutMs` | How long a passkey prompt waits for the user before failing with `PasskeyCancelledError`. Defaults to 60 seconds. Browsers may clamp it. |
|
|
48
|
+
| `wallet.name` | Name in wallet lists, such as the dApp Kit connect modal. Defaults to `Passkey`. dApp Kit reconnects a wallet by its name, so keep it stable. |
|
|
49
|
+
| `wallet.icon` | Icon in wallet lists, as a data URL. Defaults to a person with a key, the usual passkey symbol. |
|
|
50
|
+
|
|
51
|
+
### Create and connect
|
|
52
|
+
|
|
53
|
+
| Action | Call | Passkey prompts |
|
|
54
|
+
| --- | --- | --- |
|
|
55
|
+
| Create a new wallet | `await createPasskeyWallet(wallet, { label })` | 1 (2 if the authenticator cannot evaluate PRF at creation) |
|
|
56
|
+
| Use an existing passkey, on a new browser or after a disconnect | `await dAppKit.connectWallet({ wallet })` | 1, in the passkey picker |
|
|
57
|
+
| Reload the page | dApp Kit auto-connect | 0 while the saved key is valid, else 1 at the first signature |
|
|
58
|
+
| Sign after unlocking | Any signing feature | 0 |
|
|
59
|
+
|
|
60
|
+
`label` names the passkey in the password manager, such as the user's email, so a user with several wallets can tell them apart. It defaults to `webauthn.rpName`.
|
|
61
|
+
|
|
62
|
+
`wallet` is the passkey wallet from dApp Kit's `useWallets()`, found with `isPasskeyWallet()`, or the `wallet` returned by `registerPasskeyWallet()`.
|
|
63
|
+
|
|
64
|
+
`standard:connect` never creates a passkey: with no connected wallet it opens the wallet of the passkey the user picks. After creating one, call `connectWallet()` so dApp Kit marks the wallet as connected:
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
import { useDAppKit, useWallets } from '@mysten/dapp-kit-react';
|
|
68
|
+
import { createPasskeyWallet, isPasskeyWallet } from '@sugarcasino/passkey-wallet';
|
|
69
|
+
|
|
70
|
+
const dAppKit = useDAppKit();
|
|
71
|
+
const wallet = useWallets().find((uiWallet) => isPasskeyWallet(uiWallet));
|
|
72
|
+
|
|
73
|
+
async function createWallet(email: string) {
|
|
74
|
+
await createPasskeyWallet(wallet!, { label: email });
|
|
75
|
+
await dAppKit.connectWallet({ wallet: wallet! });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function useExistingPasskey() {
|
|
79
|
+
await dAppKit.connectWallet({ wallet: wallet! });
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
A disconnect forgets the wallet on this browser, so offer both creating a wallet and using an existing passkey to a disconnected user.
|
|
84
|
+
|
|
85
|
+
Connections and disconnections follow across tabs. If the browser blocks storage or IndexedDB, the wallet stays connected and unlocked until the page reloads. `unregister()` also stops following other tabs.
|
|
86
|
+
|
|
87
|
+
### Sign and execute
|
|
88
|
+
|
|
89
|
+
The wallet implements `sui:signPersonalMessage`, `sui:signTransaction` and `sui:signAndExecuteTransaction`. Use them through dApp Kit as with any other wallet. Signatures are standard secp256r1 Sui signatures, so `verifyPersonalMessageSignature()` from `@mysten/sui/verify` verifies them.
|
|
90
|
+
|
|
91
|
+
### Errors
|
|
92
|
+
|
|
93
|
+
Errors the user can recover from extend `PasskeyWalletError`:
|
|
94
|
+
|
|
95
|
+
| Error | Cause | What to show |
|
|
96
|
+
| --- | --- | --- |
|
|
97
|
+
| `PasskeyCancelledError` | The passkey prompt was closed, refused or timed out. The wallet stays connected but locked. | Offer to try again: the next action asks for the passkey. |
|
|
98
|
+
| `PasskeyPrfUnsupportedError` | The passkey manager does not support the PRF extension. | Suggest iCloud Keychain, Google Password Manager or another passkey manager. |
|
|
99
|
+
| `PasskeyWalletMismatchError` | The chosen passkey opens another wallet than the connected one. | Ask for the passkey of the connected wallet, or disconnect. |
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { PasskeyCancelledError } from '@sugarcasino/passkey-wallet';
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
await dAppKit.signPersonalMessage({ message });
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (error instanceof PasskeyCancelledError) {
|
|
108
|
+
// Show a retry button.
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Each error's `name` is its class name, so logs and error trackers show the type. `instanceof` also matches errors from another copy of the package, when npm or Yarn installs two.
|
|
114
|
+
|
|
115
|
+
## Choose the relying party ID
|
|
116
|
+
|
|
117
|
+
The browser only lets a page use an `rpId` equal to its own domain or a parent domain, so another site cannot open your users' wallets. Any page under the `rpId` can, because the derivation inputs are public: code that gets the passkey can derive the key.
|
|
118
|
+
|
|
119
|
+
- Pick the narrowest `rpId` that covers your app, such as `app.example.com` rather than `example.com`. Every subdomain of the `rpId` can ask for the passkey, and the prompt shows the same domain, so a compromised or third-party subdomain can open the wallets.
|
|
120
|
+
- Choose it before users hold funds. Passkeys stay bound to it, so changing it later locks users out of their wallets.
|
|
121
|
+
- Other origins and native apps get access only if the domain allows them, through `/.well-known/webauthn` (related origins) or app association files. Do not list origins you do not control.
|
|
122
|
+
|
|
123
|
+
## Demo
|
|
124
|
+
|
|
125
|
+
[`demo/`](demo) creates a passkey wallet or opens an existing one, signs a message, signs a transaction and executes it on testnet.
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
pnpm install
|
|
129
|
+
pnpm --dir packages/passkey-wallet/demo dev
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Get testnet SUI for gas from the [Sui faucet](https://faucet.sui.io/).
|
|
133
|
+
|
|
134
|
+
## Documentation
|
|
135
|
+
|
|
136
|
+
The package includes [an agent skill](skills/passkey-wallet/SKILL.md) that loads full instructions from [SugarCasino/agent-skills](https://github.com/SugarCasino/agent-skills) at the commit pinned for this release. Install the full skills with `npx skills add SugarCasino/agent-skills --skill passkey-wallet`.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
/**
|
|
3
|
+
* Prefix of the PRF input, the key derivation info and the storage keys. Never change it: it
|
|
4
|
+
* defines every address and the keys users reconnect with.
|
|
5
|
+
*/
|
|
6
|
+
const NAMESPACE = "sui-passkey-wallet";
|
|
7
|
+
export { NAMESPACE };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=constants.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.mjs","names":[],"sources":["../src/constants.ts"],"sourcesContent":["// Copyright (c) Sugar\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Prefix of the PRF input, the key derivation info and the storage keys. Never change it: it\n * defines every address and the keys users reconnect with.\n */\nexport const NAMESPACE = 'sui-passkey-wallet';\n"],"mappings":";;;;;AAOA,MAAa,YAAY"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Base class of the errors a user can recover from. */
|
|
2
|
+
export declare class PasskeyWalletError extends Error {
|
|
3
|
+
static [Symbol.hasInstance](value: unknown): boolean;
|
|
4
|
+
}
|
|
5
|
+
/** The user closed or refused the passkey prompt, or it timed out. Retrying asks again. */
|
|
6
|
+
export declare class PasskeyCancelledError extends PasskeyWalletError {}
|
|
7
|
+
/** The passkey manager does not support the WebAuthn PRF extension, so it cannot unlock a wallet. */
|
|
8
|
+
export declare class PasskeyPrfUnsupportedError extends PasskeyWalletError {}
|
|
9
|
+
/** The chosen passkey opens another wallet than the connected one. */
|
|
10
|
+
export declare class PasskeyWalletMismatchError extends PasskeyWalletError {}
|
|
11
|
+
//# sourceMappingURL=errors.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.mts","names":[],"sources":["../src/errors.ts"],"mappings":";qBASa,2BAA2B;UACtB,OAAO,aAAa;;;qBAgBzB,8BAA8B;;qBAG9B,mCAAmC;;qBAGnC,mCAAmC"}
|
package/dist/errors.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
/** Shared by every copy of this package, so `instanceof` works across copies. */
|
|
3
|
+
const ERROR_BRAND = Symbol.for("@sugarcasino/passkey-wallet/error");
|
|
4
|
+
/** Base class of the errors a user can recover from. */
|
|
5
|
+
var PasskeyWalletError = class PasskeyWalletError extends Error {
|
|
6
|
+
static [Symbol.hasInstance](value) {
|
|
7
|
+
if (super[Symbol.hasInstance](value)) return true;
|
|
8
|
+
return typeof value === "object" && value !== null && ERROR_BRAND in value && (this === PasskeyWalletError || "name" in value && value.name === this.prototype.name);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
/** The user closed or refused the passkey prompt, or it timed out. Retrying asks again. */
|
|
12
|
+
var PasskeyCancelledError = class extends PasskeyWalletError {};
|
|
13
|
+
/** The passkey manager does not support the WebAuthn PRF extension, so it cannot unlock a wallet. */
|
|
14
|
+
var PasskeyPrfUnsupportedError = class extends PasskeyWalletError {};
|
|
15
|
+
/** The chosen passkey opens another wallet than the connected one. */
|
|
16
|
+
var PasskeyWalletMismatchError = class extends PasskeyWalletError {};
|
|
17
|
+
Object.defineProperty(PasskeyWalletError.prototype, ERROR_BRAND, { value: true });
|
|
18
|
+
PasskeyWalletError.prototype.name = "PasskeyWalletError";
|
|
19
|
+
PasskeyCancelledError.prototype.name = "PasskeyCancelledError";
|
|
20
|
+
PasskeyPrfUnsupportedError.prototype.name = "PasskeyPrfUnsupportedError";
|
|
21
|
+
PasskeyWalletMismatchError.prototype.name = "PasskeyWalletMismatchError";
|
|
22
|
+
export { PasskeyCancelledError, PasskeyPrfUnsupportedError, PasskeyWalletError, PasskeyWalletMismatchError };
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=errors.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["// Copyright (c) Sugar\n// SPDX-License-Identifier: Apache-2.0\n\n// oxlint-disable eslint/max-classes-per-file -- Small error classes stay together, as in @mysten/dapp-kit-core.\n\n/** Shared by every copy of this package, so `instanceof` works across copies. */\nconst ERROR_BRAND = Symbol.for('@sugarcasino/passkey-wallet/error');\n\n/** Base class of the errors a user can recover from. */\nexport class PasskeyWalletError extends Error {\n\tstatic override [Symbol.hasInstance](value: unknown): boolean {\n\t\tif (super[Symbol.hasInstance](value)) {\n\t\t\treturn true;\n\t\t}\n\n\t\t// An error from another copy of the package (npm or yarn can install two): match by name.\n\t\treturn (\n\t\t\ttypeof value === 'object' &&\n\t\t\tvalue !== null &&\n\t\t\tERROR_BRAND in value &&\n\t\t\t(this === PasskeyWalletError || ('name' in value && value.name === this.prototype.name))\n\t\t);\n\t}\n}\n\n/** The user closed or refused the passkey prompt, or it timed out. Retrying asks again. */\nexport class PasskeyCancelledError extends PasskeyWalletError {}\n\n/** The passkey manager does not support the WebAuthn PRF extension, so it cannot unlock a wallet. */\nexport class PasskeyPrfUnsupportedError extends PasskeyWalletError {}\n\n/** The chosen passkey opens another wallet than the connected one. */\nexport class PasskeyWalletMismatchError extends PasskeyWalletError {}\n\nObject.defineProperty(PasskeyWalletError.prototype, ERROR_BRAND, { value: true });\n\n// Literal names, as on built-in errors: minifiers rename classes.\nPasskeyWalletError.prototype.name = 'PasskeyWalletError';\nPasskeyCancelledError.prototype.name = 'PasskeyCancelledError';\nPasskeyPrfUnsupportedError.prototype.name = 'PasskeyPrfUnsupportedError';\nPasskeyWalletMismatchError.prototype.name = 'PasskeyWalletMismatchError';\n"],"mappings":";;AAMA,MAAM,cAAc,OAAO,IAAI,mCAAmC;;AAGlE,IAAa,qBAAb,MAAa,2BAA2B,MAAM;CAC7C,QAAiB,OAAO,aAAa,OAAyB;EAC7D,IAAI,MAAM,OAAO,YAAY,CAAC,KAAK,GAClC,OAAO;EAIR,OACC,OAAO,UAAU,YACjB,UAAU,QACV,eAAe,UACd,SAAS,sBAAuB,UAAU,SAAS,MAAM,SAAS,KAAK,UAAU;CAEpF;AACD;;AAGA,IAAa,wBAAb,cAA2C,mBAAmB,CAAC;;AAG/D,IAAa,6BAAb,cAAgD,mBAAmB,CAAC;;AAGpE,IAAa,6BAAb,cAAgD,mBAAmB,CAAC;AAEpE,OAAO,eAAe,mBAAmB,WAAW,aAAa,EAAE,OAAO,KAAK,CAAC;AAGhF,mBAAmB,UAAU,OAAO;AACpC,sBAAsB,UAAU,OAAO;AACvC,2BAA2B,UAAU,OAAO;AAC5C,2BAA2B,UAAU,OAAO"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PasskeyCancelledError, PasskeyPrfUnsupportedError, PasskeyWalletError, PasskeyWalletMismatchError } from "./errors.mjs";
|
|
2
|
+
import { CreatePasskeyWalletOptions } from "./wallet/features.mjs";
|
|
3
|
+
import { passkeyWalletInitializer } from "./wallet/initializer.mjs";
|
|
4
|
+
import { registerPasskeyWallet } from "./wallet/register.mjs";
|
|
5
|
+
import { createPasskeyWallet, isPasskeyWallet } from "./wallet/utils.mjs";
|
|
6
|
+
import { PasskeyWallet } from "./wallet/wallet.mjs";
|
|
7
|
+
import { PasskeyWalletOptions } from "./wallet/types.mjs";
|
|
8
|
+
export { type CreatePasskeyWalletOptions, PasskeyCancelledError, PasskeyPrfUnsupportedError, type PasskeyWallet, PasskeyWalletError, PasskeyWalletMismatchError, type PasskeyWalletOptions, createPasskeyWallet, isPasskeyWallet, passkeyWalletInitializer, registerPasskeyWallet };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { PasskeyCancelledError, PasskeyPrfUnsupportedError, PasskeyWalletError, PasskeyWalletMismatchError } from "./errors.mjs";
|
|
2
|
+
import { passkeyWalletInitializer } from "./wallet/initializer.mjs";
|
|
3
|
+
import { registerPasskeyWallet } from "./wallet/register.mjs";
|
|
4
|
+
import { createPasskeyWallet, isPasskeyWallet } from "./wallet/utils.mjs";
|
|
5
|
+
export { PasskeyCancelledError, PasskeyPrfUnsupportedError, PasskeyWalletError, PasskeyWalletMismatchError, createPasskeyWallet, isPasskeyWallet, passkeyWalletInitializer, registerPasskeyWallet };
|
package/dist/key.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { WebCryptoSigner } from "@mysten/webcrypto-signer";
|
|
2
|
+
import { mapHashToField } from "@noble/curves/abstract/modular.js";
|
|
3
|
+
import { p256 } from "@noble/curves/nist.js";
|
|
4
|
+
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
5
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
6
|
+
import { utf8ToBytes } from "@noble/hashes/utils.js";
|
|
7
|
+
import { base64urlnopad } from "@scure/base";
|
|
8
|
+
import { NAMESPACE } from "./constants.mjs";
|
|
9
|
+
// src/key.ts
|
|
10
|
+
const KEY_INFO = utf8ToBytes(`${NAMESPACE}/v1/secp256r1`);
|
|
11
|
+
/**
|
|
12
|
+
* Derives the wallet's secp256r1 key from a passkey PRF output. The key is imported as a
|
|
13
|
+
* non-extractable WebCrypto key and the raw bytes are wiped.
|
|
14
|
+
*/
|
|
15
|
+
async function deriveSigner(prfOutput) {
|
|
16
|
+
const secretKey = mapHashToField(hkdf(sha256, prfOutput, void 0, KEY_INFO, 48), p256.Point.Fn.ORDER);
|
|
17
|
+
const publicKey = p256.getPublicKey(secretKey, false);
|
|
18
|
+
try {
|
|
19
|
+
const privateKey = await crypto.subtle.importKey("jwk", {
|
|
20
|
+
kty: "EC",
|
|
21
|
+
crv: "P-256",
|
|
22
|
+
d: base64urlnopad.encode(secretKey),
|
|
23
|
+
x: base64urlnopad.encode(publicKey.subarray(1, 33)),
|
|
24
|
+
y: base64urlnopad.encode(publicKey.subarray(33))
|
|
25
|
+
}, {
|
|
26
|
+
name: "ECDSA",
|
|
27
|
+
namedCurve: "P-256"
|
|
28
|
+
}, false, ["sign"]);
|
|
29
|
+
return new WebCryptoSigner(privateKey, p256.Point.fromBytes(publicKey).toBytes(true));
|
|
30
|
+
} finally {
|
|
31
|
+
secretKey.fill(0);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export { deriveSigner };
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=key.mjs.map
|
package/dist/key.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key.mjs","names":[],"sources":["../src/key.ts"],"sourcesContent":["// Copyright (c) Sugar\n// SPDX-License-Identifier: Apache-2.0\n\nimport { WebCryptoSigner } from '@mysten/webcrypto-signer';\nimport { mapHashToField } from '@noble/curves/abstract/modular.js';\nimport { p256 } from '@noble/curves/nist.js';\nimport { hkdf } from '@noble/hashes/hkdf.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\nimport { utf8ToBytes } from '@noble/hashes/utils.js';\nimport { base64urlnopad } from '@scure/base';\nimport { NAMESPACE } from './constants.js';\n\nconst KEY_INFO = utf8ToBytes(`${NAMESPACE}/v1/secp256r1`);\n\n/**\n * Derives the wallet's secp256r1 key from a passkey PRF output. The key is imported as a\n * non-extractable WebCrypto key and the raw bytes are wiped.\n */\nexport async function deriveSigner(prfOutput: Uint8Array) {\n\t// 48 bytes reduced modulo the curve order gives a uniform, always valid secret key.\n\tconst secretKey = mapHashToField(\n\t\thkdf(sha256, prfOutput, undefined, KEY_INFO, 48),\n\t\tp256.Point.Fn.ORDER,\n\t);\n\tconst publicKey = p256.getPublicKey(secretKey, false);\n\n\ttry {\n\t\tconst privateKey = await crypto.subtle.importKey(\n\t\t\t'jwk',\n\t\t\t{\n\t\t\t\tkty: 'EC',\n\t\t\t\tcrv: 'P-256',\n\t\t\t\td: base64urlnopad.encode(secretKey),\n\t\t\t\tx: base64urlnopad.encode(publicKey.subarray(1, 33)),\n\t\t\t\ty: base64urlnopad.encode(publicKey.subarray(33)),\n\t\t\t},\n\t\t\t{ name: 'ECDSA', namedCurve: 'P-256' },\n\t\t\tfalse,\n\t\t\t['sign'],\n\t\t);\n\t\treturn new WebCryptoSigner(privateKey, p256.Point.fromBytes(publicKey).toBytes(true));\n\t} finally {\n\t\tsecretKey.fill(0);\n\t}\n}\n"],"mappings":";;;;;;;;;AAYA,MAAM,WAAW,YAAY,GAAG,UAAU,cAAc;;;;;AAMxD,eAAsB,aAAa,WAAuB;CAEzD,MAAM,YAAY,eACjB,KAAK,QAAQ,WAAW,KAAA,GAAW,UAAU,EAAE,GAC/C,KAAK,MAAM,GAAG,KACf;CACA,MAAM,YAAY,KAAK,aAAa,WAAW,KAAK;CAEpD,IAAI;EACH,MAAM,aAAa,MAAM,OAAO,OAAO,UACtC,OACA;GACC,KAAK;GACL,KAAK;GACL,GAAG,eAAe,OAAO,SAAS;GAClC,GAAG,eAAe,OAAO,UAAU,SAAS,GAAG,EAAE,CAAC;GAClD,GAAG,eAAe,OAAO,UAAU,SAAS,EAAE,CAAC;EAChD,GACA;GAAE,MAAM;GAAS,YAAY;EAAQ,GACrC,OACA,CAAC,MAAM,CACR;EACA,OAAO,IAAI,gBAAgB,YAAY,KAAK,MAAM,UAAU,SAAS,CAAC,CAAC,QAAQ,IAAI,CAAC;CACrF,UAAU;EACT,UAAU,KAAK,CAAC;CACjB;AACD"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { utf8ToBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { NAMESPACE } from "./constants.mjs";
|
|
3
|
+
import { PasskeyCancelledError, PasskeyPrfUnsupportedError } from "./errors.mjs";
|
|
4
|
+
// src/provider.ts
|
|
5
|
+
/**
|
|
6
|
+
* Input of the WebAuthn PRF extension. A synced passkey returns the same output for it on every
|
|
7
|
+
* device.
|
|
8
|
+
*/
|
|
9
|
+
const PRF_INPUT = utf8ToBytes(`${NAMESPACE}/v1`);
|
|
10
|
+
const PRF_UNSUPPORTED = "This passkey cannot unlock a wallet. Use a passkey manager that supports the PRF extension, such as iCloud Keychain or Google Password Manager.";
|
|
11
|
+
/**
|
|
12
|
+
* Creates a discoverable passkey and returns its PRF output. Asks a second time only when the
|
|
13
|
+
* authenticator cannot evaluate PRF during creation.
|
|
14
|
+
*/
|
|
15
|
+
async function createPasskey(options, label = options.rpName) {
|
|
16
|
+
const { rpId, rpName, timeoutMs } = options;
|
|
17
|
+
const credential = await requestCredential(async () => navigator.credentials.create({ publicKey: {
|
|
18
|
+
rp: {
|
|
19
|
+
id: rpId,
|
|
20
|
+
name: rpName
|
|
21
|
+
},
|
|
22
|
+
user: {
|
|
23
|
+
id: crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16)),
|
|
24
|
+
name: label,
|
|
25
|
+
displayName: label
|
|
26
|
+
},
|
|
27
|
+
challenge: crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32)),
|
|
28
|
+
pubKeyCredParams: [{
|
|
29
|
+
alg: -7,
|
|
30
|
+
type: "public-key"
|
|
31
|
+
}, {
|
|
32
|
+
alg: -257,
|
|
33
|
+
type: "public-key"
|
|
34
|
+
}],
|
|
35
|
+
authenticatorSelection: {
|
|
36
|
+
residentKey: "required",
|
|
37
|
+
requireResidentKey: true,
|
|
38
|
+
userVerification: "required"
|
|
39
|
+
},
|
|
40
|
+
extensions: { prf: { eval: { first: PRF_INPUT } } },
|
|
41
|
+
timeout: timeoutMs
|
|
42
|
+
} }), "Passkey creation was cancelled. Try again to create your wallet.");
|
|
43
|
+
const credentialId = new Uint8Array(credential.rawId);
|
|
44
|
+
const prf = credential.getClientExtensionResults().prf;
|
|
45
|
+
if (prf?.results != null) return {
|
|
46
|
+
credentialId,
|
|
47
|
+
prfOutput: toBytes(prf.results.first)
|
|
48
|
+
};
|
|
49
|
+
if (prf?.enabled !== true) throw new PasskeyPrfUnsupportedError(PRF_UNSUPPORTED);
|
|
50
|
+
return getPasskeySecret(options, credentialId);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Asks for a passkey and returns its PRF output. Without `credentialId`, the user picks any passkey
|
|
54
|
+
* of the relying party, which recovers a wallet on a new browser.
|
|
55
|
+
*/
|
|
56
|
+
async function getPasskeySecret({ rpId, timeoutMs }, credentialId) {
|
|
57
|
+
const credential = await requestCredential(async () => navigator.credentials.get({ publicKey: {
|
|
58
|
+
rpId,
|
|
59
|
+
challenge: crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32)),
|
|
60
|
+
userVerification: "required",
|
|
61
|
+
extensions: { prf: { eval: { first: PRF_INPUT } } },
|
|
62
|
+
timeout: timeoutMs,
|
|
63
|
+
...credentialId != null && { allowCredentials: [{
|
|
64
|
+
type: "public-key",
|
|
65
|
+
id: new Uint8Array(credentialId)
|
|
66
|
+
}] }
|
|
67
|
+
} }), "The passkey prompt was closed. Try again to unlock your wallet.");
|
|
68
|
+
const results = credential.getClientExtensionResults().prf?.results;
|
|
69
|
+
if (results == null) throw new PasskeyPrfUnsupportedError(PRF_UNSUPPORTED);
|
|
70
|
+
return {
|
|
71
|
+
credentialId: new Uint8Array(credential.rawId),
|
|
72
|
+
prfOutput: toBytes(results.first)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
async function requestCredential(request, cancelledMessage) {
|
|
76
|
+
let credential;
|
|
77
|
+
try {
|
|
78
|
+
credential = await request();
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error instanceof DOMException && error.name === "NotAllowedError") throw new PasskeyCancelledError(cancelledMessage, { cause: error });
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
if (credential == null) throw new PasskeyCancelledError(cancelledMessage);
|
|
84
|
+
return credential;
|
|
85
|
+
}
|
|
86
|
+
function toBytes(source) {
|
|
87
|
+
return ArrayBuffer.isView(source) ? new Uint8Array(source.buffer, source.byteOffset, source.byteLength) : new Uint8Array(source);
|
|
88
|
+
}
|
|
89
|
+
export { createPasskey, getPasskeySecret };
|
|
90
|
+
|
|
91
|
+
//# sourceMappingURL=provider.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.mjs","names":[],"sources":["../src/provider.ts"],"sourcesContent":["// Copyright (c) Sugar\n// SPDX-License-Identifier: Apache-2.0\n\nimport { utf8ToBytes } from '@noble/hashes/utils.js';\nimport { NAMESPACE } from './constants.js';\nimport { PasskeyCancelledError, PasskeyPrfUnsupportedError } from './errors.js';\n\n/**\n * Input of the WebAuthn PRF extension. A synced passkey returns the same output for it on every\n * device.\n */\nconst PRF_INPUT = utf8ToBytes(`${NAMESPACE}/v1`);\n\nconst PRF_UNSUPPORTED =\n\t'This passkey cannot unlock a wallet. Use a passkey manager that supports the PRF extension, such as iCloud Keychain or Google Password Manager.';\n\n/** Relying party and prompt timeout of every passkey request. */\nexport type WebAuthnOptions = { rpId: string; rpName: string; timeoutMs: number };\n\nexport type PasskeySecret = {\n\tcredentialId: Uint8Array;\n\t/** 32-byte PRF output, the secret the wallet key is derived from. */\n\tprfOutput: Uint8Array;\n};\n\n/**\n * Creates a discoverable passkey and returns its PRF output. Asks a second time only when the\n * authenticator cannot evaluate PRF during creation.\n */\nexport async function createPasskey(\n\toptions: WebAuthnOptions,\n\tlabel = options.rpName,\n): Promise<PasskeySecret> {\n\tconst { rpId, rpName, timeoutMs } = options;\n\tconst credential = await requestCredential(\n\t\tasync () =>\n\t\t\tnavigator.credentials.create({\n\t\t\t\tpublicKey: {\n\t\t\t\t\trp: { id: rpId, name: rpName },\n\t\t\t\t\tuser: {\n\t\t\t\t\t\tid: crypto.getRandomValues(new Uint8Array(16)),\n\t\t\t\t\t\tname: label,\n\t\t\t\t\t\tdisplayName: label,\n\t\t\t\t\t},\n\t\t\t\t\t// No server verifies the attestation: the PRF output alone defines the wallet.\n\t\t\t\t\tchallenge: crypto.getRandomValues(new Uint8Array(32)),\n\t\t\t\t\tpubKeyCredParams: [\n\t\t\t\t\t\t{ alg: -7, type: 'public-key' },\n\t\t\t\t\t\t{ alg: -257, type: 'public-key' },\n\t\t\t\t\t],\n\t\t\t\t\tauthenticatorSelection: {\n\t\t\t\t\t\tresidentKey: 'required',\n\t\t\t\t\t\trequireResidentKey: true,\n\t\t\t\t\t\tuserVerification: 'required',\n\t\t\t\t\t},\n\t\t\t\t\textensions: { prf: { eval: { first: PRF_INPUT } } },\n\t\t\t\t\ttimeout: timeoutMs,\n\t\t\t\t},\n\t\t\t}),\n\t\t'Passkey creation was cancelled. Try again to create your wallet.',\n\t);\n\tconst credentialId = new Uint8Array(credential.rawId);\n\tconst prf = credential.getClientExtensionResults().prf;\n\n\tif (prf?.results != null) {\n\t\treturn { credentialId, prfOutput: toBytes(prf.results.first) };\n\t}\n\n\tif (prf?.enabled !== true) {\n\t\tthrow new PasskeyPrfUnsupportedError(PRF_UNSUPPORTED);\n\t}\n\n\treturn getPasskeySecret(options, credentialId);\n}\n\n/**\n * Asks for a passkey and returns its PRF output. Without `credentialId`, the user picks any passkey\n * of the relying party, which recovers a wallet on a new browser.\n */\nexport async function getPasskeySecret(\n\t{ rpId, timeoutMs }: WebAuthnOptions,\n\tcredentialId?: Uint8Array,\n): Promise<PasskeySecret> {\n\tconst credential = await requestCredential(\n\t\tasync () =>\n\t\t\tnavigator.credentials.get({\n\t\t\t\tpublicKey: {\n\t\t\t\t\trpId,\n\t\t\t\t\tchallenge: crypto.getRandomValues(new Uint8Array(32)),\n\t\t\t\t\tuserVerification: 'required',\n\t\t\t\t\textensions: { prf: { eval: { first: PRF_INPUT } } },\n\t\t\t\t\ttimeout: timeoutMs,\n\t\t\t\t\t...(credentialId != null && {\n\t\t\t\t\t\tallowCredentials: [{ type: 'public-key', id: new Uint8Array(credentialId) }],\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t}),\n\t\t'The passkey prompt was closed. Try again to unlock your wallet.',\n\t);\n\tconst results = credential.getClientExtensionResults().prf?.results;\n\n\tif (results == null) {\n\t\tthrow new PasskeyPrfUnsupportedError(PRF_UNSUPPORTED);\n\t}\n\n\treturn { credentialId: new Uint8Array(credential.rawId), prfOutput: toBytes(results.first) };\n}\n\nasync function requestCredential(\n\trequest: () => Promise<Credential | null>,\n\tcancelledMessage: string,\n) {\n\tlet credential: Credential | null;\n\ttry {\n\t\tcredential = await request();\n\t} catch (error) {\n\t\t// Browsers reject with NotAllowedError when the prompt is closed, refused or times out.\n\t\tif (error instanceof DOMException && error.name === 'NotAllowedError') {\n\t\t\tthrow new PasskeyCancelledError(cancelledMessage, { cause: error });\n\t\t}\n\n\t\tthrow error;\n\t}\n\n\tif (credential == null) {\n\t\tthrow new PasskeyCancelledError(cancelledMessage);\n\t}\n\n\t// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- DOM types return Credential; publicKey options always yield a PublicKeyCredential.\n\treturn credential as PublicKeyCredential;\n}\n\nfunction toBytes(source: BufferSource) {\n\treturn ArrayBuffer.isView(source)\n\t\t? new Uint8Array(source.buffer, source.byteOffset, source.byteLength)\n\t\t: new Uint8Array(source);\n}\n"],"mappings":";;;;;;;;AAWA,MAAM,YAAY,YAAY,GAAG,UAAU,IAAI;AAE/C,MAAM,kBACL;;;;;AAeD,eAAsB,cACrB,SACA,QAAQ,QAAQ,QACS;CACzB,MAAM,EAAE,MAAM,QAAQ,cAAc;CACpC,MAAM,aAAa,MAAM,kBACxB,YACC,UAAU,YAAY,OAAO,EAC5B,WAAW;EACV,IAAI;GAAE,IAAI;GAAM,MAAM;EAAO;EAC7B,MAAM;GACL,IAAI,OAAO,gCAAgB,IAAI,WAAW,EAAE,CAAC;GAC7C,MAAM;GACN,aAAa;EACd;EAEA,WAAW,OAAO,gCAAgB,IAAI,WAAW,EAAE,CAAC;EACpD,kBAAkB,CACjB;GAAE,KAAK;GAAI,MAAM;EAAa,GAC9B;GAAE,KAAK;GAAM,MAAM;EAAa,CACjC;EACA,wBAAwB;GACvB,aAAa;GACb,oBAAoB;GACpB,kBAAkB;EACnB;EACA,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,UAAU,EAAE,EAAE;EAClD,SAAS;CACV,EACD,CAAC,GACF,kEACD;CACA,MAAM,eAAe,IAAI,WAAW,WAAW,KAAK;CACpD,MAAM,MAAM,WAAW,0BAA0B,CAAC,CAAC;CAEnD,IAAI,KAAK,WAAW,MACnB,OAAO;EAAE;EAAc,WAAW,QAAQ,IAAI,QAAQ,KAAK;CAAE;CAG9D,IAAI,KAAK,YAAY,MACpB,MAAM,IAAI,2BAA2B,eAAe;CAGrD,OAAO,iBAAiB,SAAS,YAAY;AAC9C;;;;;AAMA,eAAsB,iBACrB,EAAE,MAAM,aACR,cACyB;CACzB,MAAM,aAAa,MAAM,kBACxB,YACC,UAAU,YAAY,IAAI,EACzB,WAAW;EACV;EACA,WAAW,OAAO,gCAAgB,IAAI,WAAW,EAAE,CAAC;EACpD,kBAAkB;EAClB,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,UAAU,EAAE,EAAE;EAClD,SAAS;EACT,GAAI,gBAAgB,QAAQ,EAC3B,kBAAkB,CAAC;GAAE,MAAM;GAAc,IAAI,IAAI,WAAW,YAAY;EAAE,CAAC,EAC5E;CACD,EACD,CAAC,GACF,iEACD;CACA,MAAM,UAAU,WAAW,0BAA0B,CAAC,CAAC,KAAK;CAE5D,IAAI,WAAW,MACd,MAAM,IAAI,2BAA2B,eAAe;CAGrD,OAAO;EAAE,cAAc,IAAI,WAAW,WAAW,KAAK;EAAG,WAAW,QAAQ,QAAQ,KAAK;CAAE;AAC5F;AAEA,eAAe,kBACd,SACA,kBACC;CACD,IAAI;CACJ,IAAI;EACH,aAAa,MAAM,QAAQ;CAC5B,SAAS,OAAO;EAEf,IAAI,iBAAiB,gBAAgB,MAAM,SAAS,mBACnD,MAAM,IAAI,sBAAsB,kBAAkB,EAAE,OAAO,MAAM,CAAC;EAGnE,MAAM;CACP;CAEA,IAAI,cAAc,MACjB,MAAM,IAAI,sBAAsB,gBAAgB;CAIjD,OAAO;AACR;AAEA,SAAS,QAAQ,QAAsB;CACtC,OAAO,YAAY,OAAO,MAAM,IAC7B,IAAI,WAAW,OAAO,QAAQ,OAAO,YAAY,OAAO,UAAU,IAClE,IAAI,WAAW,MAAM;AACzB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { StandardConnectOutput } from "@mysten/wallet-standard";
|
|
2
|
+
/** Name of the feature that creates a new passkey and connects its wallet. */
|
|
3
|
+
export declare const PasskeyWalletCreate = "passkey-wallet:create";
|
|
4
|
+
/** The latest API version of the create API. */
|
|
5
|
+
export type PasskeyWalletCreateVersion = '1.0.0';
|
|
6
|
+
/** Options of `createPasskeyWallet()`. */
|
|
7
|
+
export type CreatePasskeyWalletOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* Name of the new passkey in the password manager, such as the user's email, so a user with
|
|
10
|
+
* several wallets can tell them apart. Defaults to `rpName`.
|
|
11
|
+
*/
|
|
12
|
+
label?: string;
|
|
13
|
+
};
|
|
14
|
+
export type PasskeyWalletCreateMethod = (input?: CreatePasskeyWalletOptions) => Promise<StandardConnectOutput>;
|
|
15
|
+
/** Wallet Standard feature that creates a new passkey, which gives a new address. */
|
|
16
|
+
export type PasskeyWalletCreateFeature = {
|
|
17
|
+
[PasskeyWalletCreate]: {
|
|
18
|
+
/** Version of the feature API. */
|
|
19
|
+
version: PasskeyWalletCreateVersion;
|
|
20
|
+
create: PasskeyWalletCreateMethod;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=features.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features.d.mts","names":[],"sources":["../../src/wallet/features.ts"],"mappings":";;qBAMa;;YAGD;;YAGA;;;;;EAKX;;YAGW,6BACX,QAAQ,+BACJ,QAAQ;;YAGD;GACV;;IAEA,SAAS;IACT,QAAQ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features.mjs","names":[],"sources":["../../src/wallet/features.ts"],"sourcesContent":["// Copyright (c) Sugar\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { StandardConnectOutput } from '@mysten/wallet-standard';\n\n/** Name of the feature that creates a new passkey and connects its wallet. */\nexport const PasskeyWalletCreate = 'passkey-wallet:create';\n\n/** The latest API version of the create API. */\nexport type PasskeyWalletCreateVersion = '1.0.0';\n\n/** Options of `createPasskeyWallet()`. */\nexport type CreatePasskeyWalletOptions = {\n\t/**\n\t * Name of the new passkey in the password manager, such as the user's email, so a user with\n\t * several wallets can tell them apart. Defaults to `rpName`.\n\t */\n\tlabel?: string;\n};\n\nexport type PasskeyWalletCreateMethod = (\n\tinput?: CreatePasskeyWalletOptions,\n) => Promise<StandardConnectOutput>;\n\n/** Wallet Standard feature that creates a new passkey, which gives a new address. */\nexport type PasskeyWalletCreateFeature = {\n\t[PasskeyWalletCreate]: {\n\t\t/** Version of the feature API. */\n\t\tversion: PasskeyWalletCreateVersion;\n\t\tcreate: PasskeyWalletCreateMethod;\n\t};\n};\n"],"mappings":";;AAMA,MAAa,sBAAsB"}
|