@arkade-os/sdk 0.4.26 → 0.4.27

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.
Files changed (47) hide show
  1. package/README.md +5 -25
  2. package/dist/cjs/contracts/contractManager.js +31 -11
  3. package/dist/cjs/contracts/contractWatcher.js +2 -2
  4. package/dist/cjs/identity/hdCapableIdentity.js +18 -0
  5. package/dist/cjs/identity/index.js +3 -1
  6. package/dist/cjs/identity/seedIdentity.js +16 -0
  7. package/dist/cjs/index.js +4 -2
  8. package/dist/cjs/wallet/delegator.js +10 -4
  9. package/dist/cjs/wallet/hdDescriptorProvider.js +29 -0
  10. package/dist/cjs/wallet/inputSignerRouter.js +98 -0
  11. package/dist/cjs/wallet/serviceWorker/wallet.js +1 -0
  12. package/dist/cjs/wallet/signingErrors.js +32 -0
  13. package/dist/cjs/wallet/unroll.js +5 -1
  14. package/dist/cjs/wallet/wallet.js +232 -86
  15. package/dist/cjs/wallet/walletReceiveRotator.js +547 -0
  16. package/dist/cjs/worker/messageBus.js +1 -0
  17. package/dist/esm/contracts/contractManager.js +31 -11
  18. package/dist/esm/contracts/contractWatcher.js +2 -2
  19. package/dist/esm/identity/hdCapableIdentity.js +17 -1
  20. package/dist/esm/identity/index.js +1 -0
  21. package/dist/esm/identity/seedIdentity.js +16 -0
  22. package/dist/esm/index.js +2 -2
  23. package/dist/esm/wallet/delegator.js +10 -4
  24. package/dist/esm/wallet/hdDescriptorProvider.js +29 -0
  25. package/dist/esm/wallet/inputSignerRouter.js +94 -0
  26. package/dist/esm/wallet/serviceWorker/wallet.js +1 -0
  27. package/dist/esm/wallet/signingErrors.js +27 -0
  28. package/dist/esm/wallet/unroll.js +5 -1
  29. package/dist/esm/wallet/wallet.js +231 -86
  30. package/dist/esm/wallet/walletReceiveRotator.js +540 -0
  31. package/dist/esm/worker/messageBus.js +1 -0
  32. package/dist/types/contracts/contractManager.d.ts +33 -3
  33. package/dist/types/contracts/types.d.ts +19 -2
  34. package/dist/types/identity/descriptorProvider.d.ts +7 -0
  35. package/dist/types/identity/hdCapableIdentity.d.ts +30 -3
  36. package/dist/types/identity/index.d.ts +1 -0
  37. package/dist/types/identity/seedIdentity.d.ts +16 -0
  38. package/dist/types/index.d.ts +6 -6
  39. package/dist/types/wallet/hdDescriptorProvider.d.ts +22 -1
  40. package/dist/types/wallet/index.d.ts +34 -0
  41. package/dist/types/wallet/inputSignerRouter.d.ts +35 -0
  42. package/dist/types/wallet/serviceWorker/wallet.d.ts +10 -0
  43. package/dist/types/wallet/signingErrors.d.ts +19 -0
  44. package/dist/types/wallet/wallet.d.ts +51 -2
  45. package/dist/types/wallet/walletReceiveRotator.d.ts +306 -0
  46. package/dist/types/worker/messageBus.d.ts +1 -0
  47. package/package.json +1 -1
@@ -0,0 +1,540 @@
1
+ import { expand, networks } from "@bitcoinerlab/descriptors-scure";
2
+ import { equalBytes } from "@scure/btc-signer/utils.js";
3
+ import { hex } from "@scure/base";
4
+ import { isMainnetDescriptor } from "../identity/descriptor.js";
5
+ import { isHDCapableIdentity } from "../identity/hdCapableIdentity.js";
6
+ import { DefaultVtxo } from "../script/default.js";
7
+ import { DelegateVtxo } from "../script/delegate.js";
8
+ import { timelockToSequence } from "../utils/timelock.js";
9
+ import { HDDescriptorProvider } from "./hdDescriptorProvider.js";
10
+ /** Type guard: does this provider implement {@link ReceiveRotatorFactory}? */
11
+ export function hasReceiveRotatorFactory(provider) {
12
+ return (typeof provider
13
+ .createReceiveRotator === "function");
14
+ }
15
+ function hasPeekableDescriptor(provider) {
16
+ return (typeof provider
17
+ .getCurrentSigningDescriptor === "function");
18
+ }
19
+ /**
20
+ * Sentinel value stored in `contract.metadata.source` to identify the
21
+ * wallet's current display contract. Borrowed from btcpay-arkade's
22
+ * source-tagging pattern: every contract records "where and why it was
23
+ * generated", and the wallet only cares about the ones it generated for
24
+ * its own receive address.
25
+ *
26
+ * Tagging makes the boot lookup unambiguous — the rotator filters on
27
+ * `metadata.source === WALLET_RECEIVE_SOURCE` rather than on "any active
28
+ * default contract", so a contract repo that also holds default contracts
29
+ * created for other reasons (legacy timelock variants, external
30
+ * integrations) doesn't confuse the wallet's display state.
31
+ */
32
+ export const WALLET_RECEIVE_SOURCE = "wallet-receive";
33
+ /**
34
+ * Thrown when a descriptor expected to be rangeable (have a wildcard
35
+ * leaf) cannot produce a leaf pubkey. Surfaces from the rotator's
36
+ * `defaultBoot` path so `resolveBoot` can distinguish a legitimate
37
+ * incompatibility (silent fallback under `walletMode: 'auto'`) from
38
+ * any other runtime failure.
39
+ */
40
+ export class NonRangeableDescriptorError extends Error {
41
+ constructor(message, options) {
42
+ super(message, options);
43
+ this.name = "NonRangeableDescriptorError";
44
+ }
45
+ }
46
+ /**
47
+ * Cap on the exponential backoff applied to repeated rotation
48
+ * failures. After this delay, every fresh `vtxo_received` event
49
+ * re-attempts a rotation at this rate until one succeeds (which
50
+ * resets the counter) or the wallet is disposed.
51
+ */
52
+ export const ROTATION_MAX_BACKOFF_MS = 60000;
53
+ /**
54
+ * Owns the wallet's HD receive-rotation lifecycle.
55
+ *
56
+ * The rotator is constructed only when the wallet's `walletMode`
57
+ * resolves to a {@link DescriptorProvider}; static wallets and
58
+ * non-HD-capable wallets under `'auto'` never see one.
59
+ *
60
+ * Lifecycle:
61
+ * 1. `resolveBoot()` — pre-Wallet-construction. Resolves the provider
62
+ * from `walletMode`, then either reuses the existing display
63
+ * contract's pubkey (if any) or allocates the first descriptor.
64
+ * Returns the rotator paired with the boot pubkey.
65
+ * 2. `install(wallet)` — post-`getVtxoManager()`. Subscribes to
66
+ * `vtxo_received` on the contract manager and routes matching events
67
+ * through the rotation chain.
68
+ * 3. `dispose()` — tears down the subscription and drains any in-flight
69
+ * rotation so the contract manager can be disposed cleanly.
70
+ *
71
+ * This class follows the dotnet-sdk's split of responsibilities: the
72
+ * provider is a pure rotating allocator; "what address am I currently
73
+ * bound to?" is answered by querying the contract repository, not by
74
+ * asking the provider.
75
+ */
76
+ export class WalletReceiveRotator {
77
+ constructor(provider, priorTaggedScript, logger) {
78
+ this.provider = provider;
79
+ this.chain = Promise.resolve();
80
+ /**
81
+ * Consecutive rotation failures since the last successful rotate.
82
+ * Drives an exponential backoff (capped at
83
+ * {@link ROTATION_MAX_BACKOFF_MS}) so a broken provider can't make
84
+ * the rotator hammer `getNextSigningDescriptor` + `createContract`
85
+ * on every inbound VTXO. Reset to zero on a successful rotate.
86
+ */
87
+ this.consecutiveFailures = 0;
88
+ /**
89
+ * Unix-ms timestamp before which incoming `vtxo_received` events
90
+ * skip the rotation attempt entirely. Zero means "no backoff
91
+ * active" — the next event can rotate immediately.
92
+ */
93
+ this.nextRotationAllowedAt = 0;
94
+ this.currentTaggedScript = priorTaggedScript;
95
+ this.logger = logger ?? console;
96
+ }
97
+ /**
98
+ * Phase 1 — pre-Wallet-construction. Resolves `walletMode` to a
99
+ * {@link DescriptorProvider}, then asks that provider to construct
100
+ * the rotator (delegated through
101
+ * {@link DescriptorProvider.createReceiveRotator}, which falls back
102
+ * to {@link defaultBoot} when the provider doesn't override it).
103
+ *
104
+ * Returns the rotator paired with the offchain tapscript the wallet
105
+ * should actually install (rebuilt to the resolved receive pubkey
106
+ * when it differs from the identity's static pubkey), or
107
+ * `undefined` when the wallet should stay on the static path.
108
+ *
109
+ * Errors during pubkey resolution propagate when:
110
+ * - `walletMode === 'hd'` (caller asked for HD; loud failure expected).
111
+ * - `walletMode` is a {@link DescriptorProvider} (caller supplied an
112
+ * explicit allocator; silently degrading would hide misconfig).
113
+ *
114
+ * Errors are silently swallowed (returning `undefined`) only under
115
+ * `walletMode: 'auto'` with the built-in HD provider, to preserve
116
+ * backwards compatibility with wallets whose identity descriptor
117
+ * isn't actually rangeable.
118
+ */
119
+ static async resolveBoot(config, setup) {
120
+ const provider = await resolveDescriptorProvider(config, setup.walletRepository);
121
+ if (!provider)
122
+ return undefined;
123
+ const allowSilentFallback = (config.walletMode ?? "auto") === "auto";
124
+ const expectedContractType = setup.offchainTapscript instanceof DelegateVtxo.Script
125
+ ? "delegate"
126
+ : "default";
127
+ const factoryOpts = {
128
+ walletRepository: setup.walletRepository,
129
+ contractRepository: setup.contractRepository,
130
+ serverPubKey: setup.serverPubKey,
131
+ expectedContractType,
132
+ };
133
+ let boot;
134
+ try {
135
+ boot = hasReceiveRotatorFactory(provider)
136
+ ? await provider.createReceiveRotator(factoryOpts)
137
+ : await WalletReceiveRotator.defaultBoot(provider, factoryOpts);
138
+ }
139
+ catch (e) {
140
+ // Only swallow non-rangeable-descriptor errors, and only
141
+ // under `walletMode: 'auto'`. Explicit HD/`DescriptorProvider`
142
+ // callers always see the failure.
143
+ if (allowSilentFallback &&
144
+ e instanceof NonRangeableDescriptorError) {
145
+ return undefined;
146
+ }
147
+ throw e;
148
+ }
149
+ if (!boot)
150
+ return undefined;
151
+ // Rebuild the offchain tapscript with the resolved receive
152
+ // pubkey. Skipping the rebuild when pubkeys already match keeps
153
+ // the tapscript instance stable for static / first-boot paths
154
+ // (no allocation churn, no observable change for callers
155
+ // that retain the reference across `Wallet.create`).
156
+ const offchainTapscript = equalBytes(boot.receivePubkey, setup.offchainTapscript.options.pubKey)
157
+ ? setup.offchainTapscript
158
+ : rebuildTapscript(setup.offchainTapscript, boot.receivePubkey);
159
+ return { rotator: boot.rotator, offchainTapscript, provider };
160
+ }
161
+ /**
162
+ * Default factory-shaped boot any
163
+ * {@link ReceiveRotatorFactory.createReceiveRotator} implementation
164
+ * can delegate to. Pulls the wallet's current display contract from
165
+ * the contract repository (or allocates a fresh receive descriptor
166
+ * via the provider when no tagged display contract exists), and
167
+ * returns the rotator paired with the resolved receive pubkey.
168
+ *
169
+ * Used internally by `resolveBoot` when the provider doesn't
170
+ * implement {@link ReceiveRotatorFactory}. Exported so providers
171
+ * that *do* override can still invoke the default work for the
172
+ * parts of the boot path they don't want to customise. Tapscript
173
+ * construction is intentionally NOT in here — that's the
174
+ * orchestrator's job.
175
+ */
176
+ static async defaultBoot(provider, opts) {
177
+ const existing = await pickActiveReceive(opts.contractRepository, opts.serverPubKey, opts.expectedContractType);
178
+ if (existing) {
179
+ return {
180
+ rotator: new WalletReceiveRotator(provider, existing.script, opts.logger),
181
+ receivePubkey: existing.pubKey,
182
+ };
183
+ }
184
+ // No tagged display contract on this repo. Avoid burning a
185
+ // fresh HD index per restart: re-derive the descriptor at the
186
+ // most recently allocated index when the provider supports it
187
+ // (HD-style allocators do; static / one-shot providers don't
188
+ // and fall through to a regular allocation, which is a no-op
189
+ // for them anyway).
190
+ let descriptor;
191
+ if (hasPeekableDescriptor(provider)) {
192
+ descriptor = await provider.getCurrentSigningDescriptor();
193
+ }
194
+ descriptor ?? (descriptor = await provider.getNextSigningDescriptor());
195
+ return {
196
+ rotator: new WalletReceiveRotator(provider, undefined, opts.logger),
197
+ receivePubkey: deriveLeafPubkey(descriptor),
198
+ };
199
+ }
200
+ /**
201
+ * Phase 2 — post-`getVtxoManager()`. Subscribe to `vtxo_received`
202
+ * and trigger a rotation whenever the currently-active display
203
+ * contract receives funds. Old display contracts remain `active`
204
+ * in the repo so earlier shared addresses keep crediting this
205
+ * wallet.
206
+ */
207
+ async install(wallet) {
208
+ const manager = await wallet.getContractManager();
209
+ this.unsubscribe = manager.onContractEvent((event) => {
210
+ if (event.type !== "vtxo_received")
211
+ return;
212
+ if (event.contractScript !== wallet.defaultContractScript)
213
+ return;
214
+ // Serialise rotations: each `vtxo_received` event is its
215
+ // own rotation trigger (BIP-44-style: one receive ⇒ one
216
+ // fresh address), so two rapid events on the same script
217
+ // are *expected* to burn two consecutive HD indices. The
218
+ // chain here only prevents the rotate → rebuild →
219
+ // createContract sequences from interleaving; it does not
220
+ // — and intentionally does not — dedupe events on the same
221
+ // script. `runRotateWithBackoff` owns the failure handling
222
+ // — it logs, increments the consecutive-failure counter,
223
+ // and gates future attempts behind exponential backoff so
224
+ // a broken provider can't make the rotator hammer
225
+ // `createContract` on every event.
226
+ this.chain = this.chain
227
+ .catch(() => undefined)
228
+ .then(() => this.runRotateWithBackoff(wallet));
229
+ });
230
+ }
231
+ /**
232
+ * Run a single rotation attempt, applying exponential backoff on
233
+ * failure. Public-shaped behavior:
234
+ * - During a backoff window: log + skip (no `rotate()` call).
235
+ * - On success: reset failure count and backoff.
236
+ * - On failure: increment counter, schedule next attempt at
237
+ * `min(2^consecutiveFailures * 1s, ROTATION_MAX_BACKOFF_MS)`.
238
+ *
239
+ * Errors are deliberately swallowed (logged, not rethrown) so the
240
+ * surrounding `chain` Promise never settles to rejected — the next
241
+ * `vtxo_received` event must still get a chance to run.
242
+ */
243
+ async runRotateWithBackoff(wallet) {
244
+ const now = Date.now();
245
+ if (now < this.nextRotationAllowedAt) {
246
+ this.logger.error("WalletReceiveRotator: skipping rotation (in backoff)", {
247
+ consecutiveFailures: this.consecutiveFailures,
248
+ retryInMs: this.nextRotationAllowedAt - now,
249
+ });
250
+ return;
251
+ }
252
+ try {
253
+ await this.rotate(wallet);
254
+ this.consecutiveFailures = 0;
255
+ this.nextRotationAllowedAt = 0;
256
+ }
257
+ catch (err) {
258
+ this.consecutiveFailures += 1;
259
+ // 2^1=2s, 2^2=4s, … capped at ROTATION_MAX_BACKOFF_MS (60s).
260
+ // `Math.min` on the exponent prevents `2 ** 1024` overflow
261
+ // for pathologically long failure streaks.
262
+ const exponent = Math.min(this.consecutiveFailures, 16);
263
+ const backoffMs = Math.min(2 ** exponent * 1000, ROTATION_MAX_BACKOFF_MS);
264
+ this.nextRotationAllowedAt = Date.now() + backoffMs;
265
+ this.logger.error("WalletReceiveRotator: rotation failed", err, {
266
+ consecutiveFailures: this.consecutiveFailures,
267
+ nextAttemptInMs: backoffMs,
268
+ });
269
+ }
270
+ }
271
+ /**
272
+ * Wait for any in-flight rotation to complete. Useful in tests
273
+ * that need to observe the post-rotation state after dispatching
274
+ * a `vtxo_received` event synchronously; production code rarely
275
+ * needs to call this directly.
276
+ */
277
+ async drain() {
278
+ await this.chain.catch(() => undefined);
279
+ }
280
+ /**
281
+ * Tear down the subscription first so no late `vtxo_received` event
282
+ * can queue work on a disposing wallet, then drain any in-flight
283
+ * rotation so its `createContract` finishes before the contract
284
+ * manager itself disposes.
285
+ */
286
+ async dispose() {
287
+ if (this.unsubscribe) {
288
+ try {
289
+ this.unsubscribe();
290
+ }
291
+ catch {
292
+ // best-effort teardown
293
+ }
294
+ finally {
295
+ this.unsubscribe = undefined;
296
+ }
297
+ }
298
+ await this.chain.catch(() => undefined);
299
+ }
300
+ /**
301
+ * Allocate the next descriptor, swap it into the wallet's active
302
+ * offchain tapscript, register the new tagged contract, and retire
303
+ * the previous tagged contract (if any) by setting its state to
304
+ * `inactive`. The contract watcher keeps watching inactive
305
+ * contracts until their VTXOs are spent, so funds in flight at the
306
+ * old display address are not lost — only the address stops being
307
+ * advertised.
308
+ *
309
+ * Contract type matches the wallet's tapscript shape: a default
310
+ * wallet rotates to a new `default` contract, a delegate wallet to
311
+ * a new `delegate` contract.
312
+ *
313
+ * The first rotation on a fresh wallet does NOT deactivate
314
+ * anything: `currentTaggedScript` is `undefined` because the wallet
315
+ * was displaying the untagged index-0 baseline, which must stay
316
+ * active forever.
317
+ */
318
+ async rotate(wallet) {
319
+ // Build the new tapscript + derived strings entirely locally,
320
+ // so the wallet's visible state (`offchainTapscript`,
321
+ // `defaultContractScript`, `getAddress()`) doesn't change
322
+ // until the contract registration has succeeded. If
323
+ // `createContract` throws partway, the wallet is still
324
+ // displaying the OLD (registered) address — no
325
+ // unwatched-display-window.
326
+ const descriptor = await this.provider.getNextSigningDescriptor();
327
+ const pubKey = deriveLeafPubkey(descriptor);
328
+ const newTapscript = rebuildTapscript(wallet.offchainTapscript, pubKey);
329
+ const newScript = hex.encode(newTapscript.pkScript);
330
+ const newAddress = newTapscript
331
+ .address(wallet.network.hrp, wallet.arkServerPublicKey)
332
+ .encode();
333
+ const manager = await wallet.getContractManager();
334
+ const csvTimelock = newTapscript.options.csvTimelock ??
335
+ DefaultVtxo.Script.DEFAULT_TIMELOCK;
336
+ const csvTimelockStr = timelockToSequence(csvTimelock).toString();
337
+ const serverPubKeyHex = hex.encode(newTapscript.options.serverPubKey);
338
+ const baseParams = {
339
+ script: newScript,
340
+ address: newAddress,
341
+ state: "active",
342
+ // Persist the materialized signing descriptor alongside the
343
+ // source tag. The wallet's spending paths read this at sign
344
+ // time to route inputs locked by a rotated pubkey through
345
+ // `DescriptorProvider.signWithDescriptor` instead of the
346
+ // identity's index-0 key. Without it, post-rotation sends
347
+ // produce unsigned PSBTs that the server rejects with
348
+ // `INVALID_PSBT_INPUT (5): missing tapscript spend sig`.
349
+ metadata: {
350
+ source: WALLET_RECEIVE_SOURCE,
351
+ signingDescriptor: descriptor,
352
+ },
353
+ };
354
+ if (newTapscript instanceof DelegateVtxo.Script) {
355
+ await manager.createContract({
356
+ ...baseParams,
357
+ type: "delegate",
358
+ params: {
359
+ pubKey: hex.encode(pubKey),
360
+ serverPubKey: serverPubKeyHex,
361
+ delegatePubKey: hex.encode(newTapscript.options.delegatePubKey),
362
+ csvTimelock: csvTimelockStr,
363
+ },
364
+ });
365
+ }
366
+ else {
367
+ await manager.createContract({
368
+ ...baseParams,
369
+ type: "default",
370
+ params: {
371
+ pubKey: hex.encode(pubKey),
372
+ serverPubKey: serverPubKeyHex,
373
+ csvTimelock: csvTimelockStr,
374
+ },
375
+ });
376
+ }
377
+ // Persistence succeeded — commit the new tapscript to the
378
+ // wallet's visible state. From this point onward
379
+ // `wallet.defaultContractScript` and `getAddress()` reflect
380
+ // the rotated identity. `setOffchainTapscriptForRotation` is
381
+ // the only write path; the field is read-only otherwise.
382
+ wallet.setOffchainTapscriptForRotation(newTapscript);
383
+ // Retire the previous tagged contract (if any). The order
384
+ // matters: deactivate FIRST, then update `currentTaggedScript`,
385
+ // so that if `setContractState` throws the next rotation will
386
+ // retry deactivating the same orphaned contract instead of
387
+ // racing forward and orphaning the new one.
388
+ const previousTagged = this.currentTaggedScript;
389
+ if (previousTagged !== undefined && previousTagged !== newScript) {
390
+ await manager.setContractState(previousTagged, "inactive");
391
+ }
392
+ this.currentTaggedScript = newScript;
393
+ }
394
+ }
395
+ /**
396
+ * Extract the x-only (32-byte) pubkey from a materialized HD descriptor.
397
+ *
398
+ * `expand()` populates `@0.pubkey` for non-ranged descriptors (including
399
+ * HD ones where a concrete child index has been substituted for the
400
+ * wildcard). This sidesteps `extractPubKey`, which intentionally rejects
401
+ * any descriptor carrying a `bip32` key because it was designed for
402
+ * static `tr(pubkey)` inputs.
403
+ */
404
+ function deriveLeafPubkey(descriptor) {
405
+ const network = isMainnetDescriptor(descriptor)
406
+ ? networks.bitcoin
407
+ : networks.testnet;
408
+ // `expand` raises when the descriptor still carries a wildcard or
409
+ // is otherwise non-rangeable. Wrap so callers (most importantly
410
+ // `resolveBoot`'s silent-fallback path) can branch on a typed
411
+ // error class instead of grepping `err.message`.
412
+ let expansion;
413
+ try {
414
+ expansion = expand({ descriptor, network });
415
+ }
416
+ catch (e) {
417
+ throw new NonRangeableDescriptorError(`Cannot derive leaf pubkey from descriptor (length=${descriptor.length}): ` +
418
+ `ensure the descriptor is materialized (no wildcard) and parsable.`, { cause: e });
419
+ }
420
+ const key = expansion.expansionMap?.["@0"];
421
+ if (!key?.pubkey) {
422
+ // Avoid interpolating the descriptor itself: it normally
423
+ // contains an xpub, but a misconfigured caller could pass an
424
+ // xprv, and error messages surface in logs / crash reporters /
425
+ // Sentry. The length is enough context for debugging.
426
+ throw new NonRangeableDescriptorError(`Cannot derive leaf pubkey from descriptor (length=${descriptor.length}): ` +
427
+ `descriptor parsed but no '@0' pubkey was found in the expansion map. ` +
428
+ `The rotator expects a materialized tr(xpub/.../*) shape; ensure the ` +
429
+ `descriptor has no wildcard and that its key resolves into the '@0' slot.`);
430
+ }
431
+ return key.pubkey;
432
+ }
433
+ /**
434
+ * Rebuild the given offchain tapscript with a different owner pubkey,
435
+ * preserving its {@link DelegateVtxo.Script} vs {@link DefaultVtxo.Script}
436
+ * shape and all other options.
437
+ *
438
+ * Exported because the wallet's boot path also needs to rebuild the
439
+ * initial tapscript when the resolved boot pubkey differs from the
440
+ * identity's default pubkey.
441
+ */
442
+ export function rebuildTapscript(current, pubKey) {
443
+ if (current instanceof DelegateVtxo.Script) {
444
+ return new DelegateVtxo.Script({ ...current.options, pubKey });
445
+ }
446
+ return new DefaultVtxo.Script({ ...current.options, pubKey });
447
+ }
448
+ /**
449
+ * Look up the most-recently-created active tagged display contract that
450
+ * this wallet itself generated. Returns the contract's pubkey + script,
451
+ * or `undefined` when no such contract exists — the caller should treat
452
+ * that as "fresh wallet (or static-only history) on this repo" and
453
+ * allocate a new descriptor.
454
+ *
455
+ * Filters by `serverPubKey` so a contract repo seeded against a different
456
+ * server doesn't accidentally resurrect an unrelated pubkey, and by the
457
+ * `metadata.source` sentinel so untagged baseline contracts (and
458
+ * contracts created by other code paths — legacy timelock registrations,
459
+ * external integrations) are not mistaken for the wallet's display
460
+ * address.
461
+ *
462
+ * When `expectedType` is provided, only contracts of that type are considered,
463
+ * preventing a "default" wallet from accidentally picking up a "delegate" contract
464
+ * or vice versa.
465
+ */
466
+ async function pickActiveReceive(contractRepository, serverPubKey, expectedType) {
467
+ // Both `default` and `delegate` contract types can be the wallet's
468
+ // display address (delegate wallets use the delegate variant). The
469
+ // `metadata.source` tag is the discriminator that says "this is the
470
+ // one I generated for myself."
471
+ const candidates = await contractRepository.getContracts({
472
+ type: expectedType ? [expectedType] : ["default", "delegate"],
473
+ state: "active",
474
+ });
475
+ const serverPubKeyHex = hex.encode(serverPubKey);
476
+ const matching = candidates
477
+ .filter((c) => c.params.serverPubKey === serverPubKeyHex &&
478
+ c.metadata?.source === WALLET_RECEIVE_SOURCE)
479
+ .sort((a, b) => b.createdAt - a.createdAt);
480
+ const newest = matching[0];
481
+ if (!newest?.params.pubKey)
482
+ return undefined;
483
+ try {
484
+ return {
485
+ pubKey: hex.decode(newest.params.pubKey),
486
+ script: newest.script,
487
+ };
488
+ }
489
+ catch {
490
+ return undefined;
491
+ }
492
+ }
493
+ /**
494
+ * Resolve the polymorphic `walletMode` config field into a concrete
495
+ * {@link DescriptorProvider} (or `undefined` for the static path).
496
+ *
497
+ * - `'auto'` *(default)*: **short-term**, behaves like `'static'` — no
498
+ * HD rotation. See the `TODO` below for the criteria to flip this
499
+ * back to the identity-probing behaviour.
500
+ * - `'static'`: returns `undefined`.
501
+ * - A {@link DescriptorProvider} instance: returns it as-is.
502
+ * - `'hd'`: builds the built-in HD provider from the identity. Throws
503
+ * if the identity isn't HD-capable or the descriptor isn't rangeable —
504
+ * no silent fallback.
505
+ */
506
+ async function resolveDescriptorProvider(config, walletRepository) {
507
+ const mode = config.walletMode ?? "auto";
508
+ // TODO(hd-maturation): TEMPORARY — collapse `'auto'` into `'static'`
509
+ // until the HD receive-rotation pipeline has soaked in the field.
510
+ // Flip `'auto'` back to its identity-probing behaviour once:
511
+ // 1. At least one consumer (btcpay-arkade, arkade-os/wallet,
512
+ // Fulmine) has been running with `walletMode: 'hd'` against
513
+ // mainnet for ≥ 1 month with no rotation-induced fund-loss
514
+ // or address-drift reports.
515
+ // 2. The test `default ('auto') currently behaves like 'static'`
516
+ // in `test/walletHdRotation.test.ts` is flipped in the same
517
+ // commit (it's the explicit gate — flipping the default
518
+ // MUST flip the test).
519
+ // 3. The `WalletMode` docstring in `src/wallet/index.ts` is
520
+ // updated to drop the "behaves like 'static' for now" notice.
521
+ if (mode === "static" || mode === "auto")
522
+ return undefined;
523
+ if (typeof mode !== "string") {
524
+ // Caller supplied a DescriptorProvider directly.
525
+ return mode;
526
+ }
527
+ // mode === 'hd'
528
+ if (!isHDCapableIdentity(config.identity)) {
529
+ throw new Error("walletMode 'hd' requires an HD-capable identity " +
530
+ "(SeedIdentity / MnemonicIdentity with a rangeable BIP-32 " +
531
+ "descriptor) or an explicit DescriptorProvider.");
532
+ }
533
+ try {
534
+ return await HDDescriptorProvider.create(config.identity, walletRepository);
535
+ }
536
+ catch (e) {
537
+ throw new Error("walletMode 'hd' failed to initialize: " +
538
+ (e instanceof Error ? e.message : String(e)), { cause: e });
539
+ }
540
+ }
@@ -179,6 +179,7 @@ export class MessageBus {
179
179
  storage,
180
180
  delegatorProvider,
181
181
  settlementConfig: config.settlementConfig,
182
+ walletMode: config.walletMode,
182
183
  watcherConfig: config.watcherConfig,
183
184
  });
184
185
  return { wallet, arkProvider, readonlyWallet: wallet };
@@ -8,6 +8,23 @@ export type RefreshVtxosOptions = {
8
8
  scripts?: string[];
9
9
  after?: number;
10
10
  before?: number;
11
+ /**
12
+ * When true and `scripts` is not set, refresh every contract in
13
+ * the repository — including those marked `inactive` and those
14
+ * that have dropped out of the watcher's active set. Useful for
15
+ * "did anyone send funds to a stale rotated display address?"
16
+ * audits.
17
+ *
18
+ * Because this is a *superset* of the watcher's watched set, the
19
+ * cursor invariant still holds and the cursor advances normally
20
+ * (unless an explicit `after` / `before` window is also supplied).
21
+ *
22
+ * Ignored when `scripts` is set (the explicit list already
23
+ * specifies what to refresh, regardless of contract state).
24
+ *
25
+ * @defaultValue `false`
26
+ */
27
+ includeInactive?: boolean;
11
28
  };
12
29
  export interface IContractManager extends Disposable {
13
30
  /**
@@ -320,9 +337,22 @@ export declare class ContractManager implements IContractManager {
320
337
  /**
321
338
  * Force refresh virtual outputs from the indexer.
322
339
  *
323
- * Without options, re-fetches every contract and advances the global cursor.
324
- * With options, narrows the refresh to specific scripts and/or a time window.
325
- * Subset refreshes (scripts filter) intentionally do not advance the cursor.
340
+ * Without options, re-fetches every contract in the watcher's
341
+ * watched set and advances the global cursor.
342
+ *
343
+ * `scripts` narrows the refresh to a specific list (subset query —
344
+ * cursor is not advanced because contracts outside the list may
345
+ * have data we'd skip).
346
+ *
347
+ * `includeInactive: true` (and no `scripts`) widens the refresh to
348
+ * every contract in the repository, including ones marked
349
+ * `inactive` and ones that have dropped out of the watcher's
350
+ * active set. This is a *superset* of the watched set, so the
351
+ * cursor invariant still holds and the cursor advances normally.
352
+ *
353
+ * `after` / `before` apply a caller-supplied time window. The
354
+ * cursor never advances on a windowed query because the window
355
+ * may skip data outside its bounds.
326
356
  */
327
357
  refreshVtxos(opts?: RefreshVtxosOptions): Promise<void>;
328
358
  refreshOutpoints(outpoints: Outpoint[]): Promise<void>;
@@ -1,6 +1,6 @@
1
1
  import { Bytes } from "@scure/btc-signer/utils.js";
2
2
  import { EncodedVtxoScript, TapLeafScript, VtxoScript } from "../script/base.js";
3
- import { VirtualCoin, TapLeaves } from "../wallet/index.js";
3
+ import { ExtendedVirtualCoin, VirtualCoin, TapLeaves } from "../wallet/index.js";
4
4
  import { ContractFilter } from "../repositories/index.js";
5
5
  /**
6
6
  * Contract state indicating whether it should be actively monitored.
@@ -70,6 +70,23 @@ export type ContractVtxo = VirtualCoin & Partial<TapLeaves & EncodedVtxoScript>
70
70
  extraWitness?: Bytes[];
71
71
  contractScript: string;
72
72
  };
73
+ /**
74
+ * A {@link ContractVtxo} with all taproot annotation fields required.
75
+ *
76
+ * Mirrors the {@link ExtendedVirtualCoin} / {@link VirtualCoin} split:
77
+ * - {@link ContractVtxo} carries `TapLeaves` and `EncodedVtxoScript` as
78
+ * `Partial<>` because VTXOs fetched raw from the indexer do not yet have
79
+ * taproot data.
80
+ * - `ExtendedContractVtxo` narrows those fields to required, guaranteeing
81
+ * that `annotateVtxos` has run and the taproot leaves are present.
82
+ *
83
+ * Use this type (instead of {@link ContractVtxo}) wherever the compiler
84
+ * should enforce that annotation has happened — e.g. `saveVtxos` and
85
+ * forfeit transaction construction.
86
+ */
87
+ export type ExtendedContractVtxo = ExtendedVirtualCoin & {
88
+ contractScript: string;
89
+ };
73
90
  /**
74
91
  * Result of path selection, including the tapleaf to use and any extra witness data.
75
92
  */
@@ -218,7 +235,7 @@ export type GetContractsFilter = ContractFilter;
218
235
  */
219
236
  export type ContractWithVtxos = {
220
237
  contract: Contract;
221
- vtxos: ContractVtxo[];
238
+ vtxos: ExtendedContractVtxo[];
222
239
  };
223
240
  /**
224
241
  * Summary of a contract's balance.
@@ -18,6 +18,13 @@ export interface DescriptorSigningRequest {
18
18
  * The provider has no read accessor for "current" — it is a pure descriptor
19
19
  * allocator. "What addresses am I currently bound to?" is a question the
20
20
  * contract repository answers, not the provider.
21
+ *
22
+ * Providers that want to participate in HD receive rotation can also
23
+ * implement the wallet-side `ReceiveRotatorFactory` interface (see
24
+ * `src/wallet/walletReceiveRotator.ts`). That extension is opt-in — the
25
+ * core `DescriptorProvider` contract intentionally stays free of
26
+ * wallet-specific concerns so HSM-backed and other minimal providers
27
+ * don't have to know about the receive-rotation lifecycle.
21
28
  */
22
29
  export interface DescriptorProvider {
23
30
  /**