@arkade-os/sdk 0.1.4 → 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 -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 +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 -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 +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 -25
- 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
|
@@ -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
|
+
}
|
|
@@ -1,55 +1,11 @@
|
|
|
1
1
|
import { TxType } from '../wallet/index.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return [];
|
|
8
|
-
}
|
|
9
|
-
return vtxos.filter((v) => {
|
|
10
|
-
if (!v.spentBy)
|
|
11
|
-
return false;
|
|
12
|
-
return v.spentBy === vtxo.virtualStatus.batchTxID;
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Helper function to find vtxos that were spent in a payment
|
|
17
|
-
*/
|
|
18
|
-
function findVtxosSpentInPayment(vtxos, vtxo) {
|
|
19
|
-
return vtxos.filter((v) => {
|
|
20
|
-
if (!v.spentBy)
|
|
21
|
-
return false;
|
|
22
|
-
return v.spentBy === vtxo.txid;
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Helper function to find vtxos that resulted from a spentBy transaction
|
|
27
|
-
*/
|
|
28
|
-
function findVtxosResultedFromSpentBy(vtxos, spentBy) {
|
|
29
|
-
return vtxos.filter((v) => {
|
|
30
|
-
if (v.virtualStatus.state !== "pending" &&
|
|
31
|
-
v.virtualStatus.batchTxID === spentBy) {
|
|
32
|
-
return true;
|
|
33
|
-
}
|
|
34
|
-
return v.txid === spentBy;
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Helper function to reduce vtxos to their total amount
|
|
3
|
+
* @param spendable - Vtxos that are spendable
|
|
4
|
+
* @param spent - Vtxos that are spent
|
|
5
|
+
* @param boardingBatchTxids - Set of boarding batch txids
|
|
6
|
+
* @returns Ark transactions
|
|
39
7
|
*/
|
|
40
|
-
function
|
|
41
|
-
return vtxos.reduce((sum, v) => sum + v.value, 0);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Helper function to get a vtxo from a list of vtxos
|
|
45
|
-
*/
|
|
46
|
-
function getVtxo(resultedVtxos, spentVtxos) {
|
|
47
|
-
if (resultedVtxos.length === 0) {
|
|
48
|
-
return spentVtxos[0];
|
|
49
|
-
}
|
|
50
|
-
return resultedVtxos[0];
|
|
51
|
-
}
|
|
52
|
-
export function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
8
|
+
export function vtxosToTxs(spendable, spent, boardingBatchTxids) {
|
|
53
9
|
const txs = [];
|
|
54
10
|
// Receive case
|
|
55
11
|
// All vtxos are received unless:
|
|
@@ -57,8 +13,9 @@ export function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
57
13
|
// - they are the change of a spend tx
|
|
58
14
|
let vtxosLeftToCheck = [...spent];
|
|
59
15
|
for (const vtxo of [...spendable, ...spent]) {
|
|
60
|
-
if (vtxo.virtualStatus.state !== "
|
|
61
|
-
|
|
16
|
+
if (vtxo.virtualStatus.state !== "preconfirmed" &&
|
|
17
|
+
vtxo.virtualStatus.commitmentTxIds &&
|
|
18
|
+
vtxo.virtualStatus.commitmentTxIds.some((txid) => boardingBatchTxids.has(txid))) {
|
|
62
19
|
continue;
|
|
63
20
|
}
|
|
64
21
|
const settleVtxos = findVtxosSpentInSettlement(vtxosLeftToCheck, vtxo);
|
|
@@ -74,13 +31,13 @@ export function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
74
31
|
continue; // settlement or change, ignore
|
|
75
32
|
}
|
|
76
33
|
const txKey = {
|
|
77
|
-
|
|
34
|
+
commitmentTxid: vtxo.spentBy || "",
|
|
78
35
|
boardingTxid: "",
|
|
79
|
-
|
|
36
|
+
arkTxid: "",
|
|
80
37
|
};
|
|
81
|
-
let settled = vtxo.virtualStatus.state !== "
|
|
82
|
-
if (vtxo.virtualStatus.state === "
|
|
83
|
-
txKey.
|
|
38
|
+
let settled = vtxo.virtualStatus.state !== "preconfirmed";
|
|
39
|
+
if (vtxo.virtualStatus.state === "preconfirmed") {
|
|
40
|
+
txKey.arkTxid = vtxo.txid;
|
|
84
41
|
if (vtxo.spentBy) {
|
|
85
42
|
settled = true;
|
|
86
43
|
}
|
|
@@ -93,22 +50,27 @@ export function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
93
50
|
settled,
|
|
94
51
|
});
|
|
95
52
|
}
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
// - they are settlements
|
|
99
|
-
// aggregate spent by spentId
|
|
100
|
-
const vtxosBySpentBy = new Map();
|
|
53
|
+
// vtxos by settled by or ark txid
|
|
54
|
+
const vtxosByTxid = new Map();
|
|
101
55
|
for (const v of spent) {
|
|
102
|
-
if (
|
|
56
|
+
if (v.settledBy) {
|
|
57
|
+
if (!vtxosByTxid.has(v.settledBy)) {
|
|
58
|
+
vtxosByTxid.set(v.settledBy, []);
|
|
59
|
+
}
|
|
60
|
+
const currentVtxos = vtxosByTxid.get(v.settledBy);
|
|
61
|
+
vtxosByTxid.set(v.settledBy, [...currentVtxos, v]);
|
|
62
|
+
}
|
|
63
|
+
if (!v.arkTxId) {
|
|
103
64
|
continue;
|
|
104
|
-
if (!vtxosBySpentBy.has(v.spentBy)) {
|
|
105
|
-
vtxosBySpentBy.set(v.spentBy, []);
|
|
106
65
|
}
|
|
107
|
-
|
|
108
|
-
|
|
66
|
+
if (!vtxosByTxid.has(v.arkTxId)) {
|
|
67
|
+
vtxosByTxid.set(v.arkTxId, []);
|
|
68
|
+
}
|
|
69
|
+
const currentVtxos = vtxosByTxid.get(v.arkTxId);
|
|
70
|
+
vtxosByTxid.set(v.arkTxId, [...currentVtxos, v]);
|
|
109
71
|
}
|
|
110
|
-
for (const [sb, vtxos] of
|
|
111
|
-
const resultedVtxos =
|
|
72
|
+
for (const [sb, vtxos] of vtxosByTxid) {
|
|
73
|
+
const resultedVtxos = findVtxosResultedFromTxid([...spendable, ...spent], sb);
|
|
112
74
|
const resultedAmount = reduceVtxosAmount(resultedVtxos);
|
|
113
75
|
const spentAmount = reduceVtxosAmount(vtxos);
|
|
114
76
|
if (spentAmount <= resultedAmount) {
|
|
@@ -116,12 +78,12 @@ export function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
116
78
|
}
|
|
117
79
|
const vtxo = getVtxo(resultedVtxos, vtxos);
|
|
118
80
|
const txKey = {
|
|
119
|
-
|
|
81
|
+
commitmentTxid: vtxo.virtualStatus.commitmentTxIds?.[0] || "",
|
|
120
82
|
boardingTxid: "",
|
|
121
|
-
|
|
83
|
+
arkTxid: "",
|
|
122
84
|
};
|
|
123
|
-
if (vtxo.virtualStatus.state === "
|
|
124
|
-
txKey.
|
|
85
|
+
if (vtxo.virtualStatus.state === "preconfirmed") {
|
|
86
|
+
txKey.arkTxid = vtxo.txid;
|
|
125
87
|
}
|
|
126
88
|
txs.push({
|
|
127
89
|
key: txKey,
|
|
@@ -133,6 +95,56 @@ export function vtxosToTxs(spendable, spent, boardingRounds) {
|
|
|
133
95
|
}
|
|
134
96
|
return txs;
|
|
135
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* Helper function to find vtxos that were spent in a settlement
|
|
100
|
+
*/
|
|
101
|
+
function findVtxosSpentInSettlement(vtxos, vtxo) {
|
|
102
|
+
if (vtxo.virtualStatus.state === "preconfirmed") {
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
return vtxos.filter((v) => {
|
|
106
|
+
if (!v.settledBy)
|
|
107
|
+
return false;
|
|
108
|
+
return (vtxo.virtualStatus.commitmentTxIds?.includes(v.settledBy) ?? false);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Helper function to find vtxos that were spent in a payment
|
|
113
|
+
*/
|
|
114
|
+
function findVtxosSpentInPayment(vtxos, vtxo) {
|
|
115
|
+
return vtxos.filter((v) => {
|
|
116
|
+
if (!v.arkTxId)
|
|
117
|
+
return false;
|
|
118
|
+
return v.arkTxId === vtxo.txid;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Helper function to find vtxos that resulted from a spentBy transaction
|
|
123
|
+
*/
|
|
124
|
+
function findVtxosResultedFromTxid(vtxos, txid) {
|
|
125
|
+
return vtxos.filter((v) => {
|
|
126
|
+
if (v.virtualStatus.state !== "preconfirmed" &&
|
|
127
|
+
v.virtualStatus.commitmentTxIds?.includes(txid)) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
return v.txid === txid;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Helper function to reduce vtxos to their total amount
|
|
135
|
+
*/
|
|
136
|
+
function reduceVtxosAmount(vtxos) {
|
|
137
|
+
return vtxos.reduce((sum, v) => sum + v.value, 0);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Helper function to get a vtxo from a list of vtxos
|
|
141
|
+
*/
|
|
142
|
+
function getVtxo(resultedVtxos, spentVtxos) {
|
|
143
|
+
if (resultedVtxos.length === 0) {
|
|
144
|
+
return spentVtxos[0];
|
|
145
|
+
}
|
|
146
|
+
return resultedVtxos[0];
|
|
147
|
+
}
|
|
136
148
|
function removeVtxosFromList(vtxos, vtxosToRemove) {
|
|
137
149
|
return vtxos.filter((v) => {
|
|
138
150
|
for (const vtxoToRemove of vtxosToRemove) {
|
|
@@ -10,6 +10,11 @@ export class TxWeightEstimator {
|
|
|
10
10
|
static create() {
|
|
11
11
|
return new TxWeightEstimator(false, 0, 0, 0, 0, 0);
|
|
12
12
|
}
|
|
13
|
+
addP2AInput() {
|
|
14
|
+
this.inputCount++;
|
|
15
|
+
this.inputSize += TxWeightEstimator.INPUT_SIZE;
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
13
18
|
addKeySpendInput(isDefault = true) {
|
|
14
19
|
this.inputCount++;
|
|
15
20
|
this.inputWitnessSize += 64 + 1 + (isDefault ? 0 : 1);
|
|
@@ -45,6 +50,12 @@ export class TxWeightEstimator {
|
|
|
45
50
|
TxWeightEstimator.OUTPUT_SIZE + TxWeightEstimator.P2WKH_OUTPUT_SIZE;
|
|
46
51
|
return this;
|
|
47
52
|
}
|
|
53
|
+
addP2TROutput() {
|
|
54
|
+
this.outputCount++;
|
|
55
|
+
this.outputSize +=
|
|
56
|
+
TxWeightEstimator.OUTPUT_SIZE + TxWeightEstimator.P2TR_OUTPUT_SIZE;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
48
59
|
vsize() {
|
|
49
60
|
const getVarIntSize = (n) => {
|
|
50
61
|
if (n < 0xfd)
|
|
@@ -82,6 +93,7 @@ TxWeightEstimator.P2WKH_OUTPUT_SIZE = 1 + 1 + 20;
|
|
|
82
93
|
TxWeightEstimator.BASE_TX_SIZE = 8 + 2; // Version + LockTime
|
|
83
94
|
TxWeightEstimator.WITNESS_HEADER_SIZE = 2; // Flag + Marker
|
|
84
95
|
TxWeightEstimator.WITNESS_SCALE_FACTOR = 4;
|
|
96
|
+
TxWeightEstimator.P2TR_OUTPUT_SIZE = 1 + 1 + 32;
|
|
85
97
|
const vsize = (weight) => {
|
|
86
98
|
const value = BigInt(Math.ceil(weight / TxWeightEstimator.WITNESS_SCALE_FACTOR));
|
|
87
99
|
return {
|