@vbyte/btc-dev 1.1.8 → 2.1.0

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 (181) hide show
  1. package/CHANGELOG.md +127 -0
  2. package/LICENSE +21 -121
  3. package/README.md +69 -3
  4. package/dist/const.d.ts +3 -0
  5. package/dist/const.js +23 -22
  6. package/dist/error.d.ts +11 -0
  7. package/dist/error.js +20 -0
  8. package/dist/index.d.ts +12 -11
  9. package/dist/index.js +11 -10
  10. package/dist/lib/address/api.d.ts +2 -2
  11. package/dist/lib/address/api.js +13 -12
  12. package/dist/lib/address/encode.d.ts +1 -1
  13. package/dist/lib/address/encode.js +26 -24
  14. package/dist/lib/address/index.d.ts +6 -6
  15. package/dist/lib/address/index.js +6 -6
  16. package/dist/lib/address/p2pkh.d.ts +2 -2
  17. package/dist/lib/address/p2pkh.js +15 -15
  18. package/dist/lib/address/p2sh.d.ts +2 -2
  19. package/dist/lib/address/p2sh.js +14 -14
  20. package/dist/lib/address/p2tr.d.ts +2 -2
  21. package/dist/lib/address/p2tr.js +14 -14
  22. package/dist/lib/address/p2wpkh.d.ts +2 -2
  23. package/dist/lib/address/p2wpkh.js +15 -15
  24. package/dist/lib/address/p2wsh.d.ts +2 -2
  25. package/dist/lib/address/p2wsh.js +14 -14
  26. package/dist/lib/address/script.d.ts +1 -1
  27. package/dist/lib/address/script.js +16 -16
  28. package/dist/lib/address/util.d.ts +1 -1
  29. package/dist/lib/address/util.js +24 -22
  30. package/dist/lib/meta/index.d.ts +4 -4
  31. package/dist/lib/meta/index.js +4 -4
  32. package/dist/lib/meta/locktime.d.ts +1 -1
  33. package/dist/lib/meta/locktime.js +13 -12
  34. package/dist/lib/meta/ref.js +13 -9
  35. package/dist/lib/meta/scribe.d.ts +2 -2
  36. package/dist/lib/meta/scribe.js +71 -56
  37. package/dist/lib/meta/sequence.d.ts +1 -1
  38. package/dist/lib/meta/sequence.js +21 -19
  39. package/dist/lib/script/decode.d.ts +2 -2
  40. package/dist/lib/script/decode.js +53 -17
  41. package/dist/lib/script/encode.d.ts +1 -1
  42. package/dist/lib/script/encode.js +21 -16
  43. package/dist/lib/script/index.d.ts +5 -13
  44. package/dist/lib/script/index.js +5 -14
  45. package/dist/lib/script/lock.d.ts +2 -2
  46. package/dist/lib/script/lock.js +15 -12
  47. package/dist/lib/script/util.js +4 -4
  48. package/dist/lib/script/words.js +131 -130
  49. package/dist/lib/sighash/index.d.ts +3 -3
  50. package/dist/lib/sighash/index.js +3 -3
  51. package/dist/lib/sighash/segwit.d.ts +2 -2
  52. package/dist/lib/sighash/segwit.js +18 -14
  53. package/dist/lib/sighash/taproot.d.ts +2 -2
  54. package/dist/lib/sighash/taproot.js +27 -23
  55. package/dist/lib/sighash/util.d.ts +2 -2
  56. package/dist/lib/sighash/util.js +8 -7
  57. package/dist/lib/signer/index.d.ts +2 -2
  58. package/dist/lib/signer/index.js +2 -2
  59. package/dist/lib/signer/sign.d.ts +1 -1
  60. package/dist/lib/signer/sign.js +43 -7
  61. package/dist/lib/signer/verify.d.ts +17 -3
  62. package/dist/lib/signer/verify.js +232 -3
  63. package/dist/lib/taproot/cblock.d.ts +1 -1
  64. package/dist/lib/taproot/cblock.js +16 -17
  65. package/dist/lib/taproot/encode.d.ts +1 -1
  66. package/dist/lib/taproot/encode.js +9 -8
  67. package/dist/lib/taproot/index.d.ts +4 -4
  68. package/dist/lib/taproot/index.js +4 -4
  69. package/dist/lib/taproot/parse.d.ts +1 -1
  70. package/dist/lib/taproot/parse.js +15 -15
  71. package/dist/lib/taproot/tree.d.ts +2 -2
  72. package/dist/lib/taproot/tree.js +12 -7
  73. package/dist/lib/tx/create.d.ts +1 -1
  74. package/dist/lib/tx/create.js +28 -12
  75. package/dist/lib/tx/decode.d.ts +2 -2
  76. package/dist/lib/tx/decode.js +52 -17
  77. package/dist/lib/tx/encode.d.ts +2 -2
  78. package/dist/lib/tx/encode.js +13 -16
  79. package/dist/lib/tx/index.d.ts +7 -7
  80. package/dist/lib/tx/index.js +7 -7
  81. package/dist/lib/tx/parse.d.ts +1 -1
  82. package/dist/lib/tx/parse.js +9 -9
  83. package/dist/lib/tx/size.d.ts +2 -2
  84. package/dist/lib/tx/size.js +9 -11
  85. package/dist/lib/tx/util.d.ts +2 -2
  86. package/dist/lib/tx/util.js +22 -20
  87. package/dist/lib/tx/validate.d.ts +1 -1
  88. package/dist/lib/tx/validate.js +37 -9
  89. package/dist/lib/witness/index.d.ts +2 -2
  90. package/dist/lib/witness/index.js +2 -2
  91. package/dist/lib/witness/parse.d.ts +2 -2
  92. package/dist/lib/witness/parse.js +24 -23
  93. package/dist/lib/witness/util.d.ts +2 -2
  94. package/dist/lib/witness/util.js +5 -5
  95. package/dist/main.cjs +3305 -2035
  96. package/dist/main.cjs.map +1 -1
  97. package/dist/module.mjs +3303 -2036
  98. package/dist/module.mjs.map +1 -1
  99. package/dist/package.json +24 -19
  100. package/dist/schema/base.d.ts +1 -1
  101. package/dist/schema/base.js +17 -13
  102. package/dist/schema/index.d.ts +2 -2
  103. package/dist/schema/index.js +2 -2
  104. package/dist/schema/taproot.d.ts +1 -1
  105. package/dist/schema/taproot.js +2 -2
  106. package/dist/schema/tx.d.ts +1 -1
  107. package/dist/schema/tx.js +4 -4
  108. package/dist/script.js +10 -12
  109. package/dist/script.js.map +1 -1
  110. package/dist/types/address.d.ts +4 -4
  111. package/dist/types/index.d.ts +8 -8
  112. package/dist/types/index.js +8 -8
  113. package/dist/types/meta.d.ts +4 -4
  114. package/dist/types/psbt.d.ts +2 -2
  115. package/dist/types/script.d.ts +2 -2
  116. package/dist/types/sighash.d.ts +2 -2
  117. package/dist/types/witness.d.ts +5 -5
  118. package/docs/API.md +1145 -0
  119. package/docs/CONVENTIONS.md +316 -0
  120. package/docs/FAQ.md +396 -0
  121. package/docs/GUIDE.md +1102 -0
  122. package/package.json +24 -19
  123. package/src/const.ts +0 -61
  124. package/src/index.ts +0 -13
  125. package/src/lib/address/api.ts +0 -50
  126. package/src/lib/address/encode.ts +0 -183
  127. package/src/lib/address/index.ts +0 -7
  128. package/src/lib/address/p2pkh.ts +0 -94
  129. package/src/lib/address/p2sh.ts +0 -96
  130. package/src/lib/address/p2tr.ts +0 -91
  131. package/src/lib/address/p2wpkh.ts +0 -94
  132. package/src/lib/address/p2wsh.ts +0 -92
  133. package/src/lib/address/script.ts +0 -63
  134. package/src/lib/address/util.ts +0 -87
  135. package/src/lib/meta/index.ts +0 -4
  136. package/src/lib/meta/locktime.ts +0 -57
  137. package/src/lib/meta/ref.ts +0 -107
  138. package/src/lib/meta/scribe.ts +0 -256
  139. package/src/lib/meta/sequence.ts +0 -146
  140. package/src/lib/script/decode.ts +0 -85
  141. package/src/lib/script/encode.ts +0 -129
  142. package/src/lib/script/index.ts +0 -20
  143. package/src/lib/script/lock.ts +0 -73
  144. package/src/lib/script/util.ts +0 -78
  145. package/src/lib/script/words.ts +0 -182
  146. package/src/lib/sighash/index.ts +0 -3
  147. package/src/lib/sighash/segwit.ts +0 -152
  148. package/src/lib/sighash/taproot.ts +0 -206
  149. package/src/lib/sighash/util.ts +0 -51
  150. package/src/lib/signer/index.ts +0 -2
  151. package/src/lib/signer/sign.ts +0 -39
  152. package/src/lib/signer/verify.ts +0 -88
  153. package/src/lib/taproot/cblock.ts +0 -96
  154. package/src/lib/taproot/encode.ts +0 -49
  155. package/src/lib/taproot/index.ts +0 -4
  156. package/src/lib/taproot/parse.ts +0 -65
  157. package/src/lib/taproot/tree.ts +0 -94
  158. package/src/lib/tx/create.ts +0 -90
  159. package/src/lib/tx/decode.ts +0 -123
  160. package/src/lib/tx/encode.ts +0 -155
  161. package/src/lib/tx/index.ts +0 -7
  162. package/src/lib/tx/parse.ts +0 -69
  163. package/src/lib/tx/size.ts +0 -68
  164. package/src/lib/tx/util.ts +0 -111
  165. package/src/lib/tx/validate.ts +0 -49
  166. package/src/lib/witness/index.ts +0 -2
  167. package/src/lib/witness/parse.ts +0 -127
  168. package/src/lib/witness/util.ts +0 -18
  169. package/src/schema/base.ts +0 -57
  170. package/src/schema/index.ts +0 -2
  171. package/src/schema/taproot.ts +0 -12
  172. package/src/schema/tx.ts +0 -48
  173. package/src/types/address.ts +0 -35
  174. package/src/types/index.ts +0 -8
  175. package/src/types/meta.ts +0 -48
  176. package/src/types/psbt.ts +0 -15
  177. package/src/types/script.ts +0 -18
  178. package/src/types/sighash.ts +0 -16
  179. package/src/types/taproot.ts +0 -41
  180. package/src/types/txdata.ts +0 -85
  181. package/src/types/witness.ts +0 -42
@@ -1,94 +0,0 @@
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'
7
-
8
- import {
9
- get_address_config,
10
- get_address_info
11
- } from './util.js'
12
-
13
- import type {
14
- AddressInfo,
15
- ChainNetwork
16
- } from '@/types/index.js'
17
-
18
- const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WPKH
19
-
20
- export namespace P2WPKH {
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
27
- }
28
-
29
- function create_p2wpkh_address (
30
- pubkey : Bytes,
31
- network : ChainNetwork = 'main',
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 {
40
- // Convert the public key into bytes.
41
- const bytes = Buff.bytes(pubkey)
42
- // Assert the public key is 33 bytes.
43
- Assert.size(bytes, 33, 'invalid pubkey size')
44
- // Convert the bytes into a hash.
45
- const hash = hash160(bytes)
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 ])
52
- }
53
-
54
- function encode_p2wpkh_address (
55
- script_pk : Bytes,
56
- network : ChainNetwork = 'main',
57
- ) : string {
58
- // Get the public key hash from the script.
59
- const pk_hash = decode_p2wpkh_script(script_pk)
60
- // Get the address configuration.
61
- const config = get_address_config(network, ADDRESS_TYPE)
62
- // Assert the configuration exists.
63
- Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
64
- // Assert the payload size is correct.
65
- Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}` )
66
- // Encode the address.
67
- return encode_address({
68
- data : pk_hash,
69
- format : 'bech32',
70
- prefix : config.prefix
71
- })
72
- }
73
-
74
- function decode_p2wpkh_address (
75
- address : string
76
- ) : AddressInfo {
77
- // Parse the address.
78
- const parsed = get_address_info(address)
79
- // Assert the address type is correct.
80
- Assert.ok(parsed.type === 'p2wpkh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
81
- // Return the parsed address.
82
- return parsed
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,92 +0,0 @@
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'
7
-
8
- import {
9
- get_address_config,
10
- get_address_info
11
- } from './util.js'
12
-
13
- import type {
14
- AddressInfo,
15
- ChainNetwork
16
- } from '@/types/index.js'
17
-
18
- const ADDRESS_TYPE = LOCK_SCRIPT_TYPE.P2WSH
19
-
20
- export namespace P2WSH {
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
27
- }
28
-
29
- function create_p2wsh_address (
30
- script : Bytes,
31
- network : ChainNetwork = 'main',
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 {
40
- // Convert the script into bytes.
41
- const bytes = Buff.bytes(script)
42
- // Convert the bytes into a hash.
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 ])
50
- }
51
-
52
- function encode_p2wsh_address (
53
- script_pk : Bytes,
54
- network : ChainNetwork = 'main',
55
- ) : string {
56
- // Get the script hash from the script.
57
- const script_hash = decode_p2wsh_script(script_pk)
58
- // Get the address configuration.
59
- const config = get_address_config(network, ADDRESS_TYPE)
60
- // Assert the configuration exists.
61
- Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}` )
62
- // Assert the payload size is correct.
63
- Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}` )
64
- // Encode the address.
65
- return encode_address({
66
- data : script_hash,
67
- format : 'bech32',
68
- prefix : config.prefix
69
- })
70
- }
71
-
72
- function decode_p2wsh_address (
73
- address : string
74
- ) : AddressInfo {
75
- // Parse the address.
76
- const parsed = get_address_info(address)
77
- // Assert the address type is correct.
78
- Assert.ok(parsed.type === 'p2wsh', `address type mismatch: ${parsed.type} !== ${ADDRESS_TYPE}`)
79
- // Return the parsed address.
80
- return parsed
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,63 +0,0 @@
1
- import type { AddressType, ScriptInfo } from '@/types/index.js'
2
-
3
- /**
4
- * Get the address script.
5
- *
6
- * @param script_key - The script key.
7
- * @param script_type - The script type.
8
- * @returns The address script.
9
- */
10
- export function get_address_script (
11
- script_key : string,
12
- script_type : AddressType
13
- ) : ScriptInfo {
14
- switch (script_type) {
15
- case 'p2pkh':
16
- return get_p2pkh_script(script_key)
17
- case 'p2sh':
18
- return get_p2sh_script(script_key)
19
- case 'p2wpkh':
20
- return get_p2w_pkh_script(script_key)
21
- case 'p2wsh':
22
- return get_p2w_sh_script(script_key)
23
- case 'p2tr':
24
- return get_p2tr_script(script_key)
25
- default:
26
- throw new Error('unrecognized script type: ' + script_type)
27
- }
28
- }
29
-
30
- function get_p2pkh_script (script_key : string) {
31
- return {
32
- hex : '76a914' + script_key + '88ac',
33
- asm : [ 'OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG' ]
34
- }
35
- }
36
-
37
- function get_p2sh_script (script_key : string) {
38
- return {
39
- hex : 'a914' + script_key + '87',
40
- asm : [ 'OP_HASH160', script_key, 'OP_EQUAL' ]
41
- }
42
- }
43
-
44
- function get_p2w_pkh_script (script_key : string) {
45
- return {
46
- hex : '0014' + script_key,
47
- asm : [ 'OP_0', script_key ]
48
- }
49
- }
50
-
51
- function get_p2w_sh_script (script_key : string) {
52
- return {
53
- hex : '0020' + script_key,
54
- asm : [ 'OP_0', script_key ]
55
- }
56
- }
57
-
58
- function get_p2tr_script (script_key : string) {
59
- return {
60
- hex : '5120' + script_key,
61
- asm : [ 'OP_1', script_key ]
62
- }
63
- }
@@ -1,87 +0,0 @@
1
- import { Buff } from '@vbyte/buff'
2
- import { decode_address } from './encode.js'
3
- import { get_address_script } from './script.js'
4
-
5
- import type {
6
- AddressConfig,
7
- AddressConfigEntry,
8
- AddressInfo,
9
- ChainNetwork,
10
- LockScriptType,
11
- } from '@/types/index.js'
12
-
13
- const CONFIG_TABLE : AddressConfigEntry[] = [
14
- [ '1', 'p2pkh', 'main', 20, 'base58', 0x00 ],
15
- [ '3', 'p2sh', 'main', 20, 'base58', 0x05 ],
16
- [ 'm', 'p2pkh', 'testnet', 20, 'base58', 0x6F ],
17
- [ 'n', 'p2pkh', 'testnet', 20, 'base58', 0x6F ],
18
- [ '2', 'p2sh', 'testnet', 20, 'base58', 0xC4 ],
19
- [ 'm', 'p2pkh', 'regtest', 20, 'base58', 0x6F ],
20
- [ 'n', 'p2pkh', 'regtest', 20, 'base58', 0x6F ],
21
- [ '2', 'p2sh', 'regtest', 20, 'base58', 0xC4 ],
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 ],
28
- [ 'bc', 'p2tr', 'main', 32, 'bech32m', 1 ],
29
- [ 'tb', 'p2tr', 'testnet', 32, 'bech32m', 1 ],
30
- [ 'bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1 ]
31
- ]
32
-
33
- /**
34
- * Lookup an address configuration by its type and network.
35
- *
36
- * @param address_network - The network of the address.
37
- * @param address_type - The type of the address.
38
- * @returns The address information, or null if the address is not recognized.
39
- */
40
- export function get_address_config (
41
- address_network : ChainNetwork,
42
- address_type : LockScriptType
43
- ) : AddressConfig | null {
44
- // For each configuration in the table,
45
- for (const [ prefix, type, network, size, format, version ] of CONFIG_TABLE) {
46
- // Check if the address matches the configuration
47
- if (type === address_type && network === address_network) {
48
- // Return the address configuration.
49
- return { type, prefix, network, size, format, version }
50
- }
51
- }
52
- // If no configuration matches the address, return null.
53
- return null
54
- }
55
-
56
- /**
57
- * Parse an address into its data and script.
58
- *
59
- * @param address - The address to parse.
60
- * @returns The address data and script.
61
- */
62
- export function get_address_info (address : string) : AddressInfo {
63
- // Decode the address.
64
- const dec = decode_address(address)
65
- // For each configuration in the table,
66
- for (const [ prefix, type, network, size, format, version ] of CONFIG_TABLE) {
67
-
68
- // Check if the address matches the configuration
69
- if (format !== dec.format) continue
70
- if (size !== dec.data.length) continue
71
- if (version !== dec.version) continue
72
-
73
- if (dec.prefix) {
74
- if (prefix !== dec.prefix) continue
75
- } else {
76
- if (!address.startsWith(prefix)) continue
77
- }
78
-
79
- // Convert the decoded data into a hex string.
80
- const data = Buff.uint(dec.data).hex
81
- const script = get_address_script(data, type)
82
- // Return the address configuration and data.
83
- return { data, script,type, prefix, network, size, format, version }
84
- }
85
- // Otherwise, throw an error
86
- throw new Error('address configuration is invalid')
87
- }
@@ -1,4 +0,0 @@
1
- export * from './locktime.js'
2
- export * from './ref.js'
3
- export * from './scribe.js'
4
- export * from './sequence.js'
@@ -1,57 +0,0 @@
1
- import { Assert } from '@vbyte/micro-lib'
2
-
3
- import type { LocktimeData } from '@/types/index.js'
4
-
5
- // The threshold between block height and timestamp.
6
- const LOCKTIME_THRESHOLD = 500000000
7
-
8
- export namespace LocktimeField {
9
- export const encode = encode_locktime
10
- export const decode = decode_locktime
11
- }
12
-
13
- /**
14
- * Encodes a LockTimeData object into a string representation.
15
- * According to BIP-65, the value is simply the numeric value as a string.
16
- */
17
- export function encode_locktime (
18
- locktime : LocktimeData
19
- ) : number {
20
- switch (locktime.type) {
21
- case 'timelock':
22
- Assert.ok(locktime.stamp >= LOCKTIME_THRESHOLD, 'Invalid timestamp')
23
- return locktime.stamp
24
- case 'heightlock':
25
- Assert.ok(locktime.height > 0, 'height must be greater than 0')
26
- Assert.ok(locktime.height < LOCKTIME_THRESHOLD, 'invalid block height')
27
- return locktime.height
28
- default:
29
- throw new Error('Invalid locktime type')
30
- }
31
- }
32
-
33
- /**
34
- * Parses a string or number into a LockTimeData object.
35
- * According to BIP-65, values below LOCKTIME_THRESHOLD are interpreted as block heights,
36
- * while values at or above this threshold are interpreted as timestamps.
37
- */
38
- export function decode_locktime (
39
- locktime : number
40
- ) : LocktimeData | null {
41
- // Check if the value is valid (non-negative)
42
- if (isNaN(locktime) || locktime <= 0) {
43
- return null
44
- }
45
- // Return the appropriate locktime type.
46
- if (locktime < LOCKTIME_THRESHOLD) {
47
- return {
48
- type : 'heightlock',
49
- height : locktime
50
- }
51
- } else {
52
- return {
53
- type : 'timelock',
54
- stamp : locktime
55
- }
56
- }
57
- }
@@ -1,107 +0,0 @@
1
- export namespace RefPointer {
2
- export const outpoint = {
3
- encode : encode_outpoint,
4
- decode : decode_outpoint,
5
- verify : verify_outpoint,
6
- assert : assert_outpoint,
7
- }
8
- export const record_id = {
9
- encode : encode_inscription_id,
10
- decode : decode_inscription_id,
11
- verify : verify_inscription_id,
12
- assert : assert_inscription_id,
13
- }
14
- export const rune_id = {
15
- encode : encode_rune_id,
16
- decode : decode_rune_id,
17
- verify : verify_rune_id,
18
- assert : assert_rune_id,
19
- }
20
- }
21
-
22
- function encode_inscription_id (
23
- txid : string,
24
- order : number = 0
25
- ) : string {
26
- return `${txid}i${order}`
27
- }
28
-
29
- function decode_inscription_id (
30
- inscription_id : string
31
- ) : { txid : string, order : number } {
32
- assert_inscription_id(inscription_id)
33
- const [ txid, order ] = inscription_id.split('i')
34
- return { txid, order : parseInt(order) }
35
- }
36
-
37
- function verify_inscription_id (
38
- inscription_id : string
39
- ) : boolean {
40
- return inscription_id.match(/^[a-f0-9]{64}i\d+$/) !== null
41
- }
42
-
43
- function assert_inscription_id (
44
- inscription_id : string
45
- ) : void {
46
- if (!verify_inscription_id(inscription_id)) {
47
- throw new Error(`invalid inscription id: ${inscription_id}`)
48
- }
49
- }
50
-
51
- function encode_rune_id (
52
- block_height : number,
53
- block_index : number
54
- ) : string {
55
- return `${block_height}:${block_index}`
56
- }
57
-
58
- function decode_rune_id (
59
- rune_id : string
60
- ) : { block_height : number, block_index : number } {
61
- assert_rune_id(rune_id)
62
- const [ block_height, block_index ] = rune_id.split(':')
63
- return { block_height : parseInt(block_height), block_index : parseInt(block_index) }
64
- }
65
-
66
- function verify_rune_id (
67
- rune_id : string
68
- ) : boolean {
69
- return rune_id.match(/^\d+:\d+$/) !== null
70
- }
71
-
72
- function assert_rune_id (
73
- rune_id : string
74
- ) : void {
75
- if (!verify_rune_id(rune_id)) {
76
- throw new Error(`invalid rune id: ${rune_id}`)
77
- }
78
- }
79
-
80
- function encode_outpoint (
81
- txid : string,
82
- vout : number
83
- ) : string {
84
- return `${txid}:${vout}`
85
- }
86
-
87
- function decode_outpoint (
88
- outpoint : string
89
- ) : { txid : string, vout : number } {
90
- assert_outpoint(outpoint)
91
- const [ txid, vout ] = outpoint.split(':')
92
- return { txid, vout : parseInt(vout) }
93
- }
94
-
95
- function verify_outpoint (
96
- outpoint : string
97
- ) : boolean {
98
- return outpoint.match(/^[a-f0-9]{64}:[0-9]+$/) !== null
99
- }
100
-
101
- function assert_outpoint (
102
- outpoint : string
103
- ) : void {
104
- if (!verify_outpoint(outpoint)) {
105
- throw new Error(`invalid outpoint: ${outpoint}`)
106
- }
107
- }