@arkade-os/sdk 0.5.3 → 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 +138 -50
- package/dist/adapters/expo.cjs +10 -5
- package/dist/adapters/expo.d.cts +13 -8
- package/dist/adapters/expo.d.ts +13 -8
- package/dist/adapters/expo.js +4 -3
- package/dist/{asyncStorageTaskQueue-C9PkPcW_.d.cts → asyncStorageTaskQueue-BbH-THYJ.d.cts} +2 -2
- package/dist/{asyncStorageTaskQueue-B3U5gQ-i.d.ts → asyncStorageTaskQueue-BlvkJyTa.d.ts} +2 -2
- package/dist/base-C-Y5t_0F.d.cts +359 -0
- package/dist/base-C-Y5t_0F.d.ts +359 -0
- package/dist/{chunk-OPX2HRWM.js → chunk-4I53THL6.js} +210 -937
- package/dist/chunk-4I53THL6.js.map +1 -0
- package/dist/{chunk-5WRA66RV.cjs → chunk-5SYUEVJH.cjs} +10 -10
- package/dist/chunk-5SYUEVJH.cjs.map +1 -0
- package/dist/{chunk-6FL4N4S2.js → chunk-7GIKLFHC.js} +83 -20
- package/dist/chunk-7GIKLFHC.js.map +1 -0
- package/dist/{chunk-BYPBVHVA.cjs → chunk-AMH7Q3DF.cjs} +61 -935
- package/dist/chunk-AMH7Q3DF.cjs.map +1 -0
- package/dist/{chunk-P7B4LQ3H.js → chunk-BCA6D255.js} +18 -842
- package/dist/chunk-BCA6D255.js.map +1 -0
- package/dist/{chunk-T7C3E2HL.js → chunk-CE46RS6L.js} +20 -39
- 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-Y4BL3YYX.js → chunk-FVVXCDNE.js} +4 -4
- package/dist/chunk-FVVXCDNE.js.map +1 -0
- package/dist/{chunk-ZPF4I3K2.cjs → chunk-HQQ4NRXI.cjs} +29 -47
- 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-KW7ZEWYS.cjs → chunk-KCL5AVML.cjs} +223 -985
- package/dist/chunk-KCL5AVML.cjs.map +1 -0
- package/dist/{chunk-HAQAWBYG.js → chunk-OO2KEK64.js} +2860 -1366
- package/dist/chunk-OO2KEK64.js.map +1 -0
- package/dist/{chunk-OUTWUYJ4.cjs → chunk-T254PXSN.cjs} +3337 -1824
- 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-ZMSM3NOH.cjs → chunk-XWUCNMPN.cjs} +98 -34
- 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-Cd28gxpM.d.ts → delegate-gGkUnYpi.d.cts} +1 -1
- package/dist/{delegate-ByfKszb6.d.cts → delegate-iyDum4fw.d.ts} +1 -1
- package/dist/{index-B6X2E7W_.d.ts → index-BWwVnJhU.d.cts} +7 -272
- package/dist/{index-DgH_0j14.d.cts → index-Bx-iArDs.d.ts} +7 -272
- package/dist/{index-BURzbRup.d.cts → index-DCml4QJn.d.ts} +1090 -1140
- package/dist/{index-BURzbRup.d.ts → index-DkyzIzKL.d.cts} +1090 -1140
- package/dist/index.cjs +470 -371
- package/dist/index.d.cts +687 -410
- package/dist/index.d.ts +687 -410
- package/dist/index.js +7 -4
- package/dist/{wallet-BnmhohgZ.d.ts → keyringDescriptorProvider-CS4w4PWd.d.ts} +427 -372
- package/dist/{wallet-BjQugVUt.d.cts → keyringDescriptorProvider-CmHXrIXd.d.cts} +427 -372
- package/dist/program-BARSNg3X.d.ts +286 -0
- package/dist/program-znrWkxE4.d.cts +286 -0
- package/dist/repositories/realm/index.cjs +100 -51
- package/dist/repositories/realm/index.cjs.map +1 -1
- package/dist/repositories/realm/index.d.cts +25 -10
- package/dist/repositories/realm/index.d.ts +25 -10
- package/dist/repositories/realm/index.js +72 -24
- package/dist/repositories/realm/index.js.map +1 -1
- package/dist/repositories/sqlite/index.cjs +222 -131
- package/dist/repositories/sqlite/index.cjs.map +1 -1
- package/dist/repositories/sqlite/index.d.cts +36 -17
- package/dist/repositories/sqlite/index.d.ts +36 -17
- package/dist/repositories/sqlite/index.js +196 -107
- package/dist/repositories/sqlite/index.js.map +1 -1
- package/dist/{taskRunner-BaNCSxqK.d.ts → taskRunner-DgMLSk_O.d.ts} +4 -4
- package/dist/{taskRunner-i56-drQV.d.cts → taskRunner-buh6aFpG.d.cts} +4 -4
- 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 +10 -7
- 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 +20 -7
- 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-5WRA66RV.cjs.map +0 -1
- package/dist/chunk-6FL4N4S2.js.map +0 -1
- package/dist/chunk-BYPBVHVA.cjs.map +0 -1
- package/dist/chunk-HAQAWBYG.js.map +0 -1
- package/dist/chunk-KW7ZEWYS.cjs.map +0 -1
- package/dist/chunk-OPX2HRWM.js.map +0 -1
- package/dist/chunk-OUTWUYJ4.cjs.map +0 -1
- package/dist/chunk-P7B4LQ3H.js.map +0 -1
- package/dist/chunk-T7C3E2HL.js.map +0 -1
- package/dist/chunk-Y4BL3YYX.js.map +0 -1
- package/dist/chunk-ZMSM3NOH.cjs.map +0 -1
- package/dist/chunk-ZPF4I3K2.cjs.map +0 -1
|
@@ -1,881 +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 networkFromArkadeInfo = (info) => getNetwork(info.network);
|
|
22
|
-
var networks = {
|
|
23
|
-
bitcoin: withArkPrefix(NETWORK, "ark", "bitcoin"),
|
|
24
|
-
testnet: withArkPrefix(TEST_NETWORK, "tark", "testnet"),
|
|
25
|
-
signet: withArkPrefix(TEST_NETWORK, "tark", "signet"),
|
|
26
|
-
mutinynet: withArkPrefix(TEST_NETWORK, "tark", "mutinynet"),
|
|
27
|
-
regtest: withArkPrefix(
|
|
28
|
-
{
|
|
29
|
-
...TEST_NETWORK,
|
|
30
|
-
bech32: "bcrt",
|
|
31
|
-
pubKeyHash: 111,
|
|
32
|
-
scriptHash: 196
|
|
33
|
-
},
|
|
34
|
-
"tark",
|
|
35
|
-
"regtest"
|
|
36
|
-
)
|
|
37
|
-
};
|
|
38
|
-
function withArkPrefix(network, prefix, name) {
|
|
39
|
-
return {
|
|
40
|
-
...network,
|
|
41
|
-
hrp: prefix,
|
|
42
|
-
name
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
var DEFAULT_ARKADE_SERVER_URL = "https://arkade.computer";
|
|
46
|
-
var DEFAULT_ARKADE_EMULATOR_URL = "https://emulator.arkade.computer";
|
|
47
|
-
var DEFAULT_NETWORK = networks.bitcoin;
|
|
48
|
-
var DEFAULT_NETWORK_NAME = "bitcoin";
|
|
49
|
-
var COMPRESSED_PUBKEY = /^0[23][0-9a-f]{64}$/;
|
|
50
|
-
var BITCOIN_EMULATOR_PUBKEY = "0239c196415da47b26456a101daaa12ba9e445bfe153197f1e2b750bf40e52092e";
|
|
51
|
-
var MUTINYNET_EMULATOR_PUBKEY = "03f823b9b2febc81f4af967e77aed2f541cbd3397c6d8f5a72e32eb7b471af889a";
|
|
52
|
-
var REGTEST_EMULATOR_PUBKEY = "02999413c46fa10ada5cbc4bcc79a1d09160c2ba3cfc812705d7a13e5e545fb2a9";
|
|
53
|
-
var EMULATOR_PUBKEYS = {
|
|
54
|
-
bitcoin: BITCOIN_EMULATOR_PUBKEY,
|
|
55
|
-
mutinynet: MUTINYNET_EMULATOR_PUBKEY,
|
|
56
|
-
regtest: REGTEST_EMULATOR_PUBKEY
|
|
57
|
-
};
|
|
58
|
-
function defaultEmulatorPubkey(network) {
|
|
59
|
-
const pinned = network.name ? EMULATOR_PUBKEYS[network.name] : void 0;
|
|
60
|
-
if (!pinned) {
|
|
61
|
-
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)`;
|
|
62
|
-
throw new Error(
|
|
63
|
-
`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(", ")}`
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
return pinned;
|
|
67
|
-
}
|
|
68
|
-
function resolveEmulatorPubkey(network, override) {
|
|
69
|
-
if (override === void 0) return defaultEmulatorPubkey(network);
|
|
70
|
-
if (!COMPRESSED_PUBKEY.test(override)) {
|
|
71
|
-
throw new Error(
|
|
72
|
-
`Emulator pubkey override must be 33-byte compressed secp256k1 hex (66 lowercase chars, 02/03 prefix), got ${JSON.stringify(override)}.`
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
return override;
|
|
76
|
-
}
|
|
77
|
-
var ArkAddress = class _ArkAddress {
|
|
78
|
-
/**
|
|
79
|
-
* Create an Arkade address from its server public key, Taproot output key, and prefix.
|
|
80
|
-
*
|
|
81
|
-
* @param serverPubKey - 32-byte Arkade server public key
|
|
82
|
-
* @param vtxoTaprootKey - 32-byte Taproot output key (a.k.a. tweaked public key)
|
|
83
|
-
* @param hrp - Bech32 human-readable prefix
|
|
84
|
-
* @param version - Address version byte
|
|
85
|
-
* @defaultValue `version = 0`
|
|
86
|
-
* @throws Error if either public key is not 32 bytes long
|
|
87
|
-
*/
|
|
88
|
-
constructor(serverPubKey, vtxoTaprootKey, hrp = DEFAULT_NETWORK.hrp, version2 = 0) {
|
|
89
|
-
this.serverPubKey = serverPubKey;
|
|
90
|
-
this.vtxoTaprootKey = vtxoTaprootKey;
|
|
91
|
-
this.hrp = hrp;
|
|
92
|
-
this.version = version2;
|
|
93
|
-
if (serverPubKey.length !== 32) {
|
|
94
|
-
throw new Error(
|
|
95
|
-
"Invalid server public key length, expected 32 bytes, got " + serverPubKey.length
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
if (vtxoTaprootKey.length !== 32) {
|
|
99
|
-
throw new Error(
|
|
100
|
-
"Invalid vtxo taproot public key length, expected 32 bytes, got " + vtxoTaprootKey.length
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
serverPubKey;
|
|
105
|
-
vtxoTaprootKey;
|
|
106
|
-
hrp;
|
|
107
|
-
version;
|
|
108
|
-
/**
|
|
109
|
-
* Decode an Arkade address from its bech32m string form.
|
|
110
|
-
*
|
|
111
|
-
* @param address - Bech32m-encoded Arkade address
|
|
112
|
-
* @returns Decoded Arkade address
|
|
113
|
-
* @throws Error if the address is malformed or has an invalid payload length
|
|
114
|
-
* @see encode
|
|
115
|
-
*/
|
|
116
|
-
static decode(address) {
|
|
117
|
-
const decoded = bech32m.decodeUnsafe(address, 1023);
|
|
118
|
-
if (!decoded) {
|
|
119
|
-
throw new Error("Invalid address");
|
|
120
|
-
}
|
|
121
|
-
const data = new Uint8Array(bech32m.fromWords(decoded.words));
|
|
122
|
-
if (data.length !== 1 + 32 + 32) {
|
|
123
|
-
throw new Error("Invalid data length, expected 65 bytes, got " + data.length);
|
|
124
|
-
}
|
|
125
|
-
const version2 = data[0];
|
|
126
|
-
const serverPubKey = data.slice(1, 33);
|
|
127
|
-
const vtxoTaprootPubKey = data.slice(33, 65);
|
|
128
|
-
return new _ArkAddress(serverPubKey, vtxoTaprootPubKey, decoded.prefix, version2);
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Encode the address to its bech32m string form.
|
|
132
|
-
*
|
|
133
|
-
* @returns Bech32m-encoded Arkade address
|
|
134
|
-
* @see decode
|
|
135
|
-
*/
|
|
136
|
-
encode() {
|
|
137
|
-
const data = new Uint8Array(1 + 32 + 32);
|
|
138
|
-
data[0] = this.version;
|
|
139
|
-
data.set(this.serverPubKey, 1);
|
|
140
|
-
data.set(this.vtxoTaprootKey, 33);
|
|
141
|
-
const words = bech32m.toWords(data);
|
|
142
|
-
return bech32m.encode(this.hrp, words, 1023);
|
|
143
|
-
}
|
|
144
|
-
/** ScriptPubKey used to send non-dust funds to the address. */
|
|
145
|
-
get pkScript() {
|
|
146
|
-
return Script$1.encode(["OP_1", this.vtxoTaprootKey]);
|
|
147
|
-
}
|
|
148
|
-
/** ScriptPubKey used to send sub-dust funds to the address. */
|
|
149
|
-
get subdustPkScript() {
|
|
150
|
-
return Script$1.encode(["RETURN", this.vtxoTaprootKey]);
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
function timelockToSequence(timelock) {
|
|
154
|
-
return bip68.encode(
|
|
155
|
-
timelock.type === "blocks" ? { blocks: Number(timelock.value) } : { seconds: Number(timelock.value) }
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
function sequenceToTimelock(sequence) {
|
|
159
|
-
const decoded = bip68.decode(sequence);
|
|
160
|
-
if ("blocks" in decoded && decoded.blocks !== void 0) {
|
|
161
|
-
return { type: "blocks", value: BigInt(decoded.blocks) };
|
|
162
|
-
}
|
|
163
|
-
if ("seconds" in decoded && decoded.seconds !== void 0) {
|
|
164
|
-
return { type: "seconds", value: BigInt(decoded.seconds) };
|
|
165
|
-
}
|
|
166
|
-
throw new Error(`Invalid BIP68 sequence: ${sequence}`);
|
|
167
|
-
}
|
|
168
|
-
function sequenceToCanonicalTimelock(sequence) {
|
|
169
|
-
if (sequence < 0n || sequence >= 0x80000000n) return void 0;
|
|
170
|
-
const timelock = sequenceToTimelock(Number(sequence));
|
|
171
|
-
return BigInt(timelockToSequence(timelock)) === sequence ? timelock : void 0;
|
|
172
|
-
}
|
|
173
|
-
var MinimalScriptNum = ScriptNum(void 0, true);
|
|
174
|
-
function decodeTapscript(script) {
|
|
175
|
-
const types = [
|
|
176
|
-
MultisigTapscript,
|
|
177
|
-
CSVMultisigTapscript,
|
|
178
|
-
ConditionCSVMultisigTapscript,
|
|
179
|
-
ConditionMultisigTapscript,
|
|
180
|
-
CLTVMultisigTapscript
|
|
181
|
-
];
|
|
182
|
-
for (const type of types) {
|
|
183
|
-
try {
|
|
184
|
-
return type.decode(script);
|
|
185
|
-
} catch (error) {
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
throw new Error(`Failed to decode: script ${hex.encode(script)} is not a valid tapscript`);
|
|
190
|
-
}
|
|
191
|
-
var MultisigTapscript;
|
|
192
|
-
((MultisigTapscript2) => {
|
|
193
|
-
((MultisigType2) => {
|
|
194
|
-
MultisigType2[MultisigType2["CHECKSIG"] = 0] = "CHECKSIG";
|
|
195
|
-
MultisigType2[MultisigType2["CHECKSIGADD"] = 1] = "CHECKSIGADD";
|
|
196
|
-
})(MultisigTapscript2.MultisigType || (MultisigTapscript2.MultisigType = {}));
|
|
197
|
-
function encode2(params) {
|
|
198
|
-
if (params.pubkeys.length === 0) {
|
|
199
|
-
throw new Error("At least 1 pubkey is required");
|
|
200
|
-
}
|
|
201
|
-
for (const pubkey of params.pubkeys) {
|
|
202
|
-
if (pubkey.length !== 32) {
|
|
203
|
-
throw new Error(`Invalid pubkey length: expected 32, got ${pubkey.length}`);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (!params.type) {
|
|
207
|
-
params.type = 0 /* CHECKSIG */;
|
|
208
|
-
}
|
|
209
|
-
if (params.type === 1 /* CHECKSIGADD */) {
|
|
210
|
-
return {
|
|
211
|
-
type: "multisig" /* Multisig */,
|
|
212
|
-
params,
|
|
213
|
-
script: p2tr_ms(params.pubkeys.length, params.pubkeys).script
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
const asm = [];
|
|
217
|
-
for (let i = 0; i < params.pubkeys.length; i++) {
|
|
218
|
-
asm.push(params.pubkeys[i]);
|
|
219
|
-
if (i < params.pubkeys.length - 1) {
|
|
220
|
-
asm.push("CHECKSIGVERIFY");
|
|
221
|
-
} else {
|
|
222
|
-
asm.push("CHECKSIG");
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return {
|
|
226
|
-
type: "multisig" /* Multisig */,
|
|
227
|
-
params,
|
|
228
|
-
script: Script.encode(asm)
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
MultisigTapscript2.encode = encode2;
|
|
232
|
-
function decode2(script) {
|
|
233
|
-
if (script.length === 0) {
|
|
234
|
-
throw new Error("Failed to decode: script is empty");
|
|
235
|
-
}
|
|
236
|
-
try {
|
|
237
|
-
return decodeChecksigAdd(script);
|
|
238
|
-
} catch (error) {
|
|
239
|
-
try {
|
|
240
|
-
return decodeChecksig(script);
|
|
241
|
-
} catch (error2) {
|
|
242
|
-
throw new Error(
|
|
243
|
-
`Failed to decode script: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
MultisigTapscript2.decode = decode2;
|
|
249
|
-
function decodeChecksigAdd(script) {
|
|
250
|
-
const asm = Script.decode(script);
|
|
251
|
-
const pubkeys = [];
|
|
252
|
-
let foundNumEqual = false;
|
|
253
|
-
for (let i = 0; i < asm.length; i++) {
|
|
254
|
-
const op = asm[i];
|
|
255
|
-
if (typeof op !== "string" && typeof op !== "number") {
|
|
256
|
-
if (op.length !== 32) {
|
|
257
|
-
throw new Error(`Invalid pubkey length: expected 32, got ${op.length}`);
|
|
258
|
-
}
|
|
259
|
-
pubkeys.push(op);
|
|
260
|
-
if (i + 1 >= asm.length || asm[i + 1] !== "CHECKSIGADD" && asm[i + 1] !== "CHECKSIG") {
|
|
261
|
-
throw new Error("Expected CHECKSIGADD or CHECKSIG after pubkey");
|
|
262
|
-
}
|
|
263
|
-
i++;
|
|
264
|
-
continue;
|
|
265
|
-
}
|
|
266
|
-
if (i === asm.length - 1) {
|
|
267
|
-
if (op !== "NUMEQUAL") {
|
|
268
|
-
throw new Error("Expected NUMEQUAL at end of script");
|
|
269
|
-
}
|
|
270
|
-
foundNumEqual = true;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (!foundNumEqual) {
|
|
274
|
-
throw new Error("Missing NUMEQUAL operation");
|
|
275
|
-
}
|
|
276
|
-
if (pubkeys.length === 0) {
|
|
277
|
-
throw new Error("Invalid script: must have at least 1 pubkey");
|
|
278
|
-
}
|
|
279
|
-
const reconstructed = encode2({
|
|
280
|
-
pubkeys,
|
|
281
|
-
type: 1 /* CHECKSIGADD */
|
|
282
|
-
});
|
|
283
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
284
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
285
|
-
}
|
|
286
|
-
return {
|
|
287
|
-
type: "multisig" /* Multisig */,
|
|
288
|
-
params: { pubkeys, type: 1 /* CHECKSIGADD */ },
|
|
289
|
-
script
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
function decodeChecksig(script) {
|
|
293
|
-
const asm = Script.decode(script);
|
|
294
|
-
const pubkeys = [];
|
|
295
|
-
for (let i = 0; i < asm.length; i++) {
|
|
296
|
-
const op = asm[i];
|
|
297
|
-
if (typeof op !== "string" && typeof op !== "number") {
|
|
298
|
-
if (op.length !== 32) {
|
|
299
|
-
throw new Error(`Invalid pubkey length: expected 32, got ${op.length}`);
|
|
300
|
-
}
|
|
301
|
-
pubkeys.push(op);
|
|
302
|
-
if (i + 1 >= asm.length) {
|
|
303
|
-
throw new Error("Unexpected end of script");
|
|
304
|
-
}
|
|
305
|
-
const nextOp = asm[i + 1];
|
|
306
|
-
if (nextOp !== "CHECKSIGVERIFY" && nextOp !== "CHECKSIG") {
|
|
307
|
-
throw new Error("Expected CHECKSIGVERIFY or CHECKSIG after pubkey");
|
|
308
|
-
}
|
|
309
|
-
if (i === asm.length - 2 && nextOp !== "CHECKSIG") {
|
|
310
|
-
throw new Error("Last operation must be CHECKSIG");
|
|
311
|
-
}
|
|
312
|
-
i++;
|
|
313
|
-
continue;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (pubkeys.length === 0) {
|
|
317
|
-
throw new Error("Invalid script: must have at least 1 pubkey");
|
|
318
|
-
}
|
|
319
|
-
const reconstructed = encode2({ pubkeys, type: 0 /* CHECKSIG */ });
|
|
320
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
321
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
322
|
-
}
|
|
323
|
-
return {
|
|
324
|
-
type: "multisig" /* Multisig */,
|
|
325
|
-
params: { pubkeys, type: 0 /* CHECKSIG */ },
|
|
326
|
-
script
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
function is(tapscript) {
|
|
330
|
-
return tapscript.type === "multisig" /* Multisig */;
|
|
331
|
-
}
|
|
332
|
-
MultisigTapscript2.is = is;
|
|
333
|
-
})(MultisigTapscript || (MultisigTapscript = {}));
|
|
334
|
-
var CSVMultisigTapscript;
|
|
335
|
-
((CSVMultisigTapscript2) => {
|
|
336
|
-
function encode2(params) {
|
|
337
|
-
for (const pubkey of params.pubkeys) {
|
|
338
|
-
if (pubkey.length !== 32) {
|
|
339
|
-
throw new Error(`Invalid pubkey length: expected 32, got ${pubkey.length}`);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
const sequence = MinimalScriptNum.encode(BigInt(timelockToSequence(params.timelock)));
|
|
343
|
-
const asm = [
|
|
344
|
-
sequence.length === 1 ? sequence[0] : sequence,
|
|
345
|
-
"CHECKSEQUENCEVERIFY",
|
|
346
|
-
"DROP"
|
|
347
|
-
];
|
|
348
|
-
const multisigScript = MultisigTapscript.encode(params);
|
|
349
|
-
const script = new Uint8Array([...Script.encode(asm), ...multisigScript.script]);
|
|
350
|
-
return {
|
|
351
|
-
type: "csv-multisig" /* CSVMultisig */,
|
|
352
|
-
params,
|
|
353
|
-
script
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
CSVMultisigTapscript2.encode = encode2;
|
|
357
|
-
function decode2(script) {
|
|
358
|
-
if (script.length === 0) {
|
|
359
|
-
throw new Error("Failed to decode: script is empty");
|
|
360
|
-
}
|
|
361
|
-
const isValid = isScriptValid(script);
|
|
362
|
-
if (isValid instanceof Error) {
|
|
363
|
-
throw isValid;
|
|
364
|
-
}
|
|
365
|
-
const asm = Script.decode(script);
|
|
366
|
-
const sequence = asm[0];
|
|
367
|
-
const multisigScript = new Uint8Array(Script.encode(asm.slice(3)));
|
|
368
|
-
let multisig;
|
|
369
|
-
try {
|
|
370
|
-
multisig = MultisigTapscript.decode(multisigScript);
|
|
371
|
-
} catch (error) {
|
|
372
|
-
throw new Error(
|
|
373
|
-
`Invalid multisig script: ${error instanceof Error ? error.message : String(error)}`
|
|
374
|
-
);
|
|
375
|
-
}
|
|
376
|
-
let sequenceNum;
|
|
377
|
-
if (typeof sequence === "number") {
|
|
378
|
-
sequenceNum = sequence;
|
|
379
|
-
} else {
|
|
380
|
-
sequenceNum = Number(MinimalScriptNum.decode(sequence));
|
|
381
|
-
}
|
|
382
|
-
const timelock = sequenceToTimelock(sequenceNum);
|
|
383
|
-
const reconstructed = encode2({
|
|
384
|
-
timelock,
|
|
385
|
-
...multisig.params
|
|
386
|
-
});
|
|
387
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
388
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
389
|
-
}
|
|
390
|
-
return {
|
|
391
|
-
type: "csv-multisig" /* CSVMultisig */,
|
|
392
|
-
params: {
|
|
393
|
-
timelock,
|
|
394
|
-
...multisig.params
|
|
395
|
-
},
|
|
396
|
-
script
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
CSVMultisigTapscript2.decode = decode2;
|
|
400
|
-
function is(tapscript) {
|
|
401
|
-
return tapscript.type === "csv-multisig" /* CSVMultisig */;
|
|
402
|
-
}
|
|
403
|
-
CSVMultisigTapscript2.is = is;
|
|
404
|
-
function isScriptValid(script) {
|
|
405
|
-
const asm = Script.decode(script);
|
|
406
|
-
if (asm.length < 3) {
|
|
407
|
-
return new Error(`Invalid script: too short (expected at least 3)`);
|
|
408
|
-
}
|
|
409
|
-
const sequence = asm[0];
|
|
410
|
-
if (typeof sequence === "string") {
|
|
411
|
-
return new Error("Invalid script: expected sequence number");
|
|
412
|
-
}
|
|
413
|
-
if (asm[1] !== "CHECKSEQUENCEVERIFY" || asm[2] !== "DROP") {
|
|
414
|
-
return new Error("Invalid script: expected CHECKSEQUENCEVERIFY DROP");
|
|
415
|
-
}
|
|
416
|
-
return true;
|
|
417
|
-
}
|
|
418
|
-
CSVMultisigTapscript2.isScriptValid = isScriptValid;
|
|
419
|
-
})(CSVMultisigTapscript || (CSVMultisigTapscript = {}));
|
|
420
|
-
function isConditionScriptValid(script) {
|
|
421
|
-
const asm = Script.decode(script);
|
|
422
|
-
if (asm.length < 1) {
|
|
423
|
-
return new Error(`Invalid script: too short (expected at least 1)`);
|
|
424
|
-
}
|
|
425
|
-
if (asm.lastIndexOf("VERIFY") === -1) {
|
|
426
|
-
return new Error("Invalid script: missing VERIFY operation");
|
|
427
|
-
}
|
|
428
|
-
return true;
|
|
429
|
-
}
|
|
430
|
-
var ConditionCSVMultisigTapscript;
|
|
431
|
-
((ConditionCSVMultisigTapscript2) => {
|
|
432
|
-
function encode2(params) {
|
|
433
|
-
const script = new Uint8Array([
|
|
434
|
-
...params.conditionScript,
|
|
435
|
-
...Script.encode(["VERIFY"]),
|
|
436
|
-
...CSVMultisigTapscript.encode(params).script
|
|
437
|
-
]);
|
|
438
|
-
return {
|
|
439
|
-
type: "condition-csv-multisig" /* ConditionCSVMultisig */,
|
|
440
|
-
params,
|
|
441
|
-
script
|
|
442
|
-
};
|
|
443
|
-
}
|
|
444
|
-
ConditionCSVMultisigTapscript2.encode = encode2;
|
|
445
|
-
function decode2(script) {
|
|
446
|
-
if (script.length === 0) {
|
|
447
|
-
throw new Error("Failed to decode: script is empty");
|
|
448
|
-
}
|
|
449
|
-
const isValid = isScriptValid(script);
|
|
450
|
-
if (isValid instanceof Error) {
|
|
451
|
-
throw isValid;
|
|
452
|
-
}
|
|
453
|
-
const asm = Script.decode(script);
|
|
454
|
-
const verifyIndex = asm.lastIndexOf("VERIFY");
|
|
455
|
-
if (verifyIndex === -1) {
|
|
456
|
-
throw Error("Invalid script: missing VERIFY operation");
|
|
457
|
-
}
|
|
458
|
-
const conditionScript = new Uint8Array(Script.encode(asm.slice(0, verifyIndex)));
|
|
459
|
-
const csvMultisigScript = new Uint8Array(Script.encode(asm.slice(verifyIndex + 1)));
|
|
460
|
-
let csvMultisig;
|
|
461
|
-
try {
|
|
462
|
-
csvMultisig = CSVMultisigTapscript.decode(csvMultisigScript);
|
|
463
|
-
} catch (error) {
|
|
464
|
-
throw new Error(
|
|
465
|
-
`Invalid CSV multisig script: ${error instanceof Error ? error.message : String(error)}`
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
const reconstructed = encode2({
|
|
469
|
-
conditionScript,
|
|
470
|
-
...csvMultisig.params
|
|
471
|
-
});
|
|
472
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
473
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
474
|
-
}
|
|
475
|
-
return {
|
|
476
|
-
type: "condition-csv-multisig" /* ConditionCSVMultisig */,
|
|
477
|
-
params: {
|
|
478
|
-
conditionScript,
|
|
479
|
-
...csvMultisig.params
|
|
480
|
-
},
|
|
481
|
-
script
|
|
482
|
-
};
|
|
483
|
-
}
|
|
484
|
-
ConditionCSVMultisigTapscript2.decode = decode2;
|
|
485
|
-
function is(tapscript) {
|
|
486
|
-
return tapscript.type === "condition-csv-multisig" /* ConditionCSVMultisig */;
|
|
487
|
-
}
|
|
488
|
-
ConditionCSVMultisigTapscript2.is = is;
|
|
489
|
-
function isScriptValid(script) {
|
|
490
|
-
return isConditionScriptValid(script);
|
|
491
|
-
}
|
|
492
|
-
ConditionCSVMultisigTapscript2.isScriptValid = isScriptValid;
|
|
493
|
-
})(ConditionCSVMultisigTapscript || (ConditionCSVMultisigTapscript = {}));
|
|
494
|
-
var ConditionMultisigTapscript;
|
|
495
|
-
((ConditionMultisigTapscript2) => {
|
|
496
|
-
function encode2(params) {
|
|
497
|
-
const script = new Uint8Array([
|
|
498
|
-
...params.conditionScript,
|
|
499
|
-
...Script.encode(["VERIFY"]),
|
|
500
|
-
...MultisigTapscript.encode(params).script
|
|
501
|
-
]);
|
|
502
|
-
return {
|
|
503
|
-
type: "condition-multisig" /* ConditionMultisig */,
|
|
504
|
-
params,
|
|
505
|
-
script
|
|
506
|
-
};
|
|
507
|
-
}
|
|
508
|
-
ConditionMultisigTapscript2.encode = encode2;
|
|
509
|
-
function decode2(script) {
|
|
510
|
-
if (script.length === 0) {
|
|
511
|
-
throw new Error("Failed to decode: script is empty");
|
|
512
|
-
}
|
|
513
|
-
const isValid = isScriptValid(script);
|
|
514
|
-
if (isValid instanceof Error) {
|
|
515
|
-
throw isValid;
|
|
516
|
-
}
|
|
517
|
-
const asm = Script.decode(script);
|
|
518
|
-
const verifyIndex = asm.lastIndexOf("VERIFY");
|
|
519
|
-
if (verifyIndex === -1) {
|
|
520
|
-
throw Error("Invalid script: missing VERIFY operation");
|
|
521
|
-
}
|
|
522
|
-
const conditionScript = new Uint8Array(Script.encode(asm.slice(0, verifyIndex)));
|
|
523
|
-
const multisigScript = new Uint8Array(Script.encode(asm.slice(verifyIndex + 1)));
|
|
524
|
-
let multisig;
|
|
525
|
-
try {
|
|
526
|
-
multisig = MultisigTapscript.decode(multisigScript);
|
|
527
|
-
} catch (error) {
|
|
528
|
-
throw new Error(
|
|
529
|
-
`Invalid multisig script: ${error instanceof Error ? error.message : String(error)}`
|
|
530
|
-
);
|
|
531
|
-
}
|
|
532
|
-
const reconstructed = encode2({
|
|
533
|
-
conditionScript,
|
|
534
|
-
...multisig.params
|
|
535
|
-
});
|
|
536
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
537
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
538
|
-
}
|
|
539
|
-
return {
|
|
540
|
-
type: "condition-multisig" /* ConditionMultisig */,
|
|
541
|
-
params: {
|
|
542
|
-
conditionScript,
|
|
543
|
-
...multisig.params
|
|
544
|
-
},
|
|
545
|
-
script
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
ConditionMultisigTapscript2.decode = decode2;
|
|
549
|
-
function is(tapscript) {
|
|
550
|
-
return tapscript.type === "condition-multisig" /* ConditionMultisig */;
|
|
551
|
-
}
|
|
552
|
-
ConditionMultisigTapscript2.is = is;
|
|
553
|
-
function isScriptValid(script) {
|
|
554
|
-
return isConditionScriptValid(script);
|
|
555
|
-
}
|
|
556
|
-
ConditionMultisigTapscript2.isScriptValid = isScriptValid;
|
|
557
|
-
})(ConditionMultisigTapscript || (ConditionMultisigTapscript = {}));
|
|
558
|
-
var CLTVMultisigTapscript;
|
|
559
|
-
((CLTVMultisigTapscript2) => {
|
|
560
|
-
function encode2(params) {
|
|
561
|
-
const locktime = MinimalScriptNum.encode(params.absoluteTimelock);
|
|
562
|
-
const asm = [
|
|
563
|
-
locktime.length === 1 ? locktime[0] : locktime,
|
|
564
|
-
"CHECKLOCKTIMEVERIFY",
|
|
565
|
-
"DROP"
|
|
566
|
-
];
|
|
567
|
-
const timelockedScript = Script.encode(asm);
|
|
568
|
-
const script = new Uint8Array([
|
|
569
|
-
...timelockedScript,
|
|
570
|
-
...MultisigTapscript.encode(params).script
|
|
571
|
-
]);
|
|
572
|
-
return {
|
|
573
|
-
type: "cltv-multisig" /* CLTVMultisig */,
|
|
574
|
-
params,
|
|
575
|
-
script
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
CLTVMultisigTapscript2.encode = encode2;
|
|
579
|
-
function decode2(script) {
|
|
580
|
-
if (script.length === 0) {
|
|
581
|
-
throw new Error("Failed to decode: script is empty");
|
|
582
|
-
}
|
|
583
|
-
const isValid = isScriptValid(script);
|
|
584
|
-
if (isValid instanceof Error) {
|
|
585
|
-
throw isValid;
|
|
586
|
-
}
|
|
587
|
-
const asm = Script.decode(script);
|
|
588
|
-
const locktime = asm[0];
|
|
589
|
-
if (typeof locktime === "string") {
|
|
590
|
-
throw new Error("Invalid script: expected locktime number");
|
|
591
|
-
}
|
|
592
|
-
if (asm[1] !== "CHECKLOCKTIMEVERIFY" || asm[2] !== "DROP") {
|
|
593
|
-
throw new Error("Invalid script: expected CHECKLOCKTIMEVERIFY DROP");
|
|
594
|
-
}
|
|
595
|
-
const multisigScript = new Uint8Array(Script.encode(asm.slice(3)));
|
|
596
|
-
let multisig;
|
|
597
|
-
try {
|
|
598
|
-
multisig = MultisigTapscript.decode(multisigScript);
|
|
599
|
-
} catch (error) {
|
|
600
|
-
throw new Error(
|
|
601
|
-
`Invalid multisig script: ${error instanceof Error ? error.message : String(error)}`
|
|
602
|
-
);
|
|
603
|
-
}
|
|
604
|
-
let absoluteTimelock;
|
|
605
|
-
if (typeof locktime === "number") {
|
|
606
|
-
absoluteTimelock = BigInt(locktime);
|
|
607
|
-
} else {
|
|
608
|
-
absoluteTimelock = MinimalScriptNum.decode(locktime);
|
|
609
|
-
}
|
|
610
|
-
const reconstructed = encode2({
|
|
611
|
-
absoluteTimelock,
|
|
612
|
-
...multisig.params
|
|
613
|
-
});
|
|
614
|
-
if (hex.encode(reconstructed.script) !== hex.encode(script)) {
|
|
615
|
-
throw new Error("Invalid script format: script reconstruction mismatch");
|
|
616
|
-
}
|
|
617
|
-
return {
|
|
618
|
-
type: "cltv-multisig" /* CLTVMultisig */,
|
|
619
|
-
params: {
|
|
620
|
-
absoluteTimelock,
|
|
621
|
-
...multisig.params
|
|
622
|
-
},
|
|
623
|
-
script
|
|
624
|
-
};
|
|
625
|
-
}
|
|
626
|
-
CLTVMultisigTapscript2.decode = decode2;
|
|
627
|
-
function is(tapscript) {
|
|
628
|
-
return tapscript.type === "cltv-multisig" /* CLTVMultisig */;
|
|
629
|
-
}
|
|
630
|
-
CLTVMultisigTapscript2.is = is;
|
|
631
|
-
function isScriptValid(script) {
|
|
632
|
-
const asm = Script.decode(script);
|
|
633
|
-
if (asm.length < 3) {
|
|
634
|
-
return new Error(`Invalid script: too short (expected at least 3)`);
|
|
635
|
-
}
|
|
636
|
-
const locktime = asm[0];
|
|
637
|
-
if (typeof locktime === "string") {
|
|
638
|
-
return new Error("Invalid script: expected locktime as number or bytes");
|
|
639
|
-
}
|
|
640
|
-
if (asm[1] !== "CHECKLOCKTIMEVERIFY" || asm[2] !== "DROP") {
|
|
641
|
-
return new Error("Invalid script: expected CHECKLOCKTIMEVERIFY DROP");
|
|
642
|
-
}
|
|
643
|
-
return true;
|
|
644
|
-
}
|
|
645
|
-
CLTVMultisigTapscript2.isScriptValid = isScriptValid;
|
|
646
|
-
})(CLTVMultisigTapscript || (CLTVMultisigTapscript = {}));
|
|
647
|
-
function assembleBtcdTaprootTree(scripts) {
|
|
648
|
-
if (scripts.length === 0) {
|
|
649
|
-
throw new Error("assembleBtcdTaprootTree: empty scripts list");
|
|
650
|
-
}
|
|
651
|
-
const leaves = scripts.map((script) => ({
|
|
652
|
-
script,
|
|
653
|
-
leafVersion: TAP_LEAF_VERSION
|
|
654
|
-
}));
|
|
655
|
-
if (leaves.length === 1) {
|
|
656
|
-
return leaves[0];
|
|
657
|
-
}
|
|
658
|
-
const branches = [];
|
|
659
|
-
for (let i = 0; i < leaves.length; i += 2) {
|
|
660
|
-
if (i === leaves.length - 1) {
|
|
661
|
-
const last = branches.pop();
|
|
662
|
-
if (last === void 0) {
|
|
663
|
-
throw new Error(
|
|
664
|
-
`assembleBtcdTaprootTree: unexpected odd leaf at i=${i} with no prior branch`
|
|
665
|
-
);
|
|
666
|
-
}
|
|
667
|
-
branches.push([last, leaves[i]]);
|
|
668
|
-
} else {
|
|
669
|
-
branches.push([leaves[i], leaves[i + 1]]);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
while (branches.length >= 2) {
|
|
673
|
-
const left = branches.shift();
|
|
674
|
-
const right = branches.shift();
|
|
675
|
-
branches.push([left, right]);
|
|
676
|
-
}
|
|
677
|
-
return branches[0];
|
|
678
|
-
}
|
|
679
|
-
function btcdLeafLayout(count) {
|
|
680
|
-
const placeholders = Array.from({ length: count }, () => new Uint8Array());
|
|
681
|
-
const indexOf = new Map(placeholders.map((script, index) => [script, index]));
|
|
682
|
-
const layout = [];
|
|
683
|
-
const walk = (node, depth) => {
|
|
684
|
-
if (Array.isArray(node)) {
|
|
685
|
-
walk(node[0], depth + 1);
|
|
686
|
-
walk(node[1], depth + 1);
|
|
687
|
-
} else {
|
|
688
|
-
layout.push({ index: indexOf.get(node.script), depth });
|
|
689
|
-
}
|
|
690
|
-
};
|
|
691
|
-
walk(assembleBtcdTaprootTree(placeholders), 0);
|
|
692
|
-
return layout;
|
|
693
|
-
}
|
|
694
|
-
var TapTreeCoder = P.array(
|
|
695
|
-
null,
|
|
696
|
-
P.struct({ depth: P.U8, version: P.U8, script: VarBytes })
|
|
697
|
-
);
|
|
698
|
-
function scriptFromTapLeafScript(leaf) {
|
|
699
|
-
return leaf[1].subarray(0, leaf[1].length - 1);
|
|
700
|
-
}
|
|
701
|
-
var VtxoScript = class _VtxoScript {
|
|
702
|
-
/**
|
|
703
|
-
* Create a virtual output script from its tapleaf scripts.
|
|
704
|
-
*
|
|
705
|
-
* The Taproot script tree is assembled using btcd's algorithm
|
|
706
|
-
* (`txscript.AssembleTaprootScriptTree`) so the derived taproot output
|
|
707
|
-
* key agrees with arkd for any leaf count. `@scure/btc-signer`'s
|
|
708
|
-
* default `taprootListToTree` is a Huffman builder that only agrees
|
|
709
|
-
* with arkd for power-of-2 leaf counts.
|
|
710
|
-
*
|
|
711
|
-
* @param scripts - Raw tapscript bytes for each leaf
|
|
712
|
-
* @throws Error if the provided leaves cannot produce a valid Taproot tree
|
|
713
|
-
*/
|
|
714
|
-
constructor(scripts) {
|
|
715
|
-
this.scripts = scripts;
|
|
716
|
-
const tapTree = assembleBtcdTaprootTree(scripts);
|
|
717
|
-
const payment = p2tr(taprootNumsKey(), tapTree, void 0, true);
|
|
718
|
-
if (!payment.tapLeafScript || payment.tapLeafScript.length !== scripts.length) {
|
|
719
|
-
throw new Error("invalid scripts");
|
|
720
|
-
}
|
|
721
|
-
this.leaves = payment.tapLeafScript;
|
|
722
|
-
this.tweakedPublicKey = payment.tweakedPubkey;
|
|
723
|
-
this.pkScript = payment.script;
|
|
724
|
-
}
|
|
725
|
-
scripts;
|
|
726
|
-
leaves;
|
|
727
|
-
tweakedPublicKey;
|
|
728
|
-
pkScript;
|
|
729
|
-
/**
|
|
730
|
-
* Decode a virtual output script from an encoded TapTree.
|
|
731
|
-
*
|
|
732
|
-
* Leaves are normally read in construction order and their depths
|
|
733
|
-
* ignored: that covers the {@link encode} form (every depth 1) and the
|
|
734
|
-
* placeholder depths other writers use, such as arkd's. The exception is
|
|
735
|
-
* the BIP-371 form {@link toBIP371TapTree} writes, whose leaves are in the
|
|
736
|
-
* tree's DFS order: for some leaf counts (6, 7, 10, ...) that differs from
|
|
737
|
-
* construction order. A tree whose depths are exactly the shape
|
|
738
|
-
* `assembleBtcdTaprootTree` builds is that form, so its leaves are mapped
|
|
739
|
-
* back to construction order first. No construction-order writer produces
|
|
740
|
-
* that depth sequence when the two orders differ, so this never reorders
|
|
741
|
-
* a tree that decoded correctly before.
|
|
742
|
-
*
|
|
743
|
-
* Every leaf must carry the base tapscript version: the tree is rebuilt
|
|
744
|
-
* with {@link TAP_LEAF_VERSION} leaves, so any other version would
|
|
745
|
-
* silently derive a different taproot key.
|
|
746
|
-
*
|
|
747
|
-
* @param tapTree - Encoded TapTree bytes
|
|
748
|
-
* @returns Decoded virtual output script
|
|
749
|
-
* @throws Error if the TapTree cannot be decoded into a valid script set
|
|
750
|
-
* or a leaf carries a version other than {@link TAP_LEAF_VERSION}
|
|
751
|
-
* @see encode
|
|
752
|
-
*/
|
|
753
|
-
static decode(tapTree) {
|
|
754
|
-
const leaves = TapTreeCoder.decode(tapTree);
|
|
755
|
-
const foreign = leaves.find((leaf) => leaf.version !== TAP_LEAF_VERSION);
|
|
756
|
-
if (foreign) {
|
|
757
|
-
throw new Error(`unsupported tap leaf version 0x${foreign.version.toString(16)}`);
|
|
758
|
-
}
|
|
759
|
-
const scripts = leaves.map((leaf) => leaf.script);
|
|
760
|
-
if (leaves.every((leaf) => leaf.depth === 1)) {
|
|
761
|
-
return new _VtxoScript(scripts);
|
|
762
|
-
}
|
|
763
|
-
const layout = btcdLeafLayout(leaves.length);
|
|
764
|
-
if (layout.some((slot, i) => slot.depth !== leaves[i].depth)) {
|
|
765
|
-
return new _VtxoScript(scripts);
|
|
766
|
-
}
|
|
767
|
-
const ordered = new Array(leaves.length);
|
|
768
|
-
layout.forEach((slot, i) => {
|
|
769
|
-
ordered[slot.index] = scripts[i];
|
|
770
|
-
});
|
|
771
|
-
return new _VtxoScript(ordered);
|
|
772
|
-
}
|
|
773
|
-
/**
|
|
774
|
-
* Encode the virtual output script to a TapTree byte representation.
|
|
775
|
-
*
|
|
776
|
-
* @returns Encoded TapTree bytes
|
|
777
|
-
* @see decode
|
|
778
|
-
*/
|
|
779
|
-
encode() {
|
|
780
|
-
const tapTree = TapTreeCoder.encode(
|
|
781
|
-
this.scripts.map((script) => ({
|
|
782
|
-
depth: 1,
|
|
783
|
-
version: TAP_LEAF_VERSION,
|
|
784
|
-
script
|
|
785
|
-
}))
|
|
786
|
-
);
|
|
787
|
-
return tapTree;
|
|
788
|
-
}
|
|
789
|
-
/**
|
|
790
|
-
* Build the Arkade address corresponding to this virtual output script.
|
|
791
|
-
*
|
|
792
|
-
* @param prefix - Bech32 human-readable prefix
|
|
793
|
-
* @param serverPubKey - 32-byte Arkade server public key
|
|
794
|
-
* @returns Arkade address for this script
|
|
795
|
-
* @see ArkAddress
|
|
796
|
-
*/
|
|
797
|
-
address(prefix = DEFAULT_NETWORK.hrp, serverPubKey) {
|
|
798
|
-
return new ArkAddress(serverPubKey, this.tweakedPublicKey, prefix);
|
|
799
|
-
}
|
|
800
|
-
/**
|
|
801
|
-
* Build the Taproot onchain address corresponding to this virtual output script.
|
|
802
|
-
*
|
|
803
|
-
* @param network - Bitcoin network descriptor
|
|
804
|
-
* @returns Taproot onchain address
|
|
805
|
-
* @see address
|
|
806
|
-
*/
|
|
807
|
-
onchainAddress(network = DEFAULT_NETWORK) {
|
|
808
|
-
return Address(network).encode({
|
|
809
|
-
type: "tr",
|
|
810
|
-
pubkey: this.tweakedPublicKey
|
|
811
|
-
});
|
|
812
|
-
}
|
|
813
|
-
/**
|
|
814
|
-
* Look up a tapleaf script by its hex-encoded tapscript body.
|
|
815
|
-
*
|
|
816
|
-
* @param scriptHex - Hex-encoded tapscript body without the leaf version byte
|
|
817
|
-
* @returns Matching tapleaf script
|
|
818
|
-
* @throws Error if no matching leaf exists
|
|
819
|
-
*/
|
|
820
|
-
findLeaf(scriptHex) {
|
|
821
|
-
const leaf = this.leaves.find(
|
|
822
|
-
(leaf2) => hex.encode(scriptFromTapLeafScript(leaf2)) === scriptHex
|
|
823
|
-
);
|
|
824
|
-
if (!leaf) {
|
|
825
|
-
throw new Error(`leaf '${scriptHex}' not found`);
|
|
826
|
-
}
|
|
827
|
-
return leaf;
|
|
828
|
-
}
|
|
829
|
-
/**
|
|
830
|
-
* Return all unilateral exit paths embedded in the virtual output script.
|
|
831
|
-
*
|
|
832
|
-
* @returns CSV-based exit paths found in the leaves
|
|
833
|
-
* @see getSequence
|
|
834
|
-
*/
|
|
835
|
-
exitPaths() {
|
|
836
|
-
const paths = [];
|
|
837
|
-
for (const leaf of this.leaves) {
|
|
838
|
-
try {
|
|
839
|
-
const script = scriptFromTapLeafScript(leaf);
|
|
840
|
-
if (CSVMultisigTapscript.isScriptValid(script) === true) {
|
|
841
|
-
const tapScript = CSVMultisigTapscript.decode(script);
|
|
842
|
-
paths.push(tapScript);
|
|
843
|
-
} else if (ConditionCSVMultisigTapscript.isScriptValid(script) === true) {
|
|
844
|
-
const tapScript = ConditionCSVMultisigTapscript.decode(script);
|
|
845
|
-
paths.push(tapScript);
|
|
846
|
-
}
|
|
847
|
-
} catch (e) {
|
|
848
|
-
console.debug("Failed to decode script", e);
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
return paths;
|
|
852
|
-
}
|
|
853
|
-
};
|
|
854
|
-
function toBIP371TapTree(tapTree) {
|
|
855
|
-
const scripts = TapTreeCoder.decode(tapTree).map((leaf) => leaf.script);
|
|
856
|
-
const vtxoScript = new VtxoScript(scripts);
|
|
857
|
-
return vtxoScript.leaves.map((leaf) => ({
|
|
858
|
-
depth: leaf[0].merklePath.length,
|
|
859
|
-
version: TAP_LEAF_VERSION,
|
|
860
|
-
script: scriptFromTapLeafScript(leaf)
|
|
861
|
-
}));
|
|
862
|
-
}
|
|
863
|
-
function getSequence(tapLeafScript) {
|
|
864
|
-
let sequence = void 0;
|
|
865
|
-
try {
|
|
866
|
-
const scriptWithLeafVersion = tapLeafScript[1];
|
|
867
|
-
const script = scriptWithLeafVersion.subarray(0, scriptWithLeafVersion.length - 1);
|
|
868
|
-
try {
|
|
869
|
-
const params = CSVMultisigTapscript.decode(script).params;
|
|
870
|
-
sequence = timelockToSequence(params.timelock);
|
|
871
|
-
} catch {
|
|
872
|
-
const params = CLTVMultisigTapscript.decode(script).params;
|
|
873
|
-
sequence = Number(params.absoluteTimelock);
|
|
874
|
-
}
|
|
875
|
-
} catch {
|
|
876
|
-
}
|
|
877
|
-
return sequence;
|
|
878
|
-
}
|
|
879
8
|
var Transaction = class extends Transaction$1 {
|
|
880
9
|
static ARK_TX_OPTS = {
|
|
881
10
|
unknown: "ignore",
|
|
@@ -1170,32 +299,8 @@ function checkKeyMatch(key, arkPsbtFieldKey, prefixOnly = false) {
|
|
|
1170
299
|
return true;
|
|
1171
300
|
}
|
|
1172
301
|
|
|
1173
|
-
// src/providers/eventSource.ts
|
|
1174
|
-
var GUIDANCE = "Pass one with configureEventSource(url => new EventSource(url)) \u2014 in Node, from the `eventsource` package or by running with --experimental-eventsource. In React Native, use ExpoArkProvider / ExpoIndexerProvider instead.";
|
|
1175
|
-
var EventSourceUnavailableError = class extends Error {
|
|
1176
|
-
constructor() {
|
|
1177
|
-
super(`no EventSource is available, so server-sent events cannot be opened. ${GUIDANCE}`);
|
|
1178
|
-
this.name = "EventSourceUnavailableError";
|
|
1179
|
-
}
|
|
1180
|
-
};
|
|
1181
|
-
function isEventSourceUnavailableError(error) {
|
|
1182
|
-
return error instanceof EventSourceUnavailableError || error instanceof Error && error.name === "EventSourceUnavailableError";
|
|
1183
|
-
}
|
|
1184
|
-
var configured;
|
|
1185
|
-
function configureEventSource(factory) {
|
|
1186
|
-
configured = factory;
|
|
1187
|
-
}
|
|
1188
|
-
function getConfiguredEventSource() {
|
|
1189
|
-
return configured;
|
|
1190
|
-
}
|
|
1191
|
-
function resolveEventSource(override) {
|
|
1192
|
-
const factory = override ?? configured ?? (typeof EventSource === "undefined" ? void 0 : (url) => new EventSource(url));
|
|
1193
|
-
if (!factory) throw new EventSourceUnavailableError();
|
|
1194
|
-
return factory;
|
|
1195
|
-
}
|
|
1196
|
-
|
|
1197
302
|
// package.json
|
|
1198
|
-
var version = "0.5.
|
|
303
|
+
var version = "0.5.5";
|
|
1199
304
|
|
|
1200
305
|
// src/utils/fetch.ts
|
|
1201
306
|
var buildVersion = "0.9.9";
|
|
@@ -1261,6 +366,103 @@ function describeRequest(input, init) {
|
|
|
1261
366
|
return { url, method };
|
|
1262
367
|
}
|
|
1263
368
|
|
|
369
|
+
// src/providers/sseFrames.ts
|
|
370
|
+
async function* readSseFrames(body) {
|
|
371
|
+
const reader = body.getReader();
|
|
372
|
+
const decoder = new TextDecoder();
|
|
373
|
+
let buffer = "";
|
|
374
|
+
let pendingCR = false;
|
|
375
|
+
let event = "";
|
|
376
|
+
let data = [];
|
|
377
|
+
try {
|
|
378
|
+
for (; ; ) {
|
|
379
|
+
const { done, value: chunk } = await reader.read();
|
|
380
|
+
if (done) return;
|
|
381
|
+
const text = decoder.decode(chunk, { stream: true });
|
|
382
|
+
if (!text) continue;
|
|
383
|
+
const skipLF = pendingCR && text.startsWith("\n");
|
|
384
|
+
pendingCR = text.endsWith("\r");
|
|
385
|
+
buffer += (skipLF ? text.slice(1) : text).replace(/\r\n?/g, "\n");
|
|
386
|
+
const lines = buffer.split("\n");
|
|
387
|
+
buffer = lines.pop() ?? "";
|
|
388
|
+
for (const line of lines) {
|
|
389
|
+
if (line === "") {
|
|
390
|
+
const joined = data.join("\n");
|
|
391
|
+
if (joined) yield { event: event || "message", data: joined };
|
|
392
|
+
event = "";
|
|
393
|
+
data = [];
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
const colon = line.indexOf(":");
|
|
397
|
+
const field = colon === -1 ? line : line.slice(0, colon);
|
|
398
|
+
const value = colon === -1 ? "" : line.slice(colon + 1).replace(/^ /, "");
|
|
399
|
+
if (field === "event") event = value;
|
|
400
|
+
else if (field === "data") data.push(value);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
} finally {
|
|
404
|
+
await reader.cancel().catch(() => {
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// src/providers/eventSource.ts
|
|
410
|
+
var GUIDANCE = "Pass one with configureEventSource(url => new EventSource(url)) \u2014 in Node, from the `eventsource` package or by running with --experimental-eventsource. In React Native, use ExpoOperatorProvider / ExpoIndexerProvider instead.";
|
|
411
|
+
var EventSourceUnavailableError = class extends Error {
|
|
412
|
+
constructor() {
|
|
413
|
+
super(`no EventSource is available, so server-sent events cannot be opened. ${GUIDANCE}`);
|
|
414
|
+
this.name = "EventSourceUnavailableError";
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
function isEventSourceUnavailableError(error) {
|
|
418
|
+
return error instanceof EventSourceUnavailableError || error instanceof Error && error.name === "EventSourceUnavailableError";
|
|
419
|
+
}
|
|
420
|
+
var configured;
|
|
421
|
+
function configureEventSource(factory) {
|
|
422
|
+
configured = factory;
|
|
423
|
+
}
|
|
424
|
+
function getConfiguredEventSource() {
|
|
425
|
+
return configured;
|
|
426
|
+
}
|
|
427
|
+
function resolveEventSource(override) {
|
|
428
|
+
const factory = override ?? configured ?? (typeof globalThis.fetch === "function" && typeof ReadableStream !== "undefined" ? (url, headers) => new FetchEventSource(url, headers) : void 0) ?? (typeof EventSource === "undefined" ? void 0 : (url) => new EventSource(url));
|
|
429
|
+
if (!factory) throw new EventSourceUnavailableError();
|
|
430
|
+
return factory;
|
|
431
|
+
}
|
|
432
|
+
var FetchEventSource = class {
|
|
433
|
+
events = new EventTarget();
|
|
434
|
+
controller = new AbortController();
|
|
435
|
+
constructor(url, headers = {}) {
|
|
436
|
+
this.read(url, headers).catch(() => {
|
|
437
|
+
if (!this.controller.signal.aborted) this.events.dispatchEvent(new Event("error"));
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
addEventListener(type, listener) {
|
|
441
|
+
this.events.addEventListener(type, listener);
|
|
442
|
+
}
|
|
443
|
+
removeEventListener(type, listener) {
|
|
444
|
+
this.events.removeEventListener(type, listener);
|
|
445
|
+
}
|
|
446
|
+
close() {
|
|
447
|
+
this.controller.abort();
|
|
448
|
+
}
|
|
449
|
+
async read(url, headers) {
|
|
450
|
+
const response = await baseFetch(url, {
|
|
451
|
+
headers: { Accept: "text/event-stream", ...headers },
|
|
452
|
+
signal: this.controller.signal
|
|
453
|
+
});
|
|
454
|
+
const type = response.headers.get("Content-Type") ?? "";
|
|
455
|
+
if (response.status !== 200 || !response.body || !/^text\/event-stream\s*(;|$)/i.test(type)) {
|
|
456
|
+
await response.body?.cancel();
|
|
457
|
+
throw new Error(`SSE response ${response.status} ${type}`);
|
|
458
|
+
}
|
|
459
|
+
for await (const { event, data } of readSseFrames(response.body)) {
|
|
460
|
+
this.events.dispatchEvent(new MessageEvent(event, { data }));
|
|
461
|
+
}
|
|
462
|
+
throw new Error("SSE stream ended");
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
|
|
1264
466
|
// src/providers/errors.ts
|
|
1265
467
|
var ArkError = class extends Error {
|
|
1266
468
|
constructor(code, message, name, metadata) {
|
|
@@ -1287,7 +489,8 @@ var ArkErrorName = {
|
|
|
1287
489
|
* timestamp**, not wall clock, so a prompt spend is rejected until a later block lands.
|
|
1288
490
|
*/
|
|
1289
491
|
FORFEIT_CLOSURE_LOCKED: "FORFEIT_CLOSURE_LOCKED",
|
|
1290
|
-
INVALID_INTENT_PROOF: "INVALID_INTENT_PROOF"
|
|
492
|
+
INVALID_INTENT_PROOF: "INVALID_INTENT_PROOF",
|
|
493
|
+
INTERNAL_ERROR: "INTERNAL_ERROR"
|
|
1291
494
|
};
|
|
1292
495
|
function isArkError(error, name) {
|
|
1293
496
|
return error instanceof ArkError && (name === void 0 || error.name === name);
|
|
@@ -1355,42 +558,42 @@ function maybeArkError(error) {
|
|
|
1355
558
|
function isMetadata(value) {
|
|
1356
559
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1357
560
|
}
|
|
1358
|
-
var
|
|
1359
|
-
((
|
|
561
|
+
var Proof;
|
|
562
|
+
((Proof2) => {
|
|
1360
563
|
function create(message, ins, outputs = []) {
|
|
1361
564
|
if (typeof message !== "string") {
|
|
1362
565
|
message = encodeMessage(message);
|
|
1363
566
|
}
|
|
1364
|
-
if (ins.length == 0) throw new Error("
|
|
1365
|
-
const inputs = ins.map(
|
|
567
|
+
if (ins.length == 0) throw new Error("proof requires at least one input");
|
|
568
|
+
const inputs = ins.map(prepareProofInput);
|
|
1366
569
|
validateInputs(inputs);
|
|
1367
570
|
validateOutputs(outputs);
|
|
1368
571
|
const toSpend = craftToSpendTx(message, inputs[0].witnessUtxo.script);
|
|
1369
572
|
return craftToSignTx(toSpend, inputs, outputs, message);
|
|
1370
573
|
}
|
|
1371
|
-
|
|
574
|
+
Proof2.create = create;
|
|
1372
575
|
function fee(proof) {
|
|
1373
576
|
let sumOfInputs = 0n;
|
|
1374
577
|
for (let i = 0; i < proof.inputsLength; i++) {
|
|
1375
578
|
const input = proof.getInput(i);
|
|
1376
579
|
if (input.witnessUtxo === void 0)
|
|
1377
|
-
throw new Error("
|
|
580
|
+
throw new Error("proof input requires witness utxo");
|
|
1378
581
|
sumOfInputs += input.witnessUtxo.amount;
|
|
1379
582
|
}
|
|
1380
583
|
let sumOfOutputs = 0n;
|
|
1381
584
|
for (let i = 0; i < proof.outputsLength; i++) {
|
|
1382
585
|
const output = proof.getOutput(i);
|
|
1383
|
-
if (output.amount === void 0) throw new Error("
|
|
586
|
+
if (output.amount === void 0) throw new Error("proof output requires amount");
|
|
1384
587
|
sumOfOutputs += output.amount;
|
|
1385
588
|
}
|
|
1386
589
|
if (sumOfOutputs > sumOfInputs) {
|
|
1387
590
|
throw new Error(
|
|
1388
|
-
`
|
|
591
|
+
`proof output amount is greater than input amount: ${sumOfOutputs} > ${sumOfInputs}`
|
|
1389
592
|
);
|
|
1390
593
|
}
|
|
1391
594
|
return Number(sumOfInputs - sumOfOutputs);
|
|
1392
595
|
}
|
|
1393
|
-
|
|
596
|
+
Proof2.fee = fee;
|
|
1394
597
|
function encodeMessage(message) {
|
|
1395
598
|
switch (message.type) {
|
|
1396
599
|
case "register":
|
|
@@ -1413,31 +616,31 @@ var Intent;
|
|
|
1413
616
|
});
|
|
1414
617
|
}
|
|
1415
618
|
}
|
|
1416
|
-
|
|
1417
|
-
})(
|
|
619
|
+
Proof2.encodeMessage = encodeMessage;
|
|
620
|
+
})(Proof || (Proof = {}));
|
|
621
|
+
var Intent = Proof;
|
|
1418
622
|
var OP_RETURN_EMPTY_PKSCRIPT = new Uint8Array([OP.RETURN]);
|
|
1419
623
|
var ZERO_32 = new Uint8Array(32).fill(0);
|
|
1420
624
|
var MAX_INDEX = 4294967295;
|
|
1421
|
-
var
|
|
625
|
+
var TAG_PROOF = "ark-intent-proof-message";
|
|
1422
626
|
function validateInput(input) {
|
|
1423
|
-
if (input.index === void 0) throw new Error("
|
|
1424
|
-
if (input.txid === void 0) throw new Error("
|
|
1425
|
-
if (input.witnessUtxo === void 0)
|
|
1426
|
-
throw new Error("intent proof input requires witness utxo");
|
|
627
|
+
if (input.index === void 0) throw new Error("proof input requires index");
|
|
628
|
+
if (input.txid === void 0) throw new Error("proof input requires txid");
|
|
629
|
+
if (input.witnessUtxo === void 0) throw new Error("proof input requires witness utxo");
|
|
1427
630
|
return true;
|
|
1428
631
|
}
|
|
1429
632
|
function validateInputs(inputs) {
|
|
1430
633
|
inputs.forEach(validateInput);
|
|
1431
634
|
}
|
|
1432
635
|
function validateOutput(output) {
|
|
1433
|
-
if (output.amount === void 0) throw new Error("
|
|
1434
|
-
if (output.script === void 0) throw new Error("
|
|
636
|
+
if (output.amount === void 0) throw new Error("proof output requires amount");
|
|
637
|
+
if (output.script === void 0) throw new Error("proof output requires script");
|
|
1435
638
|
return true;
|
|
1436
639
|
}
|
|
1437
640
|
function validateOutputs(outputs) {
|
|
1438
641
|
outputs.forEach(validateOutput);
|
|
1439
642
|
}
|
|
1440
|
-
function craftToSpendTx(message, pkScript, tag =
|
|
643
|
+
function craftToSpendTx(message, pkScript, tag = TAG_PROOF) {
|
|
1441
644
|
const messageHash = hashMessage(message, tag);
|
|
1442
645
|
const tx = new Transaction({
|
|
1443
646
|
version: 0
|
|
@@ -1502,15 +705,15 @@ function craftToSignTx(toSpend, inputs, outputs, message) {
|
|
|
1502
705
|
tx.global.genericSignedMessage = new TextEncoder().encode(message);
|
|
1503
706
|
return tx;
|
|
1504
707
|
}
|
|
1505
|
-
function hashMessage(message, tag =
|
|
708
|
+
function hashMessage(message, tag = TAG_PROOF) {
|
|
1506
709
|
return schnorr.utils.taggedHash(tag, new TextEncoder().encode(message));
|
|
1507
710
|
}
|
|
1508
|
-
function
|
|
711
|
+
function prepareProofInput(coin) {
|
|
1509
712
|
if (!("tapTree" in coin)) {
|
|
1510
713
|
return coin;
|
|
1511
714
|
}
|
|
1512
715
|
const vtxoScript = VtxoScript.decode(coin.tapTree);
|
|
1513
|
-
const sequence = getSequence(coin
|
|
716
|
+
const sequence = getSequence(proofTapLeafScriptOf(coin));
|
|
1514
717
|
const unknown = [VtxoTaprootTree.encode(coin.tapTree)];
|
|
1515
718
|
if (coin.extraWitness) {
|
|
1516
719
|
unknown.push(ConditionWitness.encode(coin.extraWitness));
|
|
@@ -1526,7 +729,7 @@ function prepareCoinAsIntentProofInput(coin) {
|
|
|
1526
729
|
script: vtxoScript.pkScript
|
|
1527
730
|
},
|
|
1528
731
|
sequence,
|
|
1529
|
-
tapLeafScript: [coin
|
|
732
|
+
tapLeafScript: [proofTapLeafScriptOf(coin)],
|
|
1530
733
|
unknown
|
|
1531
734
|
};
|
|
1532
735
|
}
|
|
@@ -1789,6 +992,35 @@ var DigestMismatchError = class extends Error {
|
|
|
1789
992
|
this.name = "DigestMismatchError";
|
|
1790
993
|
}
|
|
1791
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
|
+
};
|
|
1792
1024
|
var SettlementEventType = /* @__PURE__ */ ((SettlementEventType2) => {
|
|
1793
1025
|
SettlementEventType2["BatchStarted"] = "batch_started";
|
|
1794
1026
|
SettlementEventType2["BatchFinalization"] = "batch_finalization";
|
|
@@ -1806,7 +1038,7 @@ function advertisedLimit(fromServer) {
|
|
|
1806
1038
|
const limit = BigInt(fromServer);
|
|
1807
1039
|
return limit > 0n ? limit : void 0;
|
|
1808
1040
|
}
|
|
1809
|
-
var
|
|
1041
|
+
var RestOperatorProvider = class {
|
|
1810
1042
|
constructor(serverUrl = DEFAULT_ARKADE_SERVER_URL, options = {}) {
|
|
1811
1043
|
this.serverUrl = serverUrl;
|
|
1812
1044
|
this.eventSource = options.eventSource;
|
|
@@ -1815,16 +1047,17 @@ var RestArkProvider = class {
|
|
|
1815
1047
|
/** Overrides {@link configureEventSource} for this provider's streams. */
|
|
1816
1048
|
eventSource;
|
|
1817
1049
|
/**
|
|
1818
|
-
* Last
|
|
1050
|
+
* Last operator info digest seen (from {@link getInfo}). Sent as `X-Digest`
|
|
1819
1051
|
* on outgoing requests so arkd can reject a client whose cached info is
|
|
1820
1052
|
* stale. Empty until the first {@link getInfo}.
|
|
1821
1053
|
*/
|
|
1822
1054
|
_digest = "";
|
|
1055
|
+
_onchainCosignUnsupported = false;
|
|
1823
1056
|
_hasServerInfo = false;
|
|
1824
1057
|
_suppressNextGetInfoChangeEmit = false;
|
|
1825
1058
|
_serverInfoListeners = /* @__PURE__ */ new Set();
|
|
1826
1059
|
/**
|
|
1827
|
-
* Subscribe to
|
|
1060
|
+
* Subscribe to operator info changes. Fired after a stale-info
|
|
1828
1061
|
* `DIGEST_MISMATCH` refresh or when {@link getInfo} observes a changed digest.
|
|
1829
1062
|
* Returns an unsubscribe function.
|
|
1830
1063
|
*/
|
|
@@ -1835,6 +1068,7 @@ var RestArkProvider = class {
|
|
|
1835
1068
|
};
|
|
1836
1069
|
}
|
|
1837
1070
|
emitServerInfoChanged(info) {
|
|
1071
|
+
this._onchainCosignUnsupported = false;
|
|
1838
1072
|
for (const listener of this._serverInfoListeners) {
|
|
1839
1073
|
try {
|
|
1840
1074
|
listener(info);
|
|
@@ -1844,7 +1078,7 @@ var RestArkProvider = class {
|
|
|
1844
1078
|
}
|
|
1845
1079
|
}
|
|
1846
1080
|
/**
|
|
1847
|
-
* `fetch` wrapper for arkd requests that participates in
|
|
1081
|
+
* `fetch` wrapper for arkd requests that participates in operator info digest
|
|
1848
1082
|
* negotiation. Sends the cached `X-Digest`; when arkd rejects a request with
|
|
1849
1083
|
* `DIGEST_MISMATCH`, refreshes {@link getInfo} (updating the digest), fires
|
|
1850
1084
|
* {@link onServerInfoChanged}, and THROWS {@link DigestMismatchError} — it
|
|
@@ -1878,7 +1112,7 @@ var RestArkProvider = class {
|
|
|
1878
1112
|
return response;
|
|
1879
1113
|
}
|
|
1880
1114
|
const arkError = maybeArkError(new Error(body));
|
|
1881
|
-
if (!arkError) throwIfHttpUnavailable(response, "arkade");
|
|
1115
|
+
if (!arkError && response.status !== 501) throwIfHttpUnavailable(response, "arkade");
|
|
1882
1116
|
if (!isArkError(arkError, ArkErrorName.DIGEST_MISMATCH)) return response;
|
|
1883
1117
|
this._digest = "";
|
|
1884
1118
|
this._suppressNextGetInfoChangeEmit = true;
|
|
@@ -1990,6 +1224,41 @@ var RestArkProvider = class {
|
|
|
1990
1224
|
signedCheckpointTxs: data.signedCheckpointTxs
|
|
1991
1225
|
};
|
|
1992
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
|
+
}
|
|
1993
1262
|
async finalizeTx(arkTxid, finalCheckpointTxs) {
|
|
1994
1263
|
const url = `${this.serverUrl}/v1/tx/finalize`;
|
|
1995
1264
|
await this.postJson(
|
|
@@ -2008,7 +1277,7 @@ var RestArkProvider = class {
|
|
|
2008
1277
|
{
|
|
2009
1278
|
intent: {
|
|
2010
1279
|
proof: intent.proof,
|
|
2011
|
-
message:
|
|
1280
|
+
message: Proof.encodeMessage(intent.message)
|
|
2012
1281
|
}
|
|
2013
1282
|
},
|
|
2014
1283
|
(errorText) => `Failed to register intent: ${errorText}`
|
|
@@ -2023,7 +1292,7 @@ var RestArkProvider = class {
|
|
|
2023
1292
|
{
|
|
2024
1293
|
intent: {
|
|
2025
1294
|
proof: intent.proof,
|
|
2026
|
-
message:
|
|
1295
|
+
message: Proof.encodeMessage(intent.message)
|
|
2027
1296
|
}
|
|
2028
1297
|
},
|
|
2029
1298
|
(errorText) => `Failed to delete intent: ${errorText}`
|
|
@@ -2086,7 +1355,10 @@ var RestArkProvider = class {
|
|
|
2086
1355
|
try {
|
|
2087
1356
|
while (!signal?.aborted) {
|
|
2088
1357
|
const currentIterator = eventSourceIterator(
|
|
2089
|
-
resolveEventSource(self.eventSource)(
|
|
1358
|
+
resolveEventSource(self.eventSource)(
|
|
1359
|
+
url + queryParams,
|
|
1360
|
+
ARKADE_VERSION_HEADERS
|
|
1361
|
+
)
|
|
2090
1362
|
);
|
|
2091
1363
|
iterator = currentIterator;
|
|
2092
1364
|
try {
|
|
@@ -2146,7 +1418,7 @@ var RestArkProvider = class {
|
|
|
2146
1418
|
while (!signal?.aborted) {
|
|
2147
1419
|
try {
|
|
2148
1420
|
const currentIterator = eventSourceIterator(
|
|
2149
|
-
resolveEventSource(self.eventSource)(url)
|
|
1421
|
+
resolveEventSource(self.eventSource)(url, ARKADE_VERSION_HEADERS)
|
|
2150
1422
|
);
|
|
2151
1423
|
iterator = currentIterator;
|
|
2152
1424
|
for await (const event of currentIterator) {
|
|
@@ -2200,7 +1472,7 @@ var RestArkProvider = class {
|
|
|
2200
1472
|
{
|
|
2201
1473
|
intent: {
|
|
2202
1474
|
proof: intent.proof,
|
|
2203
|
-
message:
|
|
1475
|
+
message: Proof.encodeMessage(intent.message)
|
|
2204
1476
|
}
|
|
2205
1477
|
},
|
|
2206
1478
|
(errorText) => `Failed to get pending transactions: ${errorText}`
|
|
@@ -2392,6 +1664,7 @@ function handleError(errorText, defaultMessage) {
|
|
|
2392
1664
|
const arkError = maybeArkError(error);
|
|
2393
1665
|
throw arkError ?? new Error(defaultMessage);
|
|
2394
1666
|
}
|
|
1667
|
+
var RestArkProvider = RestOperatorProvider;
|
|
2395
1668
|
|
|
2396
1669
|
// src/providers/availability.ts
|
|
2397
1670
|
function isRetryableProviderError(error) {
|
|
@@ -2561,6 +1834,6 @@ function isVirtualCoin(input) {
|
|
|
2561
1834
|
return typeof input === "object" && input !== null && typeof input.script === "string";
|
|
2562
1835
|
}
|
|
2563
1836
|
|
|
2564
|
-
export { ARKADE_VERSION_HEADERS,
|
|
2565
|
-
//# sourceMappingURL=chunk-
|
|
2566
|
-
//# 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
|