@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
|
@@ -4,6 +4,7 @@ exports.buildOffchainTx = buildOffchainTx;
|
|
|
4
4
|
exports.hasBoardingTxExpired = hasBoardingTxExpired;
|
|
5
5
|
exports.verifyTapscriptSignatures = verifyTapscriptSignatures;
|
|
6
6
|
exports.combineTapscriptSigs = combineTapscriptSigs;
|
|
7
|
+
exports.isValidArkAddress = isValidArkAddress;
|
|
7
8
|
const secp256k1_js_1 = require("@noble/curves/secp256k1.js");
|
|
8
9
|
const base_1 = require("@scure/base");
|
|
9
10
|
const btc_signer_1 = require("@scure/btc-signer");
|
|
@@ -13,6 +14,7 @@ const base_2 = require("../script/base");
|
|
|
13
14
|
const anchor_1 = require("./anchor");
|
|
14
15
|
const unknownFields_1 = require("./unknownFields");
|
|
15
16
|
const transaction_1 = require("./transaction");
|
|
17
|
+
const address_1 = require("../script/address");
|
|
16
18
|
/**
|
|
17
19
|
* Builds an offchain transaction with checkpoint transactions.
|
|
18
20
|
*
|
|
@@ -238,3 +240,17 @@ function combineTapscriptSigs(signedTx, originalTx) {
|
|
|
238
240
|
}
|
|
239
241
|
return originalTx;
|
|
240
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Validates if a given string is a valid Ark address by attempting to decode it.
|
|
245
|
+
* @param address The Ark address to validate.
|
|
246
|
+
* @returns True if the address is valid, false otherwise.
|
|
247
|
+
*/
|
|
248
|
+
function isValidArkAddress(address) {
|
|
249
|
+
try {
|
|
250
|
+
address_1.ArkAddress.decode(address);
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
catch (e) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -7,6 +7,46 @@ const txKey = {
|
|
|
7
7
|
boardingTxid: "",
|
|
8
8
|
arkTxid: "",
|
|
9
9
|
};
|
|
10
|
+
function collectAssets(vtxos) {
|
|
11
|
+
const map = new Map();
|
|
12
|
+
for (const vtxo of vtxos) {
|
|
13
|
+
if (vtxo.assets) {
|
|
14
|
+
for (const a of vtxo.assets) {
|
|
15
|
+
map.set(a.assetId, (map.get(a.assetId) ?? 0) + a.amount);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (map.size === 0)
|
|
20
|
+
return undefined;
|
|
21
|
+
return Array.from(map, ([assetId, amount]) => ({ assetId, amount }));
|
|
22
|
+
}
|
|
23
|
+
function subtractAssets(spent, change) {
|
|
24
|
+
const map = new Map();
|
|
25
|
+
for (const vtxo of change) {
|
|
26
|
+
if (vtxo.assets) {
|
|
27
|
+
for (const a of vtxo.assets) {
|
|
28
|
+
map.set(a.assetId, (map.get(a.assetId) ?? 0) + a.amount);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
for (const vtxo of spent) {
|
|
33
|
+
if (vtxo.assets) {
|
|
34
|
+
for (const a of vtxo.assets) {
|
|
35
|
+
const current = map.get(a.assetId) ?? 0;
|
|
36
|
+
const remaining = current - a.amount;
|
|
37
|
+
if (remaining !== 0) {
|
|
38
|
+
map.set(a.assetId, remaining);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
map.delete(a.assetId);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (map.size === 0)
|
|
47
|
+
return undefined;
|
|
48
|
+
return Array.from(map, ([assetId, amount]) => ({ assetId, amount }));
|
|
49
|
+
}
|
|
10
50
|
/**
|
|
11
51
|
* Builds the transaction history by analyzing virtual coins (VTXOs), boarding transactions, and ignored commitments.
|
|
12
52
|
* History is sorted from newest to oldest and is composed only of SENT and RECEIVED transactions.
|
|
@@ -26,6 +66,7 @@ async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnor
|
|
|
26
66
|
// it's translated into a received batch transaction
|
|
27
67
|
if (!commitmentsToIgnore.has(vtxo.virtualStatus.commitmentTxIds[0]) &&
|
|
28
68
|
fromOldestVtxo.filter((v) => v.settledBy === vtxo.virtualStatus.commitmentTxIds[0]).length === 0) {
|
|
69
|
+
const assets = collectAssets([vtxo]);
|
|
29
70
|
received.push({
|
|
30
71
|
key: {
|
|
31
72
|
...txKey,
|
|
@@ -36,12 +77,14 @@ async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnor
|
|
|
36
77
|
amount: vtxo.value,
|
|
37
78
|
settled: vtxo.status.isLeaf || vtxo.isSpent,
|
|
38
79
|
createdAt: vtxo.createdAt.getTime(),
|
|
80
|
+
...(assets && { assets }),
|
|
39
81
|
});
|
|
40
82
|
}
|
|
41
83
|
}
|
|
42
84
|
else if (fromOldestVtxo.filter((v) => v.arkTxId === vtxo.txid).length === 0) {
|
|
43
85
|
// If this vtxo is preconfirmed and does not spend any other vtxos,
|
|
44
86
|
// it's translated into a received offchain transaction
|
|
87
|
+
const assets = collectAssets([vtxo]);
|
|
45
88
|
received.push({
|
|
46
89
|
key: { ...txKey, arkTxid: vtxo.txid },
|
|
47
90
|
tag: "offchain",
|
|
@@ -49,6 +92,7 @@ async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnor
|
|
|
49
92
|
amount: vtxo.value,
|
|
50
93
|
settled: vtxo.status.isLeaf || vtxo.isSpent,
|
|
51
94
|
createdAt: vtxo.createdAt.getTime(),
|
|
95
|
+
...(assets && { assets }),
|
|
52
96
|
});
|
|
53
97
|
}
|
|
54
98
|
// If the vtxo is spent, it's translated into a sent transaction unless:
|
|
@@ -77,6 +121,7 @@ async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnor
|
|
|
77
121
|
? await getTxCreatedAt(vtxo.arkTxId)
|
|
78
122
|
: vtxo.createdAt.getTime() + 1;
|
|
79
123
|
}
|
|
124
|
+
const assets = subtractAssets(allSpent, changes);
|
|
80
125
|
sent.push({
|
|
81
126
|
key: { ...txKey, arkTxid: vtxo.arkTxId },
|
|
82
127
|
tag: "offchain",
|
|
@@ -84,6 +129,7 @@ async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnor
|
|
|
84
129
|
amount: txAmount,
|
|
85
130
|
settled: true,
|
|
86
131
|
createdAt: txTime,
|
|
132
|
+
...(assets && { assets }),
|
|
87
133
|
});
|
|
88
134
|
}
|
|
89
135
|
// If the vtxo is forfeited in a batch and the total sum of forfeited vtxos is bigger than the sum of new vtxos,
|
|
@@ -100,6 +146,7 @@ async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnor
|
|
|
100
146
|
// forfeitAmount > settledAmount --> collaborative exit with offchain change
|
|
101
147
|
// TODO: make this support fees!
|
|
102
148
|
if (forfeitAmount > settledAmount) {
|
|
149
|
+
const assets = subtractAssets(forfeitVtxos, changes);
|
|
103
150
|
sent.push({
|
|
104
151
|
key: { ...txKey, commitmentTxid: vtxo.settledBy },
|
|
105
152
|
tag: "exit",
|
|
@@ -107,11 +154,13 @@ async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnor
|
|
|
107
154
|
amount: forfeitAmount - settledAmount,
|
|
108
155
|
settled: true,
|
|
109
156
|
createdAt: changes[0].createdAt.getTime(),
|
|
157
|
+
...(assets && { assets }),
|
|
110
158
|
});
|
|
111
159
|
}
|
|
112
160
|
}
|
|
113
161
|
else {
|
|
114
162
|
// forfeitAmount > 0 && settledAmount == 0 --> collaborative exit without any offchain change
|
|
163
|
+
const assets = subtractAssets(forfeitVtxos, []);
|
|
115
164
|
sent.push({
|
|
116
165
|
key: { ...txKey, commitmentTxid: vtxo.settledBy },
|
|
117
166
|
tag: "exit",
|
|
@@ -120,6 +169,7 @@ async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnor
|
|
|
120
169
|
settled: true,
|
|
121
170
|
// TODO: fetch commitment tx with /v1/indexer/commitmentTx/<commitmentTxid> to know when the tx was made
|
|
122
171
|
createdAt: vtxo.createdAt.getTime() + 1,
|
|
172
|
+
...(assets && { assets }),
|
|
123
173
|
});
|
|
124
174
|
}
|
|
125
175
|
}
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AssetManager = exports.ReadonlyAssetManager = void 0;
|
|
4
|
+
const asset_1 = require("../asset");
|
|
5
|
+
const address_1 = require("../script/address");
|
|
6
|
+
const asset_2 = require("./asset");
|
|
7
|
+
const wallet_1 = require("./wallet");
|
|
8
|
+
class ReadonlyAssetManager {
|
|
9
|
+
constructor(indexer) {
|
|
10
|
+
this.indexer = indexer;
|
|
11
|
+
}
|
|
12
|
+
async getAssetDetails(assetId) {
|
|
13
|
+
// TODO: rely on db, and fallback to indexer if not found
|
|
14
|
+
return this.indexer.getAssetDetails(assetId);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.ReadonlyAssetManager = ReadonlyAssetManager;
|
|
18
|
+
class AssetManager extends ReadonlyAssetManager {
|
|
19
|
+
constructor(wallet) {
|
|
20
|
+
super(wallet.indexerProvider);
|
|
21
|
+
this.wallet = wallet;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Issue a new asset.
|
|
25
|
+
* @param params - Parameters for asset issuance
|
|
26
|
+
* @param params.amount - Amount of asset units to issue
|
|
27
|
+
* @param params.controlAsset - Optional control asset (for reissuable assets)
|
|
28
|
+
* @param params.metadata - Optional metadata to attach to the asset
|
|
29
|
+
* @returns Promise resolving to the ark transaction ID and asset ID
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // Issue a simple non-reissuable asset
|
|
34
|
+
* const result = await wallet.issueAsset({ amount: 1000 });
|
|
35
|
+
* console.log('Asset ID:', result.assetId);
|
|
36
|
+
*
|
|
37
|
+
* // Issue a reissuable asset with a new control asset
|
|
38
|
+
* const result = await wallet.issueAsset({
|
|
39
|
+
* amount: 1000,
|
|
40
|
+
* controlAsset: 1 // creates new control asset with amount 1
|
|
41
|
+
* });
|
|
42
|
+
* console.log('Control Asset ID:', result.controlAssetId);
|
|
43
|
+
* console.log('Asset ID:', result.assetId);
|
|
44
|
+
*
|
|
45
|
+
* // Issue a reissuable asset with an existing control asset
|
|
46
|
+
* const result = await wallet.issueAsset({
|
|
47
|
+
* amount: 1000,
|
|
48
|
+
* controlAsset: 'controlAssetId'
|
|
49
|
+
* });
|
|
50
|
+
* console.log('Control Asset ID:', result.controlAssetId);
|
|
51
|
+
* console.log('Asset ID:', result.assetId);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
async issue(params) {
|
|
55
|
+
if (params.amount <= 0) {
|
|
56
|
+
throw new Error(`Issue amount must be greater than 0, got ${params.amount}`);
|
|
57
|
+
}
|
|
58
|
+
const metadata = castMetadata(params.metadata);
|
|
59
|
+
const virtualCoins = await this.wallet.getVtxos({
|
|
60
|
+
withRecoverable: false,
|
|
61
|
+
});
|
|
62
|
+
const controlAssetRef = params.controlAssetId
|
|
63
|
+
? asset_1.AssetRef.fromId(asset_1.AssetId.fromString(params.controlAssetId))
|
|
64
|
+
: null;
|
|
65
|
+
const coinSelection = (0, wallet_1.selectVirtualCoins)(virtualCoins, Number(this.wallet.dustAmount));
|
|
66
|
+
let totalBtcSelected = 0n;
|
|
67
|
+
// keep track of asset changes
|
|
68
|
+
const assetChanges = new Map();
|
|
69
|
+
for (const coin of coinSelection.inputs) {
|
|
70
|
+
totalBtcSelected += BigInt(coin.value);
|
|
71
|
+
if (!coin.assets)
|
|
72
|
+
continue;
|
|
73
|
+
for (const { assetId, amount } of coin.assets) {
|
|
74
|
+
const existing = assetChanges.get(assetId) ?? 0n;
|
|
75
|
+
assetChanges.set(assetId, existing + BigInt(amount));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const groups = [];
|
|
79
|
+
// issued asset group
|
|
80
|
+
const issuedAssetOutput = asset_1.AssetOutput.create(0, BigInt(params.amount));
|
|
81
|
+
const issuedAssetGroup = asset_1.AssetGroup.create(null, controlAssetRef, [], [issuedAssetOutput], metadata);
|
|
82
|
+
groups.push(issuedAssetGroup);
|
|
83
|
+
// add asset groups for each asset change
|
|
84
|
+
if (assetChanges.size > 0) {
|
|
85
|
+
const assetInputs = (0, asset_2.selectedCoinsToAssetInputs)(coinSelection.inputs);
|
|
86
|
+
for (const [assetId, amount] of assetChanges) {
|
|
87
|
+
const changeInputs = [];
|
|
88
|
+
// collect all inputs for the asset change
|
|
89
|
+
for (const [inputIndex, assets] of assetInputs) {
|
|
90
|
+
for (const asset of assets) {
|
|
91
|
+
if (asset.assetId !== assetId)
|
|
92
|
+
continue;
|
|
93
|
+
changeInputs.push(asset_1.AssetInput.create(inputIndex, BigInt(asset.amount)));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// add the change asset group
|
|
97
|
+
groups.push(asset_1.AssetGroup.create(asset_1.AssetId.fromString(assetId), null, changeInputs, [asset_1.AssetOutput.create(0, amount)], []));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// build transaction outputs
|
|
101
|
+
const address = await this.wallet.getAddress();
|
|
102
|
+
const outputAddress = address_1.ArkAddress.decode(address);
|
|
103
|
+
const outputs = [
|
|
104
|
+
{
|
|
105
|
+
script: outputAddress.pkScript,
|
|
106
|
+
amount: BigInt(totalBtcSelected),
|
|
107
|
+
},
|
|
108
|
+
asset_1.Packet.create(groups).txOut(),
|
|
109
|
+
];
|
|
110
|
+
const { arkTxid } = await this.wallet.buildAndSubmitOffchainTx(coinSelection.inputs, outputs);
|
|
111
|
+
return {
|
|
112
|
+
arkTxId: arkTxid,
|
|
113
|
+
assetId: asset_1.AssetId.create(arkTxid, 0).toString(),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Reissue more units of an existing asset.
|
|
118
|
+
* Requires ownership of the control asset.
|
|
119
|
+
*
|
|
120
|
+
* @param params - Parameters for asset reissuance
|
|
121
|
+
* @param params.assetId - The asset ID to reissue (control asset ID is resolved via getAssetDetails)
|
|
122
|
+
* @param params.amount - Amount of additional units to issue
|
|
123
|
+
* @returns Promise resolving to the ark transaction ID
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const txid = await wallet.assetManager.reissue({
|
|
128
|
+
* assetId: 'def456...',
|
|
129
|
+
* amount: 500
|
|
130
|
+
* });
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
async reissue(params) {
|
|
134
|
+
if (params.amount <= 0) {
|
|
135
|
+
throw new Error(`Reissuance amount must be greater than 0, got ${params.amount}`);
|
|
136
|
+
}
|
|
137
|
+
const { controlAssetId } = await this.getAssetDetails(params.assetId);
|
|
138
|
+
if (!controlAssetId) {
|
|
139
|
+
throw new Error(`Asset ${params.assetId} is not reissuable`);
|
|
140
|
+
}
|
|
141
|
+
const virtualCoins = await this.wallet.getVtxos({
|
|
142
|
+
withRecoverable: false,
|
|
143
|
+
});
|
|
144
|
+
const assetChanges = new Map();
|
|
145
|
+
// select control asset inputs
|
|
146
|
+
const { selected: controlCoins } = (0, asset_2.selectCoinsWithAsset)(virtualCoins, controlAssetId, 1n);
|
|
147
|
+
let selectedCoins = [...controlCoins];
|
|
148
|
+
let assetToReissueAmount = 0n;
|
|
149
|
+
// all control inputs assets go to asset changes, including the control asset itself
|
|
150
|
+
for (const coin of controlCoins) {
|
|
151
|
+
if (!coin.assets)
|
|
152
|
+
continue;
|
|
153
|
+
for (const { assetId, amount } of coin.assets) {
|
|
154
|
+
if (assetId === params.assetId) {
|
|
155
|
+
assetToReissueAmount += BigInt(amount);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const existing = assetChanges.get(assetId) ?? 0n;
|
|
159
|
+
assetChanges.set(assetId, existing + BigInt(amount));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// select at least dust amount
|
|
163
|
+
const minBtcNeeded = Number(this.wallet.dustAmount);
|
|
164
|
+
let totalBtcSelected = selectedCoins.reduce((sum, c) => sum + c.value, 0);
|
|
165
|
+
if (totalBtcSelected < minBtcNeeded) {
|
|
166
|
+
const remainingCoins = virtualCoins.filter((c) => !selectedCoins.find((sc) => sc.txid === c.txid && sc.vout === c.vout));
|
|
167
|
+
const additional = (0, wallet_1.selectVirtualCoins)(remainingCoins, minBtcNeeded - totalBtcSelected);
|
|
168
|
+
for (const coin of additional.inputs) {
|
|
169
|
+
// additional inputs assets go to asset changes
|
|
170
|
+
if (!coin.assets)
|
|
171
|
+
continue;
|
|
172
|
+
for (const { assetId, amount } of coin.assets) {
|
|
173
|
+
if (assetId === params.assetId) {
|
|
174
|
+
assetToReissueAmount += BigInt(amount);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const existing = assetChanges.get(assetId) ?? 0n;
|
|
178
|
+
assetChanges.set(assetId, existing + BigInt(amount));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
selectedCoins = [...selectedCoins, ...additional.inputs];
|
|
182
|
+
totalBtcSelected += additional.inputs.reduce((sum, c) => sum + c.value, 0);
|
|
183
|
+
}
|
|
184
|
+
const assetInputs = (0, asset_2.selectedCoinsToAssetInputs)(selectedCoins);
|
|
185
|
+
// collect all inputs for the asset to reissue
|
|
186
|
+
const reissueInputs = [];
|
|
187
|
+
for (const [inputIndex, assets] of assetInputs) {
|
|
188
|
+
for (const asset of assets) {
|
|
189
|
+
if (asset.assetId !== params.assetId)
|
|
190
|
+
continue;
|
|
191
|
+
reissueInputs.push(asset_1.AssetInput.create(inputIndex, BigInt(asset.amount)));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// the total output amount of the asset to reissue = new + (optional) selected amount
|
|
195
|
+
const totalAssetAmount = assetToReissueAmount + BigInt(params.amount);
|
|
196
|
+
const reissueAssetIdObj = asset_1.AssetId.fromString(params.assetId);
|
|
197
|
+
// create the reissuance asset group
|
|
198
|
+
const reissueAssetGroup = asset_1.AssetGroup.create(reissueAssetIdObj, null, reissueInputs, [asset_1.AssetOutput.create(0, totalAssetAmount)], []);
|
|
199
|
+
const groups = [reissueAssetGroup];
|
|
200
|
+
// for each asset change, create a new asset group
|
|
201
|
+
for (const [assetId, amount] of assetChanges) {
|
|
202
|
+
const changeInputs = [];
|
|
203
|
+
for (const [inputIndex, assets] of assetInputs) {
|
|
204
|
+
for (const asset of assets) {
|
|
205
|
+
if (asset.assetId !== assetId)
|
|
206
|
+
continue;
|
|
207
|
+
changeInputs.push(asset_1.AssetInput.create(inputIndex, BigInt(asset.amount)));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
groups.push(asset_1.AssetGroup.create(asset_1.AssetId.fromString(assetId), null, changeInputs, [asset_1.AssetOutput.create(0, amount)], []));
|
|
211
|
+
}
|
|
212
|
+
// build transaction outputs
|
|
213
|
+
const address = await this.wallet.getAddress();
|
|
214
|
+
const outputAddress = address_1.ArkAddress.decode(address);
|
|
215
|
+
const outputs = [
|
|
216
|
+
{
|
|
217
|
+
script: outputAddress.pkScript,
|
|
218
|
+
amount: BigInt(totalBtcSelected),
|
|
219
|
+
},
|
|
220
|
+
asset_1.Packet.create(groups).txOut(),
|
|
221
|
+
];
|
|
222
|
+
const { arkTxid } = await this.wallet.buildAndSubmitOffchainTx(selectedCoins, outputs);
|
|
223
|
+
return arkTxid;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Burn assets.
|
|
227
|
+
* @param params - Parameters for burning
|
|
228
|
+
* @param params.assetId - The asset ID to burn
|
|
229
|
+
* @param params.amount - Amount of units to burn
|
|
230
|
+
* @returns Promise resolving to the ark transaction ID
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```typescript
|
|
234
|
+
* const txid = await wallet.assetManager.burn({
|
|
235
|
+
* assetId: 'abc123...',
|
|
236
|
+
* amount: 100
|
|
237
|
+
* });
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
async burn(params) {
|
|
241
|
+
if (params.amount <= 0) {
|
|
242
|
+
throw new Error(`Burn amount must be greater than 0, got ${params.amount}`);
|
|
243
|
+
}
|
|
244
|
+
const virtualCoins = await this.wallet.getVtxos({
|
|
245
|
+
withRecoverable: false,
|
|
246
|
+
});
|
|
247
|
+
const assetChanges = new Map();
|
|
248
|
+
// select vtxos with the asset to burn
|
|
249
|
+
const { selected: assetCoins } = (0, asset_2.selectCoinsWithAsset)(virtualCoins, params.assetId, BigInt(params.amount));
|
|
250
|
+
const selectedCoins = [...assetCoins];
|
|
251
|
+
let totalBtcSelected = 0;
|
|
252
|
+
// add the selected coins to asset changes, including the asset to burn
|
|
253
|
+
for (const coin of assetCoins) {
|
|
254
|
+
totalBtcSelected += coin.value;
|
|
255
|
+
if (!coin.assets)
|
|
256
|
+
continue;
|
|
257
|
+
for (const { assetId, amount } of coin.assets) {
|
|
258
|
+
const existing = assetChanges.get(assetId) ?? 0n;
|
|
259
|
+
assetChanges.set(assetId, existing + BigInt(amount));
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// subtract the amount to burn from the asset change
|
|
263
|
+
assetChanges.set(params.assetId, (assetChanges.get(params.assetId) ?? 0n) - BigInt(params.amount));
|
|
264
|
+
const minBtcNeeded = Number(this.wallet.dustAmount);
|
|
265
|
+
// we need to ensure at least dust amount is selected
|
|
266
|
+
// if not, select additional coins
|
|
267
|
+
if (totalBtcSelected < minBtcNeeded) {
|
|
268
|
+
const remainingCoins = virtualCoins.filter((c) => !selectedCoins.find((sc) => sc.txid === c.txid && sc.vout === c.vout));
|
|
269
|
+
const additional = (0, wallet_1.selectVirtualCoins)(remainingCoins, minBtcNeeded - totalBtcSelected);
|
|
270
|
+
// additional inputs assets go to asset changes
|
|
271
|
+
for (const coin of additional.inputs) {
|
|
272
|
+
totalBtcSelected += coin.value;
|
|
273
|
+
if (!coin.assets)
|
|
274
|
+
continue;
|
|
275
|
+
for (const { assetId, amount } of coin.assets) {
|
|
276
|
+
const existing = assetChanges.get(assetId) ?? 0n;
|
|
277
|
+
assetChanges.set(assetId, existing + BigInt(amount));
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
selectedCoins.push(...additional.inputs);
|
|
281
|
+
}
|
|
282
|
+
const groups = [];
|
|
283
|
+
const assetInputs = (0, asset_2.selectedCoinsToAssetInputs)(selectedCoins);
|
|
284
|
+
// for each asset, create a new asset group
|
|
285
|
+
for (const [assetId, amount] of assetChanges) {
|
|
286
|
+
const changeInputs = [];
|
|
287
|
+
for (const [inputIndex, assets] of assetInputs) {
|
|
288
|
+
for (const asset of assets) {
|
|
289
|
+
if (asset.assetId !== assetId)
|
|
290
|
+
continue;
|
|
291
|
+
changeInputs.push(asset_1.AssetInput.create(inputIndex, BigInt(asset.amount)));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
groups.push(asset_1.AssetGroup.create(asset_1.AssetId.fromString(assetId), null, changeInputs, amount > 0n ? [asset_1.AssetOutput.create(0, amount)] : [], []));
|
|
295
|
+
}
|
|
296
|
+
// build transaction outputs
|
|
297
|
+
const address = await this.wallet.getAddress();
|
|
298
|
+
const outputAddress = address_1.ArkAddress.decode(address);
|
|
299
|
+
const outputs = [
|
|
300
|
+
{
|
|
301
|
+
script: outputAddress.pkScript,
|
|
302
|
+
amount: BigInt(totalBtcSelected),
|
|
303
|
+
},
|
|
304
|
+
asset_1.Packet.create(groups).txOut(),
|
|
305
|
+
];
|
|
306
|
+
const { arkTxid } = await this.wallet.buildAndSubmitOffchainTx(selectedCoins, outputs);
|
|
307
|
+
return arkTxid;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
exports.AssetManager = AssetManager;
|
|
311
|
+
function castMetadata(metadata) {
|
|
312
|
+
if (!metadata) {
|
|
313
|
+
return [];
|
|
314
|
+
}
|
|
315
|
+
const md = [];
|
|
316
|
+
const textEncoder = new TextEncoder();
|
|
317
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
318
|
+
// convert value to bytes
|
|
319
|
+
let valueBytes;
|
|
320
|
+
if (typeof value === "string") {
|
|
321
|
+
valueBytes = textEncoder.encode(value);
|
|
322
|
+
}
|
|
323
|
+
else if (typeof value === "number") {
|
|
324
|
+
valueBytes = textEncoder.encode(String(value));
|
|
325
|
+
}
|
|
326
|
+
else if (value instanceof Uint8Array) {
|
|
327
|
+
valueBytes = value;
|
|
328
|
+
}
|
|
329
|
+
else if (value instanceof ArrayBuffer) {
|
|
330
|
+
valueBytes = new Uint8Array(value);
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
throw new Error("Invalid metadata value type");
|
|
334
|
+
}
|
|
335
|
+
md.push(asset_1.Metadata.create(textEncoder.encode(key), valueBytes));
|
|
336
|
+
}
|
|
337
|
+
return md;
|
|
338
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAssetPacket = createAssetPacket;
|
|
4
|
+
exports.selectCoinsWithAsset = selectCoinsWithAsset;
|
|
5
|
+
exports.computeAssetChange = computeAssetChange;
|
|
6
|
+
exports.selectedCoinsToAssetInputs = selectedCoinsToAssetInputs;
|
|
7
|
+
const asset_1 = require("../asset");
|
|
8
|
+
/**
|
|
9
|
+
* Creates an asset packet from asset inputs and receivers.
|
|
10
|
+
* Groups inputs and outputs by asset ID and creates the Packet object
|
|
11
|
+
* @param assetInputs - map input index -> assets
|
|
12
|
+
* @param receivers - array of recipients with their asset allocations
|
|
13
|
+
* @param changeReceiver - (optional) change receiver containing remaining assets
|
|
14
|
+
* @returns packet containing all asset groups
|
|
15
|
+
*/
|
|
16
|
+
function createAssetPacket(assetInputs, receivers, changeReceiver) {
|
|
17
|
+
// map inputs by asset id
|
|
18
|
+
const inputsByAssetId = new Map();
|
|
19
|
+
for (const [inputIndex, assets] of assetInputs) {
|
|
20
|
+
for (const asset of assets) {
|
|
21
|
+
const existing = inputsByAssetId.get(asset.assetId);
|
|
22
|
+
inputsByAssetId.set(asset.assetId, [
|
|
23
|
+
...(existing ?? []),
|
|
24
|
+
asset_1.AssetInput.create(inputIndex, BigInt(asset.amount)),
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// map outputs by asset id
|
|
29
|
+
const outputsByAssetId = new Map();
|
|
30
|
+
// track tx output index
|
|
31
|
+
let outputIndex = 0;
|
|
32
|
+
for (const receiver of receivers) {
|
|
33
|
+
if (receiver.assets) {
|
|
34
|
+
for (const asset of receiver.assets) {
|
|
35
|
+
const existing = outputsByAssetId.get(asset.assetId);
|
|
36
|
+
outputsByAssetId.set(asset.assetId, [
|
|
37
|
+
...(existing ?? []),
|
|
38
|
+
asset_1.AssetOutput.create(outputIndex, BigInt(asset.amount)),
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
outputIndex++;
|
|
43
|
+
}
|
|
44
|
+
// add change receiver assets if present
|
|
45
|
+
if (changeReceiver?.assets) {
|
|
46
|
+
for (const asset of changeReceiver.assets) {
|
|
47
|
+
const existing = outputsByAssetId.get(asset.assetId);
|
|
48
|
+
outputsByAssetId.set(asset.assetId, [
|
|
49
|
+
...(existing ?? []),
|
|
50
|
+
asset_1.AssetOutput.create(outputIndex, BigInt(asset.amount)),
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const groups = [];
|
|
55
|
+
// get all unique asset ids from both inputs and outputs
|
|
56
|
+
const allAssetIds = new Set([
|
|
57
|
+
...inputsByAssetId.keys(),
|
|
58
|
+
...outputsByAssetId.keys(),
|
|
59
|
+
]);
|
|
60
|
+
for (const assetIdStr of allAssetIds) {
|
|
61
|
+
const inputs = inputsByAssetId.get(assetIdStr);
|
|
62
|
+
const outputs = outputsByAssetId.get(assetIdStr);
|
|
63
|
+
const assetId = asset_1.AssetId.fromString(assetIdStr);
|
|
64
|
+
const group = asset_1.AssetGroup.create(assetId, null, inputs ?? [], outputs ?? [], []);
|
|
65
|
+
groups.push(group);
|
|
66
|
+
}
|
|
67
|
+
return asset_1.Packet.create(groups);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Selects coins that contain a specific asset.
|
|
71
|
+
* Returns coins sorted by amount (smallest first for better coin selection).
|
|
72
|
+
*/
|
|
73
|
+
function selectCoinsWithAsset(coins, assetId, requiredAmount) {
|
|
74
|
+
// filter only coins that have the specified asset
|
|
75
|
+
const coinsWithAsset = coins.filter((coin) => coin.assets?.some((a) => a.assetId === assetId));
|
|
76
|
+
// sort by asset amount (smallest first for better selection)
|
|
77
|
+
coinsWithAsset.sort((a, b) => {
|
|
78
|
+
const amountA = a.assets?.find((asset) => asset.assetId === assetId)?.amount ?? 0;
|
|
79
|
+
const amountB = b.assets?.find((asset) => asset.assetId === assetId)?.amount ?? 0;
|
|
80
|
+
return amountA - amountB;
|
|
81
|
+
});
|
|
82
|
+
const selected = [];
|
|
83
|
+
let totalAssetAmount = 0n;
|
|
84
|
+
for (const coin of coinsWithAsset) {
|
|
85
|
+
if (totalAssetAmount >= requiredAmount)
|
|
86
|
+
break;
|
|
87
|
+
selected.push(coin);
|
|
88
|
+
const assetAmount = coin.assets?.find((a) => a.assetId === assetId)?.amount ?? 0;
|
|
89
|
+
totalAssetAmount += BigInt(assetAmount);
|
|
90
|
+
}
|
|
91
|
+
if (totalAssetAmount < requiredAmount) {
|
|
92
|
+
throw new Error(`Insufficient asset balance: have ${totalAssetAmount}, need ${requiredAmount}`);
|
|
93
|
+
}
|
|
94
|
+
return { selected, totalAssetAmount };
|
|
95
|
+
}
|
|
96
|
+
function computeAssetChange(inputAssets, outputAssets) {
|
|
97
|
+
const change = new Map();
|
|
98
|
+
for (const [assetId, inputAmount] of inputAssets) {
|
|
99
|
+
const outputAmount = outputAssets.get(assetId) ?? 0n;
|
|
100
|
+
const changeAmount = inputAmount - outputAmount;
|
|
101
|
+
if (changeAmount > 0n) {
|
|
102
|
+
change.set(assetId, changeAmount);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return change;
|
|
106
|
+
}
|
|
107
|
+
function selectedCoinsToAssetInputs(selectedCoins) {
|
|
108
|
+
const assetInputs = new Map();
|
|
109
|
+
for (let inputIndex = 0; inputIndex < selectedCoins.length; inputIndex++) {
|
|
110
|
+
const coin = selectedCoins[inputIndex];
|
|
111
|
+
if (!coin.assets || coin.assets.length === 0) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
assetInputs.set(inputIndex, coin.assets);
|
|
115
|
+
}
|
|
116
|
+
return assetInputs;
|
|
117
|
+
}
|
package/dist/cjs/wallet/batch.js
CHANGED
|
@@ -11,7 +11,7 @@ const base_1 = require("@scure/base");
|
|
|
11
11
|
* @example
|
|
12
12
|
* ```typescript
|
|
13
13
|
* // use wallet handler or create a custom one
|
|
14
|
-
* const handler = wallet.createBatchHandler(intentId, inputs, musig2session);
|
|
14
|
+
* const handler = wallet.createBatchHandler(intentId, inputs, expectedRecipients, musig2session);
|
|
15
15
|
*
|
|
16
16
|
* const abortController = new AbortController();
|
|
17
17
|
* // Get event stream from Ark provider
|