@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,306 @@
1
+ import { DescriptorProvider } from "../identity/descriptorProvider.js";
2
+ import { ContractRepository } from "../repositories/contractRepository.js";
3
+ import { WalletRepository } from "../repositories/walletRepository.js";
4
+ import { IContractManager } from "../contracts/contractManager.js";
5
+ import { DefaultVtxo } from "../script/default.js";
6
+ import { DelegateVtxo } from "../script/delegate.js";
7
+ import type { WalletConfig } from "./index.js";
8
+ /**
9
+ * Inputs the wallet hands to a {@link ReceiveRotatorFactory} when
10
+ * asking it to construct the rotator at boot. The factory uses these
11
+ * to look up the wallet's current display contract (or allocate a
12
+ * fresh receive descriptor). Note: no `offchainTapscript` here — the
13
+ * factory's job is allocation, not script construction. The wallet's
14
+ * orchestrator (`WalletReceiveRotator.resolveBoot`) handles the
15
+ * tapscript rebuild on top of the factory's result.
16
+ */
17
+ export interface ReceiveRotatorBootOpts {
18
+ walletRepository: WalletRepository;
19
+ contractRepository: ContractRepository;
20
+ serverPubKey: Uint8Array;
21
+ /**
22
+ * Expected contract family ("default" or "delegate"). When provided,
23
+ * boot will only consider contracts of this type when looking up the
24
+ * wallet's current display contract, preventing a default wallet from
25
+ * accidentally picking up a delegate contract or vice versa.
26
+ */
27
+ expectedContractType?: "default" | "delegate";
28
+ /**
29
+ * Logger to receive rotation-failure + backoff diagnostics. Defaults
30
+ * to `console` when omitted. Any object implementing
31
+ * {@link Logger.error} works (winston, pino, Sentry breadcrumbs,
32
+ * the runtime's own logger).
33
+ */
34
+ logger?: Logger;
35
+ }
36
+ /**
37
+ * Output of {@link ReceiveRotatorFactory.createReceiveRotator}: the
38
+ * constructed rotator paired with the receive pubkey it resolved at
39
+ * boot (either the existing tagged display contract's pubkey, or a
40
+ * freshly allocated one).
41
+ */
42
+ export interface ReceiveRotatorBoot {
43
+ rotator: WalletReceiveRotator;
44
+ receivePubkey: Uint8Array;
45
+ }
46
+ /**
47
+ * Result returned by {@link WalletReceiveRotator.resolveBoot} to the
48
+ * wallet: the rotator plus the offchain tapscript the wallet should
49
+ * actually use (rebuilt to the resolved boot pubkey when it differs
50
+ * from the identity's static pubkey), plus the {@link DescriptorProvider}
51
+ * the rotator was built around. The wallet retains the provider so
52
+ * spending paths can route per-input signing through
53
+ * {@link DescriptorProvider.signWithDescriptor} instead of the
54
+ * identity's index-0 key.
55
+ */
56
+ export interface ReceiveRotatorBootResult {
57
+ rotator: WalletReceiveRotator;
58
+ offchainTapscript: DefaultVtxo.Script | DelegateVtxo.Script;
59
+ provider: DescriptorProvider;
60
+ }
61
+ /**
62
+ * Opt-in extension to {@link DescriptorProvider} for providers that
63
+ * drive HD receive rotation. Implemented by {@link HDDescriptorProvider}
64
+ * out of the box; custom providers (HSMs, external signers, …) can also
65
+ * implement it when they want to participate.
66
+ *
67
+ * Kept out of the core `DescriptorProvider` interface so providers that
68
+ * only do allocation + signing don't have to know about the wallet's
69
+ * receive lifecycle. The wallet detects support via
70
+ * {@link hasReceiveRotatorFactory} (a duck-typed `instanceof`-style
71
+ * check) and falls back to {@link WalletReceiveRotator.defaultBoot}
72
+ * when the provider doesn't implement the extension.
73
+ */
74
+ export interface ReceiveRotatorFactory {
75
+ createReceiveRotator(opts: ReceiveRotatorBootOpts): Promise<ReceiveRotatorBoot | undefined>;
76
+ }
77
+ /** Type guard: does this provider implement {@link ReceiveRotatorFactory}? */
78
+ export declare function hasReceiveRotatorFactory(provider: DescriptorProvider): provider is DescriptorProvider & ReceiveRotatorFactory;
79
+ /**
80
+ * Sentinel value stored in `contract.metadata.source` to identify the
81
+ * wallet's current display contract. Borrowed from btcpay-arkade's
82
+ * source-tagging pattern: every contract records "where and why it was
83
+ * generated", and the wallet only cares about the ones it generated for
84
+ * its own receive address.
85
+ *
86
+ * Tagging makes the boot lookup unambiguous — the rotator filters on
87
+ * `metadata.source === WALLET_RECEIVE_SOURCE` rather than on "any active
88
+ * default contract", so a contract repo that also holds default contracts
89
+ * created for other reasons (legacy timelock variants, external
90
+ * integrations) doesn't confuse the wallet's display state.
91
+ */
92
+ export declare const WALLET_RECEIVE_SOURCE = "wallet-receive";
93
+ /**
94
+ * Thrown when a descriptor expected to be rangeable (have a wildcard
95
+ * leaf) cannot produce a leaf pubkey. Surfaces from the rotator's
96
+ * `defaultBoot` path so `resolveBoot` can distinguish a legitimate
97
+ * incompatibility (silent fallback under `walletMode: 'auto'`) from
98
+ * any other runtime failure.
99
+ */
100
+ export declare class NonRangeableDescriptorError extends Error {
101
+ constructor(message: string, options?: {
102
+ cause?: unknown;
103
+ });
104
+ }
105
+ /**
106
+ * Minimal logging surface the rotator needs. `console` satisfies it
107
+ * out of the box; SDK consumers can pass a structured logger
108
+ * (winston / pino / Sentry adapter) via {@link ReceiveRotatorBootOpts}
109
+ * to capture rotation failures + backoff diagnostics through their
110
+ * own pipeline.
111
+ */
112
+ export interface Logger {
113
+ error(message: string, ...args: unknown[]): void;
114
+ }
115
+ /**
116
+ * Cap on the exponential backoff applied to repeated rotation
117
+ * failures. After this delay, every fresh `vtxo_received` event
118
+ * re-attempts a rotation at this rate until one succeeds (which
119
+ * resets the counter) or the wallet is disposed.
120
+ */
121
+ export declare const ROTATION_MAX_BACKOFF_MS = 60000;
122
+ /**
123
+ * Narrow surface the rotator needs from the wallet at runtime: the
124
+ * mutable display tapscript, the display contract's script hex, the
125
+ * contract manager (for subscribing + registering rotated contracts),
126
+ * and the display address (for the contract's `address` field).
127
+ *
128
+ * Kept as an interface so the rotator module avoids a circular
129
+ * dependency on `wallet.ts`. `Wallet` implements this surface
130
+ * structurally — no `implements` clause is required.
131
+ */
132
+ export interface RotatableWallet {
133
+ readonly defaultContractScript: string;
134
+ readonly network: {
135
+ hrp: string;
136
+ };
137
+ readonly arkServerPublicKey: Uint8Array;
138
+ readonly offchainTapscript: DefaultVtxo.Script | DelegateVtxo.Script;
139
+ /**
140
+ * @internal Sole sanctioned write path for `offchainTapscript`
141
+ * after construction. The rotator calls this once per rotation
142
+ * after persisting the new display contract.
143
+ */
144
+ setOffchainTapscriptForRotation(tapscript: DefaultVtxo.Script | DelegateVtxo.Script): void;
145
+ getContractManager(): Promise<IContractManager>;
146
+ getAddress(): Promise<string>;
147
+ }
148
+ /**
149
+ * Owns the wallet's HD receive-rotation lifecycle.
150
+ *
151
+ * The rotator is constructed only when the wallet's `walletMode`
152
+ * resolves to a {@link DescriptorProvider}; static wallets and
153
+ * non-HD-capable wallets under `'auto'` never see one.
154
+ *
155
+ * Lifecycle:
156
+ * 1. `resolveBoot()` — pre-Wallet-construction. Resolves the provider
157
+ * from `walletMode`, then either reuses the existing display
158
+ * contract's pubkey (if any) or allocates the first descriptor.
159
+ * Returns the rotator paired with the boot pubkey.
160
+ * 2. `install(wallet)` — post-`getVtxoManager()`. Subscribes to
161
+ * `vtxo_received` on the contract manager and routes matching events
162
+ * through the rotation chain.
163
+ * 3. `dispose()` — tears down the subscription and drains any in-flight
164
+ * rotation so the contract manager can be disposed cleanly.
165
+ *
166
+ * This class follows the dotnet-sdk's split of responsibilities: the
167
+ * provider is a pure rotating allocator; "what address am I currently
168
+ * bound to?" is answered by querying the contract repository, not by
169
+ * asking the provider.
170
+ */
171
+ export declare class WalletReceiveRotator {
172
+ private readonly provider;
173
+ private unsubscribe?;
174
+ private chain;
175
+ /**
176
+ * Script of the most-recent tagged display contract — populated
177
+ * either from the boot-time repo lookup or from the previous
178
+ * `rotate()` call within this session. The next `rotate()` marks
179
+ * this contract `inactive` once the new tagged contract is in
180
+ * place. `undefined` means the wallet's current display is the
181
+ * untagged index-0 baseline (no rotation has happened yet on this
182
+ * repo), and the baseline must NOT be deactivated.
183
+ */
184
+ private currentTaggedScript;
185
+ /**
186
+ * Consecutive rotation failures since the last successful rotate.
187
+ * Drives an exponential backoff (capped at
188
+ * {@link ROTATION_MAX_BACKOFF_MS}) so a broken provider can't make
189
+ * the rotator hammer `getNextSigningDescriptor` + `createContract`
190
+ * on every inbound VTXO. Reset to zero on a successful rotate.
191
+ */
192
+ private consecutiveFailures;
193
+ /**
194
+ * Unix-ms timestamp before which incoming `vtxo_received` events
195
+ * skip the rotation attempt entirely. Zero means "no backoff
196
+ * active" — the next event can rotate immediately.
197
+ */
198
+ private nextRotationAllowedAt;
199
+ private readonly logger;
200
+ private constructor();
201
+ /**
202
+ * Phase 1 — pre-Wallet-construction. Resolves `walletMode` to a
203
+ * {@link DescriptorProvider}, then asks that provider to construct
204
+ * the rotator (delegated through
205
+ * {@link DescriptorProvider.createReceiveRotator}, which falls back
206
+ * to {@link defaultBoot} when the provider doesn't override it).
207
+ *
208
+ * Returns the rotator paired with the offchain tapscript the wallet
209
+ * should actually install (rebuilt to the resolved receive pubkey
210
+ * when it differs from the identity's static pubkey), or
211
+ * `undefined` when the wallet should stay on the static path.
212
+ *
213
+ * Errors during pubkey resolution propagate when:
214
+ * - `walletMode === 'hd'` (caller asked for HD; loud failure expected).
215
+ * - `walletMode` is a {@link DescriptorProvider} (caller supplied an
216
+ * explicit allocator; silently degrading would hide misconfig).
217
+ *
218
+ * Errors are silently swallowed (returning `undefined`) only under
219
+ * `walletMode: 'auto'` with the built-in HD provider, to preserve
220
+ * backwards compatibility with wallets whose identity descriptor
221
+ * isn't actually rangeable.
222
+ */
223
+ static resolveBoot(config: WalletConfig, setup: ReceiveRotatorBootOpts & {
224
+ offchainTapscript: DefaultVtxo.Script | DelegateVtxo.Script;
225
+ }): Promise<ReceiveRotatorBootResult | undefined>;
226
+ /**
227
+ * Default factory-shaped boot any
228
+ * {@link ReceiveRotatorFactory.createReceiveRotator} implementation
229
+ * can delegate to. Pulls the wallet's current display contract from
230
+ * the contract repository (or allocates a fresh receive descriptor
231
+ * via the provider when no tagged display contract exists), and
232
+ * returns the rotator paired with the resolved receive pubkey.
233
+ *
234
+ * Used internally by `resolveBoot` when the provider doesn't
235
+ * implement {@link ReceiveRotatorFactory}. Exported so providers
236
+ * that *do* override can still invoke the default work for the
237
+ * parts of the boot path they don't want to customise. Tapscript
238
+ * construction is intentionally NOT in here — that's the
239
+ * orchestrator's job.
240
+ */
241
+ static defaultBoot(provider: DescriptorProvider, opts: ReceiveRotatorBootOpts): Promise<ReceiveRotatorBoot>;
242
+ /**
243
+ * Phase 2 — post-`getVtxoManager()`. Subscribe to `vtxo_received`
244
+ * and trigger a rotation whenever the currently-active display
245
+ * contract receives funds. Old display contracts remain `active`
246
+ * in the repo so earlier shared addresses keep crediting this
247
+ * wallet.
248
+ */
249
+ install(wallet: RotatableWallet): Promise<void>;
250
+ /**
251
+ * Run a single rotation attempt, applying exponential backoff on
252
+ * failure. Public-shaped behavior:
253
+ * - During a backoff window: log + skip (no `rotate()` call).
254
+ * - On success: reset failure count and backoff.
255
+ * - On failure: increment counter, schedule next attempt at
256
+ * `min(2^consecutiveFailures * 1s, ROTATION_MAX_BACKOFF_MS)`.
257
+ *
258
+ * Errors are deliberately swallowed (logged, not rethrown) so the
259
+ * surrounding `chain` Promise never settles to rejected — the next
260
+ * `vtxo_received` event must still get a chance to run.
261
+ */
262
+ private runRotateWithBackoff;
263
+ /**
264
+ * Wait for any in-flight rotation to complete. Useful in tests
265
+ * that need to observe the post-rotation state after dispatching
266
+ * a `vtxo_received` event synchronously; production code rarely
267
+ * needs to call this directly.
268
+ */
269
+ drain(): Promise<void>;
270
+ /**
271
+ * Tear down the subscription first so no late `vtxo_received` event
272
+ * can queue work on a disposing wallet, then drain any in-flight
273
+ * rotation so its `createContract` finishes before the contract
274
+ * manager itself disposes.
275
+ */
276
+ dispose(): Promise<void>;
277
+ /**
278
+ * Allocate the next descriptor, swap it into the wallet's active
279
+ * offchain tapscript, register the new tagged contract, and retire
280
+ * the previous tagged contract (if any) by setting its state to
281
+ * `inactive`. The contract watcher keeps watching inactive
282
+ * contracts until their VTXOs are spent, so funds in flight at the
283
+ * old display address are not lost — only the address stops being
284
+ * advertised.
285
+ *
286
+ * Contract type matches the wallet's tapscript shape: a default
287
+ * wallet rotates to a new `default` contract, a delegate wallet to
288
+ * a new `delegate` contract.
289
+ *
290
+ * The first rotation on a fresh wallet does NOT deactivate
291
+ * anything: `currentTaggedScript` is `undefined` because the wallet
292
+ * was displaying the untagged index-0 baseline, which must stay
293
+ * active forever.
294
+ */
295
+ private rotate;
296
+ }
297
+ /**
298
+ * Rebuild the given offchain tapscript with a different owner pubkey,
299
+ * preserving its {@link DelegateVtxo.Script} vs {@link DefaultVtxo.Script}
300
+ * shape and all other options.
301
+ *
302
+ * Exported because the wallet's boot path also needs to rebuild the
303
+ * initial tapscript when the resolved boot pubkey differs from the
304
+ * identity's default pubkey.
305
+ */
306
+ export declare function rebuildTapscript(current: DefaultVtxo.Script | DelegateVtxo.Script, pubKey: Uint8Array): DefaultVtxo.Script | DelegateVtxo.Script;
@@ -89,6 +89,7 @@ type Initialize = {
89
89
  indexerUrl?: string;
90
90
  esploraUrl?: string;
91
91
  settlementConfig?: SettlementConfig | false;
92
+ walletMode?: "auto" | "static" | "hd";
92
93
  watcherConfig?: Partial<Omit<ContractWatcherConfig, "indexerProvider">>;
93
94
  /**
94
95
  * Page-supplied per-operation timeout map. Keys are message types
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkade-os/sdk",
3
- "version": "0.4.26",
3
+ "version": "0.4.27",
4
4
  "description": "TypeScript SDK for building Bitcoin wallets using the Arkade protocol",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",