@arkade-os/sdk 0.3.12 → 0.4.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/README.md +483 -54
  2. package/dist/cjs/adapters/expo-db.js +35 -0
  3. package/dist/cjs/asset/assetGroup.js +141 -0
  4. package/dist/cjs/asset/assetId.js +88 -0
  5. package/dist/cjs/asset/assetInput.js +204 -0
  6. package/dist/cjs/asset/assetOutput.js +159 -0
  7. package/dist/cjs/asset/assetRef.js +82 -0
  8. package/dist/cjs/asset/index.js +24 -0
  9. package/dist/cjs/asset/metadata.js +172 -0
  10. package/dist/cjs/asset/packet.js +164 -0
  11. package/dist/cjs/asset/types.js +25 -0
  12. package/dist/cjs/asset/utils.js +105 -0
  13. package/dist/cjs/contracts/arkcontract.js +148 -0
  14. package/dist/cjs/contracts/contractManager.js +436 -0
  15. package/dist/cjs/contracts/contractWatcher.js +567 -0
  16. package/dist/cjs/contracts/handlers/default.js +85 -0
  17. package/dist/cjs/contracts/handlers/delegate.js +89 -0
  18. package/dist/cjs/contracts/handlers/helpers.js +105 -0
  19. package/dist/cjs/contracts/handlers/index.js +19 -0
  20. package/dist/cjs/contracts/handlers/registry.js +89 -0
  21. package/dist/cjs/contracts/handlers/vhtlc.js +193 -0
  22. package/dist/cjs/contracts/index.js +41 -0
  23. package/dist/cjs/contracts/types.js +2 -0
  24. package/dist/cjs/db/manager.js +97 -0
  25. package/dist/cjs/forfeit.js +12 -8
  26. package/dist/cjs/identity/index.js +1 -0
  27. package/dist/cjs/identity/seedIdentity.js +255 -0
  28. package/dist/cjs/index.js +70 -14
  29. package/dist/cjs/intent/index.js +28 -2
  30. package/dist/cjs/providers/ark.js +7 -0
  31. package/dist/cjs/providers/delegator.js +66 -0
  32. package/dist/cjs/providers/expoIndexer.js +5 -0
  33. package/dist/cjs/providers/indexer.js +68 -1
  34. package/dist/cjs/providers/onchain.js +2 -2
  35. package/dist/cjs/providers/utils.js +1 -0
  36. package/dist/cjs/repositories/contractRepository.js +0 -103
  37. package/dist/cjs/repositories/inMemory/contractRepository.js +55 -0
  38. package/dist/cjs/repositories/inMemory/walletRepository.js +80 -0
  39. package/dist/cjs/repositories/index.js +16 -0
  40. package/dist/cjs/repositories/indexedDB/contractRepository.js +187 -0
  41. package/dist/cjs/repositories/indexedDB/db.js +57 -0
  42. package/dist/cjs/repositories/indexedDB/schema.js +159 -0
  43. package/dist/cjs/repositories/indexedDB/walletRepository.js +338 -0
  44. package/dist/cjs/repositories/indexedDB/websqlAdapter.js +144 -0
  45. package/dist/cjs/repositories/migrations/contractRepositoryImpl.js +127 -0
  46. package/dist/cjs/repositories/migrations/fromStorageAdapter.js +66 -0
  47. package/dist/cjs/repositories/migrations/walletRepositoryImpl.js +180 -0
  48. package/dist/cjs/repositories/walletRepository.js +0 -169
  49. package/dist/cjs/script/base.js +54 -0
  50. package/dist/cjs/script/delegate.js +49 -0
  51. package/dist/cjs/storage/asyncStorage.js +4 -1
  52. package/dist/cjs/storage/fileSystem.js +3 -0
  53. package/dist/cjs/storage/inMemory.js +3 -0
  54. package/dist/cjs/storage/indexedDB.js +5 -1
  55. package/dist/cjs/storage/localStorage.js +3 -0
  56. package/dist/cjs/utils/arkTransaction.js +16 -0
  57. package/dist/cjs/utils/transactionHistory.js +50 -0
  58. package/dist/cjs/utils/txSizeEstimator.js +39 -14
  59. package/dist/cjs/wallet/asset-manager.js +338 -0
  60. package/dist/cjs/wallet/asset.js +117 -0
  61. package/dist/cjs/wallet/batch.js +1 -1
  62. package/dist/cjs/wallet/delegator.js +235 -0
  63. package/dist/cjs/wallet/expo/background.js +133 -0
  64. package/dist/cjs/wallet/expo/index.js +9 -0
  65. package/dist/cjs/wallet/expo/wallet.js +231 -0
  66. package/dist/cjs/wallet/onchain.js +57 -12
  67. package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +568 -0
  68. package/dist/cjs/wallet/serviceWorker/wallet.js +383 -102
  69. package/dist/cjs/wallet/unroll.js +7 -2
  70. package/dist/cjs/wallet/utils.js +60 -0
  71. package/dist/cjs/wallet/validation.js +151 -0
  72. package/dist/cjs/wallet/vtxo-manager.js +1 -1
  73. package/dist/cjs/wallet/wallet.js +702 -260
  74. package/dist/cjs/worker/browser/service-worker-manager.js +82 -0
  75. package/dist/cjs/{wallet/serviceWorker → worker/browser}/utils.js +2 -1
  76. package/dist/cjs/worker/expo/asyncStorageTaskQueue.js +78 -0
  77. package/dist/cjs/worker/expo/index.js +12 -0
  78. package/dist/cjs/worker/expo/processors/contractPollProcessor.js +61 -0
  79. package/dist/cjs/worker/expo/processors/index.js +6 -0
  80. package/dist/cjs/worker/expo/taskQueue.js +41 -0
  81. package/dist/cjs/worker/expo/taskRunner.js +57 -0
  82. package/dist/cjs/worker/messageBus.js +252 -0
  83. package/dist/esm/adapters/expo-db.js +27 -0
  84. package/dist/esm/asset/assetGroup.js +137 -0
  85. package/dist/esm/asset/assetId.js +84 -0
  86. package/dist/esm/asset/assetInput.js +199 -0
  87. package/dist/esm/asset/assetOutput.js +154 -0
  88. package/dist/esm/asset/assetRef.js +78 -0
  89. package/dist/esm/asset/index.js +8 -0
  90. package/dist/esm/asset/metadata.js +167 -0
  91. package/dist/esm/asset/packet.js +159 -0
  92. package/dist/esm/asset/types.js +22 -0
  93. package/dist/esm/asset/utils.js +99 -0
  94. package/dist/esm/contracts/arkcontract.js +141 -0
  95. package/dist/esm/contracts/contractManager.js +432 -0
  96. package/dist/esm/contracts/contractWatcher.js +563 -0
  97. package/dist/esm/contracts/handlers/default.js +82 -0
  98. package/dist/esm/contracts/handlers/delegate.js +86 -0
  99. package/dist/esm/contracts/handlers/helpers.js +66 -0
  100. package/dist/esm/contracts/handlers/index.js +12 -0
  101. package/dist/esm/contracts/handlers/registry.js +86 -0
  102. package/dist/esm/contracts/handlers/vhtlc.js +190 -0
  103. package/dist/esm/contracts/index.js +13 -0
  104. package/dist/esm/contracts/types.js +1 -0
  105. package/dist/esm/db/manager.js +92 -0
  106. package/dist/esm/forfeit.js +11 -8
  107. package/dist/esm/identity/index.js +1 -0
  108. package/dist/esm/identity/seedIdentity.js +249 -0
  109. package/dist/esm/index.js +25 -15
  110. package/dist/esm/intent/index.js +28 -2
  111. package/dist/esm/providers/ark.js +7 -0
  112. package/dist/esm/providers/delegator.js +62 -0
  113. package/dist/esm/providers/expoIndexer.js +5 -0
  114. package/dist/esm/providers/indexer.js +68 -1
  115. package/dist/esm/providers/onchain.js +2 -2
  116. package/dist/esm/providers/utils.js +1 -0
  117. package/dist/esm/repositories/contractRepository.js +1 -101
  118. package/dist/esm/repositories/inMemory/contractRepository.js +51 -0
  119. package/dist/esm/repositories/inMemory/walletRepository.js +76 -0
  120. package/dist/esm/repositories/index.js +8 -0
  121. package/dist/esm/repositories/indexedDB/contractRepository.js +183 -0
  122. package/dist/esm/repositories/indexedDB/db.js +42 -0
  123. package/dist/esm/repositories/indexedDB/schema.js +155 -0
  124. package/dist/esm/repositories/indexedDB/walletRepository.js +334 -0
  125. package/dist/esm/repositories/indexedDB/websqlAdapter.js +138 -0
  126. package/dist/esm/repositories/migrations/contractRepositoryImpl.js +121 -0
  127. package/dist/esm/repositories/migrations/fromStorageAdapter.js +58 -0
  128. package/dist/esm/repositories/migrations/walletRepositoryImpl.js +176 -0
  129. package/dist/esm/repositories/walletRepository.js +1 -167
  130. package/dist/esm/script/base.js +21 -1
  131. package/dist/esm/script/delegate.js +46 -0
  132. package/dist/esm/storage/asyncStorage.js +4 -1
  133. package/dist/esm/storage/fileSystem.js +3 -0
  134. package/dist/esm/storage/inMemory.js +3 -0
  135. package/dist/esm/storage/indexedDB.js +5 -1
  136. package/dist/esm/storage/localStorage.js +3 -0
  137. package/dist/esm/utils/arkTransaction.js +15 -0
  138. package/dist/esm/utils/transactionHistory.js +50 -0
  139. package/dist/esm/utils/txSizeEstimator.js +39 -14
  140. package/dist/esm/wallet/asset-manager.js +333 -0
  141. package/dist/esm/wallet/asset.js +111 -0
  142. package/dist/esm/wallet/batch.js +1 -1
  143. package/dist/esm/wallet/delegator.js +231 -0
  144. package/dist/esm/wallet/expo/background.js +128 -0
  145. package/dist/esm/wallet/expo/index.js +2 -0
  146. package/dist/esm/wallet/expo/wallet.js +194 -0
  147. package/dist/esm/wallet/onchain.js +57 -12
  148. package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +564 -0
  149. package/dist/esm/wallet/serviceWorker/wallet.js +382 -101
  150. package/dist/esm/wallet/unroll.js +7 -2
  151. package/dist/esm/wallet/utils.js +55 -0
  152. package/dist/esm/wallet/validation.js +139 -0
  153. package/dist/esm/wallet/vtxo-manager.js +1 -1
  154. package/dist/esm/wallet/wallet.js +704 -229
  155. package/dist/esm/worker/browser/service-worker-manager.js +76 -0
  156. package/dist/esm/{wallet/serviceWorker → worker/browser}/utils.js +2 -1
  157. package/dist/esm/worker/expo/asyncStorageTaskQueue.js +74 -0
  158. package/dist/esm/worker/expo/index.js +4 -0
  159. package/dist/esm/worker/expo/processors/contractPollProcessor.js +58 -0
  160. package/dist/esm/worker/expo/processors/index.js +1 -0
  161. package/dist/esm/worker/expo/taskQueue.js +37 -0
  162. package/dist/esm/worker/expo/taskRunner.js +54 -0
  163. package/dist/esm/worker/messageBus.js +248 -0
  164. package/dist/types/adapters/expo-db.d.ts +7 -0
  165. package/dist/types/asset/assetGroup.d.ts +28 -0
  166. package/dist/types/asset/assetId.d.ts +19 -0
  167. package/dist/types/asset/assetInput.d.ts +46 -0
  168. package/dist/types/asset/assetOutput.d.ts +39 -0
  169. package/dist/types/asset/assetRef.d.ts +25 -0
  170. package/dist/types/asset/index.d.ts +8 -0
  171. package/dist/types/asset/metadata.d.ts +37 -0
  172. package/dist/types/asset/packet.d.ts +27 -0
  173. package/dist/types/asset/types.d.ts +18 -0
  174. package/dist/types/asset/utils.d.ts +21 -0
  175. package/dist/types/contracts/arkcontract.d.ts +101 -0
  176. package/dist/types/contracts/contractManager.d.ts +331 -0
  177. package/dist/types/contracts/contractWatcher.d.ts +192 -0
  178. package/dist/types/contracts/handlers/default.d.ts +19 -0
  179. package/dist/types/contracts/handlers/delegate.d.ts +21 -0
  180. package/dist/types/contracts/handlers/helpers.d.ts +18 -0
  181. package/dist/types/contracts/handlers/index.d.ts +7 -0
  182. package/dist/types/contracts/handlers/registry.d.ts +65 -0
  183. package/dist/types/contracts/handlers/vhtlc.d.ts +32 -0
  184. package/dist/types/contracts/index.d.ts +14 -0
  185. package/dist/types/contracts/types.d.ts +222 -0
  186. package/dist/types/db/manager.d.ts +22 -0
  187. package/dist/types/forfeit.d.ts +2 -1
  188. package/dist/types/identity/index.d.ts +1 -0
  189. package/dist/types/identity/seedIdentity.d.ts +128 -0
  190. package/dist/types/index.d.ts +21 -12
  191. package/dist/types/intent/index.d.ts +2 -1
  192. package/dist/types/providers/ark.d.ts +11 -2
  193. package/dist/types/providers/delegator.d.ts +29 -0
  194. package/dist/types/providers/indexer.d.ts +11 -1
  195. package/dist/types/repositories/contractRepository.d.ts +30 -19
  196. package/dist/types/repositories/inMemory/contractRepository.d.ts +17 -0
  197. package/dist/types/repositories/inMemory/walletRepository.d.ts +26 -0
  198. package/dist/types/repositories/index.d.ts +7 -0
  199. package/dist/types/repositories/indexedDB/contractRepository.d.ts +21 -0
  200. package/dist/types/repositories/indexedDB/db.d.ts +56 -0
  201. package/dist/types/repositories/indexedDB/schema.d.ts +8 -0
  202. package/dist/types/repositories/indexedDB/walletRepository.d.ts +25 -0
  203. package/dist/types/repositories/indexedDB/websqlAdapter.d.ts +49 -0
  204. package/dist/types/repositories/migrations/contractRepositoryImpl.d.ts +24 -0
  205. package/dist/types/repositories/migrations/fromStorageAdapter.d.ts +19 -0
  206. package/dist/types/repositories/migrations/walletRepositoryImpl.d.ts +27 -0
  207. package/dist/types/repositories/walletRepository.d.ts +13 -24
  208. package/dist/types/script/base.d.ts +1 -0
  209. package/dist/types/script/delegate.d.ts +36 -0
  210. package/dist/types/storage/asyncStorage.d.ts +4 -0
  211. package/dist/types/storage/fileSystem.d.ts +3 -0
  212. package/dist/types/storage/inMemory.d.ts +3 -0
  213. package/dist/types/storage/index.d.ts +3 -0
  214. package/dist/types/storage/indexedDB.d.ts +3 -0
  215. package/dist/types/storage/localStorage.d.ts +3 -0
  216. package/dist/types/utils/arkTransaction.d.ts +6 -0
  217. package/dist/types/utils/txSizeEstimator.d.ts +12 -2
  218. package/dist/types/wallet/asset-manager.d.ts +78 -0
  219. package/dist/types/wallet/asset.d.ts +21 -0
  220. package/dist/types/wallet/batch.d.ts +1 -1
  221. package/dist/types/wallet/delegator.d.ts +24 -0
  222. package/dist/types/wallet/expo/background.d.ts +66 -0
  223. package/dist/types/wallet/expo/index.d.ts +4 -0
  224. package/dist/types/wallet/expo/wallet.d.ts +97 -0
  225. package/dist/types/wallet/index.d.ts +75 -2
  226. package/dist/types/wallet/onchain.d.ts +22 -1
  227. package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +366 -0
  228. package/dist/types/wallet/serviceWorker/wallet.d.ts +20 -11
  229. package/dist/types/wallet/utils.d.ts +13 -1
  230. package/dist/types/wallet/validation.d.ts +24 -0
  231. package/dist/types/wallet/wallet.d.ts +111 -17
  232. package/dist/types/worker/browser/service-worker-manager.d.ts +21 -0
  233. package/dist/types/{wallet/serviceWorker → worker/browser}/utils.d.ts +2 -1
  234. package/dist/types/worker/expo/asyncStorageTaskQueue.d.ts +46 -0
  235. package/dist/types/worker/expo/index.d.ts +7 -0
  236. package/dist/types/worker/expo/processors/contractPollProcessor.d.ts +14 -0
  237. package/dist/types/worker/expo/processors/index.d.ts +1 -0
  238. package/dist/types/worker/expo/taskQueue.d.ts +50 -0
  239. package/dist/types/worker/expo/taskRunner.d.ts +42 -0
  240. package/dist/types/worker/messageBus.d.ts +109 -0
  241. package/package.json +71 -17
  242. package/dist/cjs/wallet/serviceWorker/request.js +0 -78
  243. package/dist/cjs/wallet/serviceWorker/response.js +0 -222
  244. package/dist/cjs/wallet/serviceWorker/worker.js +0 -655
  245. package/dist/esm/wallet/serviceWorker/request.js +0 -75
  246. package/dist/esm/wallet/serviceWorker/response.js +0 -219
  247. package/dist/esm/wallet/serviceWorker/worker.js +0 -651
  248. package/dist/types/wallet/serviceWorker/request.d.ts +0 -74
  249. package/dist/types/wallet/serviceWorker/response.d.ts +0 -123
  250. package/dist/types/wallet/serviceWorker/worker.d.ts +0 -53
@@ -0,0 +1,176 @@
1
+ import { hex } from "@scure/base";
2
+ import { TaprootControlBlock } from "@scure/btc-signer";
3
+ const getVtxosStorageKey = (address) => `vtxos:${address}`;
4
+ const getUtxosStorageKey = (address) => `utxos:${address}`;
5
+ const getTransactionsStorageKey = (address) => `tx:${address}`;
6
+ const walletStateStorageKey = "wallet:state";
7
+ // Utility functions for (de)serializing complex structures
8
+ const serializeVtxo = (v) => ({
9
+ ...v,
10
+ tapTree: hex.encode(v.tapTree),
11
+ forfeitTapLeafScript: serializeTapLeaf(v.forfeitTapLeafScript),
12
+ intentTapLeafScript: serializeTapLeaf(v.intentTapLeafScript),
13
+ extraWitness: v.extraWitness?.map(hex.encode),
14
+ });
15
+ const serializeUtxo = (u) => ({
16
+ ...u,
17
+ tapTree: hex.encode(u.tapTree),
18
+ forfeitTapLeafScript: serializeTapLeaf(u.forfeitTapLeafScript),
19
+ intentTapLeafScript: serializeTapLeaf(u.intentTapLeafScript),
20
+ extraWitness: u.extraWitness?.map(hex.encode),
21
+ });
22
+ const deserializeVtxo = (o) => ({
23
+ ...o,
24
+ createdAt: new Date(o.createdAt),
25
+ tapTree: hex.decode(o.tapTree),
26
+ forfeitTapLeafScript: deserializeTapLeaf(o.forfeitTapLeafScript),
27
+ intentTapLeafScript: deserializeTapLeaf(o.intentTapLeafScript),
28
+ extraWitness: o.extraWitness?.map(hex.decode),
29
+ });
30
+ const deserializeUtxo = (o) => ({
31
+ ...o,
32
+ tapTree: hex.decode(o.tapTree),
33
+ forfeitTapLeafScript: deserializeTapLeaf(o.forfeitTapLeafScript),
34
+ intentTapLeafScript: deserializeTapLeaf(o.intentTapLeafScript),
35
+ extraWitness: o.extraWitness?.map(hex.decode),
36
+ });
37
+ const serializeTapLeaf = ([cb, s]) => ({
38
+ cb: hex.encode(TaprootControlBlock.encode(cb)),
39
+ s: hex.encode(s),
40
+ });
41
+ const deserializeTapLeaf = (t) => {
42
+ const cb = TaprootControlBlock.decode(hex.decode(t.cb));
43
+ const s = hex.decode(t.s);
44
+ return [cb, s];
45
+ };
46
+ /**
47
+ * @deprecated This is only to be used in migration from storage V1
48
+ */
49
+ export class WalletRepositoryImpl {
50
+ constructor(storage) {
51
+ this.version = 1;
52
+ this.storage = storage;
53
+ }
54
+ async getVtxos(address) {
55
+ const stored = await this.storage.getItem(getVtxosStorageKey(address));
56
+ if (!stored)
57
+ return [];
58
+ try {
59
+ const parsed = JSON.parse(stored);
60
+ return parsed.map(deserializeVtxo);
61
+ }
62
+ catch (error) {
63
+ console.error(`Failed to parse VTXOs for address ${address}:`, error);
64
+ return [];
65
+ }
66
+ }
67
+ async saveVtxos(address, vtxos) {
68
+ const storedVtxos = await this.getVtxos(address);
69
+ for (const vtxo of vtxos) {
70
+ const existing = storedVtxos.findIndex((v) => v.txid === vtxo.txid && v.vout === vtxo.vout);
71
+ if (existing !== -1) {
72
+ storedVtxos[existing] = vtxo;
73
+ }
74
+ else {
75
+ storedVtxos.push(vtxo);
76
+ }
77
+ }
78
+ await this.storage.setItem(getVtxosStorageKey(address), JSON.stringify(storedVtxos.map(serializeVtxo)));
79
+ }
80
+ async clearVtxos(address) {
81
+ return this.deleteVtxos(address);
82
+ }
83
+ async deleteVtxos(address) {
84
+ await this.storage.removeItem(getVtxosStorageKey(address));
85
+ }
86
+ async getUtxos(address) {
87
+ const stored = await this.storage.getItem(getUtxosStorageKey(address));
88
+ if (!stored)
89
+ return [];
90
+ try {
91
+ const parsed = JSON.parse(stored);
92
+ return parsed.map(deserializeUtxo);
93
+ }
94
+ catch (error) {
95
+ console.error(`Failed to parse UTXOs for address ${address}:`, error);
96
+ return [];
97
+ }
98
+ }
99
+ async saveUtxos(address, utxos) {
100
+ const storedUtxos = await this.getUtxos(address);
101
+ utxos.forEach((utxo) => {
102
+ const existing = storedUtxos.findIndex((u) => u.txid === utxo.txid && u.vout === utxo.vout);
103
+ if (existing !== -1) {
104
+ storedUtxos[existing] = utxo;
105
+ }
106
+ else {
107
+ storedUtxos.push(utxo);
108
+ }
109
+ });
110
+ await this.storage.setItem(getUtxosStorageKey(address), JSON.stringify(storedUtxos.map(serializeUtxo)));
111
+ }
112
+ async clearUtxos(address) {
113
+ return this.deleteVtxos(address);
114
+ }
115
+ async deleteUtxos(address) {
116
+ await this.storage.removeItem(getUtxosStorageKey(address));
117
+ }
118
+ async getTransactionHistory(address) {
119
+ const storageKey = getTransactionsStorageKey(address);
120
+ const stored = await this.storage.getItem(storageKey);
121
+ if (!stored)
122
+ return [];
123
+ try {
124
+ return JSON.parse(stored);
125
+ }
126
+ catch (error) {
127
+ console.error(`Failed to parse transactions for address ${address}:`, error);
128
+ return [];
129
+ }
130
+ }
131
+ async saveTransactions(address, txs) {
132
+ const storedTransactions = await this.getTransactionHistory(address);
133
+ for (const tx of txs) {
134
+ const existing = storedTransactions.findIndex((t) => t.key.boardingTxid === tx.key.boardingTxid &&
135
+ t.key.commitmentTxid === tx.key.commitmentTxid &&
136
+ t.key.arkTxid === tx.key.arkTxid);
137
+ if (existing !== -1) {
138
+ storedTransactions[existing] = tx;
139
+ }
140
+ else {
141
+ storedTransactions.push(tx);
142
+ }
143
+ }
144
+ await this.storage.setItem(getTransactionsStorageKey(address), JSON.stringify(storedTransactions));
145
+ }
146
+ async clearTransactions(address) {
147
+ return this.deleteTransactions(address);
148
+ }
149
+ async deleteTransactions(address) {
150
+ await this.storage.removeItem(getTransactionsStorageKey(address));
151
+ }
152
+ async getWalletState() {
153
+ const stored = await this.storage.getItem(walletStateStorageKey);
154
+ if (!stored)
155
+ return null;
156
+ try {
157
+ const state = JSON.parse(stored);
158
+ return state;
159
+ }
160
+ catch (error) {
161
+ console.error("Failed to parse wallet state:", error);
162
+ return null;
163
+ }
164
+ }
165
+ async saveWalletState(state) {
166
+ await this.storage.setItem(walletStateStorageKey, JSON.stringify(state));
167
+ }
168
+ // New method added in V2, not implemented for legacy
169
+ async clear() {
170
+ throw new Error("Method not implemented.");
171
+ }
172
+ async [Symbol.asyncDispose]() {
173
+ // deprecated StorageAdapter doesn't have a `close()` method
174
+ return;
175
+ }
176
+ }
@@ -1,167 +1 @@
1
- import { hex } from "@scure/base";
2
- import { TaprootControlBlock } from "@scure/btc-signer";
3
- const getVtxosStorageKey = (address) => `vtxos:${address}`;
4
- const getUtxosStorageKey = (address) => `utxos:${address}`;
5
- const getTransactionsStorageKey = (address) => `tx:${address}`;
6
- const walletStateStorageKey = "wallet:state";
7
- // Utility functions for (de)serializing complex structures
8
- const toHex = (b) => (b ? hex.encode(b) : undefined);
9
- const fromHex = (h) => h ? hex.decode(h) : undefined;
10
- const serializeTapLeaf = ([cb, s]) => ({
11
- cb: hex.encode(TaprootControlBlock.encode(cb)),
12
- s: hex.encode(s),
13
- });
14
- const serializeVtxo = (v) => ({
15
- ...v,
16
- tapTree: toHex(v.tapTree),
17
- forfeitTapLeafScript: serializeTapLeaf(v.forfeitTapLeafScript),
18
- intentTapLeafScript: serializeTapLeaf(v.intentTapLeafScript),
19
- extraWitness: v.extraWitness?.map(toHex),
20
- });
21
- const serializeUtxo = (u) => ({
22
- ...u,
23
- tapTree: toHex(u.tapTree),
24
- forfeitTapLeafScript: serializeTapLeaf(u.forfeitTapLeafScript),
25
- intentTapLeafScript: serializeTapLeaf(u.intentTapLeafScript),
26
- extraWitness: u.extraWitness?.map(toHex),
27
- });
28
- const deserializeTapLeaf = (t) => {
29
- const cb = TaprootControlBlock.decode(fromHex(t.cb));
30
- const s = fromHex(t.s);
31
- return [cb, s];
32
- };
33
- const deserializeVtxo = (o) => ({
34
- ...o,
35
- createdAt: new Date(o.createdAt),
36
- tapTree: fromHex(o.tapTree),
37
- forfeitTapLeafScript: deserializeTapLeaf(o.forfeitTapLeafScript),
38
- intentTapLeafScript: deserializeTapLeaf(o.intentTapLeafScript),
39
- extraWitness: o.extraWitness?.map(fromHex),
40
- });
41
- const deserializeUtxo = (o) => ({
42
- ...o,
43
- tapTree: fromHex(o.tapTree),
44
- forfeitTapLeafScript: deserializeTapLeaf(o.forfeitTapLeafScript),
45
- intentTapLeafScript: deserializeTapLeaf(o.intentTapLeafScript),
46
- extraWitness: o.extraWitness?.map(fromHex),
47
- });
48
- export class WalletRepositoryImpl {
49
- constructor(storage) {
50
- this.storage = storage;
51
- }
52
- async getVtxos(address) {
53
- const stored = await this.storage.getItem(getVtxosStorageKey(address));
54
- if (!stored)
55
- return [];
56
- try {
57
- const parsed = JSON.parse(stored);
58
- return parsed.map(deserializeVtxo);
59
- }
60
- catch (error) {
61
- console.error(`Failed to parse VTXOs for address ${address}:`, error);
62
- return [];
63
- }
64
- }
65
- async saveVtxos(address, vtxos) {
66
- const storedVtxos = await this.getVtxos(address);
67
- for (const vtxo of vtxos) {
68
- const existing = storedVtxos.findIndex((v) => v.txid === vtxo.txid && v.vout === vtxo.vout);
69
- if (existing !== -1) {
70
- storedVtxos[existing] = vtxo;
71
- }
72
- else {
73
- storedVtxos.push(vtxo);
74
- }
75
- }
76
- await this.storage.setItem(getVtxosStorageKey(address), JSON.stringify(storedVtxos.map(serializeVtxo)));
77
- }
78
- async removeVtxo(address, vtxoId) {
79
- const vtxos = await this.getVtxos(address);
80
- const [txid, vout] = vtxoId.split(":");
81
- const filtered = vtxos.filter((v) => !(v.txid === txid && v.vout === parseInt(vout, 10)));
82
- await this.storage.setItem(getVtxosStorageKey(address), JSON.stringify(filtered.map(serializeVtxo)));
83
- }
84
- async clearVtxos(address) {
85
- await this.storage.removeItem(getVtxosStorageKey(address));
86
- }
87
- async getUtxos(address) {
88
- const stored = await this.storage.getItem(getUtxosStorageKey(address));
89
- if (!stored)
90
- return [];
91
- try {
92
- const parsed = JSON.parse(stored);
93
- return parsed.map(deserializeUtxo);
94
- }
95
- catch (error) {
96
- console.error(`Failed to parse UTXOs for address ${address}:`, error);
97
- return [];
98
- }
99
- }
100
- async saveUtxos(address, utxos) {
101
- const storedUtxos = await this.getUtxos(address);
102
- utxos.forEach((utxo) => {
103
- const existing = storedUtxos.findIndex((u) => u.txid === utxo.txid && u.vout === utxo.vout);
104
- if (existing !== -1) {
105
- storedUtxos[existing] = utxo;
106
- }
107
- else {
108
- storedUtxos.push(utxo);
109
- }
110
- });
111
- await this.storage.setItem(getUtxosStorageKey(address), JSON.stringify(storedUtxos.map(serializeUtxo)));
112
- }
113
- async removeUtxo(address, utxoId) {
114
- const utxos = await this.getUtxos(address);
115
- const [txid, vout] = utxoId.split(":");
116
- const filtered = utxos.filter((v) => !(v.txid === txid && v.vout === parseInt(vout, 10)));
117
- await this.storage.setItem(getUtxosStorageKey(address), JSON.stringify(filtered.map(serializeUtxo)));
118
- }
119
- async clearUtxos(address) {
120
- await this.storage.removeItem(getUtxosStorageKey(address));
121
- }
122
- async getTransactionHistory(address) {
123
- const storageKey = getTransactionsStorageKey(address);
124
- const stored = await this.storage.getItem(storageKey);
125
- if (!stored)
126
- return [];
127
- try {
128
- return JSON.parse(stored);
129
- }
130
- catch (error) {
131
- console.error(`Failed to parse transactions for address ${address}:`, error);
132
- return [];
133
- }
134
- }
135
- async saveTransactions(address, txs) {
136
- const storedTransactions = await this.getTransactionHistory(address);
137
- for (const tx of txs) {
138
- const existing = storedTransactions.findIndex((t) => t.key === tx.key);
139
- if (existing !== -1) {
140
- storedTransactions[existing] = tx;
141
- }
142
- else {
143
- storedTransactions.push(tx);
144
- }
145
- }
146
- await this.storage.setItem(getTransactionsStorageKey(address), JSON.stringify(storedTransactions));
147
- }
148
- async clearTransactions(address) {
149
- await this.storage.removeItem(getTransactionsStorageKey(address));
150
- }
151
- async getWalletState() {
152
- const stored = await this.storage.getItem(walletStateStorageKey);
153
- if (!stored)
154
- return null;
155
- try {
156
- const state = JSON.parse(stored);
157
- return state;
158
- }
159
- catch (error) {
160
- console.error("Failed to parse wallet state:", error);
161
- return null;
162
- }
163
- }
164
- async saveWalletState(state) {
165
- await this.storage.setItem(walletStateStorageKey, JSON.stringify(state));
166
- }
167
- }
1
+ export {};
@@ -1,9 +1,10 @@
1
1
  import { Script, Address, p2tr, taprootListToTree, TAPROOT_UNSPENDABLE_KEY, } from "@scure/btc-signer";
2
+ import * as bip68 from "bip68";
2
3
  import { TAP_LEAF_VERSION } from "@scure/btc-signer/payment.js";
3
4
  import { PSBTOutput } from "@scure/btc-signer/psbt.js";
4
5
  import { hex } from "@scure/base";
5
6
  import { ArkAddress } from './address.js';
6
- import { ConditionCSVMultisigTapscript, CSVMultisigTapscript, } from './tapscript.js';
7
+ import { CLTVMultisigTapscript, ConditionCSVMultisigTapscript, CSVMultisigTapscript, } from './tapscript.js';
7
8
  export const TapTreeCoder = PSBTOutput.tapTree[2];
8
9
  export function scriptFromTapLeafScript(leaf) {
9
10
  return leaf[1].subarray(0, leaf[1].length - 1); // remove the version byte
@@ -90,3 +91,22 @@ export class VtxoScript {
90
91
  return paths;
91
92
  }
92
93
  }
94
+ export function getSequence(tapLeafScript) {
95
+ let sequence = undefined;
96
+ try {
97
+ const scriptWithLeafVersion = tapLeafScript[1];
98
+ const script = scriptWithLeafVersion.subarray(0, scriptWithLeafVersion.length - 1);
99
+ try {
100
+ const params = CSVMultisigTapscript.decode(script).params;
101
+ sequence = bip68.encode(params.timelock.type === "blocks"
102
+ ? { blocks: Number(params.timelock.value) }
103
+ : { seconds: Number(params.timelock.value) });
104
+ }
105
+ catch {
106
+ const params = CLTVMultisigTapscript.decode(script).params;
107
+ sequence = Number(params.absoluteTimelock);
108
+ }
109
+ }
110
+ catch { }
111
+ return sequence;
112
+ }
@@ -0,0 +1,46 @@
1
+ import { DefaultVtxo } from './default.js';
2
+ import { MultisigTapscript } from './tapscript.js';
3
+ import { VtxoScript } from './base.js';
4
+ import { hex } from "@scure/base";
5
+ /**
6
+ * DelegateVtxo extends DefaultVtxo with an extra delegator path
7
+ */
8
+ export var DelegateVtxo;
9
+ (function (DelegateVtxo) {
10
+ /**
11
+ * DelegateVtxo.Script extends DefaultVtxo.Script and adds a delegate path.
12
+ * @example
13
+ * ```typescript
14
+ * const vtxoScript = new DelegateVtxo.Script({
15
+ * pubKey: new Uint8Array(32),
16
+ * serverPubKey: new Uint8Array(32),
17
+ * delegatePubKey: new Uint8Array(32),
18
+ * });
19
+ *
20
+ * console.log("script pub key:", vtxoScript.pkScript)
21
+ * ```
22
+ */
23
+ class Script extends VtxoScript {
24
+ constructor(options) {
25
+ const defaultVtxo = new DefaultVtxo.Script(options);
26
+ const { delegatePubKey, pubKey, serverPubKey } = options;
27
+ const delegateScript = MultisigTapscript.encode({
28
+ pubkeys: [pubKey, delegatePubKey, serverPubKey],
29
+ }).script;
30
+ super([...defaultVtxo.scripts, delegateScript]);
31
+ this.options = options;
32
+ this.defaultVtxo = defaultVtxo;
33
+ this.delegateScript = hex.encode(delegateScript);
34
+ }
35
+ forfeit() {
36
+ return this.findLeaf(this.defaultVtxo.forfeitScript);
37
+ }
38
+ exit() {
39
+ return this.findLeaf(this.defaultVtxo.exitScript);
40
+ }
41
+ delegate() {
42
+ return this.findLeaf(this.delegateScript);
43
+ }
44
+ }
45
+ DelegateVtxo.Script = Script;
46
+ })(DelegateVtxo || (DelegateVtxo = {}));
@@ -1,4 +1,7 @@
1
- // Note: This requires @react-native-async-storage/async-storage to be installed
1
+ /**
2
+ * @deprecated Use repositories instead
3
+ * Note: This requires @react-native-async-storage/async-storage to be installed
4
+ */
2
5
  export class AsyncStorageAdapter {
3
6
  constructor() {
4
7
  try {
@@ -1,5 +1,8 @@
1
1
  import * as fs from "fs/promises";
2
2
  import * as path from "path";
3
+ /**
4
+ * @deprecated Use repositories instead
5
+ */
3
6
  export class FileSystemStorageAdapter {
4
7
  constructor(dirPath) {
5
8
  // Normalize and resolve the storage base path once
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @deprecated Use repositories instead
3
+ */
1
4
  export class InMemoryStorageAdapter {
2
5
  constructor() {
3
6
  this.store = new Map();
@@ -1,5 +1,9 @@
1
+ import { DB_VERSION } from '../repositories/indexedDB/db.js';
2
+ /**
3
+ * @deprecated Use repositories instead
4
+ */
1
5
  export class IndexedDBStorageAdapter {
2
- constructor(dbName, version = 1) {
6
+ constructor(dbName, version = DB_VERSION) {
3
7
  this.db = null;
4
8
  this.dbName = dbName;
5
9
  this.version = version;
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @deprecated Use repositories instead
3
+ */
1
4
  export class LocalStorageAdapter {
2
5
  getSafeLocalStorage() {
3
6
  try {
@@ -7,6 +7,7 @@ import { scriptFromTapLeafScript, VtxoScript, } from '../script/base.js';
7
7
  import { P2A } from './anchor.js';
8
8
  import { setArkPsbtField, VtxoTaprootTree } from './unknownFields.js';
9
9
  import { Transaction } from './transaction.js';
10
+ import { ArkAddress } from '../script/address.js';
10
11
  /**
11
12
  * Builds an offchain transaction with checkpoint transactions.
12
13
  *
@@ -232,3 +233,17 @@ export function combineTapscriptSigs(signedTx, originalTx) {
232
233
  }
233
234
  return originalTx;
234
235
  }
236
+ /**
237
+ * Validates if a given string is a valid Ark address by attempting to decode it.
238
+ * @param address The Ark address to validate.
239
+ * @returns True if the address is valid, false otherwise.
240
+ */
241
+ export function isValidArkAddress(address) {
242
+ try {
243
+ ArkAddress.decode(address);
244
+ return true;
245
+ }
246
+ catch (e) {
247
+ return false;
248
+ }
249
+ }
@@ -4,6 +4,46 @@ const txKey = {
4
4
  boardingTxid: "",
5
5
  arkTxid: "",
6
6
  };
7
+ function collectAssets(vtxos) {
8
+ const map = new Map();
9
+ for (const vtxo of vtxos) {
10
+ if (vtxo.assets) {
11
+ for (const a of vtxo.assets) {
12
+ map.set(a.assetId, (map.get(a.assetId) ?? 0) + a.amount);
13
+ }
14
+ }
15
+ }
16
+ if (map.size === 0)
17
+ return undefined;
18
+ return Array.from(map, ([assetId, amount]) => ({ assetId, amount }));
19
+ }
20
+ function subtractAssets(spent, change) {
21
+ const map = new Map();
22
+ for (const vtxo of change) {
23
+ if (vtxo.assets) {
24
+ for (const a of vtxo.assets) {
25
+ map.set(a.assetId, (map.get(a.assetId) ?? 0) + a.amount);
26
+ }
27
+ }
28
+ }
29
+ for (const vtxo of spent) {
30
+ if (vtxo.assets) {
31
+ for (const a of vtxo.assets) {
32
+ const current = map.get(a.assetId) ?? 0;
33
+ const remaining = current - a.amount;
34
+ if (remaining !== 0) {
35
+ map.set(a.assetId, remaining);
36
+ }
37
+ else {
38
+ map.delete(a.assetId);
39
+ }
40
+ }
41
+ }
42
+ }
43
+ if (map.size === 0)
44
+ return undefined;
45
+ return Array.from(map, ([assetId, amount]) => ({ assetId, amount }));
46
+ }
7
47
  /**
8
48
  * Builds the transaction history by analyzing virtual coins (VTXOs), boarding transactions, and ignored commitments.
9
49
  * History is sorted from newest to oldest and is composed only of SENT and RECEIVED transactions.
@@ -23,6 +63,7 @@ export async function buildTransactionHistory(vtxos, allBoardingTxs, commitments
23
63
  // it's translated into a received batch transaction
24
64
  if (!commitmentsToIgnore.has(vtxo.virtualStatus.commitmentTxIds[0]) &&
25
65
  fromOldestVtxo.filter((v) => v.settledBy === vtxo.virtualStatus.commitmentTxIds[0]).length === 0) {
66
+ const assets = collectAssets([vtxo]);
26
67
  received.push({
27
68
  key: {
28
69
  ...txKey,
@@ -33,12 +74,14 @@ export async function buildTransactionHistory(vtxos, allBoardingTxs, commitments
33
74
  amount: vtxo.value,
34
75
  settled: vtxo.status.isLeaf || vtxo.isSpent,
35
76
  createdAt: vtxo.createdAt.getTime(),
77
+ ...(assets && { assets }),
36
78
  });
37
79
  }
38
80
  }
39
81
  else if (fromOldestVtxo.filter((v) => v.arkTxId === vtxo.txid).length === 0) {
40
82
  // If this vtxo is preconfirmed and does not spend any other vtxos,
41
83
  // it's translated into a received offchain transaction
84
+ const assets = collectAssets([vtxo]);
42
85
  received.push({
43
86
  key: { ...txKey, arkTxid: vtxo.txid },
44
87
  tag: "offchain",
@@ -46,6 +89,7 @@ export async function buildTransactionHistory(vtxos, allBoardingTxs, commitments
46
89
  amount: vtxo.value,
47
90
  settled: vtxo.status.isLeaf || vtxo.isSpent,
48
91
  createdAt: vtxo.createdAt.getTime(),
92
+ ...(assets && { assets }),
49
93
  });
50
94
  }
51
95
  // If the vtxo is spent, it's translated into a sent transaction unless:
@@ -74,6 +118,7 @@ export async function buildTransactionHistory(vtxos, allBoardingTxs, commitments
74
118
  ? await getTxCreatedAt(vtxo.arkTxId)
75
119
  : vtxo.createdAt.getTime() + 1;
76
120
  }
121
+ const assets = subtractAssets(allSpent, changes);
77
122
  sent.push({
78
123
  key: { ...txKey, arkTxid: vtxo.arkTxId },
79
124
  tag: "offchain",
@@ -81,6 +126,7 @@ export async function buildTransactionHistory(vtxos, allBoardingTxs, commitments
81
126
  amount: txAmount,
82
127
  settled: true,
83
128
  createdAt: txTime,
129
+ ...(assets && { assets }),
84
130
  });
85
131
  }
86
132
  // If the vtxo is forfeited in a batch and the total sum of forfeited vtxos is bigger than the sum of new vtxos,
@@ -97,6 +143,7 @@ export async function buildTransactionHistory(vtxos, allBoardingTxs, commitments
97
143
  // forfeitAmount > settledAmount --> collaborative exit with offchain change
98
144
  // TODO: make this support fees!
99
145
  if (forfeitAmount > settledAmount) {
146
+ const assets = subtractAssets(forfeitVtxos, changes);
100
147
  sent.push({
101
148
  key: { ...txKey, commitmentTxid: vtxo.settledBy },
102
149
  tag: "exit",
@@ -104,11 +151,13 @@ export async function buildTransactionHistory(vtxos, allBoardingTxs, commitments
104
151
  amount: forfeitAmount - settledAmount,
105
152
  settled: true,
106
153
  createdAt: changes[0].createdAt.getTime(),
154
+ ...(assets && { assets }),
107
155
  });
108
156
  }
109
157
  }
110
158
  else {
111
159
  // forfeitAmount > 0 && settledAmount == 0 --> collaborative exit without any offchain change
160
+ const assets = subtractAssets(forfeitVtxos, []);
112
161
  sent.push({
113
162
  key: { ...txKey, commitmentTxid: vtxo.settledBy },
114
163
  tag: "exit",
@@ -117,6 +166,7 @@ export async function buildTransactionHistory(vtxos, allBoardingTxs, commitments
117
166
  settled: true,
118
167
  // TODO: fetch commitment tx with /v1/indexer/commitmentTx/<commitmentTxid> to know when the tx was made
119
168
  createdAt: vtxo.createdAt.getTime() + 1,
169
+ ...(assets && { assets }),
120
170
  });
121
171
  }
122
172
  }