@qntx/wallet 0.2.0 → 0.4.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/README.md +43 -4
- package/dist/{protocol-D9_0sY_Y.mjs → core-QJ66nLLG.mjs} +91 -228
- package/dist/core-QJ66nLLG.mjs.map +1 -0
- package/dist/host.d.mts +19 -3
- package/dist/host.mjs +101 -11
- package/dist/host.mjs.map +1 -1
- package/dist/index-Dhf6wGjQ.d.mts +668 -0
- package/dist/index.d.mts +18 -2
- package/dist/index.mjs +35 -1
- package/dist/index.mjs.map +1 -0
- package/dist/internal-LBHal13j.mjs +25 -0
- package/dist/internal-LBHal13j.mjs.map +1 -0
- package/dist/nostr.d.mts +2 -2
- package/dist/nostr.mjs +13 -5
- package/dist/nostr.mjs.map +1 -1
- package/dist/protocol-D9xBuMzq.mjs +697 -0
- package/dist/protocol-D9xBuMzq.mjs.map +1 -0
- package/dist/protocol.d.mts +3 -3
- package/dist/protocol.mjs +3 -2
- package/dist/{secret-B-P3QOym.mjs → secret-IcTjjaX0.mjs} +113 -31
- package/dist/secret-IcTjjaX0.mjs.map +1 -0
- package/dist/spark.d.mts +30 -0
- package/dist/spark.mjs +303 -0
- package/dist/spark.mjs.map +1 -0
- package/dist/{types-CEy7At1l.d.mts → types-D4OUjGVS.d.mts} +62 -8
- package/dist/types.d.mts +2 -2
- package/dist/types.mjs.map +1 -1
- package/package.json +16 -5
- package/dist/index-BJfyIwOZ.d.mts +0 -286
- package/dist/protocol-D9_0sY_Y.mjs.map +0 -1
- package/dist/secret-B-P3QOym.mjs.map +0 -1
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
import { C as opt, E as union, S as obj, T as str, _ as bool, a as MAX_TEXT, b as literal, c as groupIndex, d as sessionId, f as walletErrorCode, g as arr, h as ValidationError, i as MAX_TAG_ITEM, l as publicKey, 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 safeTime, v as bytes, w as record, x as nullValue, y as int } from "./core-QJ66nLLG.mjs";
|
|
2
|
+
//#region src/protocol/backup.ts
|
|
3
|
+
/**
|
|
4
|
+
* Backup-envelope wire validators (design/backup.mdx): the strict structural shape the kernel and
|
|
5
|
+
* the host-side `parseBackupHeader` share — version 1, bounded fields, `obj` rejects unknown keys.
|
|
6
|
+
* Cryptographic checks (the payload tag, factor unwrap) stay in the kernel; these rules only shape
|
|
7
|
+
* the JSON.
|
|
8
|
+
*/
|
|
9
|
+
const MAX_FACTOR_CHARS = 512;
|
|
10
|
+
const MAX_STRING_BYTES = 64e3;
|
|
11
|
+
const MAX_PAYLOAD_CHARS = 14e5;
|
|
12
|
+
const MAX_ENVELOPE_CHARS = 4e6;
|
|
13
|
+
/** One public factor entry — shared by the envelope and the retained `backup-key` header. */
|
|
14
|
+
const backupEnvelopeFactor = union(obj({
|
|
15
|
+
kind: literal("passkey"),
|
|
16
|
+
credentialId: str(MAX_FACTOR_CHARS),
|
|
17
|
+
sealedBek: str(MAX_FACTOR_CHARS)
|
|
18
|
+
}), obj({
|
|
19
|
+
kind: literal("password"),
|
|
20
|
+
iterations: literal(6e5),
|
|
21
|
+
salt: str(128),
|
|
22
|
+
sealedBek: str(MAX_FACTOR_CHARS)
|
|
23
|
+
}));
|
|
24
|
+
const backupEnvelope = obj({
|
|
25
|
+
version: literal(1),
|
|
26
|
+
createdAt: int(0, Number.MAX_SAFE_INTEGER),
|
|
27
|
+
header: str(MAX_STRING_BYTES),
|
|
28
|
+
factors: arr(backupEnvelopeFactor, 5, 1),
|
|
29
|
+
payload: str(MAX_PAYLOAD_CHARS)
|
|
30
|
+
});
|
|
31
|
+
const backupHeader = obj({
|
|
32
|
+
wallets: arr(obj({
|
|
33
|
+
walletId: str(128),
|
|
34
|
+
origin: union(literal("mnemonic"), literal("passkey")),
|
|
35
|
+
words: union(literal(12), literal(15), literal(18), literal(21), literal(24))
|
|
36
|
+
}), 1e3),
|
|
37
|
+
importedNostrKeyCount: int(0, 1e4),
|
|
38
|
+
importedChainKeyCount: int(0, 1e4)
|
|
39
|
+
});
|
|
40
|
+
/** The whole-envelope text bound — `backup.update` results and `backup.restore` params use it. */
|
|
41
|
+
const backupEnvelopeText = str(MAX_ENVELOPE_CHARS);
|
|
42
|
+
const backupInfo = obj({
|
|
43
|
+
id: str(32, /^[0-9a-f]{32}$/),
|
|
44
|
+
factors: arr(union(literal("passkey"), literal("password")), 5, 1),
|
|
45
|
+
createdAt: safeTime
|
|
46
|
+
});
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/protocol/nostr.ts
|
|
49
|
+
const hex128 = str(128, /^[0-9a-f]{128}$/);
|
|
50
|
+
const nostrKeyRef = union(obj({
|
|
51
|
+
kind: literal("derived"),
|
|
52
|
+
walletId,
|
|
53
|
+
groupIndex
|
|
54
|
+
}), obj({
|
|
55
|
+
kind: literal("standalone"),
|
|
56
|
+
id: publicKey
|
|
57
|
+
}), obj({
|
|
58
|
+
kind: literal("nip46-client"),
|
|
59
|
+
id: publicKey
|
|
60
|
+
}));
|
|
61
|
+
const nostrKeyInfo = union(obj({
|
|
62
|
+
ref: nostrKeyRef,
|
|
63
|
+
publicKey,
|
|
64
|
+
origin: union(literal("nsec"), literal("ncryptsec"), literal("generated")),
|
|
65
|
+
backedUp: bool,
|
|
66
|
+
createdAt: safeTime
|
|
67
|
+
}), obj({
|
|
68
|
+
ref: nostrKeyRef,
|
|
69
|
+
publicKey,
|
|
70
|
+
origin: union(literal("derived"), literal("nip46-client")),
|
|
71
|
+
createdAt: safeTime
|
|
72
|
+
}));
|
|
73
|
+
/** The nostr section of the status payload, composed by protocol/index.ts. */
|
|
74
|
+
const nostrStatus = obj({ keys: arr(nostrKeyInfo, MAX_SNAPSHOT_ITEMS) });
|
|
75
|
+
const keyResult = obj({
|
|
76
|
+
key: nostrKeyInfo,
|
|
77
|
+
session: sessionId
|
|
78
|
+
});
|
|
79
|
+
const derivedNostrKey = obj({
|
|
80
|
+
groupIndex,
|
|
81
|
+
publicKey
|
|
82
|
+
});
|
|
83
|
+
const tags = arr(arr(str(MAX_TAG_ITEM), 64), MAX_TAGS);
|
|
84
|
+
const signEventInput = obj({
|
|
85
|
+
kind: int(0, 65535),
|
|
86
|
+
created_at: safeTime,
|
|
87
|
+
tags,
|
|
88
|
+
content: str(MAX_TEXT),
|
|
89
|
+
pubkey: opt(publicKey)
|
|
90
|
+
});
|
|
91
|
+
const signedEvent = obj({
|
|
92
|
+
id: publicKey,
|
|
93
|
+
pubkey: publicKey,
|
|
94
|
+
sig: hex128,
|
|
95
|
+
kind: int(0, 65535),
|
|
96
|
+
created_at: safeTime,
|
|
97
|
+
tags,
|
|
98
|
+
content: str(MAX_TEXT)
|
|
99
|
+
});
|
|
100
|
+
const rumor = obj({
|
|
101
|
+
id: publicKey,
|
|
102
|
+
pubkey: publicKey,
|
|
103
|
+
kind: int(0, 65535),
|
|
104
|
+
created_at: safeTime,
|
|
105
|
+
tags,
|
|
106
|
+
content: str(MAX_TEXT)
|
|
107
|
+
});
|
|
108
|
+
const nostrImportInput = union(obj({
|
|
109
|
+
kind: literal("nsec"),
|
|
110
|
+
nsec: bytes(1, 256)
|
|
111
|
+
}), obj({
|
|
112
|
+
kind: literal("ncryptsec"),
|
|
113
|
+
ncryptsec: str(512, /^ncryptsec1/),
|
|
114
|
+
password: bytes(1, 1024)
|
|
115
|
+
}));
|
|
116
|
+
const nostrExportFormat = union(obj({ kind: literal("nsec") }), obj({
|
|
117
|
+
kind: literal("ncryptsec"),
|
|
118
|
+
password: bytes(1, 1024)
|
|
119
|
+
}));
|
|
120
|
+
const textParams = obj({
|
|
121
|
+
publicKey,
|
|
122
|
+
peer: publicKey,
|
|
123
|
+
text: str(MAX_TEXT)
|
|
124
|
+
});
|
|
125
|
+
const textResult = str(MAX_TEXT * 4);
|
|
126
|
+
const NOSTR_METHODS = {
|
|
127
|
+
"nostr.discover": {
|
|
128
|
+
session: true,
|
|
129
|
+
presence: false,
|
|
130
|
+
params: obj({
|
|
131
|
+
walletId,
|
|
132
|
+
from: groupIndex,
|
|
133
|
+
count: int(1, 20)
|
|
134
|
+
}),
|
|
135
|
+
result: obj({ keys: arr(derivedNostrKey, 20) })
|
|
136
|
+
},
|
|
137
|
+
"nostr.deriveKey": {
|
|
138
|
+
session: true,
|
|
139
|
+
presence: false,
|
|
140
|
+
params: obj({
|
|
141
|
+
walletId,
|
|
142
|
+
groupIndex
|
|
143
|
+
}),
|
|
144
|
+
result: nostrKeyInfo
|
|
145
|
+
},
|
|
146
|
+
"nostr.importKey": {
|
|
147
|
+
session: false,
|
|
148
|
+
presence: true,
|
|
149
|
+
params: obj({
|
|
150
|
+
input: nostrImportInput,
|
|
151
|
+
factor: opt(factor)
|
|
152
|
+
}),
|
|
153
|
+
result: keyResult
|
|
154
|
+
},
|
|
155
|
+
"nostr.generateKey": {
|
|
156
|
+
session: false,
|
|
157
|
+
presence: true,
|
|
158
|
+
params: obj({ factor: opt(factor) }),
|
|
159
|
+
result: keyResult
|
|
160
|
+
},
|
|
161
|
+
"nostr.createNip46ClientKey": {
|
|
162
|
+
session: true,
|
|
163
|
+
presence: false,
|
|
164
|
+
params: nullValue,
|
|
165
|
+
result: nostrKeyInfo
|
|
166
|
+
},
|
|
167
|
+
"nostr.removeKey": {
|
|
168
|
+
session: true,
|
|
169
|
+
presence: true,
|
|
170
|
+
params: obj({
|
|
171
|
+
publicKey,
|
|
172
|
+
verify: opt(factor)
|
|
173
|
+
}),
|
|
174
|
+
result: nullValue
|
|
175
|
+
},
|
|
176
|
+
"nostr.exportKey": {
|
|
177
|
+
session: true,
|
|
178
|
+
presence: true,
|
|
179
|
+
params: obj({
|
|
180
|
+
verify: factor,
|
|
181
|
+
publicKey,
|
|
182
|
+
format: nostrExportFormat
|
|
183
|
+
}),
|
|
184
|
+
result: bytes(16, 4096)
|
|
185
|
+
},
|
|
186
|
+
"nostr.signEvent": {
|
|
187
|
+
session: true,
|
|
188
|
+
presence: false,
|
|
189
|
+
params: obj({
|
|
190
|
+
publicKey,
|
|
191
|
+
event: signEventInput
|
|
192
|
+
}),
|
|
193
|
+
result: signedEvent
|
|
194
|
+
},
|
|
195
|
+
"nostr.nip04Encrypt": {
|
|
196
|
+
session: true,
|
|
197
|
+
presence: false,
|
|
198
|
+
params: textParams,
|
|
199
|
+
result: textResult
|
|
200
|
+
},
|
|
201
|
+
"nostr.nip04Decrypt": {
|
|
202
|
+
session: true,
|
|
203
|
+
presence: false,
|
|
204
|
+
params: textParams,
|
|
205
|
+
result: textResult
|
|
206
|
+
},
|
|
207
|
+
"nostr.nip44Encrypt": {
|
|
208
|
+
session: true,
|
|
209
|
+
presence: false,
|
|
210
|
+
params: textParams,
|
|
211
|
+
result: textResult
|
|
212
|
+
},
|
|
213
|
+
"nostr.nip44Decrypt": {
|
|
214
|
+
session: true,
|
|
215
|
+
presence: false,
|
|
216
|
+
params: textParams,
|
|
217
|
+
result: textResult
|
|
218
|
+
},
|
|
219
|
+
"nostr.unwrapGiftWraps": {
|
|
220
|
+
session: true,
|
|
221
|
+
presence: false,
|
|
222
|
+
params: obj({
|
|
223
|
+
publicKey,
|
|
224
|
+
events: arr(signedEvent, 64)
|
|
225
|
+
}),
|
|
226
|
+
result: obj({ results: arr(union(obj({ ok: rumor }), obj({ err: walletErrorCode })), 64) })
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/protocol/spark.ts
|
|
231
|
+
/**
|
|
232
|
+
* The spark module's method table and wire validators. The protocol stays free of @qntx/spark
|
|
233
|
+
* imports — even types — so the signer shapes it validates are declared here structurally; a
|
|
234
|
+
* type-level test in packages/wallet/tests pins them assignable to and from @qntx/spark/signer's
|
|
235
|
+
* types.
|
|
236
|
+
*
|
|
237
|
+
* Every method carries `account` — walletId/groupIndex/network — and is session-gated but not
|
|
238
|
+
* presence-bearing (signer work must not hold a session open). List params are capped at MAX_BATCH;
|
|
239
|
+
* the leaf lists of prepareTransfer/prepareClaim are capped at the signer's own 256.
|
|
240
|
+
*/
|
|
241
|
+
const MAX_LEAVES = 256;
|
|
242
|
+
const MAX_OPERATORS = 16;
|
|
243
|
+
const MAX_PACKAGE_BYTES = 1048576;
|
|
244
|
+
const MAX_TX_BYTES = 1048576;
|
|
245
|
+
const MAX_PREVOUTS = 1024;
|
|
246
|
+
const MAX_SCRIPT_BYTES = 16384;
|
|
247
|
+
const MAX_CIPHERTEXT_BYTES = 4096;
|
|
248
|
+
const MAX_SATS = 0x775f05a074000;
|
|
249
|
+
const network = union(literal("mainnet"), literal("regtest"));
|
|
250
|
+
const account = obj({
|
|
251
|
+
walletId,
|
|
252
|
+
groupIndex,
|
|
253
|
+
network
|
|
254
|
+
});
|
|
255
|
+
const identifier = str(64, /^[0-9a-f]{64}$/);
|
|
256
|
+
const leafId = str(64);
|
|
257
|
+
const transferId = str(64);
|
|
258
|
+
const bytes32 = bytes(32);
|
|
259
|
+
const bytes33 = bytes(33);
|
|
260
|
+
const bytes64 = bytes(64);
|
|
261
|
+
const ciphertext = bytes(1, MAX_CIPHERTEXT_BYTES);
|
|
262
|
+
const sats = int(0, MAX_SATS);
|
|
263
|
+
const sparkLeafKey = union(obj({
|
|
264
|
+
type: literal("leaf"),
|
|
265
|
+
leafId
|
|
266
|
+
}), obj({
|
|
267
|
+
type: literal("static-deposit"),
|
|
268
|
+
index: int(0, 2147483647)
|
|
269
|
+
}));
|
|
270
|
+
const sparkKey = union(obj({
|
|
271
|
+
type: literal("leaf"),
|
|
272
|
+
leafId
|
|
273
|
+
}), obj({
|
|
274
|
+
type: literal("static-deposit"),
|
|
275
|
+
index: int(0, 2147483647)
|
|
276
|
+
}), obj({
|
|
277
|
+
type: literal("received"),
|
|
278
|
+
ciphertext
|
|
279
|
+
}));
|
|
280
|
+
const signingCommitment = obj({
|
|
281
|
+
hiding: bytes33,
|
|
282
|
+
binding: bytes33
|
|
283
|
+
});
|
|
284
|
+
const operatorCommitment = obj({
|
|
285
|
+
identifier,
|
|
286
|
+
commitment: signingCommitment
|
|
287
|
+
});
|
|
288
|
+
const signingTarget = obj({
|
|
289
|
+
transaction: bytes(1, MAX_TX_BYTES),
|
|
290
|
+
inputIndex: int(0, 2147483647),
|
|
291
|
+
prevouts: arr(obj({
|
|
292
|
+
script: bytes(0, MAX_SCRIPT_BYTES),
|
|
293
|
+
amount: sats
|
|
294
|
+
}), MAX_PREVOUTS)
|
|
295
|
+
});
|
|
296
|
+
const adaptorPublicKey = opt(union(bytes(0), bytes(33)));
|
|
297
|
+
const frostSignJob = obj({
|
|
298
|
+
key: sparkKey,
|
|
299
|
+
target: signingTarget,
|
|
300
|
+
verifyingKey: bytes33,
|
|
301
|
+
commitment: signingCommitment,
|
|
302
|
+
operatorCommitments: arr(operatorCommitment, MAX_OPERATORS),
|
|
303
|
+
adaptorPublicKey
|
|
304
|
+
});
|
|
305
|
+
const frostAggregateJob = obj({
|
|
306
|
+
key: sparkKey,
|
|
307
|
+
target: signingTarget,
|
|
308
|
+
verifyingKey: bytes33,
|
|
309
|
+
commitment: signingCommitment,
|
|
310
|
+
signatureShare: bytes32,
|
|
311
|
+
operators: arr(obj({
|
|
312
|
+
identifier,
|
|
313
|
+
commitment: signingCommitment,
|
|
314
|
+
signatureShare: bytes32,
|
|
315
|
+
publicKey: bytes33
|
|
316
|
+
}), MAX_OPERATORS),
|
|
317
|
+
adaptorPublicKey
|
|
318
|
+
});
|
|
319
|
+
const staticDepositClaimStatement = obj({
|
|
320
|
+
kind: literal("static-deposit-claim"),
|
|
321
|
+
network,
|
|
322
|
+
txid: bytes32,
|
|
323
|
+
vout: int(0, 2147483647),
|
|
324
|
+
creditAmountSats: sats,
|
|
325
|
+
sspSignature: bytes(1, 1024)
|
|
326
|
+
});
|
|
327
|
+
const identityStatement = union(obj({
|
|
328
|
+
kind: literal("so-challenge"),
|
|
329
|
+
challenge: bytes(1, 4096)
|
|
330
|
+
}), obj({
|
|
331
|
+
kind: literal("ssp-challenge"),
|
|
332
|
+
protectedChallenge: str(8192)
|
|
333
|
+
}), obj({
|
|
334
|
+
kind: literal("transfer-package"),
|
|
335
|
+
transferId,
|
|
336
|
+
package: bytes(1, MAX_PACKAGE_BYTES)
|
|
337
|
+
}), obj({
|
|
338
|
+
kind: literal("claim-package"),
|
|
339
|
+
transferId,
|
|
340
|
+
package: bytes(1, MAX_PACKAGE_BYTES)
|
|
341
|
+
}), staticDepositClaimStatement, obj({
|
|
342
|
+
kind: literal("static-deposit-refund"),
|
|
343
|
+
network,
|
|
344
|
+
txid: bytes32,
|
|
345
|
+
vout: int(0, 2147483647),
|
|
346
|
+
amount: sats,
|
|
347
|
+
target: signingTarget
|
|
348
|
+
}), obj({
|
|
349
|
+
kind: literal("watchtower-recovery"),
|
|
350
|
+
network,
|
|
351
|
+
leafId,
|
|
352
|
+
target: signingTarget
|
|
353
|
+
}), obj({
|
|
354
|
+
kind: literal("spark-invoice"),
|
|
355
|
+
invoice: bytes(1, 65536)
|
|
356
|
+
}), obj({
|
|
357
|
+
kind: literal("token-transaction"),
|
|
358
|
+
partialTransaction: bytes(1, MAX_PACKAGE_BYTES)
|
|
359
|
+
}), obj({
|
|
360
|
+
kind: literal("lightning-address"),
|
|
361
|
+
action: literal("register"),
|
|
362
|
+
domain: str(253),
|
|
363
|
+
username: str(64, /^[a-z0-9\-_.+]{1,64}$/u),
|
|
364
|
+
description: str(512),
|
|
365
|
+
timestamp: safeTime
|
|
366
|
+
}), obj({
|
|
367
|
+
kind: literal("lightning-address"),
|
|
368
|
+
action: literal("unregister"),
|
|
369
|
+
domain: str(253),
|
|
370
|
+
username: str(64, /^[a-z0-9\-_.+]{1,64}$/u),
|
|
371
|
+
timestamp: safeTime
|
|
372
|
+
}));
|
|
373
|
+
const htlcTimeoutRequest = obj({
|
|
374
|
+
transaction: bytes(1, MAX_TX_BYTES),
|
|
375
|
+
inputIndex: int(0, 2147483647),
|
|
376
|
+
prevoutAmountSats: sats,
|
|
377
|
+
paymentHash: bytes32,
|
|
378
|
+
hashLockPublicKey: bytes33
|
|
379
|
+
});
|
|
380
|
+
const prepareTransferRequest = obj({
|
|
381
|
+
transferId,
|
|
382
|
+
leaves: arr(obj({
|
|
383
|
+
leafId,
|
|
384
|
+
key: sparkLeafKey,
|
|
385
|
+
receiverIdentityPublicKey: bytes33
|
|
386
|
+
}), MAX_LEAVES, 1)
|
|
387
|
+
});
|
|
388
|
+
const preparedTransfer = obj({
|
|
389
|
+
operatorPackages: record(identifier, bytes(1, MAX_PACKAGE_BYTES), MAX_OPERATORS),
|
|
390
|
+
leaves: arr(obj({
|
|
391
|
+
leafId,
|
|
392
|
+
newPublicKey: bytes33,
|
|
393
|
+
secretCipher: ciphertext,
|
|
394
|
+
signature: bytes64
|
|
395
|
+
}), MAX_LEAVES)
|
|
396
|
+
});
|
|
397
|
+
const prepareClaimRequest = obj({
|
|
398
|
+
transferId,
|
|
399
|
+
leaves: arr(obj({
|
|
400
|
+
leafId,
|
|
401
|
+
secretCipher: ciphertext
|
|
402
|
+
}), MAX_LEAVES, 1)
|
|
403
|
+
});
|
|
404
|
+
const preparedClaim = obj({ operatorPackages: record(identifier, bytes(1, MAX_PACKAGE_BYTES), MAX_OPERATORS) });
|
|
405
|
+
const preparedLightningReceive = obj({
|
|
406
|
+
paymentHash: bytes32,
|
|
407
|
+
operatorPackages: record(identifier, bytes(1, MAX_PACKAGE_BYTES), MAX_OPERATORS)
|
|
408
|
+
});
|
|
409
|
+
const staticDepositClaimRequest = obj({
|
|
410
|
+
index: int(0, 2147483647),
|
|
411
|
+
statement: staticDepositClaimStatement
|
|
412
|
+
});
|
|
413
|
+
const staticDepositClaimResult = obj({
|
|
414
|
+
encryptedSecretKey: bytes(1, MAX_CIPHERTEXT_BYTES),
|
|
415
|
+
signature: bytes(64, 128)
|
|
416
|
+
});
|
|
417
|
+
const satsAmount = () => int(0, Number.MAX_SAFE_INTEGER);
|
|
418
|
+
const U128_MAX = 340282366920938463463374607431768211455n;
|
|
419
|
+
const decimalU128 = (value) => {
|
|
420
|
+
if (typeof value !== "string" || value.length > 39 || !/^(?:0|[1-9][0-9]*)$/.test(value) || BigInt(value) > U128_MAX) throw new ValidationError();
|
|
421
|
+
return value;
|
|
422
|
+
};
|
|
423
|
+
const approveIntent = union(obj({
|
|
424
|
+
kind: literal("transfer"),
|
|
425
|
+
receivers: arr(obj({
|
|
426
|
+
identityPublicKey: bytes(33),
|
|
427
|
+
amountSats: satsAmount()
|
|
428
|
+
}), 256, 1)
|
|
429
|
+
}), obj({
|
|
430
|
+
kind: literal("lightning"),
|
|
431
|
+
invoice: str(4096),
|
|
432
|
+
amountSats: opt(satsAmount()),
|
|
433
|
+
maxFeeSats: satsAmount()
|
|
434
|
+
}), obj({
|
|
435
|
+
kind: literal("exit"),
|
|
436
|
+
address: str(128),
|
|
437
|
+
amountSats: satsAmount(),
|
|
438
|
+
maxFeeSats: satsAmount()
|
|
439
|
+
}), obj({
|
|
440
|
+
kind: literal("onchain"),
|
|
441
|
+
address: str(128),
|
|
442
|
+
maxFeeSats: satsAmount()
|
|
443
|
+
}), obj({
|
|
444
|
+
kind: literal("token"),
|
|
445
|
+
tokenIdentifier: bytes(32),
|
|
446
|
+
receivers: arr(obj({
|
|
447
|
+
ownerPublicKey: bytes(33),
|
|
448
|
+
amount: decimalU128
|
|
449
|
+
}), 256, 1)
|
|
450
|
+
}));
|
|
451
|
+
const approvalSummary = union(obj({
|
|
452
|
+
kind: literal("transfer"),
|
|
453
|
+
receivers: arr(obj({
|
|
454
|
+
identityPublicKey: bytes(33),
|
|
455
|
+
amountSats: satsAmount()
|
|
456
|
+
}), 256, 1)
|
|
457
|
+
}), obj({
|
|
458
|
+
kind: literal("lightning"),
|
|
459
|
+
paymentHash: bytes(32),
|
|
460
|
+
invoiceAmountSats: opt(satsAmount()),
|
|
461
|
+
maxFeeSats: satsAmount()
|
|
462
|
+
}), obj({
|
|
463
|
+
kind: literal("exit"),
|
|
464
|
+
address: str(128),
|
|
465
|
+
amountSats: satsAmount(),
|
|
466
|
+
maxFeeSats: satsAmount()
|
|
467
|
+
}), obj({
|
|
468
|
+
kind: literal("onchain"),
|
|
469
|
+
address: str(128),
|
|
470
|
+
maxFeeSats: satsAmount()
|
|
471
|
+
}), obj({
|
|
472
|
+
kind: literal("token"),
|
|
473
|
+
tokenIdentifier: bytes(32),
|
|
474
|
+
receivers: arr(obj({
|
|
475
|
+
ownerPublicKey: bytes(33),
|
|
476
|
+
amount: decimalU128
|
|
477
|
+
}), 256, 1)
|
|
478
|
+
}));
|
|
479
|
+
const SPARK_METHODS = {
|
|
480
|
+
"spark.identityPublicKey": {
|
|
481
|
+
session: true,
|
|
482
|
+
presence: false,
|
|
483
|
+
params: obj({ account }),
|
|
484
|
+
result: bytes33
|
|
485
|
+
},
|
|
486
|
+
"spark.publicKeys": {
|
|
487
|
+
session: true,
|
|
488
|
+
presence: false,
|
|
489
|
+
params: obj({
|
|
490
|
+
account,
|
|
491
|
+
keys: arr(sparkLeafKey, 64)
|
|
492
|
+
}),
|
|
493
|
+
result: arr(bytes33, 64)
|
|
494
|
+
},
|
|
495
|
+
"spark.receivedPublicKeys": {
|
|
496
|
+
session: true,
|
|
497
|
+
presence: false,
|
|
498
|
+
params: obj({
|
|
499
|
+
account,
|
|
500
|
+
ciphertexts: arr(ciphertext, 64)
|
|
501
|
+
}),
|
|
502
|
+
result: arr(bytes33, 64)
|
|
503
|
+
},
|
|
504
|
+
"spark.newCommitments": {
|
|
505
|
+
session: true,
|
|
506
|
+
presence: false,
|
|
507
|
+
params: obj({
|
|
508
|
+
account,
|
|
509
|
+
count: int(1, 64)
|
|
510
|
+
}),
|
|
511
|
+
result: arr(signingCommitment, 64)
|
|
512
|
+
},
|
|
513
|
+
"spark.signFrost": {
|
|
514
|
+
session: true,
|
|
515
|
+
presence: false,
|
|
516
|
+
params: obj({
|
|
517
|
+
account,
|
|
518
|
+
jobs: arr(frostSignJob, 64)
|
|
519
|
+
}),
|
|
520
|
+
result: arr(bytes32, 64)
|
|
521
|
+
},
|
|
522
|
+
"spark.aggregateFrost": {
|
|
523
|
+
session: true,
|
|
524
|
+
presence: false,
|
|
525
|
+
params: obj({
|
|
526
|
+
account,
|
|
527
|
+
jobs: arr(frostAggregateJob, 64)
|
|
528
|
+
}),
|
|
529
|
+
result: arr(bytes64, 64)
|
|
530
|
+
},
|
|
531
|
+
"spark.signIdentity": {
|
|
532
|
+
session: true,
|
|
533
|
+
presence: false,
|
|
534
|
+
params: obj({
|
|
535
|
+
account,
|
|
536
|
+
statement: identityStatement
|
|
537
|
+
}),
|
|
538
|
+
result: bytes(64, 128)
|
|
539
|
+
},
|
|
540
|
+
"spark.signKeySpend": {
|
|
541
|
+
session: true,
|
|
542
|
+
presence: false,
|
|
543
|
+
params: obj({
|
|
544
|
+
account,
|
|
545
|
+
key: sparkKey,
|
|
546
|
+
target: signingTarget
|
|
547
|
+
}),
|
|
548
|
+
result: bytes64
|
|
549
|
+
},
|
|
550
|
+
"spark.signHtlcTimeout": {
|
|
551
|
+
session: true,
|
|
552
|
+
presence: false,
|
|
553
|
+
params: obj({
|
|
554
|
+
account,
|
|
555
|
+
request: htlcTimeoutRequest
|
|
556
|
+
}),
|
|
557
|
+
result: bytes64
|
|
558
|
+
},
|
|
559
|
+
"spark.prepareTransfer": {
|
|
560
|
+
session: true,
|
|
561
|
+
presence: false,
|
|
562
|
+
params: obj({
|
|
563
|
+
account,
|
|
564
|
+
request: prepareTransferRequest
|
|
565
|
+
}),
|
|
566
|
+
result: preparedTransfer
|
|
567
|
+
},
|
|
568
|
+
"spark.prepareClaim": {
|
|
569
|
+
session: true,
|
|
570
|
+
presence: false,
|
|
571
|
+
params: obj({
|
|
572
|
+
account,
|
|
573
|
+
request: prepareClaimRequest
|
|
574
|
+
}),
|
|
575
|
+
result: preparedClaim
|
|
576
|
+
},
|
|
577
|
+
"spark.prepareLightningReceive": {
|
|
578
|
+
session: true,
|
|
579
|
+
presence: false,
|
|
580
|
+
params: obj({ account }),
|
|
581
|
+
result: preparedLightningReceive
|
|
582
|
+
},
|
|
583
|
+
"spark.claimStaticDeposit": {
|
|
584
|
+
session: true,
|
|
585
|
+
presence: false,
|
|
586
|
+
params: obj({
|
|
587
|
+
account,
|
|
588
|
+
request: staticDepositClaimRequest
|
|
589
|
+
}),
|
|
590
|
+
result: staticDepositClaimResult
|
|
591
|
+
},
|
|
592
|
+
"spark.approve": {
|
|
593
|
+
session: true,
|
|
594
|
+
presence: true,
|
|
595
|
+
params: obj({
|
|
596
|
+
account,
|
|
597
|
+
intent: approveIntent,
|
|
598
|
+
verify: factor
|
|
599
|
+
}),
|
|
600
|
+
result: approvalSummary
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
//#endregion
|
|
604
|
+
//#region src/protocol/index.ts
|
|
605
|
+
const PROTOCOL_VERSION = 1;
|
|
606
|
+
/**
|
|
607
|
+
* Responses to requests whose `id` cannot be parsed (malformed envelope) carry id 0. Main-side
|
|
608
|
+
* request ids start at 1, so a `0` is never a real pending request.
|
|
609
|
+
*/
|
|
610
|
+
const UNATTRIBUTED_ID = 0;
|
|
611
|
+
/**
|
|
612
|
+
* The worst-case encoded size of one request, enforced by both sides: the host refuses to post a
|
|
613
|
+
* request over the cap and the kernel answers an oversized raw request with `invalid-input`.
|
|
614
|
+
* Responses are not capped.
|
|
615
|
+
*/
|
|
616
|
+
const MAX_REQUEST_BYTES = 1048576;
|
|
617
|
+
/**
|
|
618
|
+
* Upper-bound byte weight of a protocol value: a string costs `length * 3` (worst-case UTF-8), a
|
|
619
|
+
* Uint8Array its `byteLength`, every scalar and every array element 8, and every object key
|
|
620
|
+
* `key.length * 3 + 8` plus its value. Iterative — a deeply nested value cannot overflow the
|
|
621
|
+
* stack.
|
|
622
|
+
*/
|
|
623
|
+
function requestBytes(value) {
|
|
624
|
+
let total = 0;
|
|
625
|
+
const stack = [value];
|
|
626
|
+
while (stack.length > 0) {
|
|
627
|
+
const item = stack.pop();
|
|
628
|
+
if (item === void 0) continue;
|
|
629
|
+
if (typeof item === "string") total += item.length * 3;
|
|
630
|
+
else if (item instanceof Uint8Array) total += item.byteLength;
|
|
631
|
+
else if (item === null || typeof item === "boolean" || typeof item === "number") total += 8;
|
|
632
|
+
else if (Array.isArray(item)) {
|
|
633
|
+
const elements = item;
|
|
634
|
+
total += elements.length * 8;
|
|
635
|
+
for (const element of elements) stack.push(element);
|
|
636
|
+
} else for (const [key, child] of Object.entries(item)) {
|
|
637
|
+
total += key.length * 3 + 8;
|
|
638
|
+
stack.push(child);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
return total;
|
|
642
|
+
}
|
|
643
|
+
/** Deep check that a structured-cloned value is a strict ProtocolValue (no cycles detected). */
|
|
644
|
+
function isProtocolValue(value) {
|
|
645
|
+
switch (typeof value) {
|
|
646
|
+
case "boolean":
|
|
647
|
+
case "string": return true;
|
|
648
|
+
case "number": return Number.isSafeInteger(value);
|
|
649
|
+
case "object": break;
|
|
650
|
+
default: return false;
|
|
651
|
+
}
|
|
652
|
+
if (value === null || value instanceof Uint8Array) return true;
|
|
653
|
+
if (Array.isArray(value)) return value.every(isProtocolValue);
|
|
654
|
+
if (Object.getPrototypeOf(value) !== Object.prototype) return false;
|
|
655
|
+
return Object.values(value).every(isProtocolValue);
|
|
656
|
+
}
|
|
657
|
+
/** The status payload: core sections plus one object section per protocol module. */
|
|
658
|
+
const statusPayload = obj({
|
|
659
|
+
protocol: literal(1),
|
|
660
|
+
vault: union(literal("uninitialized"), literal("locked"), literal("unlocked"), literal("corrupted"), literal("unsupported"), literal("unavailable")),
|
|
661
|
+
factors: arr(factorInfo, MAX_SNAPSHOT_ITEMS),
|
|
662
|
+
wallets: arr(walletInfo, MAX_SNAPSHOT_ITEMS),
|
|
663
|
+
nostr: nostrStatus,
|
|
664
|
+
backup: union(backupInfo, nullValue)
|
|
665
|
+
});
|
|
666
|
+
const METHODS = {
|
|
667
|
+
status: {
|
|
668
|
+
session: false,
|
|
669
|
+
presence: false,
|
|
670
|
+
params: nullValue,
|
|
671
|
+
result: statusPayload
|
|
672
|
+
},
|
|
673
|
+
...CORE_METHODS,
|
|
674
|
+
...NOSTR_METHODS,
|
|
675
|
+
...SPARK_METHODS
|
|
676
|
+
};
|
|
677
|
+
const lockReason = union(literal("cold-start"), literal("user"), literal("idle"), literal("background"), literal("expired"), literal("crashed"), literal("invalidated"));
|
|
678
|
+
const errorPayload = obj({
|
|
679
|
+
code: walletErrorCode,
|
|
680
|
+
retryAt: opt(safeTime)
|
|
681
|
+
});
|
|
682
|
+
/** Host-side validators: `kernelEvent` covers the two push messages. */
|
|
683
|
+
const kernelEvent = union(obj({
|
|
684
|
+
v: literal(1),
|
|
685
|
+
event: literal("status"),
|
|
686
|
+
status: statusPayload
|
|
687
|
+
}), obj({
|
|
688
|
+
v: literal(1),
|
|
689
|
+
event: literal("locked"),
|
|
690
|
+
reason: lockReason
|
|
691
|
+
}));
|
|
692
|
+
/** Validates the `err` payload of a response; `ok` is checked against `METHODS[m].result`. */
|
|
693
|
+
const responseError = errorPayload;
|
|
694
|
+
//#endregion
|
|
695
|
+
export { isProtocolValue as a, responseError as c, backupEnvelope as d, backupEnvelopeFactor as f, backupInfo as h, UNATTRIBUTED_ID as i, SPARK_METHODS as l, backupHeader as m, METHODS as n, kernelEvent as o, backupEnvelopeText as p, PROTOCOL_VERSION as r, requestBytes as s, MAX_REQUEST_BYTES as t, NOSTR_METHODS as u };
|
|
696
|
+
|
|
697
|
+
//# sourceMappingURL=protocol-D9xBuMzq.mjs.map
|