@vbyte/btc-dev 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/class/signer.js +1 -1
- package/dist/class/tx.js +5 -4
- package/dist/class/txin.js +3 -2
- package/dist/class/witness.js +1 -1
- package/dist/index.d.ts +8 -6
- package/dist/index.js +8 -6
- package/dist/lib/meta/index.d.ts +3 -1
- package/dist/lib/meta/index.js +3 -1
- package/dist/lib/{tx → meta}/locktime.d.ts +1 -1
- package/dist/lib/{tx → meta}/locktime.js +5 -5
- package/dist/lib/meta/{pointer.d.ts → ref.d.ts} +1 -1
- package/dist/lib/meta/{pointer.js → ref.js} +6 -6
- package/dist/lib/meta/scribe.d.ts +1 -1
- package/dist/lib/meta/scribe.js +5 -5
- package/dist/lib/{tx → meta}/sequence.d.ts +1 -1
- package/dist/lib/{tx → meta}/sequence.js +5 -5
- package/dist/lib/sighash/index.d.ts +0 -2
- package/dist/lib/sighash/index.js +0 -2
- package/dist/lib/sighash/segwit.js +2 -2
- package/dist/lib/sighash/taproot.js +2 -2
- package/dist/lib/signer/index.d.ts +2 -0
- package/dist/lib/signer/index.js +2 -0
- package/dist/lib/{sighash → signer}/sign.js +6 -6
- package/dist/lib/signer/verify.d.ts +4 -0
- package/dist/lib/signer/verify.js +8 -0
- package/dist/lib/taproot/parse.js +1 -1
- package/dist/lib/tx/create.d.ts +4 -4
- package/dist/lib/tx/create.js +1 -1
- package/dist/lib/tx/decode.d.ts +1 -1
- package/dist/lib/tx/decode.js +1 -1
- package/dist/lib/tx/encode.d.ts +1 -1
- package/dist/lib/tx/encode.js +3 -3
- package/dist/lib/tx/index.d.ts +1 -4
- package/dist/lib/tx/index.js +1 -4
- package/dist/lib/tx/parse.d.ts +1 -1
- package/dist/lib/tx/parse.js +5 -5
- package/dist/lib/tx/size.d.ts +1 -2
- package/dist/lib/tx/size.js +5 -12
- package/dist/lib/tx/{meta.js → util.js} +7 -7
- package/dist/lib/tx/validate.d.ts +2 -2
- package/dist/lib/witness/index.d.ts +2 -0
- package/dist/lib/witness/index.js +2 -0
- package/dist/lib/witness/util.d.ts +3 -0
- package/dist/lib/witness/util.js +8 -0
- package/dist/main.cjs +1002 -980
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +993 -973
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +9 -5
- package/dist/script.js +1 -1
- package/dist/script.js.map +1 -1
- package/dist/types/address.d.ts +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +0 -1
- package/dist/types/meta.d.ts +19 -0
- package/dist/types/transaction.d.ts +1 -1
- package/dist/types/txdata.d.ts +4 -4
- package/package.json +9 -5
- package/src/class/signer.ts +1 -1
- package/src/class/tx.ts +6 -6
- package/src/class/txin.ts +2 -2
- package/src/class/witness.ts +1 -1
- package/src/index.ts +8 -6
- package/src/lib/meta/index.ts +3 -1
- package/src/lib/{tx → meta}/locktime.ts +1 -1
- package/src/lib/meta/{pointer.ts → ref.ts} +1 -1
- package/src/lib/meta/scribe.ts +1 -1
- package/src/lib/{tx → meta}/sequence.ts +1 -1
- package/src/lib/sighash/index.ts +0 -2
- package/src/lib/sighash/segwit.ts +2 -2
- package/src/lib/sighash/taproot.ts +2 -2
- package/src/lib/signer/index.ts +2 -0
- package/src/lib/{sighash → signer}/sign.ts +6 -6
- package/src/lib/{sighash → signer}/verify.ts +22 -0
- package/src/lib/taproot/parse.ts +1 -1
- package/src/lib/tx/create.ts +5 -5
- package/src/lib/tx/decode.ts +7 -7
- package/src/lib/tx/encode.ts +3 -3
- package/src/lib/tx/index.ts +1 -4
- package/src/lib/tx/parse.ts +5 -5
- package/src/lib/tx/size.ts +7 -16
- package/src/lib/tx/{meta.ts → util.ts} +7 -7
- package/src/lib/tx/validate.ts +2 -2
- package/src/lib/witness/index.ts +2 -0
- package/src/lib/witness/util.ts +12 -0
- package/src/types/address.ts +1 -1
- package/src/types/index.ts +0 -1
- package/src/types/meta.ts +26 -0
- package/src/types/transaction.ts +2 -2
- package/src/types/txdata.ts +4 -4
- package/dist/lib/sighash/verify.d.ts +0 -1
- package/dist/lib/sighash/verify.js +0 -1
- package/dist/types/txmeta.d.ts +0 -19
- package/dist/types/txmeta.js +0 -1
- package/src/types/txmeta.ts +0 -25
- /package/dist/lib/{sighash → signer}/sign.d.ts +0 -0
- /package/dist/lib/tx/{meta.d.ts → util.d.ts} +0 -0
- /package/dist/lib/{tx/witness.d.ts → witness/parse.d.ts} +0 -0
- /package/dist/lib/{tx/witness.js → witness/parse.js} +0 -0
- /package/src/lib/{tx/witness.ts → witness/parse.ts} +0 -0
package/dist/main.cjs
CHANGED
|
@@ -8413,7 +8413,7 @@ const DEFAULT_CONFIG = {
|
|
|
8413
8413
|
prevouts: [],
|
|
8414
8414
|
segwit: true
|
|
8415
8415
|
};
|
|
8416
|
-
function
|
|
8416
|
+
function decode_tx(txbytes, options = {}) {
|
|
8417
8417
|
const config = { ...DEFAULT_CONFIG, ...options };
|
|
8418
8418
|
Assert.is_bytes(txbytes, 'txbytes must be hex or a unit array');
|
|
8419
8419
|
const stream = new Stream(txbytes);
|
|
@@ -8573,7 +8573,7 @@ var tx = /*#__PURE__*/Object.freeze({
|
|
|
8573
8573
|
vin_template: vin_template
|
|
8574
8574
|
});
|
|
8575
8575
|
|
|
8576
|
-
var index$
|
|
8576
|
+
var index$9 = /*#__PURE__*/Object.freeze({
|
|
8577
8577
|
__proto__: null,
|
|
8578
8578
|
taproot: taproot,
|
|
8579
8579
|
tx: tx
|
|
@@ -8642,7 +8642,7 @@ function create_tx_output(config) {
|
|
|
8642
8642
|
const { script_pk, value } = config;
|
|
8643
8643
|
return { script_pk, value: BigInt(value) };
|
|
8644
8644
|
}
|
|
8645
|
-
function
|
|
8645
|
+
function create_tx(config) {
|
|
8646
8646
|
assert_tx_template(config);
|
|
8647
8647
|
const { vin = [], vout = [] } = config;
|
|
8648
8648
|
const locktime = config.locktime ?? DEFAULT.LOCKTIME;
|
|
@@ -8652,13 +8652,13 @@ function create_tx_data(config) {
|
|
|
8652
8652
|
return { locktime, vin: inputs, vout: outputs, version };
|
|
8653
8653
|
}
|
|
8654
8654
|
|
|
8655
|
-
function
|
|
8655
|
+
function parse_tx(txdata) {
|
|
8656
8656
|
if (typeof txdata === 'string') {
|
|
8657
|
-
return
|
|
8657
|
+
return decode_tx(txdata);
|
|
8658
8658
|
}
|
|
8659
8659
|
else {
|
|
8660
8660
|
assert_tx_template(txdata);
|
|
8661
|
-
return
|
|
8661
|
+
return create_tx(txdata);
|
|
8662
8662
|
}
|
|
8663
8663
|
}
|
|
8664
8664
|
|
|
@@ -8995,7 +8995,7 @@ var ScriptUtil;
|
|
|
8995
8995
|
ScriptUtil.OPCODES = OPCODE_MAP;
|
|
8996
8996
|
})(ScriptUtil || (ScriptUtil = {}));
|
|
8997
8997
|
|
|
8998
|
-
var index$
|
|
8998
|
+
var index$8 = /*#__PURE__*/Object.freeze({
|
|
8999
8999
|
__proto__: null,
|
|
9000
9000
|
OPCODE_MAP: OPCODE_MAP,
|
|
9001
9001
|
get ScriptUtil () { return ScriptUtil; },
|
|
@@ -9014,8 +9014,8 @@ var index$6 = /*#__PURE__*/Object.freeze({
|
|
|
9014
9014
|
split_script_word: split_script_word
|
|
9015
9015
|
});
|
|
9016
9016
|
|
|
9017
|
-
function
|
|
9018
|
-
const tx =
|
|
9017
|
+
function encode_tx(txdata, segwit = true) {
|
|
9018
|
+
const tx = parse_tx(txdata);
|
|
9019
9019
|
const { version, vin, vout, locktime } = tx;
|
|
9020
9020
|
const buffer = [encode_tx_version(version)];
|
|
9021
9021
|
if (segwit) {
|
|
@@ -9105,43 +9105,6 @@ function encode_script_data(script) {
|
|
|
9105
9105
|
}
|
|
9106
9106
|
}
|
|
9107
9107
|
|
|
9108
|
-
const LOCKTIME_THRESHOLD = 500000000;
|
|
9109
|
-
var Locktime;
|
|
9110
|
-
(function (Locktime) {
|
|
9111
|
-
Locktime.encode = encode_locktime;
|
|
9112
|
-
Locktime.decode = decode_locktime;
|
|
9113
|
-
})(Locktime || (Locktime = {}));
|
|
9114
|
-
function encode_locktime(locktime) {
|
|
9115
|
-
switch (locktime.type) {
|
|
9116
|
-
case 'timelock':
|
|
9117
|
-
Assert.ok(locktime.stamp >= LOCKTIME_THRESHOLD, 'Invalid timestamp');
|
|
9118
|
-
return locktime.stamp;
|
|
9119
|
-
case 'heightlock':
|
|
9120
|
-
Assert.ok(locktime.height > 0, 'height must be greater than 0');
|
|
9121
|
-
Assert.ok(locktime.height < LOCKTIME_THRESHOLD, 'invalid block height');
|
|
9122
|
-
return locktime.height;
|
|
9123
|
-
default:
|
|
9124
|
-
throw new Error('Invalid locktime type');
|
|
9125
|
-
}
|
|
9126
|
-
}
|
|
9127
|
-
function decode_locktime(locktime) {
|
|
9128
|
-
if (isNaN(locktime) || locktime <= 0) {
|
|
9129
|
-
return null;
|
|
9130
|
-
}
|
|
9131
|
-
if (locktime < LOCKTIME_THRESHOLD) {
|
|
9132
|
-
return {
|
|
9133
|
-
type: 'heightlock',
|
|
9134
|
-
height: locktime
|
|
9135
|
-
};
|
|
9136
|
-
}
|
|
9137
|
-
else {
|
|
9138
|
-
return {
|
|
9139
|
-
type: 'timelock',
|
|
9140
|
-
stamp: locktime
|
|
9141
|
-
};
|
|
9142
|
-
}
|
|
9143
|
-
}
|
|
9144
|
-
|
|
9145
9108
|
function is_return_script(script) {
|
|
9146
9109
|
return script.startsWith('6a');
|
|
9147
9110
|
}
|
|
@@ -9170,100 +9133,33 @@ function get_vout_version(script) {
|
|
|
9170
9133
|
}
|
|
9171
9134
|
}
|
|
9172
9135
|
function get_txid(txdata) {
|
|
9173
|
-
const json =
|
|
9174
|
-
const data =
|
|
9136
|
+
const json = parse_tx(txdata);
|
|
9137
|
+
const data = encode_tx(json, false);
|
|
9175
9138
|
return hash256(data).reverse().hex;
|
|
9176
9139
|
}
|
|
9177
9140
|
function get_txhash(txdata) {
|
|
9178
|
-
const json =
|
|
9179
|
-
const data =
|
|
9141
|
+
const json = parse_tx(txdata);
|
|
9142
|
+
const data = encode_tx(json, true);
|
|
9180
9143
|
return hash256(data).reverse().hex;
|
|
9181
9144
|
}
|
|
9182
9145
|
function get_tx_value(txdata) {
|
|
9183
|
-
const tx =
|
|
9146
|
+
const tx = parse_tx(txdata);
|
|
9184
9147
|
const vin = tx.vin.reduce((acc, txin) => acc + (txin.prevout?.value ?? 0n), 0n);
|
|
9185
9148
|
const vout = tx.vout.reduce((acc, txout) => acc + txout.value, 0n);
|
|
9186
9149
|
const fees = (vin > vout) ? (vin - vout) : 0n;
|
|
9187
9150
|
return { fees, vin, vout };
|
|
9188
9151
|
}
|
|
9189
9152
|
|
|
9190
|
-
const TIMELOCK_DISABLE = 0x80000000;
|
|
9191
|
-
const TIMELOCK_TYPE = 0x00400000;
|
|
9192
|
-
const TIMELOCK_VALUE_MASK = 0x0000FFFF;
|
|
9193
|
-
const TIMELOCK_VALUE_MAX = 0xFFFF;
|
|
9194
|
-
const TIMELOCK_GRANULARITY = 512;
|
|
9195
|
-
var Sequence;
|
|
9196
|
-
(function (Sequence) {
|
|
9197
|
-
Sequence.encode = encode_sequence;
|
|
9198
|
-
Sequence.decode = decode_sequence;
|
|
9199
|
-
})(Sequence || (Sequence = {}));
|
|
9200
|
-
function encode_sequence(data) {
|
|
9201
|
-
if (data.mode === 'height') {
|
|
9202
|
-
const height = parse_height(data.height);
|
|
9203
|
-
return (height & TIMELOCK_VALUE_MASK) >>> 0;
|
|
9204
|
-
}
|
|
9205
|
-
if (data.mode === 'stamp') {
|
|
9206
|
-
const stamp = parse_stamp(data.stamp);
|
|
9207
|
-
return (TIMELOCK_TYPE | (stamp & TIMELOCK_VALUE_MASK)) >>> 0;
|
|
9208
|
-
}
|
|
9209
|
-
throw new Error('invalid timelock mode: ' + data.mode);
|
|
9210
|
-
}
|
|
9211
|
-
function decode_sequence(sequence) {
|
|
9212
|
-
const seq = parse_sequence(sequence);
|
|
9213
|
-
if (seq & TIMELOCK_DISABLE)
|
|
9214
|
-
return null;
|
|
9215
|
-
const value = seq & TIMELOCK_VALUE_MASK;
|
|
9216
|
-
if (seq & TIMELOCK_TYPE) {
|
|
9217
|
-
const stamp = value * TIMELOCK_GRANULARITY;
|
|
9218
|
-
if (stamp > 0xFFFFFFFF) {
|
|
9219
|
-
throw new Error('Decoded timestamp exceeds 32-bit limit');
|
|
9220
|
-
}
|
|
9221
|
-
return { mode: 'stamp', stamp };
|
|
9222
|
-
}
|
|
9223
|
-
else {
|
|
9224
|
-
if (value > TIMELOCK_VALUE_MAX) {
|
|
9225
|
-
throw new Error('Decoded height exceeds maximum');
|
|
9226
|
-
}
|
|
9227
|
-
return { mode: 'height', height: value };
|
|
9228
|
-
}
|
|
9229
|
-
}
|
|
9230
|
-
function parse_sequence(sequence) {
|
|
9231
|
-
const seq = (typeof sequence === 'string')
|
|
9232
|
-
? parseInt(sequence, 16)
|
|
9233
|
-
: sequence;
|
|
9234
|
-
if (!Number.isInteger(seq) || seq < 0 || seq > 0xFFFFFFFF) {
|
|
9235
|
-
throw new Error(`invalid sequence value: ${seq}`);
|
|
9236
|
-
}
|
|
9237
|
-
return seq;
|
|
9238
|
-
}
|
|
9239
|
-
function parse_stamp(stamp) {
|
|
9240
|
-
if (stamp === undefined || !Number.isInteger(stamp)) {
|
|
9241
|
-
throw new Error(`timestamp must be a number`);
|
|
9242
|
-
}
|
|
9243
|
-
const ts = Math.floor(stamp / TIMELOCK_GRANULARITY);
|
|
9244
|
-
if (!Number.isInteger(ts) || ts < 0 || ts > TIMELOCK_VALUE_MAX) {
|
|
9245
|
-
throw new Error(`timelock value must be an integer between 0 and ${TIMELOCK_VALUE_MAX} (in 512-second increments)`);
|
|
9246
|
-
}
|
|
9247
|
-
return ts;
|
|
9248
|
-
}
|
|
9249
|
-
function parse_height(height) {
|
|
9250
|
-
if (height === undefined || !Number.isInteger(height) || height < 0 || height > TIMELOCK_VALUE_MAX) {
|
|
9251
|
-
throw new Error(`Heightlock value must be an integer between 0 and ${TIMELOCK_VALUE_MAX}`);
|
|
9252
|
-
}
|
|
9253
|
-
return height;
|
|
9254
|
-
}
|
|
9255
|
-
|
|
9256
9153
|
const WIT_FLAG_BYTES = 2;
|
|
9257
|
-
const WIT_LENGTH_BYTE = 1;
|
|
9258
9154
|
function get_vsize$1(bytes) {
|
|
9259
9155
|
const weight = Buff.bytes(bytes).length;
|
|
9260
9156
|
const remain = (weight % 4 > 0) ? 1 : 0;
|
|
9261
9157
|
return Math.floor(weight / 4) + remain;
|
|
9262
9158
|
}
|
|
9263
9159
|
function get_txsize(txdata) {
|
|
9264
|
-
const json =
|
|
9265
|
-
const base =
|
|
9266
|
-
const size =
|
|
9160
|
+
const json = parse_tx(txdata);
|
|
9161
|
+
const base = encode_tx(json, false).length;
|
|
9162
|
+
const size = encode_tx(json, true).length;
|
|
9267
9163
|
const weight = base * 3 + size;
|
|
9268
9164
|
const remain = (weight % 4 > 0) ? 1 : 0;
|
|
9269
9165
|
const vsize = Math.floor(weight / 4) + remain;
|
|
@@ -9292,100 +9188,9 @@ function get_txout_size(txoutput) {
|
|
|
9292
9188
|
const bytes = encode_tx_vout(txoutput);
|
|
9293
9189
|
return bytes.length;
|
|
9294
9190
|
}
|
|
9295
|
-
function get_witness_size(witness) {
|
|
9296
|
-
const stack = witness.map(e => Buff.bytes(e));
|
|
9297
|
-
const size = stack.reduce((prev, next) => prev + next.length, 0);
|
|
9298
|
-
const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
|
|
9299
|
-
return { size, vsize };
|
|
9300
|
-
}
|
|
9301
|
-
|
|
9302
|
-
function parse_witness_data(witness) {
|
|
9303
|
-
const elems = witness.map(e => Buff.bytes(e));
|
|
9304
|
-
const annex = parse_annex_data(elems);
|
|
9305
|
-
if (annex !== null)
|
|
9306
|
-
elems.pop();
|
|
9307
|
-
const cblock = parse_cblock_data(elems);
|
|
9308
|
-
if (cblock !== null)
|
|
9309
|
-
elems.pop();
|
|
9310
|
-
const type = parse_witness_type(elems, cblock);
|
|
9311
|
-
const version = parse_witness_version(type);
|
|
9312
|
-
const script = parse_witness_script(elems, type);
|
|
9313
|
-
if (script !== null)
|
|
9314
|
-
elems.pop();
|
|
9315
|
-
const params = elems.map(e => e.hex);
|
|
9316
|
-
return { annex, cblock, params, script, type, version };
|
|
9317
|
-
}
|
|
9318
|
-
function parse_annex_data(data) {
|
|
9319
|
-
let elem = data.at(-1);
|
|
9320
|
-
if (data.length > 1 &&
|
|
9321
|
-
elem instanceof Uint8Array &&
|
|
9322
|
-
elem[0] === 0x50) {
|
|
9323
|
-
return new Buff(elem).hex;
|
|
9324
|
-
}
|
|
9325
|
-
else {
|
|
9326
|
-
return null;
|
|
9327
|
-
}
|
|
9328
|
-
}
|
|
9329
|
-
function parse_cblock_data(data) {
|
|
9330
|
-
let elem = data.at(-1);
|
|
9331
|
-
if (data.length > 1 &&
|
|
9332
|
-
elem instanceof Uint8Array &&
|
|
9333
|
-
elem.length > 32 &&
|
|
9334
|
-
TAPLEAF_VERSIONS.includes(elem[0] & 0xfe)) {
|
|
9335
|
-
return new Buff(elem).hex;
|
|
9336
|
-
}
|
|
9337
|
-
else {
|
|
9338
|
-
return null;
|
|
9339
|
-
}
|
|
9340
|
-
}
|
|
9341
|
-
function parse_witness_script(elems, type) {
|
|
9342
|
-
let script;
|
|
9343
|
-
switch (type) {
|
|
9344
|
-
case 'p2tr-ts':
|
|
9345
|
-
script = elems.at(-1);
|
|
9346
|
-
case 'p2w-sh':
|
|
9347
|
-
script = elems.at(-1);
|
|
9348
|
-
}
|
|
9349
|
-
return (script !== undefined) ? new Buff(script).hex : null;
|
|
9350
|
-
}
|
|
9351
|
-
function parse_witness_type(elems, cblock) {
|
|
9352
|
-
let param_0 = elems.at(0), param_1 = elems.at(1), param_x = elems.at(-1);
|
|
9353
|
-
if (cblock !== null && param_x !== undefined) {
|
|
9354
|
-
return 'p2tr-ts';
|
|
9355
|
-
}
|
|
9356
|
-
else if (elems.length === 2 &&
|
|
9357
|
-
param_0 !== undefined &&
|
|
9358
|
-
param_1 !== undefined &&
|
|
9359
|
-
param_0.length >= 64 &&
|
|
9360
|
-
param_1.length === 33) {
|
|
9361
|
-
return 'p2w-pkh';
|
|
9362
|
-
}
|
|
9363
|
-
else if (elems.length === 1 &&
|
|
9364
|
-
param_0 !== undefined &&
|
|
9365
|
-
param_0.length === 64) {
|
|
9366
|
-
return 'p2tr-pk';
|
|
9367
|
-
}
|
|
9368
|
-
else if (elems.length > 1 &&
|
|
9369
|
-
param_x !== undefined &&
|
|
9370
|
-
is_valid_script(param_x)) {
|
|
9371
|
-
return 'p2w-sh';
|
|
9372
|
-
}
|
|
9373
|
-
else {
|
|
9374
|
-
return 'unknown';
|
|
9375
|
-
}
|
|
9376
|
-
}
|
|
9377
|
-
function parse_witness_version(type) {
|
|
9378
|
-
if (type.startsWith('p2tr'))
|
|
9379
|
-
return 1;
|
|
9380
|
-
if (type.startsWith('p2w'))
|
|
9381
|
-
return 0;
|
|
9382
|
-
return null;
|
|
9383
|
-
}
|
|
9384
9191
|
|
|
9385
|
-
var index$
|
|
9192
|
+
var index$7 = /*#__PURE__*/Object.freeze({
|
|
9386
9193
|
__proto__: null,
|
|
9387
|
-
get Locktime () { return Locktime; },
|
|
9388
|
-
get Sequence () { return Sequence; },
|
|
9389
9194
|
assert_has_prevouts: assert_has_prevouts,
|
|
9390
9195
|
assert_tx_data: assert_tx_data,
|
|
9391
9196
|
assert_tx_input: assert_tx_input,
|
|
@@ -9394,16 +9199,12 @@ var index$5 = /*#__PURE__*/Object.freeze({
|
|
|
9394
9199
|
assert_vin_template: assert_vin_template,
|
|
9395
9200
|
create_coinbase_input: create_coinbase_input,
|
|
9396
9201
|
create_spend_input: create_spend_input,
|
|
9397
|
-
|
|
9202
|
+
create_tx: create_tx,
|
|
9398
9203
|
create_tx_input: create_tx_input,
|
|
9399
9204
|
create_tx_output: create_tx_output,
|
|
9400
|
-
|
|
9401
|
-
decode_sequence: decode_sequence,
|
|
9402
|
-
decode_tx_data: decode_tx_data,
|
|
9403
|
-
encode_locktime: encode_locktime,
|
|
9205
|
+
decode_tx: decode_tx,
|
|
9404
9206
|
encode_script_data: encode_script_data,
|
|
9405
|
-
|
|
9406
|
-
encode_tx_data: encode_tx_data,
|
|
9207
|
+
encode_tx: encode_tx,
|
|
9407
9208
|
encode_tx_inputs: encode_tx_inputs,
|
|
9408
9209
|
encode_tx_locktime: encode_tx_locktime,
|
|
9409
9210
|
encode_tx_outputs: encode_tx_outputs,
|
|
@@ -9428,16 +9229,14 @@ var index$5 = /*#__PURE__*/Object.freeze({
|
|
|
9428
9229
|
get_vout_type: get_vout_type,
|
|
9429
9230
|
get_vout_version: get_vout_version,
|
|
9430
9231
|
get_vsize: get_vsize$1,
|
|
9431
|
-
get_witness_size: get_witness_size,
|
|
9432
9232
|
is_return_script: is_return_script,
|
|
9433
|
-
|
|
9434
|
-
parse_witness_data: parse_witness_data,
|
|
9233
|
+
parse_tx: parse_tx,
|
|
9435
9234
|
read_script: read_script
|
|
9436
9235
|
});
|
|
9437
9236
|
|
|
9438
9237
|
function hash_segwit_tx(txdata, options = {}) {
|
|
9439
9238
|
const { sigflag = 0x01, txindex } = options;
|
|
9440
|
-
const tx =
|
|
9239
|
+
const tx = parse_tx(txdata);
|
|
9441
9240
|
const is_anypay = (sigflag & 0x80) === 0x80;
|
|
9442
9241
|
const flag = sigflag % 0x80;
|
|
9443
9242
|
if (!SIGHASH_SEGWIT.includes(flag)) {
|
|
@@ -9543,7 +9342,7 @@ function encode_taptweak(pubkey, data = new Uint8Array()) {
|
|
|
9543
9342
|
|
|
9544
9343
|
function hash_taproot_tx(template, config = {}) {
|
|
9545
9344
|
const { script, txindex, sigflag = 0x00, extflag = 0x00, key_version = 0x00, separator_pos = 0xFFFFFFFF } = config;
|
|
9546
|
-
const tx =
|
|
9345
|
+
const tx = parse_tx(template);
|
|
9547
9346
|
const { version, vin: input, vout: output, locktime } = tx;
|
|
9548
9347
|
const txinput = parse_txinput(tx, config);
|
|
9549
9348
|
const { txid, vout, sequence, witness = [] } = txinput;
|
|
@@ -9658,14 +9457,14 @@ function get_prevout(vin) {
|
|
|
9658
9457
|
}
|
|
9659
9458
|
|
|
9660
9459
|
function sign_segwit_tx(seckey, txdata, options) {
|
|
9661
|
-
const tx =
|
|
9460
|
+
const tx = parse_tx(txdata);
|
|
9662
9461
|
const msg = hash_segwit_tx(tx, options);
|
|
9663
9462
|
const sig = sign_ecdsa(seckey, msg).hex;
|
|
9664
9463
|
const flag = format_sigflag(options.sigflag ?? SIGHASH_DEFAULT);
|
|
9665
9464
|
return sig + flag;
|
|
9666
9465
|
}
|
|
9667
9466
|
function sign_taproot_tx(seckey, txdata, options) {
|
|
9668
|
-
const tx =
|
|
9467
|
+
const tx = parse_tx(txdata);
|
|
9669
9468
|
const msg = hash_taproot_tx(tx, options);
|
|
9670
9469
|
const sig = sign_bip340(seckey, msg).hex;
|
|
9671
9470
|
const flag = format_sigflag(options.sigflag ?? 0);
|
|
@@ -9702,840 +9501,1046 @@ class TxSigner {
|
|
|
9702
9501
|
}
|
|
9703
9502
|
}
|
|
9704
9503
|
|
|
9705
|
-
|
|
9706
|
-
|
|
9707
|
-
|
|
9708
|
-
|
|
9709
|
-
|
|
9504
|
+
const LOCKTIME_THRESHOLD = 500000000;
|
|
9505
|
+
var LocktimeUtil;
|
|
9506
|
+
(function (LocktimeUtil) {
|
|
9507
|
+
LocktimeUtil.encode = encode_locktime;
|
|
9508
|
+
LocktimeUtil.decode = decode_locktime;
|
|
9509
|
+
})(LocktimeUtil || (LocktimeUtil = {}));
|
|
9510
|
+
function encode_locktime(locktime) {
|
|
9511
|
+
switch (locktime.type) {
|
|
9512
|
+
case 'timelock':
|
|
9513
|
+
Assert.ok(locktime.stamp >= LOCKTIME_THRESHOLD, 'Invalid timestamp');
|
|
9514
|
+
return locktime.stamp;
|
|
9515
|
+
case 'heightlock':
|
|
9516
|
+
Assert.ok(locktime.height > 0, 'height must be greater than 0');
|
|
9517
|
+
Assert.ok(locktime.height < LOCKTIME_THRESHOLD, 'invalid block height');
|
|
9518
|
+
return locktime.height;
|
|
9519
|
+
default:
|
|
9520
|
+
throw new Error('Invalid locktime type');
|
|
9710
9521
|
}
|
|
9711
|
-
|
|
9522
|
+
}
|
|
9523
|
+
function decode_locktime(locktime) {
|
|
9524
|
+
if (isNaN(locktime) || locktime <= 0) {
|
|
9525
|
+
return null;
|
|
9526
|
+
}
|
|
9527
|
+
if (locktime < LOCKTIME_THRESHOLD) {
|
|
9712
9528
|
return {
|
|
9713
|
-
|
|
9714
|
-
|
|
9715
|
-
type: this.type,
|
|
9716
|
-
value: this.value,
|
|
9717
|
-
version: this.version
|
|
9529
|
+
type: 'heightlock',
|
|
9530
|
+
height: locktime
|
|
9718
9531
|
};
|
|
9719
9532
|
}
|
|
9720
|
-
|
|
9533
|
+
else {
|
|
9721
9534
|
return {
|
|
9722
|
-
|
|
9723
|
-
|
|
9535
|
+
type: 'timelock',
|
|
9536
|
+
stamp: locktime
|
|
9724
9537
|
};
|
|
9725
9538
|
}
|
|
9726
|
-
get size() {
|
|
9727
|
-
return this._size;
|
|
9728
|
-
}
|
|
9729
|
-
get type() {
|
|
9730
|
-
return this._info.type;
|
|
9731
|
-
}
|
|
9732
|
-
get value() {
|
|
9733
|
-
return this._txout.value;
|
|
9734
|
-
}
|
|
9735
|
-
get version() {
|
|
9736
|
-
return this._info.version;
|
|
9737
|
-
}
|
|
9738
|
-
toJSON() { return this.data; }
|
|
9739
|
-
toString() { return JSON.stringify(this.data); }
|
|
9740
9539
|
}
|
|
9741
9540
|
|
|
9742
|
-
|
|
9743
|
-
|
|
9744
|
-
|
|
9745
|
-
|
|
9746
|
-
|
|
9747
|
-
|
|
9748
|
-
|
|
9749
|
-
|
|
9750
|
-
|
|
9751
|
-
|
|
9752
|
-
|
|
9753
|
-
|
|
9754
|
-
|
|
9755
|
-
|
|
9756
|
-
|
|
9757
|
-
|
|
9758
|
-
|
|
9759
|
-
|
|
9760
|
-
|
|
9761
|
-
|
|
9762
|
-
|
|
9763
|
-
|
|
9764
|
-
|
|
9765
|
-
|
|
9766
|
-
|
|
9767
|
-
|
|
9768
|
-
|
|
9769
|
-
}
|
|
9770
|
-
|
|
9771
|
-
|
|
9772
|
-
|
|
9773
|
-
|
|
9774
|
-
|
|
9775
|
-
|
|
9776
|
-
};
|
|
9777
|
-
}
|
|
9778
|
-
get size() {
|
|
9779
|
-
return this._size.size;
|
|
9780
|
-
}
|
|
9781
|
-
get stack() {
|
|
9782
|
-
return this._data.map(e => e.hex);
|
|
9783
|
-
}
|
|
9784
|
-
get type() {
|
|
9785
|
-
return this._meta.type;
|
|
9541
|
+
var RefEncoder;
|
|
9542
|
+
(function (RefEncoder) {
|
|
9543
|
+
RefEncoder.outpoint = {
|
|
9544
|
+
encode: encode_outpoint,
|
|
9545
|
+
decode: decode_outpoint,
|
|
9546
|
+
verify: verify_outpoint,
|
|
9547
|
+
assert: assert_outpoint,
|
|
9548
|
+
};
|
|
9549
|
+
RefEncoder.record_id = {
|
|
9550
|
+
encode: encode_inscription_id,
|
|
9551
|
+
decode: decode_inscription_id,
|
|
9552
|
+
verify: verify_inscription_id,
|
|
9553
|
+
assert: assert_inscription_id,
|
|
9554
|
+
};
|
|
9555
|
+
RefEncoder.rune_id = {
|
|
9556
|
+
encode: encode_rune_id,
|
|
9557
|
+
decode: decode_rune_id,
|
|
9558
|
+
verify: verify_rune_id,
|
|
9559
|
+
assert: assert_rune_id,
|
|
9560
|
+
};
|
|
9561
|
+
})(RefEncoder || (RefEncoder = {}));
|
|
9562
|
+
function encode_inscription_id(txid, order = 0) {
|
|
9563
|
+
return `${txid}i${order}`;
|
|
9564
|
+
}
|
|
9565
|
+
function decode_inscription_id(inscription_id) {
|
|
9566
|
+
assert_inscription_id(inscription_id);
|
|
9567
|
+
const [txid, order] = inscription_id.split('i');
|
|
9568
|
+
return { txid, order: parseInt(order) };
|
|
9569
|
+
}
|
|
9570
|
+
function verify_inscription_id(inscription_id) {
|
|
9571
|
+
return inscription_id.match(/^[a-f0-9]{64}i\d+$/) !== null;
|
|
9572
|
+
}
|
|
9573
|
+
function assert_inscription_id(inscription_id) {
|
|
9574
|
+
if (!verify_inscription_id(inscription_id)) {
|
|
9575
|
+
throw new Error(`invalid inscription id: ${inscription_id}`);
|
|
9786
9576
|
}
|
|
9787
|
-
|
|
9788
|
-
|
|
9577
|
+
}
|
|
9578
|
+
function encode_rune_id(block_height, block_index) {
|
|
9579
|
+
return `${block_height}:${block_index}`;
|
|
9580
|
+
}
|
|
9581
|
+
function decode_rune_id(rune_id) {
|
|
9582
|
+
assert_rune_id(rune_id);
|
|
9583
|
+
const [block_height, block_index] = rune_id.split(':');
|
|
9584
|
+
return { block_height: parseInt(block_height), block_index: parseInt(block_index) };
|
|
9585
|
+
}
|
|
9586
|
+
function verify_rune_id(rune_id) {
|
|
9587
|
+
return rune_id.match(/^\d+:\d+$/) !== null;
|
|
9588
|
+
}
|
|
9589
|
+
function assert_rune_id(rune_id) {
|
|
9590
|
+
if (!verify_rune_id(rune_id)) {
|
|
9591
|
+
throw new Error(`invalid rune id: ${rune_id}`);
|
|
9789
9592
|
}
|
|
9790
|
-
|
|
9791
|
-
|
|
9593
|
+
}
|
|
9594
|
+
function encode_outpoint(txid, vout) {
|
|
9595
|
+
return `${txid}:${vout}`;
|
|
9596
|
+
}
|
|
9597
|
+
function decode_outpoint(outpoint) {
|
|
9598
|
+
assert_outpoint(outpoint);
|
|
9599
|
+
const [txid, vout] = outpoint.split(':');
|
|
9600
|
+
return { txid, vout: parseInt(vout) };
|
|
9601
|
+
}
|
|
9602
|
+
function verify_outpoint(outpoint) {
|
|
9603
|
+
return outpoint.match(/^[a-f0-9]{64}:[0-9]+$/) !== null;
|
|
9604
|
+
}
|
|
9605
|
+
function assert_outpoint(outpoint) {
|
|
9606
|
+
if (!verify_outpoint(outpoint)) {
|
|
9607
|
+
throw new Error(`invalid outpoint: ${outpoint}`);
|
|
9792
9608
|
}
|
|
9793
|
-
toJSON() { return this.data; }
|
|
9794
|
-
toString() { return JSON.stringify(this.data); }
|
|
9795
9609
|
}
|
|
9796
9610
|
|
|
9797
|
-
|
|
9798
|
-
|
|
9799
|
-
|
|
9800
|
-
|
|
9801
|
-
|
|
9802
|
-
|
|
9803
|
-
|
|
9804
|
-
|
|
9805
|
-
|
|
9806
|
-
|
|
9807
|
-
|
|
9808
|
-
|
|
9809
|
-
|
|
9810
|
-
|
|
9811
|
-
|
|
9812
|
-
|
|
9813
|
-
|
|
9814
|
-
|
|
9815
|
-
|
|
9816
|
-
|
|
9817
|
-
witness: this.witness?.data ?? null
|
|
9818
|
-
};
|
|
9819
|
-
}
|
|
9820
|
-
get has_prevout() {
|
|
9821
|
-
return this._txin.prevout !== null;
|
|
9611
|
+
const _0n = BigInt(0);
|
|
9612
|
+
const _1n = BigInt(1);
|
|
9613
|
+
const _26n = BigInt(26);
|
|
9614
|
+
var ScribeEncoder;
|
|
9615
|
+
(function (ScribeEncoder) {
|
|
9616
|
+
ScribeEncoder.encode = encode_inscription;
|
|
9617
|
+
ScribeEncoder.decode = decode_inscription;
|
|
9618
|
+
})(ScribeEncoder || (ScribeEncoder = {}));
|
|
9619
|
+
function decode_inscription(script) {
|
|
9620
|
+
const envelopes = parse_envelopes(script);
|
|
9621
|
+
return envelopes.map(parse_record);
|
|
9622
|
+
}
|
|
9623
|
+
function encode_inscription(data) {
|
|
9624
|
+
return data.map(create_envelope).join('');
|
|
9625
|
+
}
|
|
9626
|
+
function create_envelope(data) {
|
|
9627
|
+
let asm = ['OP_0', 'OP_IF', '6f7264'];
|
|
9628
|
+
if (typeof data.delegate === 'string') {
|
|
9629
|
+
const id = encode_id(data.delegate);
|
|
9630
|
+
asm.push('OP_11', id);
|
|
9822
9631
|
}
|
|
9823
|
-
|
|
9824
|
-
|
|
9632
|
+
if (typeof data.ref === 'string') {
|
|
9633
|
+
asm.push('OP_WITHIN', data.ref);
|
|
9825
9634
|
}
|
|
9826
|
-
|
|
9827
|
-
|
|
9828
|
-
|
|
9829
|
-
: null;
|
|
9635
|
+
if (typeof data.parent === 'string') {
|
|
9636
|
+
const id = encode_id(data.parent);
|
|
9637
|
+
asm.push('OP_3', id);
|
|
9830
9638
|
}
|
|
9831
|
-
|
|
9832
|
-
|
|
9833
|
-
|
|
9834
|
-
return {
|
|
9835
|
-
asm: decode_script(this._txin.script_sig),
|
|
9836
|
-
hex: this._txin.script_sig
|
|
9837
|
-
};
|
|
9639
|
+
if (typeof data.opcode === 'number') {
|
|
9640
|
+
const code = encode_pointer(data.opcode);
|
|
9641
|
+
asm.push('OP_NOP', code);
|
|
9838
9642
|
}
|
|
9839
|
-
|
|
9840
|
-
|
|
9841
|
-
|
|
9842
|
-
data: Sequence.decode(this._txin.sequence),
|
|
9843
|
-
value: this._txin.sequence
|
|
9844
|
-
};
|
|
9643
|
+
if (typeof data.pointer === 'number') {
|
|
9644
|
+
const ptr = encode_pointer(data.pointer);
|
|
9645
|
+
asm.push('OP_2', ptr);
|
|
9845
9646
|
}
|
|
9846
|
-
|
|
9847
|
-
|
|
9647
|
+
if (typeof data.rune === 'string') {
|
|
9648
|
+
const label = encode_rune_label(data.rune);
|
|
9649
|
+
asm.push('OP_13', label);
|
|
9848
9650
|
}
|
|
9849
|
-
|
|
9850
|
-
|
|
9651
|
+
if (typeof data.mimetype === 'string') {
|
|
9652
|
+
const label = encode_label(data.mimetype);
|
|
9653
|
+
asm.push('OP_1', label);
|
|
9851
9654
|
}
|
|
9852
|
-
|
|
9853
|
-
|
|
9655
|
+
if (typeof data.content === 'string') {
|
|
9656
|
+
const chunks = encode_content(data.content);
|
|
9657
|
+
asm.push('OP_0', ...chunks);
|
|
9854
9658
|
}
|
|
9855
|
-
|
|
9856
|
-
|
|
9659
|
+
asm.push('OP_ENDIF');
|
|
9660
|
+
return encode_script(asm);
|
|
9661
|
+
}
|
|
9662
|
+
function parse_envelopes(script) {
|
|
9663
|
+
const words = decode_script(script);
|
|
9664
|
+
const start_idx = words.findIndex(e => e === 'OP_0');
|
|
9665
|
+
Assert.ok(start_idx !== -1, 'inscription envelope not found');
|
|
9666
|
+
const envelopes = [];
|
|
9667
|
+
for (let idx = start_idx; idx < words.length; idx++) {
|
|
9668
|
+
Assert.ok(words[idx + 1] === 'OP_IF', 'OP_IF missing from envelope');
|
|
9669
|
+
Assert.ok(words[idx + 2] === '6f7264', 'magic bytes missing from envelope');
|
|
9670
|
+
const stop_idx = words.findIndex(e => e === 'OP_ENDIF');
|
|
9671
|
+
Assert.ok(stop_idx !== -1, 'inscription envelope missing END_IF statement');
|
|
9672
|
+
const env = words.slice(idx + 3, stop_idx);
|
|
9673
|
+
envelopes.push(env);
|
|
9674
|
+
idx += stop_idx;
|
|
9857
9675
|
}
|
|
9858
|
-
|
|
9859
|
-
toString() { return JSON.stringify(this.data); }
|
|
9676
|
+
return envelopes;
|
|
9860
9677
|
}
|
|
9861
|
-
|
|
9862
|
-
|
|
9863
|
-
|
|
9864
|
-
|
|
9865
|
-
|
|
9866
|
-
|
|
9867
|
-
|
|
9868
|
-
|
|
9869
|
-
|
|
9870
|
-
|
|
9871
|
-
|
|
9678
|
+
function parse_record(envelope) {
|
|
9679
|
+
const record = {};
|
|
9680
|
+
for (let i = 0; i < envelope.length; i++) {
|
|
9681
|
+
switch (envelope[i]) {
|
|
9682
|
+
case 'OP_1':
|
|
9683
|
+
record.mimetype = decode_label(envelope[i + 1]);
|
|
9684
|
+
i += 1;
|
|
9685
|
+
break;
|
|
9686
|
+
case 'OP_2':
|
|
9687
|
+
record.pointer = decode_pointer(envelope[i + 1]);
|
|
9688
|
+
i += 1;
|
|
9689
|
+
break;
|
|
9690
|
+
case 'OP_3':
|
|
9691
|
+
record.parent = decode_id(envelope[i + 1]);
|
|
9692
|
+
i += 1;
|
|
9693
|
+
break;
|
|
9694
|
+
case 'OP_11':
|
|
9695
|
+
record.delegate = decode_id(envelope[i + 1]);
|
|
9696
|
+
i += 1;
|
|
9697
|
+
break;
|
|
9698
|
+
case 'OP_13':
|
|
9699
|
+
record.rune = decode_rune_label(envelope[i + 1]);
|
|
9700
|
+
i += 1;
|
|
9701
|
+
break;
|
|
9702
|
+
case 'OP_WITHIN':
|
|
9703
|
+
record.ref = envelope[i + 1];
|
|
9704
|
+
i += 1;
|
|
9705
|
+
break;
|
|
9706
|
+
case 'OP_NOP':
|
|
9707
|
+
record.opcode = decode_pointer(envelope[i + 1]);
|
|
9708
|
+
i += 1;
|
|
9709
|
+
break;
|
|
9710
|
+
case 'OP_0':
|
|
9711
|
+
record.content = decode_content(envelope.slice(i + 1));
|
|
9712
|
+
return record;
|
|
9713
|
+
}
|
|
9872
9714
|
}
|
|
9873
|
-
|
|
9874
|
-
|
|
9875
|
-
|
|
9876
|
-
|
|
9877
|
-
|
|
9878
|
-
|
|
9879
|
-
|
|
9880
|
-
|
|
9881
|
-
|
|
9882
|
-
|
|
9883
|
-
|
|
9884
|
-
|
|
9885
|
-
|
|
9715
|
+
return record;
|
|
9716
|
+
}
|
|
9717
|
+
function encode_id(identifier) {
|
|
9718
|
+
Assert.ok(identifier.includes('i'), 'identifier must include an index');
|
|
9719
|
+
const parts = identifier.split('i');
|
|
9720
|
+
const bytes = Buff.hex(parts[0]);
|
|
9721
|
+
const idx = Number(parts[1]);
|
|
9722
|
+
const txid = bytes.reverse().hex;
|
|
9723
|
+
return (idx !== 0) ? txid + Buff.num(idx).hex : txid;
|
|
9724
|
+
}
|
|
9725
|
+
function decode_id(hexstr) {
|
|
9726
|
+
const bytes = Buff.hex(hexstr);
|
|
9727
|
+
const idx = bytes.at(-1) ?? 0;
|
|
9728
|
+
const txid = bytes.slice(0, -1).reverse().hex;
|
|
9729
|
+
return txid + 'i' + String(idx);
|
|
9730
|
+
}
|
|
9731
|
+
function encode_pointer(pointer) {
|
|
9732
|
+
return Buff.num(pointer).reverse().hex;
|
|
9733
|
+
}
|
|
9734
|
+
function decode_pointer(hexstr) {
|
|
9735
|
+
return Buff.hex(hexstr).reverse().num;
|
|
9736
|
+
}
|
|
9737
|
+
function encode_label(label) {
|
|
9738
|
+
return Buff.str(label).hex;
|
|
9739
|
+
}
|
|
9740
|
+
function decode_label(hexstr) {
|
|
9741
|
+
return Buff.hex(hexstr).str;
|
|
9742
|
+
}
|
|
9743
|
+
function encode_content(content) {
|
|
9744
|
+
const bytes = Buff.is_hex(content)
|
|
9745
|
+
? Buff.hex(content)
|
|
9746
|
+
: Buff.str(content);
|
|
9747
|
+
const stream = new Stream(bytes);
|
|
9748
|
+
const chunks = [];
|
|
9749
|
+
while (stream.size > 0) {
|
|
9750
|
+
if (stream.size > 520) {
|
|
9751
|
+
const chunk = stream.read(520);
|
|
9752
|
+
chunks.push(chunk.hex);
|
|
9753
|
+
}
|
|
9754
|
+
else {
|
|
9755
|
+
const chunk = stream.read(stream.size);
|
|
9756
|
+
chunks.push(chunk.hex);
|
|
9757
|
+
}
|
|
9886
9758
|
}
|
|
9887
|
-
|
|
9888
|
-
|
|
9759
|
+
return chunks;
|
|
9760
|
+
}
|
|
9761
|
+
function decode_content(hexstrs, type = 'hex') {
|
|
9762
|
+
const data = Buff.join(hexstrs);
|
|
9763
|
+
return (type === 'hex')
|
|
9764
|
+
? data.hex
|
|
9765
|
+
: data.str;
|
|
9766
|
+
}
|
|
9767
|
+
function encode_rune_label(label) {
|
|
9768
|
+
const str = label.toUpperCase();
|
|
9769
|
+
let big = _0n;
|
|
9770
|
+
for (const char of str) {
|
|
9771
|
+
if (char >= 'A' && char <= 'Z') {
|
|
9772
|
+
big = big * _26n + BigInt(char.charCodeAt(0) - ('A'.charCodeAt(0) - 1));
|
|
9773
|
+
}
|
|
9774
|
+
else {
|
|
9775
|
+
continue;
|
|
9776
|
+
}
|
|
9889
9777
|
}
|
|
9890
|
-
|
|
9778
|
+
big = big - _1n;
|
|
9779
|
+
return Buff.big(big).reverse().hex;
|
|
9780
|
+
}
|
|
9781
|
+
function decode_rune_label(hex) {
|
|
9782
|
+
let big = Buff.hex(hex).reverse().big;
|
|
9783
|
+
big = big + _1n;
|
|
9784
|
+
let result = '';
|
|
9785
|
+
while (big > _0n) {
|
|
9786
|
+
const mod = big % _26n;
|
|
9787
|
+
if (mod === _0n) {
|
|
9788
|
+
result = 'Z' + result;
|
|
9789
|
+
big = big / _26n - _1n;
|
|
9790
|
+
}
|
|
9791
|
+
else {
|
|
9792
|
+
const charCode = Number(mod) + 'A'.charCodeAt(0) - 1;
|
|
9793
|
+
result = String.fromCharCode(charCode) + result;
|
|
9794
|
+
big = big / _26n;
|
|
9795
|
+
}
|
|
9796
|
+
}
|
|
9797
|
+
return result;
|
|
9798
|
+
}
|
|
9799
|
+
|
|
9800
|
+
const TIMELOCK_DISABLE = 0x80000000;
|
|
9801
|
+
const TIMELOCK_TYPE = 0x00400000;
|
|
9802
|
+
const TIMELOCK_VALUE_MASK = 0x0000FFFF;
|
|
9803
|
+
const TIMELOCK_VALUE_MAX = 0xFFFF;
|
|
9804
|
+
const TIMELOCK_GRANULARITY = 512;
|
|
9805
|
+
var SequenceUtil;
|
|
9806
|
+
(function (SequenceUtil) {
|
|
9807
|
+
SequenceUtil.encode = encode_sequence;
|
|
9808
|
+
SequenceUtil.decode = decode_sequence;
|
|
9809
|
+
})(SequenceUtil || (SequenceUtil = {}));
|
|
9810
|
+
function encode_sequence(data) {
|
|
9811
|
+
if (data.mode === 'height') {
|
|
9812
|
+
const height = parse_height(data.height);
|
|
9813
|
+
return (height & TIMELOCK_VALUE_MASK) >>> 0;
|
|
9814
|
+
}
|
|
9815
|
+
if (data.mode === 'stamp') {
|
|
9816
|
+
const stamp = parse_stamp(data.stamp);
|
|
9817
|
+
return (TIMELOCK_TYPE | (stamp & TIMELOCK_VALUE_MASK)) >>> 0;
|
|
9818
|
+
}
|
|
9819
|
+
throw new Error('invalid timelock mode: ' + data.mode);
|
|
9820
|
+
}
|
|
9821
|
+
function decode_sequence(sequence) {
|
|
9822
|
+
const seq = parse_sequence(sequence);
|
|
9823
|
+
if (seq & TIMELOCK_DISABLE)
|
|
9824
|
+
return null;
|
|
9825
|
+
const value = seq & TIMELOCK_VALUE_MASK;
|
|
9826
|
+
if (seq & TIMELOCK_TYPE) {
|
|
9827
|
+
const stamp = value * TIMELOCK_GRANULARITY;
|
|
9828
|
+
if (stamp > 0xFFFFFFFF) {
|
|
9829
|
+
throw new Error('Decoded timestamp exceeds 32-bit limit');
|
|
9830
|
+
}
|
|
9831
|
+
return { mode: 'stamp', stamp };
|
|
9832
|
+
}
|
|
9833
|
+
else {
|
|
9834
|
+
if (value > TIMELOCK_VALUE_MAX) {
|
|
9835
|
+
throw new Error('Decoded height exceeds maximum');
|
|
9836
|
+
}
|
|
9837
|
+
return { mode: 'height', height: value };
|
|
9838
|
+
}
|
|
9839
|
+
}
|
|
9840
|
+
function parse_sequence(sequence) {
|
|
9841
|
+
const seq = (typeof sequence === 'string')
|
|
9842
|
+
? parseInt(sequence, 16)
|
|
9843
|
+
: sequence;
|
|
9844
|
+
if (!Number.isInteger(seq) || seq < 0 || seq > 0xFFFFFFFF) {
|
|
9845
|
+
throw new Error(`invalid sequence value: ${seq}`);
|
|
9846
|
+
}
|
|
9847
|
+
return seq;
|
|
9848
|
+
}
|
|
9849
|
+
function parse_stamp(stamp) {
|
|
9850
|
+
if (stamp === undefined || !Number.isInteger(stamp)) {
|
|
9851
|
+
throw new Error(`timestamp must be a number`);
|
|
9852
|
+
}
|
|
9853
|
+
const ts = Math.floor(stamp / TIMELOCK_GRANULARITY);
|
|
9854
|
+
if (!Number.isInteger(ts) || ts < 0 || ts > TIMELOCK_VALUE_MAX) {
|
|
9855
|
+
throw new Error(`timelock value must be an integer between 0 and ${TIMELOCK_VALUE_MAX} (in 512-second increments)`);
|
|
9856
|
+
}
|
|
9857
|
+
return ts;
|
|
9858
|
+
}
|
|
9859
|
+
function parse_height(height) {
|
|
9860
|
+
if (height === undefined || !Number.isInteger(height) || height < 0 || height > TIMELOCK_VALUE_MAX) {
|
|
9861
|
+
throw new Error(`Heightlock value must be an integer between 0 and ${TIMELOCK_VALUE_MAX}`);
|
|
9862
|
+
}
|
|
9863
|
+
return height;
|
|
9864
|
+
}
|
|
9865
|
+
|
|
9866
|
+
var index$6 = /*#__PURE__*/Object.freeze({
|
|
9867
|
+
__proto__: null,
|
|
9868
|
+
get LocktimeUtil () { return LocktimeUtil; },
|
|
9869
|
+
get RefEncoder () { return RefEncoder; },
|
|
9870
|
+
get ScribeEncoder () { return ScribeEncoder; },
|
|
9871
|
+
get SequenceUtil () { return SequenceUtil; },
|
|
9872
|
+
decode_inscription: decode_inscription,
|
|
9873
|
+
decode_locktime: decode_locktime,
|
|
9874
|
+
decode_sequence: decode_sequence,
|
|
9875
|
+
encode_inscription: encode_inscription,
|
|
9876
|
+
encode_locktime: encode_locktime,
|
|
9877
|
+
encode_sequence: encode_sequence
|
|
9878
|
+
});
|
|
9879
|
+
|
|
9880
|
+
class TransactionOutput {
|
|
9881
|
+
constructor(txout) {
|
|
9882
|
+
this._info = get_vout_info(txout);
|
|
9883
|
+
this._size = get_txout_size(txout);
|
|
9884
|
+
this._txout = txout;
|
|
9885
|
+
}
|
|
9886
|
+
get data() {
|
|
9891
9887
|
return {
|
|
9892
|
-
|
|
9893
|
-
|
|
9894
|
-
|
|
9888
|
+
script_pk: this.script_pk,
|
|
9889
|
+
size: this.size,
|
|
9890
|
+
type: this.type,
|
|
9891
|
+
value: this.value,
|
|
9892
|
+
version: this.version
|
|
9895
9893
|
};
|
|
9896
9894
|
}
|
|
9897
|
-
get
|
|
9898
|
-
return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
|
|
9899
|
-
}
|
|
9900
|
-
get size() {
|
|
9895
|
+
get script_pk() {
|
|
9901
9896
|
return {
|
|
9902
|
-
|
|
9903
|
-
|
|
9904
|
-
vin: this._vin.reduce((acc, txin) => acc + txin.size, 0),
|
|
9905
|
-
vout: this._vout.reduce((acc, txout) => acc + txout.size, 0),
|
|
9906
|
-
witness: this._vin.reduce((acc, txin) => acc + (txin.witness?.size ?? 0), 0)
|
|
9897
|
+
hex: this._txout.script_pk,
|
|
9898
|
+
asm: decode_script(this._txout.script_pk)
|
|
9907
9899
|
};
|
|
9908
9900
|
}
|
|
9909
|
-
get
|
|
9910
|
-
return this.
|
|
9911
|
-
.filter(txin => txin.prevout !== null)
|
|
9912
|
-
.map(txin => new TransactionOutput(txin.prevout));
|
|
9901
|
+
get size() {
|
|
9902
|
+
return this._size;
|
|
9913
9903
|
}
|
|
9914
|
-
get
|
|
9915
|
-
return
|
|
9904
|
+
get type() {
|
|
9905
|
+
return this._info.type;
|
|
9916
9906
|
}
|
|
9917
9907
|
get value() {
|
|
9918
|
-
return this.
|
|
9908
|
+
return this._txout.value;
|
|
9919
9909
|
}
|
|
9920
9910
|
get version() {
|
|
9921
|
-
return this.
|
|
9922
|
-
}
|
|
9923
|
-
get vin() {
|
|
9924
|
-
return this._vin;
|
|
9925
|
-
}
|
|
9926
|
-
get vout() {
|
|
9927
|
-
return this._vout;
|
|
9911
|
+
return this._info.version;
|
|
9928
9912
|
}
|
|
9929
9913
|
toJSON() { return this.data; }
|
|
9930
9914
|
toString() { return JSON.stringify(this.data); }
|
|
9931
|
-
};
|
|
9932
|
-
|
|
9933
|
-
const ENCODING_REGEX = {
|
|
9934
|
-
base58: /^[13mn2][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
|
|
9935
|
-
bech32: /^(bc|tb|bcrt)1q[ac-hj-np-z02-9]{6,87}$/,
|
|
9936
|
-
bech32m: /^(bc|tb|bcrt)1p[ac-hj-np-z02-9]{6,87}$/
|
|
9937
|
-
};
|
|
9938
|
-
const VERSION = {
|
|
9939
|
-
bech32: 0,
|
|
9940
|
-
bech32m: 1
|
|
9941
|
-
};
|
|
9942
|
-
function decode_address(address) {
|
|
9943
|
-
const format = get_address_format(address);
|
|
9944
|
-
if (format === null)
|
|
9945
|
-
throw new Error('unrecognized address format: ' + format);
|
|
9946
|
-
if (format === 'base58')
|
|
9947
|
-
return base58_decode(address);
|
|
9948
|
-
if (format === 'bech32')
|
|
9949
|
-
return bech32_decode(address);
|
|
9950
|
-
if (format === 'bech32m')
|
|
9951
|
-
return bech32m_decode(address);
|
|
9952
|
-
throw new Error('unable to find a matching address configuration');
|
|
9953
|
-
}
|
|
9954
|
-
function encode_address(config) {
|
|
9955
|
-
if (config.format === 'base58')
|
|
9956
|
-
return base58_encode(config);
|
|
9957
|
-
if (config.format === 'bech32')
|
|
9958
|
-
return bech32_encode(config);
|
|
9959
|
-
if (config.format === 'bech32m')
|
|
9960
|
-
return bech32m_encode(config);
|
|
9961
|
-
throw new Error('unrecognized encoding format: ' + config.format);
|
|
9962
9915
|
}
|
|
9963
|
-
|
|
9964
|
-
|
|
9965
|
-
|
|
9966
|
-
|
|
9967
|
-
|
|
9968
|
-
|
|
9916
|
+
|
|
9917
|
+
function parse_witness_data(witness) {
|
|
9918
|
+
const elems = witness.map(e => Buff.bytes(e));
|
|
9919
|
+
const annex = parse_annex_data(elems);
|
|
9920
|
+
if (annex !== null)
|
|
9921
|
+
elems.pop();
|
|
9922
|
+
const cblock = parse_cblock_data(elems);
|
|
9923
|
+
if (cblock !== null)
|
|
9924
|
+
elems.pop();
|
|
9925
|
+
const type = parse_witness_type(elems, cblock);
|
|
9926
|
+
const version = parse_witness_version(type);
|
|
9927
|
+
const script = parse_witness_script(elems, type);
|
|
9928
|
+
if (script !== null)
|
|
9929
|
+
elems.pop();
|
|
9930
|
+
const params = elems.map(e => e.hex);
|
|
9931
|
+
return { annex, cblock, params, script, type, version };
|
|
9969
9932
|
}
|
|
9970
|
-
function
|
|
9971
|
-
|
|
9972
|
-
|
|
9973
|
-
|
|
9974
|
-
|
|
9975
|
-
|
|
9976
|
-
|
|
9977
|
-
|
|
9978
|
-
|
|
9979
|
-
|
|
9980
|
-
return { data, format: 'base58', version };
|
|
9981
|
-
}
|
|
9982
|
-
function bech32_encode(config) {
|
|
9983
|
-
Assert.ok(config.format === 'bech32', 'encoding mismatch');
|
|
9984
|
-
Assert.exists(config.prefix, 'prefix is required');
|
|
9985
|
-
const bytes = Buff.bytes(config.data);
|
|
9986
|
-
const words = Bech32.to_words(bytes);
|
|
9987
|
-
return Bech32.encode(config.prefix, [VERSION.bech32, ...words]);
|
|
9988
|
-
}
|
|
9989
|
-
function bech32_decode(encoded) {
|
|
9990
|
-
const { prefix, words } = Bech32.decode(encoded);
|
|
9991
|
-
const [version, ...rest] = words;
|
|
9992
|
-
Assert.ok(version === VERSION.bech32, 'bech32 version mismatch');
|
|
9993
|
-
const data = Bech32.to_bytes(rest);
|
|
9994
|
-
return { data, format: 'bech32', prefix, version };
|
|
9995
|
-
}
|
|
9996
|
-
function bech32m_encode(config) {
|
|
9997
|
-
Assert.ok(config.format === 'bech32m', 'encoding mismatch');
|
|
9998
|
-
Assert.exists(config.prefix, 'prefix is required');
|
|
9999
|
-
const bytes = Buff.bytes(config.data);
|
|
10000
|
-
const words = Bech32m.to_words(bytes);
|
|
10001
|
-
return Bech32m.encode(config.prefix, [VERSION.bech32m, ...words]);
|
|
9933
|
+
function parse_annex_data(data) {
|
|
9934
|
+
let elem = data.at(-1);
|
|
9935
|
+
if (data.length > 1 &&
|
|
9936
|
+
elem instanceof Uint8Array &&
|
|
9937
|
+
elem[0] === 0x50) {
|
|
9938
|
+
return new Buff(elem).hex;
|
|
9939
|
+
}
|
|
9940
|
+
else {
|
|
9941
|
+
return null;
|
|
9942
|
+
}
|
|
10002
9943
|
}
|
|
10003
|
-
function
|
|
10004
|
-
|
|
10005
|
-
|
|
10006
|
-
|
|
10007
|
-
|
|
10008
|
-
|
|
9944
|
+
function parse_cblock_data(data) {
|
|
9945
|
+
let elem = data.at(-1);
|
|
9946
|
+
if (data.length > 1 &&
|
|
9947
|
+
elem instanceof Uint8Array &&
|
|
9948
|
+
elem.length > 32 &&
|
|
9949
|
+
TAPLEAF_VERSIONS.includes(elem[0] & 0xfe)) {
|
|
9950
|
+
return new Buff(elem).hex;
|
|
9951
|
+
}
|
|
9952
|
+
else {
|
|
9953
|
+
return null;
|
|
9954
|
+
}
|
|
10009
9955
|
}
|
|
10010
|
-
|
|
10011
|
-
|
|
10012
|
-
switch (
|
|
10013
|
-
case '
|
|
10014
|
-
|
|
10015
|
-
case 'p2sh':
|
|
10016
|
-
return get_p2sh_script(script_key);
|
|
10017
|
-
case 'p2w-pkh':
|
|
10018
|
-
return get_p2w_pkh_script(script_key);
|
|
9956
|
+
function parse_witness_script(elems, type) {
|
|
9957
|
+
let script;
|
|
9958
|
+
switch (type) {
|
|
9959
|
+
case 'p2tr-ts':
|
|
9960
|
+
script = elems.at(-1);
|
|
10019
9961
|
case 'p2w-sh':
|
|
10020
|
-
|
|
10021
|
-
case 'p2tr':
|
|
10022
|
-
return get_p2tr_script(script_key);
|
|
10023
|
-
default:
|
|
10024
|
-
throw new Error('unrecognized script type: ' + script_type);
|
|
9962
|
+
script = elems.at(-1);
|
|
10025
9963
|
}
|
|
9964
|
+
return (script !== undefined) ? new Buff(script).hex : null;
|
|
10026
9965
|
}
|
|
10027
|
-
function
|
|
10028
|
-
|
|
10029
|
-
|
|
10030
|
-
|
|
10031
|
-
};
|
|
10032
|
-
}
|
|
10033
|
-
function get_p2sh_script(script_key) {
|
|
10034
|
-
return {
|
|
10035
|
-
script_hex: 'a914' + script_key + '87',
|
|
10036
|
-
script_asm: ['OP_HASH160', script_key, 'OP_EQUAL']
|
|
10037
|
-
};
|
|
10038
|
-
}
|
|
10039
|
-
function get_p2w_pkh_script(script_key) {
|
|
10040
|
-
return {
|
|
10041
|
-
script_hex: '0014' + script_key,
|
|
10042
|
-
script_asm: ['OP_0', script_key]
|
|
10043
|
-
};
|
|
10044
|
-
}
|
|
10045
|
-
function get_p2w_sh_script(script_key) {
|
|
10046
|
-
return {
|
|
10047
|
-
script_hex: '0020' + script_key,
|
|
10048
|
-
script_asm: ['OP_0', script_key]
|
|
10049
|
-
};
|
|
10050
|
-
}
|
|
10051
|
-
function get_p2tr_script(script_key) {
|
|
10052
|
-
return {
|
|
10053
|
-
script_hex: '5120' + script_key,
|
|
10054
|
-
script_asm: ['OP_1', script_key]
|
|
10055
|
-
};
|
|
10056
|
-
}
|
|
10057
|
-
|
|
10058
|
-
const CONFIG_TABLE = [
|
|
10059
|
-
['1', 'p2pkh', 'main', 20, 'base58', 0x00],
|
|
10060
|
-
['3', 'p2sh', 'main', 20, 'base58', 0x05],
|
|
10061
|
-
['m', 'p2pkh', 'testnet', 20, 'base58', 0x6F],
|
|
10062
|
-
['n', 'p2pkh', 'testnet', 20, 'base58', 0x6F],
|
|
10063
|
-
['2', 'p2sh', 'testnet', 20, 'base58', 0xC4],
|
|
10064
|
-
['m', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
|
|
10065
|
-
['n', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
|
|
10066
|
-
['2', 'p2sh', 'regtest', 20, 'base58', 0xC4],
|
|
10067
|
-
['bc', 'p2w-pkh', 'main', 20, 'bech32', 0],
|
|
10068
|
-
['tb', 'p2w-pkh', 'testnet', 20, 'bech32', 0],
|
|
10069
|
-
['bcrt', 'p2w-pkh', 'regtest', 20, 'bech32', 0],
|
|
10070
|
-
['bc', 'p2w-sh', 'main', 32, 'bech32', 0],
|
|
10071
|
-
['tb', 'p2w-sh', 'testnet', 32, 'bech32', 0],
|
|
10072
|
-
['bcrt', 'p2w-sh', 'regtest', 32, 'bech32', 0],
|
|
10073
|
-
['bc', 'p2tr', 'main', 32, 'bech32m', 1],
|
|
10074
|
-
['tb', 'p2tr', 'testnet', 32, 'bech32m', 1],
|
|
10075
|
-
['bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1]
|
|
10076
|
-
];
|
|
10077
|
-
function get_address_config(address_network, address_type) {
|
|
10078
|
-
for (const [prefix, type, network, size, format, version] of CONFIG_TABLE) {
|
|
10079
|
-
if (type === address_type && network === address_network) {
|
|
10080
|
-
return { type, prefix, network, size, format, version };
|
|
10081
|
-
}
|
|
9966
|
+
function parse_witness_type(elems, cblock) {
|
|
9967
|
+
let param_0 = elems.at(0), param_1 = elems.at(1), param_x = elems.at(-1);
|
|
9968
|
+
if (cblock !== null && param_x !== undefined) {
|
|
9969
|
+
return 'p2tr-ts';
|
|
10082
9970
|
}
|
|
10083
|
-
|
|
10084
|
-
|
|
10085
|
-
|
|
10086
|
-
|
|
10087
|
-
|
|
10088
|
-
|
|
10089
|
-
|
|
10090
|
-
|
|
10091
|
-
|
|
10092
|
-
|
|
10093
|
-
|
|
10094
|
-
|
|
10095
|
-
|
|
10096
|
-
|
|
10097
|
-
|
|
10098
|
-
|
|
10099
|
-
|
|
10100
|
-
|
|
10101
|
-
|
|
10102
|
-
const hex = Buff.uint(dec.data).hex;
|
|
10103
|
-
return { data: dec.data, hex, type, prefix, network, size, format, version };
|
|
9971
|
+
else if (elems.length === 2 &&
|
|
9972
|
+
param_0 !== undefined &&
|
|
9973
|
+
param_1 !== undefined &&
|
|
9974
|
+
param_0.length >= 64 &&
|
|
9975
|
+
param_1.length === 33) {
|
|
9976
|
+
return 'p2w-pkh';
|
|
9977
|
+
}
|
|
9978
|
+
else if (elems.length === 1 &&
|
|
9979
|
+
param_0 !== undefined &&
|
|
9980
|
+
param_0.length === 64) {
|
|
9981
|
+
return 'p2tr-pk';
|
|
9982
|
+
}
|
|
9983
|
+
else if (elems.length > 1 &&
|
|
9984
|
+
param_x !== undefined &&
|
|
9985
|
+
is_valid_script(param_x)) {
|
|
9986
|
+
return 'p2w-sh';
|
|
9987
|
+
}
|
|
9988
|
+
else {
|
|
9989
|
+
return 'unknown';
|
|
10104
9990
|
}
|
|
10105
|
-
throw new Error('address configuration is invalid');
|
|
10106
9991
|
}
|
|
10107
|
-
function
|
|
10108
|
-
|
|
10109
|
-
|
|
10110
|
-
|
|
9992
|
+
function parse_witness_version(type) {
|
|
9993
|
+
if (type.startsWith('p2tr'))
|
|
9994
|
+
return 1;
|
|
9995
|
+
if (type.startsWith('p2w'))
|
|
9996
|
+
return 0;
|
|
9997
|
+
return null;
|
|
10111
9998
|
}
|
|
10112
9999
|
|
|
10113
|
-
const
|
|
10114
|
-
|
|
10115
|
-
(
|
|
10116
|
-
|
|
10117
|
-
|
|
10118
|
-
|
|
10119
|
-
})(P2PKH || (P2PKH = {}));
|
|
10120
|
-
function create_p2pkh_address(script, network = 'main') {
|
|
10121
|
-
const bytes = Buff.bytes(script);
|
|
10122
|
-
const hash = hash160$1(bytes);
|
|
10123
|
-
return encode_p2pkh_address(hash, network);
|
|
10124
|
-
}
|
|
10125
|
-
function encode_p2pkh_address(pk_hash, network = 'main') {
|
|
10126
|
-
const bytes = Buff.bytes(pk_hash);
|
|
10127
|
-
const config = get_address_config(network, ADDR_TYPE$4);
|
|
10128
|
-
Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$4} on ${network}`);
|
|
10129
|
-
Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
|
|
10130
|
-
return encode_address({
|
|
10131
|
-
data: bytes,
|
|
10132
|
-
format: 'base58',
|
|
10133
|
-
version: config.version
|
|
10134
|
-
});
|
|
10135
|
-
}
|
|
10136
|
-
function decode_p2pkh_address(address) {
|
|
10137
|
-
const parsed = parse_address(address);
|
|
10138
|
-
Assert.ok(parsed.type === 'p2pkh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$4}`);
|
|
10139
|
-
return parsed;
|
|
10000
|
+
const WIT_LENGTH_BYTE = 1;
|
|
10001
|
+
function get_witness_size(witness) {
|
|
10002
|
+
const stack = witness.map(e => Buff.bytes(e));
|
|
10003
|
+
const size = stack.reduce((prev, next) => prev + next.length, 0);
|
|
10004
|
+
const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4);
|
|
10005
|
+
return { size, vsize };
|
|
10140
10006
|
}
|
|
10141
10007
|
|
|
10142
|
-
|
|
10143
|
-
|
|
10144
|
-
|
|
10145
|
-
|
|
10146
|
-
|
|
10147
|
-
P2SH.decode = decode_p2sh_address;
|
|
10148
|
-
})(P2SH || (P2SH = {}));
|
|
10149
|
-
function create_p2sh_address(script, network = 'main') {
|
|
10150
|
-
const bytes = Buff.bytes(script);
|
|
10151
|
-
const hash = hash160$1(bytes);
|
|
10152
|
-
return encode_p2sh_address(hash, network);
|
|
10153
|
-
}
|
|
10154
|
-
function encode_p2sh_address(script_hash, network = 'main') {
|
|
10155
|
-
const bytes = Buff.bytes(script_hash);
|
|
10156
|
-
const config = get_address_config(network, ADDR_TYPE$3);
|
|
10157
|
-
Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$3} on ${network}`);
|
|
10158
|
-
Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
|
|
10159
|
-
return encode_address({
|
|
10160
|
-
data: bytes,
|
|
10161
|
-
format: 'base58',
|
|
10162
|
-
version: config.version
|
|
10163
|
-
});
|
|
10164
|
-
}
|
|
10165
|
-
function decode_p2sh_address(address) {
|
|
10166
|
-
const parsed = parse_address(address);
|
|
10167
|
-
Assert.ok(parsed.type === 'p2sh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$3}`);
|
|
10168
|
-
return parsed;
|
|
10169
|
-
}
|
|
10008
|
+
var index$5 = /*#__PURE__*/Object.freeze({
|
|
10009
|
+
__proto__: null,
|
|
10010
|
+
get_witness_size: get_witness_size,
|
|
10011
|
+
parse_witness_data: parse_witness_data
|
|
10012
|
+
});
|
|
10170
10013
|
|
|
10171
|
-
|
|
10172
|
-
|
|
10173
|
-
(
|
|
10174
|
-
|
|
10175
|
-
|
|
10176
|
-
|
|
10177
|
-
|
|
10178
|
-
|
|
10179
|
-
|
|
10180
|
-
|
|
10181
|
-
|
|
10182
|
-
|
|
10183
|
-
|
|
10184
|
-
|
|
10185
|
-
|
|
10186
|
-
|
|
10187
|
-
|
|
10188
|
-
|
|
10189
|
-
|
|
10190
|
-
|
|
10191
|
-
|
|
10192
|
-
|
|
10193
|
-
|
|
10194
|
-
}
|
|
10195
|
-
|
|
10196
|
-
|
|
10197
|
-
|
|
10198
|
-
|
|
10199
|
-
|
|
10200
|
-
|
|
10201
|
-
|
|
10202
|
-
|
|
10203
|
-
|
|
10204
|
-
|
|
10205
|
-
|
|
10206
|
-
|
|
10207
|
-
|
|
10208
|
-
|
|
10209
|
-
|
|
10210
|
-
|
|
10211
|
-
|
|
10212
|
-
}
|
|
10213
|
-
|
|
10214
|
-
|
|
10215
|
-
|
|
10216
|
-
|
|
10217
|
-
|
|
10218
|
-
|
|
10219
|
-
|
|
10220
|
-
|
|
10221
|
-
|
|
10222
|
-
|
|
10223
|
-
}
|
|
10224
|
-
function decode_p2wsh_address(address) {
|
|
10225
|
-
const parsed = parse_address(address);
|
|
10226
|
-
Assert.ok(parsed.type === 'p2w-sh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$1}`);
|
|
10227
|
-
return parsed;
|
|
10014
|
+
class TransactionWitness {
|
|
10015
|
+
constructor(witness) {
|
|
10016
|
+
this._data = witness.map(e => Buff.bytes(e));
|
|
10017
|
+
this._meta = parse_witness_data(witness);
|
|
10018
|
+
this._size = get_witness_size(witness);
|
|
10019
|
+
}
|
|
10020
|
+
get annex() {
|
|
10021
|
+
return this._meta.annex;
|
|
10022
|
+
}
|
|
10023
|
+
get cblock() {
|
|
10024
|
+
return this._meta.cblock;
|
|
10025
|
+
}
|
|
10026
|
+
get data() {
|
|
10027
|
+
return {
|
|
10028
|
+
annex: this.annex,
|
|
10029
|
+
cblock: this.cblock,
|
|
10030
|
+
params: this.params,
|
|
10031
|
+
script: this.script,
|
|
10032
|
+
size: this.size,
|
|
10033
|
+
stack: this.stack,
|
|
10034
|
+
type: this.type,
|
|
10035
|
+
version: this.version,
|
|
10036
|
+
vsize: this.vsize
|
|
10037
|
+
};
|
|
10038
|
+
}
|
|
10039
|
+
get params() {
|
|
10040
|
+
return this._meta.params;
|
|
10041
|
+
}
|
|
10042
|
+
get script() {
|
|
10043
|
+
if (this._meta.script === null)
|
|
10044
|
+
return null;
|
|
10045
|
+
return {
|
|
10046
|
+
hex: this._meta.script,
|
|
10047
|
+
asm: decode_script(this._meta.script)
|
|
10048
|
+
};
|
|
10049
|
+
}
|
|
10050
|
+
get size() {
|
|
10051
|
+
return this._size.size;
|
|
10052
|
+
}
|
|
10053
|
+
get stack() {
|
|
10054
|
+
return this._data.map(e => e.hex);
|
|
10055
|
+
}
|
|
10056
|
+
get type() {
|
|
10057
|
+
return this._meta.type;
|
|
10058
|
+
}
|
|
10059
|
+
get version() {
|
|
10060
|
+
return this._meta.version;
|
|
10061
|
+
}
|
|
10062
|
+
get vsize() {
|
|
10063
|
+
return this._size.vsize;
|
|
10064
|
+
}
|
|
10065
|
+
toJSON() { return this.data; }
|
|
10066
|
+
toString() { return JSON.stringify(this.data); }
|
|
10228
10067
|
}
|
|
10229
10068
|
|
|
10230
|
-
|
|
10231
|
-
|
|
10232
|
-
|
|
10233
|
-
|
|
10234
|
-
|
|
10235
|
-
|
|
10236
|
-
|
|
10237
|
-
|
|
10238
|
-
|
|
10239
|
-
|
|
10240
|
-
|
|
10241
|
-
|
|
10242
|
-
|
|
10243
|
-
|
|
10244
|
-
|
|
10245
|
-
|
|
10246
|
-
|
|
10247
|
-
|
|
10248
|
-
|
|
10249
|
-
|
|
10250
|
-
|
|
10069
|
+
class TransactionInput {
|
|
10070
|
+
constructor(txin) {
|
|
10071
|
+
this._size = get_txin_size(txin);
|
|
10072
|
+
this._txin = txin;
|
|
10073
|
+
this._witness = txin.witness.length > 0
|
|
10074
|
+
? new TransactionWitness(txin.witness)
|
|
10075
|
+
: null;
|
|
10076
|
+
}
|
|
10077
|
+
get coinbase() {
|
|
10078
|
+
return this._txin.coinbase;
|
|
10079
|
+
}
|
|
10080
|
+
get data() {
|
|
10081
|
+
return {
|
|
10082
|
+
coinbase: this.coinbase,
|
|
10083
|
+
prevout: this.prevout?.data ?? null,
|
|
10084
|
+
script_sig: this.script_sig,
|
|
10085
|
+
sequence: this.sequence,
|
|
10086
|
+
size: this.size,
|
|
10087
|
+
txid: this.txid,
|
|
10088
|
+
vout: this.vout,
|
|
10089
|
+
witness: this.witness?.data ?? null
|
|
10090
|
+
};
|
|
10091
|
+
}
|
|
10092
|
+
get has_prevout() {
|
|
10093
|
+
return this._txin.prevout !== null;
|
|
10094
|
+
}
|
|
10095
|
+
get is_coinbase() {
|
|
10096
|
+
return this._txin.coinbase !== null;
|
|
10097
|
+
}
|
|
10098
|
+
get prevout() {
|
|
10099
|
+
return this._txin.prevout
|
|
10100
|
+
? new TransactionOutput(this._txin.prevout)
|
|
10101
|
+
: null;
|
|
10102
|
+
}
|
|
10103
|
+
get script_sig() {
|
|
10104
|
+
if (this._txin.script_sig === null)
|
|
10105
|
+
return null;
|
|
10106
|
+
return {
|
|
10107
|
+
asm: decode_script(this._txin.script_sig),
|
|
10108
|
+
hex: this._txin.script_sig
|
|
10109
|
+
};
|
|
10110
|
+
}
|
|
10111
|
+
get sequence() {
|
|
10112
|
+
return {
|
|
10113
|
+
hex: encode_txin_sequence(this._txin.sequence).hex,
|
|
10114
|
+
data: SequenceUtil.decode(this._txin.sequence),
|
|
10115
|
+
value: this._txin.sequence
|
|
10116
|
+
};
|
|
10117
|
+
}
|
|
10118
|
+
get size() {
|
|
10119
|
+
return this._size;
|
|
10120
|
+
}
|
|
10121
|
+
get txid() {
|
|
10122
|
+
return this._txin.txid;
|
|
10123
|
+
}
|
|
10124
|
+
get vout() {
|
|
10125
|
+
return this._txin.vout;
|
|
10126
|
+
}
|
|
10127
|
+
get witness() {
|
|
10128
|
+
return this._witness;
|
|
10129
|
+
}
|
|
10130
|
+
toJSON() { return this.data; }
|
|
10131
|
+
toString() { return JSON.stringify(this.data); }
|
|
10251
10132
|
}
|
|
10252
10133
|
|
|
10253
|
-
|
|
10254
|
-
(
|
|
10255
|
-
|
|
10256
|
-
|
|
10257
|
-
|
|
10258
|
-
|
|
10259
|
-
|
|
10260
|
-
|
|
10261
|
-
|
|
10262
|
-
|
|
10263
|
-
|
|
10264
|
-
|
|
10265
|
-
|
|
10266
|
-
|
|
10267
|
-
|
|
10268
|
-
|
|
10269
|
-
|
|
10270
|
-
|
|
10271
|
-
|
|
10272
|
-
|
|
10134
|
+
let Transaction$1 = class Transaction {
|
|
10135
|
+
constructor(txdata) {
|
|
10136
|
+
this._tx = (typeof txdata !== 'string')
|
|
10137
|
+
? parse_tx(txdata)
|
|
10138
|
+
: decode_tx(txdata);
|
|
10139
|
+
this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
|
|
10140
|
+
this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
|
|
10141
|
+
this._size = get_txsize(this._tx);
|
|
10142
|
+
this._hash = get_txhash(this._tx);
|
|
10143
|
+
this._value = get_tx_value(this._tx);
|
|
10144
|
+
}
|
|
10145
|
+
get data() {
|
|
10146
|
+
return {
|
|
10147
|
+
hash: this.hash,
|
|
10148
|
+
locktime: this.locktime,
|
|
10149
|
+
return: this.return,
|
|
10150
|
+
size: this.size,
|
|
10151
|
+
spends: this.spends,
|
|
10152
|
+
txid: this.txid,
|
|
10153
|
+
value: this.value,
|
|
10154
|
+
version: this.version,
|
|
10155
|
+
vin: this.vin.map(txin => txin.data),
|
|
10156
|
+
vout: this.vout.map(txout => txout.data)
|
|
10157
|
+
};
|
|
10158
|
+
}
|
|
10159
|
+
get hash() {
|
|
10160
|
+
return this._hash;
|
|
10161
|
+
}
|
|
10162
|
+
get locktime() {
|
|
10163
|
+
return {
|
|
10164
|
+
hex: encode_tx_locktime(this._tx.locktime).hex,
|
|
10165
|
+
data: LocktimeUtil.decode(this._tx.locktime),
|
|
10166
|
+
value: this._tx.locktime
|
|
10167
|
+
};
|
|
10168
|
+
}
|
|
10169
|
+
get return() {
|
|
10170
|
+
return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
|
|
10171
|
+
}
|
|
10172
|
+
get size() {
|
|
10173
|
+
return {
|
|
10174
|
+
...this._size,
|
|
10175
|
+
segwit: this._vin.reduce((acc, txin) => acc + (txin.witness?.vsize ?? 0), 0),
|
|
10176
|
+
vin: this._vin.reduce((acc, txin) => acc + txin.size, 0),
|
|
10177
|
+
vout: this._vout.reduce((acc, txout) => acc + txout.size, 0),
|
|
10178
|
+
witness: this._vin.reduce((acc, txin) => acc + (txin.witness?.size ?? 0), 0)
|
|
10179
|
+
};
|
|
10180
|
+
}
|
|
10181
|
+
get spends() {
|
|
10182
|
+
return this._tx.vin
|
|
10183
|
+
.filter(txin => txin.prevout !== null)
|
|
10184
|
+
.map(txin => new TransactionOutput(txin.prevout));
|
|
10185
|
+
}
|
|
10186
|
+
get txid() {
|
|
10187
|
+
return get_txid(this._tx);
|
|
10188
|
+
}
|
|
10189
|
+
get value() {
|
|
10190
|
+
return this._value;
|
|
10191
|
+
}
|
|
10192
|
+
get version() {
|
|
10193
|
+
return this._tx.version;
|
|
10194
|
+
}
|
|
10195
|
+
get vin() {
|
|
10196
|
+
return this._vin;
|
|
10197
|
+
}
|
|
10198
|
+
get vout() {
|
|
10199
|
+
return this._vout;
|
|
10200
|
+
}
|
|
10201
|
+
toJSON() { return this.data; }
|
|
10202
|
+
toString() { return JSON.stringify(this.data); }
|
|
10203
|
+
};
|
|
10273
10204
|
|
|
10274
|
-
|
|
10275
|
-
|
|
10276
|
-
|
|
10277
|
-
|
|
10278
|
-
|
|
10279
|
-
|
|
10280
|
-
|
|
10281
|
-
|
|
10282
|
-
|
|
10283
|
-
|
|
10284
|
-
|
|
10285
|
-
|
|
10286
|
-
|
|
10287
|
-
|
|
10288
|
-
|
|
10289
|
-
|
|
10290
|
-
|
|
10291
|
-
|
|
10292
|
-
|
|
10293
|
-
|
|
10294
|
-
})(TxPointer || (TxPointer = {}));
|
|
10295
|
-
function encode_inscription_id(txid, order = 0) {
|
|
10296
|
-
return `${txid}i${order}`;
|
|
10297
|
-
}
|
|
10298
|
-
function decode_inscription_id(inscription_id) {
|
|
10299
|
-
assert_inscription_id(inscription_id);
|
|
10300
|
-
const [txid, order] = inscription_id.split('i');
|
|
10301
|
-
return { txid, order: parseInt(order) };
|
|
10205
|
+
const ENCODING_REGEX = {
|
|
10206
|
+
base58: /^[13mn2][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
|
|
10207
|
+
bech32: /^(bc|tb|bcrt)1q[ac-hj-np-z02-9]{6,87}$/,
|
|
10208
|
+
bech32m: /^(bc|tb|bcrt)1p[ac-hj-np-z02-9]{6,87}$/
|
|
10209
|
+
};
|
|
10210
|
+
const VERSION = {
|
|
10211
|
+
bech32: 0,
|
|
10212
|
+
bech32m: 1
|
|
10213
|
+
};
|
|
10214
|
+
function decode_address(address) {
|
|
10215
|
+
const format = get_address_format(address);
|
|
10216
|
+
if (format === null)
|
|
10217
|
+
throw new Error('unrecognized address format: ' + format);
|
|
10218
|
+
if (format === 'base58')
|
|
10219
|
+
return base58_decode(address);
|
|
10220
|
+
if (format === 'bech32')
|
|
10221
|
+
return bech32_decode(address);
|
|
10222
|
+
if (format === 'bech32m')
|
|
10223
|
+
return bech32m_decode(address);
|
|
10224
|
+
throw new Error('unable to find a matching address configuration');
|
|
10302
10225
|
}
|
|
10303
|
-
function
|
|
10304
|
-
|
|
10226
|
+
function encode_address(config) {
|
|
10227
|
+
if (config.format === 'base58')
|
|
10228
|
+
return base58_encode(config);
|
|
10229
|
+
if (config.format === 'bech32')
|
|
10230
|
+
return bech32_encode(config);
|
|
10231
|
+
if (config.format === 'bech32m')
|
|
10232
|
+
return bech32m_encode(config);
|
|
10233
|
+
throw new Error('unrecognized encoding format: ' + config.format);
|
|
10305
10234
|
}
|
|
10306
|
-
function
|
|
10307
|
-
|
|
10308
|
-
|
|
10235
|
+
function get_address_format(address) {
|
|
10236
|
+
for (const [format, regex] of Object.entries(ENCODING_REGEX)) {
|
|
10237
|
+
if (regex.test(address))
|
|
10238
|
+
return format;
|
|
10309
10239
|
}
|
|
10240
|
+
return null;
|
|
10310
10241
|
}
|
|
10311
|
-
function
|
|
10312
|
-
|
|
10313
|
-
|
|
10314
|
-
|
|
10315
|
-
|
|
10316
|
-
const [block_height, block_index] = rune_id.split(':');
|
|
10317
|
-
return { block_height: parseInt(block_height), block_index: parseInt(block_index) };
|
|
10242
|
+
function base58_encode(config) {
|
|
10243
|
+
Assert.ok(config.format === 'base58', 'encoding mismatch');
|
|
10244
|
+
Assert.exists(config.version, 'must specify a version');
|
|
10245
|
+
const bytes = Buff.join([config.version, config.data]);
|
|
10246
|
+
return B58chk.encode(bytes);
|
|
10318
10247
|
}
|
|
10319
|
-
function
|
|
10320
|
-
|
|
10248
|
+
function base58_decode(encoded) {
|
|
10249
|
+
const bytes = B58chk.decode(encoded);
|
|
10250
|
+
const data = bytes.slice(1);
|
|
10251
|
+
const version = bytes[0];
|
|
10252
|
+
return { data, format: 'base58', version };
|
|
10321
10253
|
}
|
|
10322
|
-
function
|
|
10323
|
-
|
|
10324
|
-
|
|
10325
|
-
|
|
10254
|
+
function bech32_encode(config) {
|
|
10255
|
+
Assert.ok(config.format === 'bech32', 'encoding mismatch');
|
|
10256
|
+
Assert.exists(config.prefix, 'prefix is required');
|
|
10257
|
+
const bytes = Buff.bytes(config.data);
|
|
10258
|
+
const words = Bech32.to_words(bytes);
|
|
10259
|
+
return Bech32.encode(config.prefix, [VERSION.bech32, ...words]);
|
|
10326
10260
|
}
|
|
10327
|
-
function
|
|
10328
|
-
|
|
10261
|
+
function bech32_decode(encoded) {
|
|
10262
|
+
const { prefix, words } = Bech32.decode(encoded);
|
|
10263
|
+
const [version, ...rest] = words;
|
|
10264
|
+
Assert.ok(version === VERSION.bech32, 'bech32 version mismatch');
|
|
10265
|
+
const data = Bech32.to_bytes(rest);
|
|
10266
|
+
return { data, format: 'bech32', prefix, version };
|
|
10329
10267
|
}
|
|
10330
|
-
function
|
|
10331
|
-
|
|
10332
|
-
|
|
10333
|
-
|
|
10268
|
+
function bech32m_encode(config) {
|
|
10269
|
+
Assert.ok(config.format === 'bech32m', 'encoding mismatch');
|
|
10270
|
+
Assert.exists(config.prefix, 'prefix is required');
|
|
10271
|
+
const bytes = Buff.bytes(config.data);
|
|
10272
|
+
const words = Bech32m.to_words(bytes);
|
|
10273
|
+
return Bech32m.encode(config.prefix, [VERSION.bech32m, ...words]);
|
|
10334
10274
|
}
|
|
10335
|
-
function
|
|
10336
|
-
|
|
10275
|
+
function bech32m_decode(encoded) {
|
|
10276
|
+
const { prefix, words } = Bech32m.decode(encoded);
|
|
10277
|
+
const [version, ...rest] = words;
|
|
10278
|
+
Assert.ok(version === VERSION.bech32m, 'bech32m version mismatch');
|
|
10279
|
+
const data = Bech32m.to_bytes(rest);
|
|
10280
|
+
return { data, format: 'bech32m', prefix, version };
|
|
10337
10281
|
}
|
|
10338
|
-
|
|
10339
|
-
|
|
10340
|
-
|
|
10282
|
+
|
|
10283
|
+
function get_address_script(script_key, script_type) {
|
|
10284
|
+
switch (script_type) {
|
|
10285
|
+
case 'p2pkh':
|
|
10286
|
+
return get_p2pkh_script(script_key);
|
|
10287
|
+
case 'p2sh':
|
|
10288
|
+
return get_p2sh_script(script_key);
|
|
10289
|
+
case 'p2w-pkh':
|
|
10290
|
+
return get_p2w_pkh_script(script_key);
|
|
10291
|
+
case 'p2w-sh':
|
|
10292
|
+
return get_p2w_sh_script(script_key);
|
|
10293
|
+
case 'p2tr':
|
|
10294
|
+
return get_p2tr_script(script_key);
|
|
10295
|
+
default:
|
|
10296
|
+
throw new Error('unrecognized script type: ' + script_type);
|
|
10341
10297
|
}
|
|
10342
10298
|
}
|
|
10343
|
-
|
|
10344
|
-
|
|
10345
|
-
|
|
10346
|
-
|
|
10347
|
-
|
|
10348
|
-
(function (Inscription) {
|
|
10349
|
-
Inscription.encode = encode_inscription;
|
|
10350
|
-
Inscription.decode = decode_inscription;
|
|
10351
|
-
})(Inscription || (Inscription = {}));
|
|
10352
|
-
function decode_inscription(script) {
|
|
10353
|
-
const envelopes = parse_envelopes(script);
|
|
10354
|
-
return envelopes.map(parse_record);
|
|
10299
|
+
function get_p2pkh_script(script_key) {
|
|
10300
|
+
return {
|
|
10301
|
+
script_hex: '76a914' + script_key + '88ac',
|
|
10302
|
+
script_asm: ['OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG']
|
|
10303
|
+
};
|
|
10355
10304
|
}
|
|
10356
|
-
function
|
|
10357
|
-
return
|
|
10305
|
+
function get_p2sh_script(script_key) {
|
|
10306
|
+
return {
|
|
10307
|
+
script_hex: 'a914' + script_key + '87',
|
|
10308
|
+
script_asm: ['OP_HASH160', script_key, 'OP_EQUAL']
|
|
10309
|
+
};
|
|
10358
10310
|
}
|
|
10359
|
-
function
|
|
10360
|
-
|
|
10361
|
-
|
|
10362
|
-
|
|
10363
|
-
|
|
10364
|
-
}
|
|
10365
|
-
if (typeof data.ref === 'string') {
|
|
10366
|
-
asm.push('OP_WITHIN', data.ref);
|
|
10367
|
-
}
|
|
10368
|
-
if (typeof data.parent === 'string') {
|
|
10369
|
-
const id = encode_id(data.parent);
|
|
10370
|
-
asm.push('OP_3', id);
|
|
10371
|
-
}
|
|
10372
|
-
if (typeof data.opcode === 'number') {
|
|
10373
|
-
const code = encode_pointer(data.opcode);
|
|
10374
|
-
asm.push('OP_NOP', code);
|
|
10375
|
-
}
|
|
10376
|
-
if (typeof data.pointer === 'number') {
|
|
10377
|
-
const ptr = encode_pointer(data.pointer);
|
|
10378
|
-
asm.push('OP_2', ptr);
|
|
10379
|
-
}
|
|
10380
|
-
if (typeof data.rune === 'string') {
|
|
10381
|
-
const label = encode_rune_label(data.rune);
|
|
10382
|
-
asm.push('OP_13', label);
|
|
10383
|
-
}
|
|
10384
|
-
if (typeof data.mimetype === 'string') {
|
|
10385
|
-
const label = encode_label(data.mimetype);
|
|
10386
|
-
asm.push('OP_1', label);
|
|
10387
|
-
}
|
|
10388
|
-
if (typeof data.content === 'string') {
|
|
10389
|
-
const chunks = encode_content(data.content);
|
|
10390
|
-
asm.push('OP_0', ...chunks);
|
|
10391
|
-
}
|
|
10392
|
-
asm.push('OP_ENDIF');
|
|
10393
|
-
return encode_script(asm);
|
|
10311
|
+
function get_p2w_pkh_script(script_key) {
|
|
10312
|
+
return {
|
|
10313
|
+
script_hex: '0014' + script_key,
|
|
10314
|
+
script_asm: ['OP_0', script_key]
|
|
10315
|
+
};
|
|
10394
10316
|
}
|
|
10395
|
-
function
|
|
10396
|
-
|
|
10397
|
-
|
|
10398
|
-
|
|
10399
|
-
|
|
10400
|
-
|
|
10401
|
-
|
|
10402
|
-
|
|
10403
|
-
|
|
10404
|
-
|
|
10405
|
-
|
|
10406
|
-
|
|
10407
|
-
|
|
10317
|
+
function get_p2w_sh_script(script_key) {
|
|
10318
|
+
return {
|
|
10319
|
+
script_hex: '0020' + script_key,
|
|
10320
|
+
script_asm: ['OP_0', script_key]
|
|
10321
|
+
};
|
|
10322
|
+
}
|
|
10323
|
+
function get_p2tr_script(script_key) {
|
|
10324
|
+
return {
|
|
10325
|
+
script_hex: '5120' + script_key,
|
|
10326
|
+
script_asm: ['OP_1', script_key]
|
|
10327
|
+
};
|
|
10328
|
+
}
|
|
10329
|
+
|
|
10330
|
+
const CONFIG_TABLE = [
|
|
10331
|
+
['1', 'p2pkh', 'main', 20, 'base58', 0x00],
|
|
10332
|
+
['3', 'p2sh', 'main', 20, 'base58', 0x05],
|
|
10333
|
+
['m', 'p2pkh', 'testnet', 20, 'base58', 0x6F],
|
|
10334
|
+
['n', 'p2pkh', 'testnet', 20, 'base58', 0x6F],
|
|
10335
|
+
['2', 'p2sh', 'testnet', 20, 'base58', 0xC4],
|
|
10336
|
+
['m', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
|
|
10337
|
+
['n', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
|
|
10338
|
+
['2', 'p2sh', 'regtest', 20, 'base58', 0xC4],
|
|
10339
|
+
['bc', 'p2w-pkh', 'main', 20, 'bech32', 0],
|
|
10340
|
+
['tb', 'p2w-pkh', 'testnet', 20, 'bech32', 0],
|
|
10341
|
+
['bcrt', 'p2w-pkh', 'regtest', 20, 'bech32', 0],
|
|
10342
|
+
['bc', 'p2w-sh', 'main', 32, 'bech32', 0],
|
|
10343
|
+
['tb', 'p2w-sh', 'testnet', 32, 'bech32', 0],
|
|
10344
|
+
['bcrt', 'p2w-sh', 'regtest', 32, 'bech32', 0],
|
|
10345
|
+
['bc', 'p2tr', 'main', 32, 'bech32m', 1],
|
|
10346
|
+
['tb', 'p2tr', 'testnet', 32, 'bech32m', 1],
|
|
10347
|
+
['bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1]
|
|
10348
|
+
];
|
|
10349
|
+
function get_address_config(address_network, address_type) {
|
|
10350
|
+
for (const [prefix, type, network, size, format, version] of CONFIG_TABLE) {
|
|
10351
|
+
if (type === address_type && network === address_network) {
|
|
10352
|
+
return { type, prefix, network, size, format, version };
|
|
10353
|
+
}
|
|
10408
10354
|
}
|
|
10409
|
-
return
|
|
10355
|
+
return null;
|
|
10410
10356
|
}
|
|
10411
|
-
function
|
|
10412
|
-
const
|
|
10413
|
-
for (
|
|
10414
|
-
|
|
10415
|
-
|
|
10416
|
-
|
|
10417
|
-
|
|
10418
|
-
|
|
10419
|
-
|
|
10420
|
-
|
|
10421
|
-
|
|
10422
|
-
|
|
10423
|
-
case 'OP_3':
|
|
10424
|
-
record.parent = decode_id(envelope[i + 1]);
|
|
10425
|
-
i += 1;
|
|
10426
|
-
break;
|
|
10427
|
-
case 'OP_11':
|
|
10428
|
-
record.delegate = decode_id(envelope[i + 1]);
|
|
10429
|
-
i += 1;
|
|
10430
|
-
break;
|
|
10431
|
-
case 'OP_13':
|
|
10432
|
-
record.rune = decode_rune_label(envelope[i + 1]);
|
|
10433
|
-
i += 1;
|
|
10434
|
-
break;
|
|
10435
|
-
case 'OP_WITHIN':
|
|
10436
|
-
record.ref = envelope[i + 1];
|
|
10437
|
-
i += 1;
|
|
10438
|
-
break;
|
|
10439
|
-
case 'OP_NOP':
|
|
10440
|
-
record.opcode = decode_pointer(envelope[i + 1]);
|
|
10441
|
-
i += 1;
|
|
10442
|
-
break;
|
|
10443
|
-
case 'OP_0':
|
|
10444
|
-
record.content = decode_content(envelope.slice(i + 1));
|
|
10445
|
-
return record;
|
|
10357
|
+
function get_address_ctx(address) {
|
|
10358
|
+
const dec = decode_address(address);
|
|
10359
|
+
for (const [prefix, type, network, size, format, version] of CONFIG_TABLE) {
|
|
10360
|
+
if (format !== dec.format)
|
|
10361
|
+
continue;
|
|
10362
|
+
if (size !== dec.data.length)
|
|
10363
|
+
continue;
|
|
10364
|
+
if (version !== dec.version)
|
|
10365
|
+
continue;
|
|
10366
|
+
if (dec.prefix) {
|
|
10367
|
+
if (prefix !== dec.prefix)
|
|
10368
|
+
continue;
|
|
10446
10369
|
}
|
|
10370
|
+
else {
|
|
10371
|
+
if (!address.startsWith(prefix))
|
|
10372
|
+
continue;
|
|
10373
|
+
}
|
|
10374
|
+
const hex = Buff.uint(dec.data).hex;
|
|
10375
|
+
return { data: dec.data, hex, type, prefix, network, size, format, version };
|
|
10447
10376
|
}
|
|
10448
|
-
|
|
10377
|
+
throw new Error('address configuration is invalid');
|
|
10449
10378
|
}
|
|
10450
|
-
function
|
|
10451
|
-
|
|
10452
|
-
const
|
|
10453
|
-
|
|
10454
|
-
const idx = Number(parts[1]);
|
|
10455
|
-
const txid = bytes.reverse().hex;
|
|
10456
|
-
return (idx !== 0) ? txid + Buff.num(idx).hex : txid;
|
|
10379
|
+
function parse_address(address) {
|
|
10380
|
+
const ctx = get_address_ctx(address);
|
|
10381
|
+
const script = get_address_script(ctx.hex, ctx.type);
|
|
10382
|
+
return { ...ctx, ...script };
|
|
10457
10383
|
}
|
|
10458
|
-
|
|
10459
|
-
|
|
10460
|
-
|
|
10461
|
-
|
|
10462
|
-
|
|
10384
|
+
|
|
10385
|
+
const ADDR_TYPE$4 = 'p2pkh';
|
|
10386
|
+
var P2PKH;
|
|
10387
|
+
(function (P2PKH) {
|
|
10388
|
+
P2PKH.create = create_p2pkh_address;
|
|
10389
|
+
P2PKH.encode = encode_p2pkh_address;
|
|
10390
|
+
P2PKH.decode = decode_p2pkh_address;
|
|
10391
|
+
})(P2PKH || (P2PKH = {}));
|
|
10392
|
+
function create_p2pkh_address(script, network = 'main') {
|
|
10393
|
+
const bytes = Buff.bytes(script);
|
|
10394
|
+
const hash = hash160$1(bytes);
|
|
10395
|
+
return encode_p2pkh_address(hash, network);
|
|
10463
10396
|
}
|
|
10464
|
-
function
|
|
10465
|
-
|
|
10397
|
+
function encode_p2pkh_address(pk_hash, network = 'main') {
|
|
10398
|
+
const bytes = Buff.bytes(pk_hash);
|
|
10399
|
+
const config = get_address_config(network, ADDR_TYPE$4);
|
|
10400
|
+
Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$4} on ${network}`);
|
|
10401
|
+
Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
|
|
10402
|
+
return encode_address({
|
|
10403
|
+
data: bytes,
|
|
10404
|
+
format: 'base58',
|
|
10405
|
+
version: config.version
|
|
10406
|
+
});
|
|
10466
10407
|
}
|
|
10467
|
-
function
|
|
10468
|
-
|
|
10408
|
+
function decode_p2pkh_address(address) {
|
|
10409
|
+
const parsed = parse_address(address);
|
|
10410
|
+
Assert.ok(parsed.type === 'p2pkh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$4}`);
|
|
10411
|
+
return parsed;
|
|
10469
10412
|
}
|
|
10470
|
-
|
|
10471
|
-
|
|
10413
|
+
|
|
10414
|
+
const ADDR_TYPE$3 = 'p2sh';
|
|
10415
|
+
var P2SH;
|
|
10416
|
+
(function (P2SH) {
|
|
10417
|
+
P2SH.create = create_p2sh_address;
|
|
10418
|
+
P2SH.encode = encode_p2sh_address;
|
|
10419
|
+
P2SH.decode = decode_p2sh_address;
|
|
10420
|
+
})(P2SH || (P2SH = {}));
|
|
10421
|
+
function create_p2sh_address(script, network = 'main') {
|
|
10422
|
+
const bytes = Buff.bytes(script);
|
|
10423
|
+
const hash = hash160$1(bytes);
|
|
10424
|
+
return encode_p2sh_address(hash, network);
|
|
10472
10425
|
}
|
|
10473
|
-
function
|
|
10474
|
-
|
|
10426
|
+
function encode_p2sh_address(script_hash, network = 'main') {
|
|
10427
|
+
const bytes = Buff.bytes(script_hash);
|
|
10428
|
+
const config = get_address_config(network, ADDR_TYPE$3);
|
|
10429
|
+
Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$3} on ${network}`);
|
|
10430
|
+
Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
|
|
10431
|
+
return encode_address({
|
|
10432
|
+
data: bytes,
|
|
10433
|
+
format: 'base58',
|
|
10434
|
+
version: config.version
|
|
10435
|
+
});
|
|
10475
10436
|
}
|
|
10476
|
-
function
|
|
10477
|
-
const
|
|
10478
|
-
|
|
10479
|
-
|
|
10480
|
-
const stream = new Stream(bytes);
|
|
10481
|
-
const chunks = [];
|
|
10482
|
-
while (stream.size > 0) {
|
|
10483
|
-
if (stream.size > 520) {
|
|
10484
|
-
const chunk = stream.read(520);
|
|
10485
|
-
chunks.push(chunk.hex);
|
|
10486
|
-
}
|
|
10487
|
-
else {
|
|
10488
|
-
const chunk = stream.read(stream.size);
|
|
10489
|
-
chunks.push(chunk.hex);
|
|
10490
|
-
}
|
|
10491
|
-
}
|
|
10492
|
-
return chunks;
|
|
10437
|
+
function decode_p2sh_address(address) {
|
|
10438
|
+
const parsed = parse_address(address);
|
|
10439
|
+
Assert.ok(parsed.type === 'p2sh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$3}`);
|
|
10440
|
+
return parsed;
|
|
10493
10441
|
}
|
|
10494
|
-
|
|
10495
|
-
|
|
10496
|
-
|
|
10497
|
-
|
|
10498
|
-
|
|
10442
|
+
|
|
10443
|
+
const ADDR_TYPE$2 = 'p2w-pkh';
|
|
10444
|
+
var P2WPKH;
|
|
10445
|
+
(function (P2WPKH) {
|
|
10446
|
+
P2WPKH.create = create_p2wpkh_address;
|
|
10447
|
+
P2WPKH.encode = encode_p2wpkh_address;
|
|
10448
|
+
P2WPKH.decode = decode_p2wpkh_address;
|
|
10449
|
+
})(P2WPKH || (P2WPKH = {}));
|
|
10450
|
+
function create_p2wpkh_address(pubkey, network = 'main') {
|
|
10451
|
+
const bytes = Buff.bytes(pubkey);
|
|
10452
|
+
Assert.size(bytes, 33, `invalid payload size: ${bytes.length} !== 33`);
|
|
10453
|
+
const hash = hash160$1(bytes);
|
|
10454
|
+
return encode_p2wpkh_address(hash, network);
|
|
10499
10455
|
}
|
|
10500
|
-
function
|
|
10501
|
-
const
|
|
10502
|
-
|
|
10503
|
-
|
|
10504
|
-
|
|
10505
|
-
|
|
10506
|
-
|
|
10507
|
-
|
|
10508
|
-
|
|
10509
|
-
|
|
10510
|
-
}
|
|
10511
|
-
big = big - _1n;
|
|
10512
|
-
return Buff.big(big).reverse().hex;
|
|
10456
|
+
function encode_p2wpkh_address(pk_hash, network = 'main') {
|
|
10457
|
+
const bytes = Buff.bytes(pk_hash);
|
|
10458
|
+
const config = get_address_config(network, ADDR_TYPE$2);
|
|
10459
|
+
Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$2} on ${network}`);
|
|
10460
|
+
Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
|
|
10461
|
+
return encode_address({
|
|
10462
|
+
data: bytes,
|
|
10463
|
+
format: 'bech32',
|
|
10464
|
+
prefix: config.prefix
|
|
10465
|
+
});
|
|
10513
10466
|
}
|
|
10514
|
-
function
|
|
10515
|
-
|
|
10516
|
-
|
|
10517
|
-
|
|
10518
|
-
while (big > _0n) {
|
|
10519
|
-
const mod = big % _26n;
|
|
10520
|
-
if (mod === _0n) {
|
|
10521
|
-
result = 'Z' + result;
|
|
10522
|
-
big = big / _26n - _1n;
|
|
10523
|
-
}
|
|
10524
|
-
else {
|
|
10525
|
-
const charCode = Number(mod) + 'A'.charCodeAt(0) - 1;
|
|
10526
|
-
result = String.fromCharCode(charCode) + result;
|
|
10527
|
-
big = big / _26n;
|
|
10528
|
-
}
|
|
10529
|
-
}
|
|
10530
|
-
return result;
|
|
10467
|
+
function decode_p2wpkh_address(address) {
|
|
10468
|
+
const parsed = parse_address(address);
|
|
10469
|
+
Assert.ok(parsed.type === 'p2w-pkh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$2}`);
|
|
10470
|
+
return parsed;
|
|
10531
10471
|
}
|
|
10532
10472
|
|
|
10533
|
-
|
|
10473
|
+
const ADDR_TYPE$1 = 'p2w-sh';
|
|
10474
|
+
var P2WSH;
|
|
10475
|
+
(function (P2WSH) {
|
|
10476
|
+
P2WSH.create = create_p2wsh_address;
|
|
10477
|
+
P2WSH.encode = encode_p2wsh_address;
|
|
10478
|
+
P2WSH.decode = decode_p2wsh_address;
|
|
10479
|
+
})(P2WSH || (P2WSH = {}));
|
|
10480
|
+
function create_p2wsh_address(script, network = 'main') {
|
|
10481
|
+
const bytes = Buff.bytes(script);
|
|
10482
|
+
const hash = sha256(bytes);
|
|
10483
|
+
return encode_p2wsh_address(hash, network);
|
|
10484
|
+
}
|
|
10485
|
+
function encode_p2wsh_address(script_hash, network = 'main') {
|
|
10486
|
+
const bytes = Buff.bytes(script_hash);
|
|
10487
|
+
const config = get_address_config(network, ADDR_TYPE$1);
|
|
10488
|
+
Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$1} on ${network}`);
|
|
10489
|
+
Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
|
|
10490
|
+
return encode_address({
|
|
10491
|
+
data: bytes,
|
|
10492
|
+
format: 'bech32',
|
|
10493
|
+
prefix: config.prefix
|
|
10494
|
+
});
|
|
10495
|
+
}
|
|
10496
|
+
function decode_p2wsh_address(address) {
|
|
10497
|
+
const parsed = parse_address(address);
|
|
10498
|
+
Assert.ok(parsed.type === 'p2w-sh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$1}`);
|
|
10499
|
+
return parsed;
|
|
10500
|
+
}
|
|
10501
|
+
|
|
10502
|
+
const ADDR_TYPE = 'p2tr';
|
|
10503
|
+
var P2TR;
|
|
10504
|
+
(function (P2TR) {
|
|
10505
|
+
P2TR.encode = encode_p2tr_address;
|
|
10506
|
+
P2TR.decode = decode_p2tr_address;
|
|
10507
|
+
})(P2TR || (P2TR = {}));
|
|
10508
|
+
function encode_p2tr_address(pubkey, network = 'main') {
|
|
10509
|
+
const bytes = Buff.bytes(pubkey);
|
|
10510
|
+
const config = get_address_config(network, ADDR_TYPE);
|
|
10511
|
+
Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}`);
|
|
10512
|
+
Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
|
|
10513
|
+
return encode_address({
|
|
10514
|
+
data: bytes,
|
|
10515
|
+
format: 'bech32m',
|
|
10516
|
+
prefix: config.prefix
|
|
10517
|
+
});
|
|
10518
|
+
}
|
|
10519
|
+
function decode_p2tr_address(address) {
|
|
10520
|
+
const parsed = parse_address(address);
|
|
10521
|
+
Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`);
|
|
10522
|
+
return parsed;
|
|
10523
|
+
}
|
|
10524
|
+
|
|
10525
|
+
var AddressTool;
|
|
10526
|
+
(function (AddressTool) {
|
|
10527
|
+
AddressTool.P2PKH = P2PKH;
|
|
10528
|
+
AddressTool.P2SH = P2SH;
|
|
10529
|
+
AddressTool.P2WPKH = P2WPKH;
|
|
10530
|
+
AddressTool.P2WSH = P2WSH;
|
|
10531
|
+
AddressTool.P2TR = P2TR;
|
|
10532
|
+
AddressTool.parse = parse_address;
|
|
10533
|
+
})(AddressTool || (AddressTool = {}));
|
|
10534
|
+
|
|
10535
|
+
var index$4 = /*#__PURE__*/Object.freeze({
|
|
10534
10536
|
__proto__: null,
|
|
10535
|
-
get
|
|
10536
|
-
get
|
|
10537
|
-
|
|
10538
|
-
|
|
10537
|
+
get AddressTool () { return AddressTool; },
|
|
10538
|
+
get P2PKH () { return P2PKH; },
|
|
10539
|
+
get P2SH () { return P2SH; },
|
|
10540
|
+
get P2TR () { return P2TR; },
|
|
10541
|
+
get P2WPKH () { return P2WPKH; },
|
|
10542
|
+
get P2WSH () { return P2WSH; },
|
|
10543
|
+
parse_address: parse_address
|
|
10539
10544
|
});
|
|
10540
10545
|
|
|
10541
10546
|
/**
|
|
@@ -14420,7 +14425,7 @@ function assert_psbt_is_funded(psbt) {
|
|
|
14420
14425
|
Assert.ok(vin_amt >= out_amt, `value in (${vin_amt}) < value out (${out_amt})`);
|
|
14421
14426
|
}
|
|
14422
14427
|
|
|
14423
|
-
var index$
|
|
14428
|
+
var index$3 = /*#__PURE__*/Object.freeze({
|
|
14424
14429
|
__proto__: null,
|
|
14425
14430
|
assert_psbt_is_funded: assert_psbt_is_funded,
|
|
14426
14431
|
collect_prevouts: collect_prevouts,
|
|
@@ -14434,7 +14439,7 @@ var index$2 = /*#__PURE__*/Object.freeze({
|
|
|
14434
14439
|
parse_psbt: parse_psbt
|
|
14435
14440
|
});
|
|
14436
14441
|
|
|
14437
|
-
var index$
|
|
14442
|
+
var index$2 = /*#__PURE__*/Object.freeze({
|
|
14438
14443
|
__proto__: null,
|
|
14439
14444
|
format_sigflag: format_sigflag,
|
|
14440
14445
|
hash_amounts: hash_amounts,
|
|
@@ -14445,9 +14450,24 @@ var index$1 = /*#__PURE__*/Object.freeze({
|
|
|
14445
14450
|
hash_segwit_tx: hash_segwit_tx,
|
|
14446
14451
|
hash_sequence: hash_sequence,
|
|
14447
14452
|
hash_taproot_tx: hash_taproot_tx,
|
|
14448
|
-
parse_txinput: parse_txinput
|
|
14453
|
+
parse_txinput: parse_txinput
|
|
14454
|
+
});
|
|
14455
|
+
|
|
14456
|
+
function verify_segwit_tx(_txdata, _config = {}) {
|
|
14457
|
+
console.warn('verify_segwit_tx is not implemented');
|
|
14458
|
+
return true;
|
|
14459
|
+
}
|
|
14460
|
+
function verify_taproot_tx(_txdata, _config = {}) {
|
|
14461
|
+
console.warn('verify_taproot_tx is not implemented');
|
|
14462
|
+
return true;
|
|
14463
|
+
}
|
|
14464
|
+
|
|
14465
|
+
var index$1 = /*#__PURE__*/Object.freeze({
|
|
14466
|
+
__proto__: null,
|
|
14449
14467
|
sign_segwit_tx: sign_segwit_tx,
|
|
14450
|
-
sign_taproot_tx: sign_taproot_tx
|
|
14468
|
+
sign_taproot_tx: sign_taproot_tx,
|
|
14469
|
+
verify_segwit_tx: verify_segwit_tx,
|
|
14470
|
+
verify_taproot_tx: verify_taproot_tx
|
|
14451
14471
|
});
|
|
14452
14472
|
|
|
14453
14473
|
function get_merkle_root(leaves) {
|
|
@@ -14608,18 +14628,20 @@ var index = /*#__PURE__*/Object.freeze({
|
|
|
14608
14628
|
verify_taproot: verify_taproot
|
|
14609
14629
|
});
|
|
14610
14630
|
|
|
14611
|
-
exports.
|
|
14631
|
+
exports.ADDRESS = index$4;
|
|
14612
14632
|
exports.CONST = _const;
|
|
14613
|
-
exports.
|
|
14614
|
-
exports.PSBT = index$
|
|
14615
|
-
exports.SCHEMA = index$
|
|
14616
|
-
exports.
|
|
14617
|
-
exports.
|
|
14618
|
-
exports.
|
|
14633
|
+
exports.META = index$6;
|
|
14634
|
+
exports.PSBT = index$3;
|
|
14635
|
+
exports.SCHEMA = index$9;
|
|
14636
|
+
exports.SCRIPT = index$8;
|
|
14637
|
+
exports.SIGHASH = index$2;
|
|
14638
|
+
exports.SIGNER = index$1;
|
|
14639
|
+
exports.TAPROOT = index;
|
|
14640
|
+
exports.TX = index$7;
|
|
14619
14641
|
exports.Transaction = Transaction$1;
|
|
14620
14642
|
exports.TransactionInput = TransactionInput;
|
|
14621
14643
|
exports.TransactionOutput = TransactionOutput;
|
|
14622
14644
|
exports.TransactionWitness = TransactionWitness;
|
|
14623
|
-
exports.Tx = index$5;
|
|
14624
14645
|
exports.TxSigner = TxSigner;
|
|
14646
|
+
exports.WITNESS = index$5;
|
|
14625
14647
|
//# sourceMappingURL=main.cjs.map
|