@qntx/wallet 0.0.0-stage → 0.2.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/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +73 -2
- package/dist/host.d.mts +54 -0
- package/dist/host.mjs +291 -0
- package/dist/host.mjs.map +1 -0
- package/dist/index-BJfyIwOZ.d.mts +286 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/nostr.d.mts +47 -0
- package/dist/nostr.mjs +174 -0
- package/dist/nostr.mjs.map +1 -0
- package/dist/protocol-D9_0sY_Y.mjs +466 -0
- package/dist/protocol-D9_0sY_Y.mjs.map +1 -0
- package/dist/protocol.d.mts +3 -0
- package/dist/protocol.mjs +2 -0
- package/dist/secret-B-P3QOym.mjs +375 -0
- package/dist/secret-B-P3QOym.mjs.map +1 -0
- package/dist/types-CEy7At1l.d.mts +168 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +16 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +97 -4
package/LICENSE-APACHE
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/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 QuantX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @qntx/wallet
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Public API surface of the qntx wallet: the `Wallet` interface, the immutable `WalletSnapshot`,
|
|
4
|
+
`WalletError`, the core API groups (vault, wallets, dataKeys), the host/kernel wire protocol, and
|
|
5
|
+
the `@qntx/wallet/nostr` subpath — the first protocol module. This package contains no runtime of
|
|
6
|
+
its own — a `Wallet` comes from a host package that spawns the kernel, currently
|
|
7
|
+
[`@qntx/wallet-worker`](https://www.npmjs.com/package/@qntx/wallet-worker) for Web and Node.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @qntx/wallet @qntx/wallet-worker
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createWebWallet } from "@qntx/wallet-worker/browser";
|
|
17
|
+
|
|
18
|
+
const wallet = await createWebWallet({
|
|
19
|
+
passkey: {
|
|
20
|
+
rpId: "app.example.com", // the origin's effective domain or a registrable parent
|
|
21
|
+
rpName: "Example",
|
|
22
|
+
walletSalt: "example/wallet",
|
|
23
|
+
vaultSalt: "example/vault",
|
|
24
|
+
},
|
|
25
|
+
autoLock: { platform: "web", idleMinutes: 15 },
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// First run: create the wallet and register its passkey.
|
|
29
|
+
const created = await wallet.wallets.createWithPasskey({ name: "Example Wallet" });
|
|
30
|
+
|
|
31
|
+
// Later runs: unlock with the passkey (or a password factor).
|
|
32
|
+
await wallet.vault.unlock({ kind: "passkey" });
|
|
33
|
+
|
|
34
|
+
// The Nostr module lives in its own subpath — the root entry never pulls @qntx/nostr.
|
|
35
|
+
import { nostr } from "@qntx/wallet/nostr";
|
|
36
|
+
|
|
37
|
+
const key = await nostr(wallet).deriveKey(created.walletId, 0);
|
|
38
|
+
// A proxy NostrSigner — resolves the kernel key per call and survives lock/unlock.
|
|
39
|
+
const signer = nostr(wallet).signer(key.ref);
|
|
40
|
+
const event = await signer.signEvent({
|
|
41
|
+
kind: 1,
|
|
42
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
43
|
+
tags: [],
|
|
44
|
+
content: "gm",
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### React
|
|
49
|
+
|
|
50
|
+
`getSnapshot` and `subscribe` are bound properties, safe to pass unbound:
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { useSyncExternalStore } from "react";
|
|
54
|
+
|
|
55
|
+
const snapshot = useSyncExternalStore(wallet.subscribe, wallet.getSnapshot);
|
|
56
|
+
// snapshot.vault: "uninitialized" | "locked" | "unlocked" | "corrupted"
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Security notes
|
|
60
|
+
|
|
61
|
+
- Secrets (seed, keys, DEK, PRF output) exist only inside the kernel's Web Worker; the host
|
|
62
|
+
thread sees ciphertext and public headers only.
|
|
63
|
+
- The Web host is a **hot wallet**: it protects against the host page reading secrets, not
|
|
64
|
+
against a compromised browser or device.
|
|
65
|
+
- Your CSP must allow the bundled worker: `worker-src 'self'` (the worker loads from your own
|
|
66
|
+
origin — a stricter policy breaks the wallet).
|
|
67
|
+
- Vite users: set `optimizeDeps.exclude: ["@qntx/wallet-worker"]` so the worker URL survives dev.
|
|
68
|
+
- `@qntx/wallet/host` is reserved for host packages — applications must not use it.
|
|
69
|
+
- A vault that reports `corrupted` cannot be unlocked; `wallet.vault.reset()` is the only way
|
|
70
|
+
forward and destroys the stored vault.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT OR Apache-2.0
|
package/dist/host.d.mts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { d as PasskeyConfig, h as Wallet } from "./types-CEy7At1l.mjs";
|
|
2
|
+
import { l as ProtocolValue } from "./index-BJfyIwOZ.mjs";
|
|
3
|
+
//#region src/channel.d.ts
|
|
4
|
+
/** The host packages' side of the kernel channel (worker, JSI runtime, in-process test port). */
|
|
5
|
+
type MainPort = {
|
|
6
|
+
readonly post: (message: ProtocolValue, transfer: ArrayBuffer[]) => void;
|
|
7
|
+
readonly listen: (handler: (message: unknown) => void) => () => void;
|
|
8
|
+
readonly onExit: (handler: () => void) => () => void;
|
|
9
|
+
readonly terminate: () => void;
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/factors.d.ts
|
|
13
|
+
type PrfSalts = {
|
|
14
|
+
readonly first: Uint8Array;
|
|
15
|
+
readonly second?: Uint8Array;
|
|
16
|
+
};
|
|
17
|
+
type PrfResult = {
|
|
18
|
+
readonly credentialId: Uint8Array;
|
|
19
|
+
readonly first: Uint8Array;
|
|
20
|
+
readonly second?: Uint8Array;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The WebAuthn ceremony the host performs. Implementations throw `WalletError` — "cancelled" when
|
|
24
|
+
* the user aborts, "factor-unavailable" when the credential cannot produce a PRF. `name` is the new
|
|
25
|
+
* credential's display name, validated by the host before the call (1–64 characters).
|
|
26
|
+
*/
|
|
27
|
+
type PasskeyCeremony = {
|
|
28
|
+
readonly create: (input: {
|
|
29
|
+
readonly purpose: "wallet" | "vault";
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly salts: PrfSalts;
|
|
32
|
+
}) => Promise<PrfResult>;
|
|
33
|
+
readonly assert: (input: {
|
|
34
|
+
readonly credentialIds?: ReadonlyArray<Uint8Array>;
|
|
35
|
+
readonly salts: PrfSalts;
|
|
36
|
+
}) => Promise<PrfResult>;
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/wallet.d.ts
|
|
40
|
+
type ConnectOptions = {
|
|
41
|
+
readonly spawn: () => MainPort;
|
|
42
|
+
readonly passkey: PasskeyConfig;
|
|
43
|
+
readonly ceremony: PasskeyCeremony | undefined;
|
|
44
|
+
/** Runs once inside `destroy()` — host-side cleanup (page listeners, timers). */
|
|
45
|
+
readonly onDestroy?: () => void;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Connects a kernel spawned by the host package and returns the host-facing `Wallet`. The first
|
|
49
|
+
* spawn must deliver a status event before this resolves.
|
|
50
|
+
*/
|
|
51
|
+
export declare function connectKernel(options: ConnectOptions): Promise<Wallet>;
|
|
52
|
+
//#endregion
|
|
53
|
+
export type { ConnectOptions, MainPort, PasskeyCeremony, PrfResult, PrfSalts };
|
|
54
|
+
//# sourceMappingURL=host.d.mts.map
|
package/dist/host.mjs
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { WalletError } from "./types.mjs";
|
|
2
|
+
import { a as requireCeremony, c as vaultSaltBytes, d as wireSecretText, f as Channel, i as passkeyName, l as walletSaltBytes, o as resolveFactor, r as registerInternals, s as resolveInitialFactor, t as SingleUseSecret, u as wireBytes } from "./secret-B-P3QOym.mjs";
|
|
3
|
+
//#region src/wallet.ts
|
|
4
|
+
/**
|
|
5
|
+
* The host-facing `Wallet` (design/entry.mdx "API"): snapshot state, the core API groups, and the
|
|
6
|
+
* glue between the kernel `Channel` and factor resolution. `connectKernel` is exported under the
|
|
7
|
+
* `./host` subpath — reserved for host packages; applications must not use it. Protocol modules
|
|
8
|
+
* (@qntx/wallet/nostr and future siblings) reach this wallet's channel through the registered
|
|
9
|
+
* internals in internal.ts — never through the public type.
|
|
10
|
+
*/
|
|
11
|
+
/** Structural equality over plain snapshot data (validated protocol payloads only). */
|
|
12
|
+
function deepEqual(a, b) {
|
|
13
|
+
if (a === b) return true;
|
|
14
|
+
if (typeof a !== "object" || a === null || typeof b !== "object" || b === null) return false;
|
|
15
|
+
if (Array.isArray(a) || Array.isArray(b)) return Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((item, index) => deepEqual(item, b[index]));
|
|
16
|
+
const aKeys = Object.keys(a);
|
|
17
|
+
const bKeys = Object.keys(b);
|
|
18
|
+
return aKeys.length === bKeys.length && aKeys.every((key) => key in b && deepEqual(a[key], b[key]));
|
|
19
|
+
}
|
|
20
|
+
var KernelWallet = class KernelWallet {
|
|
21
|
+
channel;
|
|
22
|
+
passkeyCfg;
|
|
23
|
+
ceremony;
|
|
24
|
+
onDestroy;
|
|
25
|
+
destroyed = false;
|
|
26
|
+
status;
|
|
27
|
+
lockReason;
|
|
28
|
+
verifying = 0;
|
|
29
|
+
snapshotValue;
|
|
30
|
+
listeners = /* @__PURE__ */ new Set();
|
|
31
|
+
constructor(options) {
|
|
32
|
+
this.passkeyCfg = options.passkey;
|
|
33
|
+
this.ceremony = options.ceremony;
|
|
34
|
+
this.onDestroy = options.onDestroy;
|
|
35
|
+
this.snapshotValue = {
|
|
36
|
+
vault: "uninitialized",
|
|
37
|
+
lockReason: void 0,
|
|
38
|
+
verifying: false,
|
|
39
|
+
factors: [],
|
|
40
|
+
wallets: [],
|
|
41
|
+
nostr: { keys: [] }
|
|
42
|
+
};
|
|
43
|
+
this.channel = new Channel(options.spawn, {
|
|
44
|
+
onStatus: (status) => {
|
|
45
|
+
this.onStatus(status);
|
|
46
|
+
},
|
|
47
|
+
onLocked: (reason) => {
|
|
48
|
+
this.lockReason = reason;
|
|
49
|
+
this.emit();
|
|
50
|
+
},
|
|
51
|
+
onCrashed: () => {
|
|
52
|
+
this.lockReason = "crashed";
|
|
53
|
+
this.emit();
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
registerInternals(this, {
|
|
57
|
+
call: async (method, params, transfer) => this.channel.call(method, params, transfer),
|
|
58
|
+
ready: async () => this.channel.ready,
|
|
59
|
+
snapshot: () => this.snapshotValue,
|
|
60
|
+
withVerifying: async (fn) => this.withVerifying(fn),
|
|
61
|
+
resolveFactor: async (factor, transfer) => resolveFactor(this.factorSource, factor, transfer),
|
|
62
|
+
createFactorParam: async (factor, transfer) => this.createFactorParam(factor, transfer),
|
|
63
|
+
sessionOf: (result) => {
|
|
64
|
+
this.sessionOf(result);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
static async connect(options) {
|
|
69
|
+
const wallet = new KernelWallet(options);
|
|
70
|
+
await wallet.channel.start();
|
|
71
|
+
return wallet;
|
|
72
|
+
}
|
|
73
|
+
onStatus(status) {
|
|
74
|
+
if (this.lockReason === void 0 && status.vault === "locked") this.lockReason = "cold-start";
|
|
75
|
+
this.status = status;
|
|
76
|
+
this.emit();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Rebuilds the immutable snapshot and notifies subscribers — only when the content actually
|
|
80
|
+
* changed (identical status events produce no new reference and no notification).
|
|
81
|
+
*/
|
|
82
|
+
emit() {
|
|
83
|
+
const { status } = this;
|
|
84
|
+
const next = {
|
|
85
|
+
vault: status === void 0 ? "uninitialized" : status.vault,
|
|
86
|
+
lockReason: this.lockReason,
|
|
87
|
+
verifying: this.verifying > 0,
|
|
88
|
+
factors: status === void 0 ? [] : status.factors,
|
|
89
|
+
wallets: status === void 0 ? [] : status.wallets,
|
|
90
|
+
nostr: { keys: status === void 0 ? [] : status.nostr.keys }
|
|
91
|
+
};
|
|
92
|
+
const current = this.snapshotValue;
|
|
93
|
+
if (current.vault === next.vault && current.lockReason === next.lockReason && current.verifying === next.verifying && deepEqual(current.factors, next.factors) && deepEqual(current.wallets, next.wallets) && deepEqual(current.nostr, next.nostr)) return;
|
|
94
|
+
this.snapshotValue = next;
|
|
95
|
+
for (const listener of this.listeners) listener();
|
|
96
|
+
}
|
|
97
|
+
getSnapshot = () => this.snapshotValue;
|
|
98
|
+
subscribe = (listener) => {
|
|
99
|
+
this.listeners.add(listener);
|
|
100
|
+
return () => {
|
|
101
|
+
this.listeners.delete(listener);
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
get factorSource() {
|
|
105
|
+
return {
|
|
106
|
+
passkey: this.passkeyCfg,
|
|
107
|
+
ceremony: this.ceremony,
|
|
108
|
+
factors: () => this.snapshotValue.factors
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/** True while a ceremony or a factor-bearing request is in flight; drives `verifying`. */
|
|
112
|
+
async withVerifying(fn) {
|
|
113
|
+
this.verifying++;
|
|
114
|
+
this.emit();
|
|
115
|
+
try {
|
|
116
|
+
return await fn();
|
|
117
|
+
} finally {
|
|
118
|
+
this.verifying--;
|
|
119
|
+
this.emit();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
initialized() {
|
|
123
|
+
return this.snapshotValue.vault !== "uninitialized";
|
|
124
|
+
}
|
|
125
|
+
/** The factor gate on create/import commands; keeps useless ceremonies from firing. */
|
|
126
|
+
async createFactorParam(factor, transfer) {
|
|
127
|
+
if (this.initialized()) {
|
|
128
|
+
if (factor !== void 0) throw new WalletError("invalid-input", "the vault is already initialized");
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (factor === void 0) throw new WalletError("invalid-input", "an initial factor is required");
|
|
132
|
+
return resolveInitialFactor(this.factorSource, factor, transfer);
|
|
133
|
+
}
|
|
134
|
+
sessionOf(result) {
|
|
135
|
+
this.channel.session = result.session;
|
|
136
|
+
}
|
|
137
|
+
vault = {
|
|
138
|
+
unlock: async (factor) => this.withVerifying(async () => {
|
|
139
|
+
const transfer = [];
|
|
140
|
+
const wire = await resolveFactor(this.factorSource, factor ?? { kind: "passkey" }, transfer);
|
|
141
|
+
const result = await this.channel.call("vault.unlock", { factor: wire }, transfer);
|
|
142
|
+
this.sessionOf(result);
|
|
143
|
+
}),
|
|
144
|
+
lock: async () => {
|
|
145
|
+
try {
|
|
146
|
+
await this.channel.call("vault.lock", null);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (!(error instanceof WalletError) || error.code !== "locked") throw error;
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
addFactor: async (factor, verify) => this.withVerifying(async () => {
|
|
152
|
+
const transfer = [];
|
|
153
|
+
const wireVerify = await resolveFactor(this.factorSource, verify, transfer);
|
|
154
|
+
const wireFactor = await resolveInitialFactor(this.factorSource, factor, transfer);
|
|
155
|
+
return this.channel.call("vault.addFactor", {
|
|
156
|
+
verify: wireVerify,
|
|
157
|
+
factor: wireFactor
|
|
158
|
+
}, transfer);
|
|
159
|
+
}),
|
|
160
|
+
removeFactor: async (id, verify) => this.withVerifying(async () => {
|
|
161
|
+
const transfer = [];
|
|
162
|
+
const wireVerify = await resolveFactor(this.factorSource, verify, transfer);
|
|
163
|
+
await this.channel.call("vault.removeFactor", {
|
|
164
|
+
verify: wireVerify,
|
|
165
|
+
id
|
|
166
|
+
}, transfer);
|
|
167
|
+
}),
|
|
168
|
+
reset: async () => {
|
|
169
|
+
await this.channel.call("vault.reset", null);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
wallets = {
|
|
173
|
+
createWithPasskey: async (options) => this.withVerifying(async () => {
|
|
174
|
+
const ceremony = requireCeremony(this.factorSource);
|
|
175
|
+
const name = passkeyName(options.name);
|
|
176
|
+
const transfer = [];
|
|
177
|
+
const result = await ceremony.create({
|
|
178
|
+
purpose: "wallet",
|
|
179
|
+
name,
|
|
180
|
+
salts: {
|
|
181
|
+
first: walletSaltBytes(this.factorSource),
|
|
182
|
+
second: vaultSaltBytes(this.factorSource)
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
let factor;
|
|
186
|
+
if (this.initialized()) {
|
|
187
|
+
if (options.factor !== void 0) throw new WalletError("invalid-input", "the vault is already initialized");
|
|
188
|
+
result.second?.fill(0);
|
|
189
|
+
} else factor = await resolveInitialFactor(this.factorSource, options.factor, transfer, result);
|
|
190
|
+
const created = await this.channel.call("wallet.createWithPasskey", {
|
|
191
|
+
credentialId: wireBytes(result.credentialId, transfer),
|
|
192
|
+
prfWallet: wireBytes(result.first, transfer, true),
|
|
193
|
+
...factor === void 0 ? {} : { factor }
|
|
194
|
+
}, transfer);
|
|
195
|
+
this.sessionOf(created);
|
|
196
|
+
return created.wallet;
|
|
197
|
+
}),
|
|
198
|
+
restoreWithPasskey: async (options) => this.withVerifying(async () => {
|
|
199
|
+
const ceremony = requireCeremony(this.factorSource);
|
|
200
|
+
const transfer = [];
|
|
201
|
+
const result = await ceremony.assert({ salts: {
|
|
202
|
+
first: walletSaltBytes(this.factorSource),
|
|
203
|
+
second: vaultSaltBytes(this.factorSource)
|
|
204
|
+
} });
|
|
205
|
+
let factor;
|
|
206
|
+
if (this.initialized()) {
|
|
207
|
+
if (options?.factor !== void 0) throw new WalletError("invalid-input", "the vault is already initialized");
|
|
208
|
+
result.second?.fill(0);
|
|
209
|
+
} else factor = await resolveInitialFactor(this.factorSource, options?.factor, transfer, result);
|
|
210
|
+
const created = await this.channel.call("wallet.restoreWithPasskey", {
|
|
211
|
+
credentialId: wireBytes(result.credentialId, transfer),
|
|
212
|
+
prfWallet: wireBytes(result.first, transfer, true),
|
|
213
|
+
...factor === void 0 ? {} : { factor }
|
|
214
|
+
}, transfer);
|
|
215
|
+
this.sessionOf(created);
|
|
216
|
+
return created.wallet;
|
|
217
|
+
}),
|
|
218
|
+
createMnemonic: async (options) => {
|
|
219
|
+
const transfer = [];
|
|
220
|
+
const factor = await this.createFactorParam(options.factor, transfer);
|
|
221
|
+
const created = await this.channel.call("wallet.createMnemonic", {
|
|
222
|
+
words: options.words,
|
|
223
|
+
...factor === void 0 ? {} : { factor }
|
|
224
|
+
}, transfer);
|
|
225
|
+
const draftId = created.draft;
|
|
226
|
+
return {
|
|
227
|
+
words: new SingleUseSecret(created.words),
|
|
228
|
+
quiz: created.quiz,
|
|
229
|
+
confirm: async (answers) => {
|
|
230
|
+
const answerTransfer = [];
|
|
231
|
+
const encoded = answers.map((word) => wireSecretText(word, answerTransfer));
|
|
232
|
+
const confirmed = await this.channel.call("wallet.confirmMnemonic", {
|
|
233
|
+
draft: draftId,
|
|
234
|
+
answers: encoded
|
|
235
|
+
}, answerTransfer);
|
|
236
|
+
this.sessionOf(confirmed);
|
|
237
|
+
return confirmed.wallet;
|
|
238
|
+
},
|
|
239
|
+
discard: async () => {
|
|
240
|
+
await this.channel.call("wallet.discardMnemonic", { draft: draftId });
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
},
|
|
244
|
+
importMnemonic: async (options) => {
|
|
245
|
+
const transfer = [];
|
|
246
|
+
const factor = await this.createFactorParam(options.factor, transfer);
|
|
247
|
+
const mnemonic = wireSecretText(options.mnemonic, transfer);
|
|
248
|
+
const created = await this.channel.call("wallet.importMnemonic", {
|
|
249
|
+
mnemonic,
|
|
250
|
+
...factor === void 0 ? {} : { factor }
|
|
251
|
+
}, transfer);
|
|
252
|
+
this.sessionOf(created);
|
|
253
|
+
return created.wallet;
|
|
254
|
+
},
|
|
255
|
+
exportMnemonic: async (walletId, verify) => this.withVerifying(async () => {
|
|
256
|
+
const transfer = [];
|
|
257
|
+
const wireVerify = await resolveFactor(this.factorSource, verify, transfer);
|
|
258
|
+
const bytes = await this.channel.call("wallet.exportMnemonic", {
|
|
259
|
+
verify: wireVerify,
|
|
260
|
+
walletId
|
|
261
|
+
}, transfer);
|
|
262
|
+
return new SingleUseSecret(bytes);
|
|
263
|
+
}),
|
|
264
|
+
delete: async (walletId, verify) => this.withVerifying(async () => {
|
|
265
|
+
const transfer = [];
|
|
266
|
+
const wireVerify = await resolveFactor(this.factorSource, verify, transfer);
|
|
267
|
+
await this.channel.call("wallet.delete", {
|
|
268
|
+
verify: wireVerify,
|
|
269
|
+
walletId
|
|
270
|
+
}, transfer);
|
|
271
|
+
})
|
|
272
|
+
};
|
|
273
|
+
dataKeys = { get: async (purpose) => this.channel.call("dataKeys.get", { purpose }) };
|
|
274
|
+
async destroy() {
|
|
275
|
+
if (this.destroyed) return;
|
|
276
|
+
this.destroyed = true;
|
|
277
|
+
this.onDestroy?.();
|
|
278
|
+
this.channel.destroy();
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
/**
|
|
282
|
+
* Connects a kernel spawned by the host package and returns the host-facing `Wallet`. The first
|
|
283
|
+
* spawn must deliver a status event before this resolves.
|
|
284
|
+
*/
|
|
285
|
+
async function connectKernel(options) {
|
|
286
|
+
return KernelWallet.connect(options);
|
|
287
|
+
}
|
|
288
|
+
//#endregion
|
|
289
|
+
export { connectKernel };
|
|
290
|
+
|
|
291
|
+
//# sourceMappingURL=host.mjs.map
|