@arkade-os/sdk 0.1.4 → 0.2.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 +157 -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 +43 -17
- package/dist/cjs/providers/ark.js +261 -321
- 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 +61 -20
- package/dist/cjs/script/vhtlc.js +85 -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 +61 -34
- package/dist/cjs/wallet/serviceWorker/worker.js +120 -108
- package/dist/cjs/wallet/unroll.js +270 -0
- package/dist/cjs/wallet/wallet.js +701 -454
- 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 +31 -12
- package/dist/esm/providers/ark.js +259 -320
- 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 +61 -20
- package/dist/esm/script/vhtlc.js +85 -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 +62 -35
- package/dist/esm/wallet/serviceWorker/worker.js +120 -108
- package/dist/esm/wallet/unroll.js +267 -0
- package/dist/esm/wallet/wallet.js +674 -461
- 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 +24 -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 -25
- package/package.json +37 -35
- 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
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TxTree = void 0;
|
|
4
|
+
const btc_signer_1 = require("@scure/btc-signer");
|
|
5
|
+
const base_1 = require("@scure/base");
|
|
6
|
+
const base_2 = require("@scure/base");
|
|
7
|
+
const utils_1 = require("@scure/btc-signer/utils");
|
|
8
|
+
/**
|
|
9
|
+
* TxTree is a graph of bitcoin transactions.
|
|
10
|
+
* It is used to represent batch tree created during settlement session
|
|
11
|
+
*/
|
|
12
|
+
class TxTree {
|
|
13
|
+
constructor(root, children = new Map()) {
|
|
14
|
+
this.root = root;
|
|
15
|
+
this.children = children;
|
|
16
|
+
}
|
|
17
|
+
static create(chunks) {
|
|
18
|
+
if (chunks.length === 0) {
|
|
19
|
+
throw new Error("empty chunks");
|
|
20
|
+
}
|
|
21
|
+
// Create a map to store all chunks by their txid for easy lookup
|
|
22
|
+
const chunksByTxid = new Map();
|
|
23
|
+
for (const chunk of chunks) {
|
|
24
|
+
const decodedChunk = decodeNode(chunk);
|
|
25
|
+
const txid = base_2.hex.encode((0, utils_1.sha256x2)(decodedChunk.tx.toBytes(true)).reverse());
|
|
26
|
+
chunksByTxid.set(txid, decodedChunk);
|
|
27
|
+
}
|
|
28
|
+
// Find the root chunks (the ones that aren't referenced as a child)
|
|
29
|
+
const rootTxids = [];
|
|
30
|
+
for (const [txid] of chunksByTxid) {
|
|
31
|
+
let isChild = false;
|
|
32
|
+
for (const [otherTxid, otherChunk] of chunksByTxid) {
|
|
33
|
+
if (otherTxid === txid) {
|
|
34
|
+
// skip self
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
// check if the current chunk is a child of the other chunk
|
|
38
|
+
isChild = hasChild(otherChunk, txid);
|
|
39
|
+
if (isChild) {
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// if the chunk is not a child of any other chunk, it is a root
|
|
44
|
+
if (!isChild) {
|
|
45
|
+
rootTxids.push(txid);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (rootTxids.length === 0) {
|
|
50
|
+
throw new Error("no root chunk found");
|
|
51
|
+
}
|
|
52
|
+
if (rootTxids.length > 1) {
|
|
53
|
+
throw new Error(`multiple root chunks found: ${rootTxids.join(", ")}`);
|
|
54
|
+
}
|
|
55
|
+
const graph = buildGraph(rootTxids[0], chunksByTxid);
|
|
56
|
+
if (!graph) {
|
|
57
|
+
throw new Error(`chunk not found for root txid: ${rootTxids[0]}`);
|
|
58
|
+
}
|
|
59
|
+
// verify that the number of chunks is equal to the number node in the graph
|
|
60
|
+
if (graph.nbOfNodes() !== chunks.length) {
|
|
61
|
+
throw new Error(`number of chunks (${chunks.length}) is not equal to the number of nodes in the graph (${graph.nbOfNodes()})`);
|
|
62
|
+
}
|
|
63
|
+
return graph;
|
|
64
|
+
}
|
|
65
|
+
nbOfNodes() {
|
|
66
|
+
let count = 1; // count this node
|
|
67
|
+
for (const child of this.children.values()) {
|
|
68
|
+
count += child.nbOfNodes();
|
|
69
|
+
}
|
|
70
|
+
return count;
|
|
71
|
+
}
|
|
72
|
+
validate() {
|
|
73
|
+
if (!this.root) {
|
|
74
|
+
throw new Error("unexpected nil root");
|
|
75
|
+
}
|
|
76
|
+
const nbOfOutputs = this.root.outputsLength;
|
|
77
|
+
const nbOfInputs = this.root.inputsLength;
|
|
78
|
+
if (nbOfInputs !== 1) {
|
|
79
|
+
throw new Error(`unexpected number of inputs: ${nbOfInputs}, expected 1`);
|
|
80
|
+
}
|
|
81
|
+
// the children map can't be bigger than the number of outputs (excluding the P2A)
|
|
82
|
+
// a graph can be "partial" and specify only some of the outputs as children,
|
|
83
|
+
// that's why we allow len(g.Children) to be less than nbOfOutputs-1
|
|
84
|
+
if (this.children.size > nbOfOutputs - 1) {
|
|
85
|
+
throw new Error(`unexpected number of children: ${this.children.size}, expected maximum ${nbOfOutputs - 1}`);
|
|
86
|
+
}
|
|
87
|
+
// validate each child
|
|
88
|
+
for (const [outputIndex, child] of this.children) {
|
|
89
|
+
if (outputIndex >= nbOfOutputs) {
|
|
90
|
+
throw new Error(`output index ${outputIndex} is out of bounds (nb of outputs: ${nbOfOutputs})`);
|
|
91
|
+
}
|
|
92
|
+
child.validate();
|
|
93
|
+
const childInput = child.root.getInput(0);
|
|
94
|
+
const parentTxid = base_2.hex.encode((0, utils_1.sha256x2)(this.root.toBytes(true)).reverse());
|
|
95
|
+
// verify the input of the child is the output of the parent
|
|
96
|
+
if (!childInput.txid ||
|
|
97
|
+
base_2.hex.encode(childInput.txid) !== parentTxid ||
|
|
98
|
+
childInput.index !== outputIndex) {
|
|
99
|
+
throw new Error(`input of child ${outputIndex} is not the output of the parent`);
|
|
100
|
+
}
|
|
101
|
+
// verify the sum of the child's outputs is equal to the output of the parent
|
|
102
|
+
let childOutputsSum = 0n;
|
|
103
|
+
for (let i = 0; i < child.root.outputsLength; i++) {
|
|
104
|
+
const output = child.root.getOutput(i);
|
|
105
|
+
if (output?.amount) {
|
|
106
|
+
childOutputsSum += output.amount;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const parentOutput = this.root.getOutput(outputIndex);
|
|
110
|
+
if (!parentOutput?.amount) {
|
|
111
|
+
throw new Error(`parent output ${outputIndex} has no amount`);
|
|
112
|
+
}
|
|
113
|
+
if (childOutputsSum !== parentOutput.amount) {
|
|
114
|
+
throw new Error(`sum of child's outputs is not equal to the output of the parent: ${childOutputsSum} != ${parentOutput.amount}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
leaves() {
|
|
119
|
+
if (this.children.size === 0) {
|
|
120
|
+
return [this.root];
|
|
121
|
+
}
|
|
122
|
+
const leaves = [];
|
|
123
|
+
for (const child of this.children.values()) {
|
|
124
|
+
leaves.push(...child.leaves());
|
|
125
|
+
}
|
|
126
|
+
return leaves;
|
|
127
|
+
}
|
|
128
|
+
get txid() {
|
|
129
|
+
return base_2.hex.encode((0, utils_1.sha256x2)(this.root.toBytes(true)).reverse());
|
|
130
|
+
}
|
|
131
|
+
find(txid) {
|
|
132
|
+
if (txid === this.txid) {
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
for (const child of this.children.values()) {
|
|
136
|
+
const found = child.find(txid);
|
|
137
|
+
if (found) {
|
|
138
|
+
return found;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
update(txid, fn) {
|
|
144
|
+
if (txid === this.txid) {
|
|
145
|
+
fn(this.root);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
for (const child of this.children.values()) {
|
|
149
|
+
try {
|
|
150
|
+
child.update(txid, fn);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
// Continue searching in other children if not found
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
throw new Error(`tx not found: ${txid}`);
|
|
159
|
+
}
|
|
160
|
+
*[Symbol.iterator]() {
|
|
161
|
+
yield this;
|
|
162
|
+
for (const child of this.children.values()) {
|
|
163
|
+
yield* child;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
exports.TxTree = TxTree;
|
|
168
|
+
// Helper function to check if a chunk has a specific child
|
|
169
|
+
function hasChild(chunk, childTxid) {
|
|
170
|
+
return Object.values(chunk.children).includes(childTxid);
|
|
171
|
+
}
|
|
172
|
+
// buildGraph recursively builds the TxGraph starting from the given txid
|
|
173
|
+
function buildGraph(rootTxid, chunksByTxid) {
|
|
174
|
+
const chunk = chunksByTxid.get(rootTxid);
|
|
175
|
+
if (!chunk) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
const rootTx = chunk.tx;
|
|
179
|
+
const children = new Map();
|
|
180
|
+
// Recursively build children graphs
|
|
181
|
+
for (const [outputIndexStr, childTxid] of Object.entries(chunk.children)) {
|
|
182
|
+
const outputIndex = parseInt(outputIndexStr);
|
|
183
|
+
const childGraph = buildGraph(childTxid, chunksByTxid);
|
|
184
|
+
if (childGraph) {
|
|
185
|
+
children.set(outputIndex, childGraph);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return new TxTree(rootTx, children);
|
|
189
|
+
}
|
|
190
|
+
function decodeNode(chunk) {
|
|
191
|
+
const tx = btc_signer_1.Transaction.fromPSBT(base_1.base64.decode(chunk.tx));
|
|
192
|
+
return { tx, children: chunk.children };
|
|
193
|
+
}
|
|
@@ -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
|
+
}
|