@vbyte/btc-dev 1.1.3 → 1.1.4

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 +2 -1
  25. package/dist/lib/script/index.js +2 -1
  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 +313 -191
  32. package/dist/main.cjs.map +1 -1
  33. package/dist/module.mjs +313 -191
  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 +3 -1
  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,113 @@ 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 get_lock_script_type(script) {
8317
+ const hex = Buff.bytes(script).hex;
8318
+ for (const [type, regex] of Object.entries(LOCK_SCRIPT_REGEX)) {
8319
+ if (regex.test(hex))
8320
+ return type;
8321
+ }
8322
+ return null;
8323
+ }
8324
+ function is_p2pkh_script(script) {
8325
+ const hex = Buff.bytes(script).hex;
8326
+ return LOCK_SCRIPT_REGEX['p2pkh'].test(hex);
8327
+ }
8328
+ function is_p2sh_script(script) {
8329
+ const hex = Buff.bytes(script).hex;
8330
+ return LOCK_SCRIPT_REGEX['p2sh'].test(hex);
8331
+ }
8332
+ function is_p2wpkh_script(script) {
8333
+ const hex = Buff.bytes(script).hex;
8334
+ return LOCK_SCRIPT_REGEX['p2wpkh'].test(hex);
8335
+ }
8336
+ function is_p2wsh_script(script) {
8337
+ const hex = Buff.bytes(script).hex;
8338
+ return LOCK_SCRIPT_REGEX['p2wsh'].test(hex);
8339
+ }
8340
+ function is_p2tr_script(script) {
8341
+ const hex = Buff.bytes(script).hex;
8342
+ return LOCK_SCRIPT_REGEX['p2tr'].test(hex);
8343
+ }
8344
+
8247
8345
  function get_address_script(script_key, script_type) {
8248
8346
  switch (script_type) {
8249
8347
  case 'p2pkh':
8250
8348
  return get_p2pkh_script(script_key);
8251
8349
  case 'p2sh':
8252
8350
  return get_p2sh_script(script_key);
8253
- case 'p2w-pkh':
8351
+ case 'p2wpkh':
8254
8352
  return get_p2w_pkh_script(script_key);
8255
- case 'p2w-sh':
8353
+ case 'p2wsh':
8256
8354
  return get_p2w_sh_script(script_key);
8257
8355
  case 'p2tr':
8258
8356
  return get_p2tr_script(script_key);
@@ -8262,32 +8360,32 @@ function get_address_script(script_key, script_type) {
8262
8360
  }
8263
8361
  function get_p2pkh_script(script_key) {
8264
8362
  return {
8265
- script_hex: '76a914' + script_key + '88ac',
8266
- script_asm: ['OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG']
8363
+ hex: '76a914' + script_key + '88ac',
8364
+ asm: ['OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG']
8267
8365
  };
8268
8366
  }
8269
8367
  function get_p2sh_script(script_key) {
8270
8368
  return {
8271
- script_hex: 'a914' + script_key + '87',
8272
- script_asm: ['OP_HASH160', script_key, 'OP_EQUAL']
8369
+ hex: 'a914' + script_key + '87',
8370
+ asm: ['OP_HASH160', script_key, 'OP_EQUAL']
8273
8371
  };
8274
8372
  }
8275
8373
  function get_p2w_pkh_script(script_key) {
8276
8374
  return {
8277
- script_hex: '0014' + script_key,
8278
- script_asm: ['OP_0', script_key]
8375
+ hex: '0014' + script_key,
8376
+ asm: ['OP_0', script_key]
8279
8377
  };
8280
8378
  }
8281
8379
  function get_p2w_sh_script(script_key) {
8282
8380
  return {
8283
- script_hex: '0020' + script_key,
8284
- script_asm: ['OP_0', script_key]
8381
+ hex: '0020' + script_key,
8382
+ asm: ['OP_0', script_key]
8285
8383
  };
8286
8384
  }
8287
8385
  function get_p2tr_script(script_key) {
8288
8386
  return {
8289
- script_hex: '5120' + script_key,
8290
- script_asm: ['OP_1', script_key]
8387
+ hex: '5120' + script_key,
8388
+ asm: ['OP_1', script_key]
8291
8389
  };
8292
8390
  }
8293
8391
 
@@ -8300,12 +8398,12 @@ const CONFIG_TABLE = [
8300
8398
  ['m', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
8301
8399
  ['n', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
8302
8400
  ['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],
8401
+ ['bc', 'p2wpkh', 'main', 20, 'bech32', 0],
8402
+ ['tb', 'p2wpkh', 'testnet', 20, 'bech32', 0],
8403
+ ['bcrt', 'p2wpkh', 'regtest', 20, 'bech32', 0],
8404
+ ['bc', 'p2wsh', 'main', 32, 'bech32', 0],
8405
+ ['tb', 'p2wsh', 'testnet', 32, 'bech32', 0],
8406
+ ['bcrt', 'p2wsh', 'regtest', 32, 'bech32', 0],
8309
8407
  ['bc', 'p2tr', 'main', 32, 'bech32m', 1],
8310
8408
  ['tb', 'p2tr', 'testnet', 32, 'bech32m', 1],
8311
8409
  ['bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1]
@@ -8318,7 +8416,7 @@ function get_address_config(address_network, address_type) {
8318
8416
  }
8319
8417
  return null;
8320
8418
  }
8321
- function get_address_ctx(address) {
8419
+ function get_address_info(address) {
8322
8420
  const dec = decode_address(address);
8323
8421
  for (const [prefix, type, network, size, format, version] of CONFIG_TABLE) {
8324
8422
  if (format !== dec.format)
@@ -8335,175 +8433,269 @@ function get_address_ctx(address) {
8335
8433
  if (!address.startsWith(prefix))
8336
8434
  continue;
8337
8435
  }
8338
- const hex = Buff.uint(dec.data).hex;
8339
- return { data: dec.data, hex, type, prefix, network, size, format, version };
8436
+ const data = Buff.uint(dec.data).hex;
8437
+ const script = get_address_script(data, type);
8438
+ return { data, script, type, prefix, network, size, format, version };
8340
8439
  }
8341
8440
  throw new Error('address configuration is invalid');
8342
8441
  }
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
8442
 
8349
- const ADDR_TYPE$4 = 'p2pkh';
8443
+ const ADDRESS_TYPE$4 = LOCK_SCRIPT_TYPE.P2PKH;
8350
8444
  var P2PKH;
8351
8445
  (function (P2PKH) {
8352
- P2PKH.create = create_p2pkh_address;
8353
- P2PKH.encode = encode_p2pkh_address;
8354
- P2PKH.decode = decode_p2pkh_address;
8446
+ P2PKH.create_address = create_p2pkh_address;
8447
+ P2PKH.create_script = create_p2pkh_script;
8448
+ P2PKH.encode_address = encode_p2pkh_address;
8449
+ P2PKH.encode_script = encode_p2pkh_script;
8450
+ P2PKH.decode_address = decode_p2pkh_address;
8451
+ P2PKH.decode_script = decode_p2pkh_script;
8355
8452
  })(P2PKH || (P2PKH = {}));
8356
- function create_p2pkh_address(script, network = 'main') {
8357
- const bytes = Buff.bytes(script);
8453
+ function create_p2pkh_address(pubkey, network = 'main') {
8454
+ const script = create_p2pkh_script(pubkey);
8455
+ return encode_p2pkh_address(script, network);
8456
+ }
8457
+ function create_p2pkh_script(pubkey) {
8458
+ const bytes = Buff.bytes(pubkey);
8459
+ Assert.size(bytes, 33, 'invalid pubkey size');
8358
8460
  const hash = hash160(bytes);
8359
- return encode_p2pkh_address(hash, network);
8461
+ return encode_p2pkh_script(hash);
8462
+ }
8463
+ function encode_p2pkh_script(pk_hash) {
8464
+ return Buff.join(['76a914', pk_hash, '88ac']);
8360
8465
  }
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}`);
8466
+ function encode_p2pkh_address(script, network = 'main') {
8467
+ const pk_hash = decode_p2pkh_script(script);
8468
+ const config = get_address_config(network, ADDRESS_TYPE$4);
8469
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE$4} on ${network}`);
8470
+ Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}`);
8366
8471
  return encode_address({
8367
- data: bytes,
8472
+ data: pk_hash,
8368
8473
  format: 'base58',
8369
8474
  version: config.version
8370
8475
  });
8371
8476
  }
8372
8477
  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}`);
8478
+ const parsed = get_address_info(address);
8479
+ Assert.ok(parsed.type === 'p2pkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE$4}`);
8375
8480
  return parsed;
8376
8481
  }
8482
+ function decode_p2pkh_script(script) {
8483
+ const bytes = Buff.bytes(script);
8484
+ Assert.ok(is_p2pkh_script(script), `invalid p2pkh script`);
8485
+ return bytes.slice(3, 23);
8486
+ }
8377
8487
 
8378
- const ADDR_TYPE$3 = 'p2sh';
8488
+ const ADDRESS_TYPE$3 = LOCK_SCRIPT_TYPE.P2SH;
8379
8489
  var P2SH;
8380
8490
  (function (P2SH) {
8381
- P2SH.create = create_p2sh_address;
8382
- P2SH.encode = encode_p2sh_address;
8383
- P2SH.decode = decode_p2sh_address;
8491
+ P2SH.create_address = create_p2sh_address;
8492
+ P2SH.create_script = create_p2sh_script;
8493
+ P2SH.encode_address = encode_p2sh_address;
8494
+ P2SH.encode_script = encode_p2sh_script;
8495
+ P2SH.decode_address = decode_p2sh_address;
8496
+ P2SH.decode_script = decode_p2sh_script;
8384
8497
  })(P2SH || (P2SH = {}));
8385
8498
  function create_p2sh_address(script, network = 'main') {
8386
8499
  const bytes = Buff.bytes(script);
8387
8500
  const hash = hash160(bytes);
8388
- return encode_p2sh_address(hash, network);
8501
+ const p2sh_script = encode_p2sh_script(hash);
8502
+ return encode_p2sh_address(p2sh_script, network);
8389
8503
  }
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}`);
8504
+ function create_p2sh_script(script) {
8505
+ const bytes = Buff.bytes(script);
8506
+ const hash = hash160(bytes);
8507
+ return encode_p2sh_script(hash);
8508
+ }
8509
+ function encode_p2sh_script(script_hash) {
8510
+ return Buff.join(['a914', script_hash, '87']);
8511
+ }
8512
+ function encode_p2sh_address(script_pk, network = 'main') {
8513
+ const script_hash = decode_p2sh_script(script_pk);
8514
+ const config = get_address_config(network, ADDRESS_TYPE$3);
8515
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE$3} on ${network}`);
8516
+ Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}`);
8395
8517
  return encode_address({
8396
- data: bytes,
8518
+ data: script_hash,
8397
8519
  format: 'base58',
8398
8520
  version: config.version
8399
8521
  });
8400
8522
  }
8401
8523
  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}`);
8524
+ const parsed = get_address_info(address);
8525
+ Assert.ok(parsed.type === 'p2sh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE$3}`);
8404
8526
  return parsed;
8405
8527
  }
8528
+ function decode_p2sh_script(script) {
8529
+ Assert.ok(is_p2sh_script(script), `invalid p2sh script`);
8530
+ const bytes = Buff.bytes(script);
8531
+ return bytes.slice(2, 22);
8532
+ }
8406
8533
 
8407
- const ADDR_TYPE$2 = 'p2w-pkh';
8534
+ const ADDRESS_TYPE$2 = LOCK_SCRIPT_TYPE.P2WPKH;
8408
8535
  var P2WPKH;
8409
8536
  (function (P2WPKH) {
8410
- P2WPKH.create = create_p2wpkh_address;
8411
- P2WPKH.encode = encode_p2wpkh_address;
8412
- P2WPKH.decode = decode_p2wpkh_address;
8537
+ P2WPKH.create_address = create_p2wpkh_address;
8538
+ P2WPKH.create_script = create_p2wpkh_script;
8539
+ P2WPKH.encode_address = encode_p2wpkh_address;
8540
+ P2WPKH.encode_script = encode_p2wpkh_script;
8541
+ P2WPKH.decode_address = decode_p2wpkh_address;
8542
+ P2WPKH.decode_script = decode_p2wpkh_script;
8413
8543
  })(P2WPKH || (P2WPKH = {}));
8414
8544
  function create_p2wpkh_address(pubkey, network = 'main') {
8545
+ const script = create_p2wpkh_script(pubkey);
8546
+ return encode_p2wpkh_address(script, network);
8547
+ }
8548
+ function create_p2wpkh_script(pubkey) {
8415
8549
  const bytes = Buff.bytes(pubkey);
8416
- Assert.size(bytes, 33, `invalid payload size: ${bytes.length} !== 33`);
8550
+ Assert.size(bytes, 33, 'invalid pubkey size');
8417
8551
  const hash = hash160(bytes);
8418
- return encode_p2wpkh_address(hash, network);
8552
+ return encode_p2wpkh_script(hash);
8553
+ }
8554
+ function encode_p2wpkh_script(pk_hash) {
8555
+ return Buff.join(['0014', pk_hash]);
8419
8556
  }
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}`);
8557
+ function encode_p2wpkh_address(script_pk, network = 'main') {
8558
+ const pk_hash = decode_p2wpkh_script(script_pk);
8559
+ const config = get_address_config(network, ADDRESS_TYPE$2);
8560
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE$2} on ${network}`);
8561
+ Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}`);
8425
8562
  return encode_address({
8426
- data: bytes,
8563
+ data: pk_hash,
8427
8564
  format: 'bech32',
8428
8565
  prefix: config.prefix
8429
8566
  });
8430
8567
  }
8431
8568
  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}`);
8569
+ const parsed = get_address_info(address);
8570
+ Assert.ok(parsed.type === 'p2wpkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE$2}`);
8434
8571
  return parsed;
8435
8572
  }
8573
+ function decode_p2wpkh_script(script) {
8574
+ Assert.ok(is_p2wpkh_script(script), `invalid p2wpkh script`);
8575
+ const bytes = Buff.bytes(script);
8576
+ return bytes.slice(2, 22);
8577
+ }
8436
8578
 
8437
- const ADDR_TYPE$1 = 'p2w-sh';
8579
+ const ADDRESS_TYPE$1 = LOCK_SCRIPT_TYPE.P2WSH;
8438
8580
  var P2WSH;
8439
8581
  (function (P2WSH) {
8440
- P2WSH.create = create_p2wsh_address;
8441
- P2WSH.encode = encode_p2wsh_address;
8442
- P2WSH.decode = decode_p2wsh_address;
8582
+ P2WSH.create_address = create_p2wsh_address;
8583
+ P2WSH.create_script = create_p2wsh_script;
8584
+ P2WSH.encode_address = encode_p2wsh_address;
8585
+ P2WSH.encode_script = encode_p2wsh_script;
8586
+ P2WSH.decode_address = decode_p2wsh_address;
8587
+ P2WSH.decode_script = decode_p2wsh_script;
8443
8588
  })(P2WSH || (P2WSH = {}));
8444
8589
  function create_p2wsh_address(script, network = 'main') {
8590
+ const wsh_script = create_p2wsh_script(script);
8591
+ return encode_p2wsh_address(wsh_script, network);
8592
+ }
8593
+ function create_p2wsh_script(script) {
8445
8594
  const bytes = Buff.bytes(script);
8446
8595
  const hash = sha256(bytes);
8447
- return encode_p2wsh_address(hash, network);
8596
+ return encode_p2wsh_script(hash);
8597
+ }
8598
+ function encode_p2wsh_script(script_hash) {
8599
+ return Buff.join(['0020', script_hash]);
8448
8600
  }
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}`);
8601
+ function encode_p2wsh_address(script_pk, network = 'main') {
8602
+ const script_hash = decode_p2wsh_script(script_pk);
8603
+ const config = get_address_config(network, ADDRESS_TYPE$1);
8604
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE$1} on ${network}`);
8605
+ Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}`);
8454
8606
  return encode_address({
8455
- data: bytes,
8607
+ data: script_hash,
8456
8608
  format: 'bech32',
8457
8609
  prefix: config.prefix
8458
8610
  });
8459
8611
  }
8460
8612
  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}`);
8613
+ const parsed = get_address_info(address);
8614
+ Assert.ok(parsed.type === 'p2wsh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE$1}`);
8463
8615
  return parsed;
8464
8616
  }
8617
+ function decode_p2wsh_script(script) {
8618
+ Assert.ok(is_p2wsh_script(script), `invalid p2wsh script`);
8619
+ const bytes = Buff.bytes(script);
8620
+ return bytes.slice(2, 34);
8621
+ }
8465
8622
 
8466
- const ADDR_TYPE = 'p2tr';
8623
+ const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2TR;
8467
8624
  var P2TR;
8468
8625
  (function (P2TR) {
8469
- P2TR.encode = encode_p2tr_address;
8470
- P2TR.decode = decode_p2tr_address;
8626
+ P2TR.create_address = create_p2tr_address;
8627
+ P2TR.create_script = create_p2tr_script;
8628
+ P2TR.encode_address = encode_p2tr_address;
8629
+ P2TR.encode_script = encode_p2tr_script;
8630
+ P2TR.decode_address = decode_p2tr_address;
8631
+ P2TR.decode_script = decode_p2tr_script;
8471
8632
  })(P2TR || (P2TR = {}));
8472
- function encode_p2tr_address(pubkey, network = 'main') {
8633
+ function create_p2tr_address(pubkey, network = 'main') {
8634
+ const script = create_p2tr_script(pubkey);
8635
+ return encode_p2tr_address(script, network);
8636
+ }
8637
+ function create_p2tr_script(pubkey) {
8473
8638
  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}`);
8639
+ Assert.size(bytes, 32, 'invalid pubkey size');
8640
+ return encode_p2tr_script(bytes);
8641
+ }
8642
+ function encode_p2tr_script(pubkey) {
8643
+ return Buff.join(['5120', pubkey]);
8644
+ }
8645
+ function encode_p2tr_address(script_pk, network = 'main') {
8646
+ const pubkey = decode_p2tr_script(script_pk);
8647
+ const config = get_address_config(network, ADDRESS_TYPE);
8648
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
8649
+ Assert.size(pubkey, config.size, `invalid payload size: ${pubkey.length} !== ${config.size}`);
8477
8650
  return encode_address({
8478
- data: bytes,
8651
+ data: pubkey,
8479
8652
  format: 'bech32m',
8480
8653
  prefix: config.prefix
8481
8654
  });
8482
8655
  }
8483
8656
  function decode_p2tr_address(address) {
8484
- const parsed = parse_address(address);
8485
- Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`);
8657
+ const parsed = get_address_info(address);
8658
+ Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`);
8486
8659
  return parsed;
8487
8660
  }
8661
+ function decode_p2tr_script(script) {
8662
+ Assert.ok(is_p2tr_script(script), `invalid p2tr script`);
8663
+ const bytes = Buff.bytes(script);
8664
+ return bytes.slice(2, 34);
8665
+ }
8488
8666
 
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 = {}));
8667
+ function create_address(script, network = 'main') {
8668
+ const bytes = Buff.bytes(script);
8669
+ const type = get_lock_script_type(bytes);
8670
+ if (type === null)
8671
+ throw new Error('unrecognized script type: ' + bytes.hex);
8672
+ switch (type) {
8673
+ case LOCK_SCRIPT_TYPE.P2PKH:
8674
+ return P2PKH.create_address(script, network);
8675
+ case LOCK_SCRIPT_TYPE.P2SH:
8676
+ return P2SH.create_address(script, network);
8677
+ case LOCK_SCRIPT_TYPE.P2WPKH:
8678
+ return P2WPKH.create_address(script, network);
8679
+ case LOCK_SCRIPT_TYPE.P2WSH:
8680
+ return P2WSH.create_address(script, network);
8681
+ case LOCK_SCRIPT_TYPE.P2TR:
8682
+ return P2TR.create_address(script, network);
8683
+ default:
8684
+ throw new Error('unrecognized script type: ' + type);
8685
+ }
8686
+ }
8687
+ function parse_address(address) {
8688
+ return get_address_info(address);
8689
+ }
8498
8690
 
8499
8691
  var index$8 = /*#__PURE__*/Object.freeze({
8500
8692
  __proto__: null,
8501
- get AddressTool () { return AddressTool; },
8502
8693
  get P2PKH () { return P2PKH; },
8503
8694
  get P2SH () { return P2SH; },
8504
8695
  get P2TR () { return P2TR; },
8505
8696
  get P2WPKH () { return P2WPKH; },
8506
8697
  get P2WSH () { return P2WSH; },
8698
+ create_address: create_address,
8507
8699
  parse_address: parse_address
8508
8700
  });
8509
8701
 
@@ -8858,6 +9050,13 @@ function get_size_varint(size) {
8858
9050
  }
8859
9051
  }
8860
9052
 
9053
+ function parse_script(script) {
9054
+ const bytes = Buff.bytes(script);
9055
+ return {
9056
+ asm: decode_script(bytes),
9057
+ hex: bytes.hex
9058
+ };
9059
+ }
8861
9060
  function decode_script(script) {
8862
9061
  const stream = new Stream(script);
8863
9062
  const stack = [];
@@ -9197,6 +9396,7 @@ function parse_script_pubkeys(script) {
9197
9396
  var ScriptUtil;
9198
9397
  (function (ScriptUtil) {
9199
9398
  ScriptUtil.prefix_size = prefix_script_size;
9399
+ ScriptUtil.parse = parse_script;
9200
9400
  ScriptUtil.decode = decode_script;
9201
9401
  ScriptUtil.encode = encode_script;
9202
9402
  ScriptUtil.is_valid = is_valid_script;
@@ -9217,6 +9417,7 @@ var index$6 = /*#__PURE__*/Object.freeze({
9217
9417
  get_size_varint: get_size_varint,
9218
9418
  is_valid_op: is_valid_op,
9219
9419
  is_valid_script: is_valid_script,
9420
+ parse_script: parse_script,
9220
9421
  parse_script_pubkeys: parse_script_pubkeys,
9221
9422
  prefix_script_size: prefix_script_size,
9222
9423
  prefix_word_size: prefix_word_size,
@@ -9251,57 +9452,6 @@ function get_annex_data(witness) {
9251
9452
  return undefined;
9252
9453
  }
9253
9454
 
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
9455
  const taptree = union([array(byte32), byte32]);
9306
9456
  const config = object({
9307
9457
  pubkey: byte32,
@@ -9624,32 +9774,6 @@ function serialize_tx(txdata) {
9624
9774
  return { version, locktime, vin, vout };
9625
9775
  }
9626
9776
 
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
9777
  function get_txid(txdata) {
9654
9778
  if (typeof txdata === 'object') {
9655
9779
  assert_tx_template(txdata);
@@ -9825,12 +9949,8 @@ var index$4 = /*#__PURE__*/Object.freeze({
9825
9949
  get_txout_size: get_txout_size,
9826
9950
  get_txsize: get_txsize,
9827
9951
  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
9952
  get_vout_size: get_vout_size,
9832
9953
  get_vsize: get_vsize,
9833
- is_return_script: is_return_script,
9834
9954
  normalize_prevout: normalize_prevout,
9835
9955
  normalize_sequence: normalize_sequence,
9836
9956
  normalize_value: normalize_value,
@@ -10173,9 +10293,9 @@ function parse_cblock_data(data) {
10173
10293
  function parse_witness_script(elems, type) {
10174
10294
  let script;
10175
10295
  switch (type) {
10176
- case 'p2tr-ts':
10296
+ case 'p2ts':
10177
10297
  script = elems.at(-1);
10178
- case 'p2w-sh':
10298
+ case 'p2wsh':
10179
10299
  script = elems.at(-1);
10180
10300
  }
10181
10301
  return (script !== undefined) ? new Buff(script).hex : null;
@@ -10183,34 +10303,36 @@ function parse_witness_script(elems, type) {
10183
10303
  function parse_witness_type(elems, cblock) {
10184
10304
  let param_0 = elems.at(0), param_1 = elems.at(1), param_x = elems.at(-1);
10185
10305
  if (cblock !== null && param_x !== undefined) {
10186
- return 'p2tr-ts';
10306
+ return 'p2ts';
10187
10307
  }
10188
10308
  else if (elems.length === 2 &&
10189
10309
  param_0 !== undefined &&
10190
10310
  param_1 !== undefined &&
10191
10311
  param_0.length >= 64 &&
10192
10312
  param_1.length === 33) {
10193
- return 'p2w-pkh';
10313
+ return 'p2wpkh';
10194
10314
  }
10195
10315
  else if (elems.length === 1 &&
10196
10316
  param_0 !== undefined &&
10197
10317
  param_0.length === 64) {
10198
- return 'p2tr-pk';
10318
+ return 'p2tr';
10199
10319
  }
10200
10320
  else if (elems.length > 1 &&
10201
10321
  param_x !== undefined &&
10202
10322
  is_valid_script(param_x)) {
10203
- return 'p2w-sh';
10323
+ return 'p2wsh';
10204
10324
  }
10205
10325
  else {
10206
- return 'unknown';
10326
+ return null;
10207
10327
  }
10208
10328
  }
10209
10329
  function parse_witness_version(type) {
10210
- if (type.startsWith('p2tr'))
10211
- return 1;
10330
+ if (type === null)
10331
+ return null;
10212
10332
  if (type.startsWith('p2w'))
10213
10333
  return 0;
10334
+ if (type.startsWith('p2t'))
10335
+ return 1;
10214
10336
  return null;
10215
10337
  }
10216
10338