@vbyte/btc-dev 1.1.3 → 1.1.5

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.
Files changed (66) hide show
  1. package/dist/const.d.ts +16 -0
  2. package/dist/const.js +22 -6
  3. package/dist/lib/address/api.d.ts +4 -0
  4. package/dist/lib/address/api.js +32 -0
  5. package/dist/lib/address/encode.d.ts +3 -3
  6. package/dist/lib/address/index.d.ts +1 -15
  7. package/dist/lib/address/index.js +1 -16
  8. package/dist/lib/address/p2pkh.d.ts +14 -7
  9. package/dist/lib/address/p2pkh.js +34 -16
  10. package/dist/lib/address/p2sh.d.ts +14 -7
  11. package/dist/lib/address/p2sh.js +33 -14
  12. package/dist/lib/address/p2tr.d.ts +14 -5
  13. package/dist/lib/address/p2tr.js +34 -11
  14. package/dist/lib/address/p2wpkh.d.ts +14 -7
  15. package/dist/lib/address/p2wpkh.js +32 -15
  16. package/dist/lib/address/p2wsh.d.ts +14 -7
  17. package/dist/lib/address/p2wsh.js +31 -14
  18. package/dist/lib/address/script.d.ts +2 -5
  19. package/dist/lib/address/script.js +12 -12
  20. package/dist/lib/address/util.d.ts +3 -4
  21. package/dist/lib/address/util.js +10 -14
  22. package/dist/lib/script/decode.d.ts +2 -0
  23. package/dist/lib/script/decode.js +8 -1
  24. package/dist/lib/script/index.d.ts +3 -116
  25. package/dist/lib/script/index.js +3 -6
  26. package/dist/lib/script/lock.d.ts +12 -0
  27. package/dist/lib/script/lock.js +52 -0
  28. package/dist/lib/tx/util.d.ts +1 -6
  29. package/dist/lib/tx/util.js +1 -27
  30. package/dist/lib/witness/parse.js +11 -9
  31. package/dist/main.cjs +345 -194
  32. package/dist/main.cjs.map +1 -1
  33. package/dist/module.mjs +345 -194
  34. package/dist/module.mjs.map +1 -1
  35. package/dist/package.json +1 -1
  36. package/dist/script.js +8 -8
  37. package/dist/script.js.map +1 -1
  38. package/dist/types/address.d.ts +6 -10
  39. package/dist/types/index.d.ts +1 -0
  40. package/dist/types/index.js +1 -0
  41. package/dist/types/meta.d.ts +0 -14
  42. package/dist/types/script.d.ts +15 -0
  43. package/dist/types/script.js +1 -0
  44. package/dist/types/witness.d.ts +7 -8
  45. package/package.json +1 -1
  46. package/src/const.ts +25 -7
  47. package/src/lib/address/api.ts +50 -0
  48. package/src/lib/address/encode.ts +9 -9
  49. package/src/lib/address/index.ts +1 -18
  50. package/src/lib/address/p2pkh.ts +54 -25
  51. package/src/lib/address/p2sh.ts +55 -24
  52. package/src/lib/address/p2tr.ts +59 -19
  53. package/src/lib/address/p2wpkh.ts +53 -26
  54. package/src/lib/address/p2wsh.ts +53 -26
  55. package/src/lib/address/script.ts +14 -14
  56. package/src/lib/address/util.ts +16 -31
  57. package/src/lib/script/decode.ts +11 -1
  58. package/src/lib/script/index.ts +7 -13
  59. package/src/lib/script/lock.ts +73 -0
  60. package/src/lib/tx/util.ts +3 -41
  61. package/src/lib/witness/parse.ts +17 -13
  62. package/src/types/address.ts +7 -11
  63. package/src/types/index.ts +1 -0
  64. package/src/types/meta.ts +0 -18
  65. package/src/types/script.ts +18 -0
  66. package/src/types/witness.ts +8 -8
package/dist/main.cjs CHANGED
@@ -8244,15 +8244,135 @@ function bech32m_decode(encoded) {
8244
8244
  return { data, format: 'bech32m', prefix, version };
8245
8245
  }
8246
8246
 
8247
+ const COINBASE = {
8248
+ TXID: '00'.repeat(32),
8249
+ VOUT: 0xFFFFFFFF,
8250
+ };
8251
+ const DEFAULT = {
8252
+ LOCKTIME: 0,
8253
+ SEQUENCE: 0xFFFFFFFF,
8254
+ VERSION: 2,
8255
+ };
8256
+ const TAPLEAF_VERSIONS = [
8257
+ 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce,
8258
+ 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
8259
+ 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee,
8260
+ 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
8261
+ 0x66, 0x7e, 0x80, 0x84, 0x96, 0x98, 0xba, 0xbc,
8262
+ 0xbe
8263
+ ];
8264
+ const TAPLEAF_DEFAULT_VERSION = 0xc0;
8265
+ const LOCK_SCRIPT_TYPE = {
8266
+ P2PKH: 'p2pkh',
8267
+ P2SH: 'p2sh',
8268
+ P2WPKH: 'p2wpkh',
8269
+ P2WSH: 'p2wsh',
8270
+ P2TR: 'p2tr',
8271
+ OPRETURN: 'opreturn',
8272
+ };
8273
+ const SPEND_SCRIPT_TYPE = {
8274
+ P2PKH: 'p2pkh',
8275
+ P2SH: 'p2sh',
8276
+ P2WPKH: 'p2wpkh',
8277
+ P2WSH: 'p2wsh',
8278
+ P2TR: 'p2tr',
8279
+ P2TS: 'p2ts',
8280
+ };
8281
+ const LOCK_SCRIPT_REGEX = {
8282
+ [LOCK_SCRIPT_TYPE.P2PKH]: /^76a914[0-9a-f]{40}88ac$/i,
8283
+ [LOCK_SCRIPT_TYPE.P2SH]: /^a914[0-9a-f]{40}87$/i,
8284
+ [LOCK_SCRIPT_TYPE.P2WPKH]: /^0014[0-9a-f]{40}$/i,
8285
+ [LOCK_SCRIPT_TYPE.P2WSH]: /^0020[0-9a-f]{64}$/i,
8286
+ [LOCK_SCRIPT_TYPE.P2TR]: /^5120[0-9a-f]{64}$/i,
8287
+ [LOCK_SCRIPT_TYPE.OPRETURN]: /^6a[0-9a-f]{2,}$/i,
8288
+ };
8289
+ const SCRIPT_INT_KEY = '';
8290
+ const TX_SIZE = {
8291
+ GLOBAL_BASE: 8,
8292
+ GLOBAL_WIT: 10,
8293
+ TXIN_BASE: 32 + 4 + 4,
8294
+ TXOUT_BASE: 8,
8295
+ };
8296
+ const SIGHASH_DEFAULT = 0x01;
8297
+ const SIGHASH_SEGWIT = [0x01, 0x02, 0x03, 0x81, 0x82, 0x83];
8298
+ const SIGHASH_TAPROOT = [0x00, ...SIGHASH_SEGWIT];
8299
+
8300
+ var _const = /*#__PURE__*/Object.freeze({
8301
+ __proto__: null,
8302
+ COINBASE: COINBASE,
8303
+ DEFAULT: DEFAULT,
8304
+ LOCK_SCRIPT_REGEX: LOCK_SCRIPT_REGEX,
8305
+ LOCK_SCRIPT_TYPE: LOCK_SCRIPT_TYPE,
8306
+ SCRIPT_INT_KEY: SCRIPT_INT_KEY,
8307
+ SIGHASH_DEFAULT: SIGHASH_DEFAULT,
8308
+ SIGHASH_SEGWIT: SIGHASH_SEGWIT,
8309
+ SIGHASH_TAPROOT: SIGHASH_TAPROOT,
8310
+ SPEND_SCRIPT_TYPE: SPEND_SCRIPT_TYPE,
8311
+ TAPLEAF_DEFAULT_VERSION: TAPLEAF_DEFAULT_VERSION,
8312
+ TAPLEAF_VERSIONS: TAPLEAF_VERSIONS,
8313
+ TX_SIZE: TX_SIZE
8314
+ });
8315
+
8316
+ function is_return_script(script) {
8317
+ const bytes = Buff.bytes(script);
8318
+ return bytes.at(0) === 0x6a;
8319
+ }
8320
+ function get_lock_script_info(script) {
8321
+ return {
8322
+ type: get_lock_script_type(script),
8323
+ version: get_lock_script_version(script)
8324
+ };
8325
+ }
8326
+ function get_lock_script_type(script) {
8327
+ const hex = Buff.bytes(script).hex;
8328
+ for (const [type, regex] of Object.entries(LOCK_SCRIPT_REGEX)) {
8329
+ if (regex.test(hex))
8330
+ return type;
8331
+ }
8332
+ return null;
8333
+ }
8334
+ function get_lock_script_version(script) {
8335
+ const version = Buff.bytes(script);
8336
+ switch (version.at(0)) {
8337
+ case 0x00: return 0;
8338
+ case 0x51: return 1;
8339
+ default: return null;
8340
+ }
8341
+ }
8342
+ function is_p2pkh_script(script) {
8343
+ const hex = Buff.bytes(script).hex;
8344
+ return LOCK_SCRIPT_REGEX['p2pkh'].test(hex);
8345
+ }
8346
+ function is_p2sh_script(script) {
8347
+ const hex = Buff.bytes(script).hex;
8348
+ return LOCK_SCRIPT_REGEX['p2sh'].test(hex);
8349
+ }
8350
+ function is_p2wpkh_script(script) {
8351
+ const hex = Buff.bytes(script).hex;
8352
+ return LOCK_SCRIPT_REGEX['p2wpkh'].test(hex);
8353
+ }
8354
+ function is_p2wsh_script(script) {
8355
+ const hex = Buff.bytes(script).hex;
8356
+ return LOCK_SCRIPT_REGEX['p2wsh'].test(hex);
8357
+ }
8358
+ function is_p2tr_script(script) {
8359
+ const hex = Buff.bytes(script).hex;
8360
+ return LOCK_SCRIPT_REGEX['p2tr'].test(hex);
8361
+ }
8362
+ function is_opreturn_script(script) {
8363
+ const hex = Buff.bytes(script).hex;
8364
+ return LOCK_SCRIPT_REGEX['opreturn'].test(hex);
8365
+ }
8366
+
8247
8367
  function get_address_script(script_key, script_type) {
8248
8368
  switch (script_type) {
8249
8369
  case 'p2pkh':
8250
8370
  return get_p2pkh_script(script_key);
8251
8371
  case 'p2sh':
8252
8372
  return get_p2sh_script(script_key);
8253
- case 'p2w-pkh':
8373
+ case 'p2wpkh':
8254
8374
  return get_p2w_pkh_script(script_key);
8255
- case 'p2w-sh':
8375
+ case 'p2wsh':
8256
8376
  return get_p2w_sh_script(script_key);
8257
8377
  case 'p2tr':
8258
8378
  return get_p2tr_script(script_key);
@@ -8262,32 +8382,32 @@ function get_address_script(script_key, script_type) {
8262
8382
  }
8263
8383
  function get_p2pkh_script(script_key) {
8264
8384
  return {
8265
- script_hex: '76a914' + script_key + '88ac',
8266
- script_asm: ['OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG']
8385
+ hex: '76a914' + script_key + '88ac',
8386
+ asm: ['OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG']
8267
8387
  };
8268
8388
  }
8269
8389
  function get_p2sh_script(script_key) {
8270
8390
  return {
8271
- script_hex: 'a914' + script_key + '87',
8272
- script_asm: ['OP_HASH160', script_key, 'OP_EQUAL']
8391
+ hex: 'a914' + script_key + '87',
8392
+ asm: ['OP_HASH160', script_key, 'OP_EQUAL']
8273
8393
  };
8274
8394
  }
8275
8395
  function get_p2w_pkh_script(script_key) {
8276
8396
  return {
8277
- script_hex: '0014' + script_key,
8278
- script_asm: ['OP_0', script_key]
8397
+ hex: '0014' + script_key,
8398
+ asm: ['OP_0', script_key]
8279
8399
  };
8280
8400
  }
8281
8401
  function get_p2w_sh_script(script_key) {
8282
8402
  return {
8283
- script_hex: '0020' + script_key,
8284
- script_asm: ['OP_0', script_key]
8403
+ hex: '0020' + script_key,
8404
+ asm: ['OP_0', script_key]
8285
8405
  };
8286
8406
  }
8287
8407
  function get_p2tr_script(script_key) {
8288
8408
  return {
8289
- script_hex: '5120' + script_key,
8290
- script_asm: ['OP_1', script_key]
8409
+ hex: '5120' + script_key,
8410
+ asm: ['OP_1', script_key]
8291
8411
  };
8292
8412
  }
8293
8413
 
@@ -8300,12 +8420,12 @@ const CONFIG_TABLE = [
8300
8420
  ['m', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
8301
8421
  ['n', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
8302
8422
  ['2', 'p2sh', 'regtest', 20, 'base58', 0xC4],
8303
- ['bc', 'p2w-pkh', 'main', 20, 'bech32', 0],
8304
- ['tb', 'p2w-pkh', 'testnet', 20, 'bech32', 0],
8305
- ['bcrt', 'p2w-pkh', 'regtest', 20, 'bech32', 0],
8306
- ['bc', 'p2w-sh', 'main', 32, 'bech32', 0],
8307
- ['tb', 'p2w-sh', 'testnet', 32, 'bech32', 0],
8308
- ['bcrt', 'p2w-sh', 'regtest', 32, 'bech32', 0],
8423
+ ['bc', 'p2wpkh', 'main', 20, 'bech32', 0],
8424
+ ['tb', 'p2wpkh', 'testnet', 20, 'bech32', 0],
8425
+ ['bcrt', 'p2wpkh', 'regtest', 20, 'bech32', 0],
8426
+ ['bc', 'p2wsh', 'main', 32, 'bech32', 0],
8427
+ ['tb', 'p2wsh', 'testnet', 32, 'bech32', 0],
8428
+ ['bcrt', 'p2wsh', 'regtest', 32, 'bech32', 0],
8309
8429
  ['bc', 'p2tr', 'main', 32, 'bech32m', 1],
8310
8430
  ['tb', 'p2tr', 'testnet', 32, 'bech32m', 1],
8311
8431
  ['bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1]
@@ -8318,7 +8438,7 @@ function get_address_config(address_network, address_type) {
8318
8438
  }
8319
8439
  return null;
8320
8440
  }
8321
- function get_address_ctx(address) {
8441
+ function get_address_info(address) {
8322
8442
  const dec = decode_address(address);
8323
8443
  for (const [prefix, type, network, size, format, version] of CONFIG_TABLE) {
8324
8444
  if (format !== dec.format)
@@ -8335,175 +8455,269 @@ function get_address_ctx(address) {
8335
8455
  if (!address.startsWith(prefix))
8336
8456
  continue;
8337
8457
  }
8338
- const hex = Buff.uint(dec.data).hex;
8339
- return { data: dec.data, hex, type, prefix, network, size, format, version };
8458
+ const data = Buff.uint(dec.data).hex;
8459
+ const script = get_address_script(data, type);
8460
+ return { data, script, type, prefix, network, size, format, version };
8340
8461
  }
8341
8462
  throw new Error('address configuration is invalid');
8342
8463
  }
8343
- function parse_address(address) {
8344
- const ctx = get_address_ctx(address);
8345
- const script = get_address_script(ctx.hex, ctx.type);
8346
- return { ...ctx, ...script };
8347
- }
8348
8464
 
8349
- const ADDR_TYPE$4 = 'p2pkh';
8465
+ const ADDRESS_TYPE$4 = LOCK_SCRIPT_TYPE.P2PKH;
8350
8466
  var P2PKH;
8351
8467
  (function (P2PKH) {
8352
- P2PKH.create = create_p2pkh_address;
8353
- P2PKH.encode = encode_p2pkh_address;
8354
- P2PKH.decode = decode_p2pkh_address;
8468
+ P2PKH.create_address = create_p2pkh_address;
8469
+ P2PKH.create_script = create_p2pkh_script;
8470
+ P2PKH.encode_address = encode_p2pkh_address;
8471
+ P2PKH.encode_script = encode_p2pkh_script;
8472
+ P2PKH.decode_address = decode_p2pkh_address;
8473
+ P2PKH.decode_script = decode_p2pkh_script;
8355
8474
  })(P2PKH || (P2PKH = {}));
8356
- function create_p2pkh_address(script, network = 'main') {
8357
- const bytes = Buff.bytes(script);
8475
+ function create_p2pkh_address(pubkey, network = 'main') {
8476
+ const script = create_p2pkh_script(pubkey);
8477
+ return encode_p2pkh_address(script, network);
8478
+ }
8479
+ function create_p2pkh_script(pubkey) {
8480
+ const bytes = Buff.bytes(pubkey);
8481
+ Assert.size(bytes, 33, 'invalid pubkey size');
8358
8482
  const hash = hash160(bytes);
8359
- return encode_p2pkh_address(hash, network);
8483
+ return encode_p2pkh_script(hash);
8484
+ }
8485
+ function encode_p2pkh_script(pk_hash) {
8486
+ return Buff.join(['76a914', pk_hash, '88ac']);
8360
8487
  }
8361
- function encode_p2pkh_address(pk_hash, network = 'main') {
8362
- const bytes = Buff.bytes(pk_hash);
8363
- const config = get_address_config(network, ADDR_TYPE$4);
8364
- Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$4} on ${network}`);
8365
- Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
8488
+ function encode_p2pkh_address(script, network = 'main') {
8489
+ const pk_hash = decode_p2pkh_script(script);
8490
+ const config = get_address_config(network, ADDRESS_TYPE$4);
8491
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE$4} on ${network}`);
8492
+ Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}`);
8366
8493
  return encode_address({
8367
- data: bytes,
8494
+ data: pk_hash,
8368
8495
  format: 'base58',
8369
8496
  version: config.version
8370
8497
  });
8371
8498
  }
8372
8499
  function decode_p2pkh_address(address) {
8373
- const parsed = parse_address(address);
8374
- Assert.ok(parsed.type === 'p2pkh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$4}`);
8500
+ const parsed = get_address_info(address);
8501
+ Assert.ok(parsed.type === 'p2pkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE$4}`);
8375
8502
  return parsed;
8376
8503
  }
8504
+ function decode_p2pkh_script(script) {
8505
+ const bytes = Buff.bytes(script);
8506
+ Assert.ok(is_p2pkh_script(script), `invalid p2pkh script`);
8507
+ return bytes.slice(3, 23);
8508
+ }
8377
8509
 
8378
- const ADDR_TYPE$3 = 'p2sh';
8510
+ const ADDRESS_TYPE$3 = LOCK_SCRIPT_TYPE.P2SH;
8379
8511
  var P2SH;
8380
8512
  (function (P2SH) {
8381
- P2SH.create = create_p2sh_address;
8382
- P2SH.encode = encode_p2sh_address;
8383
- P2SH.decode = decode_p2sh_address;
8513
+ P2SH.create_address = create_p2sh_address;
8514
+ P2SH.create_script = create_p2sh_script;
8515
+ P2SH.encode_address = encode_p2sh_address;
8516
+ P2SH.encode_script = encode_p2sh_script;
8517
+ P2SH.decode_address = decode_p2sh_address;
8518
+ P2SH.decode_script = decode_p2sh_script;
8384
8519
  })(P2SH || (P2SH = {}));
8385
8520
  function create_p2sh_address(script, network = 'main') {
8386
8521
  const bytes = Buff.bytes(script);
8387
8522
  const hash = hash160(bytes);
8388
- return encode_p2sh_address(hash, network);
8523
+ const p2sh_script = encode_p2sh_script(hash);
8524
+ return encode_p2sh_address(p2sh_script, network);
8525
+ }
8526
+ function create_p2sh_script(script) {
8527
+ const bytes = Buff.bytes(script);
8528
+ const hash = hash160(bytes);
8529
+ return encode_p2sh_script(hash);
8530
+ }
8531
+ function encode_p2sh_script(script_hash) {
8532
+ return Buff.join(['a914', script_hash, '87']);
8389
8533
  }
8390
- function encode_p2sh_address(script_hash, network = 'main') {
8391
- const bytes = Buff.bytes(script_hash);
8392
- const config = get_address_config(network, ADDR_TYPE$3);
8393
- Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$3} on ${network}`);
8394
- Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
8534
+ function encode_p2sh_address(script_pk, network = 'main') {
8535
+ const script_hash = decode_p2sh_script(script_pk);
8536
+ const config = get_address_config(network, ADDRESS_TYPE$3);
8537
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE$3} on ${network}`);
8538
+ Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}`);
8395
8539
  return encode_address({
8396
- data: bytes,
8540
+ data: script_hash,
8397
8541
  format: 'base58',
8398
8542
  version: config.version
8399
8543
  });
8400
8544
  }
8401
8545
  function decode_p2sh_address(address) {
8402
- const parsed = parse_address(address);
8403
- Assert.ok(parsed.type === 'p2sh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$3}`);
8546
+ const parsed = get_address_info(address);
8547
+ Assert.ok(parsed.type === 'p2sh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE$3}`);
8404
8548
  return parsed;
8405
8549
  }
8550
+ function decode_p2sh_script(script) {
8551
+ Assert.ok(is_p2sh_script(script), `invalid p2sh script`);
8552
+ const bytes = Buff.bytes(script);
8553
+ return bytes.slice(2, 22);
8554
+ }
8406
8555
 
8407
- const ADDR_TYPE$2 = 'p2w-pkh';
8556
+ const ADDRESS_TYPE$2 = LOCK_SCRIPT_TYPE.P2WPKH;
8408
8557
  var P2WPKH;
8409
8558
  (function (P2WPKH) {
8410
- P2WPKH.create = create_p2wpkh_address;
8411
- P2WPKH.encode = encode_p2wpkh_address;
8412
- P2WPKH.decode = decode_p2wpkh_address;
8559
+ P2WPKH.create_address = create_p2wpkh_address;
8560
+ P2WPKH.create_script = create_p2wpkh_script;
8561
+ P2WPKH.encode_address = encode_p2wpkh_address;
8562
+ P2WPKH.encode_script = encode_p2wpkh_script;
8563
+ P2WPKH.decode_address = decode_p2wpkh_address;
8564
+ P2WPKH.decode_script = decode_p2wpkh_script;
8413
8565
  })(P2WPKH || (P2WPKH = {}));
8414
8566
  function create_p2wpkh_address(pubkey, network = 'main') {
8567
+ const script = create_p2wpkh_script(pubkey);
8568
+ return encode_p2wpkh_address(script, network);
8569
+ }
8570
+ function create_p2wpkh_script(pubkey) {
8415
8571
  const bytes = Buff.bytes(pubkey);
8416
- Assert.size(bytes, 33, `invalid payload size: ${bytes.length} !== 33`);
8572
+ Assert.size(bytes, 33, 'invalid pubkey size');
8417
8573
  const hash = hash160(bytes);
8418
- return encode_p2wpkh_address(hash, network);
8574
+ return encode_p2wpkh_script(hash);
8419
8575
  }
8420
- function encode_p2wpkh_address(pk_hash, network = 'main') {
8421
- const bytes = Buff.bytes(pk_hash);
8422
- const config = get_address_config(network, ADDR_TYPE$2);
8423
- Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$2} on ${network}`);
8424
- Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
8576
+ function encode_p2wpkh_script(pk_hash) {
8577
+ return Buff.join(['0014', pk_hash]);
8578
+ }
8579
+ function encode_p2wpkh_address(script_pk, network = 'main') {
8580
+ const pk_hash = decode_p2wpkh_script(script_pk);
8581
+ const config = get_address_config(network, ADDRESS_TYPE$2);
8582
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE$2} on ${network}`);
8583
+ Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}`);
8425
8584
  return encode_address({
8426
- data: bytes,
8585
+ data: pk_hash,
8427
8586
  format: 'bech32',
8428
8587
  prefix: config.prefix
8429
8588
  });
8430
8589
  }
8431
8590
  function decode_p2wpkh_address(address) {
8432
- const parsed = parse_address(address);
8433
- Assert.ok(parsed.type === 'p2w-pkh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$2}`);
8591
+ const parsed = get_address_info(address);
8592
+ Assert.ok(parsed.type === 'p2wpkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE$2}`);
8434
8593
  return parsed;
8435
8594
  }
8595
+ function decode_p2wpkh_script(script) {
8596
+ Assert.ok(is_p2wpkh_script(script), `invalid p2wpkh script`);
8597
+ const bytes = Buff.bytes(script);
8598
+ return bytes.slice(2, 22);
8599
+ }
8436
8600
 
8437
- const ADDR_TYPE$1 = 'p2w-sh';
8601
+ const ADDRESS_TYPE$1 = LOCK_SCRIPT_TYPE.P2WSH;
8438
8602
  var P2WSH;
8439
8603
  (function (P2WSH) {
8440
- P2WSH.create = create_p2wsh_address;
8441
- P2WSH.encode = encode_p2wsh_address;
8442
- P2WSH.decode = decode_p2wsh_address;
8604
+ P2WSH.create_address = create_p2wsh_address;
8605
+ P2WSH.create_script = create_p2wsh_script;
8606
+ P2WSH.encode_address = encode_p2wsh_address;
8607
+ P2WSH.encode_script = encode_p2wsh_script;
8608
+ P2WSH.decode_address = decode_p2wsh_address;
8609
+ P2WSH.decode_script = decode_p2wsh_script;
8443
8610
  })(P2WSH || (P2WSH = {}));
8444
8611
  function create_p2wsh_address(script, network = 'main') {
8612
+ const wsh_script = create_p2wsh_script(script);
8613
+ return encode_p2wsh_address(wsh_script, network);
8614
+ }
8615
+ function create_p2wsh_script(script) {
8445
8616
  const bytes = Buff.bytes(script);
8446
8617
  const hash = sha256(bytes);
8447
- return encode_p2wsh_address(hash, network);
8618
+ return encode_p2wsh_script(hash);
8619
+ }
8620
+ function encode_p2wsh_script(script_hash) {
8621
+ return Buff.join(['0020', script_hash]);
8448
8622
  }
8449
- function encode_p2wsh_address(script_hash, network = 'main') {
8450
- const bytes = Buff.bytes(script_hash);
8451
- const config = get_address_config(network, ADDR_TYPE$1);
8452
- Assert.exists(config, `unrecognized address config: ${ADDR_TYPE$1} on ${network}`);
8453
- Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
8623
+ function encode_p2wsh_address(script_pk, network = 'main') {
8624
+ const script_hash = decode_p2wsh_script(script_pk);
8625
+ const config = get_address_config(network, ADDRESS_TYPE$1);
8626
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE$1} on ${network}`);
8627
+ Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}`);
8454
8628
  return encode_address({
8455
- data: bytes,
8629
+ data: script_hash,
8456
8630
  format: 'bech32',
8457
8631
  prefix: config.prefix
8458
8632
  });
8459
8633
  }
8460
8634
  function decode_p2wsh_address(address) {
8461
- const parsed = parse_address(address);
8462
- Assert.ok(parsed.type === 'p2w-sh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE$1}`);
8635
+ const parsed = get_address_info(address);
8636
+ Assert.ok(parsed.type === 'p2wsh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE$1}`);
8463
8637
  return parsed;
8464
8638
  }
8639
+ function decode_p2wsh_script(script) {
8640
+ Assert.ok(is_p2wsh_script(script), `invalid p2wsh script`);
8641
+ const bytes = Buff.bytes(script);
8642
+ return bytes.slice(2, 34);
8643
+ }
8465
8644
 
8466
- const ADDR_TYPE = 'p2tr';
8645
+ const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2TR;
8467
8646
  var P2TR;
8468
8647
  (function (P2TR) {
8469
- P2TR.encode = encode_p2tr_address;
8470
- P2TR.decode = decode_p2tr_address;
8648
+ P2TR.create_address = create_p2tr_address;
8649
+ P2TR.create_script = create_p2tr_script;
8650
+ P2TR.encode_address = encode_p2tr_address;
8651
+ P2TR.encode_script = encode_p2tr_script;
8652
+ P2TR.decode_address = decode_p2tr_address;
8653
+ P2TR.decode_script = decode_p2tr_script;
8471
8654
  })(P2TR || (P2TR = {}));
8472
- function encode_p2tr_address(pubkey, network = 'main') {
8655
+ function create_p2tr_address(pubkey, network = 'main') {
8656
+ const script = create_p2tr_script(pubkey);
8657
+ return encode_p2tr_address(script, network);
8658
+ }
8659
+ function create_p2tr_script(pubkey) {
8473
8660
  const bytes = Buff.bytes(pubkey);
8474
- const config = get_address_config(network, ADDR_TYPE);
8475
- Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}`);
8476
- Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
8661
+ Assert.size(bytes, 32, 'invalid pubkey size');
8662
+ return encode_p2tr_script(bytes);
8663
+ }
8664
+ function encode_p2tr_script(pubkey) {
8665
+ return Buff.join(['5120', pubkey]);
8666
+ }
8667
+ function encode_p2tr_address(script_pk, network = 'main') {
8668
+ const pubkey = decode_p2tr_script(script_pk);
8669
+ const config = get_address_config(network, ADDRESS_TYPE);
8670
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
8671
+ Assert.size(pubkey, config.size, `invalid payload size: ${pubkey.length} !== ${config.size}`);
8477
8672
  return encode_address({
8478
- data: bytes,
8673
+ data: pubkey,
8479
8674
  format: 'bech32m',
8480
8675
  prefix: config.prefix
8481
8676
  });
8482
8677
  }
8483
8678
  function decode_p2tr_address(address) {
8484
- const parsed = parse_address(address);
8485
- Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`);
8679
+ const parsed = get_address_info(address);
8680
+ Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`);
8486
8681
  return parsed;
8487
8682
  }
8683
+ function decode_p2tr_script(script) {
8684
+ Assert.ok(is_p2tr_script(script), `invalid p2tr script`);
8685
+ const bytes = Buff.bytes(script);
8686
+ return bytes.slice(2, 34);
8687
+ }
8488
8688
 
8489
- var AddressTool;
8490
- (function (AddressTool) {
8491
- AddressTool.P2PKH = P2PKH;
8492
- AddressTool.P2SH = P2SH;
8493
- AddressTool.P2WPKH = P2WPKH;
8494
- AddressTool.P2WSH = P2WSH;
8495
- AddressTool.P2TR = P2TR;
8496
- AddressTool.parse = parse_address;
8497
- })(AddressTool || (AddressTool = {}));
8689
+ function create_address(script, network = 'main') {
8690
+ const bytes = Buff.bytes(script);
8691
+ const type = get_lock_script_type(bytes);
8692
+ if (type === null)
8693
+ throw new Error('unrecognized script type: ' + bytes.hex);
8694
+ switch (type) {
8695
+ case LOCK_SCRIPT_TYPE.P2PKH:
8696
+ return P2PKH.create_address(script, network);
8697
+ case LOCK_SCRIPT_TYPE.P2SH:
8698
+ return P2SH.create_address(script, network);
8699
+ case LOCK_SCRIPT_TYPE.P2WPKH:
8700
+ return P2WPKH.create_address(script, network);
8701
+ case LOCK_SCRIPT_TYPE.P2WSH:
8702
+ return P2WSH.create_address(script, network);
8703
+ case LOCK_SCRIPT_TYPE.P2TR:
8704
+ return P2TR.create_address(script, network);
8705
+ default:
8706
+ throw new Error('unrecognized script type: ' + type);
8707
+ }
8708
+ }
8709
+ function parse_address(address) {
8710
+ return get_address_info(address);
8711
+ }
8498
8712
 
8499
8713
  var index$8 = /*#__PURE__*/Object.freeze({
8500
8714
  __proto__: null,
8501
- get AddressTool () { return AddressTool; },
8502
8715
  get P2PKH () { return P2PKH; },
8503
8716
  get P2SH () { return P2SH; },
8504
8717
  get P2TR () { return P2TR; },
8505
8718
  get P2WPKH () { return P2WPKH; },
8506
8719
  get P2WSH () { return P2WSH; },
8720
+ create_address: create_address,
8507
8721
  parse_address: parse_address
8508
8722
  });
8509
8723
 
@@ -8858,6 +9072,13 @@ function get_size_varint(size) {
8858
9072
  }
8859
9073
  }
8860
9074
 
9075
+ function parse_script(script) {
9076
+ const bytes = Buff.bytes(script);
9077
+ return {
9078
+ asm: decode_script(bytes),
9079
+ hex: bytes.hex
9080
+ };
9081
+ }
8861
9082
  function decode_script(script) {
8862
9083
  const stream = new Stream(script);
8863
9084
  const stack = [];
@@ -9196,12 +9417,10 @@ function parse_script_pubkeys(script) {
9196
9417
 
9197
9418
  var ScriptUtil;
9198
9419
  (function (ScriptUtil) {
9199
- ScriptUtil.prefix_size = prefix_script_size;
9420
+ ScriptUtil.parse = parse_script;
9200
9421
  ScriptUtil.decode = decode_script;
9201
9422
  ScriptUtil.encode = encode_script;
9202
9423
  ScriptUtil.is_valid = is_valid_script;
9203
- ScriptUtil.get_pubkeys = parse_script_pubkeys;
9204
- ScriptUtil.OPCODES = OPCODE_MAP;
9205
9424
  })(ScriptUtil || (ScriptUtil = {}));
9206
9425
 
9207
9426
  var index$6 = /*#__PURE__*/Object.freeze({
@@ -9212,11 +9431,22 @@ var index$6 = /*#__PURE__*/Object.freeze({
9212
9431
  encode_script: encode_script,
9213
9432
  encode_script_word: encode_script_word,
9214
9433
  get_asm_code: get_asm_code,
9434
+ get_lock_script_info: get_lock_script_info,
9435
+ get_lock_script_type: get_lock_script_type,
9436
+ get_lock_script_version: get_lock_script_version,
9215
9437
  get_op_code: get_op_code,
9216
9438
  get_op_type: get_op_type,
9217
9439
  get_size_varint: get_size_varint,
9440
+ is_opreturn_script: is_opreturn_script,
9441
+ is_p2pkh_script: is_p2pkh_script,
9442
+ is_p2sh_script: is_p2sh_script,
9443
+ is_p2tr_script: is_p2tr_script,
9444
+ is_p2wpkh_script: is_p2wpkh_script,
9445
+ is_p2wsh_script: is_p2wsh_script,
9446
+ is_return_script: is_return_script,
9218
9447
  is_valid_op: is_valid_op,
9219
9448
  is_valid_script: is_valid_script,
9449
+ parse_script: parse_script,
9220
9450
  parse_script_pubkeys: parse_script_pubkeys,
9221
9451
  prefix_script_size: prefix_script_size,
9222
9452
  prefix_word_size: prefix_word_size,
@@ -9251,57 +9481,6 @@ function get_annex_data(witness) {
9251
9481
  return undefined;
9252
9482
  }
9253
9483
 
9254
- const COINBASE = {
9255
- TXID: '00'.repeat(32),
9256
- VOUT: 0xFFFFFFFF,
9257
- };
9258
- const DEFAULT = {
9259
- LOCKTIME: 0,
9260
- SEQUENCE: 0xFFFFFFFF,
9261
- VERSION: 2,
9262
- };
9263
- const TAPLEAF_VERSIONS = [
9264
- 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce,
9265
- 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
9266
- 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee,
9267
- 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
9268
- 0x66, 0x7e, 0x80, 0x84, 0x96, 0x98, 0xba, 0xbc,
9269
- 0xbe
9270
- ];
9271
- const TAPLEAF_DEFAULT_VERSION = 0xc0;
9272
- const LOCK_SCRIPT_REGEX = {
9273
- 'p2pkh': /^76a914[0-9a-f]{40}88ac$/i,
9274
- 'p2sh': /^a914[0-9a-f]{40}87$/i,
9275
- 'p2wpkh': /^0014[0-9a-f]{40}$/i,
9276
- 'p2wsh': /^0020[0-9a-f]{64}$/i,
9277
- 'p2tr': /^5120[0-9a-f]{64}$/i,
9278
- 'opreturn': /^6a[0-9a-f]{2,}$/i,
9279
- };
9280
- const SCRIPT_INT_KEY = '';
9281
- const TX_SIZE = {
9282
- GLOBAL_BASE: 8,
9283
- GLOBAL_WIT: 10,
9284
- TXIN_BASE: 32 + 4 + 4,
9285
- TXOUT_BASE: 8,
9286
- };
9287
- const SIGHASH_DEFAULT = 0x01;
9288
- const SIGHASH_SEGWIT = [0x01, 0x02, 0x03, 0x81, 0x82, 0x83];
9289
- const SIGHASH_TAPROOT = [0x00, ...SIGHASH_SEGWIT];
9290
-
9291
- var _const = /*#__PURE__*/Object.freeze({
9292
- __proto__: null,
9293
- COINBASE: COINBASE,
9294
- DEFAULT: DEFAULT,
9295
- LOCK_SCRIPT_REGEX: LOCK_SCRIPT_REGEX,
9296
- SCRIPT_INT_KEY: SCRIPT_INT_KEY,
9297
- SIGHASH_DEFAULT: SIGHASH_DEFAULT,
9298
- SIGHASH_SEGWIT: SIGHASH_SEGWIT,
9299
- SIGHASH_TAPROOT: SIGHASH_TAPROOT,
9300
- TAPLEAF_DEFAULT_VERSION: TAPLEAF_DEFAULT_VERSION,
9301
- TAPLEAF_VERSIONS: TAPLEAF_VERSIONS,
9302
- TX_SIZE: TX_SIZE
9303
- });
9304
-
9305
9484
  const taptree = union([array(byte32), byte32]);
9306
9485
  const config = object({
9307
9486
  pubkey: byte32,
@@ -9624,32 +9803,6 @@ function serialize_tx(txdata) {
9624
9803
  return { version, locktime, vin, vout };
9625
9804
  }
9626
9805
 
9627
- function is_return_script(script) {
9628
- const bytes = Buff.bytes(script);
9629
- return bytes.at(0) === 0x6a;
9630
- }
9631
- function get_vout_script_info(script) {
9632
- return {
9633
- type: get_vout_script_type(script),
9634
- version: get_vout_script_version(script)
9635
- };
9636
- }
9637
- function get_vout_script_type(script) {
9638
- const hex = Buff.bytes(script).hex;
9639
- for (const [type, regex] of Object.entries(LOCK_SCRIPT_REGEX)) {
9640
- if (regex.test(hex))
9641
- return type;
9642
- }
9643
- return null;
9644
- }
9645
- function get_vout_script_version(script) {
9646
- const version = Buff.bytes(script);
9647
- switch (version.at(0)) {
9648
- case 0x00: return 0;
9649
- case 0x51: return 1;
9650
- default: return null;
9651
- }
9652
- }
9653
9806
  function get_txid(txdata) {
9654
9807
  if (typeof txdata === 'object') {
9655
9808
  assert_tx_template(txdata);
@@ -9825,12 +9978,8 @@ var index$4 = /*#__PURE__*/Object.freeze({
9825
9978
  get_txout_size: get_txout_size,
9826
9979
  get_txsize: get_txsize,
9827
9980
  get_vin_size: get_vin_size,
9828
- get_vout_script_info: get_vout_script_info,
9829
- get_vout_script_type: get_vout_script_type,
9830
- get_vout_script_version: get_vout_script_version,
9831
9981
  get_vout_size: get_vout_size,
9832
9982
  get_vsize: get_vsize,
9833
- is_return_script: is_return_script,
9834
9983
  normalize_prevout: normalize_prevout,
9835
9984
  normalize_sequence: normalize_sequence,
9836
9985
  normalize_value: normalize_value,
@@ -10173,9 +10322,9 @@ function parse_cblock_data(data) {
10173
10322
  function parse_witness_script(elems, type) {
10174
10323
  let script;
10175
10324
  switch (type) {
10176
- case 'p2tr-ts':
10325
+ case 'p2ts':
10177
10326
  script = elems.at(-1);
10178
- case 'p2w-sh':
10327
+ case 'p2wsh':
10179
10328
  script = elems.at(-1);
10180
10329
  }
10181
10330
  return (script !== undefined) ? new Buff(script).hex : null;
@@ -10183,34 +10332,36 @@ function parse_witness_script(elems, type) {
10183
10332
  function parse_witness_type(elems, cblock) {
10184
10333
  let param_0 = elems.at(0), param_1 = elems.at(1), param_x = elems.at(-1);
10185
10334
  if (cblock !== null && param_x !== undefined) {
10186
- return 'p2tr-ts';
10335
+ return 'p2ts';
10187
10336
  }
10188
10337
  else if (elems.length === 2 &&
10189
10338
  param_0 !== undefined &&
10190
10339
  param_1 !== undefined &&
10191
10340
  param_0.length >= 64 &&
10192
10341
  param_1.length === 33) {
10193
- return 'p2w-pkh';
10342
+ return 'p2wpkh';
10194
10343
  }
10195
10344
  else if (elems.length === 1 &&
10196
10345
  param_0 !== undefined &&
10197
10346
  param_0.length === 64) {
10198
- return 'p2tr-pk';
10347
+ return 'p2tr';
10199
10348
  }
10200
10349
  else if (elems.length > 1 &&
10201
10350
  param_x !== undefined &&
10202
10351
  is_valid_script(param_x)) {
10203
- return 'p2w-sh';
10352
+ return 'p2wsh';
10204
10353
  }
10205
10354
  else {
10206
- return 'unknown';
10355
+ return null;
10207
10356
  }
10208
10357
  }
10209
10358
  function parse_witness_version(type) {
10210
- if (type.startsWith('p2tr'))
10211
- return 1;
10359
+ if (type === null)
10360
+ return null;
10212
10361
  if (type.startsWith('p2w'))
10213
10362
  return 0;
10363
+ if (type.startsWith('p2t'))
10364
+ return 1;
10214
10365
  return null;
10215
10366
  }
10216
10367