@arkade-os/sdk 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +156 -174
- package/dist/cjs/arknote/index.js +61 -58
- package/dist/cjs/bip322/errors.js +13 -0
- package/dist/cjs/bip322/index.js +178 -0
- package/dist/cjs/forfeit.js +14 -25
- package/dist/cjs/identity/singleKey.js +68 -0
- package/dist/cjs/index.js +41 -17
- package/dist/cjs/providers/ark.js +253 -317
- package/dist/cjs/providers/indexer.js +525 -0
- package/dist/cjs/providers/onchain.js +193 -15
- package/dist/cjs/script/address.js +48 -17
- package/dist/cjs/script/base.js +120 -3
- package/dist/cjs/script/default.js +18 -4
- package/dist/cjs/script/tapscript.js +46 -14
- package/dist/cjs/script/vhtlc.js +27 -7
- package/dist/cjs/tree/signingSession.js +63 -106
- package/dist/cjs/tree/txTree.js +193 -0
- package/dist/cjs/tree/validation.js +79 -155
- package/dist/cjs/utils/anchor.js +35 -0
- package/dist/cjs/utils/arkTransaction.js +108 -0
- package/dist/cjs/utils/transactionHistory.js +84 -72
- package/dist/cjs/utils/txSizeEstimator.js +12 -0
- package/dist/cjs/utils/unknownFields.js +211 -0
- package/dist/cjs/wallet/index.js +12 -0
- package/dist/cjs/wallet/onchain.js +201 -0
- package/dist/cjs/wallet/ramps.js +95 -0
- package/dist/cjs/wallet/serviceWorker/db/vtxo/idb.js +32 -0
- package/dist/cjs/wallet/serviceWorker/request.js +15 -12
- package/dist/cjs/wallet/serviceWorker/response.js +22 -27
- package/dist/cjs/wallet/serviceWorker/utils.js +8 -0
- package/dist/cjs/wallet/serviceWorker/wallet.js +58 -34
- package/dist/cjs/wallet/serviceWorker/worker.js +117 -108
- package/dist/cjs/wallet/unroll.js +270 -0
- package/dist/cjs/wallet/wallet.js +701 -459
- package/dist/esm/arknote/index.js +61 -57
- package/dist/esm/bip322/errors.js +9 -0
- package/dist/esm/bip322/index.js +174 -0
- package/dist/esm/forfeit.js +15 -26
- package/dist/esm/identity/singleKey.js +64 -0
- package/dist/esm/index.js +30 -12
- package/dist/esm/providers/ark.js +252 -317
- package/dist/esm/providers/indexer.js +521 -0
- package/dist/esm/providers/onchain.js +193 -15
- package/dist/esm/script/address.js +48 -17
- package/dist/esm/script/base.js +120 -3
- package/dist/esm/script/default.js +18 -4
- package/dist/esm/script/tapscript.js +46 -14
- package/dist/esm/script/vhtlc.js +27 -7
- package/dist/esm/tree/signingSession.js +65 -108
- package/dist/esm/tree/txTree.js +189 -0
- package/dist/esm/tree/validation.js +75 -152
- package/dist/esm/utils/anchor.js +31 -0
- package/dist/esm/utils/arkTransaction.js +105 -0
- package/dist/esm/utils/transactionHistory.js +84 -72
- package/dist/esm/utils/txSizeEstimator.js +12 -0
- package/dist/esm/utils/unknownFields.js +173 -0
- package/dist/esm/wallet/index.js +9 -0
- package/dist/esm/wallet/onchain.js +196 -0
- package/dist/esm/wallet/ramps.js +91 -0
- package/dist/esm/wallet/serviceWorker/db/vtxo/idb.js +32 -0
- package/dist/esm/wallet/serviceWorker/request.js +15 -12
- package/dist/esm/wallet/serviceWorker/response.js +22 -27
- package/dist/esm/wallet/serviceWorker/utils.js +8 -0
- package/dist/esm/wallet/serviceWorker/wallet.js +59 -35
- package/dist/esm/wallet/serviceWorker/worker.js +117 -108
- package/dist/esm/wallet/unroll.js +267 -0
- package/dist/esm/wallet/wallet.js +674 -466
- package/dist/types/arknote/index.d.ts +40 -13
- package/dist/types/bip322/errors.d.ts +6 -0
- package/dist/types/bip322/index.d.ts +57 -0
- package/dist/types/forfeit.d.ts +2 -14
- package/dist/types/identity/singleKey.d.ts +27 -0
- package/dist/types/index.d.ts +23 -12
- package/dist/types/providers/ark.d.ts +114 -95
- package/dist/types/providers/indexer.d.ts +186 -0
- package/dist/types/providers/onchain.d.ts +41 -11
- package/dist/types/script/address.d.ts +26 -2
- package/dist/types/script/base.d.ts +13 -3
- package/dist/types/script/default.d.ts +22 -0
- package/dist/types/script/tapscript.d.ts +61 -5
- package/dist/types/script/vhtlc.d.ts +27 -0
- package/dist/types/tree/signingSession.d.ts +5 -5
- package/dist/types/tree/txTree.d.ts +28 -0
- package/dist/types/tree/validation.d.ts +15 -22
- package/dist/types/utils/anchor.d.ts +19 -0
- package/dist/types/utils/arkTransaction.d.ts +27 -0
- package/dist/types/utils/transactionHistory.d.ts +7 -1
- package/dist/types/utils/txSizeEstimator.d.ts +3 -0
- package/dist/types/utils/unknownFields.d.ts +83 -0
- package/dist/types/wallet/index.d.ts +51 -50
- package/dist/types/wallet/onchain.d.ts +49 -0
- package/dist/types/wallet/ramps.d.ts +32 -0
- package/dist/types/wallet/serviceWorker/db/vtxo/idb.d.ts +2 -0
- package/dist/types/wallet/serviceWorker/db/vtxo/index.d.ts +2 -0
- package/dist/types/wallet/serviceWorker/request.d.ts +14 -16
- package/dist/types/wallet/serviceWorker/response.d.ts +17 -19
- package/dist/types/wallet/serviceWorker/utils.d.ts +8 -0
- package/dist/types/wallet/serviceWorker/wallet.d.ts +36 -8
- package/dist/types/wallet/serviceWorker/worker.d.ts +7 -3
- package/dist/types/wallet/unroll.d.ts +102 -0
- package/dist/types/wallet/wallet.d.ts +71 -26
- package/package.json +14 -15
- package/dist/cjs/identity/inMemoryKey.js +0 -40
- package/dist/cjs/tree/vtxoTree.js +0 -231
- package/dist/cjs/utils/coinselect.js +0 -73
- package/dist/cjs/utils/psbt.js +0 -137
- package/dist/esm/identity/inMemoryKey.js +0 -36
- package/dist/esm/tree/vtxoTree.js +0 -191
- package/dist/esm/utils/coinselect.js +0 -69
- package/dist/esm/utils/psbt.js +0 -131
- package/dist/types/identity/inMemoryKey.d.ts +0 -12
- package/dist/types/tree/vtxoTree.d.ts +0 -33
- package/dist/types/utils/coinselect.d.ts +0 -21
- package/dist/types/utils/psbt.d.ts +0 -11
|
@@ -1,184 +1,108 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.
|
|
5
|
-
exports.
|
|
3
|
+
exports.ErrMissingCosignersPublicKeys = exports.ErrWrongCommitmentTxid = exports.ErrInvalidRoundTxOutputs = exports.ErrInvalidTaprootScript = exports.ErrNoLeaves = exports.ErrInvalidAmount = exports.ErrWrongSettlementTxid = exports.ErrNumberOfInputs = exports.ErrEmptyTree = exports.ErrInvalidSettlementTxOutputs = exports.ErrInvalidSettlementTx = void 0;
|
|
4
|
+
exports.validateConnectorsTxGraph = validateConnectorsTxGraph;
|
|
5
|
+
exports.validateVtxoTxGraph = validateVtxoTxGraph;
|
|
6
6
|
const base_1 = require("@scure/base");
|
|
7
7
|
const btc_signer_1 = require("@scure/btc-signer");
|
|
8
8
|
const base_2 = require("@scure/base");
|
|
9
9
|
const utils_1 = require("@scure/btc-signer/utils");
|
|
10
10
|
const musig2_1 = require("../musig2");
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
exports.
|
|
14
|
-
exports.
|
|
15
|
-
exports.
|
|
16
|
-
exports.ErrNumberOfInputs = new
|
|
17
|
-
exports.ErrWrongSettlementTxid = new
|
|
18
|
-
exports.ErrInvalidAmount = new
|
|
19
|
-
exports.ErrNoLeaves = new
|
|
20
|
-
exports.
|
|
21
|
-
exports.
|
|
22
|
-
exports.
|
|
23
|
-
exports.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
exports.ErrInvalidRootTransaction = new vtxoTree_1.TxTreeError("invalid root transaction");
|
|
30
|
-
exports.ErrInvalidNodeTransaction = new vtxoTree_1.TxTreeError("invalid node transaction");
|
|
31
|
-
const SHARED_OUTPUT_INDEX = 0;
|
|
32
|
-
const CONNECTORS_OUTPUT_INDEX = 1;
|
|
33
|
-
function validateConnectorsTree(settlementTxB64, connectorsTree) {
|
|
34
|
-
connectorsTree.validate();
|
|
35
|
-
const rootNode = connectorsTree.root();
|
|
36
|
-
if (!rootNode)
|
|
37
|
-
throw exports.ErrEmptyTree;
|
|
38
|
-
const rootTx = btc_signer_1.Transaction.fromPSBT(base_2.base64.decode(rootNode.tx));
|
|
39
|
-
if (rootTx.inputsLength !== 1)
|
|
11
|
+
const unknownFields_1 = require("../utils/unknownFields");
|
|
12
|
+
const ErrInvalidSettlementTx = (tx) => new Error(`invalid settlement transaction: ${tx}`);
|
|
13
|
+
exports.ErrInvalidSettlementTx = ErrInvalidSettlementTx;
|
|
14
|
+
exports.ErrInvalidSettlementTxOutputs = new Error("invalid settlement transaction outputs");
|
|
15
|
+
exports.ErrEmptyTree = new Error("empty tree");
|
|
16
|
+
exports.ErrNumberOfInputs = new Error("invalid number of inputs");
|
|
17
|
+
exports.ErrWrongSettlementTxid = new Error("wrong settlement txid");
|
|
18
|
+
exports.ErrInvalidAmount = new Error("invalid amount");
|
|
19
|
+
exports.ErrNoLeaves = new Error("no leaves");
|
|
20
|
+
exports.ErrInvalidTaprootScript = new Error("invalid taproot script");
|
|
21
|
+
exports.ErrInvalidRoundTxOutputs = new Error("invalid round transaction outputs");
|
|
22
|
+
exports.ErrWrongCommitmentTxid = new Error("wrong commitment txid");
|
|
23
|
+
exports.ErrMissingCosignersPublicKeys = new Error("missing cosigners public keys");
|
|
24
|
+
const BATCH_OUTPUT_VTXO_INDEX = 0;
|
|
25
|
+
const BATCH_OUTPUT_CONNECTORS_INDEX = 1;
|
|
26
|
+
function validateConnectorsTxGraph(settlementTxB64, connectorsGraph) {
|
|
27
|
+
connectorsGraph.validate();
|
|
28
|
+
if (connectorsGraph.root.inputsLength !== 1)
|
|
40
29
|
throw exports.ErrNumberOfInputs;
|
|
41
|
-
const rootInput =
|
|
30
|
+
const rootInput = connectorsGraph.root.getInput(0);
|
|
42
31
|
const settlementTx = btc_signer_1.Transaction.fromPSBT(base_2.base64.decode(settlementTxB64));
|
|
43
|
-
if (settlementTx.outputsLength <=
|
|
32
|
+
if (settlementTx.outputsLength <= BATCH_OUTPUT_CONNECTORS_INDEX)
|
|
44
33
|
throw exports.ErrInvalidSettlementTxOutputs;
|
|
45
34
|
const expectedRootTxid = base_1.hex.encode((0, utils_1.sha256x2)(settlementTx.toBytes(true)).reverse());
|
|
46
35
|
if (!rootInput.txid)
|
|
47
36
|
throw exports.ErrWrongSettlementTxid;
|
|
48
37
|
if (base_1.hex.encode(rootInput.txid) !== expectedRootTxid)
|
|
49
38
|
throw exports.ErrWrongSettlementTxid;
|
|
50
|
-
if (rootInput.index !==
|
|
39
|
+
if (rootInput.index !== BATCH_OUTPUT_CONNECTORS_INDEX)
|
|
51
40
|
throw exports.ErrWrongSettlementTxid;
|
|
52
41
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const sharedOutputAmount = sharedOutput.amount;
|
|
70
|
-
const nbNodes = vtxoTree.numberOfNodes();
|
|
71
|
-
if (nbNodes === 0) {
|
|
42
|
+
// ValidateVtxoTxGraph checks if the given vtxo graph is valid.
|
|
43
|
+
// The function validates:
|
|
44
|
+
// - the number of nodes
|
|
45
|
+
// - the number of leaves
|
|
46
|
+
// - children coherence with parent.
|
|
47
|
+
// - every control block and taproot output scripts.
|
|
48
|
+
// - input and output amounts.
|
|
49
|
+
function validateVtxoTxGraph(graph, roundTransaction, sweepTapTreeRoot) {
|
|
50
|
+
if (roundTransaction.outputsLength < BATCH_OUTPUT_VTXO_INDEX + 1) {
|
|
51
|
+
throw exports.ErrInvalidRoundTxOutputs;
|
|
52
|
+
}
|
|
53
|
+
const batchOutputAmount = roundTransaction.getOutput(BATCH_OUTPUT_VTXO_INDEX)?.amount;
|
|
54
|
+
if (!batchOutputAmount) {
|
|
55
|
+
throw exports.ErrInvalidRoundTxOutputs;
|
|
56
|
+
}
|
|
57
|
+
if (!graph.root) {
|
|
72
58
|
throw exports.ErrEmptyTree;
|
|
73
59
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
try {
|
|
81
|
-
rootTx = btc_signer_1.Transaction.fromPSBT(base_2.base64.decode(rootNode.tx));
|
|
60
|
+
const rootInput = graph.root.getInput(0);
|
|
61
|
+
const commitmentTxid = base_1.hex.encode((0, utils_1.sha256x2)(roundTransaction.toBytes(true)).reverse());
|
|
62
|
+
if (!rootInput.txid ||
|
|
63
|
+
base_1.hex.encode(rootInput.txid) !== commitmentTxid ||
|
|
64
|
+
rootInput.index !== BATCH_OUTPUT_VTXO_INDEX) {
|
|
65
|
+
throw exports.ErrWrongCommitmentTxid;
|
|
82
66
|
}
|
|
83
|
-
catch {
|
|
84
|
-
throw exports.ErrInvalidRootTransaction;
|
|
85
|
-
}
|
|
86
|
-
if (rootTx.inputsLength !== 1) {
|
|
87
|
-
throw exports.ErrNumberOfInputs;
|
|
88
|
-
}
|
|
89
|
-
const rootInput = rootTx.getInput(0);
|
|
90
|
-
if (!rootInput.txid || rootInput.index === undefined)
|
|
91
|
-
throw exports.ErrWrongSettlementTxid;
|
|
92
|
-
const settlementTxid = base_1.hex.encode((0, utils_1.sha256x2)(settlementTransaction.toBytes(true)).reverse());
|
|
93
|
-
if (base_1.hex.encode(rootInput.txid) !== settlementTxid ||
|
|
94
|
-
rootInput.index !== SHARED_OUTPUT_INDEX) {
|
|
95
|
-
throw exports.ErrWrongSettlementTxid;
|
|
96
|
-
}
|
|
97
|
-
// Check root output amounts
|
|
98
67
|
let sumRootValue = 0n;
|
|
99
|
-
for (let i = 0; i <
|
|
100
|
-
const output =
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
|
|
68
|
+
for (let i = 0; i < graph.root.outputsLength; i++) {
|
|
69
|
+
const output = graph.root.getOutput(i);
|
|
70
|
+
if (output?.amount) {
|
|
71
|
+
sumRootValue += output.amount;
|
|
72
|
+
}
|
|
104
73
|
}
|
|
105
|
-
if (sumRootValue
|
|
74
|
+
if (sumRootValue !== batchOutputAmount) {
|
|
106
75
|
throw exports.ErrInvalidAmount;
|
|
107
76
|
}
|
|
108
|
-
|
|
77
|
+
const leaves = graph.leaves();
|
|
78
|
+
if (leaves.length === 0) {
|
|
109
79
|
throw exports.ErrNoLeaves;
|
|
110
80
|
}
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
if (tx.inputsLength !== 1) {
|
|
138
|
-
throw exports.ErrNumberOfInputs;
|
|
139
|
-
}
|
|
140
|
-
const input = tx.getInput(0);
|
|
141
|
-
if (!input.txid)
|
|
142
|
-
throw exports.ErrParentTxidInput;
|
|
143
|
-
if (base_1.hex.encode(input.txid) !== node.parentTxid) {
|
|
144
|
-
throw exports.ErrParentTxidInput;
|
|
145
|
-
}
|
|
146
|
-
const children = vtxoTree.children(node.txid);
|
|
147
|
-
if (node.leaf && children.length >= 1) {
|
|
148
|
-
throw exports.ErrLeafChildren;
|
|
149
|
-
}
|
|
150
|
-
// Validate each child
|
|
151
|
-
for (let childIndex = 0; childIndex < children.length; childIndex++) {
|
|
152
|
-
const child = children[childIndex];
|
|
153
|
-
const childTx = btc_signer_1.Transaction.fromPSBT(base_2.base64.decode(child.tx));
|
|
154
|
-
const parentOutput = tx.getOutput(childIndex);
|
|
155
|
-
if (!parentOutput?.script)
|
|
156
|
-
throw exports.ErrInvalidTaprootScript;
|
|
157
|
-
const previousScriptKey = parentOutput.script.slice(2);
|
|
158
|
-
if (previousScriptKey.length !== 32) {
|
|
159
|
-
throw exports.ErrInvalidTaprootScript;
|
|
160
|
-
}
|
|
161
|
-
// Get cosigner keys from input
|
|
162
|
-
const cosignerKeys = (0, vtxoTree_1.getCosignerKeys)(childTx);
|
|
163
|
-
// Aggregate keys
|
|
164
|
-
const { finalKey } = (0, musig2_1.aggregateKeys)(cosignerKeys, true, {
|
|
165
|
-
taprootTweak: tapTreeRoot,
|
|
166
|
-
});
|
|
167
|
-
if (base_1.hex.encode(finalKey) !== base_1.hex.encode(previousScriptKey.slice(2))) {
|
|
168
|
-
throw exports.ErrInternalKey;
|
|
169
|
-
}
|
|
170
|
-
// Check amounts
|
|
171
|
-
let sumChildAmount = 0n;
|
|
172
|
-
for (let i = 0; i < childTx.outputsLength; i++) {
|
|
173
|
-
const output = childTx.getOutput(i);
|
|
174
|
-
if (!output?.amount)
|
|
175
|
-
continue;
|
|
176
|
-
sumChildAmount += output.amount;
|
|
177
|
-
}
|
|
178
|
-
if (!parentOutput.amount)
|
|
179
|
-
throw exports.ErrInvalidAmount;
|
|
180
|
-
if (sumChildAmount >= parentOutput.amount) {
|
|
181
|
-
throw exports.ErrInvalidAmount;
|
|
81
|
+
// validate the graph structure
|
|
82
|
+
graph.validate();
|
|
83
|
+
// iterates over all the nodes of the graph to verify that cosigners public keys are corresponding to the parent output
|
|
84
|
+
for (const g of graph) {
|
|
85
|
+
for (const [childIndex, child] of g.children) {
|
|
86
|
+
const parentOutput = g.root.getOutput(childIndex);
|
|
87
|
+
if (!parentOutput?.script) {
|
|
88
|
+
throw new Error(`parent output ${childIndex} not found`);
|
|
89
|
+
}
|
|
90
|
+
const previousScriptKey = parentOutput.script.slice(2);
|
|
91
|
+
if (previousScriptKey.length !== 32) {
|
|
92
|
+
throw new Error(`parent output ${childIndex} has invalid script`);
|
|
93
|
+
}
|
|
94
|
+
const cosigners = (0, unknownFields_1.getArkPsbtFields)(child.root, 0, unknownFields_1.CosignerPublicKey);
|
|
95
|
+
if (cosigners.length === 0) {
|
|
96
|
+
throw exports.ErrMissingCosignersPublicKeys;
|
|
97
|
+
}
|
|
98
|
+
const cosignerKeys = cosigners.map((c) => c.key);
|
|
99
|
+
const { finalKey } = (0, musig2_1.aggregateKeys)(cosignerKeys, true, {
|
|
100
|
+
taprootTweak: sweepTapTreeRoot,
|
|
101
|
+
});
|
|
102
|
+
if (!finalKey ||
|
|
103
|
+
base_1.hex.encode(finalKey.slice(1)) !== base_1.hex.encode(previousScriptKey)) {
|
|
104
|
+
throw exports.ErrInvalidTaprootScript;
|
|
105
|
+
}
|
|
182
106
|
}
|
|
183
107
|
}
|
|
184
108
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.P2A = exports.ANCHOR_PKSCRIPT = exports.ANCHOR_VALUE = void 0;
|
|
4
|
+
exports.findP2AOutput = findP2AOutput;
|
|
5
|
+
const base_1 = require("@scure/base");
|
|
6
|
+
exports.ANCHOR_VALUE = 0n;
|
|
7
|
+
exports.ANCHOR_PKSCRIPT = new Uint8Array([0x51, 0x02, 0x4e, 0x73]);
|
|
8
|
+
/**
|
|
9
|
+
* A zero-value anchor output.
|
|
10
|
+
*/
|
|
11
|
+
exports.P2A = {
|
|
12
|
+
script: exports.ANCHOR_PKSCRIPT,
|
|
13
|
+
amount: exports.ANCHOR_VALUE,
|
|
14
|
+
};
|
|
15
|
+
const hexP2Ascript = base_1.hex.encode(exports.P2A.script);
|
|
16
|
+
/**
|
|
17
|
+
* search for anchor in the given transaction.
|
|
18
|
+
* @throws {Error} if the anchor is not found or has the wrong amount
|
|
19
|
+
*/
|
|
20
|
+
function findP2AOutput(tx) {
|
|
21
|
+
for (let i = 0; i < tx.outputsLength; i++) {
|
|
22
|
+
const output = tx.getOutput(i);
|
|
23
|
+
if (output.script && base_1.hex.encode(output.script) === hexP2Ascript) {
|
|
24
|
+
if (output.amount !== exports.P2A.amount) {
|
|
25
|
+
throw new Error(`P2A output has wrong amount, expected ${exports.P2A.amount} got ${output.amount}`);
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
txid: tx.id,
|
|
29
|
+
index: i,
|
|
30
|
+
witnessUtxo: exports.P2A,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
throw new Error("P2A output not found");
|
|
35
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildOffchainTx = buildOffchainTx;
|
|
4
|
+
const btc_signer_1 = require("@scure/btc-signer");
|
|
5
|
+
const tapscript_1 = require("../script/tapscript");
|
|
6
|
+
const base_1 = require("../script/base");
|
|
7
|
+
const anchor_1 = require("./anchor");
|
|
8
|
+
const base_2 = require("@scure/base");
|
|
9
|
+
const utils_1 = require("@scure/btc-signer/utils");
|
|
10
|
+
const unknownFields_1 = require("./unknownFields");
|
|
11
|
+
/**
|
|
12
|
+
* Builds an offchain transaction with checkpoint transactions.
|
|
13
|
+
*
|
|
14
|
+
* Creates one checkpoint transaction per input and a virtual transaction that
|
|
15
|
+
* combines all the checkpoints, sending to the specified outputs. This is the
|
|
16
|
+
* core function for creating Ark transactions.
|
|
17
|
+
*
|
|
18
|
+
* @param inputs - Array of virtual transaction inputs
|
|
19
|
+
* @param outputs - Array of transaction outputs
|
|
20
|
+
* @param serverUnrollScript - Server unroll script for checkpoint transactions
|
|
21
|
+
* @returns Object containing the virtual transaction and checkpoint transactions
|
|
22
|
+
*/
|
|
23
|
+
function buildOffchainTx(inputs, outputs, serverUnrollScript) {
|
|
24
|
+
const checkpoints = inputs.map((input) => buildCheckpointTx(input, serverUnrollScript));
|
|
25
|
+
const arkTx = buildVirtualTx(checkpoints.map((c) => c.input), outputs);
|
|
26
|
+
return {
|
|
27
|
+
arkTx,
|
|
28
|
+
checkpoints: checkpoints.map((c) => c.tx),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function buildVirtualTx(inputs, outputs) {
|
|
32
|
+
let lockTime = 0n;
|
|
33
|
+
for (const input of inputs) {
|
|
34
|
+
const tapscript = (0, tapscript_1.decodeTapscript)((0, base_1.scriptFromTapLeafScript)(input.tapLeafScript));
|
|
35
|
+
if (tapscript_1.CLTVMultisigTapscript.is(tapscript)) {
|
|
36
|
+
if (lockTime !== 0n) {
|
|
37
|
+
// if a locktime is already set, check if the new locktime is in the same unit
|
|
38
|
+
if (isSeconds(lockTime) !==
|
|
39
|
+
isSeconds(tapscript.params.absoluteTimelock)) {
|
|
40
|
+
throw new Error("cannot mix seconds and blocks locktime");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (tapscript.params.absoluteTimelock > lockTime) {
|
|
44
|
+
lockTime = tapscript.params.absoluteTimelock;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const tx = new btc_signer_1.Transaction({
|
|
49
|
+
version: 3,
|
|
50
|
+
allowUnknown: true,
|
|
51
|
+
allowUnknownOutputs: true,
|
|
52
|
+
lockTime: Number(lockTime),
|
|
53
|
+
});
|
|
54
|
+
for (const [i, input] of inputs.entries()) {
|
|
55
|
+
tx.addInput({
|
|
56
|
+
txid: input.txid,
|
|
57
|
+
index: input.vout,
|
|
58
|
+
sequence: lockTime ? btc_signer_1.DEFAULT_SEQUENCE - 1 : undefined,
|
|
59
|
+
witnessUtxo: {
|
|
60
|
+
script: base_1.VtxoScript.decode(input.tapTree).pkScript,
|
|
61
|
+
amount: BigInt(input.value),
|
|
62
|
+
},
|
|
63
|
+
tapLeafScript: [input.tapLeafScript],
|
|
64
|
+
});
|
|
65
|
+
(0, unknownFields_1.setArkPsbtField)(tx, i, unknownFields_1.VtxoTaprootTree, input.tapTree);
|
|
66
|
+
}
|
|
67
|
+
for (const output of outputs) {
|
|
68
|
+
tx.addOutput(output);
|
|
69
|
+
}
|
|
70
|
+
// add the anchor output
|
|
71
|
+
tx.addOutput(anchor_1.P2A);
|
|
72
|
+
return tx;
|
|
73
|
+
}
|
|
74
|
+
function buildCheckpointTx(vtxo, serverUnrollScript) {
|
|
75
|
+
// create the checkpoint vtxo script from collaborative closure
|
|
76
|
+
const collaborativeClosure = (0, tapscript_1.decodeTapscript)(vtxo.checkpointTapLeafScript ??
|
|
77
|
+
(0, base_1.scriptFromTapLeafScript)(vtxo.tapLeafScript));
|
|
78
|
+
// create the checkpoint vtxo script combining collaborative closure and server unroll script
|
|
79
|
+
const checkpointVtxoScript = new base_1.VtxoScript([
|
|
80
|
+
serverUnrollScript.script,
|
|
81
|
+
collaborativeClosure.script,
|
|
82
|
+
]);
|
|
83
|
+
// build the checkpoint virtual tx
|
|
84
|
+
const checkpointTx = buildVirtualTx([vtxo], [
|
|
85
|
+
{
|
|
86
|
+
amount: BigInt(vtxo.value),
|
|
87
|
+
script: checkpointVtxoScript.pkScript,
|
|
88
|
+
},
|
|
89
|
+
]);
|
|
90
|
+
// get the collaborative leaf proof
|
|
91
|
+
const collaborativeLeafProof = checkpointVtxoScript.findLeaf(base_2.hex.encode(collaborativeClosure.script));
|
|
92
|
+
// create the checkpoint input that will be used as input of the virtual tx
|
|
93
|
+
const checkpointInput = {
|
|
94
|
+
txid: base_2.hex.encode((0, utils_1.sha256x2)(checkpointTx.toBytes(true)).reverse()),
|
|
95
|
+
vout: 0,
|
|
96
|
+
value: vtxo.value,
|
|
97
|
+
tapLeafScript: collaborativeLeafProof,
|
|
98
|
+
tapTree: checkpointVtxoScript.encode(),
|
|
99
|
+
};
|
|
100
|
+
return {
|
|
101
|
+
tx: checkpointTx,
|
|
102
|
+
input: checkpointInput,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const nLocktimeMinSeconds = 500000000n;
|
|
106
|
+
function isSeconds(locktime) {
|
|
107
|
+
return locktime >= nLocktimeMinSeconds;
|
|
108
|
+
}
|
|
@@ -3,56 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.vtxosToTxs = vtxosToTxs;
|
|
4
4
|
const wallet_1 = require("../wallet");
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return [];
|
|
11
|
-
}
|
|
12
|
-
return vtxos.filter((v) => {
|
|
13
|
-
if (!v.spentBy)
|
|
14
|
-
return false;
|
|
15
|
-
return v.spentBy === vtxo.virtualStatus.batchTxID;
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Helper function to find vtxos that were spent in a payment
|
|
20
|
-
*/
|
|
21
|
-
function findVtxosSpentInPayment(vtxos, vtxo) {
|
|
22
|
-
return vtxos.filter((v) => {
|
|
23
|
-
if (!v.spentBy)
|
|
24
|
-
return false;
|
|
25
|
-
return v.spentBy === vtxo.txid;
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Helper function to find vtxos that resulted from a spentBy transaction
|
|
30
|
-
*/
|
|
31
|
-
function findVtxosResultedFromSpentBy(vtxos, spentBy) {
|
|
32
|
-
return vtxos.filter((v) => {
|
|
33
|
-
if (v.virtualStatus.state !== "pending" &&
|
|
34
|
-
v.virtualStatus.batchTxID === spentBy) {
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
return v.txid === spentBy;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Helper function to reduce vtxos to their total amount
|
|
6
|
+
* @param spendable - Vtxos that are spendable
|
|
7
|
+
* @param spent - Vtxos that are spent
|
|
8
|
+
* @param boardingBatchTxids - Set of boarding batch txids
|
|
9
|
+
* @returns Ark transactions
|
|
42
10
|
*/
|
|
43
|
-
function
|
|
44
|
-
return vtxos.reduce((sum, v) => sum + v.value, 0);
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Helper function to get a vtxo from a list of vtxos
|
|
48
|
-
*/
|
|
49
|
-
function getVtxo(resultedVtxos, spentVtxos) {
|
|
50
|
-
if (resultedVtxos.length === 0) {
|
|
51
|
-
return spentVtxos[0];
|
|
52
|
-
}
|
|
53
|
-
return resultedVtxos[0];
|
|
54
|
-
}
|
|
55
|
-
function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
11
|
+
function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
56
12
|
const txs = [];
|
|
57
13
|
// Receive case
|
|
58
14
|
// All vtxos are received unless:
|
|
@@ -60,8 +16,9 @@ function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
60
16
|
// - they are the change of a spend tx
|
|
61
17
|
let vtxosLeftToCheck = [...spent];
|
|
62
18
|
for (const vtxo of [...spendable, ...spent]) {
|
|
63
|
-
if (vtxo.virtualStatus.state !== "
|
|
64
|
-
|
|
19
|
+
if (vtxo.virtualStatus.state !== "preconfirmed" &&
|
|
20
|
+
vtxo.virtualStatus.commitmentTxIds &&
|
|
21
|
+
vtxo.virtualStatus.commitmentTxIds.some((txid) => boardingBatchTxids.has(txid))) {
|
|
65
22
|
continue;
|
|
66
23
|
}
|
|
67
24
|
const settleVtxos = findVtxosSpentInSettlement(vtxosLeftToCheck, vtxo);
|
|
@@ -77,13 +34,13 @@ function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
77
34
|
continue; // settlement or change, ignore
|
|
78
35
|
}
|
|
79
36
|
const txKey = {
|
|
80
|
-
|
|
37
|
+
commitmentTxid: vtxo.spentBy || "",
|
|
81
38
|
boardingTxid: "",
|
|
82
|
-
|
|
39
|
+
arkTxid: "",
|
|
83
40
|
};
|
|
84
|
-
let settled = vtxo.virtualStatus.state !== "
|
|
85
|
-
if (vtxo.virtualStatus.state === "
|
|
86
|
-
txKey.
|
|
41
|
+
let settled = vtxo.virtualStatus.state !== "preconfirmed";
|
|
42
|
+
if (vtxo.virtualStatus.state === "preconfirmed") {
|
|
43
|
+
txKey.arkTxid = vtxo.txid;
|
|
87
44
|
if (vtxo.spentBy) {
|
|
88
45
|
settled = true;
|
|
89
46
|
}
|
|
@@ -96,22 +53,27 @@ function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
96
53
|
settled,
|
|
97
54
|
});
|
|
98
55
|
}
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
// - they are settlements
|
|
102
|
-
// aggregate spent by spentId
|
|
103
|
-
const vtxosBySpentBy = new Map();
|
|
56
|
+
// vtxos by settled by or ark txid
|
|
57
|
+
const vtxosByTxid = new Map();
|
|
104
58
|
for (const v of spent) {
|
|
105
|
-
if (
|
|
59
|
+
if (v.settledBy) {
|
|
60
|
+
if (!vtxosByTxid.has(v.settledBy)) {
|
|
61
|
+
vtxosByTxid.set(v.settledBy, []);
|
|
62
|
+
}
|
|
63
|
+
const currentVtxos = vtxosByTxid.get(v.settledBy);
|
|
64
|
+
vtxosByTxid.set(v.settledBy, [...currentVtxos, v]);
|
|
65
|
+
}
|
|
66
|
+
if (!v.arkTxId) {
|
|
106
67
|
continue;
|
|
107
|
-
if (!vtxosBySpentBy.has(v.spentBy)) {
|
|
108
|
-
vtxosBySpentBy.set(v.spentBy, []);
|
|
109
68
|
}
|
|
110
|
-
|
|
111
|
-
|
|
69
|
+
if (!vtxosByTxid.has(v.arkTxId)) {
|
|
70
|
+
vtxosByTxid.set(v.arkTxId, []);
|
|
71
|
+
}
|
|
72
|
+
const currentVtxos = vtxosByTxid.get(v.arkTxId);
|
|
73
|
+
vtxosByTxid.set(v.arkTxId, [...currentVtxos, v]);
|
|
112
74
|
}
|
|
113
|
-
for (const [sb, vtxos] of
|
|
114
|
-
const resultedVtxos =
|
|
75
|
+
for (const [sb, vtxos] of vtxosByTxid) {
|
|
76
|
+
const resultedVtxos = findVtxosResultedFromTxid([...spendable, ...spent], sb);
|
|
115
77
|
const resultedAmount = reduceVtxosAmount(resultedVtxos);
|
|
116
78
|
const spentAmount = reduceVtxosAmount(vtxos);
|
|
117
79
|
if (spentAmount <= resultedAmount) {
|
|
@@ -119,12 +81,12 @@ function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
119
81
|
}
|
|
120
82
|
const vtxo = getVtxo(resultedVtxos, vtxos);
|
|
121
83
|
const txKey = {
|
|
122
|
-
|
|
84
|
+
commitmentTxid: vtxo.virtualStatus.commitmentTxIds?.[0] || "",
|
|
123
85
|
boardingTxid: "",
|
|
124
|
-
|
|
86
|
+
arkTxid: "",
|
|
125
87
|
};
|
|
126
|
-
if (vtxo.virtualStatus.state === "
|
|
127
|
-
txKey.
|
|
88
|
+
if (vtxo.virtualStatus.state === "preconfirmed") {
|
|
89
|
+
txKey.arkTxid = vtxo.txid;
|
|
128
90
|
}
|
|
129
91
|
txs.push({
|
|
130
92
|
key: txKey,
|
|
@@ -136,6 +98,56 @@ function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
136
98
|
}
|
|
137
99
|
return txs;
|
|
138
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Helper function to find vtxos that were spent in a settlement
|
|
103
|
+
*/
|
|
104
|
+
function findVtxosSpentInSettlement(vtxos, vtxo) {
|
|
105
|
+
if (vtxo.virtualStatus.state === "preconfirmed") {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
return vtxos.filter((v) => {
|
|
109
|
+
if (!v.settledBy)
|
|
110
|
+
return false;
|
|
111
|
+
return (vtxo.virtualStatus.commitmentTxIds?.includes(v.settledBy) ?? false);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Helper function to find vtxos that were spent in a payment
|
|
116
|
+
*/
|
|
117
|
+
function findVtxosSpentInPayment(vtxos, vtxo) {
|
|
118
|
+
return vtxos.filter((v) => {
|
|
119
|
+
if (!v.arkTxId)
|
|
120
|
+
return false;
|
|
121
|
+
return v.arkTxId === vtxo.txid;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Helper function to find vtxos that resulted from a spentBy transaction
|
|
126
|
+
*/
|
|
127
|
+
function findVtxosResultedFromTxid(vtxos, txid) {
|
|
128
|
+
return vtxos.filter((v) => {
|
|
129
|
+
if (v.virtualStatus.state !== "preconfirmed" &&
|
|
130
|
+
v.virtualStatus.commitmentTxIds?.includes(txid)) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
return v.txid === txid;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Helper function to reduce vtxos to their total amount
|
|
138
|
+
*/
|
|
139
|
+
function reduceVtxosAmount(vtxos) {
|
|
140
|
+
return vtxos.reduce((sum, v) => sum + v.value, 0);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Helper function to get a vtxo from a list of vtxos
|
|
144
|
+
*/
|
|
145
|
+
function getVtxo(resultedVtxos, spentVtxos) {
|
|
146
|
+
if (resultedVtxos.length === 0) {
|
|
147
|
+
return spentVtxos[0];
|
|
148
|
+
}
|
|
149
|
+
return resultedVtxos[0];
|
|
150
|
+
}
|
|
139
151
|
function removeVtxosFromList(vtxos, vtxosToRemove) {
|
|
140
152
|
return vtxos.filter((v) => {
|
|
141
153
|
for (const vtxoToRemove of vtxosToRemove) {
|
|
@@ -13,6 +13,11 @@ class TxWeightEstimator {
|
|
|
13
13
|
static create() {
|
|
14
14
|
return new TxWeightEstimator(false, 0, 0, 0, 0, 0);
|
|
15
15
|
}
|
|
16
|
+
addP2AInput() {
|
|
17
|
+
this.inputCount++;
|
|
18
|
+
this.inputSize += TxWeightEstimator.INPUT_SIZE;
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
16
21
|
addKeySpendInput(isDefault = true) {
|
|
17
22
|
this.inputCount++;
|
|
18
23
|
this.inputWitnessSize += 64 + 1 + (isDefault ? 0 : 1);
|
|
@@ -48,6 +53,12 @@ class TxWeightEstimator {
|
|
|
48
53
|
TxWeightEstimator.OUTPUT_SIZE + TxWeightEstimator.P2WKH_OUTPUT_SIZE;
|
|
49
54
|
return this;
|
|
50
55
|
}
|
|
56
|
+
addP2TROutput() {
|
|
57
|
+
this.outputCount++;
|
|
58
|
+
this.outputSize +=
|
|
59
|
+
TxWeightEstimator.OUTPUT_SIZE + TxWeightEstimator.P2TR_OUTPUT_SIZE;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
51
62
|
vsize() {
|
|
52
63
|
const getVarIntSize = (n) => {
|
|
53
64
|
if (n < 0xfd)
|
|
@@ -86,6 +97,7 @@ TxWeightEstimator.P2WKH_OUTPUT_SIZE = 1 + 1 + 20;
|
|
|
86
97
|
TxWeightEstimator.BASE_TX_SIZE = 8 + 2; // Version + LockTime
|
|
87
98
|
TxWeightEstimator.WITNESS_HEADER_SIZE = 2; // Flag + Marker
|
|
88
99
|
TxWeightEstimator.WITNESS_SCALE_FACTOR = 4;
|
|
100
|
+
TxWeightEstimator.P2TR_OUTPUT_SIZE = 1 + 1 + 32;
|
|
89
101
|
const vsize = (weight) => {
|
|
90
102
|
const value = BigInt(Math.ceil(weight / TxWeightEstimator.WITNESS_SCALE_FACTOR));
|
|
91
103
|
return {
|