@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
@@ -4,9 +4,9 @@ export function get_address_script(script_key, script_type) {
4
4
  return get_p2pkh_script(script_key);
5
5
  case 'p2sh':
6
6
  return get_p2sh_script(script_key);
7
- case 'p2w-pkh':
7
+ case 'p2wpkh':
8
8
  return get_p2w_pkh_script(script_key);
9
- case 'p2w-sh':
9
+ case 'p2wsh':
10
10
  return get_p2w_sh_script(script_key);
11
11
  case 'p2tr':
12
12
  return get_p2tr_script(script_key);
@@ -16,31 +16,31 @@ export function get_address_script(script_key, script_type) {
16
16
  }
17
17
  function get_p2pkh_script(script_key) {
18
18
  return {
19
- script_hex: '76a914' + script_key + '88ac',
20
- script_asm: ['OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG']
19
+ hex: '76a914' + script_key + '88ac',
20
+ asm: ['OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG']
21
21
  };
22
22
  }
23
23
  function get_p2sh_script(script_key) {
24
24
  return {
25
- script_hex: 'a914' + script_key + '87',
26
- script_asm: ['OP_HASH160', script_key, 'OP_EQUAL']
25
+ hex: 'a914' + script_key + '87',
26
+ asm: ['OP_HASH160', script_key, 'OP_EQUAL']
27
27
  };
28
28
  }
29
29
  function get_p2w_pkh_script(script_key) {
30
30
  return {
31
- script_hex: '0014' + script_key,
32
- script_asm: ['OP_0', script_key]
31
+ hex: '0014' + script_key,
32
+ asm: ['OP_0', script_key]
33
33
  };
34
34
  }
35
35
  function get_p2w_sh_script(script_key) {
36
36
  return {
37
- script_hex: '0020' + script_key,
38
- script_asm: ['OP_0', script_key]
37
+ hex: '0020' + script_key,
38
+ asm: ['OP_0', script_key]
39
39
  };
40
40
  }
41
41
  function get_p2tr_script(script_key) {
42
42
  return {
43
- script_hex: '5120' + script_key,
44
- script_asm: ['OP_1', script_key]
43
+ hex: '5120' + script_key,
44
+ asm: ['OP_1', script_key]
45
45
  };
46
46
  }
@@ -1,4 +1,3 @@
1
- import type { AddressConfig, AddressType, ChainNetwork, AddressContext, AddressData } from '../../types/index.js';
2
- export declare function get_address_config(address_network: ChainNetwork, address_type: AddressType): AddressConfig | null;
3
- export declare function get_address_ctx(address: string): AddressContext;
4
- export declare function parse_address(address: string): AddressData;
1
+ import type { AddressConfig, AddressInfo, ChainNetwork, LockScriptType } from '../../types/index.js';
2
+ export declare function get_address_config(address_network: ChainNetwork, address_type: LockScriptType): AddressConfig | null;
3
+ export declare function get_address_info(address: string): AddressInfo;
@@ -10,12 +10,12 @@ const CONFIG_TABLE = [
10
10
  ['m', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
11
11
  ['n', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
12
12
  ['2', 'p2sh', 'regtest', 20, 'base58', 0xC4],
13
- ['bc', 'p2w-pkh', 'main', 20, 'bech32', 0],
14
- ['tb', 'p2w-pkh', 'testnet', 20, 'bech32', 0],
15
- ['bcrt', 'p2w-pkh', 'regtest', 20, 'bech32', 0],
16
- ['bc', 'p2w-sh', 'main', 32, 'bech32', 0],
17
- ['tb', 'p2w-sh', 'testnet', 32, 'bech32', 0],
18
- ['bcrt', 'p2w-sh', 'regtest', 32, 'bech32', 0],
13
+ ['bc', 'p2wpkh', 'main', 20, 'bech32', 0],
14
+ ['tb', 'p2wpkh', 'testnet', 20, 'bech32', 0],
15
+ ['bcrt', 'p2wpkh', 'regtest', 20, 'bech32', 0],
16
+ ['bc', 'p2wsh', 'main', 32, 'bech32', 0],
17
+ ['tb', 'p2wsh', 'testnet', 32, 'bech32', 0],
18
+ ['bcrt', 'p2wsh', 'regtest', 32, 'bech32', 0],
19
19
  ['bc', 'p2tr', 'main', 32, 'bech32m', 1],
20
20
  ['tb', 'p2tr', 'testnet', 32, 'bech32m', 1],
21
21
  ['bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1]
@@ -28,7 +28,7 @@ export function get_address_config(address_network, address_type) {
28
28
  }
29
29
  return null;
30
30
  }
31
- export function get_address_ctx(address) {
31
+ export function get_address_info(address) {
32
32
  const dec = decode_address(address);
33
33
  for (const [prefix, type, network, size, format, version] of CONFIG_TABLE) {
34
34
  if (format !== dec.format)
@@ -45,13 +45,9 @@ export function get_address_ctx(address) {
45
45
  if (!address.startsWith(prefix))
46
46
  continue;
47
47
  }
48
- const hex = Buff.uint(dec.data).hex;
49
- return { data: dec.data, hex, type, prefix, network, size, format, version };
48
+ const data = Buff.uint(dec.data).hex;
49
+ const script = get_address_script(data, type);
50
+ return { data, script, type, prefix, network, size, format, version };
50
51
  }
51
52
  throw new Error('address configuration is invalid');
52
53
  }
53
- export function parse_address(address) {
54
- const ctx = get_address_ctx(address);
55
- const script = get_address_script(ctx.hex, ctx.type);
56
- return { ...ctx, ...script };
57
- }
@@ -1,3 +1,5 @@
1
1
  import { Bytes } from '@vbyte/buff';
2
+ import type { ScriptInfo } from '../../types/script.js';
3
+ export declare function parse_script(script: Bytes): ScriptInfo;
2
4
  export declare function decode_script(script: Bytes): string[];
3
5
  export declare function is_valid_script(script: string | Uint8Array): boolean;
@@ -1,5 +1,12 @@
1
- import { Stream } from '@vbyte/buff';
1
+ import { Buff, Stream } from '@vbyte/buff';
2
2
  import { get_op_code, get_op_type, is_valid_op } from './words.js';
3
+ export function parse_script(script) {
4
+ const bytes = Buff.bytes(script);
5
+ return {
6
+ asm: decode_script(bytes),
7
+ hex: bytes.hex
8
+ };
9
+ }
3
10
  export function decode_script(script) {
4
11
  const stream = new Stream(script);
5
12
  const stack = [];
@@ -1,126 +1,13 @@
1
1
  import { encode_script } from './encode.js';
2
- import { decode_script, is_valid_script } from './decode.js';
3
- import { parse_script_pubkeys, prefix_script_size } from './util.js';
2
+ import { decode_script, is_valid_script, parse_script } from './decode.js';
4
3
  export * from './decode.js';
5
4
  export * from './encode.js';
5
+ export * from './lock.js';
6
6
  export * from './util.js';
7
7
  export * from './words.js';
8
8
  export declare namespace ScriptUtil {
9
- const prefix_size: typeof prefix_script_size;
9
+ const parse: typeof parse_script;
10
10
  const decode: typeof decode_script;
11
11
  const encode: typeof encode_script;
12
12
  const is_valid: typeof is_valid_script;
13
- const get_pubkeys: typeof parse_script_pubkeys;
14
- const OPCODES: {
15
- OP_0: number;
16
- OP_PUSHDATA1: number;
17
- OP_PUSHDATA2: number;
18
- OP_PUSHDATA4: number;
19
- OP_1NEGATE: number;
20
- OP_SUCCESS80: number;
21
- OP_1: number;
22
- OP_2: number;
23
- OP_3: number;
24
- OP_4: number;
25
- OP_5: number;
26
- OP_6: number;
27
- OP_7: number;
28
- OP_8: number;
29
- OP_9: number;
30
- OP_10: number;
31
- OP_11: number;
32
- OP_12: number;
33
- OP_13: number;
34
- OP_14: number;
35
- OP_15: number;
36
- OP_16: number;
37
- OP_NOP: number;
38
- OP_SUCCESS98: number;
39
- OP_IF: number;
40
- OP_NOTIF: number;
41
- OP_ELSE: number;
42
- OP_ENDIF: number;
43
- OP_VERIFY: number;
44
- OP_RETURN: number;
45
- OP_TOALTSTACK: number;
46
- OP_FROMALTSTACK: number;
47
- OP_2DROP: number;
48
- OP_2DUP: number;
49
- OP_3DUP: number;
50
- OP_2OVER: number;
51
- OP_2ROT: number;
52
- OP_2SWAP: number;
53
- OP_IFDUP: number;
54
- OP_DEPTH: number;
55
- OP_DROP: number;
56
- OP_DUP: number;
57
- OP_NIP: number;
58
- OP_OVER: number;
59
- OP_PICK: number;
60
- OP_ROLL: number;
61
- OP_ROT: number;
62
- OP_SWAP: number;
63
- OP_TUCK: number;
64
- OP_SUCCESS126: number;
65
- OP_SUCCESS127: number;
66
- OP_SUCCESS128: number;
67
- OP_SUCCESS129: number;
68
- OP_SIZE: number;
69
- OP_SUCCESS131: number;
70
- OP_SUCCESS132: number;
71
- OP_SUCCESS133: number;
72
- OP_SUCCESS134: number;
73
- OP_EQUAL: number;
74
- OP_EQUALVERIFY: number;
75
- OP_SUCCESS137: number;
76
- OP_SUCCESS138: number;
77
- OP_1ADD: number;
78
- OP_1SUB: number;
79
- OP_SUCCESS141: number;
80
- OP_SUCCESS142: number;
81
- OP_NEGATE: number;
82
- OP_ABS: number;
83
- OP_NOT: number;
84
- OP_0NOTEQUAL: number;
85
- OP_ADD: number;
86
- OP_SUB: number;
87
- OP_SUCCESS149: number;
88
- OP_SUCCESS150: number;
89
- OP_SUCCESS151: number;
90
- OP_SUCCESS152: number;
91
- OP_SUCCESS153: number;
92
- OP_BOOLAND: number;
93
- OP_BOOLOR: number;
94
- OP_NUMEQUAL: number;
95
- OP_NUMEQUALVERIFY: number;
96
- OP_NUMNOTEQUAL: number;
97
- OP_LESSTHAN: number;
98
- OP_GREATERTHAN: number;
99
- OP_LESSTHANOREQUAL: number;
100
- OP_GREATERTHANOREQUAL: number;
101
- OP_MIN: number;
102
- OP_MAX: number;
103
- OP_WITHIN: number;
104
- OP_RIPEMD160: number;
105
- OP_SHA1: number;
106
- OP_SHA256: number;
107
- OP_HASH160: number;
108
- OP_HASH256: number;
109
- OP_CODESEPARATOR: number;
110
- OP_CHECKSIG: number;
111
- OP_CHECKSIGVERIFY: number;
112
- OP_CHECKMULTISIG: number;
113
- OP_CHECKMULTISIGVERIFY: number;
114
- OP_NOP1: number;
115
- OP_CHECKLOCKTIMEVERIFY: number;
116
- OP_CHECKSEQUENCEVERIFY: number;
117
- OP_NOP4: number;
118
- OP_NOP5: number;
119
- OP_NOP6: number;
120
- OP_NOP7: number;
121
- OP_NOP8: number;
122
- OP_NOP9: number;
123
- OP_NOP10: number;
124
- OP_CHECKSIGADD: number;
125
- };
126
13
  }
@@ -1,17 +1,14 @@
1
- import { OPCODE_MAP } from './words.js';
2
1
  import { encode_script } from './encode.js';
3
- import { decode_script, is_valid_script } from './decode.js';
4
- import { parse_script_pubkeys, prefix_script_size } from './util.js';
2
+ import { decode_script, is_valid_script, parse_script } from './decode.js';
5
3
  export * from './decode.js';
6
4
  export * from './encode.js';
5
+ export * from './lock.js';
7
6
  export * from './util.js';
8
7
  export * from './words.js';
9
8
  export var ScriptUtil;
10
9
  (function (ScriptUtil) {
11
- ScriptUtil.prefix_size = prefix_script_size;
10
+ ScriptUtil.parse = parse_script;
12
11
  ScriptUtil.decode = decode_script;
13
12
  ScriptUtil.encode = encode_script;
14
13
  ScriptUtil.is_valid = is_valid_script;
15
- ScriptUtil.get_pubkeys = parse_script_pubkeys;
16
- ScriptUtil.OPCODES = OPCODE_MAP;
17
14
  })(ScriptUtil || (ScriptUtil = {}));
@@ -0,0 +1,12 @@
1
+ import { Bytes } from '@vbyte/buff';
2
+ import type { LockScriptInfo, LockScriptType, WitnessVersion } from '../../types/index.js';
3
+ export declare function is_return_script(script: Bytes): boolean;
4
+ export declare function get_lock_script_info(script: Bytes): LockScriptInfo;
5
+ export declare function get_lock_script_type(script: Bytes): LockScriptType | null;
6
+ export declare function get_lock_script_version(script: Bytes): WitnessVersion | null;
7
+ export declare function is_p2pkh_script(script: Bytes): boolean;
8
+ export declare function is_p2sh_script(script: Bytes): boolean;
9
+ export declare function is_p2wpkh_script(script: Bytes): boolean;
10
+ export declare function is_p2wsh_script(script: Bytes): boolean;
11
+ export declare function is_p2tr_script(script: Bytes): boolean;
12
+ export declare function is_opreturn_script(script: Bytes): boolean;
@@ -0,0 +1,52 @@
1
+ import { Buff } from '@vbyte/buff';
2
+ import { LOCK_SCRIPT_REGEX } from '../../const.js';
3
+ export function is_return_script(script) {
4
+ const bytes = Buff.bytes(script);
5
+ return bytes.at(0) === 0x6a;
6
+ }
7
+ export function get_lock_script_info(script) {
8
+ return {
9
+ type: get_lock_script_type(script),
10
+ version: get_lock_script_version(script)
11
+ };
12
+ }
13
+ export function get_lock_script_type(script) {
14
+ const hex = Buff.bytes(script).hex;
15
+ for (const [type, regex] of Object.entries(LOCK_SCRIPT_REGEX)) {
16
+ if (regex.test(hex))
17
+ return type;
18
+ }
19
+ return null;
20
+ }
21
+ export function get_lock_script_version(script) {
22
+ const version = Buff.bytes(script);
23
+ switch (version.at(0)) {
24
+ case 0x00: return 0;
25
+ case 0x51: return 1;
26
+ default: return null;
27
+ }
28
+ }
29
+ export function is_p2pkh_script(script) {
30
+ const hex = Buff.bytes(script).hex;
31
+ return LOCK_SCRIPT_REGEX['p2pkh'].test(hex);
32
+ }
33
+ export function is_p2sh_script(script) {
34
+ const hex = Buff.bytes(script).hex;
35
+ return LOCK_SCRIPT_REGEX['p2sh'].test(hex);
36
+ }
37
+ export function is_p2wpkh_script(script) {
38
+ const hex = Buff.bytes(script).hex;
39
+ return LOCK_SCRIPT_REGEX['p2wpkh'].test(hex);
40
+ }
41
+ export function is_p2wsh_script(script) {
42
+ const hex = Buff.bytes(script).hex;
43
+ return LOCK_SCRIPT_REGEX['p2wsh'].test(hex);
44
+ }
45
+ export function is_p2tr_script(script) {
46
+ const hex = Buff.bytes(script).hex;
47
+ return LOCK_SCRIPT_REGEX['p2tr'].test(hex);
48
+ }
49
+ export function is_opreturn_script(script) {
50
+ const hex = Buff.bytes(script).hex;
51
+ return LOCK_SCRIPT_REGEX['opreturn'].test(hex);
52
+ }
@@ -1,9 +1,4 @@
1
- import { Bytes } from '@vbyte/buff';
2
- import type { TxData, TxOutput, TxOutputInfo, TxOutputTemplate, TxOutputType, TxValue, WitnessVersion } from '../../types/index.js';
3
- export declare function is_return_script(script: Bytes): boolean;
4
- export declare function get_vout_script_info(script: Bytes): TxOutputInfo;
5
- export declare function get_vout_script_type(script: Bytes): TxOutputType | null;
6
- export declare function get_vout_script_version(script: Bytes): WitnessVersion | null;
1
+ import type { TxData, TxOutput, TxOutputTemplate, TxValue } from '../../types/index.js';
7
2
  export declare function get_txid(txdata: string | Uint8Array | TxData): string;
8
3
  export declare function get_txhash(txdata: string | Uint8Array | TxData): string;
9
4
  export declare function get_tx_value(txdata: string | Uint8Array | TxData): TxValue;
@@ -5,33 +5,7 @@ import { hash256 } from '@vbyte/micro-lib/hash';
5
5
  import { encode_tx } from './encode.js';
6
6
  import { parse_tx } from './parse.js';
7
7
  import { assert_tx_template } from './validate.js';
8
- import { DEFAULT, LOCK_SCRIPT_REGEX } from '../../const.js';
9
- export function is_return_script(script) {
10
- const bytes = Buff.bytes(script);
11
- return bytes.at(0) === 0x6a;
12
- }
13
- export function get_vout_script_info(script) {
14
- return {
15
- type: get_vout_script_type(script),
16
- version: get_vout_script_version(script)
17
- };
18
- }
19
- export function get_vout_script_type(script) {
20
- const hex = Buff.bytes(script).hex;
21
- for (const [type, regex] of Object.entries(LOCK_SCRIPT_REGEX)) {
22
- if (regex.test(hex))
23
- return type;
24
- }
25
- return null;
26
- }
27
- export function get_vout_script_version(script) {
28
- const version = Buff.bytes(script);
29
- switch (version.at(0)) {
30
- case 0x00: return 0;
31
- case 0x51: return 1;
32
- default: return null;
33
- }
34
- }
8
+ import { DEFAULT } from '../../const.js';
35
9
  export function get_txid(txdata) {
36
10
  if (typeof txdata === 'object') {
37
11
  assert_tx_template(txdata);
@@ -44,9 +44,9 @@ function parse_cblock_data(data) {
44
44
  function parse_witness_script(elems, type) {
45
45
  let script;
46
46
  switch (type) {
47
- case 'p2tr-ts':
47
+ case 'p2ts':
48
48
  script = elems.at(-1);
49
- case 'p2w-sh':
49
+ case 'p2wsh':
50
50
  script = elems.at(-1);
51
51
  }
52
52
  return (script !== undefined) ? new Buff(script).hex : null;
@@ -54,33 +54,35 @@ function parse_witness_script(elems, type) {
54
54
  function parse_witness_type(elems, cblock) {
55
55
  let param_0 = elems.at(0), param_1 = elems.at(1), param_x = elems.at(-1);
56
56
  if (cblock !== null && param_x !== undefined) {
57
- return 'p2tr-ts';
57
+ return 'p2ts';
58
58
  }
59
59
  else if (elems.length === 2 &&
60
60
  param_0 !== undefined &&
61
61
  param_1 !== undefined &&
62
62
  param_0.length >= 64 &&
63
63
  param_1.length === 33) {
64
- return 'p2w-pkh';
64
+ return 'p2wpkh';
65
65
  }
66
66
  else if (elems.length === 1 &&
67
67
  param_0 !== undefined &&
68
68
  param_0.length === 64) {
69
- return 'p2tr-pk';
69
+ return 'p2tr';
70
70
  }
71
71
  else if (elems.length > 1 &&
72
72
  param_x !== undefined &&
73
73
  is_valid_script(param_x)) {
74
- return 'p2w-sh';
74
+ return 'p2wsh';
75
75
  }
76
76
  else {
77
- return 'unknown';
77
+ return null;
78
78
  }
79
79
  }
80
80
  function parse_witness_version(type) {
81
- if (type.startsWith('p2tr'))
82
- return 1;
81
+ if (type === null)
82
+ return null;
83
83
  if (type.startsWith('p2w'))
84
84
  return 0;
85
+ if (type.startsWith('p2t'))
86
+ return 1;
85
87
  return null;
86
88
  }