@arkade-os/sdk 0.5.0-rc.3 → 0.5.0-rc.30
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 +223 -83
- package/dist/adapters/expo.cjs +5 -6
- package/dist/adapters/expo.d.cts +5 -5
- package/dist/adapters/expo.d.ts +5 -5
- package/dist/adapters/expo.js +3 -4
- package/dist/{asyncStorageTaskQueue-DAs_n1xr.d.ts → asyncStorageTaskQueue-DAVf4Fyy.d.ts} +2 -2
- package/dist/{asyncStorageTaskQueue-DqGRG66E.d.cts → asyncStorageTaskQueue-fkkUPrhz.d.cts} +2 -2
- package/dist/{chunk-FSF2PGTR.cjs → chunk-3SM7BMIE.cjs} +6736 -5189
- package/dist/chunk-3SM7BMIE.cjs.map +1 -0
- package/dist/{chunk-UCSTNXB4.cjs → chunk-4MCQZYH6.cjs} +501 -1153
- package/dist/chunk-4MCQZYH6.cjs.map +1 -0
- package/dist/{chunk-BCT7DT2J.cjs → chunk-6BTZ6KFI.cjs} +12 -10
- package/dist/chunk-6BTZ6KFI.cjs.map +1 -0
- package/dist/{chunk-L4HZK4KK.cjs → chunk-AICUNREA.cjs} +158 -263
- package/dist/chunk-AICUNREA.cjs.map +1 -0
- package/dist/{chunk-UJTYUYY4.cjs → chunk-ASKOHVGX.cjs} +596 -467
- package/dist/chunk-ASKOHVGX.cjs.map +1 -0
- package/dist/{chunk-43XGA362.cjs → chunk-DLHE4RCH.cjs} +40 -43
- package/dist/chunk-DLHE4RCH.cjs.map +1 -0
- package/dist/{chunk-7W6FHWYN.js → chunk-ESEHYZAY.js} +434 -1109
- package/dist/chunk-ESEHYZAY.js.map +1 -0
- package/dist/{chunk-HSG2GQTM.js → chunk-G6ZPX2GL.js} +586 -462
- package/dist/chunk-G6ZPX2GL.js.map +1 -0
- package/dist/{chunk-G4UPMCKW.js → chunk-KK6GXY4L.js} +33 -36
- package/dist/chunk-KK6GXY4L.js.map +1 -0
- package/dist/{chunk-ERUAQGYF.js → chunk-PY3YQNHP.js} +146 -252
- package/dist/chunk-PY3YQNHP.js.map +1 -0
- package/dist/{chunk-VQCDDGRW.js → chunk-XW72DWYO.js} +6350 -4816
- package/dist/chunk-XW72DWYO.js.map +1 -0
- package/dist/{chunk-ROD5H7RX.js → chunk-YML2N4SC.js} +7 -5
- package/dist/chunk-YML2N4SC.js.map +1 -0
- package/dist/contracts/handlers/index.cjs +9 -10
- package/dist/contracts/handlers/index.d.cts +5 -5
- package/dist/contracts/handlers/index.d.ts +5 -5
- package/dist/contracts/handlers/index.js +2 -3
- package/dist/{delegate-CZzIduW4.d.cts → delegate-BYoNu3lf.d.cts} +1 -1
- package/dist/{delegate-B9Bmrns7.d.ts → delegate-DsqzUVId.d.ts} +1 -1
- package/dist/index-CXNTURO_.d.cts +645 -0
- package/dist/{index-DnNIOqHS.d.ts → index-DhP68WBD.d.cts} +1698 -2402
- package/dist/{index-DnNIOqHS.d.cts → index-DhP68WBD.d.ts} +1698 -2402
- package/dist/index-pQr5y77P.d.ts +645 -0
- package/dist/index.cjs +407 -356
- package/dist/index.d.cts +1045 -1302
- package/dist/index.d.ts +1045 -1302
- package/dist/index.js +4 -5
- package/dist/repositories/realm/index.cjs +201 -106
- package/dist/repositories/realm/index.cjs.map +1 -1
- package/dist/repositories/realm/index.d.cts +28 -16
- package/dist/repositories/realm/index.d.ts +28 -16
- package/dist/repositories/realm/index.js +190 -95
- package/dist/repositories/realm/index.js.map +1 -1
- package/dist/repositories/sqlite/index.cjs +301 -189
- package/dist/repositories/sqlite/index.cjs.map +1 -1
- package/dist/repositories/sqlite/index.d.cts +32 -21
- package/dist/repositories/sqlite/index.d.ts +32 -21
- package/dist/repositories/sqlite/index.js +294 -182
- package/dist/repositories/sqlite/index.js.map +1 -1
- package/dist/{taskRunner-BDb5Ee2J.d.cts → taskRunner-C8H3bSsk.d.cts} +2 -2
- package/dist/{taskRunner-F7wNJcF3.d.ts → taskRunner-DKtBy8Yo.d.ts} +2 -2
- package/dist/wallet/expo/background.cjs +14 -15
- package/dist/wallet/expo/background.cjs.map +1 -1
- package/dist/wallet/expo/background.d.cts +10 -16
- package/dist/wallet/expo/background.d.ts +10 -16
- package/dist/wallet/expo/background.js +6 -7
- package/dist/wallet/expo/background.js.map +1 -1
- package/dist/wallet/expo/index.cjs +20 -23
- package/dist/wallet/expo/index.cjs.map +1 -1
- package/dist/wallet/expo/index.d.cts +8 -9
- package/dist/wallet/expo/index.d.ts +8 -9
- package/dist/wallet/expo/index.js +12 -15
- package/dist/wallet/expo/index.js.map +1 -1
- package/dist/wallet-BmAHDzK3.d.cts +1874 -0
- package/dist/wallet-C0W8EOnA.d.ts +1874 -0
- package/dist/worker/expo/index.cjs +9 -10
- package/dist/worker/expo/index.cjs.map +1 -1
- package/dist/worker/expo/index.d.cts +6 -6
- package/dist/worker/expo/index.d.ts +6 -6
- package/dist/worker/expo/index.js +5 -6
- package/dist/worker/expo/index.js.map +1 -1
- package/package.json +12 -51
- package/dist/adapters/asyncStorage.cjs +0 -48
- package/dist/adapters/asyncStorage.cjs.map +0 -1
- package/dist/adapters/asyncStorage.d.cts +0 -16
- package/dist/adapters/asyncStorage.d.ts +0 -16
- package/dist/adapters/asyncStorage.js +0 -46
- package/dist/adapters/asyncStorage.js.map +0 -1
- package/dist/adapters/fileSystem.cjs +0 -116
- package/dist/adapters/fileSystem.cjs.map +0 -1
- package/dist/adapters/fileSystem.d.cts +0 -17
- package/dist/adapters/fileSystem.d.ts +0 -17
- package/dist/adapters/fileSystem.js +0 -93
- package/dist/adapters/fileSystem.js.map +0 -1
- package/dist/adapters/indexedDB.cjs +0 -103
- package/dist/adapters/indexedDB.cjs.map +0 -1
- package/dist/adapters/indexedDB.d.cts +0 -18
- package/dist/adapters/indexedDB.d.ts +0 -18
- package/dist/adapters/indexedDB.js +0 -101
- package/dist/adapters/indexedDB.js.map +0 -1
- package/dist/adapters/localStorage.cjs +0 -50
- package/dist/adapters/localStorage.cjs.map +0 -1
- package/dist/adapters/localStorage.d.cts +0 -14
- package/dist/adapters/localStorage.d.ts +0 -14
- package/dist/adapters/localStorage.js +0 -48
- package/dist/adapters/localStorage.js.map +0 -1
- package/dist/chunk-43XGA362.cjs.map +0 -1
- package/dist/chunk-5BLDMQED.cjs +0 -18
- package/dist/chunk-5BLDMQED.cjs.map +0 -1
- package/dist/chunk-7W6FHWYN.js.map +0 -1
- package/dist/chunk-BCT7DT2J.cjs.map +0 -1
- package/dist/chunk-ERUAQGYF.js.map +0 -1
- package/dist/chunk-FSF2PGTR.cjs.map +0 -1
- package/dist/chunk-G4UPMCKW.js.map +0 -1
- package/dist/chunk-HSG2GQTM.js.map +0 -1
- package/dist/chunk-L4HZK4KK.cjs.map +0 -1
- package/dist/chunk-NSBPE2FW.js +0 -15
- package/dist/chunk-NSBPE2FW.js.map +0 -1
- package/dist/chunk-ROD5H7RX.js.map +0 -1
- package/dist/chunk-UCSTNXB4.cjs.map +0 -1
- package/dist/chunk-UJTYUYY4.cjs.map +0 -1
- package/dist/chunk-VQCDDGRW.js.map +0 -1
- package/dist/hdWalletCapable-BIl3yLVM.d.cts +0 -2232
- package/dist/hdWalletCapable-Cr5nGpK5.d.ts +0 -2232
- package/dist/index-C0IanN1m.d.cts +0 -11
- package/dist/index-C0IanN1m.d.ts +0 -11
- package/dist/index-Ci5MmNnK.d.cts +0 -850
- package/dist/index-DQ1Hb2w_.d.ts +0 -850
|
@@ -1,192 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import * as bip68 from 'bip68';
|
|
3
|
-
import { TEST_NETWORK, NETWORK } from '@scure/btc-signer/utils.js';
|
|
1
|
+
import { TEST_NETWORK, NETWORK, concatBytes } from '@scure/btc-signer/utils.js';
|
|
4
2
|
import { hex, bech32m } from '@scure/base';
|
|
5
|
-
import { Script as Script$1 } from '@scure/btc-signer/script.js';
|
|
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
6
|
import { TAP_LEAF_VERSION } from '@scure/btc-signer/payment.js';
|
|
7
|
+
import * as P from 'micro-packed';
|
|
7
8
|
import { PSBTOutput } from '@scure/btc-signer/psbt.js';
|
|
8
9
|
import { schnorr } from '@noble/curves/secp256k1.js';
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
allowUnknownOutputs: true,
|
|
15
|
-
allowUnknownInputs: true
|
|
16
|
-
};
|
|
17
|
-
constructor(opts) {
|
|
18
|
-
super(withArkOpts(opts));
|
|
19
|
-
}
|
|
20
|
-
static fromPSBT(psbt_, opts) {
|
|
21
|
-
return Transaction$1.fromPSBT(psbt_, withArkOpts(opts));
|
|
22
|
-
}
|
|
23
|
-
static fromRaw(raw, opts) {
|
|
24
|
-
return Transaction$1.fromRaw(raw, withArkOpts(opts));
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
function withArkOpts(opts) {
|
|
28
|
-
return { ...Transaction.ARK_TX_OPTS, ...opts };
|
|
29
|
-
}
|
|
30
|
-
function formatSighash(type) {
|
|
31
|
-
return `0x${type.toString(16).padStart(2, "0")}`;
|
|
32
|
-
}
|
|
33
|
-
function assertAllowedSighashTypes(tx, allowedSighashTypes = [SigHash.DEFAULT]) {
|
|
34
|
-
for (let i = 0; i < tx.inputsLength; i++) {
|
|
35
|
-
const declared = tx.getInput(i).sighashType;
|
|
36
|
-
if (declared === void 0) continue;
|
|
37
|
-
if (!allowedSighashTypes.includes(declared)) {
|
|
38
|
-
throw new Error(`Unallowed sighash type ${formatSighash(declared)} for input ${i}.`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function timelockToSequence(timelock) {
|
|
43
|
-
return bip68.encode(
|
|
44
|
-
timelock.type === "blocks" ? { blocks: Number(timelock.value) } : { seconds: Number(timelock.value) }
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
function sequenceToTimelock(sequence) {
|
|
48
|
-
const decoded = bip68.decode(sequence);
|
|
49
|
-
if ("blocks" in decoded && decoded.blocks !== void 0) {
|
|
50
|
-
return { type: "blocks", value: BigInt(decoded.blocks) };
|
|
51
|
-
}
|
|
52
|
-
if ("seconds" in decoded && decoded.seconds !== void 0) {
|
|
53
|
-
return { type: "seconds", value: BigInt(decoded.seconds) };
|
|
54
|
-
}
|
|
55
|
-
throw new Error(`Invalid BIP68 sequence: ${sequence}`);
|
|
56
|
-
}
|
|
57
|
-
var ArkPsbtFieldKey = /* @__PURE__ */ ((ArkPsbtFieldKey2) => {
|
|
58
|
-
ArkPsbtFieldKey2["VtxoTaprootTree"] = "taptree";
|
|
59
|
-
ArkPsbtFieldKey2["VtxoTreeExpiry"] = "expiry";
|
|
60
|
-
ArkPsbtFieldKey2["Cosigner"] = "cosigner";
|
|
61
|
-
ArkPsbtFieldKey2["ConditionWitness"] = "condition";
|
|
62
|
-
ArkPsbtFieldKey2["PrevArkTx"] = "prevarktx";
|
|
63
|
-
ArkPsbtFieldKey2["PrevoutTx"] = "prevouttx";
|
|
64
|
-
return ArkPsbtFieldKey2;
|
|
65
|
-
})(ArkPsbtFieldKey || {});
|
|
66
|
-
var ArkPsbtFieldKeyType = 222;
|
|
67
|
-
function setArkPsbtField(tx, inputIndex, coder, value) {
|
|
68
|
-
tx.updateInput(inputIndex, {
|
|
69
|
-
unknown: [...tx.getInput(inputIndex)?.unknown ?? [], coder.encode(value)]
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
function getArkPsbtFields(tx, inputIndex, coder) {
|
|
73
|
-
const unknown = tx.getInput(inputIndex)?.unknown ?? [];
|
|
74
|
-
const fields = [];
|
|
75
|
-
for (const u of unknown) {
|
|
76
|
-
const v = coder.decode(u);
|
|
77
|
-
if (v !== null) fields.push(v);
|
|
78
|
-
}
|
|
79
|
-
return fields;
|
|
80
|
-
}
|
|
81
|
-
var VtxoTaprootTree = {
|
|
82
|
-
key: "taptree" /* VtxoTaprootTree */,
|
|
83
|
-
encode: (value) => [
|
|
84
|
-
{
|
|
85
|
-
type: ArkPsbtFieldKeyType,
|
|
86
|
-
key: encodedPsbtFieldKey["taptree" /* VtxoTaprootTree */]
|
|
87
|
-
},
|
|
88
|
-
value
|
|
89
|
-
],
|
|
90
|
-
decode: (value) => nullIfCatch(() => {
|
|
91
|
-
if (!checkKeyMatch(value[0], "taptree" /* VtxoTaprootTree */)) return null;
|
|
92
|
-
return value[1];
|
|
93
|
-
})
|
|
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 });
|
|
94
15
|
};
|
|
95
|
-
var ConditionWitness = {
|
|
96
|
-
key: "condition" /* ConditionWitness */,
|
|
97
|
-
encode: (value) => [
|
|
98
|
-
{
|
|
99
|
-
type: ArkPsbtFieldKeyType,
|
|
100
|
-
key: encodedPsbtFieldKey["condition" /* ConditionWitness */]
|
|
101
|
-
},
|
|
102
|
-
RawWitness.encode(value)
|
|
103
|
-
],
|
|
104
|
-
decode: (value) => nullIfCatch(() => {
|
|
105
|
-
if (!checkKeyMatch(value[0], "condition" /* ConditionWitness */)) return null;
|
|
106
|
-
return RawWitness.decode(value[1]);
|
|
107
|
-
})
|
|
108
|
-
};
|
|
109
|
-
var PrevArkTxField = {
|
|
110
|
-
key: "prevarktx" /* PrevArkTx */,
|
|
111
|
-
encode: (value) => [
|
|
112
|
-
{
|
|
113
|
-
type: ArkPsbtFieldKeyType,
|
|
114
|
-
key: encodedPsbtFieldKey["prevarktx" /* PrevArkTx */]
|
|
115
|
-
},
|
|
116
|
-
value
|
|
117
|
-
],
|
|
118
|
-
decode: (value) => nullIfCatch(() => {
|
|
119
|
-
if (!checkKeyMatch(value[0], "prevarktx" /* PrevArkTx */)) return null;
|
|
120
|
-
return value[1];
|
|
121
|
-
})
|
|
122
|
-
};
|
|
123
|
-
var PrevoutTxField = {
|
|
124
|
-
key: "prevouttx" /* PrevoutTx */,
|
|
125
|
-
encode: (value) => [
|
|
126
|
-
{
|
|
127
|
-
type: ArkPsbtFieldKeyType,
|
|
128
|
-
key: encodedPsbtFieldKey["prevouttx" /* PrevoutTx */]
|
|
129
|
-
},
|
|
130
|
-
value
|
|
131
|
-
],
|
|
132
|
-
decode: (value) => nullIfCatch(() => {
|
|
133
|
-
if (!checkKeyMatch(value[0], "prevouttx" /* PrevoutTx */)) return null;
|
|
134
|
-
return value[1];
|
|
135
|
-
})
|
|
136
|
-
};
|
|
137
|
-
var CosignerPublicKey = {
|
|
138
|
-
key: "cosigner" /* Cosigner */,
|
|
139
|
-
encode: (value) => [
|
|
140
|
-
{
|
|
141
|
-
type: ArkPsbtFieldKeyType,
|
|
142
|
-
key: new Uint8Array([...encodedPsbtFieldKey["cosigner" /* Cosigner */], value.index])
|
|
143
|
-
},
|
|
144
|
-
value.key
|
|
145
|
-
],
|
|
146
|
-
decode: (unknown) => nullIfCatch(() => {
|
|
147
|
-
if (!checkKeyMatch(unknown[0], "cosigner" /* Cosigner */, true)) return null;
|
|
148
|
-
return {
|
|
149
|
-
index: unknown[0].key[unknown[0].key.length - 1],
|
|
150
|
-
key: unknown[1]
|
|
151
|
-
};
|
|
152
|
-
})
|
|
153
|
-
};
|
|
154
|
-
var VtxoTreeExpiry = {
|
|
155
|
-
key: "expiry" /* VtxoTreeExpiry */,
|
|
156
|
-
encode: (value) => [
|
|
157
|
-
{
|
|
158
|
-
type: ArkPsbtFieldKeyType,
|
|
159
|
-
key: encodedPsbtFieldKey["expiry" /* VtxoTreeExpiry */]
|
|
160
|
-
},
|
|
161
|
-
ScriptNum(6, true).encode(value.value === 0n ? 0n : value.value)
|
|
162
|
-
],
|
|
163
|
-
decode: (unknown) => nullIfCatch(() => {
|
|
164
|
-
if (!checkKeyMatch(unknown[0], "expiry" /* VtxoTreeExpiry */)) return null;
|
|
165
|
-
const v = ScriptNum(6, true).decode(unknown[1]);
|
|
166
|
-
if (!v) return null;
|
|
167
|
-
return sequenceToTimelock(Number(v));
|
|
168
|
-
})
|
|
169
|
-
};
|
|
170
|
-
var encodedPsbtFieldKey = Object.fromEntries(
|
|
171
|
-
Object.values(ArkPsbtFieldKey).map((key) => [key, new TextEncoder().encode(key)])
|
|
172
|
-
);
|
|
173
|
-
var nullIfCatch = (fn) => {
|
|
174
|
-
try {
|
|
175
|
-
return fn();
|
|
176
|
-
} catch {
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
function checkKeyMatch(key, arkPsbtFieldKey, prefixOnly = false) {
|
|
181
|
-
if (key.type !== ArkPsbtFieldKeyType) return false;
|
|
182
|
-
const expected = encodedPsbtFieldKey[arkPsbtFieldKey];
|
|
183
|
-
if (key.key.length < expected.length) return false;
|
|
184
|
-
if (!prefixOnly && key.key.length !== expected.length) return false;
|
|
185
|
-
for (let i = 0; i < expected.length; i++) {
|
|
186
|
-
if (key.key[i] !== expected[i]) return false;
|
|
187
|
-
}
|
|
188
|
-
return true;
|
|
189
|
-
}
|
|
190
16
|
var getNetwork = (network) => {
|
|
191
17
|
const found = networks[network];
|
|
192
18
|
if (!found) throw new Error(`Unsupported network: ${network}`);
|
|
@@ -217,6 +43,7 @@ function withArkPrefix(network, prefix, name) {
|
|
|
217
43
|
};
|
|
218
44
|
}
|
|
219
45
|
var DEFAULT_ARKADE_SERVER_URL = "https://arkade.computer";
|
|
46
|
+
var DEFAULT_ARKADE_EMULATOR_URL = "https://emulator.arkade.computer";
|
|
220
47
|
var DEFAULT_NETWORK = networks.bitcoin;
|
|
221
48
|
var DEFAULT_NETWORK_NAME = "bitcoin";
|
|
222
49
|
var COMPRESSED_PUBKEY = /^0[23][0-9a-f]{64}$/;
|
|
@@ -323,6 +150,26 @@ var ArkAddress = class _ArkAddress {
|
|
|
323
150
|
return Script$1.encode(["RETURN", this.vtxoTaprootKey]);
|
|
324
151
|
}
|
|
325
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
|
+
}
|
|
326
173
|
var MinimalScriptNum = ScriptNum(void 0, true);
|
|
327
174
|
function decodeTapscript(script) {
|
|
328
175
|
const types = [
|
|
@@ -570,6 +417,16 @@ var CSVMultisigTapscript;
|
|
|
570
417
|
}
|
|
571
418
|
CSVMultisigTapscript2.isScriptValid = isScriptValid;
|
|
572
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
|
+
}
|
|
573
430
|
var ConditionCSVMultisigTapscript;
|
|
574
431
|
((ConditionCSVMultisigTapscript2) => {
|
|
575
432
|
function encode2(params) {
|
|
@@ -594,7 +451,7 @@ var ConditionCSVMultisigTapscript;
|
|
|
594
451
|
throw isValid;
|
|
595
452
|
}
|
|
596
453
|
const asm = Script.decode(script);
|
|
597
|
-
|
|
454
|
+
const verifyIndex = asm.lastIndexOf("VERIFY");
|
|
598
455
|
if (verifyIndex === -1) {
|
|
599
456
|
throw Error("Invalid script: missing VERIFY operation");
|
|
600
457
|
}
|
|
@@ -629,26 +486,8 @@ var ConditionCSVMultisigTapscript;
|
|
|
629
486
|
return tapscript.type === "condition-csv-multisig" /* ConditionCSVMultisig */;
|
|
630
487
|
}
|
|
631
488
|
ConditionCSVMultisigTapscript2.is = is;
|
|
632
|
-
function getVerifyIndex(asm) {
|
|
633
|
-
let verifyIndex = -1;
|
|
634
|
-
for (let i = asm.length - 1; i >= 0; i--) {
|
|
635
|
-
if (asm[i] === "VERIFY") {
|
|
636
|
-
verifyIndex = i;
|
|
637
|
-
return verifyIndex;
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
return verifyIndex;
|
|
641
|
-
}
|
|
642
489
|
function isScriptValid(script) {
|
|
643
|
-
|
|
644
|
-
if (asm.length < 1) {
|
|
645
|
-
return new Error(`Invalid script: too short (expected at least 1)`);
|
|
646
|
-
}
|
|
647
|
-
let verifyIndex = getVerifyIndex(asm);
|
|
648
|
-
if (verifyIndex === -1) {
|
|
649
|
-
return new Error("Invalid script: missing VERIFY operation");
|
|
650
|
-
}
|
|
651
|
-
return true;
|
|
490
|
+
return isConditionScriptValid(script);
|
|
652
491
|
}
|
|
653
492
|
ConditionCSVMultisigTapscript2.isScriptValid = isScriptValid;
|
|
654
493
|
})(ConditionCSVMultisigTapscript || (ConditionCSVMultisigTapscript = {}));
|
|
@@ -676,7 +515,7 @@ var ConditionMultisigTapscript;
|
|
|
676
515
|
throw isValid;
|
|
677
516
|
}
|
|
678
517
|
const asm = Script.decode(script);
|
|
679
|
-
|
|
518
|
+
const verifyIndex = asm.lastIndexOf("VERIFY");
|
|
680
519
|
if (verifyIndex === -1) {
|
|
681
520
|
throw Error("Invalid script: missing VERIFY operation");
|
|
682
521
|
}
|
|
@@ -711,26 +550,8 @@ var ConditionMultisigTapscript;
|
|
|
711
550
|
return tapscript.type === "condition-multisig" /* ConditionMultisig */;
|
|
712
551
|
}
|
|
713
552
|
ConditionMultisigTapscript2.is = is;
|
|
714
|
-
function getVerifyIndex(asm) {
|
|
715
|
-
let verifyIndex = -1;
|
|
716
|
-
for (let i = asm.length - 1; i >= 0; i--) {
|
|
717
|
-
if (asm[i] === "VERIFY") {
|
|
718
|
-
verifyIndex = i;
|
|
719
|
-
return verifyIndex;
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
return verifyIndex;
|
|
723
|
-
}
|
|
724
553
|
function isScriptValid(script) {
|
|
725
|
-
|
|
726
|
-
if (asm.length < 1) {
|
|
727
|
-
return new Error(`Invalid script: too short (expected at least 1)`);
|
|
728
|
-
}
|
|
729
|
-
let verifyIndex = getVerifyIndex(asm);
|
|
730
|
-
if (verifyIndex === -1) {
|
|
731
|
-
return new Error("Invalid script: missing VERIFY operation");
|
|
732
|
-
}
|
|
733
|
-
return true;
|
|
554
|
+
return isConditionScriptValid(script);
|
|
734
555
|
}
|
|
735
556
|
ConditionMultisigTapscript2.isScriptValid = isScriptValid;
|
|
736
557
|
})(ConditionMultisigTapscript || (ConditionMultisigTapscript = {}));
|
|
@@ -855,7 +676,25 @@ function assembleBtcdTaprootTree(scripts) {
|
|
|
855
676
|
}
|
|
856
677
|
return branches[0];
|
|
857
678
|
}
|
|
858
|
-
|
|
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
|
+
);
|
|
859
698
|
function scriptFromTapLeafScript(leaf) {
|
|
860
699
|
return leaf[1].subarray(0, leaf[1].length - 1);
|
|
861
700
|
}
|
|
@@ -875,7 +714,7 @@ var VtxoScript = class _VtxoScript {
|
|
|
875
714
|
constructor(scripts) {
|
|
876
715
|
this.scripts = scripts;
|
|
877
716
|
const tapTree = assembleBtcdTaprootTree(scripts);
|
|
878
|
-
const payment = p2tr(
|
|
717
|
+
const payment = p2tr(taprootNumsKey(), tapTree, void 0, true);
|
|
879
718
|
if (!payment.tapLeafScript || payment.tapLeafScript.length !== scripts.length) {
|
|
880
719
|
throw new Error("invalid scripts");
|
|
881
720
|
}
|
|
@@ -890,15 +729,46 @@ var VtxoScript = class _VtxoScript {
|
|
|
890
729
|
/**
|
|
891
730
|
* Decode a virtual output script from an encoded TapTree.
|
|
892
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
|
+
*
|
|
893
747
|
* @param tapTree - Encoded TapTree bytes
|
|
894
748
|
* @returns Decoded virtual output script
|
|
895
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}
|
|
896
751
|
* @see encode
|
|
897
752
|
*/
|
|
898
753
|
static decode(tapTree) {
|
|
899
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
|
+
}
|
|
900
759
|
const scripts = leaves.map((leaf) => leaf.script);
|
|
901
|
-
|
|
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);
|
|
902
772
|
}
|
|
903
773
|
/**
|
|
904
774
|
* Encode the virtual output script to a TapTree byte representation.
|
|
@@ -981,21 +851,323 @@ var VtxoScript = class _VtxoScript {
|
|
|
981
851
|
return paths;
|
|
982
852
|
}
|
|
983
853
|
};
|
|
984
|
-
function
|
|
985
|
-
|
|
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
|
+
var Transaction = class extends Transaction$1 {
|
|
880
|
+
static ARK_TX_OPTS = {
|
|
881
|
+
unknown: "ignore",
|
|
882
|
+
allowUnknownOutputs: true,
|
|
883
|
+
allowUnknownInputs: true
|
|
884
|
+
};
|
|
885
|
+
constructor(opts) {
|
|
886
|
+
super(withArkOpts(opts));
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Decode a PSBT. PSBTs carrying a legacy `PSBT_OUT_TAP_TREE` (see
|
|
890
|
+
* {@link repairLegacyOutputTapTrees}) are repaired and decoded again
|
|
891
|
+
* instead of being rejected.
|
|
892
|
+
*/
|
|
893
|
+
static fromPSBT(psbt_, opts) {
|
|
894
|
+
try {
|
|
895
|
+
return Transaction$1.fromPSBT(psbt_, withArkOpts(opts));
|
|
896
|
+
} catch (err) {
|
|
897
|
+
const repaired = repairLegacyOutputTapTrees(psbt_);
|
|
898
|
+
if (!repaired) throw err;
|
|
899
|
+
return Transaction$1.fromPSBT(repaired, withArkOpts(opts));
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
static fromRaw(raw, opts) {
|
|
903
|
+
return Transaction$1.fromRaw(raw, withArkOpts(opts));
|
|
904
|
+
}
|
|
905
|
+
};
|
|
906
|
+
function withArkOpts(opts) {
|
|
907
|
+
return { ...Transaction.ARK_TX_OPTS, ...opts };
|
|
908
|
+
}
|
|
909
|
+
var PSBT_MAGIC = new Uint8Array([112, 115, 98, 116, 255]);
|
|
910
|
+
var PSBT_GLOBAL_UNSIGNED_TX = 0;
|
|
911
|
+
var PSBT_GLOBAL_INPUT_COUNT = 4;
|
|
912
|
+
var PSBT_OUT_TAP_TREE = 6;
|
|
913
|
+
function repairLegacyOutputTapTrees(psbt) {
|
|
914
|
+
let maps;
|
|
915
|
+
let inputsCount;
|
|
916
|
+
try {
|
|
917
|
+
maps = readPsbtMaps(psbt);
|
|
918
|
+
inputsCount = psbtInputsCount(maps[0]);
|
|
919
|
+
} catch {
|
|
920
|
+
return void 0;
|
|
921
|
+
}
|
|
922
|
+
let repaired = false;
|
|
923
|
+
for (const output of maps.slice(1 + inputsCount)) {
|
|
924
|
+
for (const kv of output) {
|
|
925
|
+
if (kv.key.length !== 1 || kv.key[0] !== PSBT_OUT_TAP_TREE) continue;
|
|
926
|
+
const value = legacyTapTreeToBIP371(kv.value);
|
|
927
|
+
if (!value) continue;
|
|
928
|
+
kv.value = value;
|
|
929
|
+
repaired = true;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
return repaired ? writePsbtMaps(maps) : void 0;
|
|
933
|
+
}
|
|
934
|
+
function legacyTapTreeToBIP371(value) {
|
|
935
|
+
try {
|
|
936
|
+
PSBTOutput.tapTree[2].decode(value);
|
|
937
|
+
return void 0;
|
|
938
|
+
} catch {
|
|
939
|
+
}
|
|
940
|
+
let leaves;
|
|
941
|
+
try {
|
|
942
|
+
leaves = TapTreeCoder.decode(value);
|
|
943
|
+
} catch {
|
|
944
|
+
return void 0;
|
|
945
|
+
}
|
|
946
|
+
if (leaves.length === 0 || leaves.some((leaf) => leaf.depth !== 1)) return void 0;
|
|
947
|
+
try {
|
|
948
|
+
return TapTreeCoder.encode(toBIP371TapTree(value));
|
|
949
|
+
} catch {
|
|
950
|
+
return void 0;
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
function psbtInputsCount(global) {
|
|
954
|
+
for (const { key, value } of global) {
|
|
955
|
+
if (key.length !== 1) continue;
|
|
956
|
+
if (key[0] === PSBT_GLOBAL_UNSIGNED_TX) return RawTx.decode(value).inputs.length;
|
|
957
|
+
if (key[0] === PSBT_GLOBAL_INPUT_COUNT) return readCompactSize(value, 0)[0];
|
|
958
|
+
}
|
|
959
|
+
throw new Error("PSBT: missing input count");
|
|
960
|
+
}
|
|
961
|
+
function readPsbtMaps(psbt) {
|
|
962
|
+
for (let i = 0; i < PSBT_MAGIC.length; i++) {
|
|
963
|
+
if (psbt[i] !== PSBT_MAGIC[i]) throw new Error("PSBT: invalid magic");
|
|
964
|
+
}
|
|
965
|
+
const maps = [];
|
|
966
|
+
let pos = PSBT_MAGIC.length;
|
|
967
|
+
while (pos < psbt.length) {
|
|
968
|
+
const map = [];
|
|
969
|
+
for (; ; ) {
|
|
970
|
+
const [keyLen, afterKeyLen] = readCompactSize(psbt, pos);
|
|
971
|
+
pos = afterKeyLen;
|
|
972
|
+
if (keyLen === 0) break;
|
|
973
|
+
const key = readBytes(psbt, pos, keyLen);
|
|
974
|
+
pos += keyLen;
|
|
975
|
+
const [valueLen, afterValueLen] = readCompactSize(psbt, pos);
|
|
976
|
+
pos = afterValueLen;
|
|
977
|
+
const value = readBytes(psbt, pos, valueLen);
|
|
978
|
+
pos += valueLen;
|
|
979
|
+
map.push({ key, value });
|
|
980
|
+
}
|
|
981
|
+
maps.push(map);
|
|
982
|
+
}
|
|
983
|
+
if (maps.length === 0) throw new Error("PSBT: missing global map");
|
|
984
|
+
return maps;
|
|
985
|
+
}
|
|
986
|
+
function writePsbtMaps(maps) {
|
|
987
|
+
const parts = [PSBT_MAGIC];
|
|
988
|
+
for (const map of maps) {
|
|
989
|
+
for (const { key, value } of map) {
|
|
990
|
+
parts.push(writeCompactSize(key.length), key, writeCompactSize(value.length), value);
|
|
991
|
+
}
|
|
992
|
+
parts.push(new Uint8Array([0]));
|
|
993
|
+
}
|
|
994
|
+
return concatBytes(...parts);
|
|
995
|
+
}
|
|
996
|
+
function readBytes(data, pos, len) {
|
|
997
|
+
if (pos + len > data.length) throw new Error("PSBT: unexpected end of data");
|
|
998
|
+
return data.slice(pos, pos + len);
|
|
999
|
+
}
|
|
1000
|
+
function readCompactSize(data, pos) {
|
|
1001
|
+
const first = readBytes(data, pos, 1)[0];
|
|
1002
|
+
if (first < 253) return [first, pos + 1];
|
|
1003
|
+
const width = first === 253 ? 2 : first === 254 ? 4 : 8;
|
|
1004
|
+
const bytes = readBytes(data, pos + 1, width);
|
|
1005
|
+
let value = 0;
|
|
1006
|
+
for (let i = width - 1; i >= 0; i--) value = value * 256 + bytes[i];
|
|
1007
|
+
if (!Number.isSafeInteger(value)) throw new Error("PSBT: compact size too large");
|
|
1008
|
+
return [value, pos + 1 + width];
|
|
1009
|
+
}
|
|
1010
|
+
function writeCompactSize(value) {
|
|
1011
|
+
if (value < 253) return new Uint8Array([value]);
|
|
1012
|
+
const width = value <= 65535 ? 2 : value <= 4294967295 ? 4 : 8;
|
|
1013
|
+
const out = new Uint8Array(1 + width);
|
|
1014
|
+
out[0] = width === 2 ? 253 : width === 4 ? 254 : 255;
|
|
1015
|
+
for (let i = 0, v = value; i < width; i++, v = Math.floor(v / 256)) out[1 + i] = v % 256;
|
|
1016
|
+
return out;
|
|
1017
|
+
}
|
|
1018
|
+
function formatSighash(type) {
|
|
1019
|
+
return `0x${type.toString(16).padStart(2, "0")}`;
|
|
1020
|
+
}
|
|
1021
|
+
function assertAllowedSighashTypes(tx, allowedSighashTypes = [SigHash.DEFAULT]) {
|
|
1022
|
+
for (let i = 0; i < tx.inputsLength; i++) {
|
|
1023
|
+
const declared = tx.getInput(i).sighashType;
|
|
1024
|
+
if (declared === void 0) continue;
|
|
1025
|
+
if (!allowedSighashTypes.includes(declared)) {
|
|
1026
|
+
throw new Error(`Unallowed sighash type ${formatSighash(declared)} for input ${i}.`);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// src/utils/time.ts
|
|
1032
|
+
function assertNonNegativeMs(name, ms) {
|
|
1033
|
+
if (ms !== void 0 && (!Number.isSafeInteger(ms) || ms < 0)) {
|
|
1034
|
+
throw new Error(`${name} must be a non-negative safe integer`);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
var MAX_TIMER_MS = 2 ** 31 - 1;
|
|
1038
|
+
function assertTimerMs(name, ms) {
|
|
1039
|
+
assertNonNegativeMs(name, ms);
|
|
1040
|
+
if (ms !== void 0 && ms > MAX_TIMER_MS) {
|
|
1041
|
+
throw new Error(`${name} must not exceed ${MAX_TIMER_MS} milliseconds`);
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
var ArkPsbtFieldKey = /* @__PURE__ */ ((ArkPsbtFieldKey2) => {
|
|
1045
|
+
ArkPsbtFieldKey2["VtxoTaprootTree"] = "taptree";
|
|
1046
|
+
ArkPsbtFieldKey2["VtxoTreeExpiry"] = "expiry";
|
|
1047
|
+
ArkPsbtFieldKey2["Cosigner"] = "cosigner";
|
|
1048
|
+
ArkPsbtFieldKey2["ConditionWitness"] = "condition";
|
|
1049
|
+
ArkPsbtFieldKey2["PrevArkTx"] = "prevarktx";
|
|
1050
|
+
ArkPsbtFieldKey2["PrevoutTx"] = "prevouttx";
|
|
1051
|
+
return ArkPsbtFieldKey2;
|
|
1052
|
+
})(ArkPsbtFieldKey || {});
|
|
1053
|
+
var ArkPsbtFieldKeyType = 222;
|
|
1054
|
+
function setArkPsbtField(tx, inputIndex, coder, value) {
|
|
1055
|
+
tx.updateInput(inputIndex, {
|
|
1056
|
+
unknown: [...tx.getInput(inputIndex)?.unknown ?? [], coder.encode(value)]
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
function getArkPsbtFields(tx, inputIndex, coder) {
|
|
1060
|
+
const unknown = tx.getInput(inputIndex)?.unknown ?? [];
|
|
1061
|
+
return unknown.map((u) => coder.decode(u)).filter((v) => v !== null);
|
|
1062
|
+
}
|
|
1063
|
+
var VtxoTaprootTree = {
|
|
1064
|
+
key: "taptree" /* VtxoTaprootTree */,
|
|
1065
|
+
encode: (value) => [
|
|
1066
|
+
{
|
|
1067
|
+
type: ArkPsbtFieldKeyType,
|
|
1068
|
+
key: encodedPsbtFieldKey["taptree" /* VtxoTaprootTree */]
|
|
1069
|
+
},
|
|
1070
|
+
value
|
|
1071
|
+
],
|
|
1072
|
+
decode: (value) => nullIfCatch(() => {
|
|
1073
|
+
if (!checkKeyMatch(value[0], "taptree" /* VtxoTaprootTree */)) return null;
|
|
1074
|
+
return value[1];
|
|
1075
|
+
})
|
|
1076
|
+
};
|
|
1077
|
+
var ConditionWitness = {
|
|
1078
|
+
key: "condition" /* ConditionWitness */,
|
|
1079
|
+
encode: (value) => [
|
|
1080
|
+
{
|
|
1081
|
+
type: ArkPsbtFieldKeyType,
|
|
1082
|
+
key: encodedPsbtFieldKey["condition" /* ConditionWitness */]
|
|
1083
|
+
},
|
|
1084
|
+
RawWitness.encode(value)
|
|
1085
|
+
],
|
|
1086
|
+
decode: (value) => nullIfCatch(() => {
|
|
1087
|
+
if (!checkKeyMatch(value[0], "condition" /* ConditionWitness */)) return null;
|
|
1088
|
+
return RawWitness.decode(value[1]);
|
|
1089
|
+
})
|
|
1090
|
+
};
|
|
1091
|
+
var PrevArkTxField = {
|
|
1092
|
+
key: "prevarktx" /* PrevArkTx */,
|
|
1093
|
+
encode: (value) => [
|
|
1094
|
+
{
|
|
1095
|
+
type: ArkPsbtFieldKeyType,
|
|
1096
|
+
key: encodedPsbtFieldKey["prevarktx" /* PrevArkTx */]
|
|
1097
|
+
},
|
|
1098
|
+
value
|
|
1099
|
+
],
|
|
1100
|
+
decode: (value) => nullIfCatch(() => {
|
|
1101
|
+
if (!checkKeyMatch(value[0], "prevarktx" /* PrevArkTx */)) return null;
|
|
1102
|
+
return value[1];
|
|
1103
|
+
})
|
|
1104
|
+
};
|
|
1105
|
+
var PrevoutTxField = {
|
|
1106
|
+
key: "prevouttx" /* PrevoutTx */,
|
|
1107
|
+
encode: (value) => [
|
|
1108
|
+
{
|
|
1109
|
+
type: ArkPsbtFieldKeyType,
|
|
1110
|
+
key: encodedPsbtFieldKey["prevouttx" /* PrevoutTx */]
|
|
1111
|
+
},
|
|
1112
|
+
value
|
|
1113
|
+
],
|
|
1114
|
+
decode: (value) => nullIfCatch(() => {
|
|
1115
|
+
if (!checkKeyMatch(value[0], "prevouttx" /* PrevoutTx */)) return null;
|
|
1116
|
+
return value[1];
|
|
1117
|
+
})
|
|
1118
|
+
};
|
|
1119
|
+
var CosignerPublicKey = {
|
|
1120
|
+
key: "cosigner" /* Cosigner */,
|
|
1121
|
+
encode: (value) => [
|
|
1122
|
+
{
|
|
1123
|
+
type: ArkPsbtFieldKeyType,
|
|
1124
|
+
key: new Uint8Array([...encodedPsbtFieldKey["cosigner" /* Cosigner */], value.index])
|
|
1125
|
+
},
|
|
1126
|
+
value.key
|
|
1127
|
+
],
|
|
1128
|
+
decode: (unknown) => nullIfCatch(() => {
|
|
1129
|
+
if (!checkKeyMatch(unknown[0], "cosigner" /* Cosigner */, true)) return null;
|
|
1130
|
+
return {
|
|
1131
|
+
index: unknown[0].key[unknown[0].key.length - 1],
|
|
1132
|
+
key: unknown[1]
|
|
1133
|
+
};
|
|
1134
|
+
})
|
|
1135
|
+
};
|
|
1136
|
+
var VtxoTreeExpiry = {
|
|
1137
|
+
key: "expiry" /* VtxoTreeExpiry */,
|
|
1138
|
+
encode: (value) => [
|
|
1139
|
+
{
|
|
1140
|
+
type: ArkPsbtFieldKeyType,
|
|
1141
|
+
key: encodedPsbtFieldKey["expiry" /* VtxoTreeExpiry */]
|
|
1142
|
+
},
|
|
1143
|
+
ScriptNum(6, true).encode(value.value)
|
|
1144
|
+
],
|
|
1145
|
+
decode: (unknown) => nullIfCatch(() => {
|
|
1146
|
+
if (!checkKeyMatch(unknown[0], "expiry" /* VtxoTreeExpiry */)) return null;
|
|
1147
|
+
const v = ScriptNum(6, true).decode(unknown[1]);
|
|
1148
|
+
if (!v) return null;
|
|
1149
|
+
return sequenceToTimelock(Number(v));
|
|
1150
|
+
})
|
|
1151
|
+
};
|
|
1152
|
+
var encodedPsbtFieldKey = Object.fromEntries(
|
|
1153
|
+
Object.values(ArkPsbtFieldKey).map((key) => [key, new TextEncoder().encode(key)])
|
|
1154
|
+
);
|
|
1155
|
+
var nullIfCatch = (fn) => {
|
|
986
1156
|
try {
|
|
987
|
-
|
|
988
|
-
const script = scriptWithLeafVersion.subarray(0, scriptWithLeafVersion.length - 1);
|
|
989
|
-
try {
|
|
990
|
-
const params = CSVMultisigTapscript.decode(script).params;
|
|
991
|
-
sequence = timelockToSequence(params.timelock);
|
|
992
|
-
} catch {
|
|
993
|
-
const params = CLTVMultisigTapscript.decode(script).params;
|
|
994
|
-
sequence = Number(params.absoluteTimelock);
|
|
995
|
-
}
|
|
1157
|
+
return fn();
|
|
996
1158
|
} catch {
|
|
1159
|
+
return null;
|
|
997
1160
|
}
|
|
998
|
-
|
|
1161
|
+
};
|
|
1162
|
+
function checkKeyMatch(key, arkPsbtFieldKey, prefixOnly = false) {
|
|
1163
|
+
if (key.type !== ArkPsbtFieldKeyType) return false;
|
|
1164
|
+
const expected = encodedPsbtFieldKey[arkPsbtFieldKey];
|
|
1165
|
+
if (key.key.length < expected.length) return false;
|
|
1166
|
+
if (!prefixOnly && key.key.length !== expected.length) return false;
|
|
1167
|
+
for (let i = 0; i < expected.length; i++) {
|
|
1168
|
+
if (key.key[i] !== expected[i]) return false;
|
|
1169
|
+
}
|
|
1170
|
+
return true;
|
|
999
1171
|
}
|
|
1000
1172
|
|
|
1001
1173
|
// src/providers/eventSource.ts
|
|
@@ -1023,11 +1195,15 @@ function resolveEventSource(override) {
|
|
|
1023
1195
|
}
|
|
1024
1196
|
|
|
1025
1197
|
// package.json
|
|
1026
|
-
var version = "0.5.0-rc.
|
|
1198
|
+
var version = "0.5.0-rc.30";
|
|
1027
1199
|
|
|
1028
1200
|
// src/utils/fetch.ts
|
|
1029
1201
|
var buildVersion = "0.9.9";
|
|
1030
1202
|
var sdkVersion = `ts-sdk/${version}`;
|
|
1203
|
+
var ARKADE_VERSION_HEADERS = {
|
|
1204
|
+
"X-Build-Version": buildVersion,
|
|
1205
|
+
"X-SDK-VERSION": sdkVersion
|
|
1206
|
+
};
|
|
1031
1207
|
var FetchError = class extends Error {
|
|
1032
1208
|
/** The request URL that failed, when derivable from the `fetch` input. */
|
|
1033
1209
|
url;
|
|
@@ -1040,38 +1216,48 @@ var FetchError = class extends Error {
|
|
|
1040
1216
|
this.method = options.method;
|
|
1041
1217
|
}
|
|
1042
1218
|
};
|
|
1219
|
+
var READ_TIMEOUT_MS = 3e4;
|
|
1220
|
+
var warnedNoTimeoutSupport = false;
|
|
1221
|
+
function readDeadline(input, init) {
|
|
1222
|
+
if (init?.signal || input instanceof Request) return void 0;
|
|
1223
|
+
const { method } = describeRequest(input, init);
|
|
1224
|
+
const verb = method.toUpperCase();
|
|
1225
|
+
if (verb !== "GET" && verb !== "HEAD") return void 0;
|
|
1226
|
+
if (typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function") {
|
|
1227
|
+
return AbortSignal.timeout(READ_TIMEOUT_MS);
|
|
1228
|
+
}
|
|
1229
|
+
if (typeof AbortController === "function") {
|
|
1230
|
+
const controller = new AbortController();
|
|
1231
|
+
const timer = setTimeout(() => controller.abort(), READ_TIMEOUT_MS);
|
|
1232
|
+
timer?.unref?.();
|
|
1233
|
+
return controller.signal;
|
|
1234
|
+
}
|
|
1235
|
+
if (!warnedNoTimeoutSupport) {
|
|
1236
|
+
warnedNoTimeoutSupport = true;
|
|
1237
|
+
console.warn(
|
|
1238
|
+
"Neither AbortSignal.timeout nor AbortController is available in this runtime: provider reads are UNBOUNDED and READ_TIMEOUT_MS is not being applied."
|
|
1239
|
+
);
|
|
1240
|
+
}
|
|
1241
|
+
return void 0;
|
|
1242
|
+
}
|
|
1043
1243
|
function baseFetch(input, init) {
|
|
1044
1244
|
if (typeof globalThis.fetch !== "function") {
|
|
1045
1245
|
throw new Error("Fetch API is not available in this environment.");
|
|
1046
1246
|
}
|
|
1047
|
-
|
|
1247
|
+
const signal = readDeadline(input, init);
|
|
1248
|
+
return globalThis.fetch(input, signal ? { ...init, signal } : init).catch((cause) => {
|
|
1048
1249
|
const { url, method } = describeRequest(input, init);
|
|
1049
1250
|
throw new FetchError(`Network request failed: ${method} ${url}`, { url, method, cause });
|
|
1050
1251
|
});
|
|
1051
1252
|
}
|
|
1052
1253
|
function fetch(input, init) {
|
|
1053
1254
|
const headers = new Headers(init?.headers);
|
|
1054
|
-
headers.set(
|
|
1055
|
-
headers.set("X-SDK-VERSION", sdkVersion);
|
|
1255
|
+
for (const [name, value] of Object.entries(ARKADE_VERSION_HEADERS)) headers.set(name, value);
|
|
1056
1256
|
return baseFetch(input, { ...init, headers });
|
|
1057
1257
|
}
|
|
1058
1258
|
function describeRequest(input, init) {
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
url = input;
|
|
1062
|
-
} else if (input instanceof URL) {
|
|
1063
|
-
url = input.href;
|
|
1064
|
-
} else {
|
|
1065
|
-
url = input.url;
|
|
1066
|
-
}
|
|
1067
|
-
let method;
|
|
1068
|
-
if (init?.method !== void 0) {
|
|
1069
|
-
method = init.method;
|
|
1070
|
-
} else if (input instanceof Request) {
|
|
1071
|
-
method = input.method;
|
|
1072
|
-
} else {
|
|
1073
|
-
method = "GET";
|
|
1074
|
-
}
|
|
1259
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
1260
|
+
const method = init?.method !== void 0 ? init.method : input instanceof Request ? input.method : "GET";
|
|
1075
1261
|
return { url, method };
|
|
1076
1262
|
}
|
|
1077
1263
|
|
|
@@ -1095,15 +1281,13 @@ var ArkErrorName = {
|
|
|
1095
1281
|
INVALID_TX_FILTER: "INVALID_TX_FILTER",
|
|
1096
1282
|
TX_FILTERS_LIMIT_EXCEEDED: "TX_FILTERS_LIMIT_EXCEEDED",
|
|
1097
1283
|
/**
|
|
1098
|
-
* A CLTV closure was spent before its absolute locktime matured.
|
|
1099
|
-
* `
|
|
1100
|
-
*
|
|
1101
|
-
*
|
|
1102
|
-
* Self-healing, so defer and retry rather than fail: the server matures a
|
|
1103
|
-
* seconds-locktime against the **chain tip block's timestamp**, not its wall clock,
|
|
1104
|
-
* so a spend attempted promptly at maturity is rejected until a later block lands.
|
|
1284
|
+
* A CLTV closure was spent before its absolute locktime matured. `submitTx` only, with
|
|
1285
|
+
* metadata `{ locktime, current_locktime, type: "height" | "time" }`. Self-healing, so defer
|
|
1286
|
+
* and retry: the server matures a seconds-locktime against the **chain tip block's
|
|
1287
|
+
* timestamp**, not wall clock, so a prompt spend is rejected until a later block lands.
|
|
1105
1288
|
*/
|
|
1106
|
-
FORFEIT_CLOSURE_LOCKED: "FORFEIT_CLOSURE_LOCKED"
|
|
1289
|
+
FORFEIT_CLOSURE_LOCKED: "FORFEIT_CLOSURE_LOCKED",
|
|
1290
|
+
INVALID_INTENT_PROOF: "INVALID_INTENT_PROOF"
|
|
1107
1291
|
};
|
|
1108
1292
|
function isArkError(error, name) {
|
|
1109
1293
|
return error instanceof ArkError && (name === void 0 || error.name === name);
|
|
@@ -1179,8 +1363,8 @@ var Intent;
|
|
|
1179
1363
|
}
|
|
1180
1364
|
if (ins.length == 0) throw new Error("intent proof requires at least one input");
|
|
1181
1365
|
const inputs = ins.map(prepareCoinAsIntentProofInput);
|
|
1182
|
-
|
|
1183
|
-
|
|
1366
|
+
validateInputs(inputs);
|
|
1367
|
+
validateOutputs(outputs);
|
|
1184
1368
|
const toSpend = craftToSpendTx(message, inputs[0].witnessUtxo.script);
|
|
1185
1369
|
return craftToSignTx(toSpend, inputs, outputs, message);
|
|
1186
1370
|
}
|
|
@@ -1235,7 +1419,6 @@ var OP_RETURN_EMPTY_PKSCRIPT = new Uint8Array([OP.RETURN]);
|
|
|
1235
1419
|
var ZERO_32 = new Uint8Array(32).fill(0);
|
|
1236
1420
|
var MAX_INDEX = 4294967295;
|
|
1237
1421
|
var TAG_INTENT_PROOF = "ark-intent-proof-message";
|
|
1238
|
-
var PSBT_GLOBAL_GENERIC_SIGNED_MESSAGE = 9;
|
|
1239
1422
|
function validateInput(input) {
|
|
1240
1423
|
if (input.index === void 0) throw new Error("intent proof input requires index");
|
|
1241
1424
|
if (input.txid === void 0) throw new Error("intent proof input requires txid");
|
|
@@ -1245,7 +1428,6 @@ function validateInput(input) {
|
|
|
1245
1428
|
}
|
|
1246
1429
|
function validateInputs(inputs) {
|
|
1247
1430
|
inputs.forEach(validateInput);
|
|
1248
|
-
return true;
|
|
1249
1431
|
}
|
|
1250
1432
|
function validateOutput(output) {
|
|
1251
1433
|
if (output.amount === void 0) throw new Error("intent proof output requires amount");
|
|
@@ -1254,7 +1436,6 @@ function validateOutput(output) {
|
|
|
1254
1436
|
}
|
|
1255
1437
|
function validateOutputs(outputs) {
|
|
1256
1438
|
outputs.forEach(validateOutput);
|
|
1257
|
-
return true;
|
|
1258
1439
|
}
|
|
1259
1440
|
function craftToSpendTx(message, pkScript, tag = TAG_INTENT_PROOF) {
|
|
1260
1441
|
const messageHash = hashMessage(message, tag);
|
|
@@ -1282,8 +1463,9 @@ function craftToSignTx(toSpend, inputs, outputs, message) {
|
|
|
1282
1463
|
version: 2,
|
|
1283
1464
|
lockTime: 0
|
|
1284
1465
|
});
|
|
1466
|
+
const { unknown: _, ...toSpendInput } = firstInput;
|
|
1285
1467
|
tx.addInput({
|
|
1286
|
-
...
|
|
1468
|
+
...toSpendInput,
|
|
1287
1469
|
txid: toSpend.id,
|
|
1288
1470
|
index: 0,
|
|
1289
1471
|
witnessUtxo: {
|
|
@@ -1317,14 +1499,7 @@ function craftToSignTx(toSpend, inputs, outputs, message) {
|
|
|
1317
1499
|
script: output.script
|
|
1318
1500
|
});
|
|
1319
1501
|
}
|
|
1320
|
-
|
|
1321
|
-
global.unknown = [
|
|
1322
|
-
...global.unknown ?? [],
|
|
1323
|
-
[
|
|
1324
|
-
{ type: PSBT_GLOBAL_GENERIC_SIGNED_MESSAGE, key: new Uint8Array() },
|
|
1325
|
-
new TextEncoder().encode(message)
|
|
1326
|
-
]
|
|
1327
|
-
];
|
|
1502
|
+
tx.global.genericSignedMessage = new TextEncoder().encode(message);
|
|
1328
1503
|
return tx;
|
|
1329
1504
|
}
|
|
1330
1505
|
function hashMessage(message, tag = TAG_INTENT_PROOF) {
|
|
@@ -1374,6 +1549,7 @@ function eventSourceIterator(eventSource) {
|
|
|
1374
1549
|
cleanedUp = true;
|
|
1375
1550
|
eventSource.removeEventListener("message", messageHandler);
|
|
1376
1551
|
eventSource.removeEventListener("error", errorHandler);
|
|
1552
|
+
eventSource.removeEventListener("failure", failureHandler);
|
|
1377
1553
|
};
|
|
1378
1554
|
const close = () => {
|
|
1379
1555
|
if (closed) return;
|
|
@@ -1400,10 +1576,8 @@ function eventSourceIterator(eventSource) {
|
|
|
1400
1576
|
messageQueue.push(event);
|
|
1401
1577
|
}
|
|
1402
1578
|
};
|
|
1403
|
-
const
|
|
1579
|
+
const rejectWith = (error) => {
|
|
1404
1580
|
if (closed) return;
|
|
1405
|
-
const error = new Error("EventSource error");
|
|
1406
|
-
error.name = "EventSourceError";
|
|
1407
1581
|
if (errorResolve) {
|
|
1408
1582
|
const reject = errorResolve;
|
|
1409
1583
|
messageResolve = null;
|
|
@@ -1413,8 +1587,11 @@ function eventSourceIterator(eventSource) {
|
|
|
1413
1587
|
errorQueue.push(error);
|
|
1414
1588
|
}
|
|
1415
1589
|
};
|
|
1590
|
+
const errorHandler = () => rejectWith(eventSourceError("EventSource error"));
|
|
1591
|
+
const failureHandler = (event) => rejectWith(streamFailureError(event.data));
|
|
1416
1592
|
eventSource.addEventListener("message", messageHandler);
|
|
1417
1593
|
eventSource.addEventListener("error", errorHandler);
|
|
1594
|
+
eventSource.addEventListener("failure", failureHandler);
|
|
1418
1595
|
const gen = (async function* () {
|
|
1419
1596
|
try {
|
|
1420
1597
|
while (!closed) {
|
|
@@ -1452,6 +1629,20 @@ function eventSourceIterator(eventSource) {
|
|
|
1452
1629
|
};
|
|
1453
1630
|
return managed;
|
|
1454
1631
|
}
|
|
1632
|
+
function eventSourceError(message) {
|
|
1633
|
+
const error = new Error(message);
|
|
1634
|
+
error.name = "EventSourceError";
|
|
1635
|
+
return error;
|
|
1636
|
+
}
|
|
1637
|
+
function streamFailureError(data) {
|
|
1638
|
+
let message = String(data);
|
|
1639
|
+
try {
|
|
1640
|
+
const parsed = typeof data === "string" ? JSON.parse(data) : data;
|
|
1641
|
+
if (typeof parsed?.message === "string") message = parsed.message;
|
|
1642
|
+
} catch {
|
|
1643
|
+
}
|
|
1644
|
+
return eventSourceError(`EventSource failure: ${message}`);
|
|
1645
|
+
}
|
|
1455
1646
|
function isEventSourceError(error) {
|
|
1456
1647
|
return error instanceof Error && error.name === "EventSourceError";
|
|
1457
1648
|
}
|
|
@@ -1461,7 +1652,7 @@ var DEFAULT_MAX_CONCURRENT = 6;
|
|
|
1461
1652
|
var DEFAULT_COOLDOWN_MS = 5e3;
|
|
1462
1653
|
var MAX_COOLDOWN_MS = 6e4;
|
|
1463
1654
|
var DEFAULT_JITTER_MS = 500;
|
|
1464
|
-
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
1655
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, Math.min(ms, MAX_TIMER_MS)));
|
|
1465
1656
|
function parseRetryAfterMs(header) {
|
|
1466
1657
|
if (header === null || header === void 0) return void 0;
|
|
1467
1658
|
const trimmed = header.trim();
|
|
@@ -1494,6 +1685,9 @@ var OriginRateGate = class {
|
|
|
1494
1685
|
maxCooldownMs;
|
|
1495
1686
|
jitterMs;
|
|
1496
1687
|
constructor(options) {
|
|
1688
|
+
assertNonNegativeMs("defaultCooldownMs", options?.defaultCooldownMs);
|
|
1689
|
+
assertNonNegativeMs("maxCooldownMs", options?.maxCooldownMs);
|
|
1690
|
+
assertNonNegativeMs("jitterMs", options?.jitterMs);
|
|
1497
1691
|
this.maxConcurrent = options?.maxConcurrent ?? DEFAULT_MAX_CONCURRENT;
|
|
1498
1692
|
this.defaultCooldownMs = options?.defaultCooldownMs ?? DEFAULT_COOLDOWN_MS;
|
|
1499
1693
|
this.maxCooldownMs = options?.maxCooldownMs ?? MAX_COOLDOWN_MS;
|
|
@@ -1699,6 +1893,20 @@ var RestArkProvider = class {
|
|
|
1699
1893
|
"Arkade server reported a configuration digest mismatch; server info was refreshed. Rebuild and retry the request under the new server info."
|
|
1700
1894
|
);
|
|
1701
1895
|
}
|
|
1896
|
+
async postJson(url, body, failureMessage) {
|
|
1897
|
+
const response = await this.authedFetch(url, {
|
|
1898
|
+
method: "POST",
|
|
1899
|
+
headers: {
|
|
1900
|
+
"Content-Type": "application/json"
|
|
1901
|
+
},
|
|
1902
|
+
body: JSON.stringify(body)
|
|
1903
|
+
});
|
|
1904
|
+
if (!response.ok) {
|
|
1905
|
+
const errorText = await response.text();
|
|
1906
|
+
handleError(errorText, failureMessage(errorText, response));
|
|
1907
|
+
}
|
|
1908
|
+
return response;
|
|
1909
|
+
}
|
|
1702
1910
|
async getInfo() {
|
|
1703
1911
|
const url = `${this.serverUrl}/v1/info`;
|
|
1704
1912
|
const response = await rateGate.runHttp(
|
|
@@ -1764,20 +1972,14 @@ var RestArkProvider = class {
|
|
|
1764
1972
|
}
|
|
1765
1973
|
async submitTx(signedArkTx, checkpointTxs) {
|
|
1766
1974
|
const url = `${this.serverUrl}/v1/tx/submit`;
|
|
1767
|
-
const response = await this.
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
"Content-Type": "application/json"
|
|
1771
|
-
},
|
|
1772
|
-
body: JSON.stringify({
|
|
1975
|
+
const response = await this.postJson(
|
|
1976
|
+
url,
|
|
1977
|
+
{
|
|
1773
1978
|
signedArkTx,
|
|
1774
1979
|
checkpointTxs
|
|
1775
|
-
}
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
const errorText = await response.text();
|
|
1779
|
-
handleError(errorText, `Failed to submit virtual transaction: ${errorText}`);
|
|
1780
|
-
}
|
|
1980
|
+
},
|
|
1981
|
+
(errorText) => `Failed to submit virtual transaction: ${errorText}`
|
|
1982
|
+
);
|
|
1781
1983
|
const data = await response.json();
|
|
1782
1984
|
return {
|
|
1783
1985
|
arkTxid: data.arkTxid,
|
|
@@ -1787,129 +1989,87 @@ var RestArkProvider = class {
|
|
|
1787
1989
|
}
|
|
1788
1990
|
async finalizeTx(arkTxid, finalCheckpointTxs) {
|
|
1789
1991
|
const url = `${this.serverUrl}/v1/tx/finalize`;
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
"Content-Type": "application/json"
|
|
1794
|
-
},
|
|
1795
|
-
body: JSON.stringify({
|
|
1992
|
+
await this.postJson(
|
|
1993
|
+
url,
|
|
1994
|
+
{
|
|
1796
1995
|
arkTxid,
|
|
1797
1996
|
finalCheckpointTxs
|
|
1798
|
-
}
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
const errorText = await response.text();
|
|
1802
|
-
handleError(errorText, `Failed to finalize offchain transaction: ${errorText}`);
|
|
1803
|
-
}
|
|
1997
|
+
},
|
|
1998
|
+
(errorText) => `Failed to finalize offchain transaction: ${errorText}`
|
|
1999
|
+
);
|
|
1804
2000
|
}
|
|
1805
2001
|
async registerIntent(intent) {
|
|
1806
2002
|
const url = `${this.serverUrl}/v1/batch/registerIntent`;
|
|
1807
|
-
const response = await this.
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
"Content-Type": "application/json"
|
|
1811
|
-
},
|
|
1812
|
-
body: JSON.stringify({
|
|
2003
|
+
const response = await this.postJson(
|
|
2004
|
+
url,
|
|
2005
|
+
{
|
|
1813
2006
|
intent: {
|
|
1814
2007
|
proof: intent.proof,
|
|
1815
2008
|
message: Intent.encodeMessage(intent.message)
|
|
1816
2009
|
}
|
|
1817
|
-
}
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
const errorText = await response.text();
|
|
1821
|
-
handleError(errorText, `Failed to register intent: ${errorText}`);
|
|
1822
|
-
}
|
|
2010
|
+
},
|
|
2011
|
+
(errorText) => `Failed to register intent: ${errorText}`
|
|
2012
|
+
);
|
|
1823
2013
|
const data = await response.json();
|
|
1824
2014
|
return data.intentId;
|
|
1825
2015
|
}
|
|
1826
2016
|
async deleteIntent(intent) {
|
|
1827
2017
|
const url = `${this.serverUrl}/v1/batch/deleteIntent`;
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
"Content-Type": "application/json"
|
|
1832
|
-
},
|
|
1833
|
-
body: JSON.stringify({
|
|
2018
|
+
await this.postJson(
|
|
2019
|
+
url,
|
|
2020
|
+
{
|
|
1834
2021
|
intent: {
|
|
1835
2022
|
proof: intent.proof,
|
|
1836
2023
|
message: Intent.encodeMessage(intent.message)
|
|
1837
2024
|
}
|
|
1838
|
-
}
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
const errorText = await response.text();
|
|
1842
|
-
handleError(errorText, `Failed to delete intent: ${errorText}`);
|
|
1843
|
-
}
|
|
2025
|
+
},
|
|
2026
|
+
(errorText) => `Failed to delete intent: ${errorText}`
|
|
2027
|
+
);
|
|
1844
2028
|
}
|
|
1845
2029
|
async confirmRegistration(intentId) {
|
|
1846
2030
|
const url = `${this.serverUrl}/v1/batch/ack`;
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
"Content-Type": "application/json"
|
|
1851
|
-
},
|
|
1852
|
-
body: JSON.stringify({
|
|
2031
|
+
await this.postJson(
|
|
2032
|
+
url,
|
|
2033
|
+
{
|
|
1853
2034
|
intentId
|
|
1854
|
-
}
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
const errorText = await response.text();
|
|
1858
|
-
handleError(errorText, `Failed to confirm registration: ${errorText}`);
|
|
1859
|
-
}
|
|
2035
|
+
},
|
|
2036
|
+
(errorText) => `Failed to confirm registration: ${errorText}`
|
|
2037
|
+
);
|
|
1860
2038
|
}
|
|
1861
2039
|
async submitTreeNonces(batchId, pubkey, nonces) {
|
|
1862
2040
|
const url = `${this.serverUrl}/v1/batch/tree/submitNonces`;
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
"Content-Type": "application/json"
|
|
1867
|
-
},
|
|
1868
|
-
body: JSON.stringify({
|
|
2041
|
+
await this.postJson(
|
|
2042
|
+
url,
|
|
2043
|
+
{
|
|
1869
2044
|
batchId,
|
|
1870
2045
|
pubkey,
|
|
1871
2046
|
treeNonces: encodeMusig2Nonces(nonces)
|
|
1872
|
-
}
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
const errorText = await response.text();
|
|
1876
|
-
handleError(errorText, `Failed to submit tree nonces: ${errorText}`);
|
|
1877
|
-
}
|
|
2047
|
+
},
|
|
2048
|
+
(errorText) => `Failed to submit tree nonces: ${errorText}`
|
|
2049
|
+
);
|
|
1878
2050
|
}
|
|
1879
2051
|
async submitTreeSignatures(batchId, pubkey, signatures) {
|
|
1880
2052
|
const url = `${this.serverUrl}/v1/batch/tree/submitSignatures`;
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
"Content-Type": "application/json"
|
|
1885
|
-
},
|
|
1886
|
-
body: JSON.stringify({
|
|
2053
|
+
await this.postJson(
|
|
2054
|
+
url,
|
|
2055
|
+
{
|
|
1887
2056
|
batchId,
|
|
1888
2057
|
pubkey,
|
|
1889
2058
|
treeSignatures: encodeMusig2Signatures(signatures)
|
|
1890
|
-
}
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
const errorText = await response.text();
|
|
1894
|
-
handleError(errorText, `Failed to submit tree signatures: ${errorText}`);
|
|
1895
|
-
}
|
|
2059
|
+
},
|
|
2060
|
+
(errorText) => `Failed to submit tree signatures: ${errorText}`
|
|
2061
|
+
);
|
|
1896
2062
|
}
|
|
1897
2063
|
async submitSignedForfeitTxs(signedForfeitTxs, signedCommitmentTx) {
|
|
1898
2064
|
const url = `${this.serverUrl}/v1/batch/submitForfeitTxs`;
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
"Content-Type": "application/json"
|
|
1903
|
-
},
|
|
1904
|
-
body: JSON.stringify({
|
|
2065
|
+
await this.postJson(
|
|
2066
|
+
url,
|
|
2067
|
+
{
|
|
1905
2068
|
signedForfeitTxs,
|
|
1906
2069
|
signedCommitmentTx
|
|
1907
|
-
}
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
const errorText = await response.text();
|
|
1911
|
-
handleError(errorText, `Failed to submit forfeit transactions: ${response.statusText}`);
|
|
1912
|
-
}
|
|
2070
|
+
},
|
|
2071
|
+
(_, res) => `Failed to submit forfeit transactions: ${res.statusText}`
|
|
2072
|
+
);
|
|
1913
2073
|
}
|
|
1914
2074
|
getEventStream(signal, topics) {
|
|
1915
2075
|
const url = `${this.serverUrl}/v1/batch/events`;
|
|
@@ -2032,22 +2192,16 @@ var RestArkProvider = class {
|
|
|
2032
2192
|
}
|
|
2033
2193
|
async getPendingTxs(intent) {
|
|
2034
2194
|
const url = `${this.serverUrl}/v1/tx/pending`;
|
|
2035
|
-
const response = await this.
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
"Content-Type": "application/json"
|
|
2039
|
-
},
|
|
2040
|
-
body: JSON.stringify({
|
|
2195
|
+
const response = await this.postJson(
|
|
2196
|
+
url,
|
|
2197
|
+
{
|
|
2041
2198
|
intent: {
|
|
2042
2199
|
proof: intent.proof,
|
|
2043
2200
|
message: Intent.encodeMessage(intent.message)
|
|
2044
2201
|
}
|
|
2045
|
-
}
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
const errorText = await response.text();
|
|
2049
|
-
handleError(errorText, `Failed to get pending transactions: ${errorText}`);
|
|
2050
|
-
}
|
|
2202
|
+
},
|
|
2203
|
+
(errorText) => `Failed to get pending transactions: ${errorText}`
|
|
2204
|
+
);
|
|
2051
2205
|
const data = await response.json();
|
|
2052
2206
|
return data.pendingTxs;
|
|
2053
2207
|
}
|
|
@@ -2267,60 +2421,23 @@ function parseWireExpiry(raw) {
|
|
|
2267
2421
|
if (ms >= EXPIRY_MIN_PLAUSIBLE_MS) return { expiresAt: new Date(ms) };
|
|
2268
2422
|
return { expiresAtHeight: n };
|
|
2269
2423
|
}
|
|
2270
|
-
function toBatchExpiry(c) {
|
|
2271
|
-
if (c.expiresAt !== void 0) return c.expiresAt.getTime();
|
|
2272
|
-
if (c.expiresAtHeight !== void 0) return c.expiresAtHeight * 1e3;
|
|
2273
|
-
return void 0;
|
|
2274
|
-
}
|
|
2275
|
-
function parseLegacyExpiry(batchExpiry) {
|
|
2276
|
-
if (batchExpiry === void 0 || batchExpiry <= 0) return {};
|
|
2277
|
-
if (batchExpiry >= EXPIRY_MIN_PLAUSIBLE_MS) return { expiresAt: new Date(batchExpiry) };
|
|
2278
|
-
return { expiresAtHeight: batchExpiry / 1e3 };
|
|
2279
|
-
}
|
|
2280
|
-
function toVirtualStatus(c) {
|
|
2281
|
-
return {
|
|
2282
|
-
// Precedence is load-bearing: consumers bucket on this label, so any other order silently
|
|
2283
|
-
// moves VTXOs between buckets.
|
|
2284
|
-
state: c.isSpent ? "spent" : c.isSwept ? "swept" : c.isPreconfirmed ? "preconfirmed" : "settled",
|
|
2285
|
-
commitmentTxIds: c.commitmentTxIds,
|
|
2286
|
-
batchExpiry: toBatchExpiry(c)
|
|
2287
|
-
};
|
|
2288
|
-
}
|
|
2289
2424
|
function normalizeVtxo(v) {
|
|
2290
|
-
const
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
// as `Date` but returns NaN from `.getTime()` — comparing false against everything.
|
|
2295
|
-
{
|
|
2296
|
-
expiresAt: v.expiresAt === void 0 ? void 0 : new Date(v.expiresAt),
|
|
2297
|
-
expiresAtHeight: v.expiresAtHeight
|
|
2298
|
-
}
|
|
2299
|
-
) : parseLegacyExpiry(v.virtualStatus?.batchExpiry);
|
|
2300
|
-
const isSpent = v.isSpent ?? state === "spent";
|
|
2301
|
-
const isSwept = v.isSwept ?? state === "swept";
|
|
2302
|
-
const isPreconfirmed = v.isPreconfirmed ?? state === "preconfirmed";
|
|
2303
|
-
const commitmentTxIds = v.commitmentTxIds ?? v.virtualStatus?.commitmentTxIds ?? [];
|
|
2425
|
+
const expiry = {
|
|
2426
|
+
expiresAt: v.expiresAt === void 0 ? void 0 : new Date(v.expiresAt),
|
|
2427
|
+
expiresAtHeight: v.expiresAtHeight
|
|
2428
|
+
};
|
|
2304
2429
|
return {
|
|
2305
2430
|
...v,
|
|
2306
|
-
isSpent,
|
|
2307
|
-
isSwept,
|
|
2308
|
-
isPreconfirmed,
|
|
2431
|
+
isSpent: v.isSpent ?? false,
|
|
2432
|
+
isSwept: v.isSwept ?? false,
|
|
2433
|
+
isPreconfirmed: v.isPreconfirmed ?? false,
|
|
2309
2434
|
spentBy: v.spentBy ?? "",
|
|
2310
|
-
commitmentTxIds,
|
|
2311
|
-
...expiry
|
|
2312
|
-
virtualStatus: v.virtualStatus ?? toVirtualStatus({ isSpent, isSwept, isPreconfirmed, commitmentTxIds, ...expiry })
|
|
2435
|
+
commitmentTxIds: v.commitmentTxIds ?? [],
|
|
2436
|
+
...expiry
|
|
2313
2437
|
};
|
|
2314
2438
|
}
|
|
2315
2439
|
function convertVtxo(vtxo) {
|
|
2316
2440
|
const expiry = parseWireExpiry(vtxo.expiresAt);
|
|
2317
|
-
const facts = {
|
|
2318
|
-
isSpent: vtxo.isSpent,
|
|
2319
|
-
isSwept: vtxo.isSwept,
|
|
2320
|
-
isPreconfirmed: vtxo.isPreconfirmed,
|
|
2321
|
-
commitmentTxIds: vtxo.commitmentTxids,
|
|
2322
|
-
...expiry
|
|
2323
|
-
};
|
|
2324
2441
|
return {
|
|
2325
2442
|
txid: vtxo.outpoint.txid,
|
|
2326
2443
|
vout: vtxo.outpoint.vout,
|
|
@@ -2329,8 +2446,11 @@ function convertVtxo(vtxo) {
|
|
|
2329
2446
|
confirmed: !vtxo.isSwept && !vtxo.isPreconfirmed,
|
|
2330
2447
|
isLeaf: !vtxo.isPreconfirmed
|
|
2331
2448
|
},
|
|
2332
|
-
|
|
2333
|
-
|
|
2449
|
+
isSpent: vtxo.isSpent,
|
|
2450
|
+
isSwept: vtxo.isSwept,
|
|
2451
|
+
isPreconfirmed: vtxo.isPreconfirmed,
|
|
2452
|
+
commitmentTxIds: vtxo.commitmentTxids,
|
|
2453
|
+
...expiry,
|
|
2334
2454
|
spentBy: vtxo.spentBy ?? "",
|
|
2335
2455
|
settledBy: vtxo.settledBy,
|
|
2336
2456
|
arkTxId: vtxo.arkTxid,
|
|
@@ -2347,11 +2467,16 @@ async function getNormalizedVtxos(provider, opts) {
|
|
|
2347
2467
|
const { vtxos, page } = await provider.getVtxos(opts);
|
|
2348
2468
|
return { vtxos: vtxos.map(normalizeVtxo), page };
|
|
2349
2469
|
}
|
|
2470
|
+
var SCRIPT_CHUNK_CONCURRENCY = 4;
|
|
2350
2471
|
async function getAllNormalizedVtxos(provider, scripts, opts = {}) {
|
|
2351
2472
|
const { pageSize = DEFAULT_PAGE_SIZE, ...filters } = opts;
|
|
2352
2473
|
const all = [];
|
|
2474
|
+
const chunks = [];
|
|
2353
2475
|
for (let i = 0; i < scripts.length; i += SCRIPT_QUERY_CHUNK_SIZE) {
|
|
2354
|
-
|
|
2476
|
+
chunks.push(scripts.slice(i, i + SCRIPT_QUERY_CHUNK_SIZE));
|
|
2477
|
+
}
|
|
2478
|
+
const fetchChunk = async (chunk) => {
|
|
2479
|
+
const result = [];
|
|
2355
2480
|
let pageIndex = 0;
|
|
2356
2481
|
let hasMore = true;
|
|
2357
2482
|
while (hasMore) {
|
|
@@ -2361,11 +2486,18 @@ async function getAllNormalizedVtxos(provider, scripts, opts = {}) {
|
|
|
2361
2486
|
pageIndex,
|
|
2362
2487
|
pageSize
|
|
2363
2488
|
});
|
|
2364
|
-
|
|
2489
|
+
result.push(...vtxos);
|
|
2365
2490
|
hasMore = page ? vtxos.length === pageSize : false;
|
|
2366
2491
|
pageIndex++;
|
|
2367
2492
|
if (hasMore) await new Promise((r) => setTimeout(r, 500));
|
|
2368
2493
|
}
|
|
2494
|
+
return result;
|
|
2495
|
+
};
|
|
2496
|
+
for (let i = 0; i < chunks.length; i += SCRIPT_CHUNK_CONCURRENCY) {
|
|
2497
|
+
const batch = await Promise.all(
|
|
2498
|
+
chunks.slice(i, i + SCRIPT_CHUNK_CONCURRENCY).map(fetchChunk)
|
|
2499
|
+
);
|
|
2500
|
+
for (const result of batch) all.push(...result);
|
|
2369
2501
|
}
|
|
2370
2502
|
return all;
|
|
2371
2503
|
}
|
|
@@ -2388,7 +2520,7 @@ async function fetchVtxoCreatedAtByTxid(provider, txids) {
|
|
|
2388
2520
|
}
|
|
2389
2521
|
return createdAt;
|
|
2390
2522
|
}
|
|
2391
|
-
function
|
|
2523
|
+
function isVtxoSpent(vtxo) {
|
|
2392
2524
|
const n = normalizeVtxo(vtxo);
|
|
2393
2525
|
return !!n.isSpent || !!n.spentBy || !!n.settledBy;
|
|
2394
2526
|
}
|
|
@@ -2399,41 +2531,33 @@ function isPastExpiry(vtxo, now) {
|
|
|
2399
2531
|
}
|
|
2400
2532
|
function canSpendOffchain(vtxo, now) {
|
|
2401
2533
|
const n = normalizeVtxo(vtxo);
|
|
2402
|
-
return !
|
|
2534
|
+
return !isVtxoSpent(n) && !n.isUnrolled && !(n.isSwept || isPastExpiry(n, now));
|
|
2403
2535
|
}
|
|
2404
2536
|
function canRecoverOnchain(vtxo, now) {
|
|
2405
2537
|
const n = normalizeVtxo(vtxo);
|
|
2406
|
-
return !
|
|
2538
|
+
return !isVtxoSpent(n) && !n.isUnrolled && (n.isSwept || isPastExpiry(n, now));
|
|
2407
2539
|
}
|
|
2408
2540
|
function canSweepOnchain(vtxo) {
|
|
2409
2541
|
const n = normalizeVtxo(vtxo);
|
|
2410
|
-
return !
|
|
2542
|
+
return !isVtxoSpent(n) && !!n.isUnrolled;
|
|
2543
|
+
}
|
|
2544
|
+
function requiresForfeit(vtxo) {
|
|
2545
|
+
const n = normalizeVtxo(vtxo);
|
|
2546
|
+
return !n.isSwept && !n.isUnrolled;
|
|
2411
2547
|
}
|
|
2412
2548
|
function toOffchainInputFeeParams(vtxo) {
|
|
2413
|
-
const batchExpiry = toBatchExpiry(vtxo);
|
|
2414
2549
|
return {
|
|
2415
2550
|
amount: BigInt(vtxo.value),
|
|
2416
2551
|
type: vtxo.isSwept ? "recoverable" : "vtxo",
|
|
2417
2552
|
weight: 0,
|
|
2418
2553
|
birth: vtxo.createdAt,
|
|
2419
|
-
expiry:
|
|
2554
|
+
expiry: vtxo.expiresAt
|
|
2420
2555
|
};
|
|
2421
2556
|
}
|
|
2422
2557
|
function isVirtualCoin(input) {
|
|
2423
2558
|
return typeof input === "object" && input !== null && typeof input.script === "string";
|
|
2424
2559
|
}
|
|
2425
|
-
function isSpendable(vtxo) {
|
|
2426
|
-
return !hasTerminalSpend(vtxo);
|
|
2427
|
-
}
|
|
2428
|
-
function isRecoverable(vtxo) {
|
|
2429
|
-
const n = normalizeVtxo(vtxo);
|
|
2430
|
-
return n.isSwept && !hasTerminalSpend(n);
|
|
2431
|
-
}
|
|
2432
|
-
function isExpired(vtxo) {
|
|
2433
|
-
const n = normalizeVtxo(vtxo);
|
|
2434
|
-
return n.isSwept || n.expiresAt !== void 0 && n.expiresAt.getTime() <= Date.now();
|
|
2435
|
-
}
|
|
2436
2560
|
|
|
2437
|
-
export { ArkAddress, ArkError, ArkErrorName, ArkPsbtFieldKey, ArkPsbtFieldKeyType, BITCOIN_EMULATOR_PUBKEY, CLTVMultisigTapscript, CSVMultisigTapscript, ConditionCSVMultisigTapscript, ConditionMultisigTapscript, ConditionWitness, CosignerPublicKey, DEFAULT_ARKADE_SERVER_URL, DEFAULT_NETWORK, DEFAULT_NETWORK_NAME, DEFAULT_PAGE_SIZE, DigestMismatchError, EventSourceUnavailableError, FetchError, INFO_FETCH_TIMEOUT_MS, Intent, MUTINYNET_EMULATOR_PUBKEY, MultisigTapscript, OP_RETURN_EMPTY_PKSCRIPT, PrevArkTxField, PrevoutTxField, ProviderUnavailableError, REGTEST_EMULATOR_PUBKEY, RestArkProvider, SCRIPT_QUERY_CHUNK_SIZE, ServerResponseMismatchError, SettlementEventType, TapTreeCoder, Transaction, VtxoScript, VtxoTaprootTree, VtxoTreeExpiry, assembleBtcdTaprootTree, assertAllowedSighashTypes, baseFetch, buildVersion, canRecoverOnchain, canSpendOffchain, canSweepOnchain, configureEventSource, convertVtxo, craftToSpendTx, decodeTapscript, defaultEmulatorPubkey, eventSourceIterator,
|
|
2438
|
-
//# sourceMappingURL=chunk-
|
|
2439
|
-
//# sourceMappingURL=chunk-
|
|
2561
|
+
export { ARKADE_VERSION_HEADERS, ArkAddress, ArkError, ArkErrorName, ArkPsbtFieldKey, ArkPsbtFieldKeyType, BITCOIN_EMULATOR_PUBKEY, CLTVMultisigTapscript, CSVMultisigTapscript, ConditionCSVMultisigTapscript, ConditionMultisigTapscript, ConditionWitness, CosignerPublicKey, DEFAULT_ARKADE_EMULATOR_URL, DEFAULT_ARKADE_SERVER_URL, DEFAULT_NETWORK, DEFAULT_NETWORK_NAME, DEFAULT_PAGE_SIZE, DigestMismatchError, EXPIRY_MIN_PLAUSIBLE_MS, EventSourceUnavailableError, FetchError, INFO_FETCH_TIMEOUT_MS, Intent, MUTINYNET_EMULATOR_PUBKEY, MultisigTapscript, OP_RETURN_EMPTY_PKSCRIPT, PrevArkTxField, PrevoutTxField, ProviderUnavailableError, READ_TIMEOUT_MS, REGTEST_EMULATOR_PUBKEY, RestArkProvider, SCRIPT_QUERY_CHUNK_SIZE, ServerResponseMismatchError, SettlementEventType, TapTreeCoder, Transaction, VtxoScript, VtxoTaprootTree, VtxoTreeExpiry, __export, assembleBtcdTaprootTree, assertAllowedSighashTypes, assertNonNegativeMs, assertTimerMs, baseFetch, buildVersion, canRecoverOnchain, canSpendOffchain, canSweepOnchain, configureEventSource, convertVtxo, craftToSpendTx, decodeTapscript, defaultEmulatorPubkey, eventSourceIterator, fetchVtxoCreatedAtByTxid, formatSighash, getAllNormalizedVtxos, getArkPsbtFields, getConfiguredEventSource, getNetwork, getNormalizedVtxos, getSequence, isArkError, isEventSourceError, isEventSourceUnavailableError, isFetchTimeoutError, isPastExpiry, isRetryableProviderError, isVirtualCoin, isVtxoSpent, maybeArkError, networkFromArkadeInfo, networks, normalizeVtxo, rateGate, requiresForfeit, resolveEmulatorPubkey, resolveEventSource, resolveTimeHeight, scriptFromTapLeafScript, sdkVersion, sequenceToCanonicalTimelock, sequenceToTimelock, setArkPsbtField, streamFailureError, throwIfHttpUnavailable, timelockToSequence, toBIP371TapTree, toOffchainInputFeeParams, toProviderUnavailable };
|
|
2562
|
+
//# sourceMappingURL=chunk-G6ZPX2GL.js.map
|
|
2563
|
+
//# sourceMappingURL=chunk-G6ZPX2GL.js.map
|