@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
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { SigHash, Transaction } from "@scure/btc-signer";
|
|
2
|
+
import { ChainTxType } from '../providers/indexer.js';
|
|
3
|
+
import { base64, hex } from "@scure/base";
|
|
4
|
+
import { VtxoScript } from '../script/base.js';
|
|
5
|
+
import { TaprootControlBlock, } from "@scure/btc-signer/psbt";
|
|
6
|
+
import { TxWeightEstimator } from '../utils/txSizeEstimator.js';
|
|
7
|
+
import { Wallet } from './wallet.js';
|
|
8
|
+
export var Unroll;
|
|
9
|
+
(function (Unroll) {
|
|
10
|
+
let StepType;
|
|
11
|
+
(function (StepType) {
|
|
12
|
+
StepType[StepType["UNROLL"] = 0] = "UNROLL";
|
|
13
|
+
StepType[StepType["WAIT"] = 1] = "WAIT";
|
|
14
|
+
StepType[StepType["DONE"] = 2] = "DONE";
|
|
15
|
+
})(StepType = Unroll.StepType || (Unroll.StepType = {}));
|
|
16
|
+
/**
|
|
17
|
+
* Manages the unrolling process of a VTXO back to the Bitcoin blockchain.
|
|
18
|
+
*
|
|
19
|
+
* The Session class implements an async iterator that processes the unrolling steps:
|
|
20
|
+
* 1. **WAIT**: Waits for a transaction to be confirmed onchain (if it's in mempool)
|
|
21
|
+
* 2. **UNROLL**: Broadcasts the next transaction in the chain to the blockchain
|
|
22
|
+
* 3. **DONE**: Indicates the unrolling process is complete
|
|
23
|
+
*
|
|
24
|
+
* The unrolling process works by traversing the transaction chain from the root (most recent)
|
|
25
|
+
* to the leaf (oldest), broadcasting each transaction that isn't already onchain.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const session = await Unroll.Session.create(vtxoOutpoint, bumper, explorer, indexer);
|
|
30
|
+
*
|
|
31
|
+
* // iterate over the steps
|
|
32
|
+
* for await (const doneStep of session) {
|
|
33
|
+
* switch (doneStep.type) {
|
|
34
|
+
* case Unroll.StepType.WAIT:
|
|
35
|
+
* console.log(`Transaction ${doneStep.txid} confirmed`);
|
|
36
|
+
* break;
|
|
37
|
+
* case Unroll.StepType.UNROLL:
|
|
38
|
+
* console.log(`Broadcasting transaction ${doneStep.tx.id}`);
|
|
39
|
+
* break;
|
|
40
|
+
* case Unroll.StepType.DONE:
|
|
41
|
+
* console.log(`Unrolling complete for VTXO ${doneStep.vtxoTxid}`);
|
|
42
|
+
* break;
|
|
43
|
+
* }
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
**/
|
|
47
|
+
class Session {
|
|
48
|
+
constructor(toUnroll, bumper, explorer, indexer) {
|
|
49
|
+
this.toUnroll = toUnroll;
|
|
50
|
+
this.bumper = bumper;
|
|
51
|
+
this.explorer = explorer;
|
|
52
|
+
this.indexer = indexer;
|
|
53
|
+
}
|
|
54
|
+
static async create(toUnroll, bumper, explorer, indexer) {
|
|
55
|
+
const { chain } = await indexer.getVtxoChain(toUnroll);
|
|
56
|
+
return new Session({ ...toUnroll, chain }, bumper, explorer, indexer);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get the next step to be executed
|
|
60
|
+
* @returns The next step to be executed + the function to execute it
|
|
61
|
+
*/
|
|
62
|
+
async next() {
|
|
63
|
+
let nextTxToBroadcast;
|
|
64
|
+
const chain = this.toUnroll.chain;
|
|
65
|
+
// Iterate through the chain from the end (root) to the beginning (leaf)
|
|
66
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
67
|
+
const chainTx = chain[i];
|
|
68
|
+
// Skip commitment transactions as they are always onchain
|
|
69
|
+
if (chainTx.type === ChainTxType.COMMITMENT ||
|
|
70
|
+
chainTx.type === ChainTxType.UNSPECIFIED) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
// Check if the transaction is confirmed onchain
|
|
75
|
+
const txInfo = await this.explorer.getTxStatus(chainTx.txid);
|
|
76
|
+
// If found but not confirmed, it means the tx is in the mempool
|
|
77
|
+
// An unilateral exit is running, we must wait for it to be confirmed
|
|
78
|
+
if (!txInfo.confirmed) {
|
|
79
|
+
return {
|
|
80
|
+
type: StepType.WAIT,
|
|
81
|
+
txid: chainTx.txid,
|
|
82
|
+
do: doWait(this.explorer, chainTx.txid),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
// If the tx is not found, it's offchain, let's break
|
|
88
|
+
nextTxToBroadcast = chainTx;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!nextTxToBroadcast) {
|
|
93
|
+
return {
|
|
94
|
+
type: StepType.DONE,
|
|
95
|
+
vtxoTxid: this.toUnroll.txid,
|
|
96
|
+
do: () => Promise.resolve(),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// Get the virtual transaction data
|
|
100
|
+
const virtualTxs = await this.indexer.getVirtualTxs([
|
|
101
|
+
nextTxToBroadcast.txid,
|
|
102
|
+
]);
|
|
103
|
+
if (virtualTxs.txs.length === 0) {
|
|
104
|
+
throw new Error(`Tx ${nextTxToBroadcast.txid} not found`);
|
|
105
|
+
}
|
|
106
|
+
const tx = Transaction.fromPSBT(base64.decode(virtualTxs.txs[0]), {
|
|
107
|
+
allowUnknownInputs: true,
|
|
108
|
+
});
|
|
109
|
+
// finalize the tree transaction
|
|
110
|
+
if (nextTxToBroadcast.type === ChainTxType.TREE) {
|
|
111
|
+
const input = tx.getInput(0);
|
|
112
|
+
if (!input) {
|
|
113
|
+
throw new Error("Input not found");
|
|
114
|
+
}
|
|
115
|
+
const tapKeySig = input.tapKeySig;
|
|
116
|
+
if (!tapKeySig) {
|
|
117
|
+
throw new Error("Tap key sig not found");
|
|
118
|
+
}
|
|
119
|
+
tx.updateInput(0, {
|
|
120
|
+
finalScriptWitness: [tapKeySig],
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
// finalize ark transaction
|
|
125
|
+
tx.finalize();
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
type: StepType.UNROLL,
|
|
129
|
+
tx,
|
|
130
|
+
do: doUnroll(this.bumper, this.explorer, tx),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Iterate over the steps to be executed and execute them
|
|
135
|
+
* @returns An async iterator over the executed steps
|
|
136
|
+
*/
|
|
137
|
+
async *[Symbol.asyncIterator]() {
|
|
138
|
+
let lastStep;
|
|
139
|
+
do {
|
|
140
|
+
if (lastStep !== undefined) {
|
|
141
|
+
// wait 1 second before trying the next step in order to give time to the
|
|
142
|
+
// explorer to update the tx status
|
|
143
|
+
await sleep(1000);
|
|
144
|
+
}
|
|
145
|
+
const step = await this.next();
|
|
146
|
+
await step.do();
|
|
147
|
+
yield step;
|
|
148
|
+
lastStep = step.type;
|
|
149
|
+
} while (lastStep !== StepType.DONE);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
Unroll.Session = Session;
|
|
153
|
+
/**
|
|
154
|
+
* Complete the unroll of a VTXO by broadcasting the transaction that spends the CSV path.
|
|
155
|
+
* @param wallet the wallet owning the VTXO(s)
|
|
156
|
+
* @param vtxoTxids the txids of the VTXO(s) to complete unroll
|
|
157
|
+
* @param outputAddress the address to send the unrolled funds to
|
|
158
|
+
* @throws if the VTXO(s) are not fully unrolled, if the txids are not found, if the tx is not confirmed, if no exit path is found or not available
|
|
159
|
+
* @returns the txid of the transaction spending the unrolled funds
|
|
160
|
+
*/
|
|
161
|
+
async function completeUnroll(wallet, vtxoTxids, outputAddress) {
|
|
162
|
+
const chainTip = await wallet.onchainProvider.getChainTip();
|
|
163
|
+
let vtxos = await wallet.getVtxos({ withUnrolled: true });
|
|
164
|
+
vtxos = vtxos.filter((vtxo) => vtxoTxids.includes(vtxo.txid));
|
|
165
|
+
if (vtxos.length === 0) {
|
|
166
|
+
throw new Error("No vtxos to complete unroll");
|
|
167
|
+
}
|
|
168
|
+
const inputs = [];
|
|
169
|
+
let totalAmount = 0n;
|
|
170
|
+
const txWeightEstimator = TxWeightEstimator.create();
|
|
171
|
+
for (const vtxo of vtxos) {
|
|
172
|
+
if (!vtxo.isUnrolled) {
|
|
173
|
+
throw new Error(`Vtxo ${vtxo.txid}:${vtxo.vout} is not fully unrolled, use unroll first`);
|
|
174
|
+
}
|
|
175
|
+
const txStatus = await wallet.onchainProvider.getTxStatus(vtxo.txid);
|
|
176
|
+
if (!txStatus.confirmed) {
|
|
177
|
+
throw new Error(`tx ${vtxo.txid} is not confirmed`);
|
|
178
|
+
}
|
|
179
|
+
const exit = availableExitPath({ height: txStatus.blockHeight, time: txStatus.blockTime }, chainTip, vtxo);
|
|
180
|
+
if (!exit) {
|
|
181
|
+
throw new Error(`no available exit path found for vtxo ${vtxo.txid}:${vtxo.vout}`);
|
|
182
|
+
}
|
|
183
|
+
const spendingLeaf = VtxoScript.decode(vtxo.tapTree).findLeaf(hex.encode(exit.script));
|
|
184
|
+
if (!spendingLeaf) {
|
|
185
|
+
throw new Error(`spending leaf not found for vtxo ${vtxo.txid}:${vtxo.vout}`);
|
|
186
|
+
}
|
|
187
|
+
totalAmount += BigInt(vtxo.value);
|
|
188
|
+
inputs.push({
|
|
189
|
+
txid: vtxo.txid,
|
|
190
|
+
index: vtxo.vout,
|
|
191
|
+
tapLeafScript: [spendingLeaf],
|
|
192
|
+
sequence: 0xffffffff - 1,
|
|
193
|
+
witnessUtxo: {
|
|
194
|
+
amount: BigInt(vtxo.value),
|
|
195
|
+
script: VtxoScript.decode(vtxo.tapTree).pkScript,
|
|
196
|
+
},
|
|
197
|
+
sighashType: SigHash.DEFAULT,
|
|
198
|
+
});
|
|
199
|
+
txWeightEstimator.addTapscriptInput(64, spendingLeaf[1].length, TaprootControlBlock.encode(spendingLeaf[0]).length);
|
|
200
|
+
}
|
|
201
|
+
const tx = new Transaction({ allowUnknownInputs: true, version: 2 });
|
|
202
|
+
for (const input of inputs) {
|
|
203
|
+
tx.addInput(input);
|
|
204
|
+
}
|
|
205
|
+
txWeightEstimator.addP2TROutput();
|
|
206
|
+
let feeRate = await wallet.onchainProvider.getFeeRate();
|
|
207
|
+
if (!feeRate || feeRate < Wallet.MIN_FEE_RATE) {
|
|
208
|
+
feeRate = Wallet.MIN_FEE_RATE;
|
|
209
|
+
}
|
|
210
|
+
const feeAmount = txWeightEstimator.vsize().fee(BigInt(feeRate));
|
|
211
|
+
if (feeAmount > totalAmount) {
|
|
212
|
+
throw new Error("fee amount is greater than the total amount");
|
|
213
|
+
}
|
|
214
|
+
tx.addOutputAddress(outputAddress, totalAmount - feeAmount);
|
|
215
|
+
const signedTx = await wallet.identity.sign(tx);
|
|
216
|
+
signedTx.finalize();
|
|
217
|
+
await wallet.onchainProvider.broadcastTransaction(signedTx.hex);
|
|
218
|
+
return signedTx.id;
|
|
219
|
+
}
|
|
220
|
+
Unroll.completeUnroll = completeUnroll;
|
|
221
|
+
})(Unroll || (Unroll = {}));
|
|
222
|
+
function sleep(ms) {
|
|
223
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
224
|
+
}
|
|
225
|
+
function doUnroll(bumper, onchainProvider, tx) {
|
|
226
|
+
return async () => {
|
|
227
|
+
const [parent, child] = await bumper.bumpP2A(tx);
|
|
228
|
+
await onchainProvider.broadcastTransaction(parent, child);
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function doWait(onchainProvider, txid) {
|
|
232
|
+
return () => {
|
|
233
|
+
return new Promise((resolve, reject) => {
|
|
234
|
+
const interval = setInterval(async () => {
|
|
235
|
+
try {
|
|
236
|
+
const txInfo = await onchainProvider.getTxStatus(txid);
|
|
237
|
+
if (txInfo.confirmed) {
|
|
238
|
+
clearInterval(interval);
|
|
239
|
+
resolve();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
catch (e) {
|
|
243
|
+
clearInterval(interval);
|
|
244
|
+
reject(e);
|
|
245
|
+
}
|
|
246
|
+
}, 5000);
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function availableExitPath(confirmedAt, current, vtxo) {
|
|
251
|
+
const exits = VtxoScript.decode(vtxo.tapTree).exitPaths();
|
|
252
|
+
for (const exit of exits) {
|
|
253
|
+
if (exit.params.timelock.type === "blocks") {
|
|
254
|
+
if (current.height >=
|
|
255
|
+
confirmedAt.height + Number(exit.params.timelock.value)) {
|
|
256
|
+
return exit;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
if (current.time >=
|
|
261
|
+
confirmedAt.time + Number(exit.params.timelock.value)) {
|
|
262
|
+
return exit;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|