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