@vbyte/btc-dev 1.0.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 (205) hide show
  1. package/LICENSE +121 -0
  2. package/README.md +5 -0
  3. package/dist/class/index.d.ts +5 -0
  4. package/dist/class/index.js +5 -0
  5. package/dist/class/signer.d.ts +18 -0
  6. package/dist/class/signer.js +32 -0
  7. package/dist/class/tx.d.ts +38 -0
  8. package/dist/class/tx.js +73 -0
  9. package/dist/class/txin.d.ts +29 -0
  10. package/dist/class/txin.js +68 -0
  11. package/dist/class/txout.d.ts +18 -0
  12. package/dist/class/txout.js +38 -0
  13. package/dist/class/witness.d.ts +20 -0
  14. package/dist/class/witness.js +57 -0
  15. package/dist/const.d.ts +22 -0
  16. package/dist/const.js +36 -0
  17. package/dist/index.d.ts +11 -0
  18. package/dist/index.js +10 -0
  19. package/dist/lib/address/encode.d.ts +3 -0
  20. package/dist/lib/address/encode.js +79 -0
  21. package/dist/lib/address/index.d.ts +20 -0
  22. package/dist/lib/address/index.js +21 -0
  23. package/dist/lib/address/p2pkh.d.ts +10 -0
  24. package/dist/lib/address/p2pkh.js +33 -0
  25. package/dist/lib/address/p2sh.d.ts +10 -0
  26. package/dist/lib/address/p2sh.js +33 -0
  27. package/dist/lib/address/p2tr.d.ts +8 -0
  28. package/dist/lib/address/p2tr.js +26 -0
  29. package/dist/lib/address/p2wpkh.d.ts +10 -0
  30. package/dist/lib/address/p2wpkh.js +34 -0
  31. package/dist/lib/address/p2wsh.d.ts +10 -0
  32. package/dist/lib/address/p2wsh.js +33 -0
  33. package/dist/lib/address/script.d.ts +5 -0
  34. package/dist/lib/address/script.js +46 -0
  35. package/dist/lib/address/util.d.ts +4 -0
  36. package/dist/lib/address/util.js +57 -0
  37. package/dist/lib/meta/index.d.ts +2 -0
  38. package/dist/lib/meta/index.js +2 -0
  39. package/dist/lib/meta/pointer.d.ts +42 -0
  40. package/dist/lib/meta/pointer.js +69 -0
  41. package/dist/lib/meta/scribe.d.ts +7 -0
  42. package/dist/lib/meta/scribe.js +192 -0
  43. package/dist/lib/psbt/encoder.d.ts +5 -0
  44. package/dist/lib/psbt/encoder.js +21 -0
  45. package/dist/lib/psbt/index.d.ts +4 -0
  46. package/dist/lib/psbt/index.js +4 -0
  47. package/dist/lib/psbt/meta.d.ts +3 -0
  48. package/dist/lib/psbt/meta.js +11 -0
  49. package/dist/lib/psbt/util.d.ts +5 -0
  50. package/dist/lib/psbt/util.js +44 -0
  51. package/dist/lib/psbt/validate.d.ts +2 -0
  52. package/dist/lib/psbt/validate.js +11 -0
  53. package/dist/lib/script/decode.d.ts +2 -0
  54. package/dist/lib/script/decode.js +55 -0
  55. package/dist/lib/script/encode.d.ts +6 -0
  56. package/dist/lib/script/encode.js +80 -0
  57. package/dist/lib/script/index.d.ts +126 -0
  58. package/dist/lib/script/index.js +17 -0
  59. package/dist/lib/script/util.d.ts +2 -0
  60. package/dist/lib/script/util.js +10 -0
  61. package/dist/lib/script/words.d.ts +116 -0
  62. package/dist/lib/script/words.js +164 -0
  63. package/dist/lib/sighash/index.d.ts +5 -0
  64. package/dist/lib/sighash/index.js +5 -0
  65. package/dist/lib/sighash/segwit.d.ts +3 -0
  66. package/dist/lib/sighash/segwit.js +89 -0
  67. package/dist/lib/sighash/sign.d.ts +3 -0
  68. package/dist/lib/sighash/sign.js +20 -0
  69. package/dist/lib/sighash/taproot.d.ts +9 -0
  70. package/dist/lib/sighash/taproot.js +124 -0
  71. package/dist/lib/sighash/util.d.ts +3 -0
  72. package/dist/lib/sighash/util.js +16 -0
  73. package/dist/lib/sighash/verify.d.ts +1 -0
  74. package/dist/lib/sighash/verify.js +1 -0
  75. package/dist/lib/taproot/cblock.d.ts +3 -0
  76. package/dist/lib/taproot/cblock.js +55 -0
  77. package/dist/lib/taproot/encode.d.ts +6 -0
  78. package/dist/lib/taproot/encode.js +26 -0
  79. package/dist/lib/taproot/index.d.ts +4 -0
  80. package/dist/lib/taproot/index.js +4 -0
  81. package/dist/lib/taproot/parse.d.ts +11 -0
  82. package/dist/lib/taproot/parse.js +47 -0
  83. package/dist/lib/taproot/tree.d.ts +3 -0
  84. package/dist/lib/taproot/tree.js +49 -0
  85. package/dist/lib/tx/create.d.ts +6 -0
  86. package/dist/lib/tx/create.js +54 -0
  87. package/dist/lib/tx/decode.d.ts +9 -0
  88. package/dist/lib/tx/decode.js +109 -0
  89. package/dist/lib/tx/encode.d.ts +15 -0
  90. package/dist/lib/tx/encode.js +94 -0
  91. package/dist/lib/tx/index.d.ts +10 -0
  92. package/dist/lib/tx/index.js +10 -0
  93. package/dist/lib/tx/locktime.d.ts +7 -0
  94. package/dist/lib/tx/locktime.js +37 -0
  95. package/dist/lib/tx/meta.d.ts +8 -0
  96. package/dist/lib/tx/meta.js +48 -0
  97. package/dist/lib/tx/parse.d.ts +2 -0
  98. package/dist/lib/tx/parse.js +12 -0
  99. package/dist/lib/tx/sequence.d.ts +7 -0
  100. package/dist/lib/tx/sequence.js +65 -0
  101. package/dist/lib/tx/size.d.ts +10 -0
  102. package/dist/lib/tx/size.js +48 -0
  103. package/dist/lib/tx/validate.d.ts +7 -0
  104. package/dist/lib/tx/validate.js +21 -0
  105. package/dist/lib/tx/witness.d.ts +3 -0
  106. package/dist/lib/tx/witness.js +85 -0
  107. package/dist/main.cjs +14625 -0
  108. package/dist/main.cjs.map +1 -0
  109. package/dist/module.mjs +14610 -0
  110. package/dist/module.mjs.map +1 -0
  111. package/dist/package.json +106 -0
  112. package/dist/schema/index.d.ts +2 -0
  113. package/dist/schema/index.js +2 -0
  114. package/dist/schema/taproot.d.ts +18 -0
  115. package/dist/schema/taproot.js +9 -0
  116. package/dist/schema/tx.d.ts +278 -0
  117. package/dist/schema/tx.js +35 -0
  118. package/dist/script.js +15 -0
  119. package/dist/script.js.map +1 -0
  120. package/dist/types/address.d.ts +34 -0
  121. package/dist/types/address.js +1 -0
  122. package/dist/types/index.d.ts +9 -0
  123. package/dist/types/index.js +9 -0
  124. package/dist/types/meta.d.ts +10 -0
  125. package/dist/types/meta.js +1 -0
  126. package/dist/types/psbt.d.ts +9 -0
  127. package/dist/types/psbt.js +1 -0
  128. package/dist/types/sighash.d.ts +14 -0
  129. package/dist/types/sighash.js +1 -0
  130. package/dist/types/taproot.d.ts +35 -0
  131. package/dist/types/taproot.js +1 -0
  132. package/dist/types/transaction.d.ts +81 -0
  133. package/dist/types/transaction.js +1 -0
  134. package/dist/types/txdata.d.ts +45 -0
  135. package/dist/types/txdata.js +1 -0
  136. package/dist/types/txmeta.d.ts +19 -0
  137. package/dist/types/txmeta.js +1 -0
  138. package/dist/types/witness.d.ts +30 -0
  139. package/dist/types/witness.js +1 -0
  140. package/package.json +106 -0
  141. package/src/class/index.ts +5 -0
  142. package/src/class/signer.ts +47 -0
  143. package/src/class/tx.ts +118 -0
  144. package/src/class/txin.ts +95 -0
  145. package/src/class/txout.ts +57 -0
  146. package/src/class/witness.ts +85 -0
  147. package/src/const.ts +43 -0
  148. package/src/index.ts +14 -0
  149. package/src/lib/address/encode.ts +183 -0
  150. package/src/lib/address/index.ts +24 -0
  151. package/src/lib/address/p2pkh.ts +65 -0
  152. package/src/lib/address/p2sh.ts +65 -0
  153. package/src/lib/address/p2tr.ts +51 -0
  154. package/src/lib/address/p2wpkh.ts +67 -0
  155. package/src/lib/address/p2wsh.ts +65 -0
  156. package/src/lib/address/script.ts +63 -0
  157. package/src/lib/address/util.ts +102 -0
  158. package/src/lib/meta/index.ts +2 -0
  159. package/src/lib/meta/pointer.ts +107 -0
  160. package/src/lib/meta/scribe.ts +251 -0
  161. package/src/lib/psbt/encoder.ts +24 -0
  162. package/src/lib/psbt/index.ts +4 -0
  163. package/src/lib/psbt/meta.ts +15 -0
  164. package/src/lib/psbt/util.ts +62 -0
  165. package/src/lib/psbt/validate.ts +18 -0
  166. package/src/lib/script/decode.ts +75 -0
  167. package/src/lib/script/encode.ts +130 -0
  168. package/src/lib/script/index.ts +26 -0
  169. package/src/lib/script/util.ts +78 -0
  170. package/src/lib/script/words.ts +182 -0
  171. package/src/lib/sighash/index.ts +5 -0
  172. package/src/lib/sighash/segwit.ts +152 -0
  173. package/src/lib/sighash/sign.ts +35 -0
  174. package/src/lib/sighash/taproot.ts +236 -0
  175. package/src/lib/sighash/util.ts +29 -0
  176. package/src/lib/sighash/verify.ts +83 -0
  177. package/src/lib/taproot/cblock.ts +95 -0
  178. package/src/lib/taproot/encode.ts +49 -0
  179. package/src/lib/taproot/index.ts +4 -0
  180. package/src/lib/taproot/parse.ts +65 -0
  181. package/src/lib/taproot/tree.ts +94 -0
  182. package/src/lib/tx/create.ts +82 -0
  183. package/src/lib/tx/decode.ts +145 -0
  184. package/src/lib/tx/encode.ts +154 -0
  185. package/src/lib/tx/index.ts +10 -0
  186. package/src/lib/tx/locktime.ts +57 -0
  187. package/src/lib/tx/meta.ts +73 -0
  188. package/src/lib/tx/parse.ts +16 -0
  189. package/src/lib/tx/sequence.ts +146 -0
  190. package/src/lib/tx/size.ts +77 -0
  191. package/src/lib/tx/validate.ts +36 -0
  192. package/src/lib/tx/witness.ts +122 -0
  193. package/src/schema/index.ts +2 -0
  194. package/src/schema/taproot.ts +12 -0
  195. package/src/schema/tx.ts +42 -0
  196. package/src/types/address.ts +39 -0
  197. package/src/types/index.ts +9 -0
  198. package/src/types/meta.ts +10 -0
  199. package/src/types/psbt.ts +15 -0
  200. package/src/types/sighash.ts +16 -0
  201. package/src/types/taproot.ts +40 -0
  202. package/src/types/transaction.ts +98 -0
  203. package/src/types/txdata.ts +53 -0
  204. package/src/types/txmeta.ts +25 -0
  205. package/src/types/witness.ts +36 -0
@@ -0,0 +1,79 @@
1
+ import { Buff } from '@vbyte/buff';
2
+ import { Assert, B58chk, Bech32, Bech32m } from '@vbyte/micro-lib';
3
+ const ENCODING_REGEX = {
4
+ base58: /^[13mn2][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
5
+ bech32: /^(bc|tb|bcrt)1q[ac-hj-np-z02-9]{6,87}$/,
6
+ bech32m: /^(bc|tb|bcrt)1p[ac-hj-np-z02-9]{6,87}$/
7
+ };
8
+ const VERSION = {
9
+ bech32: 0,
10
+ bech32m: 1
11
+ };
12
+ export function decode_address(address) {
13
+ const format = get_address_format(address);
14
+ if (format === null)
15
+ throw new Error('unrecognized address format: ' + format);
16
+ if (format === 'base58')
17
+ return base58_decode(address);
18
+ if (format === 'bech32')
19
+ return bech32_decode(address);
20
+ if (format === 'bech32m')
21
+ return bech32m_decode(address);
22
+ throw new Error('unable to find a matching address configuration');
23
+ }
24
+ export function encode_address(config) {
25
+ if (config.format === 'base58')
26
+ return base58_encode(config);
27
+ if (config.format === 'bech32')
28
+ return bech32_encode(config);
29
+ if (config.format === 'bech32m')
30
+ return bech32m_encode(config);
31
+ throw new Error('unrecognized encoding format: ' + config.format);
32
+ }
33
+ function get_address_format(address) {
34
+ for (const [format, regex] of Object.entries(ENCODING_REGEX)) {
35
+ if (regex.test(address))
36
+ return format;
37
+ }
38
+ return null;
39
+ }
40
+ function base58_encode(config) {
41
+ Assert.ok(config.format === 'base58', 'encoding mismatch');
42
+ Assert.exists(config.version, 'must specify a version');
43
+ const bytes = Buff.join([config.version, config.data]);
44
+ return B58chk.encode(bytes);
45
+ }
46
+ function base58_decode(encoded) {
47
+ const bytes = B58chk.decode(encoded);
48
+ const data = bytes.slice(1);
49
+ const version = bytes[0];
50
+ return { data, format: 'base58', version };
51
+ }
52
+ function bech32_encode(config) {
53
+ Assert.ok(config.format === 'bech32', 'encoding mismatch');
54
+ Assert.exists(config.prefix, 'prefix is required');
55
+ const bytes = Buff.bytes(config.data);
56
+ const words = Bech32.to_words(bytes);
57
+ return Bech32.encode(config.prefix, [VERSION.bech32, ...words]);
58
+ }
59
+ function bech32_decode(encoded) {
60
+ const { prefix, words } = Bech32.decode(encoded);
61
+ const [version, ...rest] = words;
62
+ Assert.ok(version === VERSION.bech32, 'bech32 version mismatch');
63
+ const data = Bech32.to_bytes(rest);
64
+ return { data, format: 'bech32', prefix, version };
65
+ }
66
+ function bech32m_encode(config) {
67
+ Assert.ok(config.format === 'bech32m', 'encoding mismatch');
68
+ Assert.exists(config.prefix, 'prefix is required');
69
+ const bytes = Buff.bytes(config.data);
70
+ const words = Bech32m.to_words(bytes);
71
+ return Bech32m.encode(config.prefix, [VERSION.bech32m, ...words]);
72
+ }
73
+ function bech32m_decode(encoded) {
74
+ const { prefix, words } = Bech32m.decode(encoded);
75
+ const [version, ...rest] = words;
76
+ Assert.ok(version === VERSION.bech32m, 'bech32m version mismatch');
77
+ const data = Bech32m.to_bytes(rest);
78
+ return { data, format: 'bech32m', prefix, version };
79
+ }
@@ -0,0 +1,20 @@
1
+ import { P2PKH as P2PKH_TOOL } from './p2pkh.js';
2
+ import { P2SH as P2SH_TOOL } from './p2sh.js';
3
+ import { P2WPKH as P2WPKH_TOOL } from './p2wpkh.js';
4
+ import { P2WSH as P2WSH_TOOL } from './p2wsh.js';
5
+ import { P2TR as P2TR_TOOL } from './p2tr.js';
6
+ import { parse_address } from './util.js';
7
+ export { P2PKH } from './p2pkh.js';
8
+ export { P2SH } from './p2sh.js';
9
+ export { P2WPKH } from './p2wpkh.js';
10
+ export { P2WSH } from './p2wsh.js';
11
+ export { P2TR } from './p2tr.js';
12
+ export { parse_address } from './util.js';
13
+ export declare namespace AddressTool {
14
+ const P2PKH: typeof P2PKH_TOOL;
15
+ const P2SH: typeof P2SH_TOOL;
16
+ const P2WPKH: typeof P2WPKH_TOOL;
17
+ const P2WSH: typeof P2WSH_TOOL;
18
+ const P2TR: typeof P2TR_TOOL;
19
+ const parse: typeof parse_address;
20
+ }
@@ -0,0 +1,21 @@
1
+ import { P2PKH as P2PKH_TOOL } from './p2pkh.js';
2
+ import { P2SH as P2SH_TOOL } from './p2sh.js';
3
+ import { P2WPKH as P2WPKH_TOOL } from './p2wpkh.js';
4
+ import { P2WSH as P2WSH_TOOL } from './p2wsh.js';
5
+ import { P2TR as P2TR_TOOL } from './p2tr.js';
6
+ import { parse_address } from './util.js';
7
+ export { P2PKH } from './p2pkh.js';
8
+ export { P2SH } from './p2sh.js';
9
+ export { P2WPKH } from './p2wpkh.js';
10
+ export { P2WSH } from './p2wsh.js';
11
+ export { P2TR } from './p2tr.js';
12
+ export { parse_address } from './util.js';
13
+ export var AddressTool;
14
+ (function (AddressTool) {
15
+ AddressTool.P2PKH = P2PKH_TOOL;
16
+ AddressTool.P2SH = P2SH_TOOL;
17
+ AddressTool.P2WPKH = P2WPKH_TOOL;
18
+ AddressTool.P2WSH = P2WSH_TOOL;
19
+ AddressTool.P2TR = P2TR_TOOL;
20
+ AddressTool.parse = parse_address;
21
+ })(AddressTool || (AddressTool = {}));
@@ -0,0 +1,10 @@
1
+ import type { AddressData, ChainNetwork } from '../../types/index.js';
2
+ export declare namespace P2PKH {
3
+ const create: typeof create_p2pkh_address;
4
+ const encode: typeof encode_p2pkh_address;
5
+ const decode: typeof decode_p2pkh_address;
6
+ }
7
+ declare function create_p2pkh_address(script: string | Uint8Array, network?: ChainNetwork): string;
8
+ declare function encode_p2pkh_address(pk_hash: string | Uint8Array, network?: ChainNetwork): string;
9
+ declare function decode_p2pkh_address(address: string): AddressData;
10
+ export {};
@@ -0,0 +1,33 @@
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';
5
+ import { get_address_config, parse_address } from './util.js';
6
+ const ADDR_TYPE = 'p2pkh';
7
+ export var P2PKH;
8
+ (function (P2PKH) {
9
+ P2PKH.create = create_p2pkh_address;
10
+ P2PKH.encode = encode_p2pkh_address;
11
+ P2PKH.decode = decode_p2pkh_address;
12
+ })(P2PKH || (P2PKH = {}));
13
+ function create_p2pkh_address(script, network = 'main') {
14
+ const bytes = Buff.bytes(script);
15
+ const hash = hash160(bytes);
16
+ return encode_p2pkh_address(hash, network);
17
+ }
18
+ function encode_p2pkh_address(pk_hash, network = 'main') {
19
+ const bytes = Buff.bytes(pk_hash);
20
+ const config = get_address_config(network, ADDR_TYPE);
21
+ Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}`);
22
+ Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
23
+ return encode_address({
24
+ data: bytes,
25
+ format: 'base58',
26
+ version: config.version
27
+ });
28
+ }
29
+ function decode_p2pkh_address(address) {
30
+ const parsed = parse_address(address);
31
+ Assert.ok(parsed.type === 'p2pkh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`);
32
+ return parsed;
33
+ }
@@ -0,0 +1,10 @@
1
+ import type { ChainNetwork, AddressData } from '../../types/index.js';
2
+ export declare namespace P2SH {
3
+ const create: typeof create_p2sh_address;
4
+ const encode: typeof encode_p2sh_address;
5
+ const decode: typeof decode_p2sh_address;
6
+ }
7
+ declare function create_p2sh_address(script: string | Uint8Array, network?: ChainNetwork): string;
8
+ declare function encode_p2sh_address(script_hash: string | Uint8Array, network?: ChainNetwork): string;
9
+ declare function decode_p2sh_address(address: string): AddressData;
10
+ export {};
@@ -0,0 +1,33 @@
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';
5
+ import { get_address_config, parse_address } from './util.js';
6
+ const ADDR_TYPE = 'p2sh';
7
+ export var P2SH;
8
+ (function (P2SH) {
9
+ P2SH.create = create_p2sh_address;
10
+ P2SH.encode = encode_p2sh_address;
11
+ P2SH.decode = decode_p2sh_address;
12
+ })(P2SH || (P2SH = {}));
13
+ function create_p2sh_address(script, network = 'main') {
14
+ const bytes = Buff.bytes(script);
15
+ const hash = hash160(bytes);
16
+ return encode_p2sh_address(hash, network);
17
+ }
18
+ function encode_p2sh_address(script_hash, network = 'main') {
19
+ const bytes = Buff.bytes(script_hash);
20
+ const config = get_address_config(network, ADDR_TYPE);
21
+ Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}`);
22
+ Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
23
+ return encode_address({
24
+ data: bytes,
25
+ format: 'base58',
26
+ version: config.version
27
+ });
28
+ }
29
+ function decode_p2sh_address(address) {
30
+ const parsed = parse_address(address);
31
+ Assert.ok(parsed.type === 'p2sh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`);
32
+ return parsed;
33
+ }
@@ -0,0 +1,8 @@
1
+ import type { ChainNetwork, AddressData } from '../../types/index.js';
2
+ export declare namespace P2TR {
3
+ const encode: typeof encode_p2tr_address;
4
+ const decode: typeof decode_p2tr_address;
5
+ }
6
+ declare function encode_p2tr_address(pubkey: string | Uint8Array, network?: ChainNetwork): string;
7
+ declare function decode_p2tr_address(address: string): AddressData;
8
+ export {};
@@ -0,0 +1,26 @@
1
+ import { Buff } from '@vbyte/buff';
2
+ import { Assert } from '@vbyte/micro-lib';
3
+ import { encode_address } from './encode.js';
4
+ import { get_address_config, parse_address } from './util.js';
5
+ const ADDR_TYPE = 'p2tr';
6
+ export var P2TR;
7
+ (function (P2TR) {
8
+ P2TR.encode = encode_p2tr_address;
9
+ P2TR.decode = decode_p2tr_address;
10
+ })(P2TR || (P2TR = {}));
11
+ function encode_p2tr_address(pubkey, network = 'main') {
12
+ const bytes = Buff.bytes(pubkey);
13
+ const config = get_address_config(network, ADDR_TYPE);
14
+ Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}`);
15
+ Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
16
+ return encode_address({
17
+ data: bytes,
18
+ format: 'bech32m',
19
+ prefix: config.prefix
20
+ });
21
+ }
22
+ function decode_p2tr_address(address) {
23
+ const parsed = parse_address(address);
24
+ Assert.ok(parsed.type === 'p2tr', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`);
25
+ return parsed;
26
+ }
@@ -0,0 +1,10 @@
1
+ import type { AddressData, ChainNetwork } from '../../types/index.js';
2
+ export declare namespace P2WPKH {
3
+ const create: typeof create_p2wpkh_address;
4
+ const encode: typeof encode_p2wpkh_address;
5
+ const decode: typeof decode_p2wpkh_address;
6
+ }
7
+ declare function create_p2wpkh_address(pubkey: string | Uint8Array, network?: ChainNetwork): string;
8
+ declare function encode_p2wpkh_address(pk_hash: string | Uint8Array, network?: ChainNetwork): string;
9
+ declare function decode_p2wpkh_address(address: string): AddressData;
10
+ export {};
@@ -0,0 +1,34 @@
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';
5
+ import { get_address_config, parse_address } from './util.js';
6
+ const ADDR_TYPE = 'p2w-pkh';
7
+ export var P2WPKH;
8
+ (function (P2WPKH) {
9
+ P2WPKH.create = create_p2wpkh_address;
10
+ P2WPKH.encode = encode_p2wpkh_address;
11
+ P2WPKH.decode = decode_p2wpkh_address;
12
+ })(P2WPKH || (P2WPKH = {}));
13
+ function create_p2wpkh_address(pubkey, network = 'main') {
14
+ const bytes = Buff.bytes(pubkey);
15
+ Assert.size(bytes, 33, `invalid payload size: ${bytes.length} !== 33`);
16
+ const hash = hash160(bytes);
17
+ return encode_p2wpkh_address(hash, network);
18
+ }
19
+ function encode_p2wpkh_address(pk_hash, network = 'main') {
20
+ const bytes = Buff.bytes(pk_hash);
21
+ const config = get_address_config(network, ADDR_TYPE);
22
+ Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}`);
23
+ Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
24
+ return encode_address({
25
+ data: bytes,
26
+ format: 'bech32',
27
+ prefix: config.prefix
28
+ });
29
+ }
30
+ function decode_p2wpkh_address(address) {
31
+ const parsed = parse_address(address);
32
+ Assert.ok(parsed.type === 'p2w-pkh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`);
33
+ return parsed;
34
+ }
@@ -0,0 +1,10 @@
1
+ import type { ChainNetwork, AddressData } from '../../types/index.js';
2
+ export declare namespace P2WSH {
3
+ const create: typeof create_p2wsh_address;
4
+ const encode: typeof encode_p2wsh_address;
5
+ const decode: typeof decode_p2wsh_address;
6
+ }
7
+ declare function create_p2wsh_address(script: string | Uint8Array, network?: ChainNetwork): string;
8
+ declare function encode_p2wsh_address(script_hash: string | Uint8Array, network?: ChainNetwork): string;
9
+ declare function decode_p2wsh_address(address: string): AddressData;
10
+ export {};
@@ -0,0 +1,33 @@
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';
5
+ import { get_address_config, parse_address } from './util.js';
6
+ const ADDR_TYPE = 'p2w-sh';
7
+ export var P2WSH;
8
+ (function (P2WSH) {
9
+ P2WSH.create = create_p2wsh_address;
10
+ P2WSH.encode = encode_p2wsh_address;
11
+ P2WSH.decode = decode_p2wsh_address;
12
+ })(P2WSH || (P2WSH = {}));
13
+ function create_p2wsh_address(script, network = 'main') {
14
+ const bytes = Buff.bytes(script);
15
+ const hash = sha256(bytes);
16
+ return encode_p2wsh_address(hash, network);
17
+ }
18
+ function encode_p2wsh_address(script_hash, network = 'main') {
19
+ const bytes = Buff.bytes(script_hash);
20
+ const config = get_address_config(network, ADDR_TYPE);
21
+ Assert.exists(config, `unrecognized address config: ${ADDR_TYPE} on ${network}`);
22
+ Assert.size(bytes, config.size, `invalid payload size: ${bytes.length} !== ${config.size}`);
23
+ return encode_address({
24
+ data: bytes,
25
+ format: 'bech32',
26
+ prefix: config.prefix
27
+ });
28
+ }
29
+ function decode_p2wsh_address(address) {
30
+ const parsed = parse_address(address);
31
+ Assert.ok(parsed.type === 'p2w-sh', `address type mismatch: ${parsed.type} !== ${ADDR_TYPE}`);
32
+ return parsed;
33
+ }
@@ -0,0 +1,5 @@
1
+ import type { AddressType } from '../../types/index.js';
2
+ export declare function get_address_script(script_key: string, script_type: AddressType): {
3
+ script_hex: string;
4
+ script_asm: string[];
5
+ };
@@ -0,0 +1,46 @@
1
+ export function get_address_script(script_key, script_type) {
2
+ switch (script_type) {
3
+ case 'p2pkh':
4
+ return get_p2pkh_script(script_key);
5
+ case 'p2sh':
6
+ return get_p2sh_script(script_key);
7
+ case 'p2w-pkh':
8
+ return get_p2w_pkh_script(script_key);
9
+ case 'p2w-sh':
10
+ return get_p2w_sh_script(script_key);
11
+ case 'p2tr':
12
+ return get_p2tr_script(script_key);
13
+ default:
14
+ throw new Error('unrecognized script type: ' + script_type);
15
+ }
16
+ }
17
+ function get_p2pkh_script(script_key) {
18
+ return {
19
+ script_hex: '76a914' + script_key + '88ac',
20
+ script_asm: ['OP_DUP', 'OP_HASH160', script_key, 'OP_EQUALVERIFY', 'OP_CHECKSIG']
21
+ };
22
+ }
23
+ function get_p2sh_script(script_key) {
24
+ return {
25
+ script_hex: 'a914' + script_key + '87',
26
+ script_asm: ['OP_HASH160', script_key, 'OP_EQUAL']
27
+ };
28
+ }
29
+ function get_p2w_pkh_script(script_key) {
30
+ return {
31
+ script_hex: '0014' + script_key,
32
+ script_asm: ['OP_0', script_key]
33
+ };
34
+ }
35
+ function get_p2w_sh_script(script_key) {
36
+ return {
37
+ script_hex: '0020' + script_key,
38
+ script_asm: ['OP_0', script_key]
39
+ };
40
+ }
41
+ function get_p2tr_script(script_key) {
42
+ return {
43
+ script_hex: '5120' + script_key,
44
+ script_asm: ['OP_1', script_key]
45
+ };
46
+ }
@@ -0,0 +1,4 @@
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;
@@ -0,0 +1,57 @@
1
+ import { Buff } from '@vbyte/buff';
2
+ import { decode_address } from './encode.js';
3
+ import { get_address_script } from './script.js';
4
+ const CONFIG_TABLE = [
5
+ ['1', 'p2pkh', 'main', 20, 'base58', 0x00],
6
+ ['3', 'p2sh', 'main', 20, 'base58', 0x05],
7
+ ['m', 'p2pkh', 'testnet', 20, 'base58', 0x6F],
8
+ ['n', 'p2pkh', 'testnet', 20, 'base58', 0x6F],
9
+ ['2', 'p2sh', 'testnet', 20, 'base58', 0xC4],
10
+ ['m', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
11
+ ['n', 'p2pkh', 'regtest', 20, 'base58', 0x6F],
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],
19
+ ['bc', 'p2tr', 'main', 32, 'bech32m', 1],
20
+ ['tb', 'p2tr', 'testnet', 32, 'bech32m', 1],
21
+ ['bcrt', 'p2tr', 'regtest', 32, 'bech32m', 1]
22
+ ];
23
+ export function get_address_config(address_network, address_type) {
24
+ for (const [prefix, type, network, size, format, version] of CONFIG_TABLE) {
25
+ if (type === address_type && network === address_network) {
26
+ return { type, prefix, network, size, format, version };
27
+ }
28
+ }
29
+ return null;
30
+ }
31
+ export function get_address_ctx(address) {
32
+ const dec = decode_address(address);
33
+ for (const [prefix, type, network, size, format, version] of CONFIG_TABLE) {
34
+ if (format !== dec.format)
35
+ continue;
36
+ if (size !== dec.data.length)
37
+ continue;
38
+ if (version !== dec.version)
39
+ continue;
40
+ if (dec.prefix) {
41
+ if (prefix !== dec.prefix)
42
+ continue;
43
+ }
44
+ else {
45
+ if (!address.startsWith(prefix))
46
+ continue;
47
+ }
48
+ const hex = Buff.uint(dec.data).hex;
49
+ return { data: dec.data, hex, type, prefix, network, size, format, version };
50
+ }
51
+ throw new Error('address configuration is invalid');
52
+ }
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
+ }
@@ -0,0 +1,2 @@
1
+ export * from './pointer.js';
2
+ export * from './scribe.js';
@@ -0,0 +1,2 @@
1
+ export * from './pointer.js';
2
+ export * from './scribe.js';
@@ -0,0 +1,42 @@
1
+ export declare namespace TxPointer {
2
+ const outpoint: {
3
+ encode: typeof encode_outpoint;
4
+ decode: typeof decode_outpoint;
5
+ verify: typeof verify_outpoint;
6
+ assert: typeof assert_outpoint;
7
+ };
8
+ const record_id: {
9
+ encode: typeof encode_inscription_id;
10
+ decode: typeof decode_inscription_id;
11
+ verify: typeof verify_inscription_id;
12
+ assert: typeof assert_inscription_id;
13
+ };
14
+ const rune_id: {
15
+ encode: typeof encode_rune_id;
16
+ decode: typeof decode_rune_id;
17
+ verify: typeof verify_rune_id;
18
+ assert: typeof assert_rune_id;
19
+ };
20
+ }
21
+ declare function encode_inscription_id(txid: string, order?: number): string;
22
+ declare function decode_inscription_id(inscription_id: string): {
23
+ txid: string;
24
+ order: number;
25
+ };
26
+ declare function verify_inscription_id(inscription_id: string): boolean;
27
+ declare function assert_inscription_id(inscription_id: string): void;
28
+ declare function encode_rune_id(block_height: number, block_index: number): string;
29
+ declare function decode_rune_id(rune_id: string): {
30
+ block_height: number;
31
+ block_index: number;
32
+ };
33
+ declare function verify_rune_id(rune_id: string): boolean;
34
+ declare function assert_rune_id(rune_id: string): void;
35
+ declare function encode_outpoint(txid: string, vout: number): string;
36
+ declare function decode_outpoint(outpoint: string): {
37
+ txid: string;
38
+ vout: number;
39
+ };
40
+ declare function verify_outpoint(outpoint: string): boolean;
41
+ declare function assert_outpoint(outpoint: string): void;
42
+ export {};
@@ -0,0 +1,69 @@
1
+ export var TxPointer;
2
+ (function (TxPointer) {
3
+ TxPointer.outpoint = {
4
+ encode: encode_outpoint,
5
+ decode: decode_outpoint,
6
+ verify: verify_outpoint,
7
+ assert: assert_outpoint,
8
+ };
9
+ TxPointer.record_id = {
10
+ encode: encode_inscription_id,
11
+ decode: decode_inscription_id,
12
+ verify: verify_inscription_id,
13
+ assert: assert_inscription_id,
14
+ };
15
+ TxPointer.rune_id = {
16
+ encode: encode_rune_id,
17
+ decode: decode_rune_id,
18
+ verify: verify_rune_id,
19
+ assert: assert_rune_id,
20
+ };
21
+ })(TxPointer || (TxPointer = {}));
22
+ function encode_inscription_id(txid, order = 0) {
23
+ return `${txid}i${order}`;
24
+ }
25
+ function decode_inscription_id(inscription_id) {
26
+ assert_inscription_id(inscription_id);
27
+ const [txid, order] = inscription_id.split('i');
28
+ return { txid, order: parseInt(order) };
29
+ }
30
+ function verify_inscription_id(inscription_id) {
31
+ return inscription_id.match(/^[a-f0-9]{64}i\d+$/) !== null;
32
+ }
33
+ function assert_inscription_id(inscription_id) {
34
+ if (!verify_inscription_id(inscription_id)) {
35
+ throw new Error(`invalid inscription id: ${inscription_id}`);
36
+ }
37
+ }
38
+ function encode_rune_id(block_height, block_index) {
39
+ return `${block_height}:${block_index}`;
40
+ }
41
+ function decode_rune_id(rune_id) {
42
+ assert_rune_id(rune_id);
43
+ const [block_height, block_index] = rune_id.split(':');
44
+ return { block_height: parseInt(block_height), block_index: parseInt(block_index) };
45
+ }
46
+ function verify_rune_id(rune_id) {
47
+ return rune_id.match(/^\d+:\d+$/) !== null;
48
+ }
49
+ function assert_rune_id(rune_id) {
50
+ if (!verify_rune_id(rune_id)) {
51
+ throw new Error(`invalid rune id: ${rune_id}`);
52
+ }
53
+ }
54
+ function encode_outpoint(txid, vout) {
55
+ return `${txid}:${vout}`;
56
+ }
57
+ function decode_outpoint(outpoint) {
58
+ assert_outpoint(outpoint);
59
+ const [txid, vout] = outpoint.split(':');
60
+ return { txid, vout: parseInt(vout) };
61
+ }
62
+ function verify_outpoint(outpoint) {
63
+ return outpoint.match(/^[a-f0-9]{64}:[0-9]+$/) !== null;
64
+ }
65
+ function assert_outpoint(outpoint) {
66
+ if (!verify_outpoint(outpoint)) {
67
+ throw new Error(`invalid outpoint: ${outpoint}`);
68
+ }
69
+ }
@@ -0,0 +1,7 @@
1
+ import type { InscriptionData } from '../../types/index.js';
2
+ export declare namespace Inscription {
3
+ const encode: typeof encode_inscription;
4
+ const decode: typeof decode_inscription;
5
+ }
6
+ export declare function decode_inscription(script: string): InscriptionData[];
7
+ export declare function encode_inscription(data: InscriptionData[]): string;