@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.
- package/README.md +586 -54
- package/dist/cjs/asset/assetGroup.js +141 -0
- package/dist/cjs/asset/assetId.js +88 -0
- package/dist/cjs/asset/assetInput.js +204 -0
- package/dist/cjs/asset/assetOutput.js +159 -0
- package/dist/cjs/asset/assetRef.js +82 -0
- package/dist/cjs/asset/index.js +24 -0
- package/dist/cjs/asset/metadata.js +172 -0
- package/dist/cjs/asset/packet.js +164 -0
- package/dist/cjs/asset/types.js +25 -0
- package/dist/cjs/asset/utils.js +105 -0
- package/dist/cjs/bip322/index.js +270 -0
- package/dist/cjs/contracts/arkcontract.js +148 -0
- package/dist/cjs/contracts/contractManager.js +436 -0
- package/dist/cjs/contracts/contractWatcher.js +567 -0
- package/dist/cjs/contracts/handlers/default.js +85 -0
- package/dist/cjs/contracts/handlers/delegate.js +89 -0
- package/dist/cjs/contracts/handlers/helpers.js +105 -0
- package/dist/cjs/contracts/handlers/index.js +19 -0
- package/dist/cjs/contracts/handlers/registry.js +89 -0
- package/dist/cjs/contracts/handlers/vhtlc.js +193 -0
- package/dist/cjs/contracts/index.js +41 -0
- package/dist/cjs/contracts/types.js +2 -0
- package/dist/cjs/forfeit.js +12 -8
- package/dist/cjs/identity/index.js +1 -0
- package/dist/cjs/identity/seedIdentity.js +255 -0
- package/dist/cjs/index.js +72 -14
- package/dist/cjs/intent/index.js +47 -11
- package/dist/cjs/providers/ark.js +7 -0
- package/dist/cjs/providers/delegator.js +66 -0
- package/dist/cjs/providers/expoIndexer.js +5 -0
- package/dist/cjs/providers/indexer.js +68 -1
- package/dist/cjs/providers/utils.js +1 -0
- package/dist/cjs/repositories/contractRepository.js +0 -103
- package/dist/cjs/repositories/inMemory/contractRepository.js +55 -0
- package/dist/cjs/repositories/inMemory/walletRepository.js +80 -0
- package/dist/cjs/repositories/index.js +16 -0
- package/dist/cjs/repositories/indexedDB/contractRepository.js +187 -0
- package/dist/cjs/repositories/indexedDB/db.js +19 -0
- package/dist/cjs/repositories/indexedDB/manager.js +97 -0
- package/dist/cjs/repositories/indexedDB/schema.js +159 -0
- package/dist/cjs/repositories/indexedDB/walletRepository.js +338 -0
- package/dist/cjs/repositories/indexedDB/websqlAdapter.js +144 -0
- package/dist/cjs/repositories/migrations/contractRepositoryImpl.js +127 -0
- package/dist/cjs/repositories/migrations/fromStorageAdapter.js +66 -0
- package/dist/cjs/repositories/migrations/walletRepositoryImpl.js +180 -0
- package/dist/cjs/repositories/realm/contractRepository.js +120 -0
- package/dist/cjs/repositories/realm/index.js +9 -0
- package/dist/cjs/repositories/realm/schemas.js +108 -0
- package/dist/cjs/repositories/realm/types.js +7 -0
- package/dist/cjs/repositories/realm/walletRepository.js +273 -0
- package/dist/cjs/repositories/serialization.js +49 -0
- package/dist/cjs/repositories/sqlite/contractRepository.js +139 -0
- package/dist/cjs/repositories/sqlite/index.js +7 -0
- package/dist/cjs/repositories/sqlite/types.js +2 -0
- package/dist/cjs/repositories/sqlite/walletRepository.js +328 -0
- package/dist/cjs/repositories/walletRepository.js +0 -169
- package/dist/cjs/script/base.js +54 -0
- package/dist/cjs/script/delegate.js +49 -0
- package/dist/cjs/storage/asyncStorage.js +4 -1
- package/dist/cjs/storage/fileSystem.js +3 -0
- package/dist/cjs/storage/inMemory.js +3 -0
- package/dist/cjs/storage/indexedDB.js +5 -1
- package/dist/cjs/storage/localStorage.js +3 -0
- package/dist/cjs/utils/arkTransaction.js +16 -0
- package/dist/cjs/utils/transactionHistory.js +50 -0
- package/dist/cjs/wallet/asset-manager.js +338 -0
- package/dist/cjs/wallet/asset.js +117 -0
- package/dist/cjs/wallet/batch.js +1 -1
- package/dist/cjs/wallet/delegator.js +235 -0
- package/dist/cjs/wallet/expo/background.js +133 -0
- package/dist/cjs/wallet/expo/index.js +9 -0
- package/dist/cjs/wallet/expo/wallet.js +231 -0
- package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +568 -0
- package/dist/cjs/wallet/serviceWorker/wallet.js +383 -102
- package/dist/cjs/wallet/utils.js +58 -0
- package/dist/cjs/wallet/validation.js +151 -0
- package/dist/cjs/wallet/vtxo-manager.js +8 -1
- package/dist/cjs/wallet/wallet.js +702 -260
- package/dist/cjs/worker/browser/service-worker-manager.js +82 -0
- package/dist/cjs/{wallet/serviceWorker → worker/browser}/utils.js +2 -1
- package/dist/cjs/worker/expo/asyncStorageTaskQueue.js +78 -0
- package/dist/cjs/worker/expo/index.js +12 -0
- package/dist/cjs/worker/expo/processors/contractPollProcessor.js +61 -0
- package/dist/cjs/worker/expo/processors/index.js +6 -0
- package/dist/cjs/worker/expo/taskQueue.js +41 -0
- package/dist/cjs/worker/expo/taskRunner.js +57 -0
- package/dist/cjs/worker/messageBus.js +252 -0
- package/dist/esm/asset/assetGroup.js +137 -0
- package/dist/esm/asset/assetId.js +84 -0
- package/dist/esm/asset/assetInput.js +199 -0
- package/dist/esm/asset/assetOutput.js +154 -0
- package/dist/esm/asset/assetRef.js +78 -0
- package/dist/esm/asset/index.js +8 -0
- package/dist/esm/asset/metadata.js +167 -0
- package/dist/esm/asset/packet.js +159 -0
- package/dist/esm/asset/types.js +22 -0
- package/dist/esm/asset/utils.js +99 -0
- package/dist/esm/bip322/index.js +267 -0
- package/dist/esm/contracts/arkcontract.js +141 -0
- package/dist/esm/contracts/contractManager.js +432 -0
- package/dist/esm/contracts/contractWatcher.js +563 -0
- package/dist/esm/contracts/handlers/default.js +82 -0
- package/dist/esm/contracts/handlers/delegate.js +86 -0
- package/dist/esm/contracts/handlers/helpers.js +66 -0
- package/dist/esm/contracts/handlers/index.js +12 -0
- package/dist/esm/contracts/handlers/registry.js +86 -0
- package/dist/esm/contracts/handlers/vhtlc.js +190 -0
- package/dist/esm/contracts/index.js +13 -0
- package/dist/esm/contracts/types.js +1 -0
- package/dist/esm/forfeit.js +11 -8
- package/dist/esm/identity/index.js +1 -0
- package/dist/esm/identity/seedIdentity.js +249 -0
- package/dist/esm/index.js +28 -15
- package/dist/esm/intent/index.js +44 -9
- package/dist/esm/providers/ark.js +7 -0
- package/dist/esm/providers/delegator.js +62 -0
- package/dist/esm/providers/expoIndexer.js +5 -0
- package/dist/esm/providers/indexer.js +68 -1
- package/dist/esm/providers/utils.js +1 -0
- package/dist/esm/repositories/contractRepository.js +1 -101
- package/dist/esm/repositories/inMemory/contractRepository.js +51 -0
- package/dist/esm/repositories/inMemory/walletRepository.js +76 -0
- package/dist/esm/repositories/index.js +8 -0
- package/dist/esm/repositories/indexedDB/contractRepository.js +183 -0
- package/dist/esm/repositories/indexedDB/db.js +4 -0
- package/dist/esm/repositories/indexedDB/manager.js +92 -0
- package/dist/esm/repositories/indexedDB/schema.js +155 -0
- package/dist/esm/repositories/indexedDB/walletRepository.js +334 -0
- package/dist/esm/repositories/indexedDB/websqlAdapter.js +138 -0
- package/dist/esm/repositories/migrations/contractRepositoryImpl.js +121 -0
- package/dist/esm/repositories/migrations/fromStorageAdapter.js +58 -0
- package/dist/esm/repositories/migrations/walletRepositoryImpl.js +176 -0
- package/dist/esm/repositories/realm/contractRepository.js +116 -0
- package/dist/esm/repositories/realm/index.js +3 -0
- package/dist/esm/repositories/realm/schemas.js +105 -0
- package/dist/esm/repositories/realm/types.js +6 -0
- package/dist/esm/repositories/realm/walletRepository.js +269 -0
- package/dist/esm/repositories/serialization.js +40 -0
- package/dist/esm/repositories/sqlite/contractRepository.js +135 -0
- package/dist/esm/repositories/sqlite/index.js +2 -0
- package/dist/esm/repositories/sqlite/types.js +1 -0
- package/dist/esm/repositories/sqlite/walletRepository.js +324 -0
- package/dist/esm/repositories/walletRepository.js +1 -167
- package/dist/esm/script/base.js +21 -1
- package/dist/esm/script/delegate.js +46 -0
- package/dist/esm/storage/asyncStorage.js +4 -1
- package/dist/esm/storage/fileSystem.js +3 -0
- package/dist/esm/storage/inMemory.js +3 -0
- package/dist/esm/storage/indexedDB.js +5 -1
- package/dist/esm/storage/localStorage.js +3 -0
- package/dist/esm/utils/arkTransaction.js +15 -0
- package/dist/esm/utils/transactionHistory.js +50 -0
- package/dist/esm/wallet/asset-manager.js +333 -0
- package/dist/esm/wallet/asset.js +111 -0
- package/dist/esm/wallet/batch.js +1 -1
- package/dist/esm/wallet/delegator.js +231 -0
- package/dist/esm/wallet/expo/background.js +128 -0
- package/dist/esm/wallet/expo/index.js +2 -0
- package/dist/esm/wallet/expo/wallet.js +194 -0
- package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +564 -0
- package/dist/esm/wallet/serviceWorker/wallet.js +382 -101
- package/dist/esm/wallet/utils.js +54 -0
- package/dist/esm/wallet/validation.js +139 -0
- package/dist/esm/wallet/vtxo-manager.js +8 -1
- package/dist/esm/wallet/wallet.js +704 -229
- package/dist/esm/worker/browser/service-worker-manager.js +76 -0
- package/dist/esm/{wallet/serviceWorker → worker/browser}/utils.js +2 -1
- package/dist/esm/worker/expo/asyncStorageTaskQueue.js +74 -0
- package/dist/esm/worker/expo/index.js +4 -0
- package/dist/esm/worker/expo/processors/contractPollProcessor.js +58 -0
- package/dist/esm/worker/expo/processors/index.js +1 -0
- package/dist/esm/worker/expo/taskQueue.js +37 -0
- package/dist/esm/worker/expo/taskRunner.js +54 -0
- package/dist/esm/worker/messageBus.js +248 -0
- package/dist/types/asset/assetGroup.d.ts +28 -0
- package/dist/types/asset/assetId.d.ts +19 -0
- package/dist/types/asset/assetInput.d.ts +46 -0
- package/dist/types/asset/assetOutput.d.ts +39 -0
- package/dist/types/asset/assetRef.d.ts +25 -0
- package/dist/types/asset/index.d.ts +8 -0
- package/dist/types/asset/metadata.d.ts +37 -0
- package/dist/types/asset/packet.d.ts +27 -0
- package/dist/types/asset/types.d.ts +18 -0
- package/dist/types/asset/utils.d.ts +21 -0
- package/dist/types/bip322/index.d.ts +55 -0
- package/dist/types/contracts/arkcontract.d.ts +101 -0
- package/dist/types/contracts/contractManager.d.ts +331 -0
- package/dist/types/contracts/contractWatcher.d.ts +192 -0
- package/dist/types/contracts/handlers/default.d.ts +19 -0
- package/dist/types/contracts/handlers/delegate.d.ts +21 -0
- package/dist/types/contracts/handlers/helpers.d.ts +18 -0
- package/dist/types/contracts/handlers/index.d.ts +7 -0
- package/dist/types/contracts/handlers/registry.d.ts +65 -0
- package/dist/types/contracts/handlers/vhtlc.d.ts +32 -0
- package/dist/types/contracts/index.d.ts +14 -0
- package/dist/types/contracts/types.d.ts +222 -0
- package/dist/types/forfeit.d.ts +2 -1
- package/dist/types/identity/index.d.ts +1 -0
- package/dist/types/identity/seedIdentity.d.ts +128 -0
- package/dist/types/index.d.ts +22 -12
- package/dist/types/intent/index.d.ts +15 -1
- package/dist/types/providers/ark.d.ts +11 -2
- package/dist/types/providers/delegator.d.ts +29 -0
- package/dist/types/providers/indexer.d.ts +11 -1
- package/dist/types/repositories/contractRepository.d.ts +30 -19
- package/dist/types/repositories/inMemory/contractRepository.d.ts +17 -0
- package/dist/types/repositories/inMemory/walletRepository.d.ts +26 -0
- package/dist/types/repositories/index.d.ts +7 -0
- package/dist/types/repositories/indexedDB/contractRepository.d.ts +21 -0
- package/dist/types/repositories/indexedDB/db.d.ts +4 -0
- package/dist/types/repositories/indexedDB/manager.d.ts +22 -0
- package/dist/types/repositories/indexedDB/schema.d.ts +8 -0
- package/dist/types/repositories/indexedDB/walletRepository.d.ts +25 -0
- package/dist/types/repositories/indexedDB/websqlAdapter.d.ts +49 -0
- package/dist/types/repositories/migrations/contractRepositoryImpl.d.ts +24 -0
- package/dist/types/repositories/migrations/fromStorageAdapter.d.ts +19 -0
- package/dist/types/repositories/migrations/walletRepositoryImpl.d.ts +27 -0
- package/dist/types/repositories/realm/contractRepository.d.ts +24 -0
- package/dist/types/repositories/realm/index.d.ts +4 -0
- package/dist/types/repositories/realm/schemas.d.ts +208 -0
- package/dist/types/repositories/realm/types.d.ts +16 -0
- package/dist/types/repositories/realm/walletRepository.d.ts +31 -0
- package/dist/types/repositories/serialization.d.ts +40 -0
- package/dist/types/repositories/sqlite/contractRepository.d.ts +33 -0
- package/dist/types/repositories/sqlite/index.d.ts +3 -0
- package/dist/types/repositories/sqlite/types.d.ts +18 -0
- package/dist/types/repositories/sqlite/walletRepository.d.ts +40 -0
- package/dist/types/repositories/walletRepository.d.ts +13 -24
- package/dist/types/script/base.d.ts +1 -0
- package/dist/types/script/delegate.d.ts +36 -0
- package/dist/types/storage/asyncStorage.d.ts +4 -0
- package/dist/types/storage/fileSystem.d.ts +3 -0
- package/dist/types/storage/inMemory.d.ts +3 -0
- package/dist/types/storage/index.d.ts +3 -0
- package/dist/types/storage/indexedDB.d.ts +3 -0
- package/dist/types/storage/localStorage.d.ts +3 -0
- package/dist/types/utils/arkTransaction.d.ts +6 -0
- package/dist/types/wallet/asset-manager.d.ts +78 -0
- package/dist/types/wallet/asset.d.ts +21 -0
- package/dist/types/wallet/batch.d.ts +1 -1
- package/dist/types/wallet/delegator.d.ts +24 -0
- package/dist/types/wallet/expo/background.d.ts +66 -0
- package/dist/types/wallet/expo/index.d.ts +4 -0
- package/dist/types/wallet/expo/wallet.d.ts +97 -0
- package/dist/types/wallet/index.d.ts +75 -2
- package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +366 -0
- package/dist/types/wallet/serviceWorker/wallet.d.ts +20 -11
- package/dist/types/wallet/utils.d.ts +12 -1
- package/dist/types/wallet/validation.d.ts +24 -0
- package/dist/types/wallet/wallet.d.ts +111 -17
- package/dist/types/worker/browser/service-worker-manager.d.ts +21 -0
- package/dist/types/{wallet/serviceWorker → worker/browser}/utils.d.ts +2 -1
- package/dist/types/worker/expo/asyncStorageTaskQueue.d.ts +46 -0
- package/dist/types/worker/expo/index.d.ts +7 -0
- package/dist/types/worker/expo/processors/contractPollProcessor.d.ts +14 -0
- package/dist/types/worker/expo/processors/index.d.ts +1 -0
- package/dist/types/worker/expo/taskQueue.d.ts +50 -0
- package/dist/types/worker/expo/taskRunner.d.ts +42 -0
- package/dist/types/worker/messageBus.d.ts +109 -0
- package/package.json +69 -11
- package/dist/cjs/wallet/serviceWorker/request.js +0 -78
- package/dist/cjs/wallet/serviceWorker/response.js +0 -222
- package/dist/cjs/wallet/serviceWorker/worker.js +0 -655
- package/dist/esm/wallet/serviceWorker/request.js +0 -75
- package/dist/esm/wallet/serviceWorker/response.js +0 -219
- package/dist/esm/wallet/serviceWorker/worker.js +0 -651
- package/dist/types/wallet/serviceWorker/request.d.ts +0 -74
- package/dist/types/wallet/serviceWorker/response.d.ts +0 -123
- package/dist/types/wallet/serviceWorker/worker.d.ts +0 -53
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* WebSQL adapter over expo-sqlite.
|
|
4
|
+
*
|
|
5
|
+
* Bridges the WebSQL API surface that indexeddbshim expects to the
|
|
6
|
+
* synchronous expo-sqlite driver. Only the subset actually called by
|
|
7
|
+
* the shim is implemented:
|
|
8
|
+
*
|
|
9
|
+
* openDatabase(name, version, displayName, estimatedSize) → WebSQLDatabase
|
|
10
|
+
* db.transaction(cb, errCb?, successCb?)
|
|
11
|
+
* tx.executeSql(sql, args?, successCb?, errorCb?)
|
|
12
|
+
* resultSet = { insertId, rowsAffected, rows: { length, item(i) } }
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.WebSQLDatabase = exports.WebSQLTransaction = void 0;
|
|
16
|
+
exports.openDatabase = openDatabase;
|
|
17
|
+
const expo_sqlite_1 = require("expo-sqlite");
|
|
18
|
+
// ── Database cache ───────────────────────────────────────────────────
|
|
19
|
+
const dbCache = new Map();
|
|
20
|
+
function getSqliteDb(name) {
|
|
21
|
+
let db = dbCache.get(name);
|
|
22
|
+
if (db)
|
|
23
|
+
return { db, created: false };
|
|
24
|
+
db = (0, expo_sqlite_1.openDatabaseSync)(name);
|
|
25
|
+
dbCache.set(name, db);
|
|
26
|
+
return { db, created: true };
|
|
27
|
+
}
|
|
28
|
+
// ── WebSQLTransaction ────────────────────────────────────────────────
|
|
29
|
+
class WebSQLTransaction {
|
|
30
|
+
constructor() {
|
|
31
|
+
/** @internal */
|
|
32
|
+
this._queue = [];
|
|
33
|
+
}
|
|
34
|
+
executeSql(sql, args, successCb, errorCb) {
|
|
35
|
+
this._queue.push({ sql, args: args ?? [], successCb, errorCb });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.WebSQLTransaction = WebSQLTransaction;
|
|
39
|
+
// ── Helpers ──────────────────────────────────────────────────────────
|
|
40
|
+
function isRead(sql) {
|
|
41
|
+
const trimmed = sql.trimStart().toUpperCase();
|
|
42
|
+
return trimmed.startsWith("SELECT") || trimmed.startsWith("PRAGMA");
|
|
43
|
+
}
|
|
44
|
+
function buildResultSet(db, sql, args) {
|
|
45
|
+
if (isRead(sql)) {
|
|
46
|
+
const rows = db.getAllSync(sql, args);
|
|
47
|
+
return {
|
|
48
|
+
insertId: 0,
|
|
49
|
+
rowsAffected: 0,
|
|
50
|
+
rows: {
|
|
51
|
+
length: rows.length,
|
|
52
|
+
item(i) {
|
|
53
|
+
return rows[i];
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const result = db.runSync(sql, args);
|
|
59
|
+
return {
|
|
60
|
+
insertId: result.lastInsertRowId,
|
|
61
|
+
rowsAffected: result.changes,
|
|
62
|
+
rows: {
|
|
63
|
+
length: 0,
|
|
64
|
+
item(_i) {
|
|
65
|
+
return undefined;
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function drainQueue(db, tx) {
|
|
71
|
+
// Process until the queue is empty. Success callbacks may enqueue
|
|
72
|
+
// more statements, so we loop rather than iterate a snapshot.
|
|
73
|
+
while (tx._queue.length > 0) {
|
|
74
|
+
const stmt = tx._queue.shift();
|
|
75
|
+
try {
|
|
76
|
+
const rs = buildResultSet(db, stmt.sql, stmt.args);
|
|
77
|
+
if (stmt.successCb) {
|
|
78
|
+
stmt.successCb(tx, rs);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
const sqlError = {
|
|
83
|
+
code: 0,
|
|
84
|
+
message: err?.message ?? String(err),
|
|
85
|
+
};
|
|
86
|
+
if (stmt.errorCb) {
|
|
87
|
+
const shouldContinue = stmt.errorCb(tx, sqlError);
|
|
88
|
+
if (shouldContinue === true) {
|
|
89
|
+
// Error handler returned true → swallow error and continue
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Abort the transaction
|
|
94
|
+
throw err;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// ── WebSQLDatabase ───────────────────────────────────────────────────
|
|
99
|
+
class WebSQLDatabase {
|
|
100
|
+
constructor(db, version) {
|
|
101
|
+
this._db = db;
|
|
102
|
+
this.version = version;
|
|
103
|
+
}
|
|
104
|
+
transaction(callback, errorCb, successCb) {
|
|
105
|
+
// WebSQL is async/callback-based. Schedule via macrotask so the
|
|
106
|
+
// caller's subsequent code runs first (matches browser behavior).
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
const tx = new WebSQLTransaction();
|
|
109
|
+
try {
|
|
110
|
+
// Let the caller enqueue statements
|
|
111
|
+
callback(tx);
|
|
112
|
+
// Execute everything inside a real SQLite transaction
|
|
113
|
+
this._db.withTransactionSync(() => {
|
|
114
|
+
drainQueue(this._db, tx);
|
|
115
|
+
});
|
|
116
|
+
if (successCb)
|
|
117
|
+
successCb();
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
const sqlError = {
|
|
121
|
+
code: 0,
|
|
122
|
+
message: err?.message ?? String(err),
|
|
123
|
+
};
|
|
124
|
+
if (errorCb) {
|
|
125
|
+
errorCb(sqlError);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}, 0);
|
|
129
|
+
}
|
|
130
|
+
readTransaction(callback, errorCb, successCb) {
|
|
131
|
+
// Reads go through the same path — SQLite handles concurrency.
|
|
132
|
+
this.transaction(callback, errorCb, successCb);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
exports.WebSQLDatabase = WebSQLDatabase;
|
|
136
|
+
// ── openDatabase (WebSQL entry point) ────────────────────────────────
|
|
137
|
+
function openDatabase(name, version, _displayName, _estimatedSize, _creationCallback) {
|
|
138
|
+
const { db: sqliteDb, created } = getSqliteDb(name);
|
|
139
|
+
const wsdb = new WebSQLDatabase(sqliteDb, version);
|
|
140
|
+
if (created && _creationCallback) {
|
|
141
|
+
_creationCallback(wsdb);
|
|
142
|
+
}
|
|
143
|
+
return wsdb;
|
|
144
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ContractRepositoryImpl = exports.getCollectionStorageKey = exports.getContractStorageKey = void 0;
|
|
4
|
+
const getContractStorageKey = (id, key) => `contract:${id}:${key}`;
|
|
5
|
+
exports.getContractStorageKey = getContractStorageKey;
|
|
6
|
+
const getCollectionStorageKey = (type) => `collection:${type}`;
|
|
7
|
+
exports.getCollectionStorageKey = getCollectionStorageKey;
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated This is only to be used in migration from storage V1
|
|
10
|
+
*/
|
|
11
|
+
class ContractRepositoryImpl {
|
|
12
|
+
constructor(storage) {
|
|
13
|
+
this.version = 1;
|
|
14
|
+
this.storage = storage;
|
|
15
|
+
}
|
|
16
|
+
async getContractData(contractId, key) {
|
|
17
|
+
const stored = await this.storage.getItem((0, exports.getContractStorageKey)(contractId, key));
|
|
18
|
+
if (!stored)
|
|
19
|
+
return null;
|
|
20
|
+
try {
|
|
21
|
+
const data = JSON.parse(stored);
|
|
22
|
+
return data;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
console.error(`Failed to parse contract data for ${contractId}:${key}:`, error);
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async setContractData(contractId, key, data) {
|
|
30
|
+
try {
|
|
31
|
+
await this.storage.setItem((0, exports.getContractStorageKey)(contractId, key), JSON.stringify(data));
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
console.error(`Failed to persist contract data for ${contractId}:${key}:`, error);
|
|
35
|
+
throw error; // Rethrow to notify caller of failure
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async deleteContractData(contractId, key) {
|
|
39
|
+
try {
|
|
40
|
+
await this.storage.removeItem((0, exports.getContractStorageKey)(contractId, key));
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error(`Failed to remove contract data for ${contractId}:${key}:`, error);
|
|
44
|
+
throw error; // Rethrow to notify caller of failure
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async getContractCollection(contractType) {
|
|
48
|
+
const stored = await this.storage.getItem((0, exports.getCollectionStorageKey)(contractType));
|
|
49
|
+
if (!stored)
|
|
50
|
+
return [];
|
|
51
|
+
try {
|
|
52
|
+
const collection = JSON.parse(stored);
|
|
53
|
+
return collection;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.error(`Failed to parse contract collection ${contractType}:`, error);
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async saveToContractCollection(contractType, item, idField) {
|
|
61
|
+
const collection = await this.getContractCollection(contractType);
|
|
62
|
+
// Validate that the item has the required id field
|
|
63
|
+
const itemId = item[idField];
|
|
64
|
+
if (itemId === undefined || itemId === null) {
|
|
65
|
+
throw new Error(`Item is missing required field '${String(idField)}'`);
|
|
66
|
+
}
|
|
67
|
+
// Find existing item index without mutating the original collection
|
|
68
|
+
const existingIndex = collection.findIndex((i) => i[idField] === itemId);
|
|
69
|
+
// Build new collection without mutating the cached one
|
|
70
|
+
let newCollection;
|
|
71
|
+
if (existingIndex !== -1) {
|
|
72
|
+
// Replace existing item
|
|
73
|
+
newCollection = [
|
|
74
|
+
...collection.slice(0, existingIndex),
|
|
75
|
+
item,
|
|
76
|
+
...collection.slice(existingIndex + 1),
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
// Add new item
|
|
81
|
+
newCollection = [...collection, item];
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
await this.storage.setItem((0, exports.getCollectionStorageKey)(contractType), JSON.stringify(newCollection));
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
console.error(`Failed to persist contract collection ${contractType}:`, error);
|
|
88
|
+
throw error; // Rethrow to notify caller of failure
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async removeFromContractCollection(contractType, id, idField) {
|
|
92
|
+
// Validate input parameters
|
|
93
|
+
if (id === undefined || id === null) {
|
|
94
|
+
throw new Error(`Invalid id provided for removal: ${String(id)}`);
|
|
95
|
+
}
|
|
96
|
+
const collection = await this.getContractCollection(contractType);
|
|
97
|
+
// Build new collection without the specified item
|
|
98
|
+
const filtered = collection.filter((item) => item[idField] !== id);
|
|
99
|
+
try {
|
|
100
|
+
await this.storage.setItem((0, exports.getCollectionStorageKey)(contractType), JSON.stringify(filtered));
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
console.error(`Failed to persist contract collection removal for ${contractType}:`, error);
|
|
104
|
+
throw error; // Rethrow to notify caller of failure
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// The following methods are implemented for compatibility with the new ContractRepository interface
|
|
108
|
+
// but aren't used.
|
|
109
|
+
async getContracts(_) {
|
|
110
|
+
throw new TypeError("Method not implemented, this is a legacy class and should only be used for migrating data.");
|
|
111
|
+
}
|
|
112
|
+
async saveContract(_) {
|
|
113
|
+
throw new TypeError("Method not implemented, this is a legacy class and should only be used for migrating data.");
|
|
114
|
+
}
|
|
115
|
+
async deleteContract(_) {
|
|
116
|
+
throw new TypeError("Method not implemented, this is a legacy class and should only be used for migrating data.");
|
|
117
|
+
}
|
|
118
|
+
// used only for tests
|
|
119
|
+
async clear() {
|
|
120
|
+
await this.storage.clear();
|
|
121
|
+
}
|
|
122
|
+
async [Symbol.asyncDispose]() {
|
|
123
|
+
// deprecated StorageAdapter doesn't have a `close()` method
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.ContractRepositoryImpl = ContractRepositoryImpl;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MIGRATION_KEY = void 0;
|
|
4
|
+
exports.getMigrationStatus = getMigrationStatus;
|
|
5
|
+
exports.requiresMigration = requiresMigration;
|
|
6
|
+
exports.rollbackMigration = rollbackMigration;
|
|
7
|
+
exports.migrateWalletRepository = migrateWalletRepository;
|
|
8
|
+
const walletRepositoryImpl_1 = require("./walletRepositoryImpl");
|
|
9
|
+
const MIGRATION_KEY = (repoType) => `migration-from-storage-adapter-${repoType}`;
|
|
10
|
+
exports.MIGRATION_KEY = MIGRATION_KEY;
|
|
11
|
+
async function getMigrationStatus(repoType, storageAdapter) {
|
|
12
|
+
try {
|
|
13
|
+
const migration = await storageAdapter.getItem((0, exports.MIGRATION_KEY)(repoType));
|
|
14
|
+
if (migration === "done")
|
|
15
|
+
return "done";
|
|
16
|
+
if (migration === "in-progress")
|
|
17
|
+
return "in-progress";
|
|
18
|
+
return "pending";
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
if (e instanceof DOMException && e.name === "NotFoundError")
|
|
22
|
+
return "not-needed";
|
|
23
|
+
throw e;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async function requiresMigration(repoType, storageAdapter) {
|
|
27
|
+
const status = await getMigrationStatus(repoType, storageAdapter);
|
|
28
|
+
return status === "pending" || status === "in-progress";
|
|
29
|
+
}
|
|
30
|
+
async function rollbackMigration(repoType, storageAdapter) {
|
|
31
|
+
await storageAdapter.removeItem((0, exports.MIGRATION_KEY)(repoType));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Migrate wallet data from the legacy storage adapter to the new one.
|
|
35
|
+
* It accepts both onchain and offchain addresses, make sure to pass both.
|
|
36
|
+
*
|
|
37
|
+
* @param storageAdapter
|
|
38
|
+
* @param fresh
|
|
39
|
+
* @param addresses
|
|
40
|
+
*/
|
|
41
|
+
async function migrateWalletRepository(storageAdapter, fresh, addresses) {
|
|
42
|
+
const migrate = await requiresMigration("wallet", storageAdapter);
|
|
43
|
+
if (!migrate)
|
|
44
|
+
return;
|
|
45
|
+
await storageAdapter.setItem((0, exports.MIGRATION_KEY)("wallet"), "in-progress");
|
|
46
|
+
const old = new walletRepositoryImpl_1.WalletRepositoryImpl(storageAdapter);
|
|
47
|
+
const walletData = await old.getWalletState();
|
|
48
|
+
const onchainAddrData = await Promise.all(addresses.onchain.map(async (address) => {
|
|
49
|
+
const utxos = await old.getUtxos(address);
|
|
50
|
+
return { address, utxos };
|
|
51
|
+
}));
|
|
52
|
+
const offchainAddrData = await Promise.all(addresses.offchain.map(async (address) => {
|
|
53
|
+
const vtxos = await old.getVtxos(address);
|
|
54
|
+
const txs = await old.getTransactionHistory(address);
|
|
55
|
+
return { address, vtxos, txs };
|
|
56
|
+
}));
|
|
57
|
+
await Promise.all([
|
|
58
|
+
walletData && fresh.saveWalletState(walletData),
|
|
59
|
+
...offchainAddrData.map((addressData) => Promise.all([
|
|
60
|
+
fresh.saveVtxos(addressData.address, addressData.vtxos),
|
|
61
|
+
fresh.saveTransactions(addressData.address, addressData.txs),
|
|
62
|
+
])),
|
|
63
|
+
...onchainAddrData.map((addressData) => fresh.saveUtxos(addressData.address, addressData.utxos)),
|
|
64
|
+
]);
|
|
65
|
+
await storageAdapter.setItem((0, exports.MIGRATION_KEY)("wallet"), "done");
|
|
66
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WalletRepositoryImpl = void 0;
|
|
4
|
+
const base_1 = require("@scure/base");
|
|
5
|
+
const btc_signer_1 = require("@scure/btc-signer");
|
|
6
|
+
const getVtxosStorageKey = (address) => `vtxos:${address}`;
|
|
7
|
+
const getUtxosStorageKey = (address) => `utxos:${address}`;
|
|
8
|
+
const getTransactionsStorageKey = (address) => `tx:${address}`;
|
|
9
|
+
const walletStateStorageKey = "wallet:state";
|
|
10
|
+
// Utility functions for (de)serializing complex structures
|
|
11
|
+
const serializeVtxo = (v) => ({
|
|
12
|
+
...v,
|
|
13
|
+
tapTree: base_1.hex.encode(v.tapTree),
|
|
14
|
+
forfeitTapLeafScript: serializeTapLeaf(v.forfeitTapLeafScript),
|
|
15
|
+
intentTapLeafScript: serializeTapLeaf(v.intentTapLeafScript),
|
|
16
|
+
extraWitness: v.extraWitness?.map(base_1.hex.encode),
|
|
17
|
+
});
|
|
18
|
+
const serializeUtxo = (u) => ({
|
|
19
|
+
...u,
|
|
20
|
+
tapTree: base_1.hex.encode(u.tapTree),
|
|
21
|
+
forfeitTapLeafScript: serializeTapLeaf(u.forfeitTapLeafScript),
|
|
22
|
+
intentTapLeafScript: serializeTapLeaf(u.intentTapLeafScript),
|
|
23
|
+
extraWitness: u.extraWitness?.map(base_1.hex.encode),
|
|
24
|
+
});
|
|
25
|
+
const deserializeVtxo = (o) => ({
|
|
26
|
+
...o,
|
|
27
|
+
createdAt: new Date(o.createdAt),
|
|
28
|
+
tapTree: base_1.hex.decode(o.tapTree),
|
|
29
|
+
forfeitTapLeafScript: deserializeTapLeaf(o.forfeitTapLeafScript),
|
|
30
|
+
intentTapLeafScript: deserializeTapLeaf(o.intentTapLeafScript),
|
|
31
|
+
extraWitness: o.extraWitness?.map(base_1.hex.decode),
|
|
32
|
+
});
|
|
33
|
+
const deserializeUtxo = (o) => ({
|
|
34
|
+
...o,
|
|
35
|
+
tapTree: base_1.hex.decode(o.tapTree),
|
|
36
|
+
forfeitTapLeafScript: deserializeTapLeaf(o.forfeitTapLeafScript),
|
|
37
|
+
intentTapLeafScript: deserializeTapLeaf(o.intentTapLeafScript),
|
|
38
|
+
extraWitness: o.extraWitness?.map(base_1.hex.decode),
|
|
39
|
+
});
|
|
40
|
+
const serializeTapLeaf = ([cb, s]) => ({
|
|
41
|
+
cb: base_1.hex.encode(btc_signer_1.TaprootControlBlock.encode(cb)),
|
|
42
|
+
s: base_1.hex.encode(s),
|
|
43
|
+
});
|
|
44
|
+
const deserializeTapLeaf = (t) => {
|
|
45
|
+
const cb = btc_signer_1.TaprootControlBlock.decode(base_1.hex.decode(t.cb));
|
|
46
|
+
const s = base_1.hex.decode(t.s);
|
|
47
|
+
return [cb, s];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* @deprecated This is only to be used in migration from storage V1
|
|
51
|
+
*/
|
|
52
|
+
class WalletRepositoryImpl {
|
|
53
|
+
constructor(storage) {
|
|
54
|
+
this.version = 1;
|
|
55
|
+
this.storage = storage;
|
|
56
|
+
}
|
|
57
|
+
async getVtxos(address) {
|
|
58
|
+
const stored = await this.storage.getItem(getVtxosStorageKey(address));
|
|
59
|
+
if (!stored)
|
|
60
|
+
return [];
|
|
61
|
+
try {
|
|
62
|
+
const parsed = JSON.parse(stored);
|
|
63
|
+
return parsed.map(deserializeVtxo);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
console.error(`Failed to parse VTXOs for address ${address}:`, error);
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async saveVtxos(address, vtxos) {
|
|
71
|
+
const storedVtxos = await this.getVtxos(address);
|
|
72
|
+
for (const vtxo of vtxos) {
|
|
73
|
+
const existing = storedVtxos.findIndex((v) => v.txid === vtxo.txid && v.vout === vtxo.vout);
|
|
74
|
+
if (existing !== -1) {
|
|
75
|
+
storedVtxos[existing] = vtxo;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
storedVtxos.push(vtxo);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
await this.storage.setItem(getVtxosStorageKey(address), JSON.stringify(storedVtxos.map(serializeVtxo)));
|
|
82
|
+
}
|
|
83
|
+
async clearVtxos(address) {
|
|
84
|
+
return this.deleteVtxos(address);
|
|
85
|
+
}
|
|
86
|
+
async deleteVtxos(address) {
|
|
87
|
+
await this.storage.removeItem(getVtxosStorageKey(address));
|
|
88
|
+
}
|
|
89
|
+
async getUtxos(address) {
|
|
90
|
+
const stored = await this.storage.getItem(getUtxosStorageKey(address));
|
|
91
|
+
if (!stored)
|
|
92
|
+
return [];
|
|
93
|
+
try {
|
|
94
|
+
const parsed = JSON.parse(stored);
|
|
95
|
+
return parsed.map(deserializeUtxo);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error(`Failed to parse UTXOs for address ${address}:`, error);
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async saveUtxos(address, utxos) {
|
|
103
|
+
const storedUtxos = await this.getUtxos(address);
|
|
104
|
+
utxos.forEach((utxo) => {
|
|
105
|
+
const existing = storedUtxos.findIndex((u) => u.txid === utxo.txid && u.vout === utxo.vout);
|
|
106
|
+
if (existing !== -1) {
|
|
107
|
+
storedUtxos[existing] = utxo;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
storedUtxos.push(utxo);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
await this.storage.setItem(getUtxosStorageKey(address), JSON.stringify(storedUtxos.map(serializeUtxo)));
|
|
114
|
+
}
|
|
115
|
+
async clearUtxos(address) {
|
|
116
|
+
return this.deleteVtxos(address);
|
|
117
|
+
}
|
|
118
|
+
async deleteUtxos(address) {
|
|
119
|
+
await this.storage.removeItem(getUtxosStorageKey(address));
|
|
120
|
+
}
|
|
121
|
+
async getTransactionHistory(address) {
|
|
122
|
+
const storageKey = getTransactionsStorageKey(address);
|
|
123
|
+
const stored = await this.storage.getItem(storageKey);
|
|
124
|
+
if (!stored)
|
|
125
|
+
return [];
|
|
126
|
+
try {
|
|
127
|
+
return JSON.parse(stored);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
console.error(`Failed to parse transactions for address ${address}:`, error);
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async saveTransactions(address, txs) {
|
|
135
|
+
const storedTransactions = await this.getTransactionHistory(address);
|
|
136
|
+
for (const tx of txs) {
|
|
137
|
+
const existing = storedTransactions.findIndex((t) => t.key.boardingTxid === tx.key.boardingTxid &&
|
|
138
|
+
t.key.commitmentTxid === tx.key.commitmentTxid &&
|
|
139
|
+
t.key.arkTxid === tx.key.arkTxid);
|
|
140
|
+
if (existing !== -1) {
|
|
141
|
+
storedTransactions[existing] = tx;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
storedTransactions.push(tx);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
await this.storage.setItem(getTransactionsStorageKey(address), JSON.stringify(storedTransactions));
|
|
148
|
+
}
|
|
149
|
+
async clearTransactions(address) {
|
|
150
|
+
return this.deleteTransactions(address);
|
|
151
|
+
}
|
|
152
|
+
async deleteTransactions(address) {
|
|
153
|
+
await this.storage.removeItem(getTransactionsStorageKey(address));
|
|
154
|
+
}
|
|
155
|
+
async getWalletState() {
|
|
156
|
+
const stored = await this.storage.getItem(walletStateStorageKey);
|
|
157
|
+
if (!stored)
|
|
158
|
+
return null;
|
|
159
|
+
try {
|
|
160
|
+
const state = JSON.parse(stored);
|
|
161
|
+
return state;
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
console.error("Failed to parse wallet state:", error);
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async saveWalletState(state) {
|
|
169
|
+
await this.storage.setItem(walletStateStorageKey, JSON.stringify(state));
|
|
170
|
+
}
|
|
171
|
+
// New method added in V2, not implemented for legacy
|
|
172
|
+
async clear() {
|
|
173
|
+
throw new Error("Method not implemented.");
|
|
174
|
+
}
|
|
175
|
+
async [Symbol.asyncDispose]() {
|
|
176
|
+
// deprecated StorageAdapter doesn't have a `close()` method
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.WalletRepositoryImpl = WalletRepositoryImpl;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RealmContractRepository = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Realm-based implementation of ContractRepository.
|
|
6
|
+
*
|
|
7
|
+
* Consumers must open Realm with the schemas from `./schemas.ts` and pass
|
|
8
|
+
* the instance to the constructor.
|
|
9
|
+
*
|
|
10
|
+
* Realm handles schema creation on open, so `ensureInit()` is a no-op.
|
|
11
|
+
* The consumer owns the Realm lifecycle — `[Symbol.asyncDispose]` is a no-op.
|
|
12
|
+
*/
|
|
13
|
+
class RealmContractRepository {
|
|
14
|
+
constructor(realm) {
|
|
15
|
+
this.realm = realm;
|
|
16
|
+
this.version = 1;
|
|
17
|
+
}
|
|
18
|
+
// ── Lifecycle ──────────────────────────────────────────────────────
|
|
19
|
+
async ensureInit() {
|
|
20
|
+
// Realm handles schema on open — nothing to initialise.
|
|
21
|
+
}
|
|
22
|
+
async [Symbol.asyncDispose]() {
|
|
23
|
+
// no-op — consumer owns the Realm lifecycle
|
|
24
|
+
}
|
|
25
|
+
// ── Clear ──────────────────────────────────────────────────────────
|
|
26
|
+
async clear() {
|
|
27
|
+
await this.ensureInit();
|
|
28
|
+
this.realm.write(() => {
|
|
29
|
+
this.realm.delete(this.realm.objects("ArkContract"));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
// ── Contract management ────────────────────────────────────────────
|
|
33
|
+
async getContracts(filter) {
|
|
34
|
+
await this.ensureInit();
|
|
35
|
+
let results = this.realm.objects("ArkContract");
|
|
36
|
+
if (filter) {
|
|
37
|
+
const filterParts = [];
|
|
38
|
+
const filterArgs = [];
|
|
39
|
+
let argIndex = 0;
|
|
40
|
+
argIndex = this.addFilterCondition(filterParts, filterArgs, "script", filter.script, argIndex);
|
|
41
|
+
argIndex = this.addFilterCondition(filterParts, filterArgs, "state", filter.state, argIndex);
|
|
42
|
+
argIndex = this.addFilterCondition(filterParts, filterArgs, "type", filter.type, argIndex);
|
|
43
|
+
if (filterParts.length > 0) {
|
|
44
|
+
const query = filterParts.join(" AND ");
|
|
45
|
+
results = results.filtered(query, ...filterArgs);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return [...results].map(contractObjectToDomain);
|
|
49
|
+
}
|
|
50
|
+
async saveContract(contract) {
|
|
51
|
+
await this.ensureInit();
|
|
52
|
+
this.realm.write(() => {
|
|
53
|
+
this.realm.create("ArkContract", {
|
|
54
|
+
script: contract.script,
|
|
55
|
+
address: contract.address,
|
|
56
|
+
type: contract.type,
|
|
57
|
+
state: contract.state,
|
|
58
|
+
paramsJson: JSON.stringify(contract.params),
|
|
59
|
+
createdAt: contract.createdAt,
|
|
60
|
+
expiresAt: contract.expiresAt ?? null,
|
|
61
|
+
label: contract.label ?? null,
|
|
62
|
+
metadataJson: contract.metadata
|
|
63
|
+
? JSON.stringify(contract.metadata)
|
|
64
|
+
: null,
|
|
65
|
+
}, "modified");
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async deleteContract(script) {
|
|
69
|
+
await this.ensureInit();
|
|
70
|
+
this.realm.write(() => {
|
|
71
|
+
const toDelete = this.realm
|
|
72
|
+
.objects("ArkContract")
|
|
73
|
+
.filtered("script == $0", script);
|
|
74
|
+
this.realm.delete(toDelete);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
// ── Helpers ─────────────────────────────────────────────────────────
|
|
78
|
+
addFilterCondition(parts, args, column, value, argIndex) {
|
|
79
|
+
if (value === undefined)
|
|
80
|
+
return argIndex;
|
|
81
|
+
if (Array.isArray(value)) {
|
|
82
|
+
if (value.length === 0)
|
|
83
|
+
return argIndex;
|
|
84
|
+
const conditions = value.map((_, i) => {
|
|
85
|
+
return `${column} == $${argIndex + i}`;
|
|
86
|
+
});
|
|
87
|
+
parts.push(`(${conditions.join(" OR ")})`);
|
|
88
|
+
args.push(...value);
|
|
89
|
+
return argIndex + value.length;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
parts.push(`${column} == $${argIndex}`);
|
|
93
|
+
args.push(value);
|
|
94
|
+
return argIndex + 1;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.RealmContractRepository = RealmContractRepository;
|
|
99
|
+
// ── Realm object → Domain converter ──────────────────────────────────────
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
+
function contractObjectToDomain(obj) {
|
|
102
|
+
const contract = {
|
|
103
|
+
script: obj.script,
|
|
104
|
+
address: obj.address,
|
|
105
|
+
type: obj.type,
|
|
106
|
+
state: obj.state,
|
|
107
|
+
params: JSON.parse(obj.paramsJson),
|
|
108
|
+
createdAt: obj.createdAt,
|
|
109
|
+
};
|
|
110
|
+
if (obj.expiresAt !== null && obj.expiresAt !== undefined) {
|
|
111
|
+
contract.expiresAt = obj.expiresAt;
|
|
112
|
+
}
|
|
113
|
+
if (obj.label !== null && obj.label !== undefined) {
|
|
114
|
+
contract.label = obj.label;
|
|
115
|
+
}
|
|
116
|
+
if (obj.metadataJson !== null && obj.metadataJson !== undefined) {
|
|
117
|
+
contract.metadata = JSON.parse(obj.metadataJson);
|
|
118
|
+
}
|
|
119
|
+
return contract;
|
|
120
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArkRealmSchemas = exports.RealmContractRepository = exports.RealmWalletRepository = void 0;
|
|
4
|
+
var walletRepository_1 = require("./walletRepository");
|
|
5
|
+
Object.defineProperty(exports, "RealmWalletRepository", { enumerable: true, get: function () { return walletRepository_1.RealmWalletRepository; } });
|
|
6
|
+
var contractRepository_1 = require("./contractRepository");
|
|
7
|
+
Object.defineProperty(exports, "RealmContractRepository", { enumerable: true, get: function () { return contractRepository_1.RealmContractRepository; } });
|
|
8
|
+
var schemas_1 = require("./schemas");
|
|
9
|
+
Object.defineProperty(exports, "ArkRealmSchemas", { enumerable: true, get: function () { return schemas_1.ArkRealmSchemas; } });
|