@arkade-os/sdk 0.3.13 → 0.4.0-next.1

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 (270) hide show
  1. package/README.md +586 -54
  2. package/dist/cjs/asset/assetGroup.js +141 -0
  3. package/dist/cjs/asset/assetId.js +88 -0
  4. package/dist/cjs/asset/assetInput.js +204 -0
  5. package/dist/cjs/asset/assetOutput.js +159 -0
  6. package/dist/cjs/asset/assetRef.js +82 -0
  7. package/dist/cjs/asset/index.js +24 -0
  8. package/dist/cjs/asset/metadata.js +172 -0
  9. package/dist/cjs/asset/packet.js +164 -0
  10. package/dist/cjs/asset/types.js +25 -0
  11. package/dist/cjs/asset/utils.js +105 -0
  12. package/dist/cjs/bip322/index.js +270 -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/forfeit.js +12 -8
  25. package/dist/cjs/identity/index.js +1 -0
  26. package/dist/cjs/identity/seedIdentity.js +255 -0
  27. package/dist/cjs/index.js +72 -14
  28. package/dist/cjs/intent/index.js +47 -11
  29. package/dist/cjs/providers/ark.js +7 -0
  30. package/dist/cjs/providers/delegator.js +66 -0
  31. package/dist/cjs/providers/expoIndexer.js +5 -0
  32. package/dist/cjs/providers/indexer.js +68 -1
  33. package/dist/cjs/providers/utils.js +1 -0
  34. package/dist/cjs/repositories/contractRepository.js +0 -103
  35. package/dist/cjs/repositories/inMemory/contractRepository.js +55 -0
  36. package/dist/cjs/repositories/inMemory/walletRepository.js +80 -0
  37. package/dist/cjs/repositories/index.js +16 -0
  38. package/dist/cjs/repositories/indexedDB/contractRepository.js +187 -0
  39. package/dist/cjs/repositories/indexedDB/db.js +19 -0
  40. package/dist/cjs/repositories/indexedDB/manager.js +97 -0
  41. package/dist/cjs/repositories/indexedDB/schema.js +159 -0
  42. package/dist/cjs/repositories/indexedDB/walletRepository.js +338 -0
  43. package/dist/cjs/repositories/indexedDB/websqlAdapter.js +144 -0
  44. package/dist/cjs/repositories/migrations/contractRepositoryImpl.js +127 -0
  45. package/dist/cjs/repositories/migrations/fromStorageAdapter.js +66 -0
  46. package/dist/cjs/repositories/migrations/walletRepositoryImpl.js +180 -0
  47. package/dist/cjs/repositories/realm/contractRepository.js +120 -0
  48. package/dist/cjs/repositories/realm/index.js +9 -0
  49. package/dist/cjs/repositories/realm/schemas.js +108 -0
  50. package/dist/cjs/repositories/realm/types.js +7 -0
  51. package/dist/cjs/repositories/realm/walletRepository.js +273 -0
  52. package/dist/cjs/repositories/serialization.js +49 -0
  53. package/dist/cjs/repositories/sqlite/contractRepository.js +139 -0
  54. package/dist/cjs/repositories/sqlite/index.js +7 -0
  55. package/dist/cjs/repositories/sqlite/types.js +2 -0
  56. package/dist/cjs/repositories/sqlite/walletRepository.js +328 -0
  57. package/dist/cjs/repositories/walletRepository.js +0 -169
  58. package/dist/cjs/script/base.js +54 -0
  59. package/dist/cjs/script/delegate.js +49 -0
  60. package/dist/cjs/storage/asyncStorage.js +4 -1
  61. package/dist/cjs/storage/fileSystem.js +3 -0
  62. package/dist/cjs/storage/inMemory.js +3 -0
  63. package/dist/cjs/storage/indexedDB.js +5 -1
  64. package/dist/cjs/storage/localStorage.js +3 -0
  65. package/dist/cjs/utils/arkTransaction.js +16 -0
  66. package/dist/cjs/utils/transactionHistory.js +50 -0
  67. package/dist/cjs/wallet/asset-manager.js +338 -0
  68. package/dist/cjs/wallet/asset.js +117 -0
  69. package/dist/cjs/wallet/batch.js +1 -1
  70. package/dist/cjs/wallet/delegator.js +235 -0
  71. package/dist/cjs/wallet/expo/background.js +133 -0
  72. package/dist/cjs/wallet/expo/index.js +9 -0
  73. package/dist/cjs/wallet/expo/wallet.js +231 -0
  74. package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +568 -0
  75. package/dist/cjs/wallet/serviceWorker/wallet.js +383 -102
  76. package/dist/cjs/wallet/utils.js +58 -0
  77. package/dist/cjs/wallet/validation.js +151 -0
  78. package/dist/cjs/wallet/vtxo-manager.js +8 -1
  79. package/dist/cjs/wallet/wallet.js +702 -260
  80. package/dist/cjs/worker/browser/service-worker-manager.js +82 -0
  81. package/dist/cjs/{wallet/serviceWorker → worker/browser}/utils.js +2 -1
  82. package/dist/cjs/worker/expo/asyncStorageTaskQueue.js +78 -0
  83. package/dist/cjs/worker/expo/index.js +12 -0
  84. package/dist/cjs/worker/expo/processors/contractPollProcessor.js +61 -0
  85. package/dist/cjs/worker/expo/processors/index.js +6 -0
  86. package/dist/cjs/worker/expo/taskQueue.js +41 -0
  87. package/dist/cjs/worker/expo/taskRunner.js +57 -0
  88. package/dist/cjs/worker/messageBus.js +252 -0
  89. package/dist/esm/asset/assetGroup.js +137 -0
  90. package/dist/esm/asset/assetId.js +84 -0
  91. package/dist/esm/asset/assetInput.js +199 -0
  92. package/dist/esm/asset/assetOutput.js +154 -0
  93. package/dist/esm/asset/assetRef.js +78 -0
  94. package/dist/esm/asset/index.js +8 -0
  95. package/dist/esm/asset/metadata.js +167 -0
  96. package/dist/esm/asset/packet.js +159 -0
  97. package/dist/esm/asset/types.js +22 -0
  98. package/dist/esm/asset/utils.js +99 -0
  99. package/dist/esm/bip322/index.js +267 -0
  100. package/dist/esm/contracts/arkcontract.js +141 -0
  101. package/dist/esm/contracts/contractManager.js +432 -0
  102. package/dist/esm/contracts/contractWatcher.js +563 -0
  103. package/dist/esm/contracts/handlers/default.js +82 -0
  104. package/dist/esm/contracts/handlers/delegate.js +86 -0
  105. package/dist/esm/contracts/handlers/helpers.js +66 -0
  106. package/dist/esm/contracts/handlers/index.js +12 -0
  107. package/dist/esm/contracts/handlers/registry.js +86 -0
  108. package/dist/esm/contracts/handlers/vhtlc.js +190 -0
  109. package/dist/esm/contracts/index.js +13 -0
  110. package/dist/esm/contracts/types.js +1 -0
  111. package/dist/esm/forfeit.js +11 -8
  112. package/dist/esm/identity/index.js +1 -0
  113. package/dist/esm/identity/seedIdentity.js +249 -0
  114. package/dist/esm/index.js +28 -15
  115. package/dist/esm/intent/index.js +44 -9
  116. package/dist/esm/providers/ark.js +7 -0
  117. package/dist/esm/providers/delegator.js +62 -0
  118. package/dist/esm/providers/expoIndexer.js +5 -0
  119. package/dist/esm/providers/indexer.js +68 -1
  120. package/dist/esm/providers/utils.js +1 -0
  121. package/dist/esm/repositories/contractRepository.js +1 -101
  122. package/dist/esm/repositories/inMemory/contractRepository.js +51 -0
  123. package/dist/esm/repositories/inMemory/walletRepository.js +76 -0
  124. package/dist/esm/repositories/index.js +8 -0
  125. package/dist/esm/repositories/indexedDB/contractRepository.js +183 -0
  126. package/dist/esm/repositories/indexedDB/db.js +4 -0
  127. package/dist/esm/repositories/indexedDB/manager.js +92 -0
  128. package/dist/esm/repositories/indexedDB/schema.js +155 -0
  129. package/dist/esm/repositories/indexedDB/walletRepository.js +334 -0
  130. package/dist/esm/repositories/indexedDB/websqlAdapter.js +138 -0
  131. package/dist/esm/repositories/migrations/contractRepositoryImpl.js +121 -0
  132. package/dist/esm/repositories/migrations/fromStorageAdapter.js +58 -0
  133. package/dist/esm/repositories/migrations/walletRepositoryImpl.js +176 -0
  134. package/dist/esm/repositories/realm/contractRepository.js +116 -0
  135. package/dist/esm/repositories/realm/index.js +3 -0
  136. package/dist/esm/repositories/realm/schemas.js +105 -0
  137. package/dist/esm/repositories/realm/types.js +6 -0
  138. package/dist/esm/repositories/realm/walletRepository.js +269 -0
  139. package/dist/esm/repositories/serialization.js +40 -0
  140. package/dist/esm/repositories/sqlite/contractRepository.js +135 -0
  141. package/dist/esm/repositories/sqlite/index.js +2 -0
  142. package/dist/esm/repositories/sqlite/types.js +1 -0
  143. package/dist/esm/repositories/sqlite/walletRepository.js +324 -0
  144. package/dist/esm/repositories/walletRepository.js +1 -167
  145. package/dist/esm/script/base.js +21 -1
  146. package/dist/esm/script/delegate.js +46 -0
  147. package/dist/esm/storage/asyncStorage.js +4 -1
  148. package/dist/esm/storage/fileSystem.js +3 -0
  149. package/dist/esm/storage/inMemory.js +3 -0
  150. package/dist/esm/storage/indexedDB.js +5 -1
  151. package/dist/esm/storage/localStorage.js +3 -0
  152. package/dist/esm/utils/arkTransaction.js +15 -0
  153. package/dist/esm/utils/transactionHistory.js +50 -0
  154. package/dist/esm/wallet/asset-manager.js +333 -0
  155. package/dist/esm/wallet/asset.js +111 -0
  156. package/dist/esm/wallet/batch.js +1 -1
  157. package/dist/esm/wallet/delegator.js +231 -0
  158. package/dist/esm/wallet/expo/background.js +128 -0
  159. package/dist/esm/wallet/expo/index.js +2 -0
  160. package/dist/esm/wallet/expo/wallet.js +194 -0
  161. package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +564 -0
  162. package/dist/esm/wallet/serviceWorker/wallet.js +382 -101
  163. package/dist/esm/wallet/utils.js +54 -0
  164. package/dist/esm/wallet/validation.js +139 -0
  165. package/dist/esm/wallet/vtxo-manager.js +8 -1
  166. package/dist/esm/wallet/wallet.js +704 -229
  167. package/dist/esm/worker/browser/service-worker-manager.js +76 -0
  168. package/dist/esm/{wallet/serviceWorker → worker/browser}/utils.js +2 -1
  169. package/dist/esm/worker/expo/asyncStorageTaskQueue.js +74 -0
  170. package/dist/esm/worker/expo/index.js +4 -0
  171. package/dist/esm/worker/expo/processors/contractPollProcessor.js +58 -0
  172. package/dist/esm/worker/expo/processors/index.js +1 -0
  173. package/dist/esm/worker/expo/taskQueue.js +37 -0
  174. package/dist/esm/worker/expo/taskRunner.js +54 -0
  175. package/dist/esm/worker/messageBus.js +248 -0
  176. package/dist/types/asset/assetGroup.d.ts +28 -0
  177. package/dist/types/asset/assetId.d.ts +19 -0
  178. package/dist/types/asset/assetInput.d.ts +46 -0
  179. package/dist/types/asset/assetOutput.d.ts +39 -0
  180. package/dist/types/asset/assetRef.d.ts +25 -0
  181. package/dist/types/asset/index.d.ts +8 -0
  182. package/dist/types/asset/metadata.d.ts +37 -0
  183. package/dist/types/asset/packet.d.ts +27 -0
  184. package/dist/types/asset/types.d.ts +18 -0
  185. package/dist/types/asset/utils.d.ts +21 -0
  186. package/dist/types/bip322/index.d.ts +55 -0
  187. package/dist/types/contracts/arkcontract.d.ts +101 -0
  188. package/dist/types/contracts/contractManager.d.ts +331 -0
  189. package/dist/types/contracts/contractWatcher.d.ts +192 -0
  190. package/dist/types/contracts/handlers/default.d.ts +19 -0
  191. package/dist/types/contracts/handlers/delegate.d.ts +21 -0
  192. package/dist/types/contracts/handlers/helpers.d.ts +18 -0
  193. package/dist/types/contracts/handlers/index.d.ts +7 -0
  194. package/dist/types/contracts/handlers/registry.d.ts +65 -0
  195. package/dist/types/contracts/handlers/vhtlc.d.ts +32 -0
  196. package/dist/types/contracts/index.d.ts +14 -0
  197. package/dist/types/contracts/types.d.ts +222 -0
  198. package/dist/types/forfeit.d.ts +2 -1
  199. package/dist/types/identity/index.d.ts +1 -0
  200. package/dist/types/identity/seedIdentity.d.ts +128 -0
  201. package/dist/types/index.d.ts +22 -12
  202. package/dist/types/intent/index.d.ts +15 -1
  203. package/dist/types/providers/ark.d.ts +11 -2
  204. package/dist/types/providers/delegator.d.ts +29 -0
  205. package/dist/types/providers/indexer.d.ts +11 -1
  206. package/dist/types/repositories/contractRepository.d.ts +30 -19
  207. package/dist/types/repositories/inMemory/contractRepository.d.ts +17 -0
  208. package/dist/types/repositories/inMemory/walletRepository.d.ts +26 -0
  209. package/dist/types/repositories/index.d.ts +7 -0
  210. package/dist/types/repositories/indexedDB/contractRepository.d.ts +21 -0
  211. package/dist/types/repositories/indexedDB/db.d.ts +4 -0
  212. package/dist/types/repositories/indexedDB/manager.d.ts +22 -0
  213. package/dist/types/repositories/indexedDB/schema.d.ts +8 -0
  214. package/dist/types/repositories/indexedDB/walletRepository.d.ts +25 -0
  215. package/dist/types/repositories/indexedDB/websqlAdapter.d.ts +49 -0
  216. package/dist/types/repositories/migrations/contractRepositoryImpl.d.ts +24 -0
  217. package/dist/types/repositories/migrations/fromStorageAdapter.d.ts +19 -0
  218. package/dist/types/repositories/migrations/walletRepositoryImpl.d.ts +27 -0
  219. package/dist/types/repositories/realm/contractRepository.d.ts +24 -0
  220. package/dist/types/repositories/realm/index.d.ts +4 -0
  221. package/dist/types/repositories/realm/schemas.d.ts +208 -0
  222. package/dist/types/repositories/realm/types.d.ts +16 -0
  223. package/dist/types/repositories/realm/walletRepository.d.ts +31 -0
  224. package/dist/types/repositories/serialization.d.ts +40 -0
  225. package/dist/types/repositories/sqlite/contractRepository.d.ts +33 -0
  226. package/dist/types/repositories/sqlite/index.d.ts +3 -0
  227. package/dist/types/repositories/sqlite/types.d.ts +18 -0
  228. package/dist/types/repositories/sqlite/walletRepository.d.ts +40 -0
  229. package/dist/types/repositories/walletRepository.d.ts +13 -24
  230. package/dist/types/script/base.d.ts +1 -0
  231. package/dist/types/script/delegate.d.ts +36 -0
  232. package/dist/types/storage/asyncStorage.d.ts +4 -0
  233. package/dist/types/storage/fileSystem.d.ts +3 -0
  234. package/dist/types/storage/inMemory.d.ts +3 -0
  235. package/dist/types/storage/index.d.ts +3 -0
  236. package/dist/types/storage/indexedDB.d.ts +3 -0
  237. package/dist/types/storage/localStorage.d.ts +3 -0
  238. package/dist/types/utils/arkTransaction.d.ts +6 -0
  239. package/dist/types/wallet/asset-manager.d.ts +78 -0
  240. package/dist/types/wallet/asset.d.ts +21 -0
  241. package/dist/types/wallet/batch.d.ts +1 -1
  242. package/dist/types/wallet/delegator.d.ts +24 -0
  243. package/dist/types/wallet/expo/background.d.ts +66 -0
  244. package/dist/types/wallet/expo/index.d.ts +4 -0
  245. package/dist/types/wallet/expo/wallet.d.ts +97 -0
  246. package/dist/types/wallet/index.d.ts +75 -2
  247. package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +366 -0
  248. package/dist/types/wallet/serviceWorker/wallet.d.ts +20 -11
  249. package/dist/types/wallet/utils.d.ts +12 -1
  250. package/dist/types/wallet/validation.d.ts +24 -0
  251. package/dist/types/wallet/wallet.d.ts +111 -17
  252. package/dist/types/worker/browser/service-worker-manager.d.ts +21 -0
  253. package/dist/types/{wallet/serviceWorker → worker/browser}/utils.d.ts +2 -1
  254. package/dist/types/worker/expo/asyncStorageTaskQueue.d.ts +46 -0
  255. package/dist/types/worker/expo/index.d.ts +7 -0
  256. package/dist/types/worker/expo/processors/contractPollProcessor.d.ts +14 -0
  257. package/dist/types/worker/expo/processors/index.d.ts +1 -0
  258. package/dist/types/worker/expo/taskQueue.d.ts +50 -0
  259. package/dist/types/worker/expo/taskRunner.d.ts +42 -0
  260. package/dist/types/worker/messageBus.d.ts +109 -0
  261. package/package.json +69 -11
  262. package/dist/cjs/wallet/serviceWorker/request.js +0 -78
  263. package/dist/cjs/wallet/serviceWorker/response.js +0 -222
  264. package/dist/cjs/wallet/serviceWorker/worker.js +0 -655
  265. package/dist/esm/wallet/serviceWorker/request.js +0 -75
  266. package/dist/esm/wallet/serviceWorker/response.js +0 -219
  267. package/dist/esm/wallet/serviceWorker/worker.js +0 -651
  268. package/dist/types/wallet/serviceWorker/request.d.ts +0 -74
  269. package/dist/types/wallet/serviceWorker/response.d.ts +0 -123
  270. package/dist/types/wallet/serviceWorker/worker.d.ts +0 -53
@@ -0,0 +1,40 @@
1
+ import { hex } from "@scure/base";
2
+ import { TaprootControlBlock } from "@scure/btc-signer";
3
+ export const serializeTapLeaf = ([cb, s,]) => ({
4
+ cb: hex.encode(TaprootControlBlock.encode(cb)),
5
+ s: hex.encode(s),
6
+ });
7
+ export const serializeVtxo = (v) => ({
8
+ ...v,
9
+ tapTree: hex.encode(v.tapTree),
10
+ forfeitTapLeafScript: serializeTapLeaf(v.forfeitTapLeafScript),
11
+ intentTapLeafScript: serializeTapLeaf(v.intentTapLeafScript),
12
+ extraWitness: v.extraWitness?.map(hex.encode),
13
+ });
14
+ export const serializeUtxo = (u) => ({
15
+ ...u,
16
+ tapTree: hex.encode(u.tapTree),
17
+ forfeitTapLeafScript: serializeTapLeaf(u.forfeitTapLeafScript),
18
+ intentTapLeafScript: serializeTapLeaf(u.intentTapLeafScript),
19
+ extraWitness: u.extraWitness?.map(hex.encode),
20
+ });
21
+ export const deserializeTapLeaf = (t) => {
22
+ const cb = TaprootControlBlock.decode(hex.decode(t.cb));
23
+ const s = hex.decode(t.s);
24
+ return [cb, s];
25
+ };
26
+ export const deserializeVtxo = (o) => ({
27
+ ...o,
28
+ createdAt: new Date(o.createdAt),
29
+ tapTree: hex.decode(o.tapTree),
30
+ forfeitTapLeafScript: deserializeTapLeaf(o.forfeitTapLeafScript),
31
+ intentTapLeafScript: deserializeTapLeaf(o.intentTapLeafScript),
32
+ extraWitness: o.extraWitness?.map(hex.decode),
33
+ });
34
+ export const deserializeUtxo = (o) => ({
35
+ ...o,
36
+ tapTree: hex.decode(o.tapTree),
37
+ forfeitTapLeafScript: deserializeTapLeaf(o.forfeitTapLeafScript),
38
+ intentTapLeafScript: deserializeTapLeaf(o.intentTapLeafScript),
39
+ extraWitness: o.extraWitness?.map(hex.decode),
40
+ });
@@ -0,0 +1,135 @@
1
+ /**
2
+ * SQLite-based implementation of ContractRepository.
3
+ *
4
+ * Uses the SQLExecutor interface so consumers can plug in any SQLite driver
5
+ * (expo-sqlite, better-sqlite3, etc.).
6
+ *
7
+ * Tables are created lazily on first operation via `ensureInit()`.
8
+ * The consumer owns the SQLExecutor lifecycle — `[Symbol.asyncDispose]` is a no-op.
9
+ */
10
+ export class SQLiteContractRepository {
11
+ constructor(db, options) {
12
+ this.db = db;
13
+ this.version = 1;
14
+ this.initPromise = null;
15
+ this.prefix = sanitizePrefix(options?.prefix ?? "ark_");
16
+ this.table = `${this.prefix}contracts`;
17
+ }
18
+ // ── Lifecycle ──────────────────────────────────────────────────────
19
+ ensureInit() {
20
+ if (!this.initPromise) {
21
+ this.initPromise = this.init();
22
+ }
23
+ return this.initPromise;
24
+ }
25
+ async init() {
26
+ await this.db.run(`
27
+ CREATE TABLE IF NOT EXISTS ${this.table} (
28
+ script TEXT PRIMARY KEY,
29
+ address TEXT NOT NULL,
30
+ type TEXT NOT NULL,
31
+ state TEXT NOT NULL,
32
+ params_json TEXT NOT NULL,
33
+ created_at INTEGER NOT NULL,
34
+ expires_at INTEGER,
35
+ label TEXT,
36
+ metadata_json TEXT
37
+ )
38
+ `);
39
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_${this.prefix}contracts_type ON ${this.table} (type)`);
40
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_${this.prefix}contracts_state ON ${this.table} (state)`);
41
+ }
42
+ async [Symbol.asyncDispose]() {
43
+ // no-op — consumer owns the SQLExecutor lifecycle
44
+ }
45
+ // ── Clear ──────────────────────────────────────────────────────────
46
+ async clear() {
47
+ await this.ensureInit();
48
+ await this.db.run(`DELETE FROM ${this.table}`);
49
+ }
50
+ // ── Contract management ────────────────────────────────────────────
51
+ async getContracts(filter) {
52
+ await this.ensureInit();
53
+ const conditions = [];
54
+ const params = [];
55
+ if (filter) {
56
+ this.addFilterCondition(conditions, params, "script", filter.script);
57
+ this.addFilterCondition(conditions, params, "state", filter.state);
58
+ this.addFilterCondition(conditions, params, "type", filter.type);
59
+ }
60
+ let sql = `SELECT * FROM ${this.table}`;
61
+ if (conditions.length > 0) {
62
+ sql += ` WHERE ${conditions.join(" AND ")}`;
63
+ }
64
+ const rows = await this.db.all(sql, params);
65
+ return rows.map(contractRowToDomain);
66
+ }
67
+ async saveContract(contract) {
68
+ await this.ensureInit();
69
+ await this.db.run(`INSERT OR REPLACE INTO ${this.table}
70
+ (script, address, type, state, params_json,
71
+ created_at, expires_at, label, metadata_json)
72
+ VALUES (?, ?, ?, ?, ?,
73
+ ?, ?, ?, ?)`, [
74
+ contract.script,
75
+ contract.address,
76
+ contract.type,
77
+ contract.state,
78
+ JSON.stringify(contract.params),
79
+ contract.createdAt,
80
+ contract.expiresAt ?? null,
81
+ contract.label ?? null,
82
+ contract.metadata ? JSON.stringify(contract.metadata) : null,
83
+ ]);
84
+ }
85
+ async deleteContract(script) {
86
+ await this.ensureInit();
87
+ await this.db.run(`DELETE FROM ${this.table} WHERE script = ?`, [
88
+ script,
89
+ ]);
90
+ }
91
+ // ── Helpers ─────────────────────────────────────────────────────────
92
+ addFilterCondition(conditions, params, column, value) {
93
+ if (value === undefined)
94
+ return;
95
+ if (Array.isArray(value)) {
96
+ if (value.length === 0)
97
+ return;
98
+ const placeholders = value.map(() => "?").join(", ");
99
+ conditions.push(`${column} IN (${placeholders})`);
100
+ params.push(...value);
101
+ }
102
+ else {
103
+ conditions.push(`${column} = ?`);
104
+ params.push(value);
105
+ }
106
+ }
107
+ }
108
+ // ── Row → Domain converter ──────────────────────────────────────────────
109
+ const SAFE_PREFIX = /^[a-zA-Z0-9_]+$/;
110
+ function sanitizePrefix(prefix) {
111
+ if (!SAFE_PREFIX.test(prefix)) {
112
+ throw new Error(`Invalid table prefix "${prefix}": only letters, digits, and underscores are allowed`);
113
+ }
114
+ return prefix;
115
+ }
116
+ function contractRowToDomain(row) {
117
+ const contract = {
118
+ script: row.script,
119
+ address: row.address,
120
+ type: row.type,
121
+ state: row.state,
122
+ params: JSON.parse(row.params_json),
123
+ createdAt: row.created_at,
124
+ };
125
+ if (row.expires_at !== null) {
126
+ contract.expiresAt = row.expires_at;
127
+ }
128
+ if (row.label !== null) {
129
+ contract.label = row.label;
130
+ }
131
+ if (row.metadata_json !== null) {
132
+ contract.metadata = JSON.parse(row.metadata_json);
133
+ }
134
+ return contract;
135
+ }
@@ -0,0 +1,2 @@
1
+ export { SQLiteWalletRepository } from './walletRepository.js';
2
+ export { SQLiteContractRepository } from './contractRepository.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,324 @@
1
+ import { serializeVtxo, serializeUtxo, deserializeVtxo, deserializeUtxo, } from '../serialization.js';
2
+ /**
3
+ * SQLite-based implementation of WalletRepository.
4
+ *
5
+ * Uses the SQLExecutor interface so consumers can plug in any SQLite driver
6
+ * (expo-sqlite, better-sqlite3, etc.).
7
+ *
8
+ * Tables are created lazily on first operation via `ensureInit()`.
9
+ * The consumer owns the SQLExecutor lifecycle — `[Symbol.asyncDispose]` is a no-op.
10
+ */
11
+ export class SQLiteWalletRepository {
12
+ constructor(db, options) {
13
+ this.db = db;
14
+ this.version = 1;
15
+ this.initPromise = null;
16
+ this.prefix = sanitizePrefix(options?.prefix ?? "ark_");
17
+ this.tables = {
18
+ vtxos: `${this.prefix}vtxos`,
19
+ utxos: `${this.prefix}utxos`,
20
+ transactions: `${this.prefix}transactions`,
21
+ walletState: `${this.prefix}wallet_state`,
22
+ };
23
+ }
24
+ // ── Lifecycle ──────────────────────────────────────────────────────
25
+ ensureInit() {
26
+ if (!this.initPromise) {
27
+ this.initPromise = this.init();
28
+ }
29
+ return this.initPromise;
30
+ }
31
+ async init() {
32
+ await this.db.run(`
33
+ CREATE TABLE IF NOT EXISTS ${this.tables.vtxos} (
34
+ txid TEXT NOT NULL,
35
+ vout INTEGER NOT NULL,
36
+ value INTEGER NOT NULL,
37
+ address TEXT NOT NULL,
38
+ tap_tree TEXT NOT NULL,
39
+ forfeit_cb TEXT NOT NULL,
40
+ forfeit_s TEXT NOT NULL,
41
+ intent_cb TEXT NOT NULL,
42
+ intent_s TEXT NOT NULL,
43
+ status_json TEXT NOT NULL,
44
+ virtual_status_json TEXT NOT NULL,
45
+ created_at TEXT NOT NULL,
46
+ is_unrolled INTEGER NOT NULL DEFAULT 0,
47
+ is_spent INTEGER,
48
+ spent_by TEXT,
49
+ settled_by TEXT,
50
+ ark_tx_id TEXT,
51
+ extra_witness_json TEXT,
52
+ assets_json TEXT,
53
+ PRIMARY KEY (txid, vout)
54
+ )
55
+ `);
56
+ await this.db.run(`
57
+ CREATE TABLE IF NOT EXISTS ${this.tables.utxos} (
58
+ txid TEXT NOT NULL,
59
+ vout INTEGER NOT NULL,
60
+ value INTEGER NOT NULL,
61
+ address TEXT NOT NULL,
62
+ tap_tree TEXT NOT NULL,
63
+ forfeit_cb TEXT NOT NULL,
64
+ forfeit_s TEXT NOT NULL,
65
+ intent_cb TEXT NOT NULL,
66
+ intent_s TEXT NOT NULL,
67
+ status_json TEXT NOT NULL,
68
+ extra_witness_json TEXT,
69
+ PRIMARY KEY (txid, vout)
70
+ )
71
+ `);
72
+ await this.db.run(`
73
+ CREATE TABLE IF NOT EXISTS ${this.tables.transactions} (
74
+ address TEXT NOT NULL,
75
+ boarding_txid TEXT NOT NULL,
76
+ commitment_txid TEXT NOT NULL,
77
+ ark_txid TEXT NOT NULL,
78
+ type TEXT NOT NULL,
79
+ amount INTEGER NOT NULL,
80
+ settled INTEGER NOT NULL DEFAULT 0,
81
+ created_at INTEGER NOT NULL,
82
+ assets_json TEXT,
83
+ PRIMARY KEY (address, boarding_txid, commitment_txid, ark_txid)
84
+ )
85
+ `);
86
+ await this.db.run(`
87
+ CREATE TABLE IF NOT EXISTS ${this.tables.walletState} (
88
+ key TEXT PRIMARY KEY,
89
+ last_sync_time INTEGER,
90
+ settings_json TEXT
91
+ )
92
+ `);
93
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_${this.prefix}vtxos_address ON ${this.tables.vtxos} (address)`);
94
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_${this.prefix}utxos_address ON ${this.tables.utxos} (address)`);
95
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_${this.prefix}transactions_address ON ${this.tables.transactions} (address)`);
96
+ }
97
+ async [Symbol.asyncDispose]() {
98
+ // no-op — consumer owns the SQLExecutor lifecycle
99
+ }
100
+ // ── Clear ──────────────────────────────────────────────────────────
101
+ async clear() {
102
+ await this.ensureInit();
103
+ await this.db.run(`DELETE FROM ${this.tables.vtxos}`);
104
+ await this.db.run(`DELETE FROM ${this.tables.utxos}`);
105
+ await this.db.run(`DELETE FROM ${this.tables.transactions}`);
106
+ await this.db.run(`DELETE FROM ${this.tables.walletState}`);
107
+ }
108
+ // ── VTXO management ────────────────────────────────────────────────
109
+ async getVtxos(address) {
110
+ await this.ensureInit();
111
+ const rows = await this.db.all(`SELECT * FROM ${this.tables.vtxos} WHERE address = ?`, [address]);
112
+ return rows.map(vtxoRowToDomain);
113
+ }
114
+ async saveVtxos(address, vtxos) {
115
+ await this.ensureInit();
116
+ for (const vtxo of vtxos) {
117
+ const s = serializeVtxo(vtxo);
118
+ await this.db.run(`INSERT OR REPLACE INTO ${this.tables.vtxos}
119
+ (txid, vout, value, address,
120
+ tap_tree, forfeit_cb, forfeit_s, intent_cb, intent_s,
121
+ status_json, virtual_status_json, created_at,
122
+ is_unrolled, is_spent, spent_by, settled_by, ark_tx_id,
123
+ extra_witness_json, assets_json)
124
+ VALUES (?, ?, ?, ?,
125
+ ?, ?, ?, ?, ?,
126
+ ?, ?, ?,
127
+ ?, ?, ?, ?, ?,
128
+ ?, ?)`, [
129
+ s.txid,
130
+ s.vout,
131
+ s.value,
132
+ address,
133
+ s.tapTree,
134
+ s.forfeitTapLeafScript.cb,
135
+ s.forfeitTapLeafScript.s,
136
+ s.intentTapLeafScript.cb,
137
+ s.intentTapLeafScript.s,
138
+ JSON.stringify(s.status),
139
+ JSON.stringify(s.virtualStatus),
140
+ typeof s.createdAt === "string"
141
+ ? s.createdAt
142
+ : s.createdAt instanceof Date
143
+ ? s.createdAt.toISOString()
144
+ : new Date(s.createdAt).toISOString(),
145
+ s.isUnrolled ? 1 : 0,
146
+ s.isSpent === undefined ? null : s.isSpent ? 1 : 0,
147
+ s.spentBy ?? null,
148
+ s.settledBy ?? null,
149
+ s.arkTxId ?? null,
150
+ s.extraWitness ? JSON.stringify(s.extraWitness) : null,
151
+ s.assets ? JSON.stringify(s.assets) : null,
152
+ ]);
153
+ }
154
+ }
155
+ async deleteVtxos(address) {
156
+ await this.ensureInit();
157
+ await this.db.run(`DELETE FROM ${this.tables.vtxos} WHERE address = ?`, [address]);
158
+ }
159
+ // ── UTXO management ────────────────────────────────────────────────
160
+ async getUtxos(address) {
161
+ await this.ensureInit();
162
+ const rows = await this.db.all(`SELECT * FROM ${this.tables.utxos} WHERE address = ?`, [address]);
163
+ return rows.map(utxoRowToDomain);
164
+ }
165
+ async saveUtxos(address, utxos) {
166
+ await this.ensureInit();
167
+ for (const utxo of utxos) {
168
+ const s = serializeUtxo(utxo);
169
+ await this.db.run(`INSERT OR REPLACE INTO ${this.tables.utxos}
170
+ (txid, vout, value, address,
171
+ tap_tree, forfeit_cb, forfeit_s, intent_cb, intent_s,
172
+ status_json, extra_witness_json)
173
+ VALUES (?, ?, ?, ?,
174
+ ?, ?, ?, ?, ?,
175
+ ?, ?)`, [
176
+ s.txid,
177
+ s.vout,
178
+ s.value,
179
+ address,
180
+ s.tapTree,
181
+ s.forfeitTapLeafScript.cb,
182
+ s.forfeitTapLeafScript.s,
183
+ s.intentTapLeafScript.cb,
184
+ s.intentTapLeafScript.s,
185
+ JSON.stringify(s.status),
186
+ s.extraWitness ? JSON.stringify(s.extraWitness) : null,
187
+ ]);
188
+ }
189
+ }
190
+ async deleteUtxos(address) {
191
+ await this.ensureInit();
192
+ await this.db.run(`DELETE FROM ${this.tables.utxos} WHERE address = ?`, [address]);
193
+ }
194
+ // ── Transaction history ────────────────────────────────────────────
195
+ async getTransactionHistory(address) {
196
+ await this.ensureInit();
197
+ const rows = await this.db.all(`SELECT * FROM ${this.tables.transactions} WHERE address = ? ORDER BY created_at ASC`, [address]);
198
+ return rows.map(txRowToDomain);
199
+ }
200
+ async saveTransactions(address, txs) {
201
+ await this.ensureInit();
202
+ for (const tx of txs) {
203
+ await this.db.run(`INSERT OR REPLACE INTO ${this.tables.transactions}
204
+ (address, boarding_txid, commitment_txid, ark_txid,
205
+ type, amount, settled, created_at, assets_json)
206
+ VALUES (?, ?, ?, ?,
207
+ ?, ?, ?, ?, ?)`, [
208
+ address,
209
+ tx.key.boardingTxid,
210
+ tx.key.commitmentTxid,
211
+ tx.key.arkTxid,
212
+ tx.type,
213
+ tx.amount,
214
+ tx.settled ? 1 : 0,
215
+ tx.createdAt,
216
+ tx.assets ? JSON.stringify(tx.assets) : null,
217
+ ]);
218
+ }
219
+ }
220
+ async deleteTransactions(address) {
221
+ await this.ensureInit();
222
+ await this.db.run(`DELETE FROM ${this.tables.transactions} WHERE address = ?`, [address]);
223
+ }
224
+ // ── Wallet state ───────────────────────────────────────────────────
225
+ async getWalletState() {
226
+ await this.ensureInit();
227
+ const row = await this.db.get(`SELECT * FROM ${this.tables.walletState} WHERE key = ?`, ["state"]);
228
+ if (!row)
229
+ return null;
230
+ const state = {};
231
+ if (row.last_sync_time !== null && row.last_sync_time !== undefined) {
232
+ state.lastSyncTime = row.last_sync_time;
233
+ }
234
+ if (row.settings_json) {
235
+ state.settings = JSON.parse(row.settings_json);
236
+ }
237
+ return state;
238
+ }
239
+ async saveWalletState(state) {
240
+ await this.ensureInit();
241
+ await this.db.run(`INSERT OR REPLACE INTO ${this.tables.walletState}
242
+ (key, last_sync_time, settings_json)
243
+ VALUES (?, ?, ?)`, [
244
+ "state",
245
+ state.lastSyncTime ?? null,
246
+ state.settings ? JSON.stringify(state.settings) : null,
247
+ ]);
248
+ }
249
+ }
250
+ const SAFE_PREFIX = /^[a-zA-Z0-9_]+$/;
251
+ function sanitizePrefix(prefix) {
252
+ if (!SAFE_PREFIX.test(prefix)) {
253
+ throw new Error(`Invalid table prefix "${prefix}": only letters, digits, and underscores are allowed`);
254
+ }
255
+ return prefix;
256
+ }
257
+ // ── Row → Domain converters ────────────────────────────────────────────
258
+ function vtxoRowToDomain(row) {
259
+ const serialized = {
260
+ txid: row.txid,
261
+ vout: row.vout,
262
+ value: row.value,
263
+ tapTree: row.tap_tree,
264
+ forfeitTapLeafScript: {
265
+ cb: row.forfeit_cb,
266
+ s: row.forfeit_s,
267
+ },
268
+ intentTapLeafScript: {
269
+ cb: row.intent_cb,
270
+ s: row.intent_s,
271
+ },
272
+ status: JSON.parse(row.status_json),
273
+ virtualStatus: JSON.parse(row.virtual_status_json),
274
+ createdAt: new Date(row.created_at),
275
+ isUnrolled: row.is_unrolled === 1,
276
+ isSpent: row.is_spent === null ? undefined : row.is_spent === 1,
277
+ spentBy: row.spent_by ?? undefined,
278
+ settledBy: row.settled_by ?? undefined,
279
+ arkTxId: row.ark_tx_id ?? undefined,
280
+ extraWitness: row.extra_witness_json
281
+ ? JSON.parse(row.extra_witness_json)
282
+ : undefined,
283
+ assets: row.assets_json ? JSON.parse(row.assets_json) : undefined,
284
+ };
285
+ return deserializeVtxo(serialized);
286
+ }
287
+ function utxoRowToDomain(row) {
288
+ const serialized = {
289
+ txid: row.txid,
290
+ vout: row.vout,
291
+ value: row.value,
292
+ tapTree: row.tap_tree,
293
+ forfeitTapLeafScript: {
294
+ cb: row.forfeit_cb,
295
+ s: row.forfeit_s,
296
+ },
297
+ intentTapLeafScript: {
298
+ cb: row.intent_cb,
299
+ s: row.intent_s,
300
+ },
301
+ status: JSON.parse(row.status_json),
302
+ extraWitness: row.extra_witness_json
303
+ ? JSON.parse(row.extra_witness_json)
304
+ : undefined,
305
+ };
306
+ return deserializeUtxo(serialized);
307
+ }
308
+ function txRowToDomain(row) {
309
+ const tx = {
310
+ key: {
311
+ boardingTxid: row.boarding_txid,
312
+ commitmentTxid: row.commitment_txid,
313
+ arkTxid: row.ark_txid,
314
+ },
315
+ type: row.type,
316
+ amount: row.amount,
317
+ settled: row.settled === 1,
318
+ createdAt: row.created_at,
319
+ };
320
+ if (row.assets_json) {
321
+ tx.assets = JSON.parse(row.assets_json);
322
+ }
323
+ return tx;
324
+ }
@@ -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 {};