@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
1,529 lines • 276 kB
JavaScript
import { a as ae, r as Ti } from "./rolldown-runtime.js";
import { a as Ai, c as ce, d as xt, i as Be, l as ot, n as Ei, o as bi, r as Ii, s as Pt, t as Pi, u as ke } from "./vendors.js";
import { A as vi, B as C, C as E, D as Oi, E as b, F as lt, I as be, L as Vt, M as _i, N as Ui, O as Bi, P as ki, R as f, S as Le, T as Re, V as p, _ as Ie, a as De, b as y, c as Li, d as R, f as Ri, g as zt, h as Di, i as W, j as qt, k as S, l as Ci, m as Ki, n as _, o as xi, p as Mi, r as Ni, s as it, t as H, u as Fi, v as Wi, w as k, x as Hi, y as O, z as Vi } from "./btc-vision-bitcoin.js";
import { A as zi, F as Ht, c as qi, f as Gi, l as ue, n as Xi, s as $i, t as Yi, u as Mt } from "./noble-curves.js";
var Lt = Symbol;
typeof Lt.dispose != "symbol" && (Lt.dispose = /* @__PURE__ */ Symbol.for("Symbol.dispose"));
typeof Lt.asyncDispose != "symbol" && (Lt.asyncDispose = /* @__PURE__ */ Symbol.for("Symbol.asyncDispose"));
var ji = "1.8.5";
function Qi(e, t = {}) {
return new Uint8Array(xt.gzip(e, { level: t.level || 6 }));
}
function Zi(e) {
return new Uint8Array(xt.ungzip(e));
}
function Ji(e, t = {}) {
return new Uint8Array(xt.deflate(e, { level: t.level || 6 }));
}
function tr(e) {
return new Uint8Array(xt.inflate(e));
}
var Pe = {
gzipSync: Qi,
gunzipSync: Zi,
deflateSync: Ji,
inflateSync: tr
}, Z = class Gt {
static zlibOptions = {
level: 9,
maxOutputLength: 1024 * 1024 * 16
};
static compress(t) {
return new Uint8Array(Pe.gzipSync(t, Gt.zlibOptions));
}
static decompress(t) {
return new Uint8Array(Pe.gunzipSync(t, Gt.zlibOptions));
}
};
Pt();
var D = bi(), er = D;
Bi(er);
var P = /* @__PURE__ */ ae(Ai(), 1);
Pt();
ue();
qi();
var Ce = typeof P.default == "function" ? P.default : P.BIP32Factory;
if (!Ce) throw new Error("Failed to load BIP32 library");
var dt = Yi.Point, ir = dt.Fn.ORDER, ve = Mt(new Uint8Array([
84,
97,
112,
84,
119,
101,
97,
107
]));
function rr(e) {
return Mt(zi(ve, ve, e));
}
var I = class Xt {
static BIP32 = Ce(D);
static ECPairSigner = ot;
static {
dt.BASE.precompute(8);
}
static fromWIF(t, i = y.bitcoin) {
return ot.fromWIF(D, t, i);
}
static fromPrivateKey(t, i = y.bitcoin) {
return ot.fromPrivateKey(D, t, i);
}
static fromPublicKey(t, i = y.bitcoin) {
return ot.fromPublicKey(D, t, i);
}
static generateMultiSigAddress(t, i, r = y.bitcoin) {
const s = this.verifyPubKeys(t, r);
if (s.length !== t.length) throw new Error("Contains invalid public keys");
const n = _.p2ms({
m: i,
pubkeys: s,
network: r
}), o = _.p2wsh({
redeem: n,
network: r
}).address;
if (!o) throw new Error("Failed to generate address");
return o;
}
static verifyPubKeys(t, i = y.bitcoin) {
return t.map((r) => {
const s = Xt.fromPublicKey(r, i);
if (!s) throw new Error("Failed to regenerate key");
return s.publicKey;
});
}
static getP2WPKHAddress(t, i = y.bitcoin) {
const r = _.p2wpkh({
pubkey: t.publicKey,
network: i
});
if (!r.address) throw new Error("Failed to generate wallet");
return r.address;
}
static tweakedPubKeyToAddress(t, i) {
t.startsWith("0x") && (t = t.slice(2));
let r = S(t);
return r.length !== 32 && (r = O(r)), Xt.tweakedPubKeyBufferToAddress(r, i);
}
static tweakedPubKeyBufferToAddress(t, i) {
const { address: r } = _.p2tr({
pubkey: t,
network: i
});
if (!r) throw new Error("Failed to generate Taproot address");
return r;
}
static p2op(t, i = y.bitcoin, r = 0) {
const s = lt([new Uint8Array([r]), it.crypto.hash160(t)]);
if (s.length < 2 || s.length > 40) throw new Error("Witness program must be 2-40 bytes.");
return Ni(b.compile([p.OP_16, s]), i);
}
static xOnlyTweakedPubKeyToAddress(t, i) {
t.startsWith("0x") && (t = t.slice(2));
const r = S(t);
if (r.length !== 32) throw new Error("Invalid xOnly public key length");
const { address: s } = _.p2tr({
pubkey: r,
network: i
});
if (!s) throw new Error("Failed to generate Taproot address");
return s;
}
static tweakPublicKey(t) {
typeof t == "string" && t.startsWith("0x") && (t = t.slice(2));
const i = typeof t == "string" ? t : f(t), r = dt.fromHex(i), s = (r.y & 1n) === 0n ? r : r.negate(), n = Xi($i(rr(s.toBytes(!0).subarray(1))), ir);
return s.add(dt.BASE.multiply(n)).toBytes(!0);
}
static tweakBatchSharedT(t, i) {
const r = dt.BASE.multiply(i);
return t.map((s) => {
const n = dt.fromHex(f(s));
return (n.y % 2n === 0n ? n : n.negate()).add(r).toBytes(!0);
});
}
static generateWallet(t = y.bitcoin, i = P.MLDSASecurityLevel.LEVEL2) {
const r = ot.makeRandom(D, t, { rng: (o) => Ht(o) }), s = this.getP2WPKHAddress(r, t);
if (!s) throw new Error("Failed to generate wallet");
const n = this.generateQuantumKeyPair(i, t);
return {
address: s,
privateKey: r.toWIF(),
publicKey: f(r.publicKey),
quantumPrivateKey: f(n.privateKey),
quantumPublicKey: f(n.publicKey)
};
}
static generateQuantumKeyPair(t = P.MLDSASecurityLevel.LEVEL2, i = y.bitcoin) {
const r = Ht(64), s = P.QuantumBIP32Factory.fromSeed(r, i, t);
if (!s.privateKey || !s.publicKey) throw new Error("Failed to generate quantum keypair");
return {
privateKey: new Uint8Array(s.privateKey),
publicKey: new Uint8Array(s.publicKey)
};
}
static verifyContractAddress(t, i = y.bitcoin) {
return !!W.toOutputScript(t, i);
}
static getLegacySegwitAddress(t, i = y.bitcoin) {
const r = _.p2sh({
redeem: _.p2wpkh({
pubkey: t.publicKey,
network: i
}),
network: i
});
if (!r.address) throw new Error("Failed to generate wallet");
return r.address;
}
static getLegacyAddress(t, i = y.bitcoin) {
const r = _.p2pkh({
pubkey: t.publicKey,
network: i
});
if (!r.address) throw new Error("Failed to generate wallet");
return r.address;
}
static getP2PKH(t, i = y.bitcoin) {
const r = _.p2pkh({
pubkey: t,
network: i
});
if (!r.address) throw new Error("Failed to generate wallet");
return r.address;
}
static getP2PKAddress(t, i = y.bitcoin) {
const r = _.p2pk({
pubkey: t.publicKey,
network: i
});
if (!r.output) throw new Error("Failed to generate wallet");
return "0x" + f(r.output);
}
static generateRandomKeyPair(t = y.bitcoin) {
return ot.makeRandom(D, t, { rng: (i) => Ht(i) });
}
static fromSeed(t, i = y.bitcoin) {
return this.BIP32.fromSeed(t, i);
}
static getTaprootAddress(t, i = y.bitcoin) {
const { address: r } = _.p2tr({
internalPubkey: O(t.publicKey),
network: i
});
if (!r) throw new Error("Failed to generate sender address for transaction");
return r;
}
static getTaprootAddressFromAddress(t, i = y.bitcoin) {
const { address: r } = _.p2tr({
address: t,
network: i
});
if (!r) throw new Error("Failed to generate sender address for transaction");
return r;
}
static fromSeedKeyPair(t, i = y.bitcoin) {
const r = this.BIP32.fromSeed(t, i).privateKey;
if (!r) throw new Error("Failed to generate key pair");
return ot.fromPrivateKey(D, r, i);
}
}, G = /* @__PURE__ */ (function(e) {
return e[e.ACCESS_LIST = 1] = "ACCESS_LIST", e[e.EPOCH_SUBMISSION = 2] = "EPOCH_SUBMISSION", e[e.MLDSA_LINK_PUBKEY = 4] = "MLDSA_LINK_PUBKEY", e;
})({}), J = /* @__PURE__ */ (function(e) {
return e[e.ACCESS_LIST = 1] = "ACCESS_LIST", e[e.EPOCH_SUBMISSION = 2] = "EPOCH_SUBMISSION", e[e.MLDSA_LINK_PUBKEY = 3] = "MLDSA_LINK_PUBKEY", e;
})({}), Xr = 32, $r = 64, Yr = 64, jr = 4, Qr = 32, Zr = 16, Jr = 8, ts = 4, es = 2, is = 1, rs = 32, ss = 16, ns = 8, os = 4, as = 2, cs = 1, us = 1;
ue();
function sr(e, t, i, r) {
if (!e) throw new Error(`${t} (${i}: ${r})`);
}
function nr() {
if (typeof self < "u") return self;
if (typeof window < "u") return window;
if (typeof globalThis < "u") return globalThis;
throw new Error("unable to locate global object");
}
var Oe = nr(), ls = Oe.crypto || Oe.msCrypto;
function $t(e) {
switch (e) {
case "sha256":
return Mt.create();
case "sha512":
return Gi.create();
}
sr(!1, "invalid hashing algorithm name", "algorithm", e);
}
var or = /^[0-9a-fA-F]+$/, F = class Ke {
static btcToSatoshi(t) {
return BigInt(t * 1e8);
}
static rndBytes() {
return Ke.getSafeRandomValues(64);
}
static getSafeRandomValues(t) {
if (typeof globalThis.window < "u" && globalThis.window.crypto && typeof globalThis.window.crypto.getRandomValues == "function") {
const i = new Uint8Array(t);
return window.crypto.getRandomValues(i), i;
} else if (globalThis.crypto && typeof globalThis.crypto.getRandomValues == "function") {
const i = new Uint8Array(t);
return globalThis.crypto.getRandomValues(i), i;
} else
throw console.log("No secure random number generator available. Please upgrade your environment.", globalThis.window.crypto, globalThis.crypto), new Error("No secure random number generator available. Please upgrade your environment.");
}
static isValidHex(t) {
return or.test(t);
}
static opnetHash(t) {
const i = $t("sha512");
i.update(t);
const r = i.digest();
return `0x${f(new Uint8Array(r))}`;
}
}, $ = class {
static isP2WDAUTXO(e) {
if (!e.witnessScript) return !1;
const t = e.witnessScript instanceof Uint8Array ? e.witnessScript : S(e.witnessScript);
return this.isP2WDAWitnessScript(t);
}
static isP2WDAWitnessScript(e) {
try {
const t = b.decompile(e);
if (!t || t.length !== 7) return !1;
for (let i = 0; i < 5; i++) if (t[i] !== p.OP_2DROP) return !1;
return t[5] instanceof Uint8Array && t[5].length === 33 && t[6] === p.OP_CHECKSIG;
} catch {
return !1;
}
}
static generateP2WDAAddress(e, t) {
if (e.length !== 33) throw new Error("Public key must be 33 bytes (compressed)");
const i = b.compile([
p.OP_2DROP,
p.OP_2DROP,
p.OP_2DROP,
p.OP_2DROP,
p.OP_2DROP,
e,
p.OP_CHECKSIG
]), r = _.p2wsh({
redeem: { output: i },
network: t
});
if (!r.address || !r.output) throw new Error("Failed to generate P2WDA address");
return {
address: r.address,
witnessScript: i,
scriptPubKey: r.output
};
}
static extractPublicKeyFromP2WDA(e) {
try {
const t = b.decompile(e);
if (!t || t.length !== 7) return null;
for (let i = 0; i < 5; i++) if (t[i] !== p.OP_2DROP) return null;
return t[5] instanceof Uint8Array && t[5].length === 33 && t[6] === p.OP_CHECKSIG ? t[5] : null;
} catch {
return null;
}
}
static createSimpleP2WDAWitness(e, t) {
const i = [e];
for (let r = 0; r < 10; r++) i.push(new Uint8Array(0));
return i.push(t), i;
}
static validateP2WDASignature(e, t, i) {
return t.length === 64;
}
static estimateP2WDAWitnessSize(e = 0) {
return 72 + e + 39 + 12;
}
static couldBeP2WDA(e) {
return e.length === 34 && e[0] === 0 && e[1] === 32;
}
}, K = /* @__PURE__ */ (function(e) {
return e.P2PKH = "P2PKH", e.P2OP = "P2OP", e.P2SH_OR_P2SH_P2WPKH = "P2SH_OR_P2SH-P2WPKH", e.P2PK = "P2PK", e.P2TR = "P2TR", e.P2MR = "P2MR", e.P2WPKH = "P2WPKH", e.P2WSH = "P2WSH", e.P2WDA = "P2WDA", e;
})({}), bt = class pt {
static isValidP2TRAddress(t, i) {
if (!t || t.length < 50) return !1;
let r = !1;
try {
W.toOutputScript(t, i), r = W.fromBech32(t).version === 1;
} catch {
}
return r;
}
static isValidP2MRAddress(t, i) {
if (!t || t.length < 50) return !1;
let r = !1;
try {
W.toOutputScript(t, i);
const s = W.fromBech32(t);
r = s.version === 2 && s.data.length === 32;
} catch {
}
return r;
}
static isP2WPKHAddress(t, i) {
if (!t || t.length < 20 || t.length > 50) return !1;
let r = !1;
try {
const s = W.fromBech32(t);
W.toOutputScript(t, i), r = s.version === 0 && s.data.length === 20;
} catch {
}
return r;
}
static isP2WDAWitnessScript(t) {
return $.isP2WDAWitnessScript(t);
}
static isP2PKHOrP2SH(t, i) {
try {
const r = W.fromBase58Check(t);
return r.version === i.pubKeyHash ? !0 : r.version === i.scriptHash;
} catch {
return !1;
}
}
static isValidPublicKey(t, i) {
try {
if (t.startsWith("0x") && (t = t.slice(2)), !F.isValidHex(t)) return !1;
if (t.length === 64) return !0;
const r = S(t);
if (t.length === 130 && r[0] === 6 || r[0] === 7) return !0;
if (t.length === 66 || t.length === 130)
return I.fromPublicKey(r, i), !0;
} catch {
return !1;
}
return !1;
}
static isValidMLDSAPublicKey(t) {
try {
let i;
if (typeof t != "string" && t instanceof Uint8Array) i = t.length;
else {
if (t.startsWith("0x") && (t = t.slice(2)), !F.isValidHex(t)) return null;
i = t.length / 2;
}
switch (i) {
case 1312:
return P.MLDSASecurityLevel.LEVEL2;
case 1952:
return P.MLDSASecurityLevel.LEVEL3;
case 2592:
return P.MLDSASecurityLevel.LEVEL5;
default:
return null;
}
} catch {
return null;
}
}
static isValidP2OPAddress(t, i) {
if (!t || t.length < 20) return !1;
try {
const r = W.fromBech32(t);
return r.prefix === i.bech32 || r.prefix === i.bech32Opnet ? r.version === 16 && r.data.length === 21 : !1;
} catch {
return !1;
}
}
static requireRedeemScript(t, i) {
try {
const r = W.fromBase58Check(t);
return r.version === i.pubKeyHash ? !1 : r.version === i.scriptHash;
} catch {
return !1;
}
}
static detectAddressType(t, i) {
if (pt.isValidPublicKey(t, i)) return K.P2PK;
try {
const r = W.fromBase58Check(t);
if (r.version === i.pubKeyHash) return K.P2PKH;
if (r.version === i.scriptHash) return K.P2SH_OR_P2SH_P2WPKH;
} catch {
}
try {
const r = W.fromBech32(t);
if ((r.prefix === i.bech32Opnet || r.prefix === i.bech32) && r.version === 16 && r.data.length === 21) return K.P2OP;
if (r.prefix === i.bech32) {
if (r.version === 0 && r.data.length === 20) return K.P2WPKH;
if (r.version === 0 && r.data.length === 32) return K.P2WSH;
if (r.version === 1 && r.data.length === 32) return K.P2TR;
if (r.version === 2 && r.data.length === 32) return K.P2MR;
}
} catch {
}
return null;
}
static detectAddressTypeWithWitnessScript(t, i, r) {
const s = pt.detectAddressType(t, i);
return s === K.P2WSH && r && pt.isP2WDAWitnessScript(r) ? K.P2WDA : s;
}
static validateP2WDAAddress(t, i, r) {
try {
if (pt.detectAddressType(t, i) !== K.P2WSH) return {
isValid: !1,
isPotentiallyP2WDA: !1,
isDefinitelyP2WDA: !1,
error: "Not a P2WSH address"
};
if (!r) return {
isValid: !0,
isPotentiallyP2WDA: !0,
isDefinitelyP2WDA: !1
};
if (!pt.isP2WDAWitnessScript(r)) return {
isValid: !0,
isPotentiallyP2WDA: !0,
isDefinitelyP2WDA: !1,
error: "Witness script does not match P2WDA pattern"
};
if (_.p2wsh({
redeem: { output: r },
network: i
}).address !== t) return {
isValid: !1,
isPotentiallyP2WDA: !1,
isDefinitelyP2WDA: !1,
error: "Witness script does not match address"
};
const s = $.extractPublicKeyFromP2WDA(r);
return s ? {
isValid: !0,
isPotentiallyP2WDA: !0,
isDefinitelyP2WDA: !0,
publicKey: s
} : {
isValid: !1,
isPotentiallyP2WDA: !1,
isDefinitelyP2WDA: !1,
error: "Failed to extract public key from witness script"
};
} catch (s) {
return {
isValid: !1,
isPotentiallyP2WDA: !1,
isDefinitelyP2WDA: !1,
error: s.message
};
}
}
}, at = 115792089237316195423570985008687907853269984665640564039457584007908834671663n, xe = class {
maxTries;
constructor(e = 1e4) {
this.maxTries = e;
}
findOrDeriveValidPoint(e, t = !0, i = this.maxTries) {
if (e.length !== 32) throw new Error("xBytes must be exactly 32 bytes.");
let r = this.bytesToBigInt(e) % at, s = this.isValidX(r);
if (t && s === null) throw new Error("The given x is not a valid curve point.");
let n = 0;
for (; s === null; )
if (r = (r + 1n) % at, s = this.isValidX(r), n++, n > i) throw new Error(`Could not find a valid X point within ${i} increments.`);
const o = s, a = (at - o) % at;
return {
x: r,
y1: o,
y2: a
};
}
getCanonicalY(e, t) {
return e < t ? e : t;
}
getHybridPublicKey(e, t) {
const i = t % 2n === 0n ? 6 : 7, r = this.bigIntTo32Bytes(e), s = this.bigIntTo32Bytes(t), n = new Uint8Array(65);
return n[0] = i, n.set(r, 1), n.set(s, 33), n;
}
isValidX(e) {
const t = (this.modPow(e, 3n, at) + 7n) % at;
return this.sqrtModP(t, at);
}
modPow(e, t, i) {
let r = 1n, s = e % i, n = t;
for (; n > 0; )
(n & 1n) === 1n && (r = r * s % i), s = s * s % i, n >>= 1n;
return r;
}
sqrtModP(e, t) {
const i = this.modPow(e, t + 1n >> 2n, t);
return i * i % t !== e % t ? null : i;
}
bytesToBigInt(e) {
let t = 0n;
for (const i of e) t = t << 8n | BigInt(i);
return t;
}
bigIntTo32Bytes(e) {
const t = new Uint8Array(32);
for (let i = 31; i >= 0; i--)
t[i] = Number(e & 255n), e >>= 8n;
return t;
}
}, ar = class {
deriver = new xe();
generateHybridKeyFromHash(e) {
const t = this.deriver.findOrDeriveValidPoint(new Uint8Array(e), !1), i = this.deriver.getCanonicalY(t.y1, t.y2);
return new Uint8Array(this.deriver.getHybridPublicKey(t.x, i));
}
}, Yt = new ar(), nt = class ft {
static UNSPENDABLE_INTERNAL_KEY = S("50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0");
static CSV_BLOCKS = 75;
static generateTimeLockAddress(t, i = y.bitcoin, r = ft.CSV_BLOCKS) {
const s = this.generateTimeLockScript(t, r), n = it.payments.p2wsh({
redeem: { output: s },
network: i
});
if (!n.address) throw new Error("Failed to generate P2WSH address");
return {
address: n.address,
witnessScript: s
};
}
static generateTimeLockAddressP2TR(t, i = y.bitcoin, r = ft.CSV_BLOCKS) {
if (t.length !== 32) throw new Error("Public key must be 32 bytes for Taproot");
const s = this.generateTimeLockScript(t, r), n = it.payments.p2tr({
redeem: { output: s },
network: i,
internalPubkey: ft.UNSPENDABLE_INTERNAL_KEY
});
if (!n.address) throw new Error("Failed to generate P2TR address");
return n.address;
}
static generateTimeLockAddressP2MR(t, i = y.bitcoin, r = ft.CSV_BLOCKS) {
if (t.length !== 32) throw new Error("Public key must be 32 bytes for P2MR");
const s = {
output: this.generateTimeLockScript(t, r),
version: 192
}, n = _.p2mr({
scriptTree: s,
network: i
});
if (!n.address) throw new Error("Failed to generate P2MR address");
return n.address;
}
static generateTimeLockScript(t, i = ft.CSV_BLOCKS) {
return b.compile([
b.number.encode(i),
p.OP_CHECKSEQUENCEVERIFY,
p.OP_DROP,
t,
p.OP_CHECKSIG
]);
}
};
ue();
var cr = [
1312,
1952,
2592
], z = class j extends Uint8Array {
#t;
#e;
#n;
#i;
#h;
#r;
#s;
#d;
#a;
#c;
#u;
#l;
#p;
#f;
#g;
#S = !1;
#o;
constructor(t, i) {
if (super(32), !!t) {
if (i) {
if (![
32,
33,
65
].includes(i.length)) throw new Error(`Invalid public key length ${i.length}`);
this.#g = new Uint8Array(i.length), this.#g.set(i);
}
this.setMldsaKey(t);
}
}
static get [Symbol.species]() {
return Uint8Array;
}
get mldsaLevel() {
return this.#f;
}
set mldsaLevel(t) {
this.#f = t;
}
get originalMDLSAPublicKey() {
return this.#p;
}
set originalMDLSAPublicKey(t) {
this.#p = t ? new Uint8Array(t) : void 0;
}
get originalPublicKey() {
return this.ensureLegacyProcessed(), this.#i;
}
get mldsaPublicKey() {
return this.#a;
}
get legacyPublicKey() {
return this.ensureLegacyProcessed(), this.#o;
}
get keyPair() {
if (this.ensureLegacyProcessed(), !this.#h) throw new Error("Legacy public key not set for address");
return this.#h;
}
static dead() {
return j.fromString("0x0000000000000000000000000000000000000000000000000000000000000000", "0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
}
static fromString(t, i) {
if (!t) throw new Error("Invalid public key");
if (t.startsWith("0x") && (t = t.slice(2)), !F.isValidHex(t)) throw new Error('You must only pass public keys in hexadecimal format. If you have an address such as bc1q... you must convert it to a public key first. Please refer to await provider.getPublicKeyInfo("bc1q..."). If the public key associated with the address is not found, you must force the user to enter the destination public key. It looks like: 0x020373626d317ae8788ce3280b491068610d840c23ecb64c14075bbb9f670af52c.');
let r;
if (i) {
if (i.startsWith("0x") && (i = i.slice(2)), !F.isValidHex(i)) throw new Error('You must only pass classical public keys in hexadecimal format. If you have an address such as bc1q... you must convert it to a public key first. Please refer to await provider.getPublicKeyInfo("bc1q..."). If the public key associated with the address is not found, you must force the user to enter the destination public key. It looks like: 0x020373626d317ae8788ce3280b491068610d840c23ecb64c14075bbb9f670af52c.');
r = S(i);
}
return new j(S(t), r);
}
static wrap(t) {
return new j(t);
}
static uncompressedToCompressed(t) {
const i = Uint8Array.from(t), r = i.slice(1, 33), s = i.slice(33), n = new Uint8Array(33);
return n[0] = 2 + (s[s.length - 1] & 1), n.set(r, 1), n;
}
static fromBigInt(t, i) {
return new j(j.bigintToUint8Array(t), i !== void 0 ? j.bigintToUint8Array(i) : void 0);
}
static fromUint64Array(t, i, r, s) {
const n = new Uint8Array(32), o = new DataView(n.buffer);
return o.setBigUint64(0, t, !1), o.setBigUint64(8, i, !1), o.setBigUint64(16, r, !1), o.setBigUint64(24, s, !1), new j(n);
}
static bigintToUint8Array(t) {
const i = new Uint8Array(32), r = new DataView(i.buffer);
return r.setBigUint64(0, t >> 192n & 18446744073709551615n, !1), r.setBigUint64(8, t >> 128n & 18446744073709551615n, !1), r.setBigUint64(16, t >> 64n & 18446744073709551615n, !1), r.setBigUint64(24, t & 18446744073709551615n, !1), i;
}
[Symbol.dispose]() {
this.fill(0), this.#t = void 0, this.#e = void 0, this.#n = void 0, this.#i = void 0, this.#h = void 0, this.#r = void 0, this.#s = void 0, this.#d = void 0, this.#a?.fill(0), this.#a = void 0, this.#c = void 0, this.#u = void 0, this.#l = void 0, this.#p?.fill(0), this.#p = void 0, this.#f = void 0, this.#S = !1, this.#o = void 0;
}
toUint64Array() {
if (this.#l !== void 0) return this.#l;
const t = new DataView(this.buffer, this.byteOffset, 32);
return this.#l = [
t.getBigUint64(0, !1),
t.getBigUint64(8, !1),
t.getBigUint64(16, !1),
t.getBigUint64(24, !1)
], this.#l;
}
isDead() {
for (let t = 0; t < 32; t++) if (this[t] !== 0) return !1;
return !0;
}
toHex() {
return "0x" + f(this);
}
tweakedToHex() {
const t = this.legacyPublicKey;
if (!t) throw new Error("Legacy public key not set");
return "0x" + f(t);
}
toBuffer() {
return new Uint8Array(this);
}
tweakedPublicKeyToBuffer() {
const t = this.legacyPublicKey;
if (!t) throw new Error("Legacy public key not set");
return new Uint8Array(t);
}
toUncompressedHex() {
if (this.ensureLegacyProcessed(), !this.#r) throw new Error("Legacy public key not set");
return "0x" + f(this.#r.uncompressed);
}
toUncompressedBuffer() {
if (this.ensureLegacyProcessed(), !this.#r) throw new Error("Legacy public key not set");
return this.#r.uncompressed;
}
toHybridPublicKeyHex() {
if (this.ensureLegacyProcessed(), !this.#r) throw new Error("Legacy public key not set");
return "0x" + f(this.#r.hybrid);
}
toHybridPublicKeyBuffer() {
if (this.ensureLegacyProcessed(), !this.#r) throw new Error("Legacy public key not set");
return this.#r.hybrid;
}
originalPublicKeyBuffer() {
if (this.ensureLegacyProcessed(), !this.#i) throw new Error("Legacy public key not set");
return new Uint8Array(this.#i);
}
toBigInt() {
if (this.#c !== void 0) return this.#c;
const t = new DataView(this.buffer, this.byteOffset, 32);
return this.#c = t.getBigUint64(0, !1) << 192n | t.getBigUint64(8, !1) << 128n | t.getBigUint64(16, !1) << 64n | t.getBigUint64(24, !1), this.#c;
}
tweakedToBigInt() {
if (!this.legacyPublicKey) throw new Error("Legacy public key not set");
if (this.#u !== void 0) return this.#u;
const t = new DataView(this.legacyPublicKey.buffer, this.byteOffset, 32);
return this.#u = t.getBigUint64(0, !1) << 192n | t.getBigUint64(8, !1) << 128n | t.getBigUint64(16, !1) << 64n | t.getBigUint64(24, !1), this.#u;
}
equals(t) {
const i = this;
if (t.length !== i.length) return !1;
for (let r = 0; r < i.length; r++) if (i[r] !== t[r]) return !1;
return !0;
}
lessThan(t) {
const i = this;
for (let r = 0; r < 32; r++) {
const s = i[r], n = t[r];
if (s < n) return !0;
if (s > n) return !1;
}
return !1;
}
greaterThan(t) {
const i = this;
for (let r = 0; r < 32; r++) {
const s = i[r], n = t[r];
if (s > n) return !0;
if (s < n) return !1;
}
return !1;
}
set(t) {
this.setMldsaKey(t);
}
isValidLegacyPublicKey(t) {
const i = this.legacyPublicKey;
if (!i) throw new Error("Legacy key not set.");
return bt.isValidPublicKey(f(i), t);
}
p2pk() {
return this.toHex();
}
p2wpkh(t) {
return I.getP2WPKHAddress(this.keyPair, t);
}
p2pkh(t) {
return I.getLegacyAddress(this.keyPair, t);
}
p2shp2wpkh(t) {
return I.getLegacySegwitAddress(this.keyPair, t);
}
toString() {
return this.toHex();
}
toJSON() {
return this.toHex();
}
p2tr(t) {
if (this.#t && this.#n === t) return this.#t;
const i = this.legacyPublicKey;
if (!i) throw new Error("Legacy public key not set");
const r = I.tweakedPubKeyBufferToAddress(i, t);
if (r)
return this.#n = t, this.#t = r, r;
throw new Error("Legacy public key not set");
}
p2wda(t) {
if (this.#d && this.#n === t) return this.#d;
if (this.ensureLegacyProcessed(), !this.#i) throw new Error("Cannot create P2WDA address: public key not set");
const i = new Uint8Array(this.#i);
if (i.length !== 33) throw new Error("P2WDA requires a compressed public key (33 bytes)");
try {
const r = $.generateP2WDAAddress(i, t);
return this.#n = t, this.#d = r, {
address: r.address,
witnessScript: r.witnessScript
};
} catch (r) {
throw new Error(`Failed to generate P2WDA address: ${r.message}`, { cause: r });
}
}
toCSV(t, i) {
const r = Number(t);
if (r < 1 || r > 65535) throw new Error("CSV block number must be between 1 and 65535");
if (this.ensureLegacyProcessed(), !this.#i) throw new Error("Cannot create CSV address: public key not set");
const s = new Uint8Array(this.#i);
return nt.generateTimeLockAddress(s, i, r);
}
toCSVTweaked(t, i) {
const r = Number(t);
if (r < 1 || r > 65535) throw new Error("CSV block number must be between 1 and 65535");
if (this.ensureLegacyProcessed(), !this.#i) throw new Error("Cannot create CSV address: public key not set");
return nt.generateTimeLockAddressP2TR(this.tweakedPublicKeyToBuffer(), i, r);
}
toCSVP2MR(t, i) {
const r = Number(t);
if (r < 1 || r > 65535) throw new Error("CSV block number must be between 1 and 65535");
if (this.ensureLegacyProcessed(), !this.#o) throw new Error("Cannot create CSV P2MR address: public key not set");
return nt.generateTimeLockAddressP2MR(this.tweakedPublicKeyToBuffer(), i, r);
}
p2op(t) {
if (this.#e && this.#n === t) return this.#e;
const i = I.p2op(this, t);
if (i)
return this.#n = t, this.#e = i, i;
throw new Error("ML-DSA public key not set");
}
toTweakedHybridPublicKeyHex() {
if (this.ensureLegacyProcessed(), this.ensureTweakedUncompressed(), !this.#s) throw new Error("Legacy public key not set");
return "0x" + f(this.#s);
}
toTweakedHybridPublicKeyBuffer() {
if (this.ensureLegacyProcessed(), this.ensureTweakedUncompressed(), !this.#s) throw new Error("Legacy public key not set");
return this.#s;
}
ensureTweakedUncompressed() {
if (this.#s) return;
const t = this.#o;
if (t && t.length === 32 && !this.#i)
try {
this.#s = Yt.generateHybridKeyFromHash(t);
} catch {
}
}
setMldsaKey(t) {
if (t.length === 32) {
const i = new Uint8Array(32);
i.set(t), super.set(i);
} else {
if (!cr.includes(t.length)) throw new Error(`Invalid ML-DSA public key length: ${t.length}. Expected 1312 (ML-DSA-44/LEVEL2), 1952 (ML-DSA-65/LEVEL3), or 2592 (ML-DSA-87/LEVEL5) bytes.`);
this.#a = new Uint8Array(t.length), this.#a.set(t);
const i = Mt(new Uint8Array(t)), r = new Uint8Array(32);
r.set(i), super.set(r);
}
}
ensureLegacyProcessed() {
if (this.#S) return;
this.#S = !0;
const t = this.#g;
t && (t.length === 32 ? this.#o = t : this.autoFormat(t));
}
autoFormat(t) {
const i = t[0];
i === 3 || i === 2 || (i === 4 || i === 6 || i === 7) && (t = j.uncompressedToCompressed(t)), this.#i = Uint8Array.from(t), this.#h = I.fromPublicKey(this.#i), this.#r = Mi(this.#i);
const r = O(I.tweakPublicKey(this.#i));
this.#s = Yt.generateHybridKeyFromHash(r), this.#o = new Uint8Array(32), this.#o.set(r);
}
}, Nt = class Me {
_keys = [];
_values = {};
constructor(t) {
if (t instanceof Me) this.setAll(t);
else if (t) for (const [i, r] of t) this.set(i, r);
}
get size() {
return this._keys.length;
}
setAll(t) {
this._keys = [...t._keys], this._values = { ...t._values };
}
addAll(t) {
for (const [i, r] of t.entries()) this.set(i, r);
}
*keys() {
yield* this._keys;
}
*values() {
for (const t of this._keys) yield this._values[t];
}
*entries() {
for (const t of this._keys) yield [t, this._values[t]];
}
set(t, i) {
return this.has(t) || this._keys.push(t), this._values[t] = i, this;
}
indexOf(t) {
if (!this.has(t)) return -1;
for (let i = 0; i < this._keys.length; i++) if (this._keys[i] === t) return i;
throw new Error("Key not found, this should not happen.");
}
get(t) {
return this._values[t];
}
has(t) {
return Object.prototype.hasOwnProperty.call(this._values, t);
}
delete(t) {
if (!this.has(t)) return !1;
const i = this.indexOf(t);
return this._keys.splice(i, 1), delete this._values[t], !0;
}
clear() {
this._keys = [], this._values = {};
}
[Symbol.dispose]() {
this.clear();
}
forEach(t, i) {
for (const r of this._keys) t.call(i, this._values[r], r, this);
}
*[Symbol.iterator]() {
for (const t of this._keys) yield [t, this._values[t]];
}
}, Ne = class {
items;
constructor(e) {
if (this.items = new Nt(), e) for (const [t, i] of e) this.set(t, i);
}
get size() {
return this.items.size;
}
set(e, t) {
const i = e.toBigInt();
return this.items.set(i, t), this;
}
get(e) {
return this.items.get(e.toBigInt());
}
has(e) {
return this.items.has(e.toBigInt());
}
delete(e) {
const t = e.toBigInt();
return this.items.delete(t);
}
clear() {
this.items.clear();
}
[Symbol.dispose]() {
this.clear();
}
indexOf(e) {
return this.items.indexOf(e.toBigInt());
}
*entries() {
for (const [e, t] of this.items.entries()) yield [z.fromBigInt(e), t];
}
*keys() {
for (const e of this.items.keys()) yield z.fromBigInt(e);
}
*values() {
for (const e of this.items.values()) yield e;
}
forEach(e, t) {
for (const [i, r] of this.items.entries()) {
const s = z.fromBigInt(i);
e.call(t, r, s, this);
}
}
*[Symbol.iterator]() {
yield* this.entries();
}
}, Fe = class {
indexMap;
_keys = [];
_values = [];
constructor(e) {
if (this.indexMap = new Nt(), e) for (const [t, i] of e) this.set(t, i);
}
get size() {
return this._keys.length;
}
set(e, t) {
const i = e.tweakedToBigInt(), r = this.indexMap.get(i);
if (r !== void 0) this._values[r] = t;
else {
const s = this._keys.length;
this._keys.push(e), this._values.push(t), this.indexMap.set(i, s);
}
return this;
}
get(e) {
const t = e.tweakedToBigInt(), i = this.indexMap.get(t);
if (i !== void 0)
return this._values[i];
}
has(e) {
return this.indexMap.has(e.tweakedToBigInt());
}
delete(e) {
const t = e.tweakedToBigInt(), i = this.indexMap.get(t);
if (i === void 0) return !1;
this._keys.splice(i, 1), this._values.splice(i, 1), this.indexMap.clear();
for (let r = 0; r < this._keys.length; r++) this.indexMap.set(this._keys[r].tweakedToBigInt(), r);
return !0;
}
clear() {
this.indexMap.clear(), this._keys = [], this._values = [];
}
[Symbol.dispose]() {
this.clear();
}
indexOf(e) {
const t = this.indexMap.get(e.tweakedToBigInt());
return t !== void 0 ? t : -1;
}
*entries() {
for (let e = 0; e < this._keys.length; e++) yield [this._keys[e], this._values[e]];
}
*keys() {
for (const e of this._keys) yield e;
}
*values() {
for (const e of this._values) yield e;
}
forEach(e, t) {
for (let i = 0; i < this._keys.length; i++) e.call(t, this._values[i], this._keys[i], this);
}
*[Symbol.iterator]() {
yield* this.entries();
}
}, _t = class {
static EXPECTED_BUFFER_LENGTH = 32;
static bufferToUint8Array(e) {
const t = new Uint8Array(e.byteLength);
return t.set(e), t;
}
static uint8ArrayToHex(e) {
return f(e);
}
static hexToUint8Array(e) {
let t = e;
return t.length >= 2 && t[0] === "0" && (t[1] === "x" || t[1] === "X") && (t = t.slice(2)), t.length % 2 !== 0 && (t = "0" + t), S(t);
}
static pointerToUint8Array(e) {
if (e < 0n) throw new RangeError("Pointer cannot be negative");
const t = e.toString(16).padStart(64, "0");
if (t.length > 64) throw new RangeError("Pointer exceeds 256-bit range");
return S(t);
}
static uint8ArrayToPointer(e) {
return e.length === 0 ? 0n : BigInt("0x" + f(e));
}
static valueToUint8Array(e, t = 32) {
if (e < 0n) throw new RangeError("Value cannot be negative");
const i = e.toString(16).padStart(t * 2, "0");
if (i.length > t * 2) throw new RangeError(`Value exceeds ${t}-byte range`);
return S(i);
}
static uint8ArrayToValue(e) {
return e.length === 0 ? 0n : BigInt("0x" + f(e));
}
}, Ft = class {
buffer;
currentOffset = 0;
constructor(e) {
this.buffer = new DataView(e.buffer, e.byteOffset, e.byteLength);
}
get byteLength() {
return this.buffer.byteLength;
}
static stringCompare(e, t) {
return e.localeCompare(t);
}
static bigintCompare(e, t) {
return e < t ? -1 : e > t ? 1 : 0;
}
static numberCompare(e, t) {
return e < t ? -1 : e > t ? 1 : 0;
}
setBuffer(e) {
this.buffer = new DataView(e.buffer, e.byteOffset, e.byteLength), this.currentOffset = 0;
}
length() {
return this.buffer.byteLength;
}
bytesLeft() {
return this.buffer.byteLength - this.currentOffset;
}
readI8() {
this.verifyEnd(this.currentOffset + 1);
const e = this.buffer.getInt8(this.currentOffset);
return this.currentOffset += 1, e;
}
readI16(e = !0) {
this.verifyEnd(this.currentOffset + 2);
const t = this.buffer.getInt16(this.currentOffset, !e);
return this.currentOffset += 2, t;
}
readI32(e = !0) {
this.verifyEnd(this.currentOffset + 4);
const t = this.buffer.getInt32(this.currentOffset, !e);
return this.currentOffset += 4, t;
}
readI64(e = !0) {
this.verifyEnd(this.currentOffset + 8);
const t = this.buffer.getBigInt64(this.currentOffset, !e);
return this.currentOffset += 8, t;
}
readU8() {
this.verifyEnd(this.currentOffset + 1);
const e = this.buffer.getUint8(this.currentOffset);
return this.currentOffset += 1, e;
}
readU16(e = !0) {
this.verifyEnd(this.currentOffset + 2);
const t = this.buffer.getUint16(this.currentOffset, !e);
return this.currentOffset += 2, t;
}
readU32(e = !0) {
this.verifyEnd(this.currentOffset + 4);
const t = this.buffer.getUint32(this.currentOffset, !e);
return this.currentOffset += 4, t;
}
readU64(e = !0) {
this.verifyEnd(this.currentOffset + 8);
const t = this.buffer.getBigUint64(this.currentOffset, !e);
return this.currentOffset += 8, t;
}
readU128(e = !0) {
const t = this.readBytes(16);
let i = t;
return e || (i = this.reverseBytes(t)), BigInt("0x" + this.toHexString(i));
}
readU256(e = !0) {
const t = this.readBytes(32);
let i = t;
return e || (i = this.reverseBytes(t)), BigInt("0x" + this.toHexString(i));
}
readI128(e = !0) {
const t = this.readBytes(16);
let i = t;
e || (i = this.reverseBytes(t));
let r = BigInt("0x" + this.toHexString(i));
if (i[0] & 128) {
const s = BigInt(1) << BigInt(128);
r = r - s;
}
return r;
}
readBoolean() {
return this.readU8() !== 0;
}
readSelector() {
return this.readU32(!0);
}
readBytes(e, t = !1) {
this.verifyEnd(this.currentOffset + e);
let i = new Uint8Array(e);
for (let r = 0; r < e; r++) {
const s = this.buffer.getUint8(this.currentOffset++);
if (t && s === 0) {
i = new Uint8Array(i.subarray(0, r));
break;
}
i[r] = s;
}
return i;
}
readString(e) {
const t = new TextDecoder(), i = this.readBytes(e, !1);
return t.decode(i);
}
readStringWithLength(e = !0) {
const t = this.readU32(e);
return this.readString(t);
}
readAddress() {
return new z(Array.from(this.readBytes(32)));
}
readTweakedPublicKey() {
return this.verifyEnd(this.currentOffset + 32), this.readBytes(32);
}
readExtendedAddress() {
this.verifyEnd(this.currentOffset + 64);
const e = Array.from(this.readBytes(32));
return new z(Array.from(this.readBytes(32)), e);
}
readSchnorrSignature() {
return this.verifyEnd(this.currentOffset + 64 + 64), {
address: this.readExtendedAddress(),
signature: this.readBytes(64)
};
}
readBytesWithLength(e = 0, t = !0) {
const i = this.readU32(t);
if (e > 0 && i > e) throw new Error("Data length exceeds maximum length.");
return this.readBytes(i);
}
readArrayOfBuffer(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readBytesWithLength();
return i;
}
readAddressArray(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readAddress();
return i;
}
readU256Array(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readU256(e);
return i;
}
readU128Array(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readU128(e);
return i;
}
readU64Array(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readU64(e);
return i;
}
readU32Array(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readU32(e);
return i;
}
readU16Array(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readU16(e);
return i;
}
readU8Array() {
const e = this.readU16(!0), t = new Array(e);
for (let i = 0; i < e; i++) t[i] = this.readU8();
return t;
}
readStringArray(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readStringWithLength(e);
return i;
}
readBytesArray(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readBytesWithLength(0, e);
return i;
}
readAddressValueTuple(e = !0) {
const t = this.readU16(e), i = new Ne();
for (let r = 0; r < t; r++) {
const s = this.readAddress(), n = this.readU256(e);
if (i.has(s)) throw new Error("Duplicate address found in map");
i.set(s, n);
}
return i;
}
readExtendedAddressArray(e = !0) {
const t = this.readU16(e), i = new Array(t);
for (let r = 0; r < t; r++) i[r] = this.readExtendedAddress();
return i;
}
readExtendedAddressMapU256(e = !0) {
const t = this.readU16(e), i = new Fe();
for (let r = 0; r < t; r++) {
const s = this.readExtendedAddress(), n = this.readU256(e);
if (i.has(s)) throw new Error("Duplicate tweaked address found in map");
i.set(s, n);
}
return i;
}
getOffset() {
return this.currentOffset;
}
setOffset(e) {
this.currentOffset = e;
}
verifyEnd(e) {
if (e > this.buffer.byteLength) throw new Error(`Attempt to read beyond buffer length: requested up to byte offset ${e}, but buffer is only ${this.buffer.byteLength} bytes.`);
}
reverseBytes(e) {
const t = new Uint8Array(e.length);
for (let i = 0; i < e.length; i++) t[i] = e[e.length - 1 - i];
return t;
}
toHexString(e) {
return Array.from(e, (t) => t.toString(16).padStart(2, "0")).join("");
}
}, V = class We {
currentOffset = 0;
buffer;
constructor(t = 0) {
this.buffer = this.getDefaultBuffer(t);
}
static estimateArrayOfBufferLength(t) {
if (t.length > 65535) throw new Error("Array size is too large");
let i = 2;
for (let r = 0; r < t.length; r++) i += 4 + t[r].length;
return i;
}
writeU8(t) {
if (t > 255) throw new Error("u8 value is too large.");
this.allocSafe(1), this.buffer.setUint8(this.currentOffset++, t);
}
writeU16(t, i = !0) {
if (t > 65535) throw new Error("u16 value is too large.");
this.allocSafe(2), this.buffer.setUint16(this.currentOffset, t, !i), this.currentOffset += 2;
}
writeU32(t, i = !0) {
if (t > 4294967295) throw new Error("u32 value is too large.");
this.allocSafe(4), this.buffer.setUint32(this.currentOffset, t, !i), this.currentOffset += 4;
}
writeU64(t, i = !0) {
if (t > 18446744073709551615n) throw new Error("u64 value is too large.");
this.allocSafe(8), this.buffer.setBigUint64(this.currentOffset, t, !i), this.currentOffset += 8;
}
writeI8(t) {
if (t < -128 || t > 127) throw new Error("i8 value is out of range.");
this.allocSafe(1), this.buffer.setInt8(this.currentOffset, t), this.currentOffset += 1;
}
writeI16(t, i = !0) {
if (t < -32768 || t > 32767) throw new Error("i16 value is out of range.");
this.allocSafe(2), this.buffer.setInt16(this.currentOffset, t, !i), this.currentOffset += 2;
}
writeI32(t, i = !0) {
if (t < -2147483648 || t > 2147483647) throw new Error("i32 value is out of range.");
this.allocSafe(4), this.buffer.setInt32(this.currentOffset, t, !i), this.currentOffset += 4;
}
writeI64(t, i = !0) {
if (t < -9223372036854775808n || t > 9223372036854775807n) throw new Error("i64 value is out of range.");
this.allocSafe(8), this.buffer.setBigInt64(this.currentOffset, t, !i), this.currentOffset += 8;
}
writeSelector(t) {
this.writeU32(t, !0);
}
writeBoolean(t) {
this.writeU8(t ? 1 : 0);
}
writeI128(t, i = !0) {
if (t > 170141183460469231731687303715884105727n || t < -170141183460469231731687303715884105728n) throw new Error("i128 value is too large.");
this.allocSafe(16);
const r = _t.valueToUint8Array(t, 16);
if (r.byteLength !== 16) throw new Error(`Invalid i128 value: ${t}`);
if (i) for (let s = 0; s < r.byteLength; s++) this.writeU8(r[s]);
else for (let s = r.byteLength - 1; s >= 0; s--) this.writeU8(r[s]);
}
writeU256(t, i = !0) {
if (t > 115792089237316195423570985008687907853269984665640564039457584007913129639935n && t < 0n) throw new Error("u256 value is too large or negative.");
this.allocSafe(32);
const r = _t.valueToUint8Array(t);
if (r.byteLength !== 32) throw new Error(`Invalid u256 value: ${t}`);
if (i) for (let s = 0; s < r.byteLength; s++) this.writeU8(r[s]);
else for (let s = r.byteLength - 1; s >= 0; s--) this.writeU8(r[s]);
}
writeU128(t, i = !0) {
if (t > 340282366920938463463374607431768211455n && t < 0n) throw new Error("u128 value is too large or negative.");
this.allocSafe(16);
const r = _t.valueToUint8Array(t, 16);
if (r.byteLength !== 16) throw new Error(`Invalid u128 value: ${t}`);
if (i) for (let s = 0; s < r.byteLength; s++) this.writeU8(r[s]);
else for (let s = r.byteLength - 1; s >= 0; s--) this.writeU8(r[s]);
}
writeBytes(t) {
this.allocSafe(t.byteLength);
for (let i = 0; i < t.byteLength; i++) this.writeU8(t[i]);
}
writeString(t) {
const i = new TextEncoder().encode(t);
this.allocSafe(i.length), this.writeBytes(i);
}
writeStringWithLength(t) {
const i = new TextEncoder().encode(t);
this.allocSafe(4 + i.length), this.writeU32(i.length), this.writeBytes(i);
}
writeAddress(t) {
this.verifyAddress(t), this.writeBytes(t);
}
writeTweakedPublicKey(t) {
const i = t.tweakedPublicKeyToBuffer();
this.allocSafe(32), this.writeBytes(i);
}
writeExtendedAddress(t) {
this.allocSafe(64), this.writeTweakedPublicKey(t), this.writeBytes(t);
}
writeSchnorrSignature(t, i) {
if (i.length !== 64) throw new Error(`Invalid Schnorr signature length: expected 64, got ${i.length}`);
this.allocSafe(128), this.writeExtendedAddress(t), this.writeBytes(i);
}
getBuffer(t = !0) {
const i = new Uint8Array(this.buffer.byteLength);
for (let r = 0; r < this.buffer.byteLength; r++) i[r] = this.buffer.getUint8(r);
return t && this.clear(), i;
}
reset() {
this.currentOffset = 0, this.buffer = this.getDefaultBuffer(4);
}
toBytesReader() {
return new Ft(this.getBuffer());
}
getOffset() {
return this.currentOffset;
}
setOffset(t) {
this.currentOffset = t;
}
clear() {
this.currentOffset = 0, this.buffer = this.getDefaultBuffer();
}
[Symbol.dispose]() {
this.clear();
}
allocSafe(t) {
this.currentOffset + t > this.buffer.byteLength && this.resize(t);
}
writeAddressValueTuple(t, i = !0) {
if (t.size > 65535) throw new Error("Map size is too large");
this.writeU16(t.size, i);
const r = Array.from(t.keys());
for (let s = 0; s < r.length; s++) {
const n = r[s], o = t.get(n);
if (o == null) throw new Error("Value not found");
this.writeAddress(n), this.writeU256(o, i);
}
}
writeExtendedAddressMapU256(t, i = !0) {
if (t.size > 65535) throw new Error("Map size is too large");
this.writeU16(t.size, i);
for (const [r, s] of t.entries())
this.writeExtendedAddress(r), this.writeU256(s, i);
}
writeBytesWithLength(t) {
this.writeU32(t.length), this.writeBytes(t);
}
writeArrayOfBuffer(t, i = !0) {
const r = We.estimateArrayOfBufferLength(t);
this.allocSafe(r), this.writeU16(t.length, i);
for (let s = 0; s < t.length; s++)
this.writeU32(t[s].length, i), this.writeBytes(t[s]);
}
writeAddressArray(t) {
if (t.length > 65535) throw new Error("Array size is too large");
this.writeU16(t.length);
for (let i = 0; i < t.length; i++) this.writeAddress(t[i]);
}
writeExtendedAddressArray(t) {
if (t.length > 65535) throw new Error("Array size is too large");
this.allocSafe(2 + t.length * 64), this.writeU16(t.length);
for (let i = 0; i < t.length; i++) this.writeExtendedAddress(t[i]);
}
writeU32Array(t, i = !0) {
if (t.length > 65535) throw new Error("Array size is too large");
this.writeU16(t.length, i);
for (let r = 0; r < t.length; r++) this.writeU32(t[r], i);
}
writeU256Array(t, i = !0) {
if (t.length > 65535) throw new Error("Array size is too large");
this.writeU16(t.length, i);
for (let r = 0; r < t.length; r++) this.writeU256(t[r], i);
}
writeU128Array(t, i = !0) {
if (t.length > 65535) throw new Error("Array size is too large");
this.writeU16(t.length, i);
for (let r = 0; r < t.length; r++) this.writeU128(t[r], i);
}
writeStringArray(t) {
if (t.length > 65535) throw new Error("Array size is too large");
this.writeU16(t.length);
for (let i = 0; i < t.length; i++) this.writeStringWithLength(t[i]);
}
writeU16Array(t, i = !0) {
if (t.length > 65535) throw new Error("Array size is too large");
this.writeU16(t.length, i);
for (let r = 0; r < t.length; r++) this.writeU16(t[r], i);
}
writeU8Array(t) {
if (t.length > 65535) throw new Error("Array size is too large");
this.writeU16(t.length);
for (let i = 0; i < t.length; i++) this.writeU8(t[i]);
}
writeU64Array(t, i = !0) {
if (t.length > 65535) throw new Error("Array size is too large");
this.writeU16