UNPKG

@btc-stamps/tx-builder

Version:

Transaction builder for Bitcoin Stamps and SRC-20 tokens with advanced UTXO selection

1,469 lines (1,452 loc) 843 kB
import { Buffer as Buffer$1 } from 'buffer'; import { createHash } from 'crypto'; import process2 from 'process'; import * as fs from 'fs'; import * as path from 'path'; import * as net from 'net'; import * as tls from 'tls'; import { EventEmitter } from 'events'; import * as zlib from 'zlib'; import * as msgpack2 from 'msgpack-lite'; import { promisify } from 'util'; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) { if (typeof require !== "undefined") return require.apply(this, arguments); throw Error('Dynamic require of "' + x + '" is not supported'); }); var __esm = (fn, res) => function __init() { return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; }; var __commonJS = (cb, mod) => function __require2() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. __defProp(target, "default", { value: mod, enumerable: true }) , mod )); // node_modules/bitcoinjs-lib/src/networks.js var require_networks = __commonJS({ "node_modules/bitcoinjs-lib/src/networks.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.testnet = exports.regtest = exports.bitcoin = void 0; exports.bitcoin = { /** * The message prefix used for signing Bitcoin messages. */ messagePrefix: "Bitcoin Signed Message:\n", /** * The Bech32 prefix used for Bitcoin addresses. */ bech32: "bc", /** * The BIP32 key prefixes for Bitcoin. */ bip32: { /** * The public key prefix for BIP32 extended public keys. */ public: 76067358, /** * The private key prefix for BIP32 extended private keys. */ private: 76066276 }, /** * The prefix for Bitcoin public key hashes. */ pubKeyHash: 0, /** * The prefix for Bitcoin script hashes. */ scriptHash: 5, /** * The prefix for Bitcoin Wallet Import Format (WIF) private keys. */ wif: 128 }; exports.regtest = { messagePrefix: "Bitcoin Signed Message:\n", bech32: "bcrt", bip32: { public: 70617039, private: 70615956 }, pubKeyHash: 111, scriptHash: 196, wif: 239 }; exports.testnet = { messagePrefix: "Bitcoin Signed Message:\n", bech32: "tb", bip32: { public: 70617039, private: 70615956 }, pubKeyHash: 111, scriptHash: 196, wif: 239 }; } }); // node_modules/bitcoinjs-lib/src/bip66.js var require_bip66 = __commonJS({ "node_modules/bitcoinjs-lib/src/bip66.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.encode = exports.decode = exports.check = void 0; function check(buffer) { if (buffer.length < 8) return false; if (buffer.length > 72) return false; if (buffer[0] !== 48) return false; if (buffer[1] !== buffer.length - 2) return false; if (buffer[2] !== 2) return false; const lenR = buffer[3]; if (lenR === 0) return false; if (5 + lenR >= buffer.length) return false; if (buffer[4 + lenR] !== 2) return false; const lenS = buffer[5 + lenR]; if (lenS === 0) return false; if (6 + lenR + lenS !== buffer.length) return false; if (buffer[4] & 128) return false; if (lenR > 1 && buffer[4] === 0 && !(buffer[5] & 128)) return false; if (buffer[lenR + 6] & 128) return false; if (lenS > 1 && buffer[lenR + 6] === 0 && !(buffer[lenR + 7] & 128)) return false; return true; } exports.check = check; function decode3(buffer) { if (buffer.length < 8) throw new Error("DER sequence length is too short"); if (buffer.length > 72) throw new Error("DER sequence length is too long"); if (buffer[0] !== 48) throw new Error("Expected DER sequence"); if (buffer[1] !== buffer.length - 2) throw new Error("DER sequence length is invalid"); if (buffer[2] !== 2) throw new Error("Expected DER integer"); const lenR = buffer[3]; if (lenR === 0) throw new Error("R length is zero"); if (5 + lenR >= buffer.length) throw new Error("R length is too long"); if (buffer[4 + lenR] !== 2) throw new Error("Expected DER integer (2)"); const lenS = buffer[5 + lenR]; if (lenS === 0) throw new Error("S length is zero"); if (6 + lenR + lenS !== buffer.length) throw new Error("S length is invalid"); if (buffer[4] & 128) throw new Error("R value is negative"); if (lenR > 1 && buffer[4] === 0 && !(buffer[5] & 128)) throw new Error("R value excessively padded"); if (buffer[lenR + 6] & 128) throw new Error("S value is negative"); if (lenS > 1 && buffer[lenR + 6] === 0 && !(buffer[lenR + 7] & 128)) throw new Error("S value excessively padded"); return { r: buffer.slice(4, 4 + lenR), s: buffer.slice(6 + lenR) }; } exports.decode = decode3; function encode3(r, s) { const lenR = r.length; const lenS = s.length; if (lenR === 0) throw new Error("R length is zero"); if (lenS === 0) throw new Error("S length is zero"); if (lenR > 33) throw new Error("R length is too long"); if (lenS > 33) throw new Error("S length is too long"); if (r[0] & 128) throw new Error("R value is negative"); if (s[0] & 128) throw new Error("S value is negative"); if (lenR > 1 && r[0] === 0 && !(r[1] & 128)) throw new Error("R value excessively padded"); if (lenS > 1 && s[0] === 0 && !(s[1] & 128)) throw new Error("S value excessively padded"); const signature = Buffer.allocUnsafe(6 + lenR + lenS); signature[0] = 48; signature[1] = signature.length - 2; signature[2] = 2; signature[3] = r.length; r.copy(signature, 4); signature[4 + lenR] = 2; signature[5 + lenR] = s.length; s.copy(signature, 6 + lenR); return signature; } exports.encode = encode3; } }); // node_modules/bitcoinjs-lib/src/ops.js var require_ops = __commonJS({ "node_modules/bitcoinjs-lib/src/ops.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.REVERSE_OPS = exports.OPS = void 0; var OPS = { OP_FALSE: 0, OP_0: 0, OP_PUSHDATA1: 76, OP_PUSHDATA2: 77, OP_PUSHDATA4: 78, OP_1NEGATE: 79, OP_RESERVED: 80, OP_TRUE: 81, OP_1: 81, OP_2: 82, OP_3: 83, OP_4: 84, OP_5: 85, OP_6: 86, OP_7: 87, OP_8: 88, OP_9: 89, OP_10: 90, OP_11: 91, OP_12: 92, OP_13: 93, OP_14: 94, OP_15: 95, OP_16: 96, OP_NOP: 97, OP_VER: 98, OP_IF: 99, OP_NOTIF: 100, OP_VERIF: 101, OP_VERNOTIF: 102, OP_ELSE: 103, OP_ENDIF: 104, OP_VERIFY: 105, OP_RETURN: 106, OP_TOALTSTACK: 107, OP_FROMALTSTACK: 108, OP_2DROP: 109, OP_2DUP: 110, OP_3DUP: 111, OP_2OVER: 112, OP_2ROT: 113, OP_2SWAP: 114, OP_IFDUP: 115, OP_DEPTH: 116, OP_DROP: 117, OP_DUP: 118, OP_NIP: 119, OP_OVER: 120, OP_PICK: 121, OP_ROLL: 122, OP_ROT: 123, OP_SWAP: 124, OP_TUCK: 125, OP_CAT: 126, OP_SUBSTR: 127, OP_LEFT: 128, OP_RIGHT: 129, OP_SIZE: 130, OP_INVERT: 131, OP_AND: 132, OP_OR: 133, OP_XOR: 134, OP_EQUAL: 135, OP_EQUALVERIFY: 136, OP_RESERVED1: 137, OP_RESERVED2: 138, OP_1ADD: 139, OP_1SUB: 140, OP_2MUL: 141, OP_2DIV: 142, OP_NEGATE: 143, OP_ABS: 144, OP_NOT: 145, OP_0NOTEQUAL: 146, OP_ADD: 147, OP_SUB: 148, OP_MUL: 149, OP_DIV: 150, OP_MOD: 151, OP_LSHIFT: 152, OP_RSHIFT: 153, OP_BOOLAND: 154, OP_BOOLOR: 155, OP_NUMEQUAL: 156, OP_NUMEQUALVERIFY: 157, OP_NUMNOTEQUAL: 158, OP_LESSTHAN: 159, OP_GREATERTHAN: 160, OP_LESSTHANOREQUAL: 161, OP_GREATERTHANOREQUAL: 162, OP_MIN: 163, OP_MAX: 164, OP_WITHIN: 165, OP_RIPEMD160: 166, OP_SHA1: 167, OP_SHA256: 168, OP_HASH160: 169, OP_HASH256: 170, OP_CODESEPARATOR: 171, OP_CHECKSIG: 172, OP_CHECKSIGVERIFY: 173, OP_CHECKMULTISIG: 174, OP_CHECKMULTISIGVERIFY: 175, OP_NOP1: 176, OP_NOP2: 177, OP_CHECKLOCKTIMEVERIFY: 177, OP_NOP3: 178, OP_CHECKSEQUENCEVERIFY: 178, OP_NOP4: 179, OP_NOP5: 180, OP_NOP6: 181, OP_NOP7: 182, OP_NOP8: 183, OP_NOP9: 184, OP_NOP10: 185, OP_CHECKSIGADD: 186, OP_PUBKEYHASH: 253, OP_PUBKEY: 254, OP_INVALIDOPCODE: 255 }; exports.OPS = OPS; var REVERSE_OPS = {}; exports.REVERSE_OPS = REVERSE_OPS; for (const op of Object.keys(OPS)) { const code = OPS[op]; REVERSE_OPS[code] = op; } } }); // node_modules/bitcoinjs-lib/src/push_data.js var require_push_data = __commonJS({ "node_modules/bitcoinjs-lib/src/push_data.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.decode = exports.encode = exports.encodingLength = void 0; var ops_1 = require_ops(); function encodingLength(i) { return i < ops_1.OPS.OP_PUSHDATA1 ? 1 : i <= 255 ? 2 : i <= 65535 ? 3 : 5; } exports.encodingLength = encodingLength; function encode3(buffer, num, offset) { const size = encodingLength(num); if (size === 1) { buffer.writeUInt8(num, offset); } else if (size === 2) { buffer.writeUInt8(ops_1.OPS.OP_PUSHDATA1, offset); buffer.writeUInt8(num, offset + 1); } else if (size === 3) { buffer.writeUInt8(ops_1.OPS.OP_PUSHDATA2, offset); buffer.writeUInt16LE(num, offset + 1); } else { buffer.writeUInt8(ops_1.OPS.OP_PUSHDATA4, offset); buffer.writeUInt32LE(num, offset + 1); } return size; } exports.encode = encode3; function decode3(buffer, offset) { const opcode = buffer.readUInt8(offset); let num; let size; if (opcode < ops_1.OPS.OP_PUSHDATA1) { num = opcode; size = 1; } else if (opcode === ops_1.OPS.OP_PUSHDATA1) { if (offset + 2 > buffer.length) return null; num = buffer.readUInt8(offset + 1); size = 2; } else if (opcode === ops_1.OPS.OP_PUSHDATA2) { if (offset + 3 > buffer.length) return null; num = buffer.readUInt16LE(offset + 1); size = 3; } else { if (offset + 5 > buffer.length) return null; if (opcode !== ops_1.OPS.OP_PUSHDATA4) throw new Error("Unexpected opcode"); num = buffer.readUInt32LE(offset + 1); size = 5; } return { opcode, number: num, size }; } exports.decode = decode3; } }); // node_modules/bitcoinjs-lib/src/script_number.js var require_script_number = __commonJS({ "node_modules/bitcoinjs-lib/src/script_number.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.encode = exports.decode = void 0; function decode3(buffer, maxLength, minimal) { maxLength = maxLength || 4; minimal = minimal === void 0 ? true : minimal; const length = buffer.length; if (length === 0) return 0; if (length > maxLength) throw new TypeError("Script number overflow"); if (minimal) { if ((buffer[length - 1] & 127) === 0) { if (length <= 1 || (buffer[length - 2] & 128) === 0) throw new Error("Non-minimally encoded script number"); } } if (length === 5) { const a = buffer.readUInt32LE(0); const b = buffer.readUInt8(4); if (b & 128) return -((b & -129) * 4294967296 + a); return b * 4294967296 + a; } let result = 0; for (let i = 0; i < length; ++i) { result |= buffer[i] << 8 * i; } if (buffer[length - 1] & 128) return -(result & ~(128 << 8 * (length - 1))); return result; } exports.decode = decode3; function scriptNumSize(i) { return i > 2147483647 ? 5 : i > 8388607 ? 4 : i > 32767 ? 3 : i > 127 ? 2 : i > 0 ? 1 : 0; } function encode3(_number) { let value = Math.abs(_number); const size = scriptNumSize(value); const buffer = Buffer.allocUnsafe(size); const negative = _number < 0; for (let i = 0; i < size; ++i) { buffer.writeUInt8(value & 255, i); value >>= 8; } if (buffer[size - 1] & 128) { buffer.writeUInt8(negative ? 128 : 0, size - 1); } else if (negative) { buffer[size - 1] |= 128; } return buffer; } exports.encode = encode3; } }); // node_modules/typeforce/native.js var require_native = __commonJS({ "node_modules/typeforce/native.js"(exports, module) { var types = { Array: function(value) { return value !== null && value !== void 0 && value.constructor === Array; }, Boolean: function(value) { return typeof value === "boolean"; }, Function: function(value) { return typeof value === "function"; }, Nil: function(value) { return value === void 0 || value === null; }, Number: function(value) { return typeof value === "number"; }, Object: function(value) { return typeof value === "object"; }, String: function(value) { return typeof value === "string"; }, "": function() { return true; } }; types.Null = types.Nil; for (typeName in types) { types[typeName].toJSON = function(t) { return t; }.bind(null, typeName); } var typeName; module.exports = types; } }); // node_modules/typeforce/errors.js var require_errors = __commonJS({ "node_modules/typeforce/errors.js"(exports, module) { var native = require_native(); function getTypeName(fn) { return fn.name || fn.toString().match(/function (.*?)\s*\(/)[1]; } function getValueTypeName(value) { return native.Nil(value) ? "" : getTypeName(value.constructor); } function getValue(value) { if (native.Function(value)) return ""; if (native.String(value)) return JSON.stringify(value); if (value && native.Object(value)) return ""; return value; } function captureStackTrace(e, t) { if (Error.captureStackTrace) { Error.captureStackTrace(e, t); } } function tfJSON(type) { if (native.Function(type)) return type.toJSON ? type.toJSON() : getTypeName(type); if (native.Array(type)) return "Array"; if (type && native.Object(type)) return "Object"; return type !== void 0 ? type : ""; } function tfErrorString(type, value, valueTypeName) { var valueJson = getValue(value); return "Expected " + tfJSON(type) + ", got" + (valueTypeName !== "" ? " " + valueTypeName : "") + (valueJson !== "" ? " " + valueJson : ""); } function TfTypeError(type, value, valueTypeName) { valueTypeName = valueTypeName || getValueTypeName(value); this.message = tfErrorString(type, value, valueTypeName); captureStackTrace(this, TfTypeError); this.__type = type; this.__value = value; this.__valueTypeName = valueTypeName; } TfTypeError.prototype = Object.create(Error.prototype); TfTypeError.prototype.constructor = TfTypeError; function tfPropertyErrorString(type, label, name, value, valueTypeName) { var description = '" of type '; if (label === "key") description = '" with key type '; return tfErrorString('property "' + tfJSON(name) + description + tfJSON(type), value, valueTypeName); } function TfPropertyTypeError(type, property, label, value, valueTypeName) { if (type) { valueTypeName = valueTypeName || getValueTypeName(value); this.message = tfPropertyErrorString(type, label, property, value, valueTypeName); } else { this.message = 'Unexpected property "' + property + '"'; } captureStackTrace(this, TfTypeError); this.__label = label; this.__property = property; this.__type = type; this.__value = value; this.__valueTypeName = valueTypeName; } TfPropertyTypeError.prototype = Object.create(Error.prototype); TfPropertyTypeError.prototype.constructor = TfTypeError; function tfCustomError(expected, actual) { return new TfTypeError(expected, {}, actual); } function tfSubError(e, property, label) { if (e instanceof TfPropertyTypeError) { property = property + "." + e.__property; e = new TfPropertyTypeError( e.__type, property, e.__label, e.__value, e.__valueTypeName ); } else if (e instanceof TfTypeError) { e = new TfPropertyTypeError( e.__type, property, label, e.__value, e.__valueTypeName ); } captureStackTrace(e); return e; } module.exports = { TfTypeError, TfPropertyTypeError, tfCustomError, tfSubError, tfJSON, getValueTypeName }; } }); // node_modules/typeforce/extra.js var require_extra = __commonJS({ "node_modules/typeforce/extra.js"(exports, module) { var NATIVE = require_native(); var ERRORS = require_errors(); function _Buffer(value) { return Buffer.isBuffer(value); } function Hex(value) { return typeof value === "string" && /^([0-9a-f]{2})+$/i.test(value); } function _LengthN(type, length) { var name = type.toJSON(); function Length(value) { if (!type(value)) return false; if (value.length === length) return true; throw ERRORS.tfCustomError(name + "(Length: " + length + ")", name + "(Length: " + value.length + ")"); } Length.toJSON = function() { return name; }; return Length; } var _ArrayN = _LengthN.bind(null, NATIVE.Array); var _BufferN = _LengthN.bind(null, _Buffer); var _HexN = _LengthN.bind(null, Hex); var _StringN = _LengthN.bind(null, NATIVE.String); function Range(a, b, f) { f = f || NATIVE.Number; function _range(value, strict) { return f(value, strict) && value > a && value < b; } _range.toJSON = function() { return `${f.toJSON()} between [${a}, ${b}]`; }; return _range; } var INT53_MAX = Math.pow(2, 53) - 1; function Finite(value) { return typeof value === "number" && isFinite(value); } function Int8(value) { return value << 24 >> 24 === value; } function Int16(value) { return value << 16 >> 16 === value; } function Int32(value) { return (value | 0) === value; } function Int53(value) { return typeof value === "number" && value >= -INT53_MAX && value <= INT53_MAX && Math.floor(value) === value; } function UInt8(value) { return (value & 255) === value; } function UInt16(value) { return (value & 65535) === value; } function UInt32(value) { return value >>> 0 === value; } function UInt53(value) { return typeof value === "number" && value >= 0 && value <= INT53_MAX && Math.floor(value) === value; } var types = { ArrayN: _ArrayN, Buffer: _Buffer, BufferN: _BufferN, Finite, Hex, HexN: _HexN, Int8, Int16, Int32, Int53, Range, StringN: _StringN, UInt8, UInt16, UInt32, UInt53 }; for (typeName in types) { types[typeName].toJSON = function(t) { return t; }.bind(null, typeName); } var typeName; module.exports = types; } }); // node_modules/typeforce/index.js var require_typeforce = __commonJS({ "node_modules/typeforce/index.js"(exports, module) { var ERRORS = require_errors(); var NATIVE = require_native(); var tfJSON = ERRORS.tfJSON; var TfTypeError = ERRORS.TfTypeError; var TfPropertyTypeError = ERRORS.TfPropertyTypeError; var tfSubError = ERRORS.tfSubError; var getValueTypeName = ERRORS.getValueTypeName; var TYPES = { arrayOf: function arrayOf(type, options) { type = compile(type); options = options || {}; function _arrayOf(array, strict) { if (!NATIVE.Array(array)) return false; if (NATIVE.Nil(array)) return false; if (options.minLength !== void 0 && array.length < options.minLength) return false; if (options.maxLength !== void 0 && array.length > options.maxLength) return false; if (options.length !== void 0 && array.length !== options.length) return false; return array.every(function(value, i) { try { return typeforce(type, value, strict); } catch (e) { throw tfSubError(e, i); } }); } _arrayOf.toJSON = function() { var str = "[" + tfJSON(type) + "]"; if (options.length !== void 0) { str += "{" + options.length + "}"; } else if (options.minLength !== void 0 || options.maxLength !== void 0) { str += "{" + (options.minLength === void 0 ? 0 : options.minLength) + "," + (options.maxLength === void 0 ? Infinity : options.maxLength) + "}"; } return str; }; return _arrayOf; }, maybe: function maybe(type) { type = compile(type); function _maybe(value, strict) { return NATIVE.Nil(value) || type(value, strict, maybe); } _maybe.toJSON = function() { return "?" + tfJSON(type); }; return _maybe; }, map: function map(propertyType, propertyKeyType) { propertyType = compile(propertyType); if (propertyKeyType) propertyKeyType = compile(propertyKeyType); function _map(value, strict) { if (!NATIVE.Object(value)) return false; if (NATIVE.Nil(value)) return false; for (var propertyName in value) { try { if (propertyKeyType) { typeforce(propertyKeyType, propertyName, strict); } } catch (e) { throw tfSubError(e, propertyName, "key"); } try { var propertyValue = value[propertyName]; typeforce(propertyType, propertyValue, strict); } catch (e) { throw tfSubError(e, propertyName); } } return true; } if (propertyKeyType) { _map.toJSON = function() { return "{" + tfJSON(propertyKeyType) + ": " + tfJSON(propertyType) + "}"; }; } else { _map.toJSON = function() { return "{" + tfJSON(propertyType) + "}"; }; } return _map; }, object: function object(uncompiled) { var type = {}; for (var typePropertyName in uncompiled) { type[typePropertyName] = compile(uncompiled[typePropertyName]); } function _object(value, strict) { if (!NATIVE.Object(value)) return false; if (NATIVE.Nil(value)) return false; var propertyName; try { for (propertyName in type) { var propertyType = type[propertyName]; var propertyValue = value[propertyName]; typeforce(propertyType, propertyValue, strict); } } catch (e) { throw tfSubError(e, propertyName); } if (strict) { for (propertyName in value) { if (type[propertyName]) continue; throw new TfPropertyTypeError(void 0, propertyName); } } return true; } _object.toJSON = function() { return tfJSON(type); }; return _object; }, anyOf: function anyOf() { var types = [].slice.call(arguments).map(compile); function _anyOf(value, strict) { return types.some(function(type) { try { return typeforce(type, value, strict); } catch (e) { return false; } }); } _anyOf.toJSON = function() { return types.map(tfJSON).join("|"); }; return _anyOf; }, allOf: function allOf() { var types = [].slice.call(arguments).map(compile); function _allOf(value, strict) { return types.every(function(type) { try { return typeforce(type, value, strict); } catch (e) { return false; } }); } _allOf.toJSON = function() { return types.map(tfJSON).join(" & "); }; return _allOf; }, quacksLike: function quacksLike(type) { function _quacksLike(value) { return type === getValueTypeName(value); } _quacksLike.toJSON = function() { return type; }; return _quacksLike; }, tuple: function tuple() { var types = [].slice.call(arguments).map(compile); function _tuple(values, strict) { if (NATIVE.Nil(values)) return false; if (NATIVE.Nil(values.length)) return false; if (strict && values.length !== types.length) return false; return types.every(function(type, i) { try { return typeforce(type, values[i], strict); } catch (e) { throw tfSubError(e, i); } }); } _tuple.toJSON = function() { return "(" + types.map(tfJSON).join(", ") + ")"; }; return _tuple; }, value: function value(expected) { function _value(actual) { return actual === expected; } _value.toJSON = function() { return expected; }; return _value; } }; TYPES.oneOf = TYPES.anyOf; function compile(type) { if (NATIVE.String(type)) { if (type[0] === "?") return TYPES.maybe(type.slice(1)); return NATIVE[type] || TYPES.quacksLike(type); } else if (type && NATIVE.Object(type)) { if (NATIVE.Array(type)) { if (type.length !== 1) throw new TypeError("Expected compile() parameter of type Array of length 1"); return TYPES.arrayOf(type[0]); } return TYPES.object(type); } else if (NATIVE.Function(type)) { return type; } return TYPES.value(type); } function typeforce(type, value, strict, surrogate) { if (NATIVE.Function(type)) { if (type(value, strict)) return true; throw new TfTypeError(surrogate || type, value); } return typeforce(compile(type), value, strict); } for (typeName in NATIVE) { typeforce[typeName] = NATIVE[typeName]; } var typeName; for (typeName in TYPES) { typeforce[typeName] = TYPES[typeName]; } var EXTRA = require_extra(); for (typeName in EXTRA) { typeforce[typeName] = EXTRA[typeName]; } typeforce.compile = compile; typeforce.TfTypeError = TfTypeError; typeforce.TfPropertyTypeError = TfPropertyTypeError; module.exports = typeforce; } }); // node_modules/bitcoinjs-lib/src/types.js var require_types = __commonJS({ "node_modules/bitcoinjs-lib/src/types.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.oneOf = exports.Null = exports.BufferN = exports.Function = exports.UInt32 = exports.UInt8 = exports.tuple = exports.maybe = exports.Hex = exports.Buffer = exports.String = exports.Boolean = exports.Array = exports.Number = exports.Hash256bit = exports.Hash160bit = exports.Buffer256bit = exports.isTaptree = exports.isTapleaf = exports.TAPLEAF_VERSION_MASK = exports.Satoshi = exports.isPoint = exports.stacksEqual = exports.typeforce = void 0; var buffer_1 = __require("buffer"); exports.typeforce = require_typeforce(); var ZERO32 = buffer_1.Buffer.alloc(32, 0); var EC_P = buffer_1.Buffer.from( "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "hex" ); function stacksEqual(a, b) { if (a.length !== b.length) return false; return a.every((x, i) => { return x.equals(b[i]); }); } exports.stacksEqual = stacksEqual; function isPoint(p) { if (!buffer_1.Buffer.isBuffer(p)) return false; if (p.length < 33) return false; const t = p[0]; const x = p.slice(1, 33); if (x.compare(ZERO32) === 0) return false; if (x.compare(EC_P) >= 0) return false; if ((t === 2 || t === 3) && p.length === 33) { return true; } const y = p.slice(33); if (y.compare(ZERO32) === 0) return false; if (y.compare(EC_P) >= 0) return false; if (t === 4 && p.length === 65) return true; return false; } exports.isPoint = isPoint; var SATOSHI_MAX = 21 * 1e14; function Satoshi(value) { return exports.typeforce.UInt53(value) && value <= SATOSHI_MAX; } exports.Satoshi = Satoshi; exports.TAPLEAF_VERSION_MASK = 254; function isTapleaf(o) { if (!o || !("output" in o)) return false; if (!buffer_1.Buffer.isBuffer(o.output)) return false; if (o.version !== void 0) return (o.version & exports.TAPLEAF_VERSION_MASK) === o.version; return true; } exports.isTapleaf = isTapleaf; function isTaptree(scriptTree) { if (!(0, exports.Array)(scriptTree)) return isTapleaf(scriptTree); if (scriptTree.length !== 2) return false; return scriptTree.every((t) => isTaptree(t)); } exports.isTaptree = isTaptree; exports.Buffer256bit = exports.typeforce.BufferN(32); exports.Hash160bit = exports.typeforce.BufferN(20); exports.Hash256bit = exports.typeforce.BufferN(32); exports.Number = exports.typeforce.Number; exports.Array = exports.typeforce.Array; exports.Boolean = exports.typeforce.Boolean; exports.String = exports.typeforce.String; exports.Buffer = exports.typeforce.Buffer; exports.Hex = exports.typeforce.Hex; exports.maybe = exports.typeforce.maybe; exports.tuple = exports.typeforce.tuple; exports.UInt8 = exports.typeforce.UInt8; exports.UInt32 = exports.typeforce.UInt32; exports.Function = exports.typeforce.Function; exports.BufferN = exports.typeforce.BufferN; exports.Null = exports.typeforce.Null; exports.oneOf = exports.typeforce.oneOf; } }); // node_modules/bitcoinjs-lib/src/script_signature.js var require_script_signature = __commonJS({ "node_modules/bitcoinjs-lib/src/script_signature.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.encode = exports.decode = void 0; var bip66 = require_bip66(); var script_1 = require_script(); var types = require_types(); var { typeforce } = types; var ZERO = Buffer.alloc(1, 0); function toDER(x) { let i = 0; while (x[i] === 0) ++i; if (i === x.length) return ZERO; x = x.slice(i); if (x[0] & 128) return Buffer.concat([ZERO, x], 1 + x.length); return x; } function fromDER(x) { if (x[0] === 0) x = x.slice(1); const buffer = Buffer.alloc(32, 0); const bstart = Math.max(0, 32 - x.length); x.copy(buffer, bstart); return buffer; } function decode3(buffer) { const hashType = buffer.readUInt8(buffer.length - 1); if (!(0, script_1.isDefinedHashType)(hashType)) { throw new Error("Invalid hashType " + hashType); } const decoded = bip66.decode(buffer.slice(0, -1)); const r = fromDER(decoded.r); const s = fromDER(decoded.s); const signature = Buffer.concat([r, s], 64); return { signature, hashType }; } exports.decode = decode3; function encode3(signature, hashType) { typeforce( { signature: types.BufferN(64), hashType: types.UInt8 }, { signature, hashType } ); if (!(0, script_1.isDefinedHashType)(hashType)) { throw new Error("Invalid hashType " + hashType); } const hashTypeBuffer = Buffer.allocUnsafe(1); hashTypeBuffer.writeUInt8(hashType, 0); const r = toDER(signature.slice(0, 32)); const s = toDER(signature.slice(32, 64)); return Buffer.concat([bip66.encode(r, s), hashTypeBuffer]); } exports.encode = encode3; } }); // node_modules/bitcoinjs-lib/src/script.js var require_script = __commonJS({ "node_modules/bitcoinjs-lib/src/script.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.signature = exports.number = exports.isCanonicalScriptSignature = exports.isDefinedHashType = exports.isCanonicalPubKey = exports.toStack = exports.fromASM = exports.toASM = exports.decompile = exports.compile = exports.countNonPushOnlyOPs = exports.isPushOnly = exports.OPS = void 0; var bip66 = require_bip66(); var ops_1 = require_ops(); Object.defineProperty(exports, "OPS", { enumerable: true, get: function() { return ops_1.OPS; } }); var pushdata = require_push_data(); var scriptNumber = require_script_number(); var scriptSignature = require_script_signature(); var types = require_types(); var { typeforce } = types; var OP_INT_BASE = ops_1.OPS.OP_RESERVED; function isOPInt(value) { return types.Number(value) && (value === ops_1.OPS.OP_0 || value >= ops_1.OPS.OP_1 && value <= ops_1.OPS.OP_16 || value === ops_1.OPS.OP_1NEGATE); } function isPushOnlyChunk(value) { return types.Buffer(value) || isOPInt(value); } function isPushOnly(value) { return types.Array(value) && value.every(isPushOnlyChunk); } exports.isPushOnly = isPushOnly; function countNonPushOnlyOPs(value) { return value.length - value.filter(isPushOnlyChunk).length; } exports.countNonPushOnlyOPs = countNonPushOnlyOPs; function asMinimalOP(buffer) { if (buffer.length === 0) return ops_1.OPS.OP_0; if (buffer.length !== 1) return; if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0]; if (buffer[0] === 129) return ops_1.OPS.OP_1NEGATE; } function chunksIsBuffer(buf) { return Buffer.isBuffer(buf); } function chunksIsArray(buf) { return types.Array(buf); } function singleChunkIsBuffer(buf) { return Buffer.isBuffer(buf); } function compile(chunks) { if (chunksIsBuffer(chunks)) return chunks; typeforce(types.Array, chunks); const bufferSize = chunks.reduce((accum, chunk) => { if (singleChunkIsBuffer(chunk)) { if (chunk.length === 1 && asMinimalOP(chunk) !== void 0) { return accum + 1; } return accum + pushdata.encodingLength(chunk.length) + chunk.length; } return accum + 1; }, 0); const buffer = Buffer.allocUnsafe(bufferSize); let offset = 0; chunks.forEach((chunk) => { if (singleChunkIsBuffer(chunk)) { const opcode = asMinimalOP(chunk); if (opcode !== void 0) { buffer.writeUInt8(opcode, offset); offset += 1; return; } offset += pushdata.encode(buffer, chunk.length, offset); chunk.copy(buffer, offset); offset += chunk.length; } else { buffer.writeUInt8(chunk, offset); offset += 1; } }); if (offset !== buffer.length) throw new Error("Could not decode chunks"); return buffer; } exports.compile = compile; function decompile(buffer) { if (chunksIsArray(buffer)) return buffer; typeforce(types.Buffer, buffer); const chunks = []; let i = 0; while (i < buffer.length) { const opcode = buffer[i]; if (opcode > ops_1.OPS.OP_0 && opcode <= ops_1.OPS.OP_PUSHDATA4) { const d = pushdata.decode(buffer, i); if (d === null) return null; i += d.size; if (i + d.number > buffer.length) return null; const data = buffer.slice(i, i + d.number); i += d.number; const op = asMinimalOP(data); if (op !== void 0) { chunks.push(op); } else { chunks.push(data); } } else { chunks.push(opcode); i += 1; } } return chunks; } exports.decompile = decompile; function toASM(chunks) { if (chunksIsBuffer(chunks)) { chunks = decompile(chunks); } if (!chunks) { throw new Error("Could not convert invalid chunks to ASM"); } return chunks.map((chunk) => { if (singleChunkIsBuffer(chunk)) { const op = asMinimalOP(chunk); if (op === void 0) return chunk.toString("hex"); chunk = op; } return ops_1.REVERSE_OPS[chunk]; }).join(" "); } exports.toASM = toASM; function fromASM(asm) { typeforce(types.String, asm); return compile( asm.split(" ").map((chunkStr) => { if (ops_1.OPS[chunkStr] !== void 0) return ops_1.OPS[chunkStr]; typeforce(types.Hex, chunkStr); return Buffer.from(chunkStr, "hex"); }) ); } exports.fromASM = fromASM; function toStack(chunks) { chunks = decompile(chunks); typeforce(isPushOnly, chunks); return chunks.map((op) => { if (singleChunkIsBuffer(op)) return op; if (op === ops_1.OPS.OP_0) return Buffer.allocUnsafe(0); return scriptNumber.encode(op - OP_INT_BASE); }); } exports.toStack = toStack; function isCanonicalPubKey(buffer) { return types.isPoint(buffer); } exports.isCanonicalPubKey = isCanonicalPubKey; function isDefinedHashType(hashType) { const hashTypeMod = hashType & -129; return hashTypeMod > 0 && hashTypeMod < 4; } exports.isDefinedHashType = isDefinedHashType; function isCanonicalScriptSignature(buffer) { if (!Buffer.isBuffer(buffer)) return false; if (!isDefinedHashType(buffer[buffer.length - 1])) return false; return bip66.check(buffer.slice(0, -1)); } exports.isCanonicalScriptSignature = isCanonicalScriptSignature; exports.number = scriptNumber; exports.signature = scriptSignature; } }); // node_modules/bitcoinjs-lib/src/payments/lazy.js var require_lazy = __commonJS({ "node_modules/bitcoinjs-lib/src/payments/lazy.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.value = exports.prop = void 0; function prop(object, name, f) { Object.defineProperty(object, name, { configurable: true, enumerable: true, get() { const _value = f.call(this); this[name] = _value; return _value; }, set(_value) { Object.defineProperty(this, name, { configurable: true, enumerable: true, value: _value, writable: true }); } }); } exports.prop = prop; function value(f) { let _value; return () => { if (_value !== void 0) return _value; _value = f(); return _value; }; } exports.value = value; } }); // node_modules/bitcoinjs-lib/src/payments/embed.js var require_embed = __commonJS({ "node_modules/bitcoinjs-lib/src/payments/embed.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.p2data = void 0; var networks_1 = require_networks(); var bscript = require_script(); var types_1 = require_types(); var lazy = require_lazy(); var OPS = bscript.OPS; function p2data(a, opts) { if (!a.data && !a.output) throw new TypeError("Not enough data"); opts = Object.assign({ validate: true }, opts || {}); (0, types_1.typeforce)( { network: types_1.typeforce.maybe(types_1.typeforce.Object), output: types_1.typeforce.maybe(types_1.typeforce.Buffer), data: types_1.typeforce.maybe( types_1.typeforce.arrayOf(types_1.typeforce.Buffer) ) }, a ); const network = a.network || networks_1.bitcoin; const o = { name: "embed", network }; lazy.prop(o, "output", () => { if (!a.data) return; return bscript.compile([OPS.OP_RETURN].concat(a.data)); }); lazy.prop(o, "data", () => { if (!a.output) return; return bscript.decompile(a.output).slice(1); }); if (opts.validate) { if (a.output) { const chunks = bscript.decompile(a.output); if (chunks[0] !== OPS.OP_RETURN) throw new TypeError("Output is invalid"); if (!chunks.slice(1).every(types_1.typeforce.Buffer)) throw new TypeError("Output is invalid"); if (a.data && !(0, types_1.stacksEqual)(a.data, o.data)) throw new TypeError("Data mismatch"); } } return Object.assign(o, a); } exports.p2data = p2data; } }); // node_modules/bitcoinjs-lib/src/payments/p2ms.js var require_p2ms = __commonJS({ "node_modules/bitcoinjs-lib/src/payments/p2ms.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.p2ms = void 0; var networks_1 = require_networks(); var bscript = require_script(); var types_1 = require_types(); var lazy = require_lazy(); var OPS = bscript.OPS; var OP_INT_BASE = OPS.OP_RESERVED; function p2ms(a, opts) { if (!a.input && !a.output && !(a.pubkeys && a.m !== void 0) && !a.signatures) throw new TypeError("Not enough data"); opts = Object.assign({ validate: true }, opts || {}); function isAcceptableSignature(x) { return bscript.isCanonicalScriptSignature(x) || (opts.allowIncomplete && x === OPS.OP_0) !== void 0; } (0, types_1.typeforce)( { network: types_1.typeforce.maybe(types_1.typeforce.Object), m: types_1.typeforce.maybe(types_1.typeforce.Number), n: types_1.typeforce.maybe(types_1.typeforce.Number), output: types_1.typeforce.maybe(types_1.typeforce.Buffer), pubkeys: types_1.typeforce.maybe( types_1.typeforce.arrayOf(types_1.isPoint) ), signatures: types_1.typeforce.maybe( types_1.typeforce.arrayOf(isAcceptableSignature) ), input: types_1.typeforce.maybe(types_1.typeforce.Buffer) }, a ); const network = a.network || networks_1.bitcoin; const o = { network }; let chunks = []; let decoded = false; function decode3(output) { if (decoded) return; decoded = true; chunks = bscript.decompile(output); o.m = chunks[0] - OP_INT_BASE; o.n = chunks[chunks.length - 2] - OP_INT_BASE; o.pubkeys = chunks.slice(1, -2); } lazy.prop(o, "output", () => { if (!a.m) return; if (!o.n) return; if (!a.pubkeys) return; return bscript.compile( [].concat( OP_INT_BASE + a.m, a.pubkeys, OP_INT_BASE + o.n, OPS.OP_CHECKMULTISIG ) ); }); lazy.prop(o, "m", () => { if (!o.output) return; decode3(o.output); return o.m; }); lazy.prop(o, "n", () => { if (!o.pubkeys) return; return o.pubkeys.length; }); lazy.prop(o, "pubkeys", () => { if (!a.output) return; decode3(a.output); return o.pubkeys; }); lazy.prop(o, "signatures", () => { if (!a.input) return; return bscript.decompile(a.input).slice(1); }); lazy.prop(o, "input", () => { if (!a.signatures) return; return bscript.compile([OPS.OP_0].concat(a.signatures)); }); lazy.prop(o, "witness", () => { if (!o.input) return; return []; }); lazy.prop(o, "name", () => { if (!o.m || !o.n) return; return `p2ms(${o.m} of ${o.n})`; }); if (opts.validate) { if (a.output) { decode3(a.output); if (!types_1.typeforce.Number(chunks[0])) throw new TypeError("Output is invalid"); if (!types_1.typeforce.Number(chunks[chunks.length - 2])) throw new TypeError("Output is invalid"); if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) throw new TypeError("Output is invalid"); if (o.m <= 0 || o.n > 16 || o.m > o.n || o.n !== chunks.length - 3) throw new TypeError("Output is invalid"); if (!o.pubkeys.every((x) => (0, types_1.isPoint)(x))) throw new TypeError("Output is invalid"); if (a.m !== void 0 && a.m !== o.m) throw new TypeError("m mismatch"); if (a.n !== void 0 && a.n !== o.n) throw new TypeError("n mismatch"); if (a.pubkeys && !(0, types_1.stacksEqual)(a.pubkeys, o.pubkeys)) throw new TypeError("Pubkeys mismatch"); } if (a.pubkeys) { if (a.n !== void 0 && a.n !== a.pubkeys.length) throw new TypeError("Pubkey count mismatch"); o.n = a.pubkeys.length; if (o.n < o.m) throw new TypeError("Pubkey count cannot be less than m"); } if (a.signatures) { if (a.signatures.length < o.m) throw new TypeError("Not enough signatures provided"); if (a.signatures.length > o.m) throw new TypeError("Too many signatures provided"); } if (a.input) { if (a.input[0] !== OPS.OP_0) throw new TypeError("Input is invalid"); if (o.signatures.length === 0 || !o.signatures.every(isAcceptableSignature)) throw new TypeError("Input has invalid signature(s)"); if (a.signatures && !(0, types_1.stacksEqual)(a.signatures, o.signatures)) throw new TypeError("Signature mismatch"); if (a.m !== void 0 && a.m !== a.signatures.length) throw new TypeError("Signature count mismatch"); } } return Object.assign(o, a); } exports.p2ms = p2ms; } }); // node_modules/bitcoinjs-lib/src/payments/p2pk.js var require_p2pk = __commonJS({ "node_modules/bitcoinjs-lib/src/payments/p2pk.js"(exports) { Object.defineProperty(exports, "__esModule", { value: true }); exports.p2pk = void 0; var networks_1 = require_networks(); var bscript = require_script(); var types_1 = require_types(); var lazy = require_lazy(); var OPS = bscript.OPS; function p2pk(a, opts) { if (!a.input && !a.output && !a.pubkey && !a.input && !a.signature) throw new TypeError("Not enough data"); opts = Object.assign({ validate: true }, opts || {}); (0, types_1.typeforce)( { network: types_1.typeforce.maybe(types_1.typeforce.Object), output: types_1.typeforce.maybe(types_1.typeforce.Buffer), pubkey: types_1.typeforce.maybe(types_1.isPoint), signature: types_1.typeforce.maybe(bscript.isCanonicalScriptSignature), input: types_1.typeforce.maybe(types_1.typeforce.Buffer) }, a ); const _chunks = lazy.value(() => { return bscript.decompile(a.input); }); const network = a.network || networks_1.bitcoin; const o = { name: "p2pk", network }; lazy.prop(o, "output", () => { if (!a.pubkey) return; return bscript.compile([a.pubkey, OPS.OP_CHECKSIG]); }); lazy.prop(o, "pubkey", () => { if (!a.output) return; return a.output.slice(1, -1); }); lazy.prop(o, "signature", () => { if (!a.input) return; return _chunks()[0]; }); lazy.prop(o, "input", () => { if (!a.signature) return; return bscript.compile([a.signature]); }