@arkade-os/sdk 0.3.6 → 0.3.7
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.
|
@@ -14,10 +14,6 @@ function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
14
14
|
// All vtxos are received unless:
|
|
15
15
|
// - they resulted from a settlement (either boarding or refresh)
|
|
16
16
|
// - they are the change of a spend tx
|
|
17
|
-
// - they were spent in a payment (have arkTxId set)
|
|
18
|
-
// - they resulted from a payment (their txid matches an arkTxId of a spent vtxo)
|
|
19
|
-
// First, collect all arkTxIds from spent vtxos to identify payment transactions
|
|
20
|
-
const paymentArkTxIds = new Set(spent.filter((v) => v.arkTxId).map((v) => v.arkTxId));
|
|
21
17
|
let vtxosLeftToCheck = [...spent];
|
|
22
18
|
for (const vtxo of [...spendable, ...spent]) {
|
|
23
19
|
if (vtxo.virtualStatus.state !== "preconfirmed" &&
|
|
@@ -25,16 +21,6 @@ function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
25
21
|
vtxo.virtualStatus.commitmentTxIds.some((txid) => boardingBatchTxids.has(txid))) {
|
|
26
22
|
continue;
|
|
27
23
|
}
|
|
28
|
-
// Skip vtxos that were spent in a payment transaction
|
|
29
|
-
// These will be handled in the sent transaction section below
|
|
30
|
-
if (vtxo.arkTxId) {
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
// Skip vtxos that resulted from a payment transaction
|
|
34
|
-
// (their txid matches an arkTxId from a spent vtxo)
|
|
35
|
-
if (paymentArkTxIds.has(vtxo.txid)) {
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
24
|
const settleVtxos = findVtxosSpentInSettlement(vtxosLeftToCheck, vtxo);
|
|
39
25
|
vtxosLeftToCheck = removeVtxosFromList(vtxosLeftToCheck, settleVtxos);
|
|
40
26
|
const settleAmount = reduceVtxosAmount(settleVtxos);
|
|
@@ -48,7 +34,7 @@ function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
48
34
|
continue; // settlement or change, ignore
|
|
49
35
|
}
|
|
50
36
|
const txKey = {
|
|
51
|
-
commitmentTxid:
|
|
37
|
+
commitmentTxid: "",
|
|
52
38
|
boardingTxid: "",
|
|
53
39
|
arkTxid: "",
|
|
54
40
|
};
|
|
@@ -59,6 +45,10 @@ function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
59
45
|
settled = true;
|
|
60
46
|
}
|
|
61
47
|
}
|
|
48
|
+
else {
|
|
49
|
+
txKey.commitmentTxid =
|
|
50
|
+
vtxo.virtualStatus.commitmentTxIds?.[0] || "";
|
|
51
|
+
}
|
|
62
52
|
txs.push({
|
|
63
53
|
key: txKey,
|
|
64
54
|
amount: vtxo.value - settleAmount - spentAmount,
|
|
@@ -70,17 +60,21 @@ function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
70
60
|
// vtxos by settled by or ark txid
|
|
71
61
|
const vtxosByTxid = new Map();
|
|
72
62
|
for (const v of spent) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
63
|
+
if (v.settledBy) {
|
|
64
|
+
if (!vtxosByTxid.has(v.settledBy)) {
|
|
65
|
+
vtxosByTxid.set(v.settledBy, []);
|
|
66
|
+
}
|
|
67
|
+
const currentVtxos = vtxosByTxid.get(v.settledBy);
|
|
68
|
+
vtxosByTxid.set(v.settledBy, [...currentVtxos, v]);
|
|
69
|
+
}
|
|
70
|
+
if (!v.arkTxId) {
|
|
77
71
|
continue;
|
|
78
72
|
}
|
|
79
|
-
if (!vtxosByTxid.has(
|
|
80
|
-
vtxosByTxid.set(
|
|
73
|
+
if (!vtxosByTxid.has(v.arkTxId)) {
|
|
74
|
+
vtxosByTxid.set(v.arkTxId, []);
|
|
81
75
|
}
|
|
82
|
-
const currentVtxos = vtxosByTxid.get(
|
|
83
|
-
vtxosByTxid.set(
|
|
76
|
+
const currentVtxos = vtxosByTxid.get(v.arkTxId);
|
|
77
|
+
vtxosByTxid.set(v.arkTxId, [...currentVtxos, v]);
|
|
84
78
|
}
|
|
85
79
|
for (const [sb, vtxos] of vtxosByTxid) {
|
|
86
80
|
const resultedVtxos = findVtxosResultedFromTxid([...spendable, ...spent], sb);
|
|
@@ -91,18 +85,16 @@ function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
91
85
|
}
|
|
92
86
|
const vtxo = getVtxo(resultedVtxos, vtxos);
|
|
93
87
|
const txKey = {
|
|
94
|
-
commitmentTxid:
|
|
88
|
+
commitmentTxid: "",
|
|
95
89
|
boardingTxid: "",
|
|
96
90
|
arkTxid: "",
|
|
97
91
|
};
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const isArkTxId = vtxos.some((v) => v.arkTxId === sb);
|
|
101
|
-
if (isArkTxId) {
|
|
102
|
-
txKey.arkTxid = sb;
|
|
92
|
+
if (vtxo.virtualStatus.state === "preconfirmed") {
|
|
93
|
+
txKey.arkTxid = resultedAmount === 0 ? vtxo.arkTxId : vtxo.txid;
|
|
103
94
|
}
|
|
104
|
-
else
|
|
105
|
-
txKey.
|
|
95
|
+
else {
|
|
96
|
+
txKey.commitmentTxid =
|
|
97
|
+
vtxo.virtualStatus.commitmentTxIds?.[0] || "";
|
|
106
98
|
}
|
|
107
99
|
txs.push({
|
|
108
100
|
key: txKey,
|
|
@@ -62,6 +62,7 @@ const inMemory_1 = require("../storage/inMemory");
|
|
|
62
62
|
const walletRepository_1 = require("../repositories/walletRepository");
|
|
63
63
|
const contractRepository_1 = require("../repositories/contractRepository");
|
|
64
64
|
const utils_1 = require("./utils");
|
|
65
|
+
const errors_1 = require("../providers/errors");
|
|
65
66
|
/**
|
|
66
67
|
* Main wallet implementation for Bitcoin transactions with Ark protocol support.
|
|
67
68
|
* The wallet does not store any data locally and relies on Ark and onchain
|
|
@@ -589,7 +590,7 @@ class Wallet {
|
|
|
589
590
|
this.makeRegisterIntentSignature(params.inputs, outputs, onchainOutputIndexes, signingPublicKeys),
|
|
590
591
|
this.makeDeleteIntentSignature(params.inputs),
|
|
591
592
|
]);
|
|
592
|
-
const intentId = await this.
|
|
593
|
+
const intentId = await this.safeRegisterIntent(intent);
|
|
593
594
|
const abortController = new AbortController();
|
|
594
595
|
// listen to settlement events
|
|
595
596
|
try {
|
|
@@ -741,7 +742,9 @@ class Wallet {
|
|
|
741
742
|
// delete the intent to not be stuck in the queue
|
|
742
743
|
await this.arkProvider.deleteIntent(deleteIntent);
|
|
743
744
|
}
|
|
744
|
-
catch {
|
|
745
|
+
catch (error) {
|
|
746
|
+
console.error("failed to delete intent: ", error);
|
|
747
|
+
}
|
|
745
748
|
throw error;
|
|
746
749
|
}
|
|
747
750
|
throw new Error("Settlement failed");
|
|
@@ -956,6 +959,27 @@ class Wallet {
|
|
|
956
959
|
: undefined);
|
|
957
960
|
}
|
|
958
961
|
}
|
|
962
|
+
async safeRegisterIntent(intent) {
|
|
963
|
+
try {
|
|
964
|
+
return this.arkProvider.registerIntent(intent);
|
|
965
|
+
}
|
|
966
|
+
catch (error) {
|
|
967
|
+
// catch the "already registered by another intent" error
|
|
968
|
+
if (error instanceof errors_1.ArkError &&
|
|
969
|
+
error.code === 0 &&
|
|
970
|
+
error.message.includes("duplicated input")) {
|
|
971
|
+
// delete all intents spending one of the wallet coins
|
|
972
|
+
const allSpendableCoins = await this.getVtxos({
|
|
973
|
+
withRecoverable: true,
|
|
974
|
+
});
|
|
975
|
+
const deleteIntent = await this.makeDeleteIntentSignature(allSpendableCoins);
|
|
976
|
+
await this.arkProvider.deleteIntent(deleteIntent);
|
|
977
|
+
// try again
|
|
978
|
+
return this.arkProvider.registerIntent(intent);
|
|
979
|
+
}
|
|
980
|
+
throw error;
|
|
981
|
+
}
|
|
982
|
+
}
|
|
959
983
|
async makeRegisterIntentSignature(coins, outputs, onchainOutputsIndexes, cosignerPubKeys) {
|
|
960
984
|
const inputs = this.prepareIntentProofInputs(coins);
|
|
961
985
|
const message = {
|
|
@@ -11,10 +11,6 @@ export function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
11
11
|
// All vtxos are received unless:
|
|
12
12
|
// - they resulted from a settlement (either boarding or refresh)
|
|
13
13
|
// - they are the change of a spend tx
|
|
14
|
-
// - they were spent in a payment (have arkTxId set)
|
|
15
|
-
// - they resulted from a payment (their txid matches an arkTxId of a spent vtxo)
|
|
16
|
-
// First, collect all arkTxIds from spent vtxos to identify payment transactions
|
|
17
|
-
const paymentArkTxIds = new Set(spent.filter((v) => v.arkTxId).map((v) => v.arkTxId));
|
|
18
14
|
let vtxosLeftToCheck = [...spent];
|
|
19
15
|
for (const vtxo of [...spendable, ...spent]) {
|
|
20
16
|
if (vtxo.virtualStatus.state !== "preconfirmed" &&
|
|
@@ -22,16 +18,6 @@ export function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
22
18
|
vtxo.virtualStatus.commitmentTxIds.some((txid) => boardingBatchTxids.has(txid))) {
|
|
23
19
|
continue;
|
|
24
20
|
}
|
|
25
|
-
// Skip vtxos that were spent in a payment transaction
|
|
26
|
-
// These will be handled in the sent transaction section below
|
|
27
|
-
if (vtxo.arkTxId) {
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
// Skip vtxos that resulted from a payment transaction
|
|
31
|
-
// (their txid matches an arkTxId from a spent vtxo)
|
|
32
|
-
if (paymentArkTxIds.has(vtxo.txid)) {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
21
|
const settleVtxos = findVtxosSpentInSettlement(vtxosLeftToCheck, vtxo);
|
|
36
22
|
vtxosLeftToCheck = removeVtxosFromList(vtxosLeftToCheck, settleVtxos);
|
|
37
23
|
const settleAmount = reduceVtxosAmount(settleVtxos);
|
|
@@ -45,7 +31,7 @@ export function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
45
31
|
continue; // settlement or change, ignore
|
|
46
32
|
}
|
|
47
33
|
const txKey = {
|
|
48
|
-
commitmentTxid:
|
|
34
|
+
commitmentTxid: "",
|
|
49
35
|
boardingTxid: "",
|
|
50
36
|
arkTxid: "",
|
|
51
37
|
};
|
|
@@ -56,6 +42,10 @@ export function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
56
42
|
settled = true;
|
|
57
43
|
}
|
|
58
44
|
}
|
|
45
|
+
else {
|
|
46
|
+
txKey.commitmentTxid =
|
|
47
|
+
vtxo.virtualStatus.commitmentTxIds?.[0] || "";
|
|
48
|
+
}
|
|
59
49
|
txs.push({
|
|
60
50
|
key: txKey,
|
|
61
51
|
amount: vtxo.value - settleAmount - spentAmount,
|
|
@@ -67,17 +57,21 @@ export function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
67
57
|
// vtxos by settled by or ark txid
|
|
68
58
|
const vtxosByTxid = new Map();
|
|
69
59
|
for (const v of spent) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
60
|
+
if (v.settledBy) {
|
|
61
|
+
if (!vtxosByTxid.has(v.settledBy)) {
|
|
62
|
+
vtxosByTxid.set(v.settledBy, []);
|
|
63
|
+
}
|
|
64
|
+
const currentVtxos = vtxosByTxid.get(v.settledBy);
|
|
65
|
+
vtxosByTxid.set(v.settledBy, [...currentVtxos, v]);
|
|
66
|
+
}
|
|
67
|
+
if (!v.arkTxId) {
|
|
74
68
|
continue;
|
|
75
69
|
}
|
|
76
|
-
if (!vtxosByTxid.has(
|
|
77
|
-
vtxosByTxid.set(
|
|
70
|
+
if (!vtxosByTxid.has(v.arkTxId)) {
|
|
71
|
+
vtxosByTxid.set(v.arkTxId, []);
|
|
78
72
|
}
|
|
79
|
-
const currentVtxos = vtxosByTxid.get(
|
|
80
|
-
vtxosByTxid.set(
|
|
73
|
+
const currentVtxos = vtxosByTxid.get(v.arkTxId);
|
|
74
|
+
vtxosByTxid.set(v.arkTxId, [...currentVtxos, v]);
|
|
81
75
|
}
|
|
82
76
|
for (const [sb, vtxos] of vtxosByTxid) {
|
|
83
77
|
const resultedVtxos = findVtxosResultedFromTxid([...spendable, ...spent], sb);
|
|
@@ -88,18 +82,16 @@ export function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
|
88
82
|
}
|
|
89
83
|
const vtxo = getVtxo(resultedVtxos, vtxos);
|
|
90
84
|
const txKey = {
|
|
91
|
-
commitmentTxid:
|
|
85
|
+
commitmentTxid: "",
|
|
92
86
|
boardingTxid: "",
|
|
93
87
|
arkTxid: "",
|
|
94
88
|
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const isArkTxId = vtxos.some((v) => v.arkTxId === sb);
|
|
98
|
-
if (isArkTxId) {
|
|
99
|
-
txKey.arkTxid = sb;
|
|
89
|
+
if (vtxo.virtualStatus.state === "preconfirmed") {
|
|
90
|
+
txKey.arkTxid = resultedAmount === 0 ? vtxo.arkTxId : vtxo.txid;
|
|
100
91
|
}
|
|
101
|
-
else
|
|
102
|
-
txKey.
|
|
92
|
+
else {
|
|
93
|
+
txKey.commitmentTxid =
|
|
94
|
+
vtxo.virtualStatus.commitmentTxIds?.[0] || "";
|
|
103
95
|
}
|
|
104
96
|
txs.push({
|
|
105
97
|
key: txKey,
|
|
@@ -25,6 +25,7 @@ import { InMemoryStorageAdapter } from '../storage/inMemory.js';
|
|
|
25
25
|
import { WalletRepositoryImpl, } from '../repositories/walletRepository.js';
|
|
26
26
|
import { ContractRepositoryImpl, } from '../repositories/contractRepository.js';
|
|
27
27
|
import { extendCoin, extendVirtualCoin } from './utils.js';
|
|
28
|
+
import { ArkError } from '../providers/errors.js';
|
|
28
29
|
/**
|
|
29
30
|
* Main wallet implementation for Bitcoin transactions with Ark protocol support.
|
|
30
31
|
* The wallet does not store any data locally and relies on Ark and onchain
|
|
@@ -552,7 +553,7 @@ export class Wallet {
|
|
|
552
553
|
this.makeRegisterIntentSignature(params.inputs, outputs, onchainOutputIndexes, signingPublicKeys),
|
|
553
554
|
this.makeDeleteIntentSignature(params.inputs),
|
|
554
555
|
]);
|
|
555
|
-
const intentId = await this.
|
|
556
|
+
const intentId = await this.safeRegisterIntent(intent);
|
|
556
557
|
const abortController = new AbortController();
|
|
557
558
|
// listen to settlement events
|
|
558
559
|
try {
|
|
@@ -704,7 +705,9 @@ export class Wallet {
|
|
|
704
705
|
// delete the intent to not be stuck in the queue
|
|
705
706
|
await this.arkProvider.deleteIntent(deleteIntent);
|
|
706
707
|
}
|
|
707
|
-
catch {
|
|
708
|
+
catch (error) {
|
|
709
|
+
console.error("failed to delete intent: ", error);
|
|
710
|
+
}
|
|
708
711
|
throw error;
|
|
709
712
|
}
|
|
710
713
|
throw new Error("Settlement failed");
|
|
@@ -919,6 +922,27 @@ export class Wallet {
|
|
|
919
922
|
: undefined);
|
|
920
923
|
}
|
|
921
924
|
}
|
|
925
|
+
async safeRegisterIntent(intent) {
|
|
926
|
+
try {
|
|
927
|
+
return this.arkProvider.registerIntent(intent);
|
|
928
|
+
}
|
|
929
|
+
catch (error) {
|
|
930
|
+
// catch the "already registered by another intent" error
|
|
931
|
+
if (error instanceof ArkError &&
|
|
932
|
+
error.code === 0 &&
|
|
933
|
+
error.message.includes("duplicated input")) {
|
|
934
|
+
// delete all intents spending one of the wallet coins
|
|
935
|
+
const allSpendableCoins = await this.getVtxos({
|
|
936
|
+
withRecoverable: true,
|
|
937
|
+
});
|
|
938
|
+
const deleteIntent = await this.makeDeleteIntentSignature(allSpendableCoins);
|
|
939
|
+
await this.arkProvider.deleteIntent(deleteIntent);
|
|
940
|
+
// try again
|
|
941
|
+
return this.arkProvider.registerIntent(intent);
|
|
942
|
+
}
|
|
943
|
+
throw error;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
922
946
|
async makeRegisterIntentSignature(coins, outputs, onchainOutputsIndexes, cosignerPubKeys) {
|
|
923
947
|
const inputs = this.prepareIntentProofInputs(coins);
|
|
924
948
|
const message = {
|
|
@@ -94,6 +94,7 @@ export declare class Wallet implements IWallet {
|
|
|
94
94
|
private handleSettlementSigningEvent;
|
|
95
95
|
private handleSettlementTreeNoncesEvent;
|
|
96
96
|
private handleSettlementFinalizationEvent;
|
|
97
|
+
safeRegisterIntent(intent: SignedIntent): Promise<string>;
|
|
97
98
|
makeRegisterIntentSignature(coins: ExtendedCoin[], outputs: TransactionOutput[], onchainOutputsIndexes: number[], cosignerPubKeys: string[]): Promise<SignedIntent>;
|
|
98
99
|
makeDeleteIntentSignature(coins: ExtendedCoin[]): Promise<SignedIntent>;
|
|
99
100
|
private prepareIntentProofInputs;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkade-os/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
4
4
|
"description": "Bitcoin wallet SDK with Taproot and Ark integration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -63,8 +63,8 @@
|
|
|
63
63
|
"@types/node": "24.3.1",
|
|
64
64
|
"@vitest/coverage-v8": "3.2.4",
|
|
65
65
|
"esbuild": "^0.25.9",
|
|
66
|
-
"eventsource": "4.0.0",
|
|
67
66
|
"expo": "~52.0.47",
|
|
67
|
+
"eventsource": "4.0.0",
|
|
68
68
|
"glob": "11.0.3",
|
|
69
69
|
"husky": "9.1.7",
|
|
70
70
|
"prettier": "3.6.2",
|