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