@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,189 @@
|
|
|
1
|
+
import { Transaction } from "@scure/btc-signer";
|
|
2
|
+
import { base64 } from "@scure/base";
|
|
3
|
+
import { hex } from "@scure/base";
|
|
4
|
+
import { sha256x2 } from "@scure/btc-signer/utils";
|
|
5
|
+
/**
|
|
6
|
+
* TxTree is a graph of bitcoin transactions.
|
|
7
|
+
* It is used to represent batch tree created during settlement session
|
|
8
|
+
*/
|
|
9
|
+
export class TxTree {
|
|
10
|
+
constructor(root, children = new Map()) {
|
|
11
|
+
this.root = root;
|
|
12
|
+
this.children = children;
|
|
13
|
+
}
|
|
14
|
+
static create(chunks) {
|
|
15
|
+
if (chunks.length === 0) {
|
|
16
|
+
throw new Error("empty chunks");
|
|
17
|
+
}
|
|
18
|
+
// Create a map to store all chunks by their txid for easy lookup
|
|
19
|
+
const chunksByTxid = new Map();
|
|
20
|
+
for (const chunk of chunks) {
|
|
21
|
+
const decodedChunk = decodeNode(chunk);
|
|
22
|
+
const txid = hex.encode(sha256x2(decodedChunk.tx.toBytes(true)).reverse());
|
|
23
|
+
chunksByTxid.set(txid, decodedChunk);
|
|
24
|
+
}
|
|
25
|
+
// Find the root chunks (the ones that aren't referenced as a child)
|
|
26
|
+
const rootTxids = [];
|
|
27
|
+
for (const [txid] of chunksByTxid) {
|
|
28
|
+
let isChild = false;
|
|
29
|
+
for (const [otherTxid, otherChunk] of chunksByTxid) {
|
|
30
|
+
if (otherTxid === txid) {
|
|
31
|
+
// skip self
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
// check if the current chunk is a child of the other chunk
|
|
35
|
+
isChild = hasChild(otherChunk, txid);
|
|
36
|
+
if (isChild) {
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// if the chunk is not a child of any other chunk, it is a root
|
|
41
|
+
if (!isChild) {
|
|
42
|
+
rootTxids.push(txid);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (rootTxids.length === 0) {
|
|
47
|
+
throw new Error("no root chunk found");
|
|
48
|
+
}
|
|
49
|
+
if (rootTxids.length > 1) {
|
|
50
|
+
throw new Error(`multiple root chunks found: ${rootTxids.join(", ")}`);
|
|
51
|
+
}
|
|
52
|
+
const graph = buildGraph(rootTxids[0], chunksByTxid);
|
|
53
|
+
if (!graph) {
|
|
54
|
+
throw new Error(`chunk not found for root txid: ${rootTxids[0]}`);
|
|
55
|
+
}
|
|
56
|
+
// verify that the number of chunks is equal to the number node in the graph
|
|
57
|
+
if (graph.nbOfNodes() !== chunks.length) {
|
|
58
|
+
throw new Error(`number of chunks (${chunks.length}) is not equal to the number of nodes in the graph (${graph.nbOfNodes()})`);
|
|
59
|
+
}
|
|
60
|
+
return graph;
|
|
61
|
+
}
|
|
62
|
+
nbOfNodes() {
|
|
63
|
+
let count = 1; // count this node
|
|
64
|
+
for (const child of this.children.values()) {
|
|
65
|
+
count += child.nbOfNodes();
|
|
66
|
+
}
|
|
67
|
+
return count;
|
|
68
|
+
}
|
|
69
|
+
validate() {
|
|
70
|
+
if (!this.root) {
|
|
71
|
+
throw new Error("unexpected nil root");
|
|
72
|
+
}
|
|
73
|
+
const nbOfOutputs = this.root.outputsLength;
|
|
74
|
+
const nbOfInputs = this.root.inputsLength;
|
|
75
|
+
if (nbOfInputs !== 1) {
|
|
76
|
+
throw new Error(`unexpected number of inputs: ${nbOfInputs}, expected 1`);
|
|
77
|
+
}
|
|
78
|
+
// the children map can't be bigger than the number of outputs (excluding the P2A)
|
|
79
|
+
// a graph can be "partial" and specify only some of the outputs as children,
|
|
80
|
+
// that's why we allow len(g.Children) to be less than nbOfOutputs-1
|
|
81
|
+
if (this.children.size > nbOfOutputs - 1) {
|
|
82
|
+
throw new Error(`unexpected number of children: ${this.children.size}, expected maximum ${nbOfOutputs - 1}`);
|
|
83
|
+
}
|
|
84
|
+
// validate each child
|
|
85
|
+
for (const [outputIndex, child] of this.children) {
|
|
86
|
+
if (outputIndex >= nbOfOutputs) {
|
|
87
|
+
throw new Error(`output index ${outputIndex} is out of bounds (nb of outputs: ${nbOfOutputs})`);
|
|
88
|
+
}
|
|
89
|
+
child.validate();
|
|
90
|
+
const childInput = child.root.getInput(0);
|
|
91
|
+
const parentTxid = hex.encode(sha256x2(this.root.toBytes(true)).reverse());
|
|
92
|
+
// verify the input of the child is the output of the parent
|
|
93
|
+
if (!childInput.txid ||
|
|
94
|
+
hex.encode(childInput.txid) !== parentTxid ||
|
|
95
|
+
childInput.index !== outputIndex) {
|
|
96
|
+
throw new Error(`input of child ${outputIndex} is not the output of the parent`);
|
|
97
|
+
}
|
|
98
|
+
// verify the sum of the child's outputs is equal to the output of the parent
|
|
99
|
+
let childOutputsSum = 0n;
|
|
100
|
+
for (let i = 0; i < child.root.outputsLength; i++) {
|
|
101
|
+
const output = child.root.getOutput(i);
|
|
102
|
+
if (output?.amount) {
|
|
103
|
+
childOutputsSum += output.amount;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const parentOutput = this.root.getOutput(outputIndex);
|
|
107
|
+
if (!parentOutput?.amount) {
|
|
108
|
+
throw new Error(`parent output ${outputIndex} has no amount`);
|
|
109
|
+
}
|
|
110
|
+
if (childOutputsSum !== parentOutput.amount) {
|
|
111
|
+
throw new Error(`sum of child's outputs is not equal to the output of the parent: ${childOutputsSum} != ${parentOutput.amount}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
leaves() {
|
|
116
|
+
if (this.children.size === 0) {
|
|
117
|
+
return [this.root];
|
|
118
|
+
}
|
|
119
|
+
const leaves = [];
|
|
120
|
+
for (const child of this.children.values()) {
|
|
121
|
+
leaves.push(...child.leaves());
|
|
122
|
+
}
|
|
123
|
+
return leaves;
|
|
124
|
+
}
|
|
125
|
+
get txid() {
|
|
126
|
+
return hex.encode(sha256x2(this.root.toBytes(true)).reverse());
|
|
127
|
+
}
|
|
128
|
+
find(txid) {
|
|
129
|
+
if (txid === this.txid) {
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
for (const child of this.children.values()) {
|
|
133
|
+
const found = child.find(txid);
|
|
134
|
+
if (found) {
|
|
135
|
+
return found;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
update(txid, fn) {
|
|
141
|
+
if (txid === this.txid) {
|
|
142
|
+
fn(this.root);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
for (const child of this.children.values()) {
|
|
146
|
+
try {
|
|
147
|
+
child.update(txid, fn);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
// Continue searching in other children if not found
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
throw new Error(`tx not found: ${txid}`);
|
|
156
|
+
}
|
|
157
|
+
*[Symbol.iterator]() {
|
|
158
|
+
yield this;
|
|
159
|
+
for (const child of this.children.values()) {
|
|
160
|
+
yield* child;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// Helper function to check if a chunk has a specific child
|
|
165
|
+
function hasChild(chunk, childTxid) {
|
|
166
|
+
return Object.values(chunk.children).includes(childTxid);
|
|
167
|
+
}
|
|
168
|
+
// buildGraph recursively builds the TxGraph starting from the given txid
|
|
169
|
+
function buildGraph(rootTxid, chunksByTxid) {
|
|
170
|
+
const chunk = chunksByTxid.get(rootTxid);
|
|
171
|
+
if (!chunk) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
const rootTx = chunk.tx;
|
|
175
|
+
const children = new Map();
|
|
176
|
+
// Recursively build children graphs
|
|
177
|
+
for (const [outputIndexStr, childTxid] of Object.entries(chunk.children)) {
|
|
178
|
+
const outputIndex = parseInt(outputIndexStr);
|
|
179
|
+
const childGraph = buildGraph(childTxid, chunksByTxid);
|
|
180
|
+
if (childGraph) {
|
|
181
|
+
children.set(outputIndex, childGraph);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return new TxTree(rootTx, children);
|
|
185
|
+
}
|
|
186
|
+
function decodeNode(chunk) {
|
|
187
|
+
const tx = Transaction.fromPSBT(base64.decode(chunk.tx));
|
|
188
|
+
return { tx, children: chunk.children };
|
|
189
|
+
}
|
|
@@ -3,177 +3,100 @@ import { Transaction } from "@scure/btc-signer";
|
|
|
3
3
|
import { base64 } from "@scure/base";
|
|
4
4
|
import { sha256x2 } from "@scure/btc-signer/utils";
|
|
5
5
|
import { aggregateKeys } from '../musig2/index.js';
|
|
6
|
-
import {
|
|
7
|
-
export const ErrInvalidSettlementTx = new
|
|
8
|
-
export const ErrInvalidSettlementTxOutputs = new
|
|
9
|
-
export const ErrEmptyTree = new
|
|
10
|
-
export const
|
|
11
|
-
export const
|
|
12
|
-
export const
|
|
13
|
-
export const
|
|
14
|
-
export const
|
|
15
|
-
export const
|
|
16
|
-
export const
|
|
17
|
-
export const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
export const ErrInvalidControlBlock = new TxTreeError("invalid control block");
|
|
24
|
-
export const ErrInvalidRootTransaction = new TxTreeError("invalid root transaction");
|
|
25
|
-
export const ErrInvalidNodeTransaction = new TxTreeError("invalid node transaction");
|
|
26
|
-
const SHARED_OUTPUT_INDEX = 0;
|
|
27
|
-
const CONNECTORS_OUTPUT_INDEX = 1;
|
|
28
|
-
export function validateConnectorsTree(settlementTxB64, connectorsTree) {
|
|
29
|
-
connectorsTree.validate();
|
|
30
|
-
const rootNode = connectorsTree.root();
|
|
31
|
-
if (!rootNode)
|
|
32
|
-
throw ErrEmptyTree;
|
|
33
|
-
const rootTx = Transaction.fromPSBT(base64.decode(rootNode.tx));
|
|
34
|
-
if (rootTx.inputsLength !== 1)
|
|
6
|
+
import { CosignerPublicKey, getArkPsbtFields } from '../utils/unknownFields.js';
|
|
7
|
+
export const ErrInvalidSettlementTx = (tx) => new Error(`invalid settlement transaction: ${tx}`);
|
|
8
|
+
export const ErrInvalidSettlementTxOutputs = new Error("invalid settlement transaction outputs");
|
|
9
|
+
export const ErrEmptyTree = new Error("empty tree");
|
|
10
|
+
export const ErrNumberOfInputs = new Error("invalid number of inputs");
|
|
11
|
+
export const ErrWrongSettlementTxid = new Error("wrong settlement txid");
|
|
12
|
+
export const ErrInvalidAmount = new Error("invalid amount");
|
|
13
|
+
export const ErrNoLeaves = new Error("no leaves");
|
|
14
|
+
export const ErrInvalidTaprootScript = new Error("invalid taproot script");
|
|
15
|
+
export const ErrInvalidRoundTxOutputs = new Error("invalid round transaction outputs");
|
|
16
|
+
export const ErrWrongCommitmentTxid = new Error("wrong commitment txid");
|
|
17
|
+
export const ErrMissingCosignersPublicKeys = new Error("missing cosigners public keys");
|
|
18
|
+
const BATCH_OUTPUT_VTXO_INDEX = 0;
|
|
19
|
+
const BATCH_OUTPUT_CONNECTORS_INDEX = 1;
|
|
20
|
+
export function validateConnectorsTxGraph(settlementTxB64, connectorsGraph) {
|
|
21
|
+
connectorsGraph.validate();
|
|
22
|
+
if (connectorsGraph.root.inputsLength !== 1)
|
|
35
23
|
throw ErrNumberOfInputs;
|
|
36
|
-
const rootInput =
|
|
24
|
+
const rootInput = connectorsGraph.root.getInput(0);
|
|
37
25
|
const settlementTx = Transaction.fromPSBT(base64.decode(settlementTxB64));
|
|
38
|
-
if (settlementTx.outputsLength <=
|
|
26
|
+
if (settlementTx.outputsLength <= BATCH_OUTPUT_CONNECTORS_INDEX)
|
|
39
27
|
throw ErrInvalidSettlementTxOutputs;
|
|
40
28
|
const expectedRootTxid = hex.encode(sha256x2(settlementTx.toBytes(true)).reverse());
|
|
41
29
|
if (!rootInput.txid)
|
|
42
30
|
throw ErrWrongSettlementTxid;
|
|
43
31
|
if (hex.encode(rootInput.txid) !== expectedRootTxid)
|
|
44
32
|
throw ErrWrongSettlementTxid;
|
|
45
|
-
if (rootInput.index !==
|
|
33
|
+
if (rootInput.index !== BATCH_OUTPUT_CONNECTORS_INDEX)
|
|
46
34
|
throw ErrWrongSettlementTxid;
|
|
47
35
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const sharedOutputAmount = sharedOutput.amount;
|
|
65
|
-
const nbNodes = vtxoTree.numberOfNodes();
|
|
66
|
-
if (nbNodes === 0) {
|
|
36
|
+
// ValidateVtxoTxGraph checks if the given vtxo graph is valid.
|
|
37
|
+
// The function validates:
|
|
38
|
+
// - the number of nodes
|
|
39
|
+
// - the number of leaves
|
|
40
|
+
// - children coherence with parent.
|
|
41
|
+
// - every control block and taproot output scripts.
|
|
42
|
+
// - input and output amounts.
|
|
43
|
+
export function validateVtxoTxGraph(graph, roundTransaction, sweepTapTreeRoot) {
|
|
44
|
+
if (roundTransaction.outputsLength < BATCH_OUTPUT_VTXO_INDEX + 1) {
|
|
45
|
+
throw ErrInvalidRoundTxOutputs;
|
|
46
|
+
}
|
|
47
|
+
const batchOutputAmount = roundTransaction.getOutput(BATCH_OUTPUT_VTXO_INDEX)?.amount;
|
|
48
|
+
if (!batchOutputAmount) {
|
|
49
|
+
throw ErrInvalidRoundTxOutputs;
|
|
50
|
+
}
|
|
51
|
+
if (!graph.root) {
|
|
67
52
|
throw ErrEmptyTree;
|
|
68
53
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
rootTx = Transaction.fromPSBT(base64.decode(rootNode.tx));
|
|
54
|
+
const rootInput = graph.root.getInput(0);
|
|
55
|
+
const commitmentTxid = hex.encode(sha256x2(roundTransaction.toBytes(true)).reverse());
|
|
56
|
+
if (!rootInput.txid ||
|
|
57
|
+
hex.encode(rootInput.txid) !== commitmentTxid ||
|
|
58
|
+
rootInput.index !== BATCH_OUTPUT_VTXO_INDEX) {
|
|
59
|
+
throw ErrWrongCommitmentTxid;
|
|
77
60
|
}
|
|
78
|
-
catch {
|
|
79
|
-
throw ErrInvalidRootTransaction;
|
|
80
|
-
}
|
|
81
|
-
if (rootTx.inputsLength !== 1) {
|
|
82
|
-
throw ErrNumberOfInputs;
|
|
83
|
-
}
|
|
84
|
-
const rootInput = rootTx.getInput(0);
|
|
85
|
-
if (!rootInput.txid || rootInput.index === undefined)
|
|
86
|
-
throw ErrWrongSettlementTxid;
|
|
87
|
-
const settlementTxid = hex.encode(sha256x2(settlementTransaction.toBytes(true)).reverse());
|
|
88
|
-
if (hex.encode(rootInput.txid) !== settlementTxid ||
|
|
89
|
-
rootInput.index !== SHARED_OUTPUT_INDEX) {
|
|
90
|
-
throw ErrWrongSettlementTxid;
|
|
91
|
-
}
|
|
92
|
-
// Check root output amounts
|
|
93
61
|
let sumRootValue = 0n;
|
|
94
|
-
for (let i = 0; i <
|
|
95
|
-
const output =
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
|
|
62
|
+
for (let i = 0; i < graph.root.outputsLength; i++) {
|
|
63
|
+
const output = graph.root.getOutput(i);
|
|
64
|
+
if (output?.amount) {
|
|
65
|
+
sumRootValue += output.amount;
|
|
66
|
+
}
|
|
99
67
|
}
|
|
100
|
-
if (sumRootValue
|
|
68
|
+
if (sumRootValue !== batchOutputAmount) {
|
|
101
69
|
throw ErrInvalidAmount;
|
|
102
70
|
}
|
|
103
|
-
|
|
71
|
+
const leaves = graph.leaves();
|
|
72
|
+
if (leaves.length === 0) {
|
|
104
73
|
throw ErrNoLeaves;
|
|
105
74
|
}
|
|
106
|
-
//
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
if (tx.inputsLength !== 1) {
|
|
133
|
-
throw ErrNumberOfInputs;
|
|
134
|
-
}
|
|
135
|
-
const input = tx.getInput(0);
|
|
136
|
-
if (!input.txid)
|
|
137
|
-
throw ErrParentTxidInput;
|
|
138
|
-
if (hex.encode(input.txid) !== node.parentTxid) {
|
|
139
|
-
throw ErrParentTxidInput;
|
|
140
|
-
}
|
|
141
|
-
const children = vtxoTree.children(node.txid);
|
|
142
|
-
if (node.leaf && children.length >= 1) {
|
|
143
|
-
throw ErrLeafChildren;
|
|
144
|
-
}
|
|
145
|
-
// Validate each child
|
|
146
|
-
for (let childIndex = 0; childIndex < children.length; childIndex++) {
|
|
147
|
-
const child = children[childIndex];
|
|
148
|
-
const childTx = Transaction.fromPSBT(base64.decode(child.tx));
|
|
149
|
-
const parentOutput = tx.getOutput(childIndex);
|
|
150
|
-
if (!parentOutput?.script)
|
|
151
|
-
throw ErrInvalidTaprootScript;
|
|
152
|
-
const previousScriptKey = parentOutput.script.slice(2);
|
|
153
|
-
if (previousScriptKey.length !== 32) {
|
|
154
|
-
throw ErrInvalidTaprootScript;
|
|
155
|
-
}
|
|
156
|
-
// Get cosigner keys from input
|
|
157
|
-
const cosignerKeys = getCosignerKeys(childTx);
|
|
158
|
-
// Aggregate keys
|
|
159
|
-
const { finalKey } = aggregateKeys(cosignerKeys, true, {
|
|
160
|
-
taprootTweak: tapTreeRoot,
|
|
161
|
-
});
|
|
162
|
-
if (hex.encode(finalKey) !== hex.encode(previousScriptKey.slice(2))) {
|
|
163
|
-
throw ErrInternalKey;
|
|
164
|
-
}
|
|
165
|
-
// Check amounts
|
|
166
|
-
let sumChildAmount = 0n;
|
|
167
|
-
for (let i = 0; i < childTx.outputsLength; i++) {
|
|
168
|
-
const output = childTx.getOutput(i);
|
|
169
|
-
if (!output?.amount)
|
|
170
|
-
continue;
|
|
171
|
-
sumChildAmount += output.amount;
|
|
172
|
-
}
|
|
173
|
-
if (!parentOutput.amount)
|
|
174
|
-
throw ErrInvalidAmount;
|
|
175
|
-
if (sumChildAmount >= parentOutput.amount) {
|
|
176
|
-
throw ErrInvalidAmount;
|
|
75
|
+
// validate the graph structure
|
|
76
|
+
graph.validate();
|
|
77
|
+
// iterates over all the nodes of the graph to verify that cosigners public keys are corresponding to the parent output
|
|
78
|
+
for (const g of graph) {
|
|
79
|
+
for (const [childIndex, child] of g.children) {
|
|
80
|
+
const parentOutput = g.root.getOutput(childIndex);
|
|
81
|
+
if (!parentOutput?.script) {
|
|
82
|
+
throw new Error(`parent output ${childIndex} not found`);
|
|
83
|
+
}
|
|
84
|
+
const previousScriptKey = parentOutput.script.slice(2);
|
|
85
|
+
if (previousScriptKey.length !== 32) {
|
|
86
|
+
throw new Error(`parent output ${childIndex} has invalid script`);
|
|
87
|
+
}
|
|
88
|
+
const cosigners = getArkPsbtFields(child.root, 0, CosignerPublicKey);
|
|
89
|
+
if (cosigners.length === 0) {
|
|
90
|
+
throw ErrMissingCosignersPublicKeys;
|
|
91
|
+
}
|
|
92
|
+
const cosignerKeys = cosigners.map((c) => c.key);
|
|
93
|
+
const { finalKey } = aggregateKeys(cosignerKeys, true, {
|
|
94
|
+
taprootTweak: sweepTapTreeRoot,
|
|
95
|
+
});
|
|
96
|
+
if (!finalKey ||
|
|
97
|
+
hex.encode(finalKey.slice(1)) !== hex.encode(previousScriptKey)) {
|
|
98
|
+
throw ErrInvalidTaprootScript;
|
|
99
|
+
}
|
|
177
100
|
}
|
|
178
101
|
}
|
|
179
102
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { hex } from "@scure/base";
|
|
2
|
+
export const ANCHOR_VALUE = 0n;
|
|
3
|
+
export const ANCHOR_PKSCRIPT = new Uint8Array([0x51, 0x02, 0x4e, 0x73]);
|
|
4
|
+
/**
|
|
5
|
+
* A zero-value anchor output.
|
|
6
|
+
*/
|
|
7
|
+
export const P2A = {
|
|
8
|
+
script: ANCHOR_PKSCRIPT,
|
|
9
|
+
amount: ANCHOR_VALUE,
|
|
10
|
+
};
|
|
11
|
+
const hexP2Ascript = hex.encode(P2A.script);
|
|
12
|
+
/**
|
|
13
|
+
* search for anchor in the given transaction.
|
|
14
|
+
* @throws {Error} if the anchor is not found or has the wrong amount
|
|
15
|
+
*/
|
|
16
|
+
export function findP2AOutput(tx) {
|
|
17
|
+
for (let i = 0; i < tx.outputsLength; i++) {
|
|
18
|
+
const output = tx.getOutput(i);
|
|
19
|
+
if (output.script && hex.encode(output.script) === hexP2Ascript) {
|
|
20
|
+
if (output.amount !== P2A.amount) {
|
|
21
|
+
throw new Error(`P2A output has wrong amount, expected ${P2A.amount} got ${output.amount}`);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
txid: tx.id,
|
|
25
|
+
index: i,
|
|
26
|
+
witnessUtxo: P2A,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
throw new Error("P2A output not found");
|
|
31
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { DEFAULT_SEQUENCE, Transaction } from "@scure/btc-signer";
|
|
2
|
+
import { CLTVMultisigTapscript, decodeTapscript } from '../script/tapscript.js';
|
|
3
|
+
import { scriptFromTapLeafScript, VtxoScript, } from '../script/base.js';
|
|
4
|
+
import { P2A } from './anchor.js';
|
|
5
|
+
import { hex } from "@scure/base";
|
|
6
|
+
import { sha256x2 } from "@scure/btc-signer/utils";
|
|
7
|
+
import { setArkPsbtField, VtxoTaprootTree } from './unknownFields.js';
|
|
8
|
+
/**
|
|
9
|
+
* Builds an offchain transaction with checkpoint transactions.
|
|
10
|
+
*
|
|
11
|
+
* Creates one checkpoint transaction per input and a virtual transaction that
|
|
12
|
+
* combines all the checkpoints, sending to the specified outputs. This is the
|
|
13
|
+
* core function for creating Ark transactions.
|
|
14
|
+
*
|
|
15
|
+
* @param inputs - Array of virtual transaction inputs
|
|
16
|
+
* @param outputs - Array of transaction outputs
|
|
17
|
+
* @param serverUnrollScript - Server unroll script for checkpoint transactions
|
|
18
|
+
* @returns Object containing the virtual transaction and checkpoint transactions
|
|
19
|
+
*/
|
|
20
|
+
export function buildOffchainTx(inputs, outputs, serverUnrollScript) {
|
|
21
|
+
const checkpoints = inputs.map((input) => buildCheckpointTx(input, serverUnrollScript));
|
|
22
|
+
const arkTx = buildVirtualTx(checkpoints.map((c) => c.input), outputs);
|
|
23
|
+
return {
|
|
24
|
+
arkTx,
|
|
25
|
+
checkpoints: checkpoints.map((c) => c.tx),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function buildVirtualTx(inputs, outputs) {
|
|
29
|
+
let lockTime = 0n;
|
|
30
|
+
for (const input of inputs) {
|
|
31
|
+
const tapscript = decodeTapscript(scriptFromTapLeafScript(input.tapLeafScript));
|
|
32
|
+
if (CLTVMultisigTapscript.is(tapscript)) {
|
|
33
|
+
if (lockTime !== 0n) {
|
|
34
|
+
// if a locktime is already set, check if the new locktime is in the same unit
|
|
35
|
+
if (isSeconds(lockTime) !==
|
|
36
|
+
isSeconds(tapscript.params.absoluteTimelock)) {
|
|
37
|
+
throw new Error("cannot mix seconds and blocks locktime");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (tapscript.params.absoluteTimelock > lockTime) {
|
|
41
|
+
lockTime = tapscript.params.absoluteTimelock;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const tx = new Transaction({
|
|
46
|
+
version: 3,
|
|
47
|
+
allowUnknown: true,
|
|
48
|
+
allowUnknownOutputs: true,
|
|
49
|
+
lockTime: Number(lockTime),
|
|
50
|
+
});
|
|
51
|
+
for (const [i, input] of inputs.entries()) {
|
|
52
|
+
tx.addInput({
|
|
53
|
+
txid: input.txid,
|
|
54
|
+
index: input.vout,
|
|
55
|
+
sequence: lockTime ? DEFAULT_SEQUENCE - 1 : undefined,
|
|
56
|
+
witnessUtxo: {
|
|
57
|
+
script: VtxoScript.decode(input.tapTree).pkScript,
|
|
58
|
+
amount: BigInt(input.value),
|
|
59
|
+
},
|
|
60
|
+
tapLeafScript: [input.tapLeafScript],
|
|
61
|
+
});
|
|
62
|
+
setArkPsbtField(tx, i, VtxoTaprootTree, input.tapTree);
|
|
63
|
+
}
|
|
64
|
+
for (const output of outputs) {
|
|
65
|
+
tx.addOutput(output);
|
|
66
|
+
}
|
|
67
|
+
// add the anchor output
|
|
68
|
+
tx.addOutput(P2A);
|
|
69
|
+
return tx;
|
|
70
|
+
}
|
|
71
|
+
function buildCheckpointTx(vtxo, serverUnrollScript) {
|
|
72
|
+
// create the checkpoint vtxo script from collaborative closure
|
|
73
|
+
const collaborativeClosure = decodeTapscript(vtxo.checkpointTapLeafScript ??
|
|
74
|
+
scriptFromTapLeafScript(vtxo.tapLeafScript));
|
|
75
|
+
// create the checkpoint vtxo script combining collaborative closure and server unroll script
|
|
76
|
+
const checkpointVtxoScript = new VtxoScript([
|
|
77
|
+
serverUnrollScript.script,
|
|
78
|
+
collaborativeClosure.script,
|
|
79
|
+
]);
|
|
80
|
+
// build the checkpoint virtual tx
|
|
81
|
+
const checkpointTx = buildVirtualTx([vtxo], [
|
|
82
|
+
{
|
|
83
|
+
amount: BigInt(vtxo.value),
|
|
84
|
+
script: checkpointVtxoScript.pkScript,
|
|
85
|
+
},
|
|
86
|
+
]);
|
|
87
|
+
// get the collaborative leaf proof
|
|
88
|
+
const collaborativeLeafProof = checkpointVtxoScript.findLeaf(hex.encode(collaborativeClosure.script));
|
|
89
|
+
// create the checkpoint input that will be used as input of the virtual tx
|
|
90
|
+
const checkpointInput = {
|
|
91
|
+
txid: hex.encode(sha256x2(checkpointTx.toBytes(true)).reverse()),
|
|
92
|
+
vout: 0,
|
|
93
|
+
value: vtxo.value,
|
|
94
|
+
tapLeafScript: collaborativeLeafProof,
|
|
95
|
+
tapTree: checkpointVtxoScript.encode(),
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
tx: checkpointTx,
|
|
99
|
+
input: checkpointInput,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const nLocktimeMinSeconds = 500000000n;
|
|
103
|
+
function isSeconds(locktime) {
|
|
104
|
+
return locktime >= nLocktimeMinSeconds;
|
|
105
|
+
}
|