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