@qntx/wallet 0.5.2 → 0.7.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/dist/chains-CuKVAJMN.d.mts +333 -0
- package/dist/chains.d.mts +2 -4
- package/dist/chains.mjs +3 -4
- package/dist/{channel-ChYtMklb.mjs → channel-Iiz28m-e.mjs} +6 -7
- package/dist/{channel-ChYtMklb.mjs.map → channel-Iiz28m-e.mjs.map} +1 -1
- package/dist/{core-Dmz5SbyJ.mjs → core-Cq5h9X0X.mjs} +11 -6
- package/dist/core-Cq5h9X0X.mjs.map +1 -0
- package/dist/host.d.mts +2 -8
- package/dist/host.mjs +18 -23
- package/dist/host.mjs.map +1 -1
- package/dist/{chains-D_jfwTPT.d.mts → index-CIZzbqqv.d.mts} +42 -402
- package/dist/index.d.mts +2 -6
- package/dist/index.mjs +3 -6
- package/dist/index.mjs.map +1 -1
- package/dist/{internal-LBHal13j.mjs → internal-CAKfHjIg.mjs} +2 -3
- package/dist/{internal-LBHal13j.mjs.map → internal-CAKfHjIg.mjs.map} +1 -1
- package/dist/nostr.d.mts +19 -22
- package/dist/nostr.mjs +59 -51
- package/dist/nostr.mjs.map +1 -1
- package/dist/{protocol-DvzlUKB8.mjs → protocol-DbpLncEB.mjs} +81 -117
- package/dist/protocol-DbpLncEB.mjs.map +1 -0
- package/dist/protocol.d.mts +4 -3
- package/dist/protocol.mjs +2 -2
- package/dist/{secret-BiyX0Hus.mjs → secret-Dz_ItVt6.mjs} +3 -5
- package/dist/{secret-BiyX0Hus.mjs.map → secret-Dz_ItVt6.mjs.map} +1 -1
- package/dist/spark.d.mts +2 -6
- package/dist/spark.mjs +4 -7
- package/dist/spark.mjs.map +1 -1
- package/dist/{types-BD9fuAUj.d.mts → types-BgFsXgyI.d.mts} +5 -19
- package/dist/types.d.mts +1 -1
- package/dist/types.mjs +1 -2
- package/dist/types.mjs.map +1 -1
- package/package.json +4 -4
- package/dist/core-Dmz5SbyJ.mjs.map +0 -1
- package/dist/protocol-DvzlUKB8.mjs.map +0 -1
package/dist/nostr.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { a as DerivedNostrKey, b as WalletErrorCode,
|
|
2
|
-
import { Event, NostrSigner } from "@qntx/nostr";
|
|
1
|
+
import { a as DerivedNostrKey, b as WalletErrorCode, d as NostrKeyInfo, f as NostrKeyRef, h as UnlockFactor, o as ExportedSecret, v as Wallet } from "./types-BgFsXgyI.mjs";
|
|
2
|
+
import { Event, NostrSigner, UnsignedEvent } from "@qntx/nostr";
|
|
3
3
|
import { Rumor } from "@qntx/nostr/nips/nip59";
|
|
4
|
-
//#region src/nostr/types.d.ts
|
|
5
4
|
type NostrUnwrapResult = {
|
|
6
5
|
readonly ok: Rumor;
|
|
7
6
|
} | {
|
|
@@ -11,11 +10,17 @@ type NostrUnwrapResult = {
|
|
|
11
10
|
type WalletSigner = {
|
|
12
11
|
readonly unwrapGiftWraps: (events: ReadonlyArray<Event>) => Promise<ReadonlyArray<Rumor | Error>>;
|
|
13
12
|
} & NostrSigner;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
/**
|
|
14
|
+
* The wallet metadata sync signer (WD-076): one dedicated key per wallet, derived at the reserved
|
|
15
|
+
* NIP-06 segment `m/44'/1237'/2147483647'/0/0`. It authors and seals only kind-30078 metadata
|
|
16
|
+
* events; `getPublicKey`/`signEvent`/`encrypt`/`decrypt` go through the kernel's nostr.sync*
|
|
17
|
+
* methods with the same forged-event checks as a `WalletSigner`.
|
|
18
|
+
*/
|
|
19
|
+
type WalletSyncSigner = {
|
|
20
|
+
readonly getPublicKey: () => Promise<string>;
|
|
21
|
+
readonly signEvent: (unsigned: UnsignedEvent) => Promise<Event>;
|
|
22
|
+
readonly encrypt: (text: string) => Promise<string>;
|
|
23
|
+
readonly decrypt: (payload: string) => Promise<string>;
|
|
19
24
|
};
|
|
20
25
|
type NostrExportKeyFormat = {
|
|
21
26
|
readonly kind: "nsec";
|
|
@@ -25,23 +30,15 @@ type NostrExportKeyFormat = {
|
|
|
25
30
|
};
|
|
26
31
|
type NostrApi = {
|
|
27
32
|
readonly signer: (ref: NostrKeyRef) => WalletSigner;
|
|
28
|
-
/**
|
|
33
|
+
/** The wallet's dedicated metadata sync signer — seed-derived, never in `nostr.keys`. */
|
|
34
|
+
readonly syncSigner: (walletId: string) => WalletSyncSigner;
|
|
35
|
+
/** Public keys a wallet's identity indices derive — discovery before deriveKey. */
|
|
29
36
|
readonly discover: (walletId: string, from: number, count: number) => Promise<ReadonlyArray<DerivedNostrKey>>;
|
|
30
|
-
readonly deriveKey: (walletId: string,
|
|
31
|
-
readonly
|
|
32
|
-
readonly factor?: InitialFactor;
|
|
33
|
-
}) => Promise<NostrKeyInfo>;
|
|
34
|
-
readonly generateKey: (options?: {
|
|
35
|
-
readonly factor?: InitialFactor;
|
|
36
|
-
}) => Promise<NostrKeyInfo>;
|
|
37
|
-
readonly createNip46ClientKey: () => Promise<NostrKeyInfo>;
|
|
38
|
-
readonly removeKey: (ref: NostrKeyRef, verify?: UnlockFactor) => Promise<void>;
|
|
37
|
+
readonly deriveKey: (walletId: string, index: number) => Promise<NostrKeyInfo>;
|
|
38
|
+
readonly removeKey: (ref: NostrKeyRef) => Promise<void>;
|
|
39
39
|
/** One-shot key export; requires re-verification every call. */
|
|
40
40
|
readonly exportKey: (ref: NostrKeyRef, format: NostrExportKeyFormat, verify: UnlockFactor) => Promise<ExportedSecret>;
|
|
41
41
|
};
|
|
42
|
-
//#endregion
|
|
43
|
-
//#region src/nostr/index.d.ts
|
|
44
42
|
export declare function nostr(wallet: Wallet): NostrApi;
|
|
45
|
-
|
|
46
|
-
export type { DerivedNostrKey, NostrApi, NostrExportKeyFormat, NostrImportKeyInput, NostrKeyInfo, NostrKeyRef, NostrUnwrapResult, WalletSigner };
|
|
43
|
+
export type { DerivedNostrKey, NostrApi, NostrExportKeyFormat, NostrKeyInfo, NostrKeyRef, NostrUnwrapResult, WalletSigner, WalletSyncSigner };
|
|
47
44
|
//# sourceMappingURL=nostr.d.mts.map
|
package/dist/nostr.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { WalletError } from "./types.mjs";
|
|
2
|
-
import { n as integrity } from "./channel-
|
|
3
|
-
import { t as internalsOf } from "./internal-
|
|
4
|
-
import { m as wireSecretText, t as SingleUseSecret } from "./secret-
|
|
2
|
+
import { n as integrity } from "./channel-Iiz28m-e.mjs";
|
|
3
|
+
import { t as internalsOf } from "./internal-CAKfHjIg.mjs";
|
|
4
|
+
import { m as wireSecretText, t as SingleUseSecret } from "./secret-Dz_ItVt6.mjs";
|
|
5
5
|
import { signedMatchesUnsigned, verifyEvent } from "@qntx/nostr";
|
|
6
|
-
|
|
6
|
+
// src/nostr/signer.ts
|
|
7
7
|
/**
|
|
8
8
|
* The proxy NostrSigner: the ref resolves against the CURRENT snapshot at every call, so the same
|
|
9
9
|
* signer object keeps working across lock/unlock and worker respawns. Returned events are checked
|
|
@@ -14,7 +14,7 @@ import { signedMatchesUnsigned, verifyEvent } from "@qntx/nostr";
|
|
|
14
14
|
function keyInfoOf(internals, ref) {
|
|
15
15
|
const info = internals.snapshot().nostr.keys.find((key) => {
|
|
16
16
|
const k = key.ref;
|
|
17
|
-
return ref.kind === "derived" && k.
|
|
17
|
+
return ref.kind === "derived" && k.walletId === ref.walletId && k.index === ref.index;
|
|
18
18
|
});
|
|
19
19
|
if (info === void 0) throw new WalletError("not-found", "no such nostr key");
|
|
20
20
|
return info;
|
|
@@ -89,17 +89,62 @@ function proxySigner(ref, internals) {
|
|
|
89
89
|
}
|
|
90
90
|
};
|
|
91
91
|
}
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
/**
|
|
93
|
+
* The wallet metadata sync signer (WD-076): the key lives at the reserved NIP-06 segment and is
|
|
94
|
+
* sealed by `nostr.syncKey` on first use, so `getPublicKey` both returns and creates it. Signed
|
|
95
|
+
* events pass the same forged-event check as `proxySigner`.
|
|
96
|
+
*/
|
|
97
|
+
function syncSigner(walletId, internals) {
|
|
98
|
+
let cached;
|
|
99
|
+
const publicKey = async () => {
|
|
100
|
+
if (cached === void 0) cached = (await internals.call("nostr.syncKey", { walletId })).publicKey;
|
|
101
|
+
return cached;
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
getPublicKey: publicKey,
|
|
105
|
+
signEvent: async (unsigned) => {
|
|
106
|
+
const expected = await publicKey();
|
|
107
|
+
const event = {
|
|
108
|
+
kind: unsigned.kind,
|
|
109
|
+
created_at: unsigned.created_at,
|
|
110
|
+
tags: unsigned.tags,
|
|
111
|
+
content: unsigned.content
|
|
112
|
+
};
|
|
113
|
+
const signed = await internals.call("nostr.syncSign", {
|
|
114
|
+
walletId,
|
|
115
|
+
event
|
|
116
|
+
});
|
|
117
|
+
const requested = {
|
|
118
|
+
kind: unsigned.kind,
|
|
119
|
+
created_at: unsigned.created_at,
|
|
120
|
+
tags: unsigned.tags,
|
|
121
|
+
content: unsigned.content,
|
|
122
|
+
pubkey: expected
|
|
123
|
+
};
|
|
124
|
+
if (signed.pubkey !== expected || !signedMatchesUnsigned(signed, requested) || !verifyEvent(signed)) throw integrity("the kernel returned a forged event");
|
|
125
|
+
return signed;
|
|
126
|
+
},
|
|
127
|
+
encrypt: async (text) => internals.call("nostr.syncEncrypt", {
|
|
128
|
+
walletId,
|
|
129
|
+
text
|
|
130
|
+
}),
|
|
131
|
+
decrypt: async (payload) => internals.call("nostr.syncDecrypt", {
|
|
132
|
+
walletId,
|
|
133
|
+
text: payload
|
|
134
|
+
})
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
// src/nostr/index.ts
|
|
94
138
|
/**
|
|
95
139
|
* The nostr module of @qntx/wallet: `nostr(wallet)` returns the wallet's Nostr API — signer
|
|
96
|
-
* proxies, key discovery/derivation/
|
|
97
|
-
*
|
|
98
|
-
*
|
|
140
|
+
* proxies, key discovery/derivation/derivation, guarded export and removal. The API is memoized per
|
|
141
|
+
* wallet so repeated calls return the same object (stable React identity); a foreign object is
|
|
142
|
+
* rejected because only a Wallet from this package holds a registered channel.
|
|
99
143
|
*/
|
|
100
144
|
function createNostrApi(internals) {
|
|
101
145
|
return {
|
|
102
146
|
signer: (ref) => proxySigner(ref, internals),
|
|
147
|
+
syncSigner: (walletId) => syncSigner(walletId, internals),
|
|
103
148
|
discover: async (walletId, from, count) => {
|
|
104
149
|
return (await internals.call("nostr.discover", {
|
|
105
150
|
walletId,
|
|
@@ -107,49 +152,13 @@ function createNostrApi(internals) {
|
|
|
107
152
|
count
|
|
108
153
|
})).keys;
|
|
109
154
|
},
|
|
110
|
-
deriveKey: async (walletId,
|
|
155
|
+
deriveKey: async (walletId, index) => internals.call("nostr.deriveKey", {
|
|
111
156
|
walletId,
|
|
112
|
-
|
|
157
|
+
index
|
|
113
158
|
}),
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const factor = await internals.createFactorParam(options?.factor, transfer);
|
|
117
|
-
const wireInput = "nsec" in input ? {
|
|
118
|
-
kind: "nsec",
|
|
119
|
-
nsec: wireSecretText(input.nsec, transfer)
|
|
120
|
-
} : {
|
|
121
|
-
kind: "ncryptsec",
|
|
122
|
-
ncryptsec: input.ncryptsec,
|
|
123
|
-
password: wireSecretText(input.password, transfer)
|
|
124
|
-
};
|
|
125
|
-
const result = await internals.call("nostr.importKey", {
|
|
126
|
-
input: wireInput,
|
|
127
|
-
...factor === void 0 ? {} : { factor }
|
|
128
|
-
}, transfer);
|
|
129
|
-
internals.sessionOf(result);
|
|
130
|
-
return result.key;
|
|
131
|
-
},
|
|
132
|
-
generateKey: async (options) => {
|
|
133
|
-
const transfer = [];
|
|
134
|
-
const factor = await internals.createFactorParam(options?.factor, transfer);
|
|
135
|
-
const result = await internals.call("nostr.generateKey", factor === void 0 ? {} : { factor }, transfer);
|
|
136
|
-
internals.sessionOf(result);
|
|
137
|
-
return result.key;
|
|
159
|
+
removeKey: async (ref) => {
|
|
160
|
+
await internals.call("nostr.removeKey", { publicKey: keyInfoOf(internals, ref).publicKey });
|
|
138
161
|
},
|
|
139
|
-
createNip46ClientKey: async () => internals.call("nostr.createNip46ClientKey", null),
|
|
140
|
-
removeKey: async (ref, verify) => internals.withVerifying(async () => {
|
|
141
|
-
const info = keyInfoOf(internals, ref);
|
|
142
|
-
const transfer = [];
|
|
143
|
-
let wireVerify;
|
|
144
|
-
if (info.origin !== "nip46-client") {
|
|
145
|
-
if (verify === void 0) throw new WalletError("invalid-input", "factor verification is required");
|
|
146
|
-
wireVerify = await internals.resolveFactor(verify, transfer);
|
|
147
|
-
}
|
|
148
|
-
await internals.call("nostr.removeKey", {
|
|
149
|
-
publicKey: info.publicKey,
|
|
150
|
-
...wireVerify === void 0 ? {} : { verify: wireVerify }
|
|
151
|
-
}, transfer);
|
|
152
|
-
}),
|
|
153
162
|
exportKey: async (ref, format, verify) => internals.withVerifying(async () => {
|
|
154
163
|
const { publicKey } = keyInfoOf(internals, ref);
|
|
155
164
|
const transfer = [];
|
|
@@ -177,7 +186,6 @@ function nostr(wallet) {
|
|
|
177
186
|
}
|
|
178
187
|
return api;
|
|
179
188
|
}
|
|
180
|
-
//#endregion
|
|
181
189
|
export { nostr };
|
|
182
190
|
|
|
183
191
|
//# sourceMappingURL=nostr.mjs.map
|
package/dist/nostr.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nostr.mjs","names":[],"sources":["../src/nostr/signer.ts","../src/nostr/index.ts"],"sourcesContent":["/**\n * The proxy NostrSigner: the ref resolves against the CURRENT snapshot at every call, so the same\n * signer object keeps working across lock/unlock and worker respawns. Returned events are checked\n * against the request — a compromised kernel cannot make a valid signature over different content\n * pass as the requested event.\n */\nimport { signedMatchesUnsigned, verifyEvent } from \"@qntx/nostr\";\nimport type { UnsignedEvent } from \"@qntx/nostr\";\nimport type { Rumor } from \"@qntx/nostr/nips/nip59\";\n\nimport { integrity } from \"../channel.ts\";\nimport type { WalletInternals } from \"../internal.ts\";\nimport { MAX_BATCH } from \"../protocol/core.ts\";\nimport type { UnsignedEventInput } from \"../protocol/index.ts\";\nimport { WalletError } from \"../types.ts\";\nimport type { NostrKeyInfo, NostrKeyRef } from \"../types.ts\";\nimport type { WalletSigner } from \"./types.ts\";\n\n/** Resolves a public ref to its stored key record; missing refs are `not-found`. */\nexport function keyInfoOf(internals: WalletInternals, ref: NostrKeyRef): NostrKeyInfo {\n const info = internals.snapshot().nostr.keys.find((key) => {\n const k = key.ref;\n return (\n (ref.kind === \"derived\" &&\n k.kind === \"derived\" &&\n k.walletId === ref.walletId &&\n k.groupIndex === ref.groupIndex) ||\n (ref.kind === \"nip46-client\" && k.kind === \"nip46-client\" && k.id === ref.id) ||\n (ref.kind === \"standalone\" && k.kind === \"standalone\" && k.id === ref.id)\n );\n });\n if (info === undefined) {\n throw new WalletError(\"not-found\", \"no such nostr key\");\n }\n return info;\n}\n\nexport function proxySigner(ref: NostrKeyRef, internals: WalletInternals): WalletSigner {\n const resolvePublicKey = (): string => keyInfoOf(internals, ref).publicKey;\n return {\n getPublicKey: async () => {\n await internals.ready();\n return resolvePublicKey();\n },\n signEvent: async (unsigned) => {\n const publicKey = resolvePublicKey();\n // An absent pubkey must stay absent: `pubkey: undefined` is not a ProtocolValue and the\n // kernel would reject the params outright.\n const event: UnsignedEventInput =\n unsigned.pubkey === undefined\n ? {\n kind: unsigned.kind,\n created_at: unsigned.created_at,\n tags: unsigned.tags,\n content: unsigned.content,\n }\n : {\n kind: unsigned.kind,\n created_at: unsigned.created_at,\n tags: unsigned.tags,\n content: unsigned.content,\n pubkey: unsigned.pubkey,\n };\n const signed = await internals.call(\"nostr.signEvent\", { publicKey, event });\n // The kernel output must be the requested event, signed by the expected key.\n const requested: UnsignedEvent = {\n kind: unsigned.kind,\n created_at: unsigned.created_at,\n tags: unsigned.tags,\n content: unsigned.content,\n pubkey: publicKey,\n };\n if (\n signed.pubkey !== publicKey ||\n !signedMatchesUnsigned(signed, requested) ||\n !verifyEvent(signed)\n ) {\n throw integrity(\"the kernel returned a forged event\");\n }\n return signed;\n },\n nip04Encrypt: async (peer, plaintext) =>\n internals.call(\"nostr.nip04Encrypt\", {\n publicKey: resolvePublicKey(),\n peer,\n text: plaintext,\n }),\n nip04Decrypt: async (peer, ciphertext) =>\n internals.call(\"nostr.nip04Decrypt\", {\n publicKey: resolvePublicKey(),\n peer,\n text: ciphertext,\n }),\n nip44Encrypt: async (peer, plaintext) =>\n internals.call(\"nostr.nip44Encrypt\", {\n publicKey: resolvePublicKey(),\n peer,\n text: plaintext,\n }),\n nip44Decrypt: async (peer, payload) =>\n internals.call(\"nostr.nip44Decrypt\", {\n publicKey: resolvePublicKey(),\n peer,\n text: payload,\n }),\n unwrapGiftWraps: async (events) => {\n if (events.length === 0) {\n return [];\n }\n const publicKey = resolvePublicKey();\n const results: Array<Rumor | Error> = [];\n // The wire caps one batch at MAX_BATCH; chunk sequentially and concatenate in order —\n // other requests can interleave between chunks.\n for (let start = 0; start < events.length; start += MAX_BATCH) {\n // oxlint-disable-next-line eslint/no-await-in-loop -- sequential on purpose: chunks round-trip in order\n const batch = await internals.call(\"nostr.unwrapGiftWraps\", {\n publicKey,\n events: events.slice(start, start + MAX_BATCH),\n });\n for (const r of batch.results) {\n results.push(\"ok\" in r ? r.ok : new Error(r.err));\n }\n }\n return results;\n },\n };\n}\n","/**\n * The nostr module of @qntx/wallet: `nostr(wallet)` returns the wallet's Nostr API — signer\n * proxies, key discovery/derivation/import/generation, guarded export and removal. The API is\n * memoized per wallet so repeated calls return the same object (stable React identity); a foreign\n * object is rejected because only a Wallet from this package holds a registered channel.\n */\nimport { wireSecretText } from \"../factors.ts\";\nimport { internalsOf } from \"../internal.ts\";\nimport type { WalletInternals } from \"../internal.ts\";\nimport { SingleUseSecret } from \"../secret.ts\";\nimport { WalletError } from \"../types.ts\";\nimport type {\n DerivedNostrKey,\n ExportedSecret,\n InitialFactor,\n NostrKeyInfo,\n NostrKeyRef,\n UnlockFactor,\n Wallet,\n} from \"../types.ts\";\nimport { keyInfoOf, proxySigner } from \"./signer.ts\";\nimport type { NostrApi, NostrExportKeyFormat, NostrImportKeyInput, WalletSigner } from \"./types.ts\";\n\nexport type {\n DerivedNostrKey,\n NostrApi,\n NostrExportKeyFormat,\n NostrImportKeyInput,\n NostrKeyInfo,\n NostrKeyRef,\n NostrUnwrapResult,\n WalletSigner,\n} from \"./types.ts\";\n\nfunction createNostrApi(internals: WalletInternals): NostrApi {\n return {\n signer: (ref: NostrKeyRef): WalletSigner => proxySigner(ref, internals),\n\n discover: async (\n walletId: string,\n from: number,\n count: number,\n ): Promise<ReadonlyArray<DerivedNostrKey>> => {\n const result = await internals.call(\"nostr.discover\", { walletId, from, count });\n return result.keys;\n },\n\n deriveKey: async (walletId: string, groupIndex: number): Promise<NostrKeyInfo> =>\n internals.call(\"nostr.deriveKey\", { walletId, groupIndex }),\n\n importKey: async (\n input: NostrImportKeyInput,\n options?: { readonly factor?: InitialFactor },\n ): Promise<NostrKeyInfo> => {\n const transfer: ArrayBuffer[] = [];\n const factor = await internals.createFactorParam(options?.factor, transfer);\n const wireInput =\n \"nsec\" in input\n ? { kind: \"nsec\" as const, nsec: wireSecretText(input.nsec, transfer) }\n : {\n kind: \"ncryptsec\" as const,\n ncryptsec: input.ncryptsec,\n password: wireSecretText(input.password, transfer),\n };\n const result = await internals.call(\n \"nostr.importKey\",\n { input: wireInput, ...(factor === undefined ? {} : { factor }) },\n transfer,\n );\n internals.sessionOf(result);\n return result.key;\n },\n\n generateKey: async (options?: { readonly factor?: InitialFactor }): Promise<NostrKeyInfo> => {\n const transfer: ArrayBuffer[] = [];\n const factor = await internals.createFactorParam(options?.factor, transfer);\n const result = await internals.call(\n \"nostr.generateKey\",\n factor === undefined ? {} : { factor },\n transfer,\n );\n internals.sessionOf(result);\n return result.key;\n },\n\n createNip46ClientKey: async (): Promise<NostrKeyInfo> =>\n internals.call(\"nostr.createNip46ClientKey\", null),\n\n removeKey: async (ref: NostrKeyRef, verify?: UnlockFactor): Promise<void> =>\n internals.withVerifying(async () => {\n const info = keyInfoOf(internals, ref); // resolves ref → publicKey + origin\n const transfer: ArrayBuffer[] = [];\n let wireVerify;\n if (info.origin !== \"nip46-client\") {\n if (verify === undefined) {\n throw new WalletError(\"invalid-input\", \"factor verification is required\");\n }\n wireVerify = await internals.resolveFactor(verify, transfer);\n }\n await internals.call(\n \"nostr.removeKey\",\n {\n publicKey: info.publicKey,\n ...(wireVerify === undefined ? {} : { verify: wireVerify }),\n },\n transfer,\n );\n }),\n\n exportKey: async (\n ref: NostrKeyRef,\n format: NostrExportKeyFormat,\n verify: UnlockFactor,\n ): Promise<ExportedSecret> =>\n internals.withVerifying(async () => {\n const { publicKey } = keyInfoOf(internals, ref);\n const transfer: ArrayBuffer[] = [];\n const wireVerify = await internals.resolveFactor(verify, transfer);\n const wireFormat =\n format.kind === \"nsec\"\n ? { kind: \"nsec\" as const }\n : {\n kind: \"ncryptsec\" as const,\n password: wireSecretText(format.password, transfer),\n };\n const bytes = await internals.call(\n \"nostr.exportKey\",\n { verify: wireVerify, publicKey, format: wireFormat },\n transfer,\n );\n return new SingleUseSecret(bytes);\n }),\n };\n}\n\nconst apis = new WeakMap<Wallet, NostrApi>();\n\nexport function nostr(wallet: Wallet): NostrApi {\n const internals = internalsOf(wallet);\n let api = apis.get(wallet);\n if (api === undefined) {\n api = createNostrApi(internals);\n apis.set(wallet, api);\n }\n return api;\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,SAAgB,UAAU,WAA4B,KAAgC;CACpF,MAAM,OAAO,UAAU,SAAS,CAAC,CAAC,MAAM,KAAK,MAAM,QAAQ;EACzD,MAAM,IAAI,IAAI;EACd,OACG,IAAI,SAAS,aACZ,EAAE,SAAS,aACX,EAAE,aAAa,IAAI,YACnB,EAAE,eAAe,IAAI,cACtB,IAAI,SAAS,kBAAkB,EAAE,SAAS,kBAAkB,EAAE,OAAO,IAAI,MACzE,IAAI,SAAS,gBA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CACzB,IAAI,QAAQ,KAAA,GAAW;EACrB,MAAM,eAAe,SAAS;EAC9B,KAAK,IAAI,QAAQ,GAAG;CACtB;CACA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"nostr.mjs","names":[],"sources":["../src/nostr/signer.ts","../src/nostr/index.ts"],"sourcesContent":["/**\n * The proxy NostrSigner: the ref resolves against the CURRENT snapshot at every call, so the same\n * signer object keeps working across lock/unlock and worker respawns. Returned events are checked\n * against the request — a compromised kernel cannot make a valid signature over different content\n * pass as the requested event.\n */\nimport { signedMatchesUnsigned, verifyEvent } from \"@qntx/nostr\";\nimport type { UnsignedEvent } from \"@qntx/nostr\";\nimport type { Rumor } from \"@qntx/nostr/nips/nip59\";\n\nimport { integrity } from \"../channel.ts\";\nimport type { WalletInternals } from \"../internal.ts\";\nimport { MAX_BATCH } from \"../protocol/core.ts\";\nimport type { UnsignedEventInput } from \"../protocol/index.ts\";\nimport { WalletError } from \"../types.ts\";\nimport type { NostrKeyInfo, NostrKeyRef } from \"../types.ts\";\nimport type { WalletSigner, WalletSyncSigner } from \"./types.ts\";\n\n/** Resolves a public ref to its stored key record; missing refs are `not-found`. */\nexport function keyInfoOf(internals: WalletInternals, ref: NostrKeyRef): NostrKeyInfo {\n const info = internals.snapshot().nostr.keys.find((key) => {\n const k = key.ref;\n return ref.kind === \"derived\" && k.walletId === ref.walletId && k.index === ref.index;\n });\n if (info === undefined) {\n throw new WalletError(\"not-found\", \"no such nostr key\");\n }\n return info;\n}\n\nexport function proxySigner(ref: NostrKeyRef, internals: WalletInternals): WalletSigner {\n const resolvePublicKey = (): string => keyInfoOf(internals, ref).publicKey;\n return {\n getPublicKey: async () => {\n await internals.ready();\n return resolvePublicKey();\n },\n signEvent: async (unsigned) => {\n const publicKey = resolvePublicKey();\n // An absent pubkey must stay absent: `pubkey: undefined` is not a ProtocolValue and the\n // kernel would reject the params outright.\n const event: UnsignedEventInput =\n unsigned.pubkey === undefined\n ? {\n kind: unsigned.kind,\n created_at: unsigned.created_at,\n tags: unsigned.tags,\n content: unsigned.content,\n }\n : {\n kind: unsigned.kind,\n created_at: unsigned.created_at,\n tags: unsigned.tags,\n content: unsigned.content,\n pubkey: unsigned.pubkey,\n };\n const signed = await internals.call(\"nostr.signEvent\", { publicKey, event });\n // The kernel output must be the requested event, signed by the expected key.\n const requested: UnsignedEvent = {\n kind: unsigned.kind,\n created_at: unsigned.created_at,\n tags: unsigned.tags,\n content: unsigned.content,\n pubkey: publicKey,\n };\n if (\n signed.pubkey !== publicKey ||\n !signedMatchesUnsigned(signed, requested) ||\n !verifyEvent(signed)\n ) {\n throw integrity(\"the kernel returned a forged event\");\n }\n return signed;\n },\n nip04Encrypt: async (peer, plaintext) =>\n internals.call(\"nostr.nip04Encrypt\", {\n publicKey: resolvePublicKey(),\n peer,\n text: plaintext,\n }),\n nip04Decrypt: async (peer, ciphertext) =>\n internals.call(\"nostr.nip04Decrypt\", {\n publicKey: resolvePublicKey(),\n peer,\n text: ciphertext,\n }),\n nip44Encrypt: async (peer, plaintext) =>\n internals.call(\"nostr.nip44Encrypt\", {\n publicKey: resolvePublicKey(),\n peer,\n text: plaintext,\n }),\n nip44Decrypt: async (peer, payload) =>\n internals.call(\"nostr.nip44Decrypt\", {\n publicKey: resolvePublicKey(),\n peer,\n text: payload,\n }),\n unwrapGiftWraps: async (events) => {\n if (events.length === 0) {\n return [];\n }\n const publicKey = resolvePublicKey();\n const results: Array<Rumor | Error> = [];\n // The wire caps one batch at MAX_BATCH; chunk sequentially and concatenate in order —\n // other requests can interleave between chunks.\n for (let start = 0; start < events.length; start += MAX_BATCH) {\n // oxlint-disable-next-line eslint/no-await-in-loop -- sequential on purpose: chunks round-trip in order\n const batch = await internals.call(\"nostr.unwrapGiftWraps\", {\n publicKey,\n events: events.slice(start, start + MAX_BATCH),\n });\n for (const r of batch.results) {\n results.push(\"ok\" in r ? r.ok : new Error(r.err));\n }\n }\n return results;\n },\n };\n}\n\n/**\n * The wallet metadata sync signer (WD-076): the key lives at the reserved NIP-06 segment and is\n * sealed by `nostr.syncKey` on first use, so `getPublicKey` both returns and creates it. Signed\n * events pass the same forged-event check as `proxySigner`.\n */\nexport function syncSigner(walletId: string, internals: WalletInternals): WalletSyncSigner {\n let cached: string | undefined;\n const publicKey = async (): Promise<string> => {\n if (cached === undefined) {\n const resolved = await internals.call(\"nostr.syncKey\", { walletId });\n cached = resolved.publicKey;\n }\n return cached;\n };\n return {\n getPublicKey: publicKey,\n signEvent: async (unsigned) => {\n const expected = await publicKey();\n const event: UnsignedEventInput = {\n kind: unsigned.kind,\n created_at: unsigned.created_at,\n tags: unsigned.tags,\n content: unsigned.content,\n };\n const signed = await internals.call(\"nostr.syncSign\", { walletId, event });\n const requested: UnsignedEvent = {\n kind: unsigned.kind,\n created_at: unsigned.created_at,\n tags: unsigned.tags,\n content: unsigned.content,\n pubkey: expected,\n };\n if (\n signed.pubkey !== expected ||\n !signedMatchesUnsigned(signed, requested) ||\n !verifyEvent(signed)\n ) {\n throw integrity(\"the kernel returned a forged event\");\n }\n return signed;\n },\n encrypt: async (text) => internals.call(\"nostr.syncEncrypt\", { walletId, text }),\n decrypt: async (payload) => internals.call(\"nostr.syncDecrypt\", { walletId, text: payload }),\n };\n}\n","/**\n * The nostr module of @qntx/wallet: `nostr(wallet)` returns the wallet's Nostr API — signer\n * proxies, key discovery/derivation/derivation, guarded export and removal. The API is memoized per\n * wallet so repeated calls return the same object (stable React identity); a foreign object is\n * rejected because only a Wallet from this package holds a registered channel.\n */\nimport { wireSecretText } from \"../factors.ts\";\nimport { internalsOf } from \"../internal.ts\";\nimport type { WalletInternals } from \"../internal.ts\";\nimport { SingleUseSecret } from \"../secret.ts\";\nimport type {\n DerivedNostrKey,\n ExportedSecret,\n NostrKeyInfo,\n NostrKeyRef,\n UnlockFactor,\n Wallet,\n} from \"../types.ts\";\nimport { keyInfoOf, proxySigner, syncSigner } from \"./signer.ts\";\nimport type { NostrApi, NostrExportKeyFormat, WalletSigner, WalletSyncSigner } from \"./types.ts\";\n\nexport type {\n DerivedNostrKey,\n NostrApi,\n NostrExportKeyFormat,\n NostrKeyInfo,\n NostrKeyRef,\n NostrUnwrapResult,\n WalletSigner,\n WalletSyncSigner,\n} from \"./types.ts\";\n\nfunction createNostrApi(internals: WalletInternals): NostrApi {\n return {\n signer: (ref: NostrKeyRef): WalletSigner => proxySigner(ref, internals),\n\n syncSigner: (walletId: string): WalletSyncSigner => syncSigner(walletId, internals),\n\n discover: async (\n walletId: string,\n from: number,\n count: number,\n ): Promise<ReadonlyArray<DerivedNostrKey>> => {\n const result = await internals.call(\"nostr.discover\", { walletId, from, count });\n return result.keys;\n },\n\n deriveKey: async (walletId: string, index: number): Promise<NostrKeyInfo> =>\n internals.call(\"nostr.deriveKey\", { walletId, index }),\n\n removeKey: async (ref: NostrKeyRef): Promise<void> => {\n await internals.call(\"nostr.removeKey\", {\n publicKey: keyInfoOf(internals, ref).publicKey,\n });\n },\n\n exportKey: async (\n ref: NostrKeyRef,\n format: NostrExportKeyFormat,\n verify: UnlockFactor,\n ): Promise<ExportedSecret> =>\n internals.withVerifying(async () => {\n const { publicKey } = keyInfoOf(internals, ref);\n const transfer: ArrayBuffer[] = [];\n const wireVerify = await internals.resolveFactor(verify, transfer);\n const wireFormat =\n format.kind === \"nsec\"\n ? { kind: \"nsec\" as const }\n : {\n kind: \"ncryptsec\" as const,\n password: wireSecretText(format.password, transfer),\n };\n const bytes = await internals.call(\n \"nostr.exportKey\",\n { verify: wireVerify, publicKey, format: wireFormat },\n transfer,\n );\n return new SingleUseSecret(bytes);\n }),\n };\n}\n\nconst apis = new WeakMap<Wallet, NostrApi>();\n\nexport function nostr(wallet: Wallet): NostrApi {\n const internals = internalsOf(wallet);\n let api = apis.get(wallet);\n if (api === undefined) {\n api = createNostrApi(internals);\n apis.set(wallet, api);\n }\n return api;\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,SAAgB,UAAU,WAA4B,KAAgC;CACpF,MAAM,OAAO,UAAU,SAAS,C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ACT"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as opt, E as union, S as obj, T as str,
|
|
2
|
-
|
|
1
|
+
import { C as opt, E as union, S as obj, T as str, a as MAX_TEXT, b as literal, c as groupIndex, d as safeTime, f as walletErrorCode, g as arr, h as ValidationError, i as MAX_TAG_ITEM, l as identityIndex, m as walletInfo, n as MAX_SNAPSHOT_ITEMS, o as factor, p as walletId, r as MAX_TAGS, s as factorInfo, t as CORE_METHODS, u as publicKey, v as bytes, w as record, x as nullValue, y as int } from "./core-Cq5h9X0X.mjs";
|
|
2
|
+
// src/protocol/backup.ts
|
|
3
3
|
/**
|
|
4
4
|
* Backup-envelope wire validators (design/backup.mdx): the strict structural shape the kernel and
|
|
5
5
|
* the host-side `parseBackupHeader` share — version 1, bounded fields, `obj` rejects unknown keys.
|
|
@@ -28,15 +28,11 @@ const backupEnvelope = obj({
|
|
|
28
28
|
factors: arr(backupEnvelopeFactor, 5, 1),
|
|
29
29
|
payload: str(MAX_PAYLOAD_CHARS)
|
|
30
30
|
});
|
|
31
|
-
const backupHeader = obj({
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}), 1e3),
|
|
37
|
-
importedNostrKeyCount: int(0, 1e4),
|
|
38
|
-
importedChainKeyCount: int(0, 1e4)
|
|
39
|
-
});
|
|
31
|
+
const backupHeader = obj({ wallets: arr(obj({
|
|
32
|
+
walletId: str(128),
|
|
33
|
+
origin: union(literal("mnemonic"), literal("passkey")),
|
|
34
|
+
words: union(literal(12), literal(15), literal(18), literal(21), literal(24))
|
|
35
|
+
}), 1e3) });
|
|
40
36
|
/** The whole-envelope text bound — `backup.update` results and `backup.restore` params use it. */
|
|
41
37
|
const backupEnvelopeText = str(MAX_ENVELOPE_CHARS);
|
|
42
38
|
const backupInfo = obj({
|
|
@@ -44,8 +40,7 @@ const backupInfo = obj({
|
|
|
44
40
|
factors: arr(union(literal("passkey"), literal("password")), 5, 1),
|
|
45
41
|
createdAt: safeTime
|
|
46
42
|
});
|
|
47
|
-
|
|
48
|
-
//#region src/protocol/chains.ts
|
|
43
|
+
// src/protocol/chains.ts
|
|
49
44
|
const template = union(literal("evm.bip44"), literal("solana.bip44-change"), union(literal("btc.bip44"), literal("btc.bip49"), literal("btc.bip84")), literal("btc.bip86"));
|
|
50
45
|
const caip2 = str(200, /^[a-z0-9-]{3,64}:[0-9a-zA-Z-]{1,128}$/);
|
|
51
46
|
const addressResult = (t) => obj({
|
|
@@ -187,40 +182,22 @@ const CHAINS_METHODS = {
|
|
|
187
182
|
result: signResult
|
|
188
183
|
}
|
|
189
184
|
};
|
|
190
|
-
|
|
191
|
-
//#region src/protocol/nostr.ts
|
|
185
|
+
// src/protocol/nostr.ts
|
|
192
186
|
const hex128 = str(128, /^[0-9a-f]{128}$/);
|
|
193
|
-
const nostrKeyRef =
|
|
187
|
+
const nostrKeyRef = obj({
|
|
194
188
|
kind: literal("derived"),
|
|
195
189
|
walletId,
|
|
196
|
-
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
id: publicKey
|
|
200
|
-
}), obj({
|
|
201
|
-
kind: literal("nip46-client"),
|
|
202
|
-
id: publicKey
|
|
203
|
-
}));
|
|
204
|
-
const nostrKeyInfo = union(obj({
|
|
205
|
-
ref: nostrKeyRef,
|
|
206
|
-
publicKey,
|
|
207
|
-
origin: union(literal("nsec"), literal("ncryptsec"), literal("generated")),
|
|
208
|
-
backedUp: bool,
|
|
209
|
-
createdAt: safeTime
|
|
210
|
-
}), obj({
|
|
190
|
+
index: identityIndex
|
|
191
|
+
});
|
|
192
|
+
const nostrKeyInfo = obj({
|
|
211
193
|
ref: nostrKeyRef,
|
|
212
194
|
publicKey,
|
|
213
|
-
origin: union(literal("derived"), literal("nip46-client")),
|
|
214
195
|
createdAt: safeTime
|
|
215
|
-
})
|
|
196
|
+
});
|
|
216
197
|
/** The nostr section of the status payload, composed by protocol/index.ts. */
|
|
217
198
|
const nostrStatus = obj({ keys: arr(nostrKeyInfo, MAX_SNAPSHOT_ITEMS) });
|
|
218
|
-
const keyResult = obj({
|
|
219
|
-
key: nostrKeyInfo,
|
|
220
|
-
session: sessionId
|
|
221
|
-
});
|
|
222
199
|
const derivedNostrKey = obj({
|
|
223
|
-
|
|
200
|
+
index: identityIndex,
|
|
224
201
|
publicKey
|
|
225
202
|
});
|
|
226
203
|
const tags = arr(arr(str(MAX_TAG_ITEM), 64), MAX_TAGS);
|
|
@@ -248,14 +225,6 @@ const rumor = obj({
|
|
|
248
225
|
tags,
|
|
249
226
|
content: str(MAX_TEXT)
|
|
250
227
|
});
|
|
251
|
-
const nostrImportInput = union(obj({
|
|
252
|
-
kind: literal("nsec"),
|
|
253
|
-
nsec: bytes(1, 256)
|
|
254
|
-
}), obj({
|
|
255
|
-
kind: literal("ncryptsec"),
|
|
256
|
-
ncryptsec: str(512, /^ncryptsec1/),
|
|
257
|
-
password: bytes(1, 1024)
|
|
258
|
-
}));
|
|
259
228
|
const nostrExportFormat = union(obj({ kind: literal("nsec") }), obj({
|
|
260
229
|
kind: literal("ncryptsec"),
|
|
261
230
|
password: bytes(1, 1024)
|
|
@@ -272,7 +241,7 @@ const NOSTR_METHODS = {
|
|
|
272
241
|
presence: false,
|
|
273
242
|
params: obj({
|
|
274
243
|
walletId,
|
|
275
|
-
from:
|
|
244
|
+
from: identityIndex,
|
|
276
245
|
count: int(1, 20)
|
|
277
246
|
}),
|
|
278
247
|
result: obj({ keys: arr(derivedNostrKey, 20) })
|
|
@@ -282,38 +251,14 @@ const NOSTR_METHODS = {
|
|
|
282
251
|
presence: false,
|
|
283
252
|
params: obj({
|
|
284
253
|
walletId,
|
|
285
|
-
|
|
254
|
+
index: identityIndex
|
|
286
255
|
}),
|
|
287
256
|
result: nostrKeyInfo
|
|
288
257
|
},
|
|
289
|
-
"nostr.importKey": {
|
|
290
|
-
session: false,
|
|
291
|
-
presence: true,
|
|
292
|
-
params: obj({
|
|
293
|
-
input: nostrImportInput,
|
|
294
|
-
factor: opt(factor)
|
|
295
|
-
}),
|
|
296
|
-
result: keyResult
|
|
297
|
-
},
|
|
298
|
-
"nostr.generateKey": {
|
|
299
|
-
session: false,
|
|
300
|
-
presence: true,
|
|
301
|
-
params: obj({ factor: opt(factor) }),
|
|
302
|
-
result: keyResult
|
|
303
|
-
},
|
|
304
|
-
"nostr.createNip46ClientKey": {
|
|
305
|
-
session: true,
|
|
306
|
-
presence: false,
|
|
307
|
-
params: nullValue,
|
|
308
|
-
result: nostrKeyInfo
|
|
309
|
-
},
|
|
310
258
|
"nostr.removeKey": {
|
|
311
259
|
session: true,
|
|
312
260
|
presence: true,
|
|
313
|
-
params: obj({
|
|
314
|
-
publicKey,
|
|
315
|
-
verify: opt(factor)
|
|
316
|
-
}),
|
|
261
|
+
params: obj({ publicKey }),
|
|
317
262
|
result: nullValue
|
|
318
263
|
},
|
|
319
264
|
"nostr.exportKey": {
|
|
@@ -367,10 +312,42 @@ const NOSTR_METHODS = {
|
|
|
367
312
|
events: arr(signedEvent, 64)
|
|
368
313
|
}),
|
|
369
314
|
result: obj({ results: arr(union(obj({ ok: rumor }), obj({ err: walletErrorCode })), 64) })
|
|
315
|
+
},
|
|
316
|
+
"nostr.syncKey": {
|
|
317
|
+
session: true,
|
|
318
|
+
presence: false,
|
|
319
|
+
params: obj({ walletId }),
|
|
320
|
+
result: obj({ publicKey })
|
|
321
|
+
},
|
|
322
|
+
"nostr.syncSign": {
|
|
323
|
+
session: true,
|
|
324
|
+
presence: false,
|
|
325
|
+
params: obj({
|
|
326
|
+
walletId,
|
|
327
|
+
event: signEventInput
|
|
328
|
+
}),
|
|
329
|
+
result: signedEvent
|
|
330
|
+
},
|
|
331
|
+
"nostr.syncEncrypt": {
|
|
332
|
+
session: true,
|
|
333
|
+
presence: false,
|
|
334
|
+
params: obj({
|
|
335
|
+
walletId,
|
|
336
|
+
text: str(MAX_TEXT)
|
|
337
|
+
}),
|
|
338
|
+
result: textResult
|
|
339
|
+
},
|
|
340
|
+
"nostr.syncDecrypt": {
|
|
341
|
+
session: true,
|
|
342
|
+
presence: false,
|
|
343
|
+
params: obj({
|
|
344
|
+
walletId,
|
|
345
|
+
text: str(MAX_TEXT * 4)
|
|
346
|
+
}),
|
|
347
|
+
result: textResult
|
|
370
348
|
}
|
|
371
349
|
};
|
|
372
|
-
|
|
373
|
-
//#region src/protocol/spark.ts
|
|
350
|
+
// src/protocol/spark.ts
|
|
374
351
|
/**
|
|
375
352
|
* The spark module's method table and wire validators. The protocol stays free of @qntx/spark
|
|
376
353
|
* imports — even types — so the signer shapes it validates are declared here structurally; a
|
|
@@ -563,6 +540,27 @@ const decimalU128 = (value) => {
|
|
|
563
540
|
if (typeof value !== "string" || value.length > 39 || !/^(?:0|[1-9][0-9]*)$/.test(value) || BigInt(value) > U128_MAX) throw new ValidationError();
|
|
564
541
|
return value;
|
|
565
542
|
};
|
|
543
|
+
const outflowIntents = [
|
|
544
|
+
obj({
|
|
545
|
+
kind: literal("exit"),
|
|
546
|
+
address: str(128),
|
|
547
|
+
amountSats: satsAmount(),
|
|
548
|
+
maxFeeSats: satsAmount()
|
|
549
|
+
}),
|
|
550
|
+
obj({
|
|
551
|
+
kind: literal("onchain"),
|
|
552
|
+
address: str(128),
|
|
553
|
+
maxFeeSats: satsAmount()
|
|
554
|
+
}),
|
|
555
|
+
obj({
|
|
556
|
+
kind: literal("token"),
|
|
557
|
+
tokenIdentifier: bytes(32),
|
|
558
|
+
receivers: arr(obj({
|
|
559
|
+
ownerPublicKey: bytes(33),
|
|
560
|
+
amount: decimalU128
|
|
561
|
+
}), 256, 1)
|
|
562
|
+
})
|
|
563
|
+
];
|
|
566
564
|
const approveIntent = union(obj({
|
|
567
565
|
kind: literal("transfer"),
|
|
568
566
|
receivers: arr(obj({
|
|
@@ -574,23 +572,7 @@ const approveIntent = union(obj({
|
|
|
574
572
|
invoice: str(4096),
|
|
575
573
|
amountSats: opt(satsAmount()),
|
|
576
574
|
maxFeeSats: satsAmount()
|
|
577
|
-
}),
|
|
578
|
-
kind: literal("exit"),
|
|
579
|
-
address: str(128),
|
|
580
|
-
amountSats: satsAmount(),
|
|
581
|
-
maxFeeSats: satsAmount()
|
|
582
|
-
}), obj({
|
|
583
|
-
kind: literal("onchain"),
|
|
584
|
-
address: str(128),
|
|
585
|
-
maxFeeSats: satsAmount()
|
|
586
|
-
}), obj({
|
|
587
|
-
kind: literal("token"),
|
|
588
|
-
tokenIdentifier: bytes(32),
|
|
589
|
-
receivers: arr(obj({
|
|
590
|
-
ownerPublicKey: bytes(33),
|
|
591
|
-
amount: decimalU128
|
|
592
|
-
}), 256, 1)
|
|
593
|
-
}));
|
|
575
|
+
}), ...outflowIntents);
|
|
594
576
|
const approvalSummary = union(obj({
|
|
595
577
|
kind: literal("transfer"),
|
|
596
578
|
receivers: arr(obj({
|
|
@@ -602,23 +584,7 @@ const approvalSummary = union(obj({
|
|
|
602
584
|
paymentHash: bytes(32),
|
|
603
585
|
invoiceAmountSats: opt(satsAmount()),
|
|
604
586
|
maxFeeSats: satsAmount()
|
|
605
|
-
}),
|
|
606
|
-
kind: literal("exit"),
|
|
607
|
-
address: str(128),
|
|
608
|
-
amountSats: satsAmount(),
|
|
609
|
-
maxFeeSats: satsAmount()
|
|
610
|
-
}), obj({
|
|
611
|
-
kind: literal("onchain"),
|
|
612
|
-
address: str(128),
|
|
613
|
-
maxFeeSats: satsAmount()
|
|
614
|
-
}), obj({
|
|
615
|
-
kind: literal("token"),
|
|
616
|
-
tokenIdentifier: bytes(32),
|
|
617
|
-
receivers: arr(obj({
|
|
618
|
-
ownerPublicKey: bytes(33),
|
|
619
|
-
amount: decimalU128
|
|
620
|
-
}), 256, 1)
|
|
621
|
-
}));
|
|
587
|
+
}), ...outflowIntents);
|
|
622
588
|
const SPARK_METHODS = {
|
|
623
589
|
"spark.identityPublicKey": {
|
|
624
590
|
session: true,
|
|
@@ -743,9 +709,8 @@ const SPARK_METHODS = {
|
|
|
743
709
|
result: approvalSummary
|
|
744
710
|
}
|
|
745
711
|
};
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
const PROTOCOL_VERSION = 1;
|
|
712
|
+
// src/protocol/index.ts
|
|
713
|
+
const PROTOCOL_VERSION = 2;
|
|
749
714
|
/**
|
|
750
715
|
* Responses to requests whose `id` cannot be parsed (malformed envelope) carry id 0. Main-side
|
|
751
716
|
* request ids start at 1, so a `0` is never a real pending request.
|
|
@@ -799,7 +764,7 @@ function isProtocolValue(value) {
|
|
|
799
764
|
}
|
|
800
765
|
/** The status payload: core sections plus one object section per protocol module. */
|
|
801
766
|
const statusPayload = obj({
|
|
802
|
-
protocol: literal(
|
|
767
|
+
protocol: literal(2),
|
|
803
768
|
vault: union(literal("uninitialized"), literal("locked"), literal("unlocked"), literal("corrupted"), literal("unsupported"), literal("unavailable")),
|
|
804
769
|
factors: arr(factorInfo, MAX_SNAPSHOT_ITEMS),
|
|
805
770
|
wallets: arr(walletInfo, MAX_SNAPSHOT_ITEMS),
|
|
@@ -825,17 +790,16 @@ const errorPayload = obj({
|
|
|
825
790
|
});
|
|
826
791
|
/** Host-side validators: `kernelEvent` covers the two push messages. */
|
|
827
792
|
const kernelEvent = union(obj({
|
|
828
|
-
v: literal(
|
|
793
|
+
v: literal(2),
|
|
829
794
|
event: literal("status"),
|
|
830
795
|
status: statusPayload
|
|
831
796
|
}), obj({
|
|
832
|
-
v: literal(
|
|
797
|
+
v: literal(2),
|
|
833
798
|
event: literal("locked"),
|
|
834
799
|
reason: lockReason
|
|
835
800
|
}));
|
|
836
801
|
/** Validates the `err` payload of a response; `ok` is checked against `METHODS[m].result`. */
|
|
837
802
|
const responseError = errorPayload;
|
|
838
|
-
//#endregion
|
|
839
803
|
export { isProtocolValue as a, responseError as c, CHAINS_METHODS as d, backupEnvelope as f, backupInfo as g, backupHeader as h, UNATTRIBUTED_ID as i, SPARK_METHODS as l, backupEnvelopeText as m, METHODS as n, kernelEvent as o, backupEnvelopeFactor as p, PROTOCOL_VERSION as r, requestBytes as s, MAX_REQUEST_BYTES as t, NOSTR_METHODS as u };
|
|
840
804
|
|
|
841
|
-
//# sourceMappingURL=protocol-
|
|
805
|
+
//# sourceMappingURL=protocol-DbpLncEB.mjs.map
|