@arkade-os/sdk 0.5.4 → 0.5.5
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 +103 -17
- package/dist/adapters/expo.cjs +7 -6
- package/dist/adapters/expo.d.cts +4 -3
- package/dist/adapters/expo.d.ts +4 -3
- package/dist/adapters/expo.js +4 -3
- package/dist/{asyncStorageTaskQueue-DTBa8EH7.d.cts → asyncStorageTaskQueue-BbH-THYJ.d.cts} +1 -1
- package/dist/{asyncStorageTaskQueue-DHMIxY04.d.ts → asyncStorageTaskQueue-BlvkJyTa.d.ts} +1 -1
- package/dist/base-C-Y5t_0F.d.cts +359 -0
- package/dist/base-C-Y5t_0F.d.ts +359 -0
- package/dist/{chunk-AG4TFD6F.js → chunk-4I53THL6.js} +79 -884
- package/dist/chunk-4I53THL6.js.map +1 -0
- package/dist/{chunk-B6VNKGIH.cjs → chunk-5SYUEVJH.cjs} +10 -10
- package/dist/{chunk-B6VNKGIH.cjs.map → chunk-5SYUEVJH.cjs.map} +1 -1
- package/dist/{chunk-BTRCN4JJ.js → chunk-7GIKLFHC.js} +64 -4
- package/dist/chunk-7GIKLFHC.js.map +1 -0
- package/dist/{chunk-E6OOJEHL.cjs → chunk-AMH7Q3DF.cjs} +60 -934
- package/dist/chunk-AMH7Q3DF.cjs.map +1 -0
- package/dist/{chunk-6CUP6ZIF.js → chunk-BCA6D255.js} +17 -841
- package/dist/chunk-BCA6D255.js.map +1 -0
- package/dist/{chunk-TUOLZBNC.js → chunk-CE46RS6L.js} +5 -4
- package/dist/chunk-CE46RS6L.js.map +1 -0
- package/dist/chunk-DNFTHG73.js +829 -0
- package/dist/chunk-DNFTHG73.js.map +1 -0
- package/dist/chunk-FDJ734CH.js +882 -0
- package/dist/chunk-FDJ734CH.js.map +1 -0
- package/dist/chunk-FLPWNMX3.cjs +930 -0
- package/dist/chunk-FLPWNMX3.cjs.map +1 -0
- package/dist/{chunk-MWVAWFX6.js → chunk-FVVXCDNE.js} +4 -4
- package/dist/{chunk-MWVAWFX6.js.map → chunk-FVVXCDNE.js.map} +1 -1
- package/dist/{chunk-DNITZVNG.cjs → chunk-HQQ4NRXI.cjs} +18 -17
- package/dist/chunk-HQQ4NRXI.cjs.map +1 -0
- package/dist/chunk-JFJNB7ZH.cjs +884 -0
- package/dist/chunk-JFJNB7ZH.cjs.map +1 -0
- package/dist/{chunk-GHUUT54D.cjs → chunk-KCL5AVML.cjs} +89 -933
- package/dist/chunk-KCL5AVML.cjs.map +1 -0
- package/dist/{chunk-ZJHLLDBP.js → chunk-OO2KEK64.js} +2624 -1277
- package/dist/chunk-OO2KEK64.js.map +1 -0
- package/dist/{chunk-F3FAE7VJ.cjs → chunk-T254PXSN.cjs} +3115 -1752
- package/dist/chunk-T254PXSN.cjs.map +1 -0
- package/dist/chunk-V3ZLL5VY.js +380 -0
- package/dist/chunk-V3ZLL5VY.js.map +1 -0
- package/dist/chunk-X6ZI53DZ.cjs +383 -0
- package/dist/chunk-X6ZI53DZ.cjs.map +1 -0
- package/dist/{chunk-WXNSISXK.cjs → chunk-XWUCNMPN.cjs} +79 -18
- package/dist/chunk-XWUCNMPN.cjs.map +1 -0
- package/dist/compiler.cjs +50 -0
- package/dist/compiler.cjs.map +1 -0
- package/dist/compiler.d.cts +38 -0
- package/dist/compiler.d.ts +38 -0
- package/dist/compiler.js +47 -0
- package/dist/compiler.js.map +1 -0
- package/dist/contracts/handlers/index.cjs +11 -9
- package/dist/contracts/handlers/index.d.cts +6 -4
- package/dist/contracts/handlers/index.d.ts +6 -4
- package/dist/contracts/handlers/index.js +4 -2
- package/dist/{delegate-xrSBNybC.d.ts → delegate-gGkUnYpi.d.cts} +1 -1
- package/dist/{delegate-f4_t864Z.d.cts → delegate-iyDum4fw.d.ts} +1 -1
- package/dist/{index-BJDz15ms.d.ts → index-BWwVnJhU.d.cts} +5 -270
- package/dist/{index-BjiVGtp8.d.cts → index-Bx-iArDs.d.ts} +5 -270
- package/dist/{index-DRwe0UDN.d.ts → index-DCml4QJn.d.ts} +1593 -1711
- package/dist/{index-DRwe0UDN.d.cts → index-DkyzIzKL.d.cts} +1593 -1711
- package/dist/index.cjs +457 -378
- package/dist/index.d.cts +633 -378
- package/dist/index.d.ts +633 -378
- package/dist/index.js +7 -4
- package/dist/{wallet-jVBDQl6R.d.cts → keyringDescriptorProvider-CS4w4PWd.d.ts} +412 -361
- package/dist/{wallet-Oc3Wd5gv.d.ts → keyringDescriptorProvider-CmHXrIXd.d.cts} +412 -361
- package/dist/program-BARSNg3X.d.ts +286 -0
- package/dist/program-znrWkxE4.d.cts +286 -0
- package/dist/repositories/realm/index.cjs +71 -51
- package/dist/repositories/realm/index.cjs.map +1 -1
- package/dist/repositories/realm/index.d.cts +20 -9
- package/dist/repositories/realm/index.d.ts +20 -9
- package/dist/repositories/realm/index.js +42 -23
- package/dist/repositories/realm/index.js.map +1 -1
- package/dist/repositories/sqlite/index.cjs +81 -56
- package/dist/repositories/sqlite/index.cjs.map +1 -1
- package/dist/repositories/sqlite/index.d.cts +17 -8
- package/dist/repositories/sqlite/index.d.ts +17 -8
- package/dist/repositories/sqlite/index.js +55 -31
- package/dist/repositories/sqlite/index.js.map +1 -1
- package/dist/{taskRunner-CDlNeth0.d.ts → taskRunner-DgMLSk_O.d.ts} +1 -1
- package/dist/{taskRunner-BPPU-YHG.d.cts → taskRunner-buh6aFpG.d.cts} +1 -1
- package/dist/wallet/expo/background.cjs +17 -14
- package/dist/wallet/expo/background.cjs.map +1 -1
- package/dist/wallet/expo/background.d.cts +5 -4
- package/dist/wallet/expo/background.d.ts +5 -4
- package/dist/wallet/expo/background.js +9 -6
- package/dist/wallet/expo/background.js.map +1 -1
- package/dist/wallet/expo/index.cjs +29 -16
- package/dist/wallet/expo/index.cjs.map +1 -1
- package/dist/wallet/expo/index.d.cts +12 -7
- package/dist/wallet/expo/index.d.ts +12 -7
- package/dist/wallet/expo/index.js +19 -6
- package/dist/wallet/expo/index.js.map +1 -1
- package/dist/worker/expo/index.cjs +12 -9
- package/dist/worker/expo/index.cjs.map +1 -1
- package/dist/worker/expo/index.d.cts +6 -5
- package/dist/worker/expo/index.d.ts +6 -5
- package/dist/worker/expo/index.js +8 -5
- package/dist/worker/expo/index.js.map +1 -1
- package/package.json +16 -1
- package/dist/chunk-6CUP6ZIF.js.map +0 -1
- package/dist/chunk-AG4TFD6F.js.map +0 -1
- package/dist/chunk-BTRCN4JJ.js.map +0 -1
- package/dist/chunk-DNITZVNG.cjs.map +0 -1
- package/dist/chunk-E6OOJEHL.cjs.map +0 -1
- package/dist/chunk-F3FAE7VJ.cjs.map +0 -1
- package/dist/chunk-GHUUT54D.cjs.map +0 -1
- package/dist/chunk-TUOLZBNC.js.map +0 -1
- package/dist/chunk-WXNSISXK.cjs.map +0 -1
- package/dist/chunk-ZJHLLDBP.js.map +0 -1
|
@@ -1,882 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { VarBytes, Script as Script$1 } from '@scure/btc-signer/script.js';
|
|
4
|
-
import * as bip68 from 'bip68';
|
|
5
|
-
import { ScriptNum, OP, p2tr_ms, Script, SigHash, p2tr, taprootNumsKey, Address, RawWitness, Transaction as Transaction$1, RawTx } from '@scure/btc-signer';
|
|
6
|
-
import { TAP_LEAF_VERSION } from '@scure/btc-signer/payment.js';
|
|
7
|
-
import * as P from 'micro-packed';
|
|
1
|
+
import { VtxoScript, getSequence, proofTapLeafScriptOf, TapTreeCoder, toBIP371TapTree, sequenceToTimelock, DEFAULT_ARKADE_SERVER_URL } from './chunk-FDJ734CH.js';
|
|
2
|
+
import { OP, Script, SigHash, RawWitness, Transaction as Transaction$1, RawTx, ScriptNum } from '@scure/btc-signer';
|
|
8
3
|
import { PSBTOutput } from '@scure/btc-signer/psbt.js';
|
|
4
|
+
import { concatBytes } from '@scure/btc-signer/utils.js';
|
|
9
5
|
import { schnorr } from '@noble/curves/secp256k1.js';
|
|
6
|
+
import { hex } from '@scure/base';
|
|
10
7
|
|
|
11
|
-
var __defProp = Object.defineProperty;
|
|
12
|
-
var __export = (target, all) => {
|
|
13
|
-
for (var name in all)
|
|
14
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
-
};
|
|
16
|
-
var getNetwork = (network) => {
|
|
17
|
-
const found = networks[network];
|
|
18
|
-
if (!found) throw new Error(`Unsupported network: ${network}`);
|
|
19
|
-
return found;
|
|
20
|
-
};
|
|
21
|
-
var networkFromOperatorInfo = (info) => getNetwork(info.network);
|
|
22
|
-
var networkFromArkadeInfo = networkFromOperatorInfo;
|
|
23
|
-
var networks = {
|
|
24
|
-
bitcoin: withArkPrefix(NETWORK, "ark", "bitcoin"),
|
|
25
|
-
testnet: withArkPrefix(TEST_NETWORK, "tark", "testnet"),
|
|
26
|
-
signet: withArkPrefix(TEST_NETWORK, "tark", "signet"),
|
|
27
|
-
mutinynet: withArkPrefix(TEST_NETWORK, "tark", "mutinynet"),
|
|
28
|
-
regtest: withArkPrefix(
|
|
29
|
-
{
|
|
30
|
-
...TEST_NETWORK,
|
|
31
|
-
bech32: "bcrt",
|
|
32
|
-
pubKeyHash: 111,
|
|
33
|
-
scriptHash: 196
|
|
34
|
-
},
|
|
35
|
-
"tark",
|
|
36
|
-
"regtest"
|
|
37
|
-
)
|
|
38
|
-
};
|
|
39
|
-
function withArkPrefix(network, prefix, name) {
|
|
40
|
-
return {
|
|
41
|
-
...network,
|
|
42
|
-
hrp: prefix,
|
|
43
|
-
name
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
var DEFAULT_ARKADE_SERVER_URL = "https://arkade.computer";
|
|
47
|
-
var DEFAULT_ARKADE_EMULATOR_URL = "https://emulator.arkade.computer";
|
|
48
|
-
var DEFAULT_NETWORK = networks.bitcoin;
|
|
49
|
-
var DEFAULT_NETWORK_NAME = "bitcoin";
|
|
50
|
-
var COMPRESSED_PUBKEY = /^0[23][0-9a-f]{64}$/;
|
|
51
|
-
var BITCOIN_EMULATOR_PUBKEY = "0239c196415da47b26456a101daaa12ba9e445bfe153197f1e2b750bf40e52092e";
|
|
52
|
-
var MUTINYNET_EMULATOR_PUBKEY = "03f823b9b2febc81f4af967e77aed2f541cbd3397c6d8f5a72e32eb7b471af889a";
|
|
53
|
-
var REGTEST_EMULATOR_PUBKEY = "02999413c46fa10ada5cbc4bcc79a1d09160c2ba3cfc812705d7a13e5e545fb2a9";
|
|
54
|
-
var EMULATOR_PUBKEYS = {
|
|
55
|
-
bitcoin: BITCOIN_EMULATOR_PUBKEY,
|
|
56
|
-
mutinynet: MUTINYNET_EMULATOR_PUBKEY,
|
|
57
|
-
regtest: REGTEST_EMULATOR_PUBKEY
|
|
58
|
-
};
|
|
59
|
-
function defaultEmulatorPubkey(network) {
|
|
60
|
-
const pinned = network.name ? EMULATOR_PUBKEYS[network.name] : void 0;
|
|
61
|
-
if (!pinned) {
|
|
62
|
-
const cause = network.name ? `no emulator is deployed for ${network.name}` : `this Network carries no name, so it cannot be matched (build it with getNetwork(...) rather than by hand)`;
|
|
63
|
-
throw new Error(
|
|
64
|
-
`No emulator co-signer key is pinned for this network: ${cause}; pass emulatorPubkey: "<33-byte compressed hex>" to Arkade.connect to co-sign with your own emulator instead. Pinned networks: ${Object.keys(EMULATOR_PUBKEYS).join(", ")}`
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
return pinned;
|
|
68
|
-
}
|
|
69
|
-
function resolveEmulatorPubkey(network, override) {
|
|
70
|
-
if (override === void 0) return defaultEmulatorPubkey(network);
|
|
71
|
-
if (!COMPRESSED_PUBKEY.test(override)) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
`Emulator pubkey override must be 33-byte compressed secp256k1 hex (66 lowercase chars, 02/03 prefix), got ${JSON.stringify(override)}.`
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
return override;
|
|
77
|
-
}
|
|
78
|
-
var ArkAddress = class _ArkAddress {
|
|
79
|
-
/**
|
|
80
|
-
* Create an Arkade address from its server public key, Taproot output key, and prefix.
|
|
81
|
-
*
|
|
82
|
-
* @param serverPubKey - 32-byte Arkade server public key
|
|
83
|
-
* @param vtxoTaprootKey - 32-byte Taproot output key (a.k.a. tweaked public key)
|
|
84
|
-
* @param hrp - Bech32 human-readable prefix
|
|
85
|
-
* @param version - Address version byte
|
|
86
|
-
* @defaultValue `version = 0`
|
|
87
|
-
* @throws Error if either public key is not 32 bytes long
|
|
88
|
-
*/
|
|
89
|
-
constructor(serverPubKey, vtxoTaprootKey, hrp = DEFAULT_NETWORK.hrp, version2 = 0) {
|
|
90
|
-
this.serverPubKey = serverPubKey;
|
|
91
|
-
this.vtxoTaprootKey = vtxoTaprootKey;
|
|
92
|
-
this.hrp = hrp;
|
|
93
|
-
this.version = version2;
|
|
94
|
-
if (serverPubKey.length !== 32) {
|
|
95
|
-
throw new Error(
|
|
96
|
-
"Invalid server public key length, expected 32 bytes, got " + serverPubKey.length
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
if (vtxoTaprootKey.length !== 32) {
|
|
100
|
-
throw new Error(
|
|
101
|
-
"Invalid vtxo taproot public key length, expected 32 bytes, got " + vtxoTaprootKey.length
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
serverPubKey;
|
|
106
|
-
vtxoTaprootKey;
|
|
107
|
-
hrp;
|
|
108
|
-
version;
|
|
109
|
-
/**
|
|
110
|
-
* Decode an Arkade address from its bech32m string form.
|
|
111
|
-
*
|
|
112
|
-
* @param address - Bech32m-encoded Arkade address
|
|
113
|
-
* @returns Decoded Arkade address
|
|
114
|
-
* @throws Error if the address is malformed or has an invalid payload length
|
|
115
|
-
* @see encode
|
|
116
|
-
*/
|
|
117
|
-
static decode(address) {
|
|
118
|
-
const decoded = bech32m.decodeUnsafe(address, 1023);
|
|
119
|
-
if (!decoded) {
|
|
120
|
-
throw new Error("Invalid address");
|
|
121
|
-
}
|
|
122
|
-
const data = new Uint8Array(bech32m.fromWords(decoded.words));
|
|
123
|
-
if (data.length !== 1 + 32 + 32) {
|
|
124
|
-
throw new Error("Invalid data length, expected 65 bytes, got " + data.length);
|
|
125
|
-
}
|
|
126
|
-
const version2 = data[0];
|
|
127
|
-
const serverPubKey = data.slice(1, 33);
|
|
128
|
-
const vtxoTaprootPubKey = data.slice(33, 65);
|
|
129
|
-
return new _ArkAddress(serverPubKey, vtxoTaprootPubKey, decoded.prefix, version2);
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Encode the address to its bech32m string form.
|
|
133
|
-
*
|
|
134
|
-
* @returns Bech32m-encoded Arkade address
|
|
135
|
-
* @see decode
|
|
136
|
-
*/
|
|
137
|
-
encode() {
|
|
138
|
-
const data = new Uint8Array(1 + 32 + 32);
|
|
139
|
-
data[0] = this.version;
|
|
140
|
-
data.set(this.serverPubKey, 1);
|
|
141
|
-
data.set(this.vtxoTaprootKey, 33);
|
|
142
|
-
const words = bech32m.toWords(data);
|
|
143
|
-
return bech32m.encode(this.hrp, words, 1023);
|
|
144
|
-
}
|
|
145
|
-
/** ScriptPubKey used to send non-dust funds to the address. */
|
|
146
|
-
get pkScript() {
|
|
147
|
-
return Script$1.encode(["OP_1", this.vtxoTaprootKey]);
|
|
148
|
-
}
|
|
149
|
-
/** ScriptPubKey used to send sub-dust funds to the address. */
|
|
150
|
-
get subdustPkScript() {
|
|
151
|
-
return Script$1.encode(["RETURN", this.vtxoTaprootKey]);
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
function timelockToSequence(timelock) {
|
|
155
|
-
return bip68.encode(
|
|
156
|
-
timelock.type === "blocks" ? { blocks: Number(timelock.value) } : { seconds: Number(timelock.value) }
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
function sequenceToTimelock(sequence) {
|
|
160
|
-
const decoded = bip68.decode(sequence);
|
|
161
|
-
if ("blocks" in decoded && decoded.blocks !== void 0) {
|
|
162
|
-
return { type: "blocks", value: BigInt(decoded.blocks) };
|
|
163
|
-
}
|
|
164
|
-
if ("seconds" in decoded && decoded.seconds !== void 0) {
|
|
165
|
-
return { type: "seconds", value: BigInt(decoded.seconds) };
|
|
166
|
-
}
|
|
167
|
-
throw new Error(`Invalid BIP68 sequence: ${sequence}`);
|
|
168
|
-
}
|
|
169
|
-
function sequenceToCanonicalTimelock(sequence) {
|
|
170
|
-
if (sequence < 0n || sequence >= 0x80000000n) return void 0;
|
|
171
|
-
const timelock = sequenceToTimelock(Number(sequence));
|
|
172
|
-
return BigInt(timelockToSequence(timelock)) === sequence ? timelock : void 0;
|
|
173
|
-
}
|
|
174
|
-
var MinimalScriptNum = ScriptNum(void 0, true);
|
|
175
|
-
function decodeTapscript(script) {
|
|
176
|
-
const types = [
|
|
177
|
-
MultisigTapscript,
|
|
178
|
-
CSVMultisigTapscript,
|
|
179
|
-
ConditionCSVMultisigTapscript,
|
|
180
|
-
ConditionMultisigTapscript,
|
|
181
|
-
CLTVMultisigTapscript
|
|
182
|
-
];
|
|
183
|
-
for (const type of types) {
|
|
184
|
-
try {
|
|
185
|
-
return type.decode(script);
|
|
186
|
-
} catch (error) {
|
|
187
|
-
continue;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
throw new Error(`Failed to decode: script ${hex.encode(script)} is not a valid tapscript`);
|
|
191
|
-
}
|
|
192
|
-
var MultisigTapscript;
|
|
193
|
-
((MultisigTapscript2) => {
|
|
194
|
-
((MultisigType2) => {
|
|
195
|
-
MultisigType2[MultisigType2["CHECKSIG"] = 0] = "CHECKSIG";
|
|
196
|
-
MultisigType2[MultisigType2["CHECKSIGADD"] = 1] = "CHECKSIGADD";
|
|
197
|
-
})(MultisigTapscript2.MultisigType || (MultisigTapscript2.MultisigType = {}));
|
|
198
|
-
function encode2(params) {
|
|
199
|
-
if (params.pubkeys.length === 0) {
|
|
200
|
-
throw new Error("At least 1 pubkey is required");
|
|
201
|
-
}
|
|
202
|
-
for (const pubkey of params.pubkeys) {
|
|
203
|
-
if (pubkey.length !== 32) {
|
|
204
|
-
throw new Error(`Invalid pubkey length: expected 32, got ${pubkey.length}`);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (!params.type) {
|
|
208
|
-
params.type = 0 /* CHECKSIG */;
|
|
209
|
-
}
|
|
210
|
-
if (params.type === 1 /* CHECKSIGADD */) {
|
|
211
|
-
return {
|
|
212
|
-
type: "multisig" /* Multisig */,
|
|
213
|
-
params,
|
|
214
|
-
script: p2tr_ms(params.pubkeys.length, params.pubkeys).script
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
const asm = [];
|
|
218
|
-
for (let i = 0; i < params.pubkeys.length; i++) {
|
|
219
|
-
asm.push(params.pubkeys[i]);
|
|
220
|
-
if (i < params.pubkeys.length - 1) {
|
|
221
|
-
asm.push("CHECKSIGVERIFY");
|
|
222
|
-
} else {
|
|
223
|
-
asm.push("CHECKSIG");
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return {
|
|
227
|
-
type: "multisig" /* Multisig */,
|
|
228
|
-
params,
|
|
229
|
-
script: Script.encode(asm)
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
MultisigTapscript2.encode = encode2;
|
|
233
|
-
function decode2(script) {
|
|
234
|
-
if (script.length === 0) {
|
|
235
|
-
throw new Error("Failed to decode: script is empty");
|
|
236
|
-
}
|
|
237
|
-
try {
|
|
238
|
-
return decodeChecksigAdd(script);
|
|
239
|
-
} catch (error) {
|
|
240
|
-
try {
|
|
241
|
-
return decodeChecksig(script);
|
|
242
|
-
} catch (error2) {
|
|
243
|
-
throw new Error(
|
|
244
|
-
`Failed to decode script: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
MultisigTapscript2.decode = decode2;
|
|
250
|
-
function decodeChecksigAdd(script) {
|
|
251
|
-
const asm = Script.decode(script);
|
|
252
|
-
const pubkeys = [];
|
|
253
|
-
let foundNumEqual = false;
|
|
254
|
-
for (let i = 0; i < asm.length; i++) {
|
|
255
|
-
const op = asm[i];
|
|
256
|
-
if (typeof op !== "string" && typeof op !== "number") {
|
|
257
|
-
if (op.length !== 32) {
|
|
258
|
-
throw new Error(`Invalid pubkey length: expected 32, got ${op.length}`);
|
|
259
|
-
}
|
|
260
|
-
pubkeys.push(op);
|
|
261
|
-
if (i + 1 >= asm.length || asm[i + 1] !== "CHECKSIGADD" && asm[i + 1] !== "CHECKSIG") {
|
|
262
|
-
throw new Error("Expected CHECKSIGADD or CHECKSIG after pubkey");
|
|
263
|
-
}
|
|
264
|
-
i++;
|
|
265
|
-
continue;
|
|
266
|
-
}
|
|
267
|
-
if (i === asm.length - 1) {
|
|
268
|
-
if (op !== "NUMEQUAL") {
|
|
269
|
-
throw new Error("Expected NUMEQUAL at end of script");
|
|
270
|
-
}
|
|
271
|
-
foundNumEqual = true;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
if (!foundNumEqual) {
|
|
275
|
-
throw new Error("Missing NUMEQUAL operation");
|
|
276
|
-
}
|
|
277
|
-
if (pubkeys.length === 0) {
|
|
278
|
-
throw new Error("Invalid script: must have at least 1 pubkey");
|
|
279
|
-
}
|
|
280
|
-
const reconstructed = encode2({
|
|
281
|
-
pubkeys,
|
|
282
|
-
type: 1 /* CHECKSIGADD */
|
|
283
|
-
});
|
|
284
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
285
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
286
|
-
}
|
|
287
|
-
return {
|
|
288
|
-
type: "multisig" /* Multisig */,
|
|
289
|
-
params: { pubkeys, type: 1 /* CHECKSIGADD */ },
|
|
290
|
-
script
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
function decodeChecksig(script) {
|
|
294
|
-
const asm = Script.decode(script);
|
|
295
|
-
const pubkeys = [];
|
|
296
|
-
for (let i = 0; i < asm.length; i++) {
|
|
297
|
-
const op = asm[i];
|
|
298
|
-
if (typeof op !== "string" && typeof op !== "number") {
|
|
299
|
-
if (op.length !== 32) {
|
|
300
|
-
throw new Error(`Invalid pubkey length: expected 32, got ${op.length}`);
|
|
301
|
-
}
|
|
302
|
-
pubkeys.push(op);
|
|
303
|
-
if (i + 1 >= asm.length) {
|
|
304
|
-
throw new Error("Unexpected end of script");
|
|
305
|
-
}
|
|
306
|
-
const nextOp = asm[i + 1];
|
|
307
|
-
if (nextOp !== "CHECKSIGVERIFY" && nextOp !== "CHECKSIG") {
|
|
308
|
-
throw new Error("Expected CHECKSIGVERIFY or CHECKSIG after pubkey");
|
|
309
|
-
}
|
|
310
|
-
if (i === asm.length - 2 && nextOp !== "CHECKSIG") {
|
|
311
|
-
throw new Error("Last operation must be CHECKSIG");
|
|
312
|
-
}
|
|
313
|
-
i++;
|
|
314
|
-
continue;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
if (pubkeys.length === 0) {
|
|
318
|
-
throw new Error("Invalid script: must have at least 1 pubkey");
|
|
319
|
-
}
|
|
320
|
-
const reconstructed = encode2({ pubkeys, type: 0 /* CHECKSIG */ });
|
|
321
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
322
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
323
|
-
}
|
|
324
|
-
return {
|
|
325
|
-
type: "multisig" /* Multisig */,
|
|
326
|
-
params: { pubkeys, type: 0 /* CHECKSIG */ },
|
|
327
|
-
script
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
function is(tapscript) {
|
|
331
|
-
return tapscript.type === "multisig" /* Multisig */;
|
|
332
|
-
}
|
|
333
|
-
MultisigTapscript2.is = is;
|
|
334
|
-
})(MultisigTapscript || (MultisigTapscript = {}));
|
|
335
|
-
var CSVMultisigTapscript;
|
|
336
|
-
((CSVMultisigTapscript2) => {
|
|
337
|
-
function encode2(params) {
|
|
338
|
-
for (const pubkey of params.pubkeys) {
|
|
339
|
-
if (pubkey.length !== 32) {
|
|
340
|
-
throw new Error(`Invalid pubkey length: expected 32, got ${pubkey.length}`);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
const sequence = MinimalScriptNum.encode(BigInt(timelockToSequence(params.timelock)));
|
|
344
|
-
const asm = [
|
|
345
|
-
sequence.length === 1 ? sequence[0] : sequence,
|
|
346
|
-
"CHECKSEQUENCEVERIFY",
|
|
347
|
-
"DROP"
|
|
348
|
-
];
|
|
349
|
-
const multisigScript = MultisigTapscript.encode(params);
|
|
350
|
-
const script = new Uint8Array([...Script.encode(asm), ...multisigScript.script]);
|
|
351
|
-
return {
|
|
352
|
-
type: "csv-multisig" /* CSVMultisig */,
|
|
353
|
-
params,
|
|
354
|
-
script
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
CSVMultisigTapscript2.encode = encode2;
|
|
358
|
-
function decode2(script) {
|
|
359
|
-
if (script.length === 0) {
|
|
360
|
-
throw new Error("Failed to decode: script is empty");
|
|
361
|
-
}
|
|
362
|
-
const isValid = isScriptValid(script);
|
|
363
|
-
if (isValid instanceof Error) {
|
|
364
|
-
throw isValid;
|
|
365
|
-
}
|
|
366
|
-
const asm = Script.decode(script);
|
|
367
|
-
const sequence = asm[0];
|
|
368
|
-
const multisigScript = new Uint8Array(Script.encode(asm.slice(3)));
|
|
369
|
-
let multisig;
|
|
370
|
-
try {
|
|
371
|
-
multisig = MultisigTapscript.decode(multisigScript);
|
|
372
|
-
} catch (error) {
|
|
373
|
-
throw new Error(
|
|
374
|
-
`Invalid multisig script: ${error instanceof Error ? error.message : String(error)}`
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
let sequenceNum;
|
|
378
|
-
if (typeof sequence === "number") {
|
|
379
|
-
sequenceNum = sequence;
|
|
380
|
-
} else {
|
|
381
|
-
sequenceNum = Number(MinimalScriptNum.decode(sequence));
|
|
382
|
-
}
|
|
383
|
-
const timelock = sequenceToTimelock(sequenceNum);
|
|
384
|
-
const reconstructed = encode2({
|
|
385
|
-
timelock,
|
|
386
|
-
...multisig.params
|
|
387
|
-
});
|
|
388
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
389
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
390
|
-
}
|
|
391
|
-
return {
|
|
392
|
-
type: "csv-multisig" /* CSVMultisig */,
|
|
393
|
-
params: {
|
|
394
|
-
timelock,
|
|
395
|
-
...multisig.params
|
|
396
|
-
},
|
|
397
|
-
script
|
|
398
|
-
};
|
|
399
|
-
}
|
|
400
|
-
CSVMultisigTapscript2.decode = decode2;
|
|
401
|
-
function is(tapscript) {
|
|
402
|
-
return tapscript.type === "csv-multisig" /* CSVMultisig */;
|
|
403
|
-
}
|
|
404
|
-
CSVMultisigTapscript2.is = is;
|
|
405
|
-
function isScriptValid(script) {
|
|
406
|
-
const asm = Script.decode(script);
|
|
407
|
-
if (asm.length < 3) {
|
|
408
|
-
return new Error(`Invalid script: too short (expected at least 3)`);
|
|
409
|
-
}
|
|
410
|
-
const sequence = asm[0];
|
|
411
|
-
if (typeof sequence === "string") {
|
|
412
|
-
return new Error("Invalid script: expected sequence number");
|
|
413
|
-
}
|
|
414
|
-
if (asm[1] !== "CHECKSEQUENCEVERIFY" || asm[2] !== "DROP") {
|
|
415
|
-
return new Error("Invalid script: expected CHECKSEQUENCEVERIFY DROP");
|
|
416
|
-
}
|
|
417
|
-
return true;
|
|
418
|
-
}
|
|
419
|
-
CSVMultisigTapscript2.isScriptValid = isScriptValid;
|
|
420
|
-
})(CSVMultisigTapscript || (CSVMultisigTapscript = {}));
|
|
421
|
-
function isConditionScriptValid(script) {
|
|
422
|
-
const asm = Script.decode(script);
|
|
423
|
-
if (asm.length < 1) {
|
|
424
|
-
return new Error(`Invalid script: too short (expected at least 1)`);
|
|
425
|
-
}
|
|
426
|
-
if (asm.lastIndexOf("VERIFY") === -1) {
|
|
427
|
-
return new Error("Invalid script: missing VERIFY operation");
|
|
428
|
-
}
|
|
429
|
-
return true;
|
|
430
|
-
}
|
|
431
|
-
var ConditionCSVMultisigTapscript;
|
|
432
|
-
((ConditionCSVMultisigTapscript2) => {
|
|
433
|
-
function encode2(params) {
|
|
434
|
-
const script = new Uint8Array([
|
|
435
|
-
...params.conditionScript,
|
|
436
|
-
...Script.encode(["VERIFY"]),
|
|
437
|
-
...CSVMultisigTapscript.encode(params).script
|
|
438
|
-
]);
|
|
439
|
-
return {
|
|
440
|
-
type: "condition-csv-multisig" /* ConditionCSVMultisig */,
|
|
441
|
-
params,
|
|
442
|
-
script
|
|
443
|
-
};
|
|
444
|
-
}
|
|
445
|
-
ConditionCSVMultisigTapscript2.encode = encode2;
|
|
446
|
-
function decode2(script) {
|
|
447
|
-
if (script.length === 0) {
|
|
448
|
-
throw new Error("Failed to decode: script is empty");
|
|
449
|
-
}
|
|
450
|
-
const isValid = isScriptValid(script);
|
|
451
|
-
if (isValid instanceof Error) {
|
|
452
|
-
throw isValid;
|
|
453
|
-
}
|
|
454
|
-
const asm = Script.decode(script);
|
|
455
|
-
const verifyIndex = asm.lastIndexOf("VERIFY");
|
|
456
|
-
if (verifyIndex === -1) {
|
|
457
|
-
throw Error("Invalid script: missing VERIFY operation");
|
|
458
|
-
}
|
|
459
|
-
const conditionScript = new Uint8Array(Script.encode(asm.slice(0, verifyIndex)));
|
|
460
|
-
const csvMultisigScript = new Uint8Array(Script.encode(asm.slice(verifyIndex + 1)));
|
|
461
|
-
let csvMultisig;
|
|
462
|
-
try {
|
|
463
|
-
csvMultisig = CSVMultisigTapscript.decode(csvMultisigScript);
|
|
464
|
-
} catch (error) {
|
|
465
|
-
throw new Error(
|
|
466
|
-
`Invalid CSV multisig script: ${error instanceof Error ? error.message : String(error)}`
|
|
467
|
-
);
|
|
468
|
-
}
|
|
469
|
-
const reconstructed = encode2({
|
|
470
|
-
conditionScript,
|
|
471
|
-
...csvMultisig.params
|
|
472
|
-
});
|
|
473
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
474
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
475
|
-
}
|
|
476
|
-
return {
|
|
477
|
-
type: "condition-csv-multisig" /* ConditionCSVMultisig */,
|
|
478
|
-
params: {
|
|
479
|
-
conditionScript,
|
|
480
|
-
...csvMultisig.params
|
|
481
|
-
},
|
|
482
|
-
script
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
ConditionCSVMultisigTapscript2.decode = decode2;
|
|
486
|
-
function is(tapscript) {
|
|
487
|
-
return tapscript.type === "condition-csv-multisig" /* ConditionCSVMultisig */;
|
|
488
|
-
}
|
|
489
|
-
ConditionCSVMultisigTapscript2.is = is;
|
|
490
|
-
function isScriptValid(script) {
|
|
491
|
-
return isConditionScriptValid(script);
|
|
492
|
-
}
|
|
493
|
-
ConditionCSVMultisigTapscript2.isScriptValid = isScriptValid;
|
|
494
|
-
})(ConditionCSVMultisigTapscript || (ConditionCSVMultisigTapscript = {}));
|
|
495
|
-
var ConditionMultisigTapscript;
|
|
496
|
-
((ConditionMultisigTapscript2) => {
|
|
497
|
-
function encode2(params) {
|
|
498
|
-
const script = new Uint8Array([
|
|
499
|
-
...params.conditionScript,
|
|
500
|
-
...Script.encode(["VERIFY"]),
|
|
501
|
-
...MultisigTapscript.encode(params).script
|
|
502
|
-
]);
|
|
503
|
-
return {
|
|
504
|
-
type: "condition-multisig" /* ConditionMultisig */,
|
|
505
|
-
params,
|
|
506
|
-
script
|
|
507
|
-
};
|
|
508
|
-
}
|
|
509
|
-
ConditionMultisigTapscript2.encode = encode2;
|
|
510
|
-
function decode2(script) {
|
|
511
|
-
if (script.length === 0) {
|
|
512
|
-
throw new Error("Failed to decode: script is empty");
|
|
513
|
-
}
|
|
514
|
-
const isValid = isScriptValid(script);
|
|
515
|
-
if (isValid instanceof Error) {
|
|
516
|
-
throw isValid;
|
|
517
|
-
}
|
|
518
|
-
const asm = Script.decode(script);
|
|
519
|
-
const verifyIndex = asm.lastIndexOf("VERIFY");
|
|
520
|
-
if (verifyIndex === -1) {
|
|
521
|
-
throw Error("Invalid script: missing VERIFY operation");
|
|
522
|
-
}
|
|
523
|
-
const conditionScript = new Uint8Array(Script.encode(asm.slice(0, verifyIndex)));
|
|
524
|
-
const multisigScript = new Uint8Array(Script.encode(asm.slice(verifyIndex + 1)));
|
|
525
|
-
let multisig;
|
|
526
|
-
try {
|
|
527
|
-
multisig = MultisigTapscript.decode(multisigScript);
|
|
528
|
-
} catch (error) {
|
|
529
|
-
throw new Error(
|
|
530
|
-
`Invalid multisig script: ${error instanceof Error ? error.message : String(error)}`
|
|
531
|
-
);
|
|
532
|
-
}
|
|
533
|
-
const reconstructed = encode2({
|
|
534
|
-
conditionScript,
|
|
535
|
-
...multisig.params
|
|
536
|
-
});
|
|
537
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
538
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
539
|
-
}
|
|
540
|
-
return {
|
|
541
|
-
type: "condition-multisig" /* ConditionMultisig */,
|
|
542
|
-
params: {
|
|
543
|
-
conditionScript,
|
|
544
|
-
...multisig.params
|
|
545
|
-
},
|
|
546
|
-
script
|
|
547
|
-
};
|
|
548
|
-
}
|
|
549
|
-
ConditionMultisigTapscript2.decode = decode2;
|
|
550
|
-
function is(tapscript) {
|
|
551
|
-
return tapscript.type === "condition-multisig" /* ConditionMultisig */;
|
|
552
|
-
}
|
|
553
|
-
ConditionMultisigTapscript2.is = is;
|
|
554
|
-
function isScriptValid(script) {
|
|
555
|
-
return isConditionScriptValid(script);
|
|
556
|
-
}
|
|
557
|
-
ConditionMultisigTapscript2.isScriptValid = isScriptValid;
|
|
558
|
-
})(ConditionMultisigTapscript || (ConditionMultisigTapscript = {}));
|
|
559
|
-
var CLTVMultisigTapscript;
|
|
560
|
-
((CLTVMultisigTapscript2) => {
|
|
561
|
-
function encode2(params) {
|
|
562
|
-
const locktime = MinimalScriptNum.encode(params.absoluteTimelock);
|
|
563
|
-
const asm = [
|
|
564
|
-
locktime.length === 1 ? locktime[0] : locktime,
|
|
565
|
-
"CHECKLOCKTIMEVERIFY",
|
|
566
|
-
"DROP"
|
|
567
|
-
];
|
|
568
|
-
const timelockedScript = Script.encode(asm);
|
|
569
|
-
const script = new Uint8Array([
|
|
570
|
-
...timelockedScript,
|
|
571
|
-
...MultisigTapscript.encode(params).script
|
|
572
|
-
]);
|
|
573
|
-
return {
|
|
574
|
-
type: "cltv-multisig" /* CLTVMultisig */,
|
|
575
|
-
params,
|
|
576
|
-
script
|
|
577
|
-
};
|
|
578
|
-
}
|
|
579
|
-
CLTVMultisigTapscript2.encode = encode2;
|
|
580
|
-
function decode2(script) {
|
|
581
|
-
if (script.length === 0) {
|
|
582
|
-
throw new Error("Failed to decode: script is empty");
|
|
583
|
-
}
|
|
584
|
-
const isValid = isScriptValid(script);
|
|
585
|
-
if (isValid instanceof Error) {
|
|
586
|
-
throw isValid;
|
|
587
|
-
}
|
|
588
|
-
const asm = Script.decode(script);
|
|
589
|
-
const locktime = asm[0];
|
|
590
|
-
if (typeof locktime === "string") {
|
|
591
|
-
throw new Error("Invalid script: expected locktime number");
|
|
592
|
-
}
|
|
593
|
-
if (asm[1] !== "CHECKLOCKTIMEVERIFY" || asm[2] !== "DROP") {
|
|
594
|
-
throw new Error("Invalid script: expected CHECKLOCKTIMEVERIFY DROP");
|
|
595
|
-
}
|
|
596
|
-
const multisigScript = new Uint8Array(Script.encode(asm.slice(3)));
|
|
597
|
-
let multisig;
|
|
598
|
-
try {
|
|
599
|
-
multisig = MultisigTapscript.decode(multisigScript);
|
|
600
|
-
} catch (error) {
|
|
601
|
-
throw new Error(
|
|
602
|
-
`Invalid multisig script: ${error instanceof Error ? error.message : String(error)}`
|
|
603
|
-
);
|
|
604
|
-
}
|
|
605
|
-
let absoluteTimelock;
|
|
606
|
-
if (typeof locktime === "number") {
|
|
607
|
-
absoluteTimelock = BigInt(locktime);
|
|
608
|
-
} else {
|
|
609
|
-
absoluteTimelock = MinimalScriptNum.decode(locktime);
|
|
610
|
-
}
|
|
611
|
-
const reconstructed = encode2({
|
|
612
|
-
absoluteTimelock,
|
|
613
|
-
...multisig.params
|
|
614
|
-
});
|
|
615
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
616
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
617
|
-
}
|
|
618
|
-
return {
|
|
619
|
-
type: "cltv-multisig" /* CLTVMultisig */,
|
|
620
|
-
params: {
|
|
621
|
-
absoluteTimelock,
|
|
622
|
-
...multisig.params
|
|
623
|
-
},
|
|
624
|
-
script
|
|
625
|
-
};
|
|
626
|
-
}
|
|
627
|
-
CLTVMultisigTapscript2.decode = decode2;
|
|
628
|
-
function is(tapscript) {
|
|
629
|
-
return tapscript.type === "cltv-multisig" /* CLTVMultisig */;
|
|
630
|
-
}
|
|
631
|
-
CLTVMultisigTapscript2.is = is;
|
|
632
|
-
function isScriptValid(script) {
|
|
633
|
-
const asm = Script.decode(script);
|
|
634
|
-
if (asm.length < 3) {
|
|
635
|
-
return new Error(`Invalid script: too short (expected at least 3)`);
|
|
636
|
-
}
|
|
637
|
-
const locktime = asm[0];
|
|
638
|
-
if (typeof locktime === "string") {
|
|
639
|
-
return new Error("Invalid script: expected locktime as number or bytes");
|
|
640
|
-
}
|
|
641
|
-
if (asm[1] !== "CHECKLOCKTIMEVERIFY" || asm[2] !== "DROP") {
|
|
642
|
-
return new Error("Invalid script: expected CHECKLOCKTIMEVERIFY DROP");
|
|
643
|
-
}
|
|
644
|
-
return true;
|
|
645
|
-
}
|
|
646
|
-
CLTVMultisigTapscript2.isScriptValid = isScriptValid;
|
|
647
|
-
})(CLTVMultisigTapscript || (CLTVMultisigTapscript = {}));
|
|
648
|
-
function assembleBtcdTaprootTree(scripts) {
|
|
649
|
-
if (scripts.length === 0) {
|
|
650
|
-
throw new Error("assembleBtcdTaprootTree: empty scripts list");
|
|
651
|
-
}
|
|
652
|
-
const leaves = scripts.map((script) => ({
|
|
653
|
-
script,
|
|
654
|
-
leafVersion: TAP_LEAF_VERSION
|
|
655
|
-
}));
|
|
656
|
-
if (leaves.length === 1) {
|
|
657
|
-
return leaves[0];
|
|
658
|
-
}
|
|
659
|
-
const branches = [];
|
|
660
|
-
for (let i = 0; i < leaves.length; i += 2) {
|
|
661
|
-
if (i === leaves.length - 1) {
|
|
662
|
-
const last = branches.pop();
|
|
663
|
-
if (last === void 0) {
|
|
664
|
-
throw new Error(
|
|
665
|
-
`assembleBtcdTaprootTree: unexpected odd leaf at i=${i} with no prior branch`
|
|
666
|
-
);
|
|
667
|
-
}
|
|
668
|
-
branches.push([last, leaves[i]]);
|
|
669
|
-
} else {
|
|
670
|
-
branches.push([leaves[i], leaves[i + 1]]);
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
while (branches.length >= 2) {
|
|
674
|
-
const left = branches.shift();
|
|
675
|
-
const right = branches.shift();
|
|
676
|
-
branches.push([left, right]);
|
|
677
|
-
}
|
|
678
|
-
return branches[0];
|
|
679
|
-
}
|
|
680
|
-
function btcdLeafLayout(count) {
|
|
681
|
-
const placeholders = Array.from({ length: count }, () => new Uint8Array());
|
|
682
|
-
const indexOf = new Map(placeholders.map((script, index) => [script, index]));
|
|
683
|
-
const layout = [];
|
|
684
|
-
const walk = (node, depth) => {
|
|
685
|
-
if (Array.isArray(node)) {
|
|
686
|
-
walk(node[0], depth + 1);
|
|
687
|
-
walk(node[1], depth + 1);
|
|
688
|
-
} else {
|
|
689
|
-
layout.push({ index: indexOf.get(node.script), depth });
|
|
690
|
-
}
|
|
691
|
-
};
|
|
692
|
-
walk(assembleBtcdTaprootTree(placeholders), 0);
|
|
693
|
-
return layout;
|
|
694
|
-
}
|
|
695
|
-
var TapTreeCoder = P.array(
|
|
696
|
-
null,
|
|
697
|
-
P.struct({ depth: P.U8, version: P.U8, script: VarBytes })
|
|
698
|
-
);
|
|
699
|
-
function scriptFromTapLeafScript(leaf) {
|
|
700
|
-
return leaf[1].subarray(0, leaf[1].length - 1);
|
|
701
|
-
}
|
|
702
|
-
var VtxoScript = class _VtxoScript {
|
|
703
|
-
/**
|
|
704
|
-
* Create a virtual output script from its tapleaf scripts.
|
|
705
|
-
*
|
|
706
|
-
* The Taproot script tree is assembled using btcd's algorithm
|
|
707
|
-
* (`txscript.AssembleTaprootScriptTree`) so the derived taproot output
|
|
708
|
-
* key agrees with arkd for any leaf count. `@scure/btc-signer`'s
|
|
709
|
-
* default `taprootListToTree` is a Huffman builder that only agrees
|
|
710
|
-
* with arkd for power-of-2 leaf counts.
|
|
711
|
-
*
|
|
712
|
-
* @param scripts - Raw tapscript bytes for each leaf
|
|
713
|
-
* @throws Error if the provided leaves cannot produce a valid Taproot tree
|
|
714
|
-
*/
|
|
715
|
-
constructor(scripts) {
|
|
716
|
-
this.scripts = scripts;
|
|
717
|
-
const tapTree = assembleBtcdTaprootTree(scripts);
|
|
718
|
-
const payment = p2tr(taprootNumsKey(), tapTree, void 0, true);
|
|
719
|
-
if (!payment.tapLeafScript || payment.tapLeafScript.length !== scripts.length) {
|
|
720
|
-
throw new Error("invalid scripts");
|
|
721
|
-
}
|
|
722
|
-
this.leaves = payment.tapLeafScript;
|
|
723
|
-
this.tweakedPublicKey = payment.tweakedPubkey;
|
|
724
|
-
this.pkScript = payment.script;
|
|
725
|
-
}
|
|
726
|
-
scripts;
|
|
727
|
-
leaves;
|
|
728
|
-
tweakedPublicKey;
|
|
729
|
-
pkScript;
|
|
730
|
-
/**
|
|
731
|
-
* Decode a virtual output script from an encoded TapTree.
|
|
732
|
-
*
|
|
733
|
-
* Leaves are normally read in construction order and their depths
|
|
734
|
-
* ignored: that covers the {@link encode} form (every depth 1) and the
|
|
735
|
-
* placeholder depths other writers use, such as arkd's. The exception is
|
|
736
|
-
* the BIP-371 form {@link toBIP371TapTree} writes, whose leaves are in the
|
|
737
|
-
* tree's DFS order: for some leaf counts (6, 7, 10, ...) that differs from
|
|
738
|
-
* construction order. A tree whose depths are exactly the shape
|
|
739
|
-
* `assembleBtcdTaprootTree` builds is that form, so its leaves are mapped
|
|
740
|
-
* back to construction order first. No construction-order writer produces
|
|
741
|
-
* that depth sequence when the two orders differ, so this never reorders
|
|
742
|
-
* a tree that decoded correctly before.
|
|
743
|
-
*
|
|
744
|
-
* Every leaf must carry the base tapscript version: the tree is rebuilt
|
|
745
|
-
* with {@link TAP_LEAF_VERSION} leaves, so any other version would
|
|
746
|
-
* silently derive a different taproot key.
|
|
747
|
-
*
|
|
748
|
-
* @param tapTree - Encoded TapTree bytes
|
|
749
|
-
* @returns Decoded virtual output script
|
|
750
|
-
* @throws Error if the TapTree cannot be decoded into a valid script set
|
|
751
|
-
* or a leaf carries a version other than {@link TAP_LEAF_VERSION}
|
|
752
|
-
* @see encode
|
|
753
|
-
*/
|
|
754
|
-
static decode(tapTree) {
|
|
755
|
-
const leaves = TapTreeCoder.decode(tapTree);
|
|
756
|
-
const foreign = leaves.find((leaf) => leaf.version !== TAP_LEAF_VERSION);
|
|
757
|
-
if (foreign) {
|
|
758
|
-
throw new Error(`unsupported tap leaf version 0x${foreign.version.toString(16)}`);
|
|
759
|
-
}
|
|
760
|
-
const scripts = leaves.map((leaf) => leaf.script);
|
|
761
|
-
if (leaves.every((leaf) => leaf.depth === 1)) {
|
|
762
|
-
return new _VtxoScript(scripts);
|
|
763
|
-
}
|
|
764
|
-
const layout = btcdLeafLayout(leaves.length);
|
|
765
|
-
if (layout.some((slot, i) => slot.depth !== leaves[i].depth)) {
|
|
766
|
-
return new _VtxoScript(scripts);
|
|
767
|
-
}
|
|
768
|
-
const ordered = new Array(leaves.length);
|
|
769
|
-
layout.forEach((slot, i) => {
|
|
770
|
-
ordered[slot.index] = scripts[i];
|
|
771
|
-
});
|
|
772
|
-
return new _VtxoScript(ordered);
|
|
773
|
-
}
|
|
774
|
-
/**
|
|
775
|
-
* Encode the virtual output script to a TapTree byte representation.
|
|
776
|
-
*
|
|
777
|
-
* @returns Encoded TapTree bytes
|
|
778
|
-
* @see decode
|
|
779
|
-
*/
|
|
780
|
-
encode() {
|
|
781
|
-
const tapTree = TapTreeCoder.encode(
|
|
782
|
-
this.scripts.map((script) => ({
|
|
783
|
-
depth: 1,
|
|
784
|
-
version: TAP_LEAF_VERSION,
|
|
785
|
-
script
|
|
786
|
-
}))
|
|
787
|
-
);
|
|
788
|
-
return tapTree;
|
|
789
|
-
}
|
|
790
|
-
/**
|
|
791
|
-
* Build the Arkade address corresponding to this virtual output script.
|
|
792
|
-
*
|
|
793
|
-
* @param prefix - Bech32 human-readable prefix
|
|
794
|
-
* @param serverPubKey - 32-byte Arkade server public key
|
|
795
|
-
* @returns Arkade address for this script
|
|
796
|
-
* @see ArkAddress
|
|
797
|
-
*/
|
|
798
|
-
address(prefix = DEFAULT_NETWORK.hrp, serverPubKey) {
|
|
799
|
-
return new ArkAddress(serverPubKey, this.tweakedPublicKey, prefix);
|
|
800
|
-
}
|
|
801
|
-
/**
|
|
802
|
-
* Build the Taproot onchain address corresponding to this virtual output script.
|
|
803
|
-
*
|
|
804
|
-
* @param network - Bitcoin network descriptor
|
|
805
|
-
* @returns Taproot onchain address
|
|
806
|
-
* @see address
|
|
807
|
-
*/
|
|
808
|
-
onchainAddress(network = DEFAULT_NETWORK) {
|
|
809
|
-
return Address(network).encode({
|
|
810
|
-
type: "tr",
|
|
811
|
-
pubkey: this.tweakedPublicKey
|
|
812
|
-
});
|
|
813
|
-
}
|
|
814
|
-
/**
|
|
815
|
-
* Look up a tapleaf script by its hex-encoded tapscript body.
|
|
816
|
-
*
|
|
817
|
-
* @param scriptHex - Hex-encoded tapscript body without the leaf version byte
|
|
818
|
-
* @returns Matching tapleaf script
|
|
819
|
-
* @throws Error if no matching leaf exists
|
|
820
|
-
*/
|
|
821
|
-
findLeaf(scriptHex) {
|
|
822
|
-
const leaf = this.leaves.find(
|
|
823
|
-
(leaf2) => hex.encode(scriptFromTapLeafScript(leaf2)) === scriptHex
|
|
824
|
-
);
|
|
825
|
-
if (!leaf) {
|
|
826
|
-
throw new Error(`leaf '${scriptHex}' not found`);
|
|
827
|
-
}
|
|
828
|
-
return leaf;
|
|
829
|
-
}
|
|
830
|
-
/**
|
|
831
|
-
* Return all unilateral exit paths embedded in the virtual output script.
|
|
832
|
-
*
|
|
833
|
-
* @returns CSV-based exit paths found in the leaves
|
|
834
|
-
* @see getSequence
|
|
835
|
-
*/
|
|
836
|
-
exitPaths() {
|
|
837
|
-
const paths = [];
|
|
838
|
-
for (const leaf of this.leaves) {
|
|
839
|
-
try {
|
|
840
|
-
const script = scriptFromTapLeafScript(leaf);
|
|
841
|
-
if (CSVMultisigTapscript.isScriptValid(script) === true) {
|
|
842
|
-
const tapScript = CSVMultisigTapscript.decode(script);
|
|
843
|
-
paths.push(tapScript);
|
|
844
|
-
} else if (ConditionCSVMultisigTapscript.isScriptValid(script) === true) {
|
|
845
|
-
const tapScript = ConditionCSVMultisigTapscript.decode(script);
|
|
846
|
-
paths.push(tapScript);
|
|
847
|
-
}
|
|
848
|
-
} catch (e) {
|
|
849
|
-
console.debug("Failed to decode script", e);
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
return paths;
|
|
853
|
-
}
|
|
854
|
-
};
|
|
855
|
-
function toBIP371TapTree(tapTree) {
|
|
856
|
-
const scripts = TapTreeCoder.decode(tapTree).map((leaf) => leaf.script);
|
|
857
|
-
const vtxoScript = new VtxoScript(scripts);
|
|
858
|
-
return vtxoScript.leaves.map((leaf) => ({
|
|
859
|
-
depth: leaf[0].merklePath.length,
|
|
860
|
-
version: TAP_LEAF_VERSION,
|
|
861
|
-
script: scriptFromTapLeafScript(leaf)
|
|
862
|
-
}));
|
|
863
|
-
}
|
|
864
|
-
function getSequence(tapLeafScript) {
|
|
865
|
-
let sequence = void 0;
|
|
866
|
-
try {
|
|
867
|
-
const scriptWithLeafVersion = tapLeafScript[1];
|
|
868
|
-
const script = scriptWithLeafVersion.subarray(0, scriptWithLeafVersion.length - 1);
|
|
869
|
-
try {
|
|
870
|
-
const params = CSVMultisigTapscript.decode(script).params;
|
|
871
|
-
sequence = timelockToSequence(params.timelock);
|
|
872
|
-
} catch {
|
|
873
|
-
const params = CLTVMultisigTapscript.decode(script).params;
|
|
874
|
-
sequence = Number(params.absoluteTimelock);
|
|
875
|
-
}
|
|
876
|
-
} catch {
|
|
877
|
-
}
|
|
878
|
-
return sequence;
|
|
879
|
-
}
|
|
880
8
|
var Transaction = class extends Transaction$1 {
|
|
881
9
|
static ARK_TX_OPTS = {
|
|
882
10
|
unknown: "ignore",
|
|
@@ -1172,7 +300,7 @@ function checkKeyMatch(key, arkPsbtFieldKey, prefixOnly = false) {
|
|
|
1172
300
|
}
|
|
1173
301
|
|
|
1174
302
|
// package.json
|
|
1175
|
-
var version = "0.5.
|
|
303
|
+
var version = "0.5.5";
|
|
1176
304
|
|
|
1177
305
|
// src/utils/fetch.ts
|
|
1178
306
|
var buildVersion = "0.9.9";
|
|
@@ -1361,7 +489,8 @@ var ArkErrorName = {
|
|
|
1361
489
|
* timestamp**, not wall clock, so a prompt spend is rejected until a later block lands.
|
|
1362
490
|
*/
|
|
1363
491
|
FORFEIT_CLOSURE_LOCKED: "FORFEIT_CLOSURE_LOCKED",
|
|
1364
|
-
INVALID_INTENT_PROOF: "INVALID_INTENT_PROOF"
|
|
492
|
+
INVALID_INTENT_PROOF: "INVALID_INTENT_PROOF",
|
|
493
|
+
INTERNAL_ERROR: "INTERNAL_ERROR"
|
|
1365
494
|
};
|
|
1366
495
|
function isArkError(error, name) {
|
|
1367
496
|
return error instanceof ArkError && (name === void 0 || error.name === name);
|
|
@@ -1584,7 +713,7 @@ function prepareProofInput(coin) {
|
|
|
1584
713
|
return coin;
|
|
1585
714
|
}
|
|
1586
715
|
const vtxoScript = VtxoScript.decode(coin.tapTree);
|
|
1587
|
-
const sequence = getSequence(coin
|
|
716
|
+
const sequence = getSequence(proofTapLeafScriptOf(coin));
|
|
1588
717
|
const unknown = [VtxoTaprootTree.encode(coin.tapTree)];
|
|
1589
718
|
if (coin.extraWitness) {
|
|
1590
719
|
unknown.push(ConditionWitness.encode(coin.extraWitness));
|
|
@@ -1600,7 +729,7 @@ function prepareProofInput(coin) {
|
|
|
1600
729
|
script: vtxoScript.pkScript
|
|
1601
730
|
},
|
|
1602
731
|
sequence,
|
|
1603
|
-
tapLeafScript: [coin
|
|
732
|
+
tapLeafScript: [proofTapLeafScriptOf(coin)],
|
|
1604
733
|
unknown
|
|
1605
734
|
};
|
|
1606
735
|
}
|
|
@@ -1863,6 +992,35 @@ var DigestMismatchError = class extends Error {
|
|
|
1863
992
|
this.name = "DigestMismatchError";
|
|
1864
993
|
}
|
|
1865
994
|
};
|
|
995
|
+
var ONCHAIN_COSIGN_UNSUPPORTED_PREFIX = "Arkade server does not support onchain cosigning";
|
|
996
|
+
var ONCHAIN_COSIGN_REJECTED_PREFIX = "Arkade server refused to cosign onchain tx: ";
|
|
997
|
+
var OnchainCosignUnsupportedError = class extends Error {
|
|
998
|
+
constructor() {
|
|
999
|
+
super(ONCHAIN_COSIGN_UNSUPPORTED_PREFIX);
|
|
1000
|
+
this.name = "OnchainCosignUnsupportedError";
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
var OnchainCosignRejectedError = class extends Error {
|
|
1004
|
+
constructor(serverMessage, arkErrorName) {
|
|
1005
|
+
super(
|
|
1006
|
+
`${ONCHAIN_COSIGN_REJECTED_PREFIX}${arkErrorName ? `[${arkErrorName}] ` : ""}${serverMessage}`
|
|
1007
|
+
);
|
|
1008
|
+
this.serverMessage = serverMessage;
|
|
1009
|
+
this.arkErrorName = arkErrorName;
|
|
1010
|
+
this.name = "OnchainCosignRejectedError";
|
|
1011
|
+
}
|
|
1012
|
+
serverMessage;
|
|
1013
|
+
arkErrorName;
|
|
1014
|
+
};
|
|
1015
|
+
var ONCHAIN_COSIGN_AMBIGUOUS_PREFIX = "Onchain cosign outcome unknown, the transaction may have been broadcast: ";
|
|
1016
|
+
var OnchainCosignAmbiguousError = class extends Error {
|
|
1017
|
+
constructor(detail, options) {
|
|
1018
|
+
super(`${ONCHAIN_COSIGN_AMBIGUOUS_PREFIX}${detail}`, options);
|
|
1019
|
+
this.detail = detail;
|
|
1020
|
+
this.name = "OnchainCosignAmbiguousError";
|
|
1021
|
+
}
|
|
1022
|
+
detail;
|
|
1023
|
+
};
|
|
1866
1024
|
var SettlementEventType = /* @__PURE__ */ ((SettlementEventType2) => {
|
|
1867
1025
|
SettlementEventType2["BatchStarted"] = "batch_started";
|
|
1868
1026
|
SettlementEventType2["BatchFinalization"] = "batch_finalization";
|
|
@@ -1894,6 +1052,7 @@ var RestOperatorProvider = class {
|
|
|
1894
1052
|
* stale. Empty until the first {@link getInfo}.
|
|
1895
1053
|
*/
|
|
1896
1054
|
_digest = "";
|
|
1055
|
+
_onchainCosignUnsupported = false;
|
|
1897
1056
|
_hasServerInfo = false;
|
|
1898
1057
|
_suppressNextGetInfoChangeEmit = false;
|
|
1899
1058
|
_serverInfoListeners = /* @__PURE__ */ new Set();
|
|
@@ -1909,6 +1068,7 @@ var RestOperatorProvider = class {
|
|
|
1909
1068
|
};
|
|
1910
1069
|
}
|
|
1911
1070
|
emitServerInfoChanged(info) {
|
|
1071
|
+
this._onchainCosignUnsupported = false;
|
|
1912
1072
|
for (const listener of this._serverInfoListeners) {
|
|
1913
1073
|
try {
|
|
1914
1074
|
listener(info);
|
|
@@ -1952,7 +1112,7 @@ var RestOperatorProvider = class {
|
|
|
1952
1112
|
return response;
|
|
1953
1113
|
}
|
|
1954
1114
|
const arkError = maybeArkError(new Error(body));
|
|
1955
|
-
if (!arkError) throwIfHttpUnavailable(response, "arkade");
|
|
1115
|
+
if (!arkError && response.status !== 501) throwIfHttpUnavailable(response, "arkade");
|
|
1956
1116
|
if (!isArkError(arkError, ArkErrorName.DIGEST_MISMATCH)) return response;
|
|
1957
1117
|
this._digest = "";
|
|
1958
1118
|
this._suppressNextGetInfoChangeEmit = true;
|
|
@@ -2064,6 +1224,41 @@ var RestOperatorProvider = class {
|
|
|
2064
1224
|
signedCheckpointTxs: data.signedCheckpointTxs
|
|
2065
1225
|
};
|
|
2066
1226
|
}
|
|
1227
|
+
async cosignOnchainTx(psbtB64) {
|
|
1228
|
+
if (this._onchainCosignUnsupported) throw new OnchainCosignUnsupportedError();
|
|
1229
|
+
let response;
|
|
1230
|
+
let text;
|
|
1231
|
+
try {
|
|
1232
|
+
response = await this.authedFetch(`${this.serverUrl}/v1/tx/onchain/cosign`, {
|
|
1233
|
+
method: "POST",
|
|
1234
|
+
headers: { "Content-Type": "application/json" },
|
|
1235
|
+
body: JSON.stringify({ tx: psbtB64 })
|
|
1236
|
+
});
|
|
1237
|
+
if (response.ok) {
|
|
1238
|
+
const { txid } = await response.json();
|
|
1239
|
+
if (typeof txid !== "string" || !txid)
|
|
1240
|
+
throw new Error("cosign response has no txid");
|
|
1241
|
+
return txid;
|
|
1242
|
+
}
|
|
1243
|
+
text = await response.text();
|
|
1244
|
+
} catch (e) {
|
|
1245
|
+
if (e instanceof DigestMismatchError) throw e;
|
|
1246
|
+
throw new OnchainCosignAmbiguousError(e instanceof Error ? e.message : String(e), {
|
|
1247
|
+
cause: e
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
const arkError = maybeArkError(new Error(text));
|
|
1251
|
+
if (!arkError && (response.status === 404 || response.status === 501)) {
|
|
1252
|
+
this._onchainCosignUnsupported = true;
|
|
1253
|
+
throw new OnchainCosignUnsupportedError();
|
|
1254
|
+
}
|
|
1255
|
+
if (!arkError || arkError.name === ArkErrorName.INTERNAL_ERROR) {
|
|
1256
|
+
throw new OnchainCosignAmbiguousError(
|
|
1257
|
+
arkError?.message ?? `${response.status} ${text}`
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1260
|
+
throw new OnchainCosignRejectedError(arkError.message, arkError.name);
|
|
1261
|
+
}
|
|
2067
1262
|
async finalizeTx(arkTxid, finalCheckpointTxs) {
|
|
2068
1263
|
const url = `${this.serverUrl}/v1/tx/finalize`;
|
|
2069
1264
|
await this.postJson(
|
|
@@ -2639,6 +1834,6 @@ function isVirtualCoin(input) {
|
|
|
2639
1834
|
return typeof input === "object" && input !== null && typeof input.script === "string";
|
|
2640
1835
|
}
|
|
2641
1836
|
|
|
2642
|
-
export { ARKADE_VERSION_HEADERS,
|
|
2643
|
-
//# sourceMappingURL=chunk-
|
|
2644
|
-
//# sourceMappingURL=chunk-
|
|
1837
|
+
export { ARKADE_VERSION_HEADERS, ArkError, ArkErrorName, ArkPsbtFieldKey, ArkPsbtFieldKeyType, ConditionWitness, CosignerPublicKey, DEFAULT_PAGE_SIZE, DigestMismatchError, EXPIRY_MIN_PLAUSIBLE_MS, EventSourceUnavailableError, FetchError, INFO_FETCH_TIMEOUT_MS, Intent, ONCHAIN_COSIGN_AMBIGUOUS_PREFIX, ONCHAIN_COSIGN_REJECTED_PREFIX, ONCHAIN_COSIGN_UNSUPPORTED_PREFIX, OP_RETURN_EMPTY_PKSCRIPT, OUTPOINT_QUERY_CHUNK_SIZE, OnchainCosignAmbiguousError, OnchainCosignRejectedError, OnchainCosignUnsupportedError, PrevArkTxField, PrevoutTxField, Proof, ProviderUnavailableError, READ_TIMEOUT_MS, RestArkProvider, RestOperatorProvider, SCRIPT_QUERY_CHUNK_SIZE, ServerResponseMismatchError, SettlementEventType, Transaction, VtxoTaprootTree, VtxoTreeExpiry, assertAllowedSighashTypes, assertNonNegativeMs, assertTimerMs, baseFetch, buildVersion, canRecoverOnchain, canSpendOffchain, canSweepOnchain, configureEventSource, convertVtxo, craftToSpendTx, eventSourceIterator, fetchVtxoCreatedAtByTxid, formatSighash, getAllNormalizedVtxos, getArkPsbtFields, getConfiguredEventSource, getNormalizedVtxos, isArkError, isEventSourceError, isEventSourceUnavailableError, isFetchTimeoutError, isPastExpiry, isRetryableProviderError, isVirtualCoin, isVtxoSpent, maybeArkError, normalizeVtxo, rateGate, readSseFrames, requiresForfeit, resolveEventSource, resolveTimeHeight, sdkVersion, setArkPsbtField, streamFailureError, throwIfHttpUnavailable, toOffchainInputFeeParams, toProviderUnavailable };
|
|
1838
|
+
//# sourceMappingURL=chunk-4I53THL6.js.map
|
|
1839
|
+
//# sourceMappingURL=chunk-4I53THL6.js.map
|