opnet
Version:
The perfect library for building Bitcoin-based applications.
1,779 lines • 1.57 MB
JavaScript
import { a as __toESM, i as __toCommonJS, n as __esmMin, r as __exportAll, t as __commonJSMin } from "./rolldown-runtime.js";
import { A as concatBytes$1, F as randomBytes$2, M as init_utils$1, N as isBytes$1, T as abytes$1, a as reverseBits, c as init_utils$2, d as init_sha2, f as sha256$1, i as init_fft, l as hmac_exports, m as sha512, n as mod$1, o as abool, p as sha2_exports, r as FFTCore, s as bytesToNumberBE, t as secp256k1, u as init_hmac } from "./noble-curves.js";
import { a as init_sha3, c as init_legacy, d as sha1$1, i as require_utils, l as legacy_exports, n as require_sha512$1, o as shake128, r as require_sha256$1, s as shake256, t as require_pbkdf2, u as ripemd160$1 } from "./noble-hashes.js";
//#region node_modules/@btc-vision/bitcoin/build/networks.js
var networks_exports = /* @__PURE__ */ __exportAll({
bitcoin: () => bitcoin$1,
bitcoinCash: () => bitcoinCash,
bitcoinCashTestnet: () => bitcoinCashTestnet,
dash: () => dash,
dashTestnet: () => dashTestnet,
dogecoin: () => dogecoin,
dogecoinTestnet: () => dogecoinTestnet,
litecoin: () => litecoin,
litecoinTestnet: () => litecoinTestnet,
opnetTestnet: () => opnetTestnet,
regtest: () => regtest,
testnet: () => testnet
});
/**
* Represents the Bitcoin network configuration.
*/
var bitcoin$1 = {
messagePrefix: "Bitcoin Signed Message:\n",
bech32: "bc",
bech32Opnet: "op",
bip32: {
public: 76067358,
private: 76066276
},
pubKeyHash: 0,
scriptHash: 5,
wif: 128
};
/**
* Represents the regtest network configuration.
*/
var regtest = {
messagePrefix: "Bitcoin Signed Message:\n",
bech32: "bcrt",
bech32Opnet: "opr",
bip32: {
public: 70617039,
private: 70615956
},
pubKeyHash: 111,
scriptHash: 196,
wif: 239
};
/**
* Represents the testnet network configuration.
*/
var testnet = {
messagePrefix: "Bitcoin Signed Message:\n",
bech32: "tb",
bech32Opnet: "opt",
bip32: {
public: 70617039,
private: 70615956
},
pubKeyHash: 111,
scriptHash: 196,
wif: 239
};
var opnetTestnet = {
messagePrefix: "Bitcoin Signed Message:\n",
bech32: "opt",
bech32Opnet: "opt",
bip32: {
public: 70617039,
private: 70615956
},
pubKeyHash: 111,
scriptHash: 196,
wif: 239
};
/**
* Represents the Dogecoin mainnet configuration.
*
* Prefixes from:
* - P2PKH: 0x1e (30 decimal) - addresses start with 'D'
* - P2SH: 0x16 (22 decimal) - addresses often start with '9' or 'A'
* - WIF: 0x9e (158 decimal)
* - BIP32:
* - public: 0x02facafd
* - private: 0x02fac398
* Message prefix:
* - Dogecoin uses "\x19Dogecoin Signed Message:\n"
*/
var dogecoin = {
messagePrefix: "Dogecoin Signed Message:\n",
bech32: "",
bech32Opnet: "",
bip32: {
public: 49990397,
private: 49988504
},
pubKeyHash: 30,
scriptHash: 22,
wif: 158
};
/**
* Represents the Dogecoin testnet configuration.
*
* Prefixes from Dogecoin testnet chainparams:
* - P2PKH: 0x71 (113 decimal)
* - P2SH: 0xc4 (196 decimal)
* - WIF: 0xf1 (241 decimal)
* - BIP32:
* - public: 0x0432a9a8
* - private: 0x0432a243
* Message prefix:
* - Same as mainnet: "\x19Dogecoin Signed Message:\n"
*/
var dogecoinTestnet = {
messagePrefix: "Dogecoin Signed Message:\n",
bech32: "",
bech32Opnet: "",
bip32: {
public: 70429096,
private: 70427203
},
pubKeyHash: 113,
scriptHash: 196,
wif: 241
};
/**
* Litecoin mainnet configuration.
*/
var litecoin = {
messagePrefix: "Litecoin Signed Message:\n",
bech32: "ltc",
bech32Opnet: "opl",
bip32: {
public: 27108450,
private: 27106558
},
pubKeyHash: 48,
scriptHash: 50,
wif: 176
};
/**
* Litecoin testnet configuration.
*/
var litecoinTestnet = {
messagePrefix: "Litecoin Signed Message:\n",
bech32: "tltc",
bech32Opnet: "oplt",
bip32: {
public: 70709117,
private: 70711009
},
pubKeyHash: 111,
scriptHash: 58,
wif: 239
};
/**
* Bitcoin Cash mainnet configuration (legacy).
* Note: Bitcoin Cash uses Cashaddr starting with 'q' or 'p',
* but we retain the legacy prefixes for compatibility.
*/
var bitcoinCash = {
messagePrefix: "Bitcoin Signed Message:\n",
bech32: "bitcoincash",
bech32Opnet: "opbch",
bip32: {
public: 76067358,
private: 76066276
},
pubKeyHash: 0,
scriptHash: 5,
wif: 128
};
/**
* Bitcoin Cash testnet configuration (legacy).
*/
var bitcoinCashTestnet = {
messagePrefix: "Bitcoin Signed Message:\n",
bech32: "bchtest",
bech32Opnet: "opbcht",
bip32: {
public: 70617039,
private: 70615956
},
pubKeyHash: 111,
scriptHash: 196,
wif: 239
};
/**
* Dash mainnet configuration.
*/
var dash = {
messagePrefix: "DarkCoin Signed Message:\n",
bech32: "",
bech32Opnet: "",
bip32: {
public: 50221772,
private: 50221816
},
pubKeyHash: 76,
scriptHash: 16,
wif: 204
};
/**
* Dash testnet configuration.
*/
var dashTestnet = {
messagePrefix: "DarkCoin Signed Message:\n",
bech32: "",
bech32Opnet: "",
bip32: {
public: 981492128,
private: 981489719
},
pubKeyHash: 140,
scriptHash: 19,
wif: 239
};
//#endregion
//#region node_modules/@btc-vision/bitcoin/build/bech32utils.js
var import_dist = (/* @__PURE__ */ __commonJSMin(((exports) => {
Object.defineProperty(exports, "__esModule", { value: true });
exports.bech32m = exports.bech32 = void 0;
var ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
var ALPHABET_MAP = {};
for (let z = 0; z < 32; z++) {
const x = ALPHABET.charAt(z);
ALPHABET_MAP[x] = z;
}
function polymodStep(pre) {
const b = pre >> 25;
return (pre & 33554431) << 5 ^ -(b >> 0 & 1) & 996825010 ^ -(b >> 1 & 1) & 642813549 ^ -(b >> 2 & 1) & 513874426 ^ -(b >> 3 & 1) & 1027748829 ^ -(b >> 4 & 1) & 705979059;
}
function prefixChk(prefix) {
let chk = 1;
for (let i = 0; i < prefix.length; ++i) {
const c = prefix.charCodeAt(i);
if (c < 33 || c > 126) return "Invalid prefix (" + prefix + ")";
chk = polymodStep(chk) ^ c >> 5;
}
chk = polymodStep(chk);
for (let i = 0; i < prefix.length; ++i) {
const v = prefix.charCodeAt(i);
chk = polymodStep(chk) ^ v & 31;
}
return chk;
}
function convert(data, inBits, outBits, pad) {
let value = 0;
let bits = 0;
const maxV = (1 << outBits) - 1;
const result = [];
for (let i = 0; i < data.length; ++i) {
value = value << inBits | data[i];
bits += inBits;
while (bits >= outBits) {
bits -= outBits;
result.push(value >> bits & maxV);
}
}
if (pad) {
if (bits > 0) result.push(value << outBits - bits & maxV);
} else {
if (bits >= inBits) return "Excess padding";
if (value << outBits - bits & maxV) return "Non-zero padding";
}
return result;
}
function toWords(bytes) {
return convert(bytes, 8, 5, true);
}
function fromWordsUnsafe(words) {
const res = convert(words, 5, 8, false);
if (Array.isArray(res)) return res;
}
function fromWords(words) {
const res = convert(words, 5, 8, false);
if (Array.isArray(res)) return res;
throw new Error(res);
}
function getLibraryFromEncoding(encoding) {
let ENCODING_CONST;
if (encoding === "bech32") ENCODING_CONST = 1;
else ENCODING_CONST = 734539939;
function encode(prefix, words, LIMIT) {
LIMIT = LIMIT || 90;
if (prefix.length + 7 + words.length > LIMIT) throw new TypeError("Exceeds length limit");
prefix = prefix.toLowerCase();
let chk = prefixChk(prefix);
if (typeof chk === "string") throw new Error(chk);
let result = prefix + "1";
for (let i = 0; i < words.length; ++i) {
const x = words[i];
if (x >> 5 !== 0) throw new Error("Non 5-bit word");
chk = polymodStep(chk) ^ x;
result += ALPHABET.charAt(x);
}
for (let i = 0; i < 6; ++i) chk = polymodStep(chk);
chk ^= ENCODING_CONST;
for (let i = 0; i < 6; ++i) {
const v = chk >> (5 - i) * 5 & 31;
result += ALPHABET.charAt(v);
}
return result;
}
function __decode(str, LIMIT) {
LIMIT = LIMIT || 90;
if (str.length < 8) return str + " too short";
if (str.length > LIMIT) return "Exceeds length limit";
const lowered = str.toLowerCase();
const uppered = str.toUpperCase();
if (str !== lowered && str !== uppered) return "Mixed-case string " + str;
str = lowered;
const split = str.lastIndexOf("1");
if (split === -1) return "No separator character for " + str;
if (split === 0) return "Missing prefix for " + str;
const prefix = str.slice(0, split);
const wordChars = str.slice(split + 1);
if (wordChars.length < 6) return "Data too short";
let chk = prefixChk(prefix);
if (typeof chk === "string") return chk;
const words = [];
for (let i = 0; i < wordChars.length; ++i) {
const c = wordChars.charAt(i);
const v = ALPHABET_MAP[c];
if (v === void 0) return "Unknown character " + c;
chk = polymodStep(chk) ^ v;
if (i + 6 >= wordChars.length) continue;
words.push(v);
}
if (chk !== ENCODING_CONST) return "Invalid checksum for " + str;
return {
prefix,
words
};
}
function decodeUnsafe(str, LIMIT) {
const res = __decode(str, LIMIT);
if (typeof res === "object") return res;
}
function decode(str, LIMIT) {
const res = __decode(str, LIMIT);
if (typeof res === "object") return res;
throw new Error(res);
}
return {
decodeUnsafe,
decode,
encode,
toWords,
fromWordsUnsafe,
fromWords
};
}
exports.bech32 = getLibraryFromEncoding("bech32");
exports.bech32m = getLibraryFromEncoding("bech32m");
})))();
/**
* decode address with bech32 specification, return address version、address prefix and address data if valid
*/
function fromBech32(address) {
let result;
let version;
try {
result = import_dist.bech32.decode(address);
} catch (e) {}
if (result) {
version = result.words[0];
if (version !== 0) throw new TypeError(address + " uses wrong encoding");
} else {
result = import_dist.bech32m.decode(address);
version = result.words[0];
if (version === 0) throw new TypeError(address + " uses wrong encoding");
}
const data = import_dist.bech32.fromWords(result.words.slice(1));
return {
version,
prefix: result.prefix,
data: new Uint8Array(data)
};
}
//#endregion
//#region node_modules/varuint-bitcoin/node_modules/uint8array-tools/src/mjs/browser.js
var HEX_STRINGS$1 = "0123456789abcdefABCDEF";
HEX_STRINGS$1.split("").map((c) => c.codePointAt(0));
Array(256).fill(true).map((_, i) => {
const s = String.fromCodePoint(i);
const index = HEX_STRINGS$1.indexOf(s);
return index < 0 ? void 0 : index < 16 ? index : index - 6;
});
new TextEncoder();
new TextDecoder();
function writeUInt16$1(buffer, offset, value, littleEndian) {
if (offset + 2 > buffer.length) throw new Error("Offset is outside the bounds of Uint8Array");
littleEndian = littleEndian.toUpperCase();
if (value > 65535) throw new Error(`The value of "value" is out of range. It must be >= 0 and <= 65535. Received ${value}`);
if (littleEndian === "LE") {
buffer[offset] = value & 255;
buffer[offset + 1] = value >> 8 & 255;
} else {
buffer[offset] = value >> 8 & 255;
buffer[offset + 1] = value & 255;
}
}
function writeUInt32$1(buffer, offset, value, littleEndian) {
if (offset + 4 > buffer.length) throw new Error("Offset is outside the bounds of Uint8Array");
littleEndian = littleEndian.toUpperCase();
if (value > 4294967295) throw new Error(`The value of "value" is out of range. It must be >= 0 and <= 4294967295. Received ${value}`);
if (littleEndian === "LE") {
buffer[offset] = value & 255;
buffer[offset + 1] = value >> 8 & 255;
buffer[offset + 2] = value >> 16 & 255;
buffer[offset + 3] = value >> 24 & 255;
} else {
buffer[offset] = value >> 24 & 255;
buffer[offset + 1] = value >> 16 & 255;
buffer[offset + 2] = value >> 8 & 255;
buffer[offset + 3] = value & 255;
}
}
function writeUInt64$1(buffer, offset, value, littleEndian) {
if (offset + 8 > buffer.length) throw new Error("Offset is outside the bounds of Uint8Array");
littleEndian = littleEndian.toUpperCase();
if (value > 18446744073709551615n) throw new Error(`The value of "value" is out of range. It must be >= 0 and <= 18446744073709551615. Received ${value}`);
if (littleEndian === "LE") {
buffer[offset] = Number(value & 255n);
buffer[offset + 1] = Number(value >> 8n & 255n);
buffer[offset + 2] = Number(value >> 16n & 255n);
buffer[offset + 3] = Number(value >> 24n & 255n);
buffer[offset + 4] = Number(value >> 32n & 255n);
buffer[offset + 5] = Number(value >> 40n & 255n);
buffer[offset + 6] = Number(value >> 48n & 255n);
buffer[offset + 7] = Number(value >> 56n & 255n);
} else {
buffer[offset] = Number(value >> 56n & 255n);
buffer[offset + 1] = Number(value >> 48n & 255n);
buffer[offset + 2] = Number(value >> 40n & 255n);
buffer[offset + 3] = Number(value >> 32n & 255n);
buffer[offset + 4] = Number(value >> 24n & 255n);
buffer[offset + 5] = Number(value >> 16n & 255n);
buffer[offset + 6] = Number(value >> 8n & 255n);
buffer[offset + 7] = Number(value & 255n);
}
}
function readUInt16$1(buffer, offset, littleEndian) {
if (offset + 2 > buffer.length) throw new Error("Offset is outside the bounds of Uint8Array");
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
let num = 0;
num = (num << 8) + buffer[offset + 1];
num = (num << 8) + buffer[offset];
return num;
} else {
let num = 0;
num = (num << 8) + buffer[offset];
num = (num << 8) + buffer[offset + 1];
return num;
}
}
function readUInt32$1(buffer, offset, littleEndian) {
if (offset + 4 > buffer.length) throw new Error("Offset is outside the bounds of Uint8Array");
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
let num = 0;
num = (num << 8) + buffer[offset + 3] >>> 0;
num = (num << 8) + buffer[offset + 2] >>> 0;
num = (num << 8) + buffer[offset + 1] >>> 0;
num = (num << 8) + buffer[offset] >>> 0;
return num;
} else {
let num = 0;
num = (num << 8) + buffer[offset] >>> 0;
num = (num << 8) + buffer[offset + 1] >>> 0;
num = (num << 8) + buffer[offset + 2] >>> 0;
num = (num << 8) + buffer[offset + 3] >>> 0;
return num;
}
}
function readUInt64$1(buffer, offset, littleEndian) {
if (offset + 8 > buffer.length) throw new Error("Offset is outside the bounds of Uint8Array");
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
let num = 0n;
num = (num << 8n) + BigInt(buffer[offset + 7]);
num = (num << 8n) + BigInt(buffer[offset + 6]);
num = (num << 8n) + BigInt(buffer[offset + 5]);
num = (num << 8n) + BigInt(buffer[offset + 4]);
num = (num << 8n) + BigInt(buffer[offset + 3]);
num = (num << 8n) + BigInt(buffer[offset + 2]);
num = (num << 8n) + BigInt(buffer[offset + 1]);
num = (num << 8n) + BigInt(buffer[offset]);
return num;
} else {
let num = 0n;
num = (num << 8n) + BigInt(buffer[offset]);
num = (num << 8n) + BigInt(buffer[offset + 1]);
num = (num << 8n) + BigInt(buffer[offset + 2]);
num = (num << 8n) + BigInt(buffer[offset + 3]);
num = (num << 8n) + BigInt(buffer[offset + 4]);
num = (num << 8n) + BigInt(buffer[offset + 5]);
num = (num << 8n) + BigInt(buffer[offset + 6]);
num = (num << 8n) + BigInt(buffer[offset + 7]);
return num;
}
}
//#endregion
//#region node_modules/varuint-bitcoin/src/esm/index.js
var checkUInt64 = (n) => {
if (n < 0 || n > 18446744073709551615n) throw new RangeError("value out of range");
};
function checkUInt53(n) {
if (n < 0 || n > Number.MAX_SAFE_INTEGER || n % 1 !== 0) throw new RangeError("value out of range");
}
function checkUint53OrUint64(n) {
if (typeof n === "number") checkUInt53(n);
else checkUInt64(n);
}
function encode$20(n, buffer, offset) {
checkUint53OrUint64(n);
if (offset === void 0) offset = 0;
if (buffer === void 0) buffer = new Uint8Array(encodingLength$1(n));
let bytes = 0;
if (n < 253) {
buffer.set([Number(n)], offset);
bytes = 1;
} else if (n <= 65535) {
buffer.set([253], offset);
writeUInt16$1(buffer, offset + 1, Number(n), "LE");
bytes = 3;
} else if (n <= 4294967295) {
buffer.set([254], offset);
writeUInt32$1(buffer, offset + 1, Number(n), "LE");
bytes = 5;
} else {
buffer.set([255], offset);
writeUInt64$1(buffer, offset + 1, BigInt(n), "LE");
bytes = 9;
}
return {
buffer,
bytes
};
}
function decode$19(buffer, offset) {
if (offset === void 0) offset = 0;
const first = buffer.at(offset);
if (first === void 0) throw new Error("buffer too small");
if (first < 253) return {
numberValue: first,
bigintValue: BigInt(first),
bytes: 1
};
else if (first === 253) {
const val = readUInt16$1(buffer, offset + 1, "LE");
return {
numberValue: val,
bigintValue: BigInt(val),
bytes: 3
};
} else if (first === 254) {
const val = readUInt32$1(buffer, offset + 1, "LE");
return {
numberValue: val,
bigintValue: BigInt(val),
bytes: 5
};
} else {
const number = readUInt64$1(buffer, offset + 1, "LE");
return {
numberValue: number <= Number.MAX_SAFE_INTEGER ? Number(number) : null,
bigintValue: number,
bytes: 9
};
}
}
function encodingLength$1(n) {
checkUint53OrUint64(n);
return n < 253 ? 1 : n <= 65535 ? 3 : n <= 4294967295 ? 5 : 9;
}
//#endregion
//#region node_modules/@btc-vision/bitcoin/build/io/hex.js
/**
* Hex encoding and decoding utilities.
*
* Direct implementations with zero dependencies and no wrappers.
* Optimized for performance with lookup tables.
*
* @packageDocumentation
*/
/**
* Lookup table for byte to hex conversion.
* Pre-computed for O(1) lookup per byte.
*/
var BYTE_TO_HEX = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
/**
* Lookup table for hex character to nibble conversion.
* Returns -1 for invalid characters.
*/
var HEX_TO_NIBBLE = (() => {
const table = new Array(128).fill(-1);
for (let i = 0; i < 10; i++) table[48 + i] = i;
for (let i = 0; i < 6; i++) {
table[65 + i] = 10 + i;
table[97 + i] = 10 + i;
}
return table;
})();
/**
* Converts a Uint8Array to a lowercase hex string.
*
* @param bytes - The byte array to convert
* @returns Lowercase hex string representation
*
* @example
* ```typescript
* import { toHex } from '@btc-vision/bitcoin';
*
* const bytes = new Uint8Array([0xde, 0xad, 0xbe, 0xef]);
* const hex = toHex(bytes);
* console.log(hex); // 'deadbeef'
* ```
*/
function toHex$1(bytes) {
let result = "";
for (let i = 0; i < bytes.length; i++) {
const hex = BYTE_TO_HEX[bytes[i]];
result += hex;
}
return result;
}
/**
* Converts a hex string to a Uint8Array.
*
* Accepts hex strings with or without '0x' prefix.
* Case-insensitive (accepts both 'DEADBEEF' and 'deadbeef').
*
* @param hex - The hex string to convert
* @returns Uint8Array containing the decoded bytes
* @throws TypeError if hex string has odd length
* @throws TypeError if hex string contains invalid characters
*
* @example
* ```typescript
* import { fromHex, toHex } from '@btc-vision/bitcoin';
*
* const bytes = fromHex('deadbeef');
* console.log(bytes); // Uint8Array [222, 173, 190, 239]
*
* // Also works with 0x prefix
* const bytes2 = fromHex('0xCAFEBABE');
* console.log(toHex(bytes2)); // 'cafebabe'
* ```
*/
function fromHex$1(hex) {
if (hex.length >= 2 && hex[0] === "0" && (hex[1] === "x" || hex[1] === "X")) hex = hex.slice(2);
const len = hex.length;
if (len % 2 !== 0) throw new TypeError("Invalid hex string: odd length");
const byteLength = len / 2;
const result = new Uint8Array(byteLength);
for (let i = 0; i < byteLength; i++) {
const charIndex = i * 2;
const highCode = hex.charCodeAt(charIndex);
const lowCode = hex.charCodeAt(charIndex + 1);
if (highCode >= 128 || lowCode >= 128) throw new TypeError(`Invalid hex character at position ${charIndex}`);
const high = HEX_TO_NIBBLE[highCode];
const low = HEX_TO_NIBBLE[lowCode];
if (high === -1) throw new TypeError(`Invalid hex character at position ${charIndex}`);
if (low === -1) throw new TypeError(`Invalid hex character at position ${charIndex + 1}`);
result[i] = high << 4 | low;
}
return result;
}
//#endregion
//#region node_modules/@btc-vision/bitcoin/build/io/BinaryReader.js
/**
* Stateful binary reader with a single DataView instance.
*
* Zero allocations during read operations. The DataView is created once
* in the constructor and reused for all reads.
*
* @packageDocumentation
*/
/**
* High-performance binary reader for parsing binary data.
*
* Creates exactly ONE DataView instance that is reused for all read operations.
* This eliminates garbage collection pressure from repeated DataView allocations.
*
* @example
* ```typescript
* import { BinaryReader, fromHex } from '@btc-vision/bitcoin';
*
* // Parse a Bitcoin transaction
* const data = fromHex('01000000...');
* const reader = new BinaryReader(data);
*
* const version = reader.readInt32LE();
* const inputCount = reader.readVarInt();
*
* for (let i = 0; i < inputCount; i++) {
* const txid = reader.readBytes(32);
* const vout = reader.readUInt32LE();
* const script = reader.readVarBytes();
* const sequence = reader.readUInt32LE();
* }
* ```
*/
var BinaryReader$1 = class BinaryReader$1 {
/**
* The underlying byte array.
*/
#data;
/**
* Single DataView instance reused for all reads.
*/
#view;
/**
* Current read position.
*/
#offset;
/**
* Creates a new BinaryReader.
*
* @param data - The byte array to read from
* @param offset - Initial read position (default 0)
*
* @example
* ```typescript
* import { BinaryReader, fromHex } from '@btc-vision/bitcoin';
*
* const data = fromHex('01020304');
* const reader = new BinaryReader(data);
* ```
*/
constructor(data, offset = 0) {
this.#data = data;
this.#view = new DataView(data.buffer, data.byteOffset, data.byteLength);
this.#offset = offset;
}
/**
* Current read position in the buffer.
*/
get offset() {
return this.#offset;
}
/**
* Sets the read position.
*
* @param value - New offset value
* @throws RangeError if offset is negative or beyond buffer length
*/
set offset(value) {
if (value < 0 || value > this.#data.length) throw new RangeError(`Offset ${value} is out of bounds [0, ${this.#data.length}]`);
this.#offset = value;
}
/**
* Total length of the underlying buffer.
*/
get length() {
return this.#data.length;
}
/**
* Number of bytes remaining to be read.
*/
get remaining() {
return this.#data.length - this.#offset;
}
/**
* The underlying data buffer.
*/
get data() {
return this.#data;
}
/**
* Creates a BinaryReader from a hex string.
*
* @param hex - Hex string (with or without 0x prefix)
* @returns A new BinaryReader instance
*
* @example
* ```typescript
* import { BinaryReader } from '@btc-vision/bitcoin';
*
* const reader = BinaryReader.fromHex('01000000');
* const version = reader.readInt32LE(); // 1
* ```
*/
static fromHex(hex) {
return new BinaryReader$1(fromHex$1(hex));
}
/**
* Reads an 8-bit unsigned integer.
*
* @returns The value (0-255)
* @throws RangeError if reading past end of buffer
*
* @example
* ```typescript
* const reader = BinaryReader.fromHex('ff');
* reader.readUInt8(); // 255
* ```
*/
readUInt8() {
if (this.#offset >= this.#data.length) throw new RangeError("Read past end of buffer");
return this.#data[this.#offset++];
}
/**
* Reads an 8-bit signed integer.
*
* @returns The value (-128 to 127)
* @throws RangeError if reading past end of buffer
*/
readInt8() {
const value = this.readUInt8();
return value > 127 ? value - 256 : value;
}
/**
* Reads a 16-bit unsigned integer in little-endian format.
*
* @returns The value (0-65535)
* @throws RangeError if reading past end of buffer
*
* @example
* ```typescript
* const reader = BinaryReader.fromHex('0100'); // 1 in LE
* reader.readUInt16LE(); // 1
* ```
*/
readUInt16LE() {
if (this.#offset + 2 > this.#data.length) throw new RangeError("Read past end of buffer");
const value = this.#view.getUint16(this.#offset, true);
this.#offset += 2;
return value;
}
/**
* Reads a 16-bit signed integer in little-endian format.
*
* @returns The value (-32768 to 32767)
* @throws RangeError if reading past end of buffer
*/
readInt16LE() {
if (this.#offset + 2 > this.#data.length) throw new RangeError("Read past end of buffer");
const value = this.#view.getInt16(this.#offset, true);
this.#offset += 2;
return value;
}
/**
* Reads a 32-bit unsigned integer in little-endian format.
*
* @returns The value (0 to 4294967295)
* @throws RangeError if reading past end of buffer
*
* @example
* ```typescript
* const reader = BinaryReader.fromHex('01000000'); // 1 in LE
* reader.readUInt32LE(); // 1
* ```
*/
readUInt32LE() {
if (this.#offset + 4 > this.#data.length) throw new RangeError("Read past end of buffer");
const value = this.#view.getUint32(this.#offset, true);
this.#offset += 4;
return value;
}
/**
* Reads a 32-bit signed integer in little-endian format.
*
* @returns The value (-2147483648 to 2147483647)
* @throws RangeError if reading past end of buffer
*
* @example
* ```typescript
* const reader = BinaryReader.fromHex('ffffffff'); // -1 in LE signed
* reader.readInt32LE(); // -1
* ```
*/
readInt32LE() {
if (this.#offset + 4 > this.#data.length) throw new RangeError("Read past end of buffer");
const value = this.#view.getInt32(this.#offset, true);
this.#offset += 4;
return value;
}
/**
* Reads a 64-bit unsigned integer in little-endian format as bigint.
*
* @returns The value as bigint
* @throws RangeError if reading past end of buffer
*
* @example
* ```typescript
* const reader = BinaryReader.fromHex('0100000000000000'); // 1 in LE 64-bit
* reader.readUInt64LE(); // 1n
* ```
*/
readUInt64LE() {
if (this.#offset + 8 > this.#data.length) throw new RangeError("Read past end of buffer");
const value = this.#view.getBigUint64(this.#offset, true);
this.#offset += 8;
return value;
}
/**
* Reads a 64-bit signed integer in little-endian format as bigint.
*
* @returns The value as bigint
* @throws RangeError if reading past end of buffer
*/
readInt64LE() {
if (this.#offset + 8 > this.#data.length) throw new RangeError("Read past end of buffer");
const value = this.#view.getBigInt64(this.#offset, true);
this.#offset += 8;
return value;
}
/**
* Reads a specified number of bytes.
*
* Returns a subarray view (no copy) for performance.
* Use readBytesCopy() if you need an independent copy.
*
* @param length - Number of bytes to read
* @returns Uint8Array view into the buffer
* @throws RangeError if reading past end of buffer
*
* @example
* ```typescript
* const reader = BinaryReader.fromHex('deadbeefcafebabe');
* const first4 = reader.readBytes(4); // deadbeef
* const next4 = reader.readBytes(4); // cafebabe
* ```
*/
readBytes(length) {
if (this.#offset + length > this.#data.length) throw new RangeError("Read past end of buffer");
const value = this.#data.subarray(this.#offset, this.#offset + length);
this.#offset += length;
return value;
}
/**
* Reads a specified number of bytes as an independent copy.
*
* @param length - Number of bytes to read
* @returns New Uint8Array with copied data
* @throws RangeError if reading past end of buffer
*/
readBytesCopy(length) {
if (this.#offset + length > this.#data.length) throw new RangeError("Read past end of buffer");
const value = this.#data.slice(this.#offset, this.#offset + length);
this.#offset += length;
return value;
}
/**
* Reads a Bitcoin CompactSize variable-length integer.
*
* CompactSize encoding:
* - 0x00-0xFC: 1 byte (value as-is)
* - 0xFD: 3 bytes (0xFD + 2-byte LE uint16)
* - 0xFE: 5 bytes (0xFE + 4-byte LE uint32)
* - 0xFF: 9 bytes (0xFF + 8-byte LE uint64)
*
* @returns The decoded integer value
* @throws RangeError if reading past end of buffer
* @throws RangeError if value exceeds MAX_SAFE_INTEGER
*
* @example
* ```typescript
* const reader = BinaryReader.fromHex('fd0302'); // 515 encoded
* reader.readVarInt(); // 515
* ```
*/
readVarInt() {
const first = this.readUInt8();
if (first < 253) return first;
if (first === 253) return this.readUInt16LE();
if (first === 254) return this.readUInt32LE();
const value = this.readUInt64LE();
if (value > BigInt(Number.MAX_SAFE_INTEGER)) throw new RangeError("VarInt value exceeds MAX_SAFE_INTEGER");
return Number(value);
}
/**
* Reads a Bitcoin CompactSize variable-length integer as bigint.
*
* Use this when you need the full 64-bit range.
*
* @returns The decoded integer value as bigint
* @throws RangeError if reading past end of buffer
*/
readVarIntBig() {
const first = this.readUInt8();
if (first < 253) return BigInt(first);
if (first === 253) return BigInt(this.readUInt16LE());
if (first === 254) return BigInt(this.readUInt32LE());
return this.readUInt64LE();
}
/**
* Reads a length-prefixed byte array (VarInt length + bytes).
*
* @returns Uint8Array view into the buffer
* @throws RangeError if reading past end of buffer
*
* @example
* ```typescript
* const reader = BinaryReader.fromHex('04deadbeef');
* reader.readVarBytes(); // Uint8Array [0xde, 0xad, 0xbe, 0xef]
* ```
*/
readVarBytes() {
const length = this.readVarInt();
return this.readBytes(length);
}
/**
* Reads an array of length-prefixed byte arrays.
*
* Format: VarInt count + (VarInt length + bytes) for each item.
*
* @returns Array of Uint8Array views
* @throws RangeError if reading past end of buffer
*/
readVector() {
const count = this.readVarInt();
const result = [];
for (let i = 0; i < count; i++) result.push(this.readVarBytes());
return result;
}
/**
* Peeks at the next byte without advancing the position.
*
* @returns The next byte value, or undefined if at end
*/
peek() {
if (this.#offset >= this.#data.length) return;
return this.#data[this.#offset];
}
/**
* Skips a specified number of bytes.
*
* @param length - Number of bytes to skip
* @throws RangeError if skipping past end of buffer
*/
skip(length) {
if (this.#offset + length > this.#data.length) throw new RangeError("Skip past end of buffer");
this.#offset += length;
}
/**
* Resets the read position to the beginning.
*/
reset() {
this.#offset = 0;
}
/**
* Checks if there are more bytes to read.
*
* @returns True if there are remaining bytes
*/
hasMore() {
return this.#offset < this.#data.length;
}
};
//#endregion
//#region node_modules/@btc-vision/bitcoin/build/io/BinaryWriter.js
/**
* Stateful binary writer with a single DataView instance.
*
* Zero allocations during write operations. The DataView is created once
* in the constructor and reused for all writes.
*
* @packageDocumentation
*/
/**
* High-performance binary writer for serializing binary data.
*
* Creates exactly ONE DataView instance that is reused for all write operations.
* This eliminates garbage collection pressure from repeated DataView allocations.
*
* Methods return `this` for chaining.
*
* @example
* ```typescript
* import { BinaryWriter } from '@btc-vision/bitcoin';
*
* // Serialize a simple structure
* const writer = new BinaryWriter(16);
* writer
* .writeInt32LE(1) // version
* .writeUInt32LE(0) // input count
* .writeUInt32LE(0) // output count
* .writeUInt32LE(0); // locktime
*
* const bytes = writer.finish();
* ```
*/
var BinaryWriter$1 = class {
/**
* The underlying byte array.
*/
#data;
/**
* Single DataView instance reused for all writes.
*/
#view;
/**
* Current write position.
*/
#offset;
constructor(arg, offset = 0) {
if (typeof arg === "number") {
this.#data = new Uint8Array(arg);
this.#offset = 0;
} else {
this.#data = arg;
this.#offset = offset;
}
this.#view = new DataView(this.#data.buffer, this.#data.byteOffset, this.#data.byteLength);
}
/**
* Current write position in the buffer.
*/
get offset() {
return this.#offset;
}
/**
* Sets the write position.
*
* @param value - New offset value
* @throws RangeError if offset is negative or beyond buffer length
*/
set offset(value) {
if (value < 0 || value > this.#data.length) throw new RangeError(`Offset ${value} is out of bounds [0, ${this.#data.length}]`);
this.#offset = value;
}
/**
* Total capacity of the underlying buffer.
*/
get capacity() {
return this.#data.length;
}
/**
* Number of bytes remaining in the buffer.
*/
get remaining() {
return this.#data.length - this.#offset;
}
/**
* The underlying data buffer.
*/
get data() {
return this.#data;
}
/**
* Creates a BinaryWriter with automatic capacity management.
*
* Initial capacity is 256 bytes, grows as needed.
*
* @returns A new GrowableBinaryWriter instance
*
* @example
* ```typescript
* import { BinaryWriter } from '@btc-vision/bitcoin';
*
* const writer = BinaryWriter.growable();
* writer.writeUInt32LE(1);
* writer.writeBytes(new Uint8Array(1000)); // Automatically grows
* ```
*/
static growable(initialCapacity = 256) {
return new GrowableBinaryWriter(initialCapacity);
}
/**
* Writes an 8-bit unsigned integer.
*
* @param value - Value to write (0-255)
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*
* @example
* ```typescript
* writer.writeUInt8(255);
* ```
*/
writeUInt8(value) {
if (this.#offset >= this.#data.length) throw new RangeError("Write past end of buffer");
this.#data[this.#offset++] = value & 255;
return this;
}
/**
* Writes an 8-bit signed integer.
*
* @param value - Value to write (-128 to 127)
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*/
writeInt8(value) {
return this.writeUInt8(value < 0 ? value + 256 : value);
}
/**
* Writes a 16-bit unsigned integer in little-endian format.
*
* @param value - Value to write (0-65535)
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*
* @example
* ```typescript
* writer.writeUInt16LE(1); // Writes 01 00
* ```
*/
writeUInt16LE(value) {
if (this.#offset + 2 > this.#data.length) throw new RangeError("Write past end of buffer");
this.#view.setUint16(this.#offset, value, true);
this.#offset += 2;
return this;
}
/**
* Writes a 16-bit signed integer in little-endian format.
*
* @param value - Value to write (-32768 to 32767)
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*/
writeInt16LE(value) {
if (this.#offset + 2 > this.#data.length) throw new RangeError("Write past end of buffer");
this.#view.setInt16(this.#offset, value, true);
this.#offset += 2;
return this;
}
/**
* Writes a 32-bit unsigned integer in little-endian format.
*
* @param value - Value to write (0 to 4294967295)
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*
* @example
* ```typescript
* writer.writeUInt32LE(1); // Writes 01 00 00 00
* ```
*/
writeUInt32LE(value) {
if (this.#offset + 4 > this.#data.length) throw new RangeError("Write past end of buffer");
this.#view.setUint32(this.#offset, value, true);
this.#offset += 4;
return this;
}
/**
* Writes a 32-bit signed integer in little-endian format.
*
* @param value - Value to write (-2147483648 to 2147483647)
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*
* @example
* ```typescript
* writer.writeInt32LE(-1); // Writes ff ff ff ff
* ```
*/
writeInt32LE(value) {
if (this.#offset + 4 > this.#data.length) throw new RangeError("Write past end of buffer");
this.#view.setInt32(this.#offset, value, true);
this.#offset += 4;
return this;
}
/**
* Writes a 64-bit unsigned integer in little-endian format.
*
* @param value - Value to write as bigint
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*
* @example
* ```typescript
* writer.writeUInt64LE(50000n); // Writes 50 c3 00 00 00 00 00 00
* ```
*/
writeUInt64LE(value) {
if (this.#offset + 8 > this.#data.length) throw new RangeError("Write past end of buffer");
this.#view.setBigUint64(this.#offset, value, true);
this.#offset += 8;
return this;
}
/**
* Writes a 64-bit signed integer in little-endian format.
*
* @param value - Value to write as bigint
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*/
writeInt64LE(value) {
if (this.#offset + 8 > this.#data.length) throw new RangeError("Write past end of buffer");
this.#view.setBigInt64(this.#offset, value, true);
this.#offset += 8;
return this;
}
/**
* Writes raw bytes.
*
* @param bytes - Bytes to write
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*
* @example
* ```typescript
* writer.writeBytes(new Uint8Array([0xde, 0xad, 0xbe, 0xef]));
* ```
*/
writeBytes(bytes) {
if (this.#offset + bytes.length > this.#data.length) throw new RangeError("Write past end of buffer");
this.#data.set(bytes, this.#offset);
this.#offset += bytes.length;
return this;
}
/**
* Writes a Bitcoin CompactSize variable-length integer.
*
* CompactSize encoding:
* - 0x00-0xFC: 1 byte (value as-is)
* - 0xFD-0xFFFF: 3 bytes (0xFD + 2-byte LE uint16)
* - 0x10000-0xFFFFFFFF: 5 bytes (0xFE + 4-byte LE uint32)
* - Larger: 9 bytes (0xFF + 8-byte LE uint64)
*
* @param value - Value to write
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*
* @example
* ```typescript
* writer.writeVarInt(252); // Writes fc
* writer.writeVarInt(253); // Writes fd fd 00
* writer.writeVarInt(65535); // Writes fd ff ff
* writer.writeVarInt(65536); // Writes fe 00 00 01 00
* ```
*/
writeVarInt(value) {
if (value < 253) return this.writeUInt8(value);
if (value <= 65535) {
this.writeUInt8(253);
return this.writeUInt16LE(value);
}
if (value <= 4294967295) {
this.writeUInt8(254);
return this.writeUInt32LE(value);
}
this.writeUInt8(255);
return this.writeUInt64LE(BigInt(value));
}
/**
* Writes a Bitcoin CompactSize variable-length integer from bigint.
*
* @param value - Value to write as bigint
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*/
writeVarIntBig(value) {
if (value < 253n) return this.writeUInt8(Number(value));
if (value <= 65535n) {
this.writeUInt8(253);
return this.writeUInt16LE(Number(value));
}
if (value <= 4294967295n) {
this.writeUInt8(254);
return this.writeUInt32LE(Number(value));
}
this.writeUInt8(255);
return this.writeUInt64LE(value);
}
/**
* Writes a length-prefixed byte array (VarInt length + bytes).
*
* @param bytes - Bytes to write
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*
* @example
* ```typescript
* writer.writeVarBytes(new Uint8Array([0xde, 0xad])); // Writes 02 de ad
* ```
*/
writeVarBytes(bytes) {
this.writeVarInt(bytes.length);
return this.writeBytes(bytes);
}
/**
* Writes an array of length-prefixed byte arrays.
*
* Format: VarInt count + (VarInt length + bytes) for each item.
*
* @param vector - Array of byte arrays to write
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*/
writeVector(vector) {
this.writeVarInt(vector.length);
for (const item of vector) this.writeVarBytes(item);
return this;
}
/**
* Fills a region with a specific byte value.
*
* @param value - Byte value to fill with
* @param length - Number of bytes to fill
* @returns This writer for chaining
* @throws RangeError if writing past end of buffer
*/
fill(value, length) {
if (this.#offset + length > this.#data.length) throw new RangeError("Write past end of buffer");
this.#data.fill(value, this.#offset, this.#offset + length);
this.#offset += length;
return this;
}
/**
* Skips a specified number of bytes (leaves them unchanged).
*
* @param length - Number of bytes to skip
* @returns This writer for chaining
* @throws RangeError if skipping past end of buffer
*/
skip(length) {
if (this.#offset + length > this.#data.length) throw new RangeError("Skip past end of buffer");
this.#offset += length;
return this;
}
/**
* Resets the write position to the beginning.
*
* @returns This writer for chaining
*/
reset() {
this.#offset = 0;
return this;
}
/**
* Verifies the buffer was fully written and returns it.
*
* Unlike {@link finish}, this method throws if the writer has not
* written exactly to the end of the buffer.
*
* @returns The underlying buffer
* @throws Error if the buffer was not fully written
*
* @example
* ```typescript
* const writer = new BinaryWriter(8);
* writer.writeUInt32LE(1);
* writer.writeUInt32LE(2);
* const bytes = writer.end(); // OK: wrote exactly 8 bytes
*
* const writer2 = new BinaryWriter(8);
* writer2.writeUInt32LE(1);
* writer2.end(); // throws: buffer size 8, offset 4
* ```
*/
end() {
if (this.#offset === this.#data.length) return this.#data;
throw new Error(`buffer size ${this.#data.length}, offset ${this.#offset}`);
}
/**
* Returns the written portion of the buffer.
*
* If the entire buffer was written, returns the buffer directly (no copy).
* Otherwise, returns a subarray view.
*
* @returns Uint8Array containing the written data
*
* @example
* ```typescript
* const writer = new BinaryWriter(100);
* writer.writeUInt32LE(42);
* const bytes = writer.finish(); // 4 bytes
* ```
*/
finish() {
return this.#offset === this.#data.length ? this.#data : this.#data.subarray(0, this.#offset);
}
/**
* Returns the written portion as a hex string.
*
* @returns Hex string representation
*
* @example
* ```typescript
* const writer = new BinaryWriter(4);
* writer.writeUInt32LE(1);
* writer.toHex(); // '01000000'
* ```
*/
toHex() {
return toHex$1(this.finish());
}
};
/**
* A BinaryWriter that automatically grows its buffer as needed.
*
* Use when the final size is unknown.
*
* @example
* ```typescript
* const writer = BinaryWriter.growable();
* writer.writeBytes(largeData); // Automatically grows
* const bytes = writer.finish();
* ```
*/
var GrowableBinaryWriter = class {
#data;
#view;
#offset = 0;
/**
* Creates a new GrowableBinaryWriter.
*
* @param initialCapacity - Initial buffer size (default 256)
*/
constructor(initialCapacity = 256) {
this.#data = new Uint8Array(initialCapacity);
this.#view = new DataView(this.#data.buffer);
}
/**
* Current write position.
*/
get offset() {
return this.#offset;
}
/**
* Sets the write position.
*
* @param value - New offset value
* @throws RangeError if offset is negative
*/
set offset(value) {
if (value < 0) throw new RangeError(`Offset ${value} cannot be negative`);
this.#offset = value;
}
/**
* Current buffer capacity.
*/
get capacity() {
return this.#data.length;
}
writeUInt8(value) {
this.#ensureCapacity(1);
this.#data[this.#offset++] = value & 255;
return this;
}
writeUInt16LE(value) {
this.#ensureCapacity(2);
this.#view.setUint16(this.#offset, value, true);
this.#offset += 2;
return this;
}
writeUInt32LE(value) {
this.#ensureCapacity(4);
this.#view.setUint32(this.#offset, value, true);
this.#offset += 4;
return this;
}
writeInt32LE(value) {
this.#ensureCapacity(4);
this.#view.setInt32(this.#offset, value, true);
this.#offset += 4;
return this;
}
writeUInt64LE(value) {
this.#ensureCapacity(8);
this.#view.setBigUint64(this.#offset, value, true);
this.#offset += 8;
return this;
}
writeBytes(bytes) {
this.#ensureCapacity(bytes.length);
this.#data.set(bytes, this.#offset);
this.#offset += bytes.length;
return this;
}
writeVarInt(value) {
if (value < 253) return this.writeUInt8(value);
if (value <= 65535) {
this.writeUInt8(253);
return this.writeUInt16LE(value);
}
if (value <= 4294967295) {
this.writeUInt8(254);
return this.writeUInt32LE(value);
}
this.writeUInt8(255);
return this.writeUInt64LE(BigInt(value));
}
writeVarBytes(bytes) {
this.writeVarInt(bytes.length);
return this.writeBytes(bytes);
}
writeVector(vector) {
this.writeVarInt(vector.length);
for (const item of vector) this.writeVarBytes(item);
return this;
}
/**
* Returns the written data as a new Uint8Array.
*
* @returns Copy of the written data
*/
finish() {
return this.#data.slice(0, this.#offset);
}
/**
* Returns the written data as a hex string.
*
* @returns Hex string representation
*/
toHex() {
return toHex$1(this.finish());
}
/**
* Ensures the buffer has enough space for additional bytes.
*
* @param additionalBytes - Number of additional bytes needed
*/
#ensureCapacity(additionalBytes) {
const required = this.#offset + additionalBytes;
if (required <= this.#data.length) return;
let newCapacity = this.#data.length * 2;
while (newCapacity < required) newCapacity *= 2;
const newData = new Uint8Array(newCapacity);
newData.set(this.#data.subarray(0, this.#offset));
this.#data = newData;
this.#view = new DataView(this.#data.buffer);
}
};
//#endregion
//#region node_modules/@btc-vision/bitcoin/build/io/base64.js
/**
* Base64 encoding and decoding utilities.
*
* @packageDocumentation
*/
/**
* Decodes a base64 string to a Uint8Array.
*
* @param base64 - The base64-encoded string to decode
* @returns Uint8Array containing the decoded bytes
*/
function fromBase64$1(base64) {
const binaryString = atob(base64);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) bytes[i] = binaryString.charCodeAt(i);
return bytes;
}
/**
* Encode a Uint8Array into a Base64 string.
* Uses the native btoa() available in both browser and service worker contexts.
*
* @param bytes - The bytes to encode
* @returns Base64-encoded string
*/
function toBase64$1(bytes) {
let binaryString = "";
for (let i = 0; i < bytes.length; i++) binaryString += String.fromCharCode(bytes[i]);
return btoa(binaryString);
}
//#endregion
//#region node_modules/base-x/src/esm/index.js
function base(ALPHABET) {
if (ALPHABET.length >= 255) throw new TypeError("Alphabet too long");
const BASE_MAP = new Uint8Array(256);
for (let j = 0; j < BASE_MAP.length; j++) BASE_MAP[j] = 255;
for (let i = 0; i < ALPHABET.length; i++) {
const x = ALPHABET.charAt(i);
const xc = x.charCodeAt(0);
if (BASE_MAP[xc] !== 255) throw new TypeError(x + " is ambiguous");
BASE_MAP[xc] = i;
}
const BASE = ALPHABET.length;
const LEADER = ALPHABET.charAt(0);
const FACTOR = Math.log(BASE) / Math.log(256);
const iFACTOR = Math.log(256) / Math.log(BASE);
function encode(source) {
if (source instanceof Uint8Array) {} else if (ArrayBuffer.isView(source)) source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
else if (Array.isArray(source)) source = Uint8Array.from(source);
if (!(source instanceof Uint8Array)) throw new TypeError("Expected Uint8Array");
if (source.length === 0) return "";
let zeroes = 0;
let length = 0;
let pbegin = 0;
const pend = source.length;
while (pbegin !== pend && source[pbegin] === 0) {
pbegin++;
zeroes++;
}
const size = (pend - pbegin) * iFACTOR + 1 >>> 0;
const b58 = new Uint8Array(size);
while (pbegin !== pend) {
let carry = source[pbegin];
let i = 0;
for (let it1 = size - 1; (carry !== 0 || i < length) && it1 !== -1; it1--, i++) {
carry += 256 * b58[it1] >>> 0;
b58[it1] = carry % BASE >>> 0;
carry = carry / BASE >>> 0;
}
if (carry !== 0) throw new Error("Non-zero carry");
length = i;
pbegin++;
}
let it2 = size - length;
while (it2 !== size && b58[it2] === 0) it2++;
let str = LEADER.repeat(zeroes);
for (; it2 < size; ++it2) str += ALPHABET.charAt(b58[it2]);
return str;
}
function decodeUnsafe(source) {
if (typeof source !== "string") throw new TypeError("Expected String");
if (source.length === 0) return new Uint8Array();
let psz = 0;
let zeroes = 0;
let length = 0;
while (source[psz] === LEADER) {
zeroes++;
psz++;
}
const size = (source.length - psz) * FACTOR + 1 >>> 0;
const b256 = new Uint8Array(size);
while (psz < source.length) {
const charCode = source.charCodeAt(psz);
if (charCode > 255) return;
let carry = BASE_MAP[charCode];
if (carry === 255) return;
let i = 0;
for (let it3 = size - 1; (carry !== 0 || i < length) && it3 !== -1; it3--, i++) {
carry += BASE * b256[it3] >>> 0;
b256[it3] = carry % 256 >>> 0;
carry = carry / 256 >>> 0;
}
if (carry !== 0) throw new Error("Non-zero carry");
length = i;
psz++;
}
let it4 = size - length;
while (it4 !== size && b256[it4] === 0) it4++;
const vch = new Uint8Array(zeroes + (size - it4));
let j = zeroes;
while (it4 !== size) vch[j++] = b256[it4++];
return vch;
}
function decode(string) {
const buffer = decodeUnsafe(string);
if (buffer) return buffer;
throw new Error("Non-base" + BASE + " character");
}
return {
encode,
decodeUnsafe,
decode
};
}
var init_esm$1 = __esmMin((() => {}));
//#endregion
//#region node_modules/bs58/src/esm/index.js
var ALPHABET, esm_default;
var init_esm = __esmMin((() => {
init_esm$1();
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
esm_default = base(ALPHABET);
}));
//#endregion
//#region node_modules/@btc-vision/bs58check/build/base.js
function bs58checkBase(checksumFn) {
function encode(payload) {
const checksum = checksumFn(payload);
const length = payload.length + 4;
const both = new Uint8Array(length);
both.set(payload, 0);
both.set(checksum.subarray(0, 4), payload.length);
return esm_default.encode(both);
}
function decodeRaw(buffer) {
const payload = buffer.slice(0, -4);
const checksum = buffer.slice(-4);
const newChecksum = checksumFn(payload);
if (checksum[0] ^ newChecksum[0] | checksum[1] ^ newChecksum[1] | checksum[2] ^ newChecksum[2] | checksum[3] ^ newChecksum[3]) return;
return payload;
}
function decodeUnsafe(str) {
const buffer = esm_default.decodeUnsafe(str);
if (buffer === void 0) return void 0;
return decodeRaw(buffer);
}
function decode(str) {
const payload