@babylonlabs-io/ts-sdk 0.27.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{buildAndBroadcastRefund-BzYJv-Fv.js → buildAndBroadcastRefund-Cc4-L7gX.js} +32 -31
- package/dist/buildAndBroadcastRefund-Cc4-L7gX.js.map +1 -0
- package/dist/buildAndBroadcastRefund-OoaQaNqn.cjs +2 -0
- package/dist/buildAndBroadcastRefund-OoaQaNqn.cjs.map +1 -0
- package/dist/deriveVaultRoot-B4gnRbW_.cjs +2 -0
- package/dist/deriveVaultRoot-B4gnRbW_.cjs.map +1 -0
- package/dist/{context-CClNOyD3.js → deriveVaultRoot-DAMZDqg-.js} +495 -541
- package/dist/deriveVaultRoot-DAMZDqg-.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +55 -55
- package/dist/psbtInputFields-CB8hqjQ5.cjs +2 -0
- package/dist/psbtInputFields-CB8hqjQ5.cjs.map +1 -0
- package/dist/{psbtInputFields-C3dqKWjQ.js → psbtInputFields-DeTFSJOq.js} +70 -64
- package/dist/psbtInputFields-DeTFSJOq.js.map +1 -0
- package/dist/sha2-CPdTLk1u.js +280 -0
- package/dist/sha2-CPdTLk1u.js.map +1 -0
- package/dist/sha2-CsTynrfJ.cjs +2 -0
- package/dist/sha2-CsTynrfJ.cjs.map +1 -0
- package/dist/shared/index.cjs.map +1 -1
- package/dist/shared/index.js.map +1 -1
- package/dist/shared/wallets/index.d.ts +2 -1
- package/dist/shared/wallets/index.d.ts.map +1 -1
- package/dist/shared/wallets/interfaces/BitcoinWallet.d.ts +8 -0
- package/dist/shared/wallets/interfaces/BitcoinWallet.d.ts.map +1 -1
- package/dist/signing-BZigafm0.js.map +1 -1
- package/dist/signing-DHSXjhLM.cjs.map +1 -1
- package/dist/tbv/core/clients/index.cjs +1 -1
- package/dist/tbv/core/clients/index.js +2 -2
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +52 -52
- package/dist/tbv/core/services/deposit/peginState.d.ts +1 -1
- package/dist/tbv/core/services/deposit/peginState.d.ts.map +1 -1
- package/dist/tbv/core/services/index.cjs +1 -1
- package/dist/tbv/core/services/index.js +1 -1
- package/dist/tbv/core/utils/index.cjs +1 -1
- package/dist/tbv/core/utils/index.js +1 -1
- package/dist/tbv/core/utils/utxo/reservation.d.ts +5 -0
- package/dist/tbv/core/utils/utxo/reservation.d.ts.map +1 -1
- package/dist/tbv/core/vault-secrets/__tests__/deriveVaultRoot.test.d.ts +13 -0
- package/dist/tbv/core/vault-secrets/__tests__/deriveVaultRoot.test.d.ts.map +1 -0
- package/dist/tbv/core/vault-secrets/context.d.ts +9 -0
- package/dist/tbv/core/vault-secrets/context.d.ts.map +1 -1
- package/dist/tbv/core/vault-secrets/deriveVaultRoot.d.ts +56 -0
- package/dist/tbv/core/vault-secrets/deriveVaultRoot.d.ts.map +1 -0
- package/dist/tbv/core/vault-secrets/expand.d.ts +18 -3
- package/dist/tbv/core/vault-secrets/expand.d.ts.map +1 -1
- package/dist/tbv/core/vault-secrets/index.d.ts +6 -3
- package/dist/tbv/core/vault-secrets/index.d.ts.map +1 -1
- package/dist/tbv/core/wots/blockDerivation.d.ts +20 -24
- package/dist/tbv/core/wots/blockDerivation.d.ts.map +1 -1
- package/dist/tbv/core/wots/errors.d.ts +3 -1
- package/dist/tbv/core/wots/errors.d.ts.map +1 -1
- package/dist/tbv/core/wots/index.d.ts +1 -2
- package/dist/tbv/core/wots/index.d.ts.map +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +52 -52
- package/dist/testing/MockBitcoinWallet.d.ts +9 -3
- package/dist/testing/MockBitcoinWallet.d.ts.map +1 -1
- package/dist/testing/index.cjs +1 -1
- package/dist/testing/index.cjs.map +1 -1
- package/dist/testing/index.js +65 -54
- package/dist/testing/index.js.map +1 -1
- package/dist/types-CQ86O7NX.js +650 -0
- package/dist/types-CQ86O7NX.js.map +1 -0
- package/dist/types-CcwaEPE1.cjs +2 -0
- package/dist/types-CcwaEPE1.cjs.map +1 -0
- package/dist/{vault-registry-reader-CpCOte7w.js → vault-registry-reader-CshEgmS0.js} +31 -30
- package/dist/{vault-registry-reader-CpCOte7w.js.map → vault-registry-reader-CshEgmS0.js.map} +1 -1
- package/dist/vault-registry-reader-_2BjSjoN.cjs +2 -0
- package/dist/{vault-registry-reader-BizmBSBV.cjs.map → vault-registry-reader-_2BjSjoN.cjs.map} +1 -1
- package/package.json +3 -3
- package/dist/buildAndBroadcastRefund-BEASMFsz.cjs +0 -2
- package/dist/buildAndBroadcastRefund-BEASMFsz.cjs.map +0 -1
- package/dist/buildAndBroadcastRefund-BzYJv-Fv.js.map +0 -1
- package/dist/context-BSdcD4J_.cjs +0 -2
- package/dist/context-BSdcD4J_.cjs.map +0 -1
- package/dist/context-CClNOyD3.js.map +0 -1
- package/dist/psbtInputFields-C3dqKWjQ.js.map +0 -1
- package/dist/psbtInputFields-DhGm77bP.cjs +0 -2
- package/dist/psbtInputFields-DhGm77bP.cjs.map +0 -1
- package/dist/sha2-1XZuToHP.cjs +0 -2
- package/dist/sha2-1XZuToHP.cjs.map +0 -1
- package/dist/sha2-ZzfZqQSw.js +0 -1123
- package/dist/sha2-ZzfZqQSw.js.map +0 -1
- package/dist/tbv/core/wots/deriveWotsPkHash.d.ts +0 -9
- package/dist/tbv/core/wots/deriveWotsPkHash.d.ts.map +0 -1
- package/dist/vault-registry-reader-BizmBSBV.cjs +0 -2
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import * as
|
|
5
|
-
import { Transaction as
|
|
6
|
-
import { Buffer as
|
|
7
|
-
import { isAddressEqual as
|
|
8
|
-
import { c as
|
|
9
|
-
import { deriveVaultId as
|
|
10
|
-
import { b as
|
|
11
|
-
import { s as v, g as
|
|
12
|
-
import { M as
|
|
13
|
-
import { b as
|
|
14
|
-
import { p as
|
|
15
|
-
import { p as
|
|
16
|
-
import { B as
|
|
17
|
-
|
|
1
|
+
var zt = Object.defineProperty;
|
|
2
|
+
var Yt = (e, t, n) => t in e ? zt(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
+
var p = (e, t, n) => Yt(e, typeof t != "symbol" ? t + "" : t, n);
|
|
4
|
+
import * as Jt from "bitcoinjs-lib";
|
|
5
|
+
import { Transaction as Qt, Psbt as dt } from "bitcoinjs-lib";
|
|
6
|
+
import { Buffer as et } from "buffer";
|
|
7
|
+
import { isAddressEqual as ft, createPublicClient as j, http as q, encodeFunctionData as gt, zeroAddress as te } from "viem";
|
|
8
|
+
import { c as ee } from "./signing-BZigafm0.js";
|
|
9
|
+
import { deriveVaultId as pt } from "@babylonlabs-io/babylon-tbv-rust-wasm";
|
|
10
|
+
import { b as ne, a as se, d as oe, e as re, f as ie } from "./challengeAssert-j2Vwqo0-.js";
|
|
11
|
+
import { s as v, g as mt, e as _, i as ae, p as ce, u as le, h as he } from "./bitcoin-B-Y0DlqR.js";
|
|
12
|
+
import { M as bt } from "./validation-CxqROCno.js";
|
|
13
|
+
import { b as ue, f as G, i as de } from "./psbtInputFields-DeTFSJOq.js";
|
|
14
|
+
import { p as fe, f as ge } from "./fundPeginTransaction-oV-dNJOU.js";
|
|
15
|
+
import { p as pe, f as me } from "./vault-registry-reader-CshEgmS0.js";
|
|
16
|
+
import { B as S } from "./types-CQ86O7NX.js";
|
|
17
|
+
import { c as St, H as be, r as W, a as H, b as nt, u as we, d as wt, e as F, f as U, g as ye, s as L, h as Ot } from "./sha2-CPdTLk1u.js";
|
|
18
|
+
const C = {
|
|
18
19
|
// VaultAlreadyExists()
|
|
19
20
|
"0x04aabf33": "Vault already exists: This Bitcoin transaction has already been registered. Please select different UTXOs or use a different amount to create a unique transaction.",
|
|
20
21
|
// ScriptPubKeyMismatch() - taproot output doesn't match expected script
|
|
@@ -48,7 +49,7 @@ const H = {
|
|
|
48
49
|
// PeginTransactionAlreadyUsed()
|
|
49
50
|
"0x7ed061c9": "This pegin transaction has already been used to activate another vault."
|
|
50
51
|
};
|
|
51
|
-
function
|
|
52
|
+
function it(e) {
|
|
52
53
|
if (!e || typeof e != "object") return;
|
|
53
54
|
const t = e;
|
|
54
55
|
if (typeof t.data == "string" && t.data.startsWith("0x"))
|
|
@@ -67,24 +68,24 @@ function dt(e) {
|
|
|
67
68
|
if (i)
|
|
68
69
|
return i[1];
|
|
69
70
|
}
|
|
70
|
-
function
|
|
71
|
-
const t =
|
|
71
|
+
function An(e) {
|
|
72
|
+
const t = it(e);
|
|
72
73
|
if (t) {
|
|
73
74
|
const n = t.substring(0, 10);
|
|
74
|
-
return
|
|
75
|
+
return C[t] ?? C[n];
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
|
-
function
|
|
78
|
-
const t =
|
|
78
|
+
function Hn(e) {
|
|
79
|
+
const t = it(e);
|
|
79
80
|
if (t === void 0) return !1;
|
|
80
81
|
const n = t.substring(0, 10);
|
|
81
|
-
return t in
|
|
82
|
+
return t in C || n in C;
|
|
82
83
|
}
|
|
83
84
|
function A(e) {
|
|
84
85
|
console.error("[Contract Error] Raw error:", e);
|
|
85
|
-
const t =
|
|
86
|
+
const t = it(e);
|
|
86
87
|
if (console.error("[Contract Error] Extracted error data:", t), t) {
|
|
87
|
-
const s = t.substring(0, 10), o =
|
|
88
|
+
const s = t.substring(0, 10), o = C[t] ?? C[s];
|
|
88
89
|
if (o)
|
|
89
90
|
throw console.error("[Contract Error] Known error:", o), new Error(o);
|
|
90
91
|
}
|
|
@@ -102,50 +103,50 @@ function A(e) {
|
|
|
102
103
|
}
|
|
103
104
|
throw e instanceof Error ? (console.error("[Contract Error] Unhandled error:", e.message), e) : new Error(`Contract call failed: ${String(e)}`);
|
|
104
105
|
}
|
|
105
|
-
const
|
|
106
|
-
function
|
|
106
|
+
const xe = 0, Pe = /^0x[0-9a-f]+$/i, Ee = /^[0-9a-f]+$/i, Te = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
107
|
+
function O(e) {
|
|
107
108
|
if (typeof e != "string" || e.length === 0)
|
|
108
109
|
throw new Error("BTC wallet returned empty public key");
|
|
109
|
-
return
|
|
110
|
+
return ce(e).toLowerCase();
|
|
110
111
|
}
|
|
111
|
-
function
|
|
112
|
+
function ke(e) {
|
|
112
113
|
if (typeof e != "string" || e.length === 0)
|
|
113
114
|
throw new Error("BTC wallet returned empty BIP-322 signature");
|
|
114
115
|
if (e.startsWith("0x") || e.startsWith("0X")) {
|
|
115
|
-
if (!
|
|
116
|
+
if (!Pe.test(e) || e.length < 4 || e.length % 2 !== 0)
|
|
116
117
|
throw new Error("BTC wallet returned malformed hex BIP-322 signature");
|
|
117
118
|
return e.toLowerCase();
|
|
118
119
|
}
|
|
119
|
-
if (
|
|
120
|
+
if (Ee.test(e)) {
|
|
120
121
|
if (e.length % 2 !== 0)
|
|
121
122
|
throw new Error("BTC wallet returned malformed hex BIP-322 signature");
|
|
122
123
|
return `0x${e.toLowerCase()}`;
|
|
123
124
|
}
|
|
124
|
-
if (!
|
|
125
|
+
if (!Te.test(e) || e.length % 4 !== 0)
|
|
125
126
|
throw new Error("BTC wallet returned malformed base64 BIP-322 signature");
|
|
126
|
-
const t =
|
|
127
|
+
const t = et.from(e, "base64");
|
|
127
128
|
if (t.length === 0 || t.toString("base64") !== e)
|
|
128
129
|
throw new Error("BTC wallet returned malformed base64 BIP-322 signature");
|
|
129
130
|
return `0x${t.toString("hex")}`;
|
|
130
131
|
}
|
|
131
|
-
function
|
|
132
|
+
function _e(e, t, n, s) {
|
|
132
133
|
const o = n == null ? void 0 : n[`${e}:${t}`];
|
|
133
134
|
return o ? Promise.resolve({
|
|
134
135
|
txid: e,
|
|
135
136
|
vout: t,
|
|
136
137
|
value: o.value,
|
|
137
138
|
scriptPubKey: o.scriptPubKey
|
|
138
|
-
}) :
|
|
139
|
+
}) : me(e, t, s);
|
|
139
140
|
}
|
|
140
|
-
const
|
|
141
|
-
class
|
|
141
|
+
const yt = 12e4;
|
|
142
|
+
class Cn {
|
|
142
143
|
/**
|
|
143
144
|
* Creates a new PeginManager instance.
|
|
144
145
|
*
|
|
145
146
|
* @param config - Manager configuration including wallets and contract addresses
|
|
146
147
|
*/
|
|
147
148
|
constructor(t) {
|
|
148
|
-
|
|
149
|
+
p(this, "config");
|
|
149
150
|
this.config = t;
|
|
150
151
|
}
|
|
151
152
|
/**
|
|
@@ -169,14 +170,14 @@ class Nn {
|
|
|
169
170
|
* @throws Error if wallet operations fail or insufficient funds
|
|
170
171
|
*/
|
|
171
172
|
async preparePegin(t) {
|
|
172
|
-
const n = await this.config.btcWallet.getPublicKeyHex(), s =
|
|
173
|
+
const n = await this.config.btcWallet.getPublicKeyHex(), s = O(n), o = v(t.vaultProviderBtcPubkey), r = t.vaultKeeperBtcPubkeys.map(v), i = t.universalChallengerBtcPubkeys.map(v);
|
|
173
174
|
if (t.hashlocks.length !== t.amounts.length)
|
|
174
175
|
throw new Error(
|
|
175
176
|
`hashlocks.length (${t.hashlocks.length}) must equal amounts.length (${t.amounts.length})`
|
|
176
177
|
);
|
|
177
178
|
if (t.hashlocks.length === 0)
|
|
178
179
|
throw new Error("hashlocks must contain at least one entry");
|
|
179
|
-
const a = r.length,
|
|
180
|
+
const a = r.length, l = {
|
|
180
181
|
depositorPubkey: s,
|
|
181
182
|
vaultProviderPubkey: o,
|
|
182
183
|
vaultKeeperPubkeys: r,
|
|
@@ -189,64 +190,64 @@ class Nn {
|
|
|
189
190
|
councilQuorum: t.councilQuorum,
|
|
190
191
|
councilSize: t.councilSize,
|
|
191
192
|
network: this.config.btcNetwork
|
|
192
|
-
},
|
|
193
|
+
}, h = await ne(l), c = ue(
|
|
193
194
|
[...t.availableUTXOs],
|
|
194
|
-
|
|
195
|
+
h.totalOutputValue,
|
|
195
196
|
t.mempoolFeeRate,
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
197
|
+
fe(
|
|
198
|
+
h.htlcValues.length,
|
|
199
|
+
l.authAnchorHash
|
|
199
200
|
)
|
|
200
|
-
),
|
|
201
|
-
unfundedTxHex:
|
|
201
|
+
), b = mt(this.config.btcNetwork), m = ge({
|
|
202
|
+
unfundedTxHex: h.psbtHex,
|
|
202
203
|
selectedUTXOs: c.selectedUTXOs,
|
|
203
204
|
changeAddress: t.changeAddress,
|
|
204
205
|
changeAmount: c.changeAmount,
|
|
205
|
-
network:
|
|
206
|
-
}),
|
|
207
|
-
for (let
|
|
208
|
-
const
|
|
209
|
-
prePeginParams:
|
|
206
|
+
network: b
|
|
207
|
+
}), w = v(G(m)), g = [], E = [], P = [];
|
|
208
|
+
for (let u = 0; u < t.hashlocks.length; u++) {
|
|
209
|
+
const d = await se({
|
|
210
|
+
prePeginParams: l,
|
|
210
211
|
timelockPegin: t.timelockPegin,
|
|
211
|
-
fundedPrePeginTxHex:
|
|
212
|
-
htlcVout:
|
|
213
|
-
}),
|
|
214
|
-
peginTxHex:
|
|
215
|
-
fundedPrePeginTxHex:
|
|
212
|
+
fundedPrePeginTxHex: m,
|
|
213
|
+
htlcVout: u
|
|
214
|
+
}), x = await oe({
|
|
215
|
+
peginTxHex: d.txHex,
|
|
216
|
+
fundedPrePeginTxHex: m,
|
|
216
217
|
depositorPubkey: s,
|
|
217
218
|
vaultProviderPubkey: o,
|
|
218
219
|
vaultKeeperPubkeys: r,
|
|
219
220
|
universalChallengerPubkeys: i,
|
|
220
|
-
hashlock: t.hashlocks[
|
|
221
|
+
hashlock: t.hashlocks[u],
|
|
221
222
|
timelockRefund: t.timelockRefund,
|
|
222
223
|
network: this.config.btcNetwork
|
|
223
224
|
});
|
|
224
|
-
|
|
225
|
-
|
|
225
|
+
g.push(d), E.push(x.psbtHex), P.push(
|
|
226
|
+
ee(n, 1)
|
|
226
227
|
);
|
|
227
228
|
}
|
|
228
|
-
const
|
|
229
|
-
|
|
229
|
+
const k = await this.signPsbtsWithFallback(
|
|
230
|
+
E,
|
|
230
231
|
P
|
|
231
|
-
),
|
|
232
|
-
for (let
|
|
233
|
-
const
|
|
234
|
-
|
|
232
|
+
), f = [];
|
|
233
|
+
for (let u = 0; u < k.length; u++) {
|
|
234
|
+
const d = re(
|
|
235
|
+
k[u],
|
|
235
236
|
s
|
|
236
|
-
),
|
|
237
|
-
|
|
238
|
-
htlcVout:
|
|
239
|
-
htlcValue:
|
|
240
|
-
peginTxHex:
|
|
241
|
-
peginTxid:
|
|
242
|
-
peginInputSignature:
|
|
243
|
-
vaultScriptPubKey:
|
|
237
|
+
), x = ie(k[u]);
|
|
238
|
+
f.push({
|
|
239
|
+
htlcVout: u,
|
|
240
|
+
htlcValue: h.htlcValues[u],
|
|
241
|
+
peginTxHex: x,
|
|
242
|
+
peginTxid: g[u].txid,
|
|
243
|
+
peginInputSignature: d,
|
|
244
|
+
vaultScriptPubKey: g[u].vaultScriptPubKey
|
|
244
245
|
});
|
|
245
246
|
}
|
|
246
247
|
return {
|
|
247
|
-
fundedPrePeginTxHex:
|
|
248
|
-
prePeginTxid:
|
|
249
|
-
perVault:
|
|
248
|
+
fundedPrePeginTxHex: m,
|
|
249
|
+
prePeginTxid: w,
|
|
250
|
+
perVault: f,
|
|
250
251
|
selectedUTXOs: c.selectedUTXOs,
|
|
251
252
|
fee: c.fee,
|
|
252
253
|
changeAmount: c.changeAmount
|
|
@@ -297,68 +298,68 @@ class Nn {
|
|
|
297
298
|
* @throws Error if signing or broadcasting fails
|
|
298
299
|
*/
|
|
299
300
|
async signAndBroadcast(t) {
|
|
300
|
-
const { fundedPrePeginTxHex: n, depositorBtcPubkey: s } = t, o = n.startsWith("0x") ? n.slice(2) : n, r =
|
|
301
|
+
const { fundedPrePeginTxHex: n, depositorBtcPubkey: s } = t, o = n.startsWith("0x") ? n.slice(2) : n, r = Qt.fromHex(o);
|
|
301
302
|
if (r.ins.length === 0)
|
|
302
303
|
throw new Error("Transaction has no inputs");
|
|
303
|
-
const i = new
|
|
304
|
+
const i = new dt();
|
|
304
305
|
i.setVersion(r.version), i.setLocktime(r.locktime);
|
|
305
|
-
const a =
|
|
306
|
-
|
|
306
|
+
const a = et.from(
|
|
307
|
+
O(s),
|
|
307
308
|
"hex"
|
|
308
|
-
),
|
|
309
|
-
const
|
|
310
|
-
return
|
|
311
|
-
(
|
|
309
|
+
), l = this.config.mempoolApiUrl, h = r.ins.map((f) => {
|
|
310
|
+
const u = et.from(f.hash).reverse().toString("hex"), d = f.index;
|
|
311
|
+
return _e(u, d, t.localPrevouts, l).then(
|
|
312
|
+
(x) => ({ input: f, utxoData: x, txid: u, vout: d })
|
|
312
313
|
);
|
|
313
|
-
}), c = await Promise.all(
|
|
314
|
-
(
|
|
314
|
+
}), c = await Promise.all(h), b = c.reduce(
|
|
315
|
+
(f, u) => f + BigInt(u.utxoData.value),
|
|
315
316
|
0n
|
|
316
|
-
),
|
|
317
|
-
(
|
|
317
|
+
), m = r.outs.reduce(
|
|
318
|
+
(f, u) => f + BigInt(u.value),
|
|
318
319
|
0n
|
|
319
320
|
);
|
|
320
|
-
if (
|
|
321
|
+
if (b < m)
|
|
321
322
|
throw new Error(
|
|
322
|
-
`UTXO value mismatch: total input value (${
|
|
323
|
+
`UTXO value mismatch: total input value (${b} sat) is less than total output value (${m} sat). This may indicate the mempool API returned manipulated UTXO data.`
|
|
323
324
|
);
|
|
324
|
-
const
|
|
325
|
-
if (
|
|
325
|
+
const w = b - m;
|
|
326
|
+
if (w > bt)
|
|
326
327
|
throw new Error(
|
|
327
|
-
`Implied transaction fee (${
|
|
328
|
+
`Implied transaction fee (${w} sat) exceeds maximum reasonable fee (${bt} sat). This may indicate manipulated UTXO data.`
|
|
328
329
|
);
|
|
329
|
-
for (const { input:
|
|
330
|
-
const
|
|
330
|
+
for (const { input: f, utxoData: u, txid: d, vout: x } of c) {
|
|
331
|
+
const y = de(
|
|
331
332
|
{
|
|
332
|
-
value:
|
|
333
|
-
scriptPubKey:
|
|
333
|
+
value: u.value,
|
|
334
|
+
scriptPubKey: u.scriptPubKey
|
|
334
335
|
},
|
|
335
336
|
a
|
|
336
337
|
);
|
|
337
338
|
i.addInput({
|
|
338
|
-
hash:
|
|
339
|
-
index:
|
|
340
|
-
sequence:
|
|
341
|
-
...
|
|
339
|
+
hash: f.hash,
|
|
340
|
+
index: f.index,
|
|
341
|
+
sequence: f.sequence,
|
|
342
|
+
...y
|
|
342
343
|
});
|
|
343
344
|
}
|
|
344
|
-
for (const
|
|
345
|
+
for (const f of r.outs)
|
|
345
346
|
i.addOutput({
|
|
346
|
-
script:
|
|
347
|
-
value:
|
|
347
|
+
script: f.script,
|
|
348
|
+
value: f.value
|
|
348
349
|
});
|
|
349
|
-
const
|
|
350
|
+
const g = await this.config.btcWallet.signPsbt(i.toHex()), E = dt.fromHex(g);
|
|
350
351
|
try {
|
|
351
|
-
|
|
352
|
-
} catch (
|
|
353
|
-
if (!
|
|
354
|
-
(
|
|
352
|
+
E.finalizeAllInputs();
|
|
353
|
+
} catch (f) {
|
|
354
|
+
if (!E.data.inputs.every(
|
|
355
|
+
(d) => d.finalScriptWitness || d.finalScriptSig
|
|
355
356
|
))
|
|
356
357
|
throw new Error(
|
|
357
|
-
`PSBT finalization failed and wallet did not auto-finalize: ${
|
|
358
|
+
`PSBT finalization failed and wallet did not auto-finalize: ${f}`
|
|
358
359
|
);
|
|
359
360
|
}
|
|
360
|
-
const P =
|
|
361
|
-
return await
|
|
361
|
+
const P = E.extractTransaction().toHex();
|
|
362
|
+
return await pe(P, l);
|
|
362
363
|
}
|
|
363
364
|
/**
|
|
364
365
|
* Registers a peg-in on Ethereum by calling the BTCVaultRegistry contract.
|
|
@@ -392,36 +393,36 @@ class Nn {
|
|
|
392
393
|
hashlock: r,
|
|
393
394
|
htlcVout: i,
|
|
394
395
|
depositorPayoutBtcAddress: a,
|
|
395
|
-
depositorWotsPkHash:
|
|
396
|
-
popSignature:
|
|
396
|
+
depositorWotsPkHash: l,
|
|
397
|
+
popSignature: h
|
|
397
398
|
} = t;
|
|
398
399
|
if (!this.config.ethWallet.account)
|
|
399
400
|
throw new Error("Ethereum wallet account not found");
|
|
400
401
|
const c = this.config.ethWallet.account.address;
|
|
401
|
-
if (!
|
|
402
|
+
if (!ft(h.depositorEthAddress, c))
|
|
402
403
|
throw new Error(
|
|
403
|
-
`Proof of possession was signed for ${
|
|
404
|
+
`Proof of possession was signed for ${h.depositorEthAddress} but the Ethereum wallet is currently connected to ${c}. Reconnect the original account or call signProofOfPossession() again.`
|
|
404
405
|
);
|
|
405
|
-
await this.assertPopMatchesBtcWallet(
|
|
406
|
-
const
|
|
406
|
+
await this.assertPopMatchesBtcWallet(h);
|
|
407
|
+
const b = h.btcPopSignature, m = _(h.depositorBtcPubkey), w = _(n), g = _(s), E = await this.resolvePayoutScriptPubKey(
|
|
407
408
|
a
|
|
408
|
-
), P = G(
|
|
409
|
+
), P = G(g), k = await pt(
|
|
409
410
|
v(P),
|
|
410
411
|
v(c)
|
|
411
|
-
),
|
|
412
|
-
if (await this.checkVaultExists(
|
|
412
|
+
), f = _(k);
|
|
413
|
+
if (await this.checkVaultExists(f))
|
|
413
414
|
throw new Error(
|
|
414
|
-
`Vault already exists (ID: ${
|
|
415
|
+
`Vault already exists (ID: ${f}, peginTxHash: ${P}). Vault IDs are derived from the pegin transaction hash and depositor address. To create a new vault, use different UTXOs or a different amount to generate a unique transaction.`
|
|
415
416
|
);
|
|
416
|
-
const
|
|
417
|
+
const d = j({
|
|
417
418
|
chain: this.config.ethChain,
|
|
418
|
-
transport:
|
|
419
|
+
transport: q()
|
|
419
420
|
});
|
|
420
|
-
let
|
|
421
|
+
let x;
|
|
421
422
|
try {
|
|
422
|
-
|
|
423
|
+
x = await d.readContract({
|
|
423
424
|
address: this.config.vaultContracts.btcVaultRegistry,
|
|
424
|
-
abi:
|
|
425
|
+
abi: S,
|
|
425
426
|
functionName: "getPegInFee",
|
|
426
427
|
args: [o]
|
|
427
428
|
});
|
|
@@ -430,57 +431,57 @@ class Nn {
|
|
|
430
431
|
"Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct."
|
|
431
432
|
);
|
|
432
433
|
}
|
|
433
|
-
const
|
|
434
|
-
abi:
|
|
434
|
+
const y = gt({
|
|
435
|
+
abi: S,
|
|
435
436
|
functionName: "submitPeginRequest",
|
|
436
437
|
args: [
|
|
437
438
|
c,
|
|
438
|
-
l,
|
|
439
439
|
m,
|
|
440
|
-
|
|
441
|
-
|
|
440
|
+
b,
|
|
441
|
+
w,
|
|
442
|
+
g,
|
|
442
443
|
o,
|
|
443
444
|
r,
|
|
444
445
|
i,
|
|
445
|
-
|
|
446
|
-
|
|
446
|
+
E,
|
|
447
|
+
l
|
|
447
448
|
]
|
|
448
449
|
});
|
|
449
|
-
let
|
|
450
|
+
let I;
|
|
450
451
|
try {
|
|
451
|
-
|
|
452
|
+
I = await d.estimateGas({
|
|
452
453
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
453
|
-
data:
|
|
454
|
-
value:
|
|
454
|
+
data: y,
|
|
455
|
+
value: x,
|
|
455
456
|
account: this.config.ethWallet.account.address
|
|
456
457
|
});
|
|
457
|
-
} catch (
|
|
458
|
-
A(
|
|
458
|
+
} catch (K) {
|
|
459
|
+
A(K);
|
|
459
460
|
}
|
|
460
461
|
let B;
|
|
461
462
|
try {
|
|
462
463
|
B = await this.config.ethWallet.sendTransaction({
|
|
463
464
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
464
|
-
data:
|
|
465
|
-
value:
|
|
465
|
+
data: y,
|
|
466
|
+
value: x,
|
|
466
467
|
account: this.config.ethWallet.account,
|
|
467
468
|
chain: this.config.ethChain,
|
|
468
|
-
gas:
|
|
469
|
+
gas: I
|
|
469
470
|
});
|
|
470
|
-
} catch (
|
|
471
|
-
A(
|
|
471
|
+
} catch (K) {
|
|
472
|
+
A(K);
|
|
472
473
|
}
|
|
473
|
-
const
|
|
474
|
+
const D = await d.waitForTransactionReceipt({
|
|
474
475
|
hash: B,
|
|
475
|
-
timeout:
|
|
476
|
+
timeout: yt
|
|
476
477
|
});
|
|
477
|
-
return
|
|
478
|
+
return D.status === "reverted" && A(
|
|
478
479
|
new Error(
|
|
479
480
|
`Transaction reverted. Hash: ${B}. Check the transaction on block explorer for details.`
|
|
480
481
|
)
|
|
481
482
|
), {
|
|
482
|
-
ethTxHash:
|
|
483
|
-
vaultId:
|
|
483
|
+
ethTxHash: D.transactionHash,
|
|
484
|
+
vaultId: f,
|
|
484
485
|
peginTxHash: P
|
|
485
486
|
};
|
|
486
487
|
}
|
|
@@ -501,39 +502,39 @@ class Nn {
|
|
|
501
502
|
if (!this.config.ethWallet.account)
|
|
502
503
|
throw new Error("Ethereum wallet account not found");
|
|
503
504
|
const i = this.config.ethWallet.account.address;
|
|
504
|
-
if (!
|
|
505
|
+
if (!ft(r.depositorEthAddress, i))
|
|
505
506
|
throw new Error(
|
|
506
507
|
`Proof of possession was signed for ${r.depositorEthAddress} but the Ethereum wallet is currently connected to ${i}. Reconnect the original account or call signProofOfPossession() again.`
|
|
507
508
|
);
|
|
508
509
|
await this.assertPopMatchesBtcWallet(r);
|
|
509
|
-
const a = r.btcPopSignature,
|
|
510
|
-
for (const
|
|
511
|
-
|
|
512
|
-
await this.resolvePayoutScriptPubKey(
|
|
510
|
+
const a = r.btcPopSignature, l = [];
|
|
511
|
+
for (const d of o)
|
|
512
|
+
l.push(
|
|
513
|
+
await this.resolvePayoutScriptPubKey(d.depositorPayoutBtcAddress)
|
|
513
514
|
);
|
|
514
|
-
const
|
|
515
|
-
for (const
|
|
516
|
-
const
|
|
517
|
-
|
|
518
|
-
),
|
|
519
|
-
v(
|
|
515
|
+
const h = [];
|
|
516
|
+
for (const d of o) {
|
|
517
|
+
const x = _(
|
|
518
|
+
d.depositorSignedPeginTx
|
|
519
|
+
), y = G(x), I = await pt(
|
|
520
|
+
v(y),
|
|
520
521
|
v(i)
|
|
521
|
-
), B = I
|
|
522
|
+
), B = _(I);
|
|
522
523
|
if (await this.checkVaultExists(B))
|
|
523
524
|
throw new Error(
|
|
524
|
-
`Vault already exists (ID: ${B}, peginTxHash: ${
|
|
525
|
+
`Vault already exists (ID: ${B}, peginTxHash: ${y}). To create a new vault, use different UTXOs or a different amount.`
|
|
525
526
|
);
|
|
526
|
-
|
|
527
|
+
h.push({ vaultId: B, peginTxHash: y });
|
|
527
528
|
}
|
|
528
|
-
const c =
|
|
529
|
+
const c = j({
|
|
529
530
|
chain: this.config.ethChain,
|
|
530
|
-
transport:
|
|
531
|
+
transport: q()
|
|
531
532
|
});
|
|
532
|
-
let
|
|
533
|
+
let b;
|
|
533
534
|
try {
|
|
534
|
-
|
|
535
|
+
b = await c.readContract({
|
|
535
536
|
address: this.config.vaultContracts.btcVaultRegistry,
|
|
536
|
-
abi:
|
|
537
|
+
abi: S,
|
|
537
538
|
functionName: "getPegInFee",
|
|
538
539
|
args: [n]
|
|
539
540
|
});
|
|
@@ -542,60 +543,60 @@ class Nn {
|
|
|
542
543
|
"Failed to query pegin fee from the contract. Please check your network connection and that the contract address is correct."
|
|
543
544
|
);
|
|
544
545
|
}
|
|
545
|
-
const
|
|
546
|
+
const m = b * BigInt(o.length), w = _(
|
|
546
547
|
r.depositorBtcPubkey
|
|
547
|
-
),
|
|
548
|
-
depositorBtcPubKey:
|
|
548
|
+
), g = _(s), E = o.map((d, x) => ({
|
|
549
|
+
depositorBtcPubKey: w,
|
|
549
550
|
btcPopSignature: a,
|
|
550
|
-
unsignedPrePeginTx:
|
|
551
|
-
depositorSignedPeginTx:
|
|
552
|
-
|
|
551
|
+
unsignedPrePeginTx: g,
|
|
552
|
+
depositorSignedPeginTx: _(
|
|
553
|
+
d.depositorSignedPeginTx
|
|
553
554
|
),
|
|
554
|
-
hashlock:
|
|
555
|
-
htlcVout:
|
|
556
|
-
referralCode:
|
|
557
|
-
depositorPayoutBtcAddress:
|
|
558
|
-
depositorWotsPkHash:
|
|
559
|
-
})), P =
|
|
560
|
-
abi:
|
|
555
|
+
hashlock: d.hashlock,
|
|
556
|
+
htlcVout: d.htlcVout,
|
|
557
|
+
referralCode: xe,
|
|
558
|
+
depositorPayoutBtcAddress: l[x],
|
|
559
|
+
depositorWotsPkHash: d.depositorWotsPkHash
|
|
560
|
+
})), P = gt({
|
|
561
|
+
abi: S,
|
|
561
562
|
functionName: "submitPeginRequestBatch",
|
|
562
|
-
args: [i, n,
|
|
563
|
+
args: [i, n, E]
|
|
563
564
|
});
|
|
564
|
-
let
|
|
565
|
+
let k;
|
|
565
566
|
try {
|
|
566
|
-
|
|
567
|
+
k = await c.estimateGas({
|
|
567
568
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
568
569
|
data: P,
|
|
569
|
-
value:
|
|
570
|
+
value: m,
|
|
570
571
|
account: this.config.ethWallet.account.address
|
|
571
572
|
});
|
|
572
|
-
} catch (
|
|
573
|
-
A(
|
|
573
|
+
} catch (d) {
|
|
574
|
+
A(d);
|
|
574
575
|
}
|
|
575
|
-
let
|
|
576
|
+
let f;
|
|
576
577
|
try {
|
|
577
|
-
|
|
578
|
+
f = await this.config.ethWallet.sendTransaction({
|
|
578
579
|
to: this.config.vaultContracts.btcVaultRegistry,
|
|
579
580
|
data: P,
|
|
580
|
-
value:
|
|
581
|
+
value: m,
|
|
581
582
|
account: this.config.ethWallet.account,
|
|
582
583
|
chain: this.config.ethChain,
|
|
583
|
-
gas:
|
|
584
|
+
gas: k
|
|
584
585
|
});
|
|
585
|
-
} catch (
|
|
586
|
-
A(
|
|
586
|
+
} catch (d) {
|
|
587
|
+
A(d);
|
|
587
588
|
}
|
|
588
|
-
const
|
|
589
|
-
hash:
|
|
590
|
-
timeout:
|
|
589
|
+
const u = await c.waitForTransactionReceipt({
|
|
590
|
+
hash: f,
|
|
591
|
+
timeout: yt
|
|
591
592
|
});
|
|
592
|
-
return
|
|
593
|
+
return u.status === "reverted" && A(
|
|
593
594
|
new Error(
|
|
594
|
-
`Batch transaction reverted. Hash: ${
|
|
595
|
+
`Batch transaction reverted. Hash: ${f}. Check the transaction on block explorer for details.`
|
|
595
596
|
)
|
|
596
597
|
), {
|
|
597
|
-
ethTxHash:
|
|
598
|
-
vaults:
|
|
598
|
+
ethTxHash: u.transactionHash,
|
|
599
|
+
vaults: h
|
|
599
600
|
};
|
|
600
601
|
}
|
|
601
602
|
/**
|
|
@@ -606,15 +607,15 @@ class Nn {
|
|
|
606
607
|
*/
|
|
607
608
|
async checkVaultExists(t) {
|
|
608
609
|
try {
|
|
609
|
-
return (await
|
|
610
|
+
return (await j({
|
|
610
611
|
chain: this.config.ethChain,
|
|
611
|
-
transport:
|
|
612
|
+
transport: q()
|
|
612
613
|
}).readContract({
|
|
613
614
|
address: this.config.vaultContracts.btcVaultRegistry,
|
|
614
|
-
abi:
|
|
615
|
+
abi: S,
|
|
615
616
|
functionName: "getBtcVaultBasicInfo",
|
|
616
617
|
args: [t]
|
|
617
|
-
}))[0] !==
|
|
618
|
+
}))[0] !== te;
|
|
618
619
|
} catch {
|
|
619
620
|
return !1;
|
|
620
621
|
}
|
|
@@ -633,7 +634,7 @@ class Nn {
|
|
|
633
634
|
else {
|
|
634
635
|
n = await this.config.btcWallet.getAddress();
|
|
635
636
|
const o = await this.config.btcWallet.getPublicKeyHex();
|
|
636
|
-
if (!
|
|
637
|
+
if (!ae(
|
|
637
638
|
n,
|
|
638
639
|
o,
|
|
639
640
|
this.config.btcNetwork
|
|
@@ -642,9 +643,9 @@ class Nn {
|
|
|
642
643
|
"The BTC address from your wallet does not match the wallet's public key. Please ensure your wallet is using a supported address type (Taproot or Native SegWit)."
|
|
643
644
|
);
|
|
644
645
|
}
|
|
645
|
-
const s =
|
|
646
|
+
const s = mt(this.config.btcNetwork);
|
|
646
647
|
try {
|
|
647
|
-
return `0x${
|
|
648
|
+
return `0x${Jt.address.toOutputScript(n, s).toString("hex")}`;
|
|
648
649
|
} catch {
|
|
649
650
|
throw new Error(
|
|
650
651
|
`Invalid BTC payout address: "${n}". Please provide a valid Bitcoin address for the ${this.config.btcNetwork} network.`
|
|
@@ -660,22 +661,22 @@ class Nn {
|
|
|
660
661
|
async signProofOfPossession() {
|
|
661
662
|
if (!this.config.ethWallet.account)
|
|
662
663
|
throw new Error("Ethereum wallet account not found");
|
|
663
|
-
const t = this.config.ethWallet.account.address, n =
|
|
664
|
+
const t = this.config.ethWallet.account.address, n = O(
|
|
664
665
|
await this.config.btcWallet.getPublicKeyHex()
|
|
665
666
|
), s = this.config.vaultContracts.btcVaultRegistry, o = `${t.toLowerCase()}:${this.config.ethChain.id}:pegin:${s.toLowerCase()}`, r = await this.config.btcWallet.signMessage(
|
|
666
667
|
o,
|
|
667
668
|
"bip322-simple"
|
|
668
669
|
);
|
|
669
670
|
return {
|
|
670
|
-
btcPopSignature:
|
|
671
|
+
btcPopSignature: ke(r),
|
|
671
672
|
depositorEthAddress: t,
|
|
672
673
|
depositorBtcPubkey: n
|
|
673
674
|
};
|
|
674
675
|
}
|
|
675
676
|
async assertPopMatchesBtcWallet(t) {
|
|
676
|
-
const n =
|
|
677
|
+
const n = O(
|
|
677
678
|
await this.config.btcWallet.getPublicKeyHex()
|
|
678
|
-
), s =
|
|
679
|
+
), s = O(t.depositorBtcPubkey);
|
|
679
680
|
if (n !== s)
|
|
680
681
|
throw new Error(
|
|
681
682
|
`Proof of possession was signed with BTC pubkey ${s} but the BTC wallet is currently connected to ${n}. Reconnect the original wallet or call signProofOfPossession() again.`
|
|
@@ -698,51 +699,20 @@ class Nn {
|
|
|
698
699
|
return this.config.vaultContracts.btcVaultRegistry;
|
|
699
700
|
}
|
|
700
701
|
}
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen;
|
|
712
|
-
const s = this.blockLen, o = new Uint8Array(s);
|
|
713
|
-
o.set(n.length > s ? t.create().update(n).digest() : n);
|
|
714
|
-
for (let r = 0; r < o.length; r++)
|
|
715
|
-
o[r] ^= 54;
|
|
716
|
-
this.iHash.update(o), this.oHash = t.create();
|
|
717
|
-
for (let r = 0; r < o.length; r++)
|
|
718
|
-
o[r] ^= 106;
|
|
719
|
-
this.oHash.update(o), S(o);
|
|
720
|
-
}
|
|
721
|
-
update(t) {
|
|
722
|
-
return V(this), this.iHash.update(t), this;
|
|
723
|
-
}
|
|
724
|
-
digestInto(t) {
|
|
725
|
-
V(this), U(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy();
|
|
726
|
-
}
|
|
727
|
-
digest() {
|
|
728
|
-
const t = new Uint8Array(this.oHash.outputLen);
|
|
729
|
-
return this.digestInto(t), t;
|
|
730
|
-
}
|
|
731
|
-
_cloneInto(t) {
|
|
732
|
-
t || (t = Object.create(Object.getPrototypeOf(this), {}));
|
|
733
|
-
const { oHash: n, iHash: s, finished: o, destroyed: r, blockLen: i, outputLen: a } = this;
|
|
734
|
-
return t = t, t.finished = o, t.destroyed = r, t.blockLen = i, t.outputLen = a, t.oHash = n._cloneInto(t.oHash), t.iHash = s._cloneInto(t.iHash), t;
|
|
735
|
-
}
|
|
736
|
-
clone() {
|
|
737
|
-
return this._cloneInto();
|
|
738
|
-
}
|
|
739
|
-
destroy() {
|
|
740
|
-
this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy();
|
|
702
|
+
const M = /* @__PURE__ */ BigInt(2 ** 32 - 1), xt = /* @__PURE__ */ BigInt(32);
|
|
703
|
+
function Ie(e, t = !1) {
|
|
704
|
+
return t ? { h: Number(e & M), l: Number(e >> xt & M) } : { h: Number(e >> xt & M) | 0, l: Number(e & M) | 0 };
|
|
705
|
+
}
|
|
706
|
+
function ve(e, t = !1) {
|
|
707
|
+
const n = e.length;
|
|
708
|
+
let s = new Uint32Array(n), o = new Uint32Array(n);
|
|
709
|
+
for (let r = 0; r < n; r++) {
|
|
710
|
+
const { h: i, l: a } = Ie(e[r], t);
|
|
711
|
+
[s[r], o[r]] = [i, a];
|
|
741
712
|
}
|
|
713
|
+
return [s, o];
|
|
742
714
|
}
|
|
743
|
-
const
|
|
744
|
-
K.create = (e, t) => new Ft(e, t);
|
|
745
|
-
const Le = /* @__PURE__ */ Uint8Array.from([
|
|
715
|
+
const Be = (e, t, n) => e << n | t >>> 32 - n, Ae = (e, t, n) => t << n | e >>> 32 - n, He = (e, t, n) => t << n - 32 | e >>> 64 - n, Ce = (e, t, n) => e << n - 32 | t >>> 64 - n, Se = /* @__PURE__ */ Uint8Array.from([
|
|
746
716
|
7,
|
|
747
717
|
4,
|
|
748
718
|
13,
|
|
@@ -759,43 +729,43 @@ const Le = /* @__PURE__ */ Uint8Array.from([
|
|
|
759
729
|
14,
|
|
760
730
|
11,
|
|
761
731
|
8
|
|
762
|
-
]),
|
|
763
|
-
const n = [[
|
|
732
|
+
]), $t = Uint8Array.from(new Array(16).fill(0).map((e, t) => t)), Oe = $t.map((e) => (9 * e + 5) % 16), Rt = /* @__PURE__ */ (() => {
|
|
733
|
+
const n = [[$t], [Oe]];
|
|
764
734
|
for (let s = 0; s < 4; s++)
|
|
765
735
|
for (let o of n)
|
|
766
|
-
o.push(o[s].map((r) =>
|
|
736
|
+
o.push(o[s].map((r) => Se[r]));
|
|
767
737
|
return n;
|
|
768
|
-
})(),
|
|
738
|
+
})(), Ut = Rt[0], Lt = Rt[1], Wt = /* @__PURE__ */ [
|
|
769
739
|
[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
|
|
770
740
|
[12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
|
|
771
741
|
[13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
|
|
772
742
|
[14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
|
|
773
743
|
[15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5]
|
|
774
|
-
].map((e) => Uint8Array.from(e)),
|
|
744
|
+
].map((e) => Uint8Array.from(e)), $e = /* @__PURE__ */ Ut.map((e, t) => e.map((n) => Wt[t][n])), Re = /* @__PURE__ */ Lt.map((e, t) => e.map((n) => Wt[t][n])), Ue = /* @__PURE__ */ Uint32Array.from([
|
|
775
745
|
0,
|
|
776
746
|
1518500249,
|
|
777
747
|
1859775393,
|
|
778
748
|
2400959708,
|
|
779
749
|
2840853838
|
|
780
|
-
]),
|
|
750
|
+
]), Le = /* @__PURE__ */ Uint32Array.from([
|
|
781
751
|
1352829926,
|
|
782
752
|
1548603684,
|
|
783
753
|
1836072691,
|
|
784
754
|
2053994217,
|
|
785
755
|
0
|
|
786
756
|
]);
|
|
787
|
-
function
|
|
757
|
+
function Pt(e, t, n, s) {
|
|
788
758
|
return e === 0 ? t ^ n ^ s : e === 1 ? t & n | ~t & s : e === 2 ? (t | ~n) ^ s : e === 3 ? t & s | n & ~s : t ^ (n | ~s);
|
|
789
759
|
}
|
|
790
|
-
const
|
|
791
|
-
class
|
|
760
|
+
const N = /* @__PURE__ */ new Uint32Array(16);
|
|
761
|
+
class We extends be {
|
|
792
762
|
constructor() {
|
|
793
763
|
super(64, 20, 8, !0);
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
764
|
+
p(this, "h0", 1732584193);
|
|
765
|
+
p(this, "h1", -271733879);
|
|
766
|
+
p(this, "h2", -1732584194);
|
|
767
|
+
p(this, "h3", 271733878);
|
|
768
|
+
p(this, "h4", -1009589776);
|
|
799
769
|
}
|
|
800
770
|
get() {
|
|
801
771
|
const { h0: n, h1: s, h2: o, h3: r, h4: i } = this;
|
|
@@ -805,52 +775,52 @@ class Ne extends be {
|
|
|
805
775
|
this.h0 = n | 0, this.h1 = s | 0, this.h2 = o | 0, this.h3 = r | 0, this.h4 = i | 0;
|
|
806
776
|
}
|
|
807
777
|
process(n, s) {
|
|
808
|
-
for (let
|
|
809
|
-
|
|
810
|
-
let o = this.h0 | 0, r = o, i = this.h1 | 0, a = i,
|
|
811
|
-
for (let
|
|
812
|
-
const
|
|
813
|
-
for (let
|
|
814
|
-
const
|
|
815
|
-
o =
|
|
778
|
+
for (let g = 0; g < 16; g++, s += 4)
|
|
779
|
+
N[g] = n.getUint32(s, !0);
|
|
780
|
+
let o = this.h0 | 0, r = o, i = this.h1 | 0, a = i, l = this.h2 | 0, h = l, c = this.h3 | 0, b = c, m = this.h4 | 0, w = m;
|
|
781
|
+
for (let g = 0; g < 5; g++) {
|
|
782
|
+
const E = 4 - g, P = Ue[g], k = Le[g], f = Ut[g], u = Lt[g], d = $e[g], x = Re[g];
|
|
783
|
+
for (let y = 0; y < 16; y++) {
|
|
784
|
+
const I = W(o + Pt(g, i, l, c) + N[f[y]] + P, d[y]) + m | 0;
|
|
785
|
+
o = m, m = c, c = W(l, 10) | 0, l = i, i = I;
|
|
816
786
|
}
|
|
817
|
-
for (let
|
|
818
|
-
const
|
|
819
|
-
r =
|
|
787
|
+
for (let y = 0; y < 16; y++) {
|
|
788
|
+
const I = W(r + Pt(E, a, h, b) + N[u[y]] + k, x[y]) + w | 0;
|
|
789
|
+
r = w, w = b, b = W(h, 10) | 0, h = a, a = I;
|
|
820
790
|
}
|
|
821
791
|
}
|
|
822
|
-
this.set(this.h1 +
|
|
792
|
+
this.set(this.h1 + l + b | 0, this.h2 + c + w | 0, this.h3 + m + r | 0, this.h4 + o + a | 0, this.h0 + i + h | 0);
|
|
823
793
|
}
|
|
824
794
|
roundClean() {
|
|
825
|
-
|
|
795
|
+
H(N);
|
|
826
796
|
}
|
|
827
797
|
destroy() {
|
|
828
|
-
this.destroyed = !0,
|
|
798
|
+
this.destroyed = !0, H(this.buffer), this.set(0, 0, 0, 0, 0);
|
|
829
799
|
}
|
|
830
800
|
}
|
|
831
|
-
const
|
|
832
|
-
for (let e = 0, t =
|
|
833
|
-
[n, s] = [s, (2 * n + 3 * s) % 5],
|
|
834
|
-
let o =
|
|
801
|
+
const Me = /* @__PURE__ */ St(() => new We()), Ne = BigInt(0), $ = BigInt(1), Ve = BigInt(2), Fe = BigInt(7), Xe = BigInt(256), De = BigInt(113), Mt = [], Nt = [], Vt = [];
|
|
802
|
+
for (let e = 0, t = $, n = 1, s = 0; e < 24; e++) {
|
|
803
|
+
[n, s] = [s, (2 * n + 3 * s) % 5], Mt.push(2 * (5 * s + n)), Nt.push((e + 1) * (e + 2) / 2 % 64);
|
|
804
|
+
let o = Ne;
|
|
835
805
|
for (let r = 0; r < 7; r++)
|
|
836
|
-
t = (t <<
|
|
837
|
-
|
|
806
|
+
t = (t << $ ^ (t >> Fe) * De) % Xe, t & Ve && (o ^= $ << ($ << BigInt(r)) - $);
|
|
807
|
+
Vt.push(o);
|
|
838
808
|
}
|
|
839
|
-
const
|
|
840
|
-
function
|
|
809
|
+
const Ft = ve(Vt, !0), Ke = Ft[0], je = Ft[1], Et = (e, t, n) => n > 32 ? He(e, t, n) : Be(e, t, n), Tt = (e, t, n) => n > 32 ? Ce(e, t, n) : Ae(e, t, n);
|
|
810
|
+
function qe(e, t = 24) {
|
|
841
811
|
const n = new Uint32Array(10);
|
|
842
812
|
for (let s = 24 - t; s < 24; s++) {
|
|
843
813
|
for (let i = 0; i < 10; i++)
|
|
844
814
|
n[i] = e[i] ^ e[i + 10] ^ e[i + 20] ^ e[i + 30] ^ e[i + 40];
|
|
845
815
|
for (let i = 0; i < 10; i += 2) {
|
|
846
|
-
const a = (i + 8) % 10,
|
|
847
|
-
for (let
|
|
848
|
-
e[i +
|
|
816
|
+
const a = (i + 8) % 10, l = (i + 2) % 10, h = n[l], c = n[l + 1], b = Et(h, c, 1) ^ n[a], m = Tt(h, c, 1) ^ n[a + 1];
|
|
817
|
+
for (let w = 0; w < 50; w += 10)
|
|
818
|
+
e[i + w] ^= b, e[i + w + 1] ^= m;
|
|
849
819
|
}
|
|
850
820
|
let o = e[2], r = e[3];
|
|
851
821
|
for (let i = 0; i < 24; i++) {
|
|
852
|
-
const a =
|
|
853
|
-
o = e[c], r = e[c + 1], e[c] =
|
|
822
|
+
const a = Nt[i], l = Et(o, r, a), h = Tt(o, r, a), c = Mt[i];
|
|
823
|
+
o = e[c], r = e[c + 1], e[c] = l, e[c + 1] = h;
|
|
854
824
|
}
|
|
855
825
|
for (let i = 0; i < 50; i += 10) {
|
|
856
826
|
for (let a = 0; a < 10; a++)
|
|
@@ -858,36 +828,36 @@ function ze(e, t = 24) {
|
|
|
858
828
|
for (let a = 0; a < 10; a++)
|
|
859
829
|
e[i + a] ^= ~n[(a + 2) % 10] & n[(a + 4) % 10];
|
|
860
830
|
}
|
|
861
|
-
e[0] ^=
|
|
831
|
+
e[0] ^= Ke[s], e[1] ^= je[s];
|
|
862
832
|
}
|
|
863
|
-
|
|
833
|
+
H(n);
|
|
864
834
|
}
|
|
865
|
-
class
|
|
835
|
+
class at {
|
|
866
836
|
// NOTE: we accept arguments in bytes instead of bits here.
|
|
867
837
|
constructor(t, n, s, o = !1, r = 24) {
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
if (this.blockLen = t, this.suffix = n, this.outputLen = s, this.enableXOF = o, this.rounds = r,
|
|
838
|
+
p(this, "state");
|
|
839
|
+
p(this, "pos", 0);
|
|
840
|
+
p(this, "posOut", 0);
|
|
841
|
+
p(this, "finished", !1);
|
|
842
|
+
p(this, "state32");
|
|
843
|
+
p(this, "destroyed", !1);
|
|
844
|
+
p(this, "blockLen");
|
|
845
|
+
p(this, "suffix");
|
|
846
|
+
p(this, "outputLen");
|
|
847
|
+
p(this, "enableXOF", !1);
|
|
848
|
+
p(this, "rounds");
|
|
849
|
+
if (this.blockLen = t, this.suffix = n, this.outputLen = s, this.enableXOF = o, this.rounds = r, nt(s, "outputLen"), !(0 < t && t < 200))
|
|
880
850
|
throw new Error("only keccak-f1600 function is supported");
|
|
881
|
-
this.state = new Uint8Array(200), this.state32 =
|
|
851
|
+
this.state = new Uint8Array(200), this.state32 = we(this.state);
|
|
882
852
|
}
|
|
883
853
|
clone() {
|
|
884
854
|
return this._cloneInto();
|
|
885
855
|
}
|
|
886
856
|
keccak() {
|
|
887
|
-
|
|
857
|
+
wt(this.state32), qe(this.state32, this.rounds), wt(this.state32), this.posOut = 0, this.pos = 0;
|
|
888
858
|
}
|
|
889
859
|
update(t) {
|
|
890
|
-
|
|
860
|
+
F(this), U(t);
|
|
891
861
|
const { blockLen: n, state: s } = this, o = t.length;
|
|
892
862
|
for (let r = 0; r < o; ) {
|
|
893
863
|
const i = Math.min(n - this.pos, o - r);
|
|
@@ -905,7 +875,7 @@ class ft {
|
|
|
905
875
|
t[s] ^= n, (n & 128) !== 0 && s === o - 1 && this.keccak(), t[o - 1] ^= 128, this.keccak();
|
|
906
876
|
}
|
|
907
877
|
writeInto(t) {
|
|
908
|
-
|
|
878
|
+
F(this, !1), U(t), this.finish();
|
|
909
879
|
const n = this.state, { blockLen: s } = this;
|
|
910
880
|
for (let o = 0, r = t.length; o < r; ) {
|
|
911
881
|
this.posOut >= s && this.keccak();
|
|
@@ -920,10 +890,10 @@ class ft {
|
|
|
920
890
|
return this.writeInto(t);
|
|
921
891
|
}
|
|
922
892
|
xof(t) {
|
|
923
|
-
return
|
|
893
|
+
return nt(t), this.xofInto(new Uint8Array(t));
|
|
924
894
|
}
|
|
925
895
|
digestInto(t) {
|
|
926
|
-
if (
|
|
896
|
+
if (ye(t, this), this.finished)
|
|
927
897
|
throw new Error("digest() was already called");
|
|
928
898
|
return this.writeInto(t), this.destroy(), t;
|
|
929
899
|
}
|
|
@@ -931,93 +901,30 @@ class ft {
|
|
|
931
901
|
return this.digestInto(new Uint8Array(this.outputLen));
|
|
932
902
|
}
|
|
933
903
|
destroy() {
|
|
934
|
-
this.destroyed = !0,
|
|
904
|
+
this.destroyed = !0, H(this.state);
|
|
935
905
|
}
|
|
936
906
|
_cloneInto(t) {
|
|
937
907
|
const { blockLen: n, suffix: s, outputLen: o, rounds: r, enableXOF: i } = this;
|
|
938
|
-
return t || (t = new
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
const Ye = (e, t, n, s = {}) => Ut(() => new ft(t, e, n), s), Je = /* @__PURE__ */ Ye(1, 136, 32);
|
|
942
|
-
function Qe(e, t, n, s) {
|
|
943
|
-
ht(e);
|
|
944
|
-
const o = Be({ dkLen: 32, asyncTick: 10 }, s), { c: r, dkLen: i, asyncTick: a } = o;
|
|
945
|
-
if (C(r, "c"), C(i, "dkLen"), C(a, "asyncTick"), r < 1)
|
|
946
|
-
throw new Error("iterations (c) must be >= 1");
|
|
947
|
-
const h = It(t, "password"), u = It(n, "salt"), c = new Uint8Array(i), m = K.create(e, h), l = m._cloneInto().update(u);
|
|
948
|
-
return { c: r, dkLen: i, asyncTick: a, DK: c, PRF: m, PRFSalt: l };
|
|
949
|
-
}
|
|
950
|
-
function tn(e, t, n, s, o) {
|
|
951
|
-
return e.destroy(), t.destroy(), s && s.destroy(), S(o), n;
|
|
952
|
-
}
|
|
953
|
-
function en(e, t, n, s) {
|
|
954
|
-
const { c: o, dkLen: r, DK: i, PRF: a, PRFSalt: h } = Qe(e, t, n, s);
|
|
955
|
-
let u;
|
|
956
|
-
const c = new Uint8Array(4), m = ve(c), l = new Uint8Array(a.outputLen);
|
|
957
|
-
for (let y = 1, f = 0; f < r; y++, f += a.outputLen) {
|
|
958
|
-
const x = i.subarray(f, f + a.outputLen);
|
|
959
|
-
m.setInt32(0, y, !1), (u = h._cloneInto(u)).update(c).digestInto(l), x.set(l.subarray(0, x.length));
|
|
960
|
-
for (let P = 1; P < o; P++) {
|
|
961
|
-
a._cloneInto(u).update(l).digestInto(l);
|
|
962
|
-
for (let w = 0; w < x.length; w++)
|
|
963
|
-
x[w] ^= l[w];
|
|
964
|
-
}
|
|
908
|
+
return t || (t = new at(n, s, o, i, r)), t.state32.set(this.state32), t.pos = this.pos, t.posOut = this.posOut, t.finished = this.finished, t.rounds = r, t.suffix = s, t.outputLen = o, t.enableXOF = i, t.destroyed = this.destroyed, t;
|
|
965
909
|
}
|
|
966
|
-
return tn(a, h, i, u, l);
|
|
967
910
|
}
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
return e.normalize("NFKD");
|
|
911
|
+
const Ge = (e, t, n, s = {}) => St(() => new at(t, e, n), s), Ze = /* @__PURE__ */ Ge(1, 136, 32), kt = 64, T = 20, st = 4, Xt = 2, ze = 0, Ye = 1, V = [64, 64], Je = (e) => Array.from(e).map((t) => t.toString(16).padStart(2, "0")).join("");
|
|
912
|
+
function ct(e) {
|
|
913
|
+
return Me(L(e));
|
|
972
914
|
}
|
|
973
|
-
function
|
|
974
|
-
const t = Gt(e), n = t.split(" ");
|
|
975
|
-
if (![12, 15, 18, 21, 24].includes(n.length))
|
|
976
|
-
throw new Error("Invalid mnemonic");
|
|
977
|
-
return { nfkd: t, words: n };
|
|
978
|
-
}
|
|
979
|
-
const sn = (e) => Gt("mnemonic" + e);
|
|
980
|
-
function on(e, t = "") {
|
|
981
|
-
return en(Wt, nn(e).nfkd, sn(t), { c: 2048, dkLen: 64 });
|
|
982
|
-
}
|
|
983
|
-
const z = 32, Y = 64, rn = 4, T = 20, at = 4, zt = 2, an = 0, cn = 1, N = [64, 64];
|
|
984
|
-
function ct(...e) {
|
|
985
|
-
const t = e.reduce((o, r) => o + r.length, 0), n = new Uint8Array(t);
|
|
986
|
-
let s = 0;
|
|
987
|
-
for (const o of e)
|
|
988
|
-
n.set(o, s), s += o.length;
|
|
989
|
-
return n;
|
|
990
|
-
}
|
|
991
|
-
function J(e) {
|
|
992
|
-
return new TextEncoder().encode(e);
|
|
993
|
-
}
|
|
994
|
-
function Q(e) {
|
|
995
|
-
const t = new Uint8Array(rn);
|
|
996
|
-
return new DataView(t.buffer).setUint32(0, e.length, !1), ct(t, e);
|
|
997
|
-
}
|
|
998
|
-
function At(e) {
|
|
999
|
-
return e.startsWith("0x") || e.startsWith("0X") ? e.slice(2) : e;
|
|
1000
|
-
}
|
|
1001
|
-
const ln = (e) => Array.from(e).map((t) => t.toString(16).padStart(2, "0")).join("");
|
|
1002
|
-
function un(e, t) {
|
|
1003
|
-
return K(Wt, e, t);
|
|
1004
|
-
}
|
|
1005
|
-
function gt(e) {
|
|
1006
|
-
return Ve(W(e));
|
|
1007
|
-
}
|
|
1008
|
-
function Yt(e) {
|
|
915
|
+
function Dt(e) {
|
|
1009
916
|
return (1 << e) - 1;
|
|
1010
917
|
}
|
|
1011
|
-
function
|
|
918
|
+
function Qe(e) {
|
|
1012
919
|
let t = 1;
|
|
1013
920
|
for (; t * t < e + 1; ) t++;
|
|
1014
921
|
return Math.max(t, 2);
|
|
1015
922
|
}
|
|
1016
|
-
function
|
|
1017
|
-
const t =
|
|
1018
|
-
return { d: t, n: e, checksum_radix:
|
|
923
|
+
function tn(e) {
|
|
924
|
+
const t = st, n = e * Dt(t);
|
|
925
|
+
return { d: t, n: e, checksum_radix: Qe(n) };
|
|
1019
926
|
}
|
|
1020
|
-
function
|
|
927
|
+
function Z(e, t) {
|
|
1021
928
|
const n = [];
|
|
1022
929
|
let s = t;
|
|
1023
930
|
for (; s > 0; )
|
|
@@ -1026,93 +933,93 @@ function tt(e, t) {
|
|
|
1026
933
|
o.set(e);
|
|
1027
934
|
for (let r = 0; r < n.length; r++)
|
|
1028
935
|
o[e.length + r] = n[r];
|
|
1029
|
-
return
|
|
936
|
+
return ct(o);
|
|
1030
937
|
}
|
|
1031
|
-
function
|
|
938
|
+
function z(e, t) {
|
|
1032
939
|
let n = e;
|
|
1033
940
|
for (let s = 0; s < t; s++)
|
|
1034
|
-
n =
|
|
941
|
+
n = ct(n);
|
|
1035
942
|
return n;
|
|
1036
943
|
}
|
|
1037
|
-
function
|
|
1038
|
-
const n =
|
|
944
|
+
function en(e, t) {
|
|
945
|
+
const n = Dt(t.d), s = t.checksum_radix - 1, o = Math.floor(t.n * n / t.checksum_radix), r = [];
|
|
1039
946
|
for (let c = 0; c < t.n; c++) {
|
|
1040
|
-
const
|
|
1041
|
-
r.push(Array.from(
|
|
947
|
+
const b = Z(e, c + Xt), m = z(b, n);
|
|
948
|
+
r.push(Array.from(m));
|
|
1042
949
|
}
|
|
1043
|
-
const i =
|
|
950
|
+
const i = Z(
|
|
1044
951
|
e,
|
|
1045
|
-
|
|
1046
|
-
), a =
|
|
952
|
+
ze
|
|
953
|
+
), a = z(
|
|
1047
954
|
i,
|
|
1048
955
|
s
|
|
1049
|
-
),
|
|
956
|
+
), l = Z(
|
|
1050
957
|
e,
|
|
1051
|
-
|
|
1052
|
-
),
|
|
1053
|
-
|
|
958
|
+
Ye
|
|
959
|
+
), h = z(
|
|
960
|
+
l,
|
|
1054
961
|
o
|
|
1055
962
|
);
|
|
1056
963
|
return {
|
|
1057
964
|
config: t,
|
|
1058
965
|
message_terminals: r,
|
|
1059
|
-
checksum_major_terminal: Array.from(
|
|
966
|
+
checksum_major_terminal: Array.from(h),
|
|
1060
967
|
checksum_minor_terminal: Array.from(a)
|
|
1061
968
|
};
|
|
1062
969
|
}
|
|
1063
|
-
function
|
|
1064
|
-
const t = on(e), n = new Uint8Array(t);
|
|
1065
|
-
return t.fill(0), n;
|
|
1066
|
-
}
|
|
1067
|
-
async function pn(e, t, n, s) {
|
|
1068
|
-
if (e.length !== Y)
|
|
1069
|
-
throw new Error(
|
|
1070
|
-
`WOTS seed must be exactly ${Y} bytes, got ${e.length}`
|
|
1071
|
-
);
|
|
1072
|
-
const o = At(t).toLowerCase(), r = At(n).toLowerCase(), i = e.slice(z, Y), a = e.slice(0, z), h = ct(
|
|
1073
|
-
a,
|
|
1074
|
-
ct(
|
|
1075
|
-
Q(J(o)),
|
|
1076
|
-
Q(J(r)),
|
|
1077
|
-
Q(J(s.toLowerCase()))
|
|
1078
|
-
)
|
|
1079
|
-
), u = un(i, h), c = u.slice(0, z);
|
|
970
|
+
async function Sn(e) {
|
|
1080
971
|
try {
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
972
|
+
if (e.length !== kt)
|
|
973
|
+
throw new Error(
|
|
974
|
+
`WOTS seed must be exactly ${kt} bytes, got ${e.length}`
|
|
975
|
+
);
|
|
976
|
+
const t = [];
|
|
977
|
+
for (let n = 0; n < V.length; n++) {
|
|
978
|
+
const s = V[n], o = tn(s), r = new Uint8Array(e.length + 1);
|
|
979
|
+
r.set(e), r[e.length] = n;
|
|
980
|
+
const i = ct(r);
|
|
1086
981
|
try {
|
|
1087
|
-
const
|
|
1088
|
-
if (
|
|
1089
|
-
throw new Error(
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
if (
|
|
1093
|
-
throw new Error(
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
982
|
+
const a = en(i, o);
|
|
983
|
+
if (a.config.d !== st)
|
|
984
|
+
throw new Error(
|
|
985
|
+
`Block ${n}: expected d=${st}, got d=${a.config.d}`
|
|
986
|
+
);
|
|
987
|
+
if (a.config.n !== s)
|
|
988
|
+
throw new Error(
|
|
989
|
+
`Block ${n}: expected n=${s}, got n=${a.config.n}`
|
|
990
|
+
);
|
|
991
|
+
if (a.message_terminals.length !== s)
|
|
992
|
+
throw new Error(
|
|
993
|
+
`Block ${n}: expected ${s} message terminals, got ${a.message_terminals.length}`
|
|
994
|
+
);
|
|
995
|
+
for (let l = 0; l < a.message_terminals.length; l++)
|
|
996
|
+
if (a.message_terminals[l].length !== T)
|
|
997
|
+
throw new Error(
|
|
998
|
+
`Block ${n} terminal ${l}: expected ${T} bytes, got ${a.message_terminals[l].length}`
|
|
999
|
+
);
|
|
1000
|
+
if (a.checksum_minor_terminal.length !== T)
|
|
1001
|
+
throw new Error(
|
|
1002
|
+
`Block ${n} checksum_minor: expected ${T} bytes`
|
|
1003
|
+
);
|
|
1004
|
+
if (a.checksum_major_terminal.length !== T)
|
|
1005
|
+
throw new Error(
|
|
1006
|
+
`Block ${n} checksum_major: expected ${T} bytes`
|
|
1007
|
+
);
|
|
1008
|
+
t.push(a);
|
|
1102
1009
|
} finally {
|
|
1103
|
-
|
|
1010
|
+
r.fill(0), i.fill(0);
|
|
1104
1011
|
}
|
|
1105
1012
|
}
|
|
1106
|
-
if (
|
|
1013
|
+
if (t.length !== V.length)
|
|
1107
1014
|
throw new Error(
|
|
1108
|
-
`Expected ${
|
|
1015
|
+
`Expected ${V.length} blocks, got ${t.length}`
|
|
1109
1016
|
);
|
|
1110
|
-
return
|
|
1017
|
+
return t;
|
|
1111
1018
|
} finally {
|
|
1112
|
-
|
|
1019
|
+
e.fill(0);
|
|
1113
1020
|
}
|
|
1114
1021
|
}
|
|
1115
|
-
function
|
|
1022
|
+
function Y(e, t, n) {
|
|
1116
1023
|
if (e.length !== T)
|
|
1117
1024
|
throw new Error(
|
|
1118
1025
|
`Block ${t} ${n}: expected ${T} bytes, got ${e.length}`
|
|
@@ -1125,18 +1032,18 @@ function nt(e, t, n) {
|
|
|
1125
1032
|
);
|
|
1126
1033
|
}
|
|
1127
1034
|
}
|
|
1128
|
-
function
|
|
1035
|
+
function On(e) {
|
|
1129
1036
|
if (e.length === 0)
|
|
1130
1037
|
throw new Error("Public keys array must not be empty");
|
|
1131
1038
|
for (let r = 0; r < e.length; r++) {
|
|
1132
1039
|
const i = e[r];
|
|
1133
|
-
|
|
1040
|
+
Y(i.checksum_minor_terminal, r, "checksum_minor_terminal"), Y(i.checksum_major_terminal, r, "checksum_major_terminal");
|
|
1134
1041
|
for (let a = 0; a < i.message_terminals.length; a++)
|
|
1135
|
-
|
|
1042
|
+
Y(i.message_terminals[a], r, `message_terminal[${a}]`);
|
|
1136
1043
|
}
|
|
1137
1044
|
let t = 0;
|
|
1138
1045
|
for (const r of e)
|
|
1139
|
-
t +=
|
|
1046
|
+
t += Xt + r.message_terminals.length;
|
|
1140
1047
|
const n = new Uint8Array(t * T);
|
|
1141
1048
|
let s = 0;
|
|
1142
1049
|
for (const r of e) {
|
|
@@ -1144,154 +1051,201 @@ function mn(e) {
|
|
|
1144
1051
|
for (const i of r.message_terminals)
|
|
1145
1052
|
n.set(i, s), s += T;
|
|
1146
1053
|
}
|
|
1147
|
-
const o =
|
|
1148
|
-
return `0x${
|
|
1149
|
-
}
|
|
1150
|
-
async function Vn(e, t, n, s) {
|
|
1151
|
-
const o = gn(e);
|
|
1152
|
-
try {
|
|
1153
|
-
const r = await pn(
|
|
1154
|
-
o,
|
|
1155
|
-
t,
|
|
1156
|
-
n,
|
|
1157
|
-
s
|
|
1158
|
-
);
|
|
1159
|
-
return mn(r);
|
|
1160
|
-
} finally {
|
|
1161
|
-
o.fill(0);
|
|
1162
|
-
}
|
|
1054
|
+
const o = Ze(n);
|
|
1055
|
+
return `0x${Je(o)}`;
|
|
1163
1056
|
}
|
|
1164
|
-
function
|
|
1057
|
+
function $n(e) {
|
|
1165
1058
|
const t = (e instanceof Error ? e.message : typeof e == "string" ? e : "").toLowerCase();
|
|
1166
1059
|
return t.includes("wots") && t.includes("hash") && t.includes("does not match");
|
|
1167
1060
|
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1061
|
+
class Kt {
|
|
1062
|
+
constructor(t, n) {
|
|
1063
|
+
p(this, "oHash");
|
|
1064
|
+
p(this, "iHash");
|
|
1065
|
+
p(this, "blockLen");
|
|
1066
|
+
p(this, "outputLen");
|
|
1067
|
+
p(this, "finished", !1);
|
|
1068
|
+
p(this, "destroyed", !1);
|
|
1069
|
+
if (Ot(t), U(n, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function")
|
|
1070
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
|
1071
|
+
this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen;
|
|
1072
|
+
const s = this.blockLen, o = new Uint8Array(s);
|
|
1073
|
+
o.set(n.length > s ? t.create().update(n).digest() : n);
|
|
1074
|
+
for (let r = 0; r < o.length; r++)
|
|
1075
|
+
o[r] ^= 54;
|
|
1076
|
+
this.iHash.update(o), this.oHash = t.create();
|
|
1077
|
+
for (let r = 0; r < o.length; r++)
|
|
1078
|
+
o[r] ^= 106;
|
|
1079
|
+
this.oHash.update(o), H(o);
|
|
1080
|
+
}
|
|
1081
|
+
update(t) {
|
|
1082
|
+
return F(this), this.iHash.update(t), this;
|
|
1083
|
+
}
|
|
1084
|
+
digestInto(t) {
|
|
1085
|
+
F(this), U(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy();
|
|
1086
|
+
}
|
|
1087
|
+
digest() {
|
|
1088
|
+
const t = new Uint8Array(this.oHash.outputLen);
|
|
1089
|
+
return this.digestInto(t), t;
|
|
1090
|
+
}
|
|
1091
|
+
_cloneInto(t) {
|
|
1092
|
+
t || (t = Object.create(Object.getPrototypeOf(this), {}));
|
|
1093
|
+
const { oHash: n, iHash: s, finished: o, destroyed: r, blockLen: i, outputLen: a } = this;
|
|
1094
|
+
return t = t, t.finished = o, t.destroyed = r, t.blockLen = i, t.outputLen = a, t.oHash = n._cloneInto(t.oHash), t.iHash = s._cloneInto(t.iHash), t;
|
|
1095
|
+
}
|
|
1096
|
+
clone() {
|
|
1097
|
+
return this._cloneInto();
|
|
1098
|
+
}
|
|
1099
|
+
destroy() {
|
|
1100
|
+
this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy();
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
const jt = (e, t, n) => new Kt(e, t).update(n).digest();
|
|
1104
|
+
jt.create = (e, t) => new Kt(e, t);
|
|
1105
|
+
const J = /* @__PURE__ */ Uint8Array.of(0), _t = /* @__PURE__ */ Uint8Array.of();
|
|
1106
|
+
function lt(e, t, n, s = 32) {
|
|
1107
|
+
Ot(e), nt(s, "length");
|
|
1171
1108
|
const o = e.outputLen;
|
|
1172
1109
|
if (s > 255 * o)
|
|
1173
1110
|
throw new Error("Length must be <= 255*HashLen");
|
|
1174
1111
|
const r = Math.ceil(s / o);
|
|
1175
|
-
n === void 0 ? n =
|
|
1176
|
-
const i = new Uint8Array(r * o), a =
|
|
1112
|
+
n === void 0 ? n = _t : U(n, void 0, "info");
|
|
1113
|
+
const i = new Uint8Array(r * o), a = jt.create(e, t), l = a._cloneInto(), h = new Uint8Array(a.outputLen);
|
|
1177
1114
|
for (let c = 0; c < r; c++)
|
|
1178
|
-
|
|
1179
|
-
return a.destroy(),
|
|
1115
|
+
J[0] = c + 1, l.update(c === 0 ? _t : h).update(n).update(J).digestInto(h), i.set(h, o * c), a._cloneInto(l);
|
|
1116
|
+
return a.destroy(), l.destroy(), H(h, J), i.slice(0, s);
|
|
1180
1117
|
}
|
|
1181
|
-
const
|
|
1182
|
-
function
|
|
1118
|
+
const Q = new TextEncoder().encode("babylonvault"), It = 255, vt = 65535, Bt = 2, nn = "hashlock", sn = "auth-anchor", on = "wots-seed";
|
|
1119
|
+
function qt(e) {
|
|
1183
1120
|
if (!Number.isInteger(e) || e < 0 || e > 4294967295)
|
|
1184
1121
|
throw new Error(`i2osp4: value must be a u32, got ${e}`);
|
|
1185
1122
|
const t = new Uint8Array(4);
|
|
1186
1123
|
return t[0] = e >>> 24 & 255, t[1] = e >>> 16 & 255, t[2] = e >>> 8 & 255, t[3] = e & 255, t;
|
|
1187
1124
|
}
|
|
1188
|
-
function
|
|
1125
|
+
function ht(e, t = new Uint8Array(0)) {
|
|
1189
1126
|
const n = new TextEncoder().encode(e);
|
|
1190
|
-
if (n.length === 0 || n.length >
|
|
1127
|
+
if (n.length === 0 || n.length > It)
|
|
1191
1128
|
throw new Error(
|
|
1192
|
-
`info: label length must be in [1, ${
|
|
1129
|
+
`info: label length must be in [1, ${It}], got ${n.length}`
|
|
1193
1130
|
);
|
|
1194
|
-
if (t.length >
|
|
1131
|
+
if (t.length > vt)
|
|
1195
1132
|
throw new Error(
|
|
1196
|
-
`info: ctx length must be in [0, ${
|
|
1133
|
+
`info: ctx length must be in [0, ${vt}], got ${t.length}`
|
|
1197
1134
|
);
|
|
1198
|
-
const s =
|
|
1135
|
+
const s = Q.length + 1 + n.length + Bt + t.length, o = new Uint8Array(s);
|
|
1199
1136
|
let r = 0;
|
|
1200
|
-
return o.set(
|
|
1137
|
+
return o.set(Q, r), r += Q.length, o[r] = n.length, r += 1, o.set(n, r), r += n.length, o[r] = t.length >>> 8 & 255, o[r + 1] = t.length & 255, r += Bt, o.set(t, r), o;
|
|
1201
1138
|
}
|
|
1202
|
-
const
|
|
1203
|
-
function
|
|
1204
|
-
if (e.length !==
|
|
1139
|
+
const At = 32, rn = 32, an = 32, cn = 64;
|
|
1140
|
+
function ut(e) {
|
|
1141
|
+
if (e.length !== At)
|
|
1205
1142
|
throw new Error(
|
|
1206
|
-
`vault-secrets: root must be exactly ${
|
|
1143
|
+
`vault-secrets: root must be exactly ${At} bytes, got ${e.length}`
|
|
1207
1144
|
);
|
|
1208
1145
|
}
|
|
1209
|
-
function
|
|
1210
|
-
return
|
|
1211
|
-
|
|
1146
|
+
function Rn(e) {
|
|
1147
|
+
return ut(e), lt(
|
|
1148
|
+
L,
|
|
1212
1149
|
e,
|
|
1213
|
-
|
|
1214
|
-
|
|
1150
|
+
ht(sn),
|
|
1151
|
+
rn
|
|
1215
1152
|
);
|
|
1216
1153
|
}
|
|
1217
|
-
function
|
|
1218
|
-
return
|
|
1219
|
-
|
|
1154
|
+
function Un(e, t) {
|
|
1155
|
+
return ut(e), lt(
|
|
1156
|
+
L,
|
|
1220
1157
|
e,
|
|
1221
|
-
|
|
1222
|
-
|
|
1158
|
+
ht(nn, qt(t)),
|
|
1159
|
+
an
|
|
1223
1160
|
);
|
|
1224
1161
|
}
|
|
1225
|
-
function
|
|
1226
|
-
return
|
|
1227
|
-
|
|
1162
|
+
function Ln(e, t) {
|
|
1163
|
+
return ut(e), lt(
|
|
1164
|
+
L,
|
|
1228
1165
|
e,
|
|
1229
|
-
|
|
1230
|
-
|
|
1166
|
+
ht(on, qt(t)),
|
|
1167
|
+
cn
|
|
1231
1168
|
);
|
|
1232
1169
|
}
|
|
1233
|
-
const R = 32,
|
|
1234
|
-
function
|
|
1170
|
+
const R = 32, tt = 32, ot = 36, Gt = 32, X = 4, ln = X + R + X + Gt;
|
|
1171
|
+
function rt(e, t, n) {
|
|
1235
1172
|
e[t] = n >>> 24 & 255, e[t + 1] = n >>> 16 & 255, e[t + 2] = n >>> 8 & 255, e[t + 3] = n & 255;
|
|
1236
1173
|
}
|
|
1237
|
-
function
|
|
1238
|
-
if (e.txid.length !==
|
|
1174
|
+
function hn(e) {
|
|
1175
|
+
if (e.txid.length !== tt)
|
|
1239
1176
|
throw new Error(
|
|
1240
|
-
`outpoint.txid must be exactly ${
|
|
1177
|
+
`outpoint.txid must be exactly ${tt} bytes, got ${e.txid.length}`
|
|
1241
1178
|
);
|
|
1242
1179
|
if (!Number.isInteger(e.vout) || e.vout < 0 || e.vout > 4294967295)
|
|
1243
1180
|
throw new Error(`outpoint.vout must be a u32, got ${e.vout}`);
|
|
1244
|
-
const t = new Uint8Array(
|
|
1245
|
-
return t.set(e.txid, 0),
|
|
1181
|
+
const t = new Uint8Array(ot);
|
|
1182
|
+
return t.set(e.txid, 0), rt(t, tt, e.vout), t;
|
|
1246
1183
|
}
|
|
1247
|
-
function
|
|
1184
|
+
function Ht(e, t) {
|
|
1248
1185
|
const n = Math.min(e.length, t.length);
|
|
1249
1186
|
for (let s = 0; s < n; s++)
|
|
1250
1187
|
if (e[s] !== t[s]) return e[s] - t[s];
|
|
1251
1188
|
return e.length - t.length;
|
|
1252
1189
|
}
|
|
1253
|
-
function
|
|
1190
|
+
function un(e) {
|
|
1254
1191
|
if (e.length === 0)
|
|
1255
1192
|
throw new Error(
|
|
1256
1193
|
"buildFundingOutpointsCommitment: outpoints must be non-empty"
|
|
1257
1194
|
);
|
|
1258
|
-
const t = e.map(
|
|
1259
|
-
t.sort(
|
|
1195
|
+
const t = e.map(hn);
|
|
1196
|
+
t.sort(Ht);
|
|
1260
1197
|
for (let s = 1; s < t.length; s++)
|
|
1261
|
-
if (
|
|
1198
|
+
if (Ht(t[s - 1], t[s]) === 0)
|
|
1262
1199
|
throw new Error(
|
|
1263
1200
|
"buildFundingOutpointsCommitment: duplicate outpoint detected"
|
|
1264
1201
|
);
|
|
1265
|
-
const n = new Uint8Array(t.length *
|
|
1202
|
+
const n = new Uint8Array(t.length * ot);
|
|
1266
1203
|
for (let s = 0; s < t.length; s++)
|
|
1267
|
-
n.set(t[s], s *
|
|
1268
|
-
return
|
|
1204
|
+
n.set(t[s], s * ot);
|
|
1205
|
+
return L(n);
|
|
1269
1206
|
}
|
|
1270
|
-
function
|
|
1207
|
+
function dn(e) {
|
|
1271
1208
|
if (e.depositorBtcPubkey.length !== R)
|
|
1272
1209
|
throw new Error(
|
|
1273
1210
|
`vaultContext: depositorBtcPubkey must be exactly ${R} bytes, got ${e.depositorBtcPubkey.length}`
|
|
1274
1211
|
);
|
|
1275
|
-
const t =
|
|
1212
|
+
const t = un(e.fundingOutpoints), n = new Uint8Array(ln);
|
|
1276
1213
|
let s = 0;
|
|
1277
|
-
return
|
|
1214
|
+
return rt(n, s, R), s += X, n.set(e.depositorBtcPubkey, s), s += R, rt(n, s, Gt), s += X, n.set(t, s), n;
|
|
1215
|
+
}
|
|
1216
|
+
const fn = "babylon-vault", Zt = 32, Ct = Zt * 2, gn = /^[0-9a-f]+$/;
|
|
1217
|
+
async function Wn(e, t) {
|
|
1218
|
+
const n = dn(t), s = le(n), o = await e.deriveContextHash(fn, s);
|
|
1219
|
+
if (typeof o != "string")
|
|
1220
|
+
throw new Error(
|
|
1221
|
+
`deriveVaultRoot: wallet must return a string, got ${typeof o}`
|
|
1222
|
+
);
|
|
1223
|
+
if (o.length !== Ct)
|
|
1224
|
+
throw new Error(
|
|
1225
|
+
`deriveVaultRoot: wallet must return a ${Ct}-character hex string (${Zt} bytes), got length ${o.length}`
|
|
1226
|
+
);
|
|
1227
|
+
if (!gn.test(o))
|
|
1228
|
+
throw new Error(
|
|
1229
|
+
"deriveVaultRoot: wallet must return lowercase hex per derive-context-hash.md §2.1; got value with non-lowercase or non-hex characters"
|
|
1230
|
+
);
|
|
1231
|
+
return he(o);
|
|
1278
1232
|
}
|
|
1279
1233
|
export {
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1234
|
+
C,
|
|
1235
|
+
Cn as P,
|
|
1236
|
+
fn as V,
|
|
1237
|
+
$n as a,
|
|
1238
|
+
Rn as b,
|
|
1239
|
+
On as c,
|
|
1240
|
+
Sn as d,
|
|
1241
|
+
it as e,
|
|
1242
|
+
Un as f,
|
|
1243
|
+
An as g,
|
|
1289
1244
|
A as h,
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
qn as n
|
|
1245
|
+
Hn as i,
|
|
1246
|
+
Ln as j,
|
|
1247
|
+
un as k,
|
|
1248
|
+
dn as l,
|
|
1249
|
+
Wn as m
|
|
1296
1250
|
};
|
|
1297
|
-
//# sourceMappingURL=
|
|
1251
|
+
//# sourceMappingURL=deriveVaultRoot-DAMZDqg-.js.map
|