@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
@@ -1,39 +1,68 @@
1
- import { Buff } from '@vbyte/buff'
2
- import { Assert } from '@vbyte/micro-lib'
3
- import { encode_address } from './encode.js'
1
+ import { Buff, Bytes } from '@vbyte/buff'
2
+ import { Assert } from '@vbyte/micro-lib'
3
+ import { encode_address } from './encode.js'
4
+ import { is_p2tr_script } from '@/lib/script/lock.js'
5
+ import { LOCK_SCRIPT_TYPE } from '@/const.js'
4
6
 
5
7
  import {
6
8
  get_address_config,
7
- parse_address
9
+ get_address_info
8
10
  } from './util.js'
9
11
 
10
12
  import type {
11
- ChainNetwork,
12
- AddressData
13
+ AddressInfo,
14
+ ChainNetwork
13
15
  } from '@/types/index.js'
14
16
 
15
- const ADDR_TYPE = 'p2tr'
17
+ const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2TR
16
18
 
17
19
  export namespace P2TR {
18
- export const encode = encode_p2tr_address
19
- export const decode = decode_p2tr_address
20
+ export const create_address = create_p2tr_address
21
+ export const create_script = create_p2tr_script
22
+ export const encode_address = encode_p2tr_address
23
+ export const encode_script = encode_p2tr_script
24
+ export const decode_address = decode_p2tr_address
25
+ export const decode_script = decode_p2tr_script
20
26
  }
21
27
 
22
- function encode_p2tr_address (
23
- pubkey : string | Uint8Array,
24
- network : ChainNetwork = 'main'
28
+ function create_p2tr_address (
29
+ pubkey : Bytes,
30
+ network : ChainNetwork = 'main',
25
31
  ) : string {
32
+ // Create the p2tr script.
33
+ const script = create_p2tr_script(pubkey)
34
+ // Encode the script as an address.
35
+ return encode_p2tr_address(script, network)
36
+ }
37
+
38
+ function create_p2tr_script (pubkey : Bytes) : Buff {
26
39
  // Convert the public key into bytes.
27
40
  const bytes = Buff.bytes(pubkey)
41
+ // Assert the public key is 32 bytes.
42
+ Assert.size(bytes, 32, 'invalid pubkey size')
43
+ // Return the script.
44
+ return encode_p2tr_script(bytes)
45
+ }
46
+
47
+ function encode_p2tr_script (pubkey : Bytes) : Buff {
48
+ return Buff.join([ '5120', pubkey ])
49
+ }
50
+
51
+ function encode_p2tr_address (
52
+ script_pk : Bytes,
53
+ network : ChainNetwork = 'main'
54
+ ) : string {
55
+ // Get the public key from the script.
56
+ const pubkey = decode_p2tr_script(script_pk)
28
57
  // Get the address configuration.
29
- const config = get_address_config(network, ADDR_TYPE)
58
+ const config = get_address_config(network, ADDRESS_TYPE)
30
59
  // Assert the configuration exists.
31
- Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}` )
60
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
32
61
  // Assert the payload size is correct.
33
- Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}` )
62
+ Assert.size(pubkey, config.size, `invalid payload size: ${pubkey.length} !== ${config.size}` )
34
63
  // Encode the address.
35
64
  return encode_address({
36
- data : bytes,
65
+ data : pubkey,
37
66
  format : 'bech32m',
38
67
  prefix : config.prefix
39
68
  })
@@ -41,11 +70,22 @@ function encode_p2tr_address (
41
70
 
42
71
  function decode_p2tr_address (
43
72
  address : string
44
- ) : AddressData {
73
+ ) : AddressInfo {
45
74
  // Parse the address.
46
- const parsed = parse_address(address)
75
+ const parsed = get_address_info(address)
47
76
  // Assert the address type is correct.
48
- Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`)
77
+ Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
49
78
  // Return the parsed address.
50
79
  return parsed
51
80
  }
81
+
82
+ function decode_p2tr_script (
83
+ script : Bytes,
84
+ ) : Buff {
85
+ // Assert the script is a p2tr script.
86
+ Assert.ok(is_p2tr_script(script), `invalid p2tr script`)
87
+ // Convert the script into bytes.
88
+ const bytes = Buff.bytes(script)
89
+ // Return the public key from the script.
90
+ return bytes.slice(2, 34)
91
+ }
@@ -1,55 +1,71 @@
1
- import { Buff } from '@vbyte/buff'
2
- import { Assert } from '@vbyte/micro-lib'
3
- import { hash160 } from '@vbyte/micro-lib/hash'
4
- import { encode_address } from './encode.js'
1
+ import { Buff, Bytes } from '@vbyte/buff'
2
+ import { Assert } from '@vbyte/micro-lib'
3
+ import { hash160 } from '@vbyte/micro-lib/hash'
4
+ import { encode_address } from './encode.js'
5
+ import { is_p2wpkh_script } from '@/lib/script/lock.js'
6
+ import { LOCK_SCRIPT_TYPE } from '@/const.js'
5
7
 
6
8
  import {
7
9
  get_address_config,
8
- parse_address
10
+ get_address_info
9
11
  } from './util.js'
10
12
 
11
13
  import type {
12
- AddressData,
14
+ AddressInfo,
13
15
  ChainNetwork
14
16
  } from '@/types/index.js'
15
17
 
16
- const ADDR_TYPE = 'p2w-pkh'
18
+ const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WPKH
17
19
 
18
20
  export namespace P2WPKH {
19
- export const create = create_p2wpkh_address
20
- export const encode = encode_p2wpkh_address
21
- export const decode = decode_p2wpkh_address
21
+ export const create_address = create_p2wpkh_address
22
+ export const create_script = create_p2wpkh_script
23
+ export const encode_address = encode_p2wpkh_address
24
+ export const encode_script = encode_p2wpkh_script
25
+ export const decode_address = decode_p2wpkh_address
26
+ export const decode_script = decode_p2wpkh_script
22
27
  }
23
28
 
24
29
  function create_p2wpkh_address (
25
- pubkey : string | Uint8Array,
30
+ pubkey : Bytes,
26
31
  network : ChainNetwork = 'main',
27
32
  ) : string {
33
+ // Create the p2wpkh script.
34
+ const script = create_p2wpkh_script(pubkey)
35
+ // Encode the script as an address.
36
+ return encode_p2wpkh_address(script, network)
37
+ }
38
+
39
+ function create_p2wpkh_script (pubkey : Bytes) : Buff {
28
40
  // Convert the public key into bytes.
29
41
  const bytes = Buff.bytes(pubkey)
30
- // Assert the payload size is correct.
31
- Assert.size(bytes, 33, `invalid payload size: ${bytes.length} !== 33` )
42
+ // Assert the public key is 33 bytes.
43
+ Assert.size(bytes, 33, 'invalid pubkey size')
32
44
  // Convert the bytes into a hash.
33
45
  const hash = hash160(bytes)
34
- // Encode the address.
35
- return encode_p2wpkh_address(hash, network)
46
+ // Return the script.
47
+ return encode_p2wpkh_script(hash)
48
+ }
49
+
50
+ function encode_p2wpkh_script (pk_hash : Bytes) : Buff {
51
+ return Buff.join([ '0014', pk_hash ])
36
52
  }
37
53
 
38
54
  function encode_p2wpkh_address (
39
- pk_hash : string | Uint8Array,
40
- network : ChainNetwork = 'main',
55
+ script_pk : Bytes,
56
+ network : ChainNetwork = 'main',
41
57
  ) : string {
42
- // Convert the public key hash into bytes.
43
- const bytes = Buff.bytes(pk_hash)
58
+ // Get the public key hash from the script.
59
+ const pk_hash = decode_p2wpkh_script(script_pk)
44
60
  // Get the address configuration.
45
- const config = get_address_config(network, ADDR_TYPE)
61
+ const config = get_address_config(network, ADDRESS_TYPE)
46
62
  // Assert the configuration exists.
47
- Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}` )
63
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
48
64
  // Assert the payload size is correct.
49
- Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}` )
65
+ Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}` )
50
66
  // Encode the address.
51
67
  return encode_address({
52
- data : bytes,
68
+ data : pk_hash,
53
69
  format : 'bech32',
54
70
  prefix : config.prefix
55
71
  })
@@ -57,11 +73,22 @@ function encode_p2wpkh_address (
57
73
 
58
74
  function decode_p2wpkh_address (
59
75
  address : string
60
- ) : AddressData {
76
+ ) : AddressInfo {
61
77
  // Parse the address.
62
- const parsed = parse_address(address)
78
+ const parsed = get_address_info(address)
63
79
  // Assert the address type is correct.
64
- Assert.ok(parsed.type === 'p2w-pkh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`)
80
+ Assert.ok(parsed.type === 'p2wpkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
65
81
  // Return the parsed address.
66
82
  return parsed
67
83
  }
84
+
85
+ function decode_p2wpkh_script (
86
+ script : Bytes,
87
+ ) : Buff {
88
+ // Assert the script is a p2wpkh script.
89
+ Assert.ok(is_p2wpkh_script(script), `invalid p2wpkh script`)
90
+ // Convert the script into bytes.
91
+ const bytes = Buff.bytes(script)
92
+ // Return the public key hash from the script.
93
+ return bytes.slice(2, 22)
94
+ }
@@ -1,53 +1,69 @@
1
- import { Buff } from '@vbyte/buff'
2
- import { Assert } from '@vbyte/micro-lib'
3
- import { sha256 } from '@vbyte/micro-lib/hash'
4
- import { encode_address } from './encode.js'
1
+ import { Buff, Bytes } from '@vbyte/buff'
2
+ import { Assert } from '@vbyte/micro-lib'
3
+ import { sha256 } from '@vbyte/micro-lib/hash'
4
+ import { encode_address } from './encode.js'
5
+ import { is_p2wsh_script } from '@/lib/script/lock.js'
6
+ import { LOCK_SCRIPT_TYPE } from '@/const.js'
5
7
 
6
8
  import {
7
9
  get_address_config,
8
- parse_address
10
+ get_address_info
9
11
  } from './util.js'
10
12
 
11
13
  import type {
12
- ChainNetwork,
13
- AddressData
14
+ AddressInfo,
15
+ ChainNetwork
14
16
  } from '@/types/index.js'
15
17
 
16
- const ADDR_TYPE = 'p2w-sh'
18
+ const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WSH
17
19
 
18
20
  export namespace P2WSH {
19
- export const create = create_p2wsh_address
20
- export const encode = encode_p2wsh_address
21
- export const decode = decode_p2wsh_address
21
+ export const create_address = create_p2wsh_address
22
+ export const create_script = create_p2wsh_script
23
+ export const encode_address = encode_p2wsh_address
24
+ export const encode_script = encode_p2wsh_script
25
+ export const decode_address = decode_p2wsh_address
26
+ export const decode_script = decode_p2wsh_script
22
27
  }
23
28
 
24
29
  function create_p2wsh_address (
25
- script : string | Uint8Array,
30
+ script : Bytes,
26
31
  network : ChainNetwork = 'main',
27
32
  ) : string {
33
+ // Create the p2wsh script.
34
+ const wsh_script = create_p2wsh_script(script)
35
+ // Encode the script as an address.
36
+ return encode_p2wsh_address(wsh_script, network)
37
+ }
38
+
39
+ function create_p2wsh_script (script : Bytes) : Buff {
28
40
  // Convert the script into bytes.
29
41
  const bytes = Buff.bytes(script)
30
42
  // Convert the bytes into a hash.
31
- const hash = sha256(bytes)
32
- // Encode the address.
33
- return encode_p2wsh_address(hash, network)
43
+ const hash = sha256(bytes)
44
+ // Return the script.
45
+ return encode_p2wsh_script(hash)
46
+ }
47
+
48
+ function encode_p2wsh_script (script_hash : Bytes) : Buff {
49
+ return Buff.join([ '0020', script_hash ])
34
50
  }
35
51
 
36
52
  function encode_p2wsh_address (
37
- script_hash : string | Uint8Array,
38
- network : ChainNetwork = 'main',
53
+ script_pk : Bytes,
54
+ network : ChainNetwork = 'main',
39
55
  ) : string {
40
- // Convert the script hash into bytes.
41
- const bytes = Buff.bytes(script_hash)
56
+ // Get the script hash from the script.
57
+ const script_hash = decode_p2wsh_script(script_pk)
42
58
  // Get the address configuration.
43
- const config = get_address_config(network, ADDR_TYPE)
59
+ const config = get_address_config(network, ADDRESS_TYPE)
44
60
  // Assert the configuration exists.
45
- Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}` )
61
+ Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
46
62
  // Assert the payload size is correct.
47
- Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}` )
63
+ Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}` )
48
64
  // Encode the address.
49
65
  return encode_address({
50
- data : bytes,
66
+ data : script_hash,
51
67
  format : 'bech32',
52
68
  prefix : config.prefix
53
69
  })
@@ -55,11 +71,22 @@ function encode_p2wsh_address (
55
71
 
56
72
  function decode_p2wsh_address (
57
73
  address : string
58
- ) : AddressData {
74
+ ) : AddressInfo {
59
75
  // Parse the address.
60
- const parsed = parse_address(address)
76
+ const parsed = get_address_info(address)
61
77
  // Assert the address type is correct.
62
- Assert.ok(parsed.type === 'p2w-sh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`)
78
+ Assert.ok(parsed.type === 'p2wsh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
63
79
  // Return the parsed address.
64
80
  return parsed
65
81
  }
82
+
83
+ function decode_p2wsh_script (
84
+ script : Bytes,
85
+ ) : Buff {
86
+ // Assert the script is a p2wsh script.
87
+ Assert.ok(is_p2wsh_script(script), `invalid p2wsh script`)
88
+ // Convert the script into bytes.
89
+ const bytes = Buff.bytes(script)
90
+ // Return the script hash from the script.
91
+ return bytes.slice(2, 34)
92
+ }
@@ -1,4 +1,4 @@
1
- import type { AddressType } from '@/types/index.js'
1
+ import type { AddressType, ScriptInfo } from '@/types/index.js'
2
2
 
3
3
  /**
4
4
  * Get the address script.
@@ -10,15 +10,15 @@ import type { AddressType } from '@/types/index.js'
10
10
  export function get_address_script (
11
11
  script_key : string,
12
12
  script_type : AddressType
13
- ) {
13
+ ) : ScriptInfo {
14
14
  switch (script_type) {
15
15
  case 'p2pkh':
16
16
  return get_p2pkh_script(script_key)
17
17
  case 'p2sh':
18
18
  return get_p2sh_script(script_key)
19
- case 'p2w-pkh':
19
+ case 'p2wpkh':
20
20
  return get_p2w_pkh_script(script_key)
21
- case 'p2w-sh':
21
+ case 'p2wsh':
22
22
  return get_p2w_sh_script(script_key)
23
23
  case 'p2tr':
24
24
  return get_p2tr_script(script_key)
@@ -29,35 +29,35 @@ export function get_address_script (
29
29
 
30
30
  function get_p2pkh_script (script_key : string) {
31
31
  return {
32
- script_hex : '76a914' + script_key + '88ac',
33
- script_asm : [ 'OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG' ]
32
+ hex : '76a914' + script_key + '88ac',
33
+ asm : [ 'OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG' ]
34
34
  }
35
35
  }
36
36
 
37
37
  function get_p2sh_script (script_key : string) {
38
38
  return {
39
- script_hex : 'a914' + script_key + '87',
40
- script_asm : [ 'OP_HASH160', script_key, 'OP_EQUAL' ]
39
+ hex : 'a914' + script_key + '87',
40
+ asm : [ 'OP_HASH160', script_key, 'OP_EQUAL' ]
41
41
  }
42
42
  }
43
43
 
44
44
  function get_p2w_pkh_script (script_key : string) {
45
45
  return {
46
- script_hex : '0014' + script_key,
47
- script_asm : [ 'OP_0', script_key ]
46
+ hex : '0014' + script_key,
47
+ asm : [ 'OP_0', script_key ]
48
48
  }
49
49
  }
50
50
 
51
51
  function get_p2w_sh_script (script_key : string) {
52
52
  return {
53
- script_hex : '0020' + script_key,
54
- script_asm : [ 'OP_0', script_key ]
53
+ hex : '0020' + script_key,
54
+ asm : [ 'OP_0', script_key ]
55
55
  }
56
56
  }
57
57
 
58
58
  function get_p2tr_script (script_key : string) {
59
59
  return {
60
- script_hex : '5120' + script_key,
61
- script_asm : [ 'OP_1', script_key ]
60
+ hex : '5120' + script_key,
61
+ asm : [ 'OP_1', script_key ]
62
62
  }
63
63
  }
@@ -5,10 +5,9 @@ import { get_address_script } from './script.js'
5
5
  import type {
6
6
  AddressConfig,
7
7
  AddressConfigEntry,
8
- AddressType,
8
+ AddressInfo,
9
9
  ChainNetwork,
10
- AddressContext,
11
- AddressData
10
+ LockScriptType,
12
11
  } from '@/types/index.js'
13
12
 
14
13
  const CONFIG_TABLE : AddressConfigEntry[] = [
@@ -20,12 +19,12 @@ const CONFIG_TABLE : AddressConfigEntry[] = [
20
19
  [ 'm', 'p2pkh', 'regtest', 20, 'base58', 0x6F ],
21
20
  [ 'n', 'p2pkh', 'regtest', 20, 'base58', 0x6F ],
22
21
  [ '2', 'p2sh', 'regtest', 20, 'base58', 0xC4 ],
23
- [ 'bc', 'p2w-pkh', 'main', 20, 'bech32', 0 ],
24
- [ 'tb', 'p2w-pkh', 'testnet', 20, 'bech32', 0 ],
25
- [ 'bcrt', 'p2w-pkh', 'regtest', 20, 'bech32', 0 ],
26
- [ 'bc', 'p2w-sh', 'main', 32, 'bech32', 0 ],
27
- [ 'tb', 'p2w-sh', 'testnet', 32, 'bech32', 0 ],
28
- [ 'bcrt', 'p2w-sh', 'regtest', 32, 'bech32', 0 ],
22
+ [ 'bc', 'p2wpkh', 'main', 20, 'bech32', 0 ],
23
+ [ 'tb', 'p2wpkh', 'testnet', 20, 'bech32', 0 ],
24
+ [ 'bcrt', 'p2wpkh', 'regtest', 20, 'bech32', 0 ],
25
+ [ 'bc', 'p2wsh', 'main', 32, 'bech32', 0 ],
26
+ [ 'tb', 'p2wsh', 'testnet', 32, 'bech32', 0 ],
27
+ [ 'bcrt', 'p2wsh', 'regtest', 32, 'bech32', 0 ],
29
28
  [ 'bc', 'p2tr', 'main', 32, 'bech32m', 1 ],
30
29
  [ 'tb', 'p2tr', 'testnet', 32, 'bech32m', 1 ],
31
30
  [ 'bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1 ]
@@ -40,7 +39,7 @@ const CONFIG_TABLE : AddressConfigEntry[] = [
40
39
  */
41
40
  export function get_address_config (
42
41
  address_network : ChainNetwork,
43
- address_type : AddressType
42
+ address_type : LockScriptType
44
43
  ) : AddressConfig | null {
45
44
  // For each configuration in the table,
46
45
  for (const [ prefix, type, network, size, format, version ] of CONFIG_TABLE) {
@@ -55,12 +54,12 @@ export function get_address_config (
55
54
  }
56
55
 
57
56
  /**
58
- * Get the address context.
57
+ * Parse an address into its data and script.
59
58
  *
60
- * @param address - The address to get the context for.
61
- * @returns The address context.
59
+ * @param address - The address to parse.
60
+ * @returns The address data and script.
62
61
  */
63
- export function get_address_ctx (address : string) : AddressContext {
62
+ export function get_address_info (address : string) : AddressInfo {
64
63
  // Decode the address.
65
64
  const dec = decode_address(address)
66
65
  // For each configuration in the table,
@@ -78,25 +77,11 @@ export function get_address_ctx (address : string) : AddressContext {
78
77
  }
79
78
 
80
79
  // Convert the decoded data into a hex string.
81
- const hex = Buff.uint(dec.data).hex
80
+ const data = Buff.uint(dec.data).hex
81
+ const script = get_address_script(data, type)
82
82
  // Return the address configuration and data.
83
- return { data: dec.data, hex, type, prefix, network, size, format, version }
83
+ return { data, script,type, prefix, network, size, format, version }
84
84
  }
85
85
  // Otherwise, throw an error
86
86
  throw new Error('address configuration is invalid')
87
87
  }
88
-
89
- /**
90
- * Parse an address into its data and script.
91
- *
92
- * @param address - The address to parse.
93
- * @returns The address data and script.
94
- */
95
- export function parse_address (address : string) : AddressData {
96
- // Get the address context.
97
- const ctx = get_address_ctx(address)
98
- // Get the address script.
99
- const script = get_address_script(ctx.hex, ctx.type)
100
- // Return the address data.
101
- return { ...ctx, ...script }
102
- }
@@ -1,4 +1,4 @@
1
- import { Bytes, Stream } from '@vbyte/buff'
1
+ import { Buff, Bytes, Stream } from '@vbyte/buff'
2
2
 
3
3
  import {
4
4
  get_op_code,
@@ -6,6 +6,16 @@ import {
6
6
  is_valid_op
7
7
  } from './words.js'
8
8
 
9
+ import type { ScriptInfo } from '@/types/script.js'
10
+
11
+ export function parse_script (script: Bytes): ScriptInfo {
12
+ const bytes = Buff.bytes(script)
13
+ return {
14
+ asm: decode_script(bytes),
15
+ hex: bytes.hex
16
+ }
17
+ }
18
+
9
19
  /**
10
20
  * Decode a bitcoin script into asm instructions.
11
21
  */
@@ -1,26 +1,20 @@
1
- import { OPCODE_MAP } from './words.js'
2
1
  import { encode_script } from './encode.js'
3
2
 
4
3
  import {
5
4
  decode_script,
6
- is_valid_script
5
+ is_valid_script,
6
+ parse_script
7
7
  } from './decode.js'
8
8
 
9
- import {
10
- parse_script_pubkeys,
11
- prefix_script_size
12
- } from './util.js'
13
-
14
9
  export * from './decode.js'
15
10
  export * from './encode.js'
11
+ export * from './lock.js'
16
12
  export * from './util.js'
17
13
  export * from './words.js'
18
14
 
19
15
  export namespace ScriptUtil {
20
- export const prefix_size = prefix_script_size
21
- export const decode = decode_script
22
- export const encode = encode_script
23
- export const is_valid = is_valid_script
24
- export const get_pubkeys = parse_script_pubkeys
25
- export const OPCODES = OPCODE_MAP
16
+ export const parse = parse_script
17
+ export const decode = decode_script
18
+ export const encode = encode_script
19
+ export const is_valid = is_valid_script
26
20
  }
@@ -0,0 +1,73 @@
1
+ import { Buff, Bytes } from '@vbyte/buff'
2
+ import { LOCK_SCRIPT_REGEX } from '@/const.js'
3
+
4
+ import type {
5
+ LockScriptInfo,
6
+ LockScriptType,
7
+ WitnessVersion
8
+ } from '@/types/index.js'
9
+
10
+ export function is_return_script (script : Bytes) : boolean {
11
+ const bytes = Buff.bytes(script)
12
+ return bytes.at(0) === 0x6a
13
+ }
14
+
15
+ export function get_lock_script_info (script : Bytes) : LockScriptInfo {
16
+ return {
17
+ type : get_lock_script_type(script),
18
+ version : get_lock_script_version(script)
19
+ }
20
+ }
21
+
22
+ export function get_lock_script_type (script : Bytes) : LockScriptType | null {
23
+ // Get the hex string of the script.
24
+ const hex = Buff.bytes(script).hex
25
+ // Iterate over the lock script regexes.
26
+ for (const [ type, regex ] of Object.entries(LOCK_SCRIPT_REGEX)) {
27
+ // If the script matches the regex, return the type.
28
+ if (regex.test(hex)) return type as LockScriptType
29
+ }
30
+ // If the script does not match any regex, return null.
31
+ return null
32
+ }
33
+
34
+ export function get_lock_script_version (script : Bytes) : WitnessVersion | null {
35
+ // Get the version of the script.
36
+ const version = Buff.bytes(script)
37
+ // Return the version of the script.
38
+ switch (version.at(0)) {
39
+ case 0x00 : return 0
40
+ case 0x51 : return 1
41
+ default : return null
42
+ }
43
+ }
44
+
45
+ export function is_p2pkh_script (script : Bytes) : boolean {
46
+ const hex = Buff.bytes(script).hex
47
+ return LOCK_SCRIPT_REGEX['p2pkh'].test(hex)
48
+ }
49
+
50
+ export function is_p2sh_script (script : Bytes) : boolean {
51
+ const hex = Buff.bytes(script).hex
52
+ return LOCK_SCRIPT_REGEX['p2sh'].test(hex)
53
+ }
54
+
55
+ export function is_p2wpkh_script (script : Bytes) : boolean {
56
+ const hex = Buff.bytes(script).hex
57
+ return LOCK_SCRIPT_REGEX['p2wpkh'].test(hex)
58
+ }
59
+
60
+ export function is_p2wsh_script (script : Bytes) : boolean {
61
+ const hex = Buff.bytes(script).hex
62
+ return LOCK_SCRIPT_REGEX['p2wsh'].test(hex)
63
+ }
64
+
65
+ export function is_p2tr_script (script : Bytes) : boolean {
66
+ const hex = Buff.bytes(script).hex
67
+ return LOCK_SCRIPT_REGEX['p2tr'].test(hex)
68
+ }
69
+
70
+ export function is_opreturn_script (script : Bytes) : boolean {
71
+ const hex = Buff.bytes(script).hex
72
+ return LOCK_SCRIPT_REGEX['opreturn'].test(hex)
73
+ }