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