@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
package/LICENSE ADDED
@@ -0,0 +1,121 @@
1
+ Creative Commons Legal Code
2
+
3
+ CC0 1.0 Universal
4
+
5
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12
+ HEREUNDER.
13
+
14
+ Statement of Purpose
15
+
16
+ The laws of most jurisdictions throughout the world automatically confer
17
+ exclusive Copyright and Related Rights (defined below) upon the creator
18
+ and subsequent owner(s) (each and all, an "owner") of an original work of
19
+ authorship and/or a database (each, a "Work").
20
+
21
+ Certain owners wish to permanently relinquish those rights to a Work for
22
+ the purpose of contributing to a commons of creative, cultural and
23
+ scientific works ("Commons") that the public can reliably and without fear
24
+ of later claims of infringement build upon, modify, incorporate in other
25
+ works, reuse and redistribute as freely as possible in any form whatsoever
26
+ and for any purposes, including without limitation commercial purposes.
27
+ These owners may contribute to the Commons to promote the ideal of a free
28
+ culture and the further production of creative, cultural and scientific
29
+ works, or to gain reputation or greater distribution for their Work in
30
+ part through the use and efforts of others.
31
+
32
+ For these and/or other purposes and motivations, and without any
33
+ expectation of additional consideration or compensation, the person
34
+ associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35
+ is an owner of Copyright and Related Rights in the Work, voluntarily
36
+ elects to apply CC0 to the Work and publicly distribute the Work under its
37
+ terms, with knowledge of his or her Copyright and Related Rights in the
38
+ Work and the meaning and intended legal effect of CC0 on those rights.
39
+
40
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
41
+ protected by copyright and related or neighboring rights ("Copyright and
42
+ Related Rights"). Copyright and Related Rights include, but are not
43
+ limited to, the following:
44
+
45
+ i. the right to reproduce, adapt, distribute, perform, display,
46
+ communicate, and translate a Work;
47
+ ii. moral rights retained by the original author(s) and/or performer(s);
48
+ iii. publicity and privacy rights pertaining to a person's image or
49
+ likeness depicted in a Work;
50
+ iv. rights protecting against unfair competition in regards to a Work,
51
+ subject to the limitations in paragraph 4(a), below;
52
+ v. rights protecting the extraction, dissemination, use and reuse of data
53
+ in a Work;
54
+ vi. database rights (such as those arising under Directive 96/9/EC of the
55
+ European Parliament and of the Council of 11 March 1996 on the legal
56
+ protection of databases, and under any national implementation
57
+ thereof, including any amended or successor version of such
58
+ directive); and
59
+ vii. other similar, equivalent or corresponding rights throughout the
60
+ world based on applicable law or treaty, and any national
61
+ implementations thereof.
62
+
63
+ 2. Waiver. To the greatest extent permitted by, but not in contravention
64
+ of, applicable law, Affirmer hereby overtly, fully, permanently,
65
+ irrevocably and unconditionally waives, abandons, and surrenders all of
66
+ Affirmer's Copyright and Related Rights and associated claims and causes
67
+ of action, whether now known or unknown (including existing as well as
68
+ future claims and causes of action), in the Work (i) in all territories
69
+ worldwide, (ii) for the maximum duration provided by applicable law or
70
+ treaty (including future time extensions), (iii) in any current or future
71
+ medium and for any number of copies, and (iv) for any purpose whatsoever,
72
+ including without limitation commercial, advertising or promotional
73
+ purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74
+ member of the public at large and to the detriment of Affirmer's heirs and
75
+ successors, fully intending that such Waiver shall not be subject to
76
+ revocation, rescission, cancellation, termination, or any other legal or
77
+ equitable action to disrupt the quiet enjoyment of the Work by the public
78
+ as contemplated by Affirmer's express Statement of Purpose.
79
+
80
+ 3. Public License Fallback. Should any part of the Waiver for any reason
81
+ be judged legally invalid or ineffective under applicable law, then the
82
+ Waiver shall be preserved to the maximum extent permitted taking into
83
+ account Affirmer's express Statement of Purpose. In addition, to the
84
+ extent the Waiver is so judged Affirmer hereby grants to each affected
85
+ person a royalty-free, non transferable, non sublicensable, non exclusive,
86
+ irrevocable and unconditional license to exercise Affirmer's Copyright and
87
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
88
+ maximum duration provided by applicable law or treaty (including future
89
+ time extensions), (iii) in any current or future medium and for any number
90
+ of copies, and (iv) for any purpose whatsoever, including without
91
+ limitation commercial, advertising or promotional purposes (the
92
+ "License"). The License shall be deemed effective as of the date CC0 was
93
+ applied by Affirmer to the Work. Should any part of the License for any
94
+ reason be judged legally invalid or ineffective under applicable law, such
95
+ partial invalidity or ineffectiveness shall not invalidate the remainder
96
+ of the License, and in such case Affirmer hereby affirms that he or she
97
+ will not (i) exercise any of his or her remaining Copyright and Related
98
+ Rights in the Work or (ii) assert any associated claims and causes of
99
+ action with respect to the Work, in either case contrary to Affirmer's
100
+ express Statement of Purpose.
101
+
102
+ 4. Limitations and Disclaimers.
103
+
104
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
105
+ surrendered, licensed or otherwise affected by this document.
106
+ b. Affirmer offers the Work as-is and makes no representations or
107
+ warranties of any kind concerning the Work, express, implied,
108
+ statutory or otherwise, including without limitation warranties of
109
+ title, merchantability, fitness for a particular purpose, non
110
+ infringement, or the absence of latent or other defects, accuracy, or
111
+ the present or absence of errors, whether or not discoverable, all to
112
+ the greatest extent permissible under applicable law.
113
+ c. Affirmer disclaims responsibility for clearing rights of other persons
114
+ that may apply to the Work or any use thereof, including without
115
+ limitation any person's Copyright and Related Rights in the Work.
116
+ Further, Affirmer disclaims responsibility for obtaining any necessary
117
+ consents, permissions or other rights required for any use of the
118
+ Work.
119
+ d. Affirmer understands and acknowledges that Creative Commons is not a
120
+ party to this document and has no duty or obligation with respect to
121
+ this CC0 or use of the Work.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # btc-dev
2
+
3
+ Experimental revision of the bitcoin [tapscript](https://www.npmjs.com/package/@cmdcode/tapscript) library.
4
+
5
+ Documentation coming soon!
@@ -0,0 +1,5 @@
1
+ export * from './signer.js';
2
+ export * from './tx.js';
3
+ export * from './txin.js';
4
+ export * from './txout.js';
5
+ export * from './witness.js';
@@ -0,0 +1,5 @@
1
+ export * from './signer.js';
2
+ export * from './tx.js';
3
+ export * from './txin.js';
4
+ export * from './txout.js';
5
+ export * from './witness.js';
@@ -0,0 +1,18 @@
1
+ import { Buff, Bytes } from '@vbyte/buff';
2
+ import type { SigHashOptions, TxData } from '../types/index.js';
3
+ export declare class TxSigner {
4
+ private readonly _seckey;
5
+ constructor(seckey: Bytes);
6
+ get pubkey(): {
7
+ segwit: Buff;
8
+ taproot: Buff;
9
+ };
10
+ get sign_msg(): {
11
+ ecdsa: (msg: Bytes) => Buff;
12
+ bip340: (msg: Bytes) => Buff;
13
+ };
14
+ get sign_tx(): {
15
+ segwit: (tx: TxData, options: SigHashOptions) => string;
16
+ taproot: (tx: TxData, options: SigHashOptions) => string;
17
+ };
18
+ }
@@ -0,0 +1,32 @@
1
+ import { Buff } from '@vbyte/buff';
2
+ import { ECC } from '@vbyte/micro-lib';
3
+ import { sign_segwit_tx, sign_taproot_tx } from '../lib/sighash/sign.js';
4
+ export class TxSigner {
5
+ constructor(seckey) {
6
+ this._seckey = Buff.bytes(seckey).hex;
7
+ }
8
+ get pubkey() {
9
+ return {
10
+ segwit: ECC.get_pubkey(this._seckey, 'ecdsa'),
11
+ taproot: ECC.get_pubkey(this._seckey, 'bip340')
12
+ };
13
+ }
14
+ get sign_msg() {
15
+ return {
16
+ ecdsa: (msg) => {
17
+ const bytes = Buff.bytes(msg);
18
+ return ECC.sign_ecdsa(this._seckey, bytes);
19
+ },
20
+ bip340: (msg) => {
21
+ const bytes = Buff.bytes(msg);
22
+ return ECC.sign_bip340(this._seckey, bytes);
23
+ }
24
+ };
25
+ }
26
+ get sign_tx() {
27
+ return {
28
+ segwit: (tx, options) => sign_segwit_tx(this._seckey, tx, options),
29
+ taproot: (tx, options) => sign_taproot_tx(this._seckey, tx, options)
30
+ };
31
+ }
32
+ }
@@ -0,0 +1,38 @@
1
+ import { TransactionInput } from './txin.js';
2
+ import { TransactionOutput } from './txout.js';
3
+ import type { TxData, TxTemplate, TxValue, TransactionData } from '../types/index.js';
4
+ export declare class Transaction {
5
+ private readonly _size;
6
+ private readonly _tx;
7
+ private readonly _hash;
8
+ private readonly _value;
9
+ private readonly _vin;
10
+ private readonly _vout;
11
+ constructor(txdata: string | TxData | TxTemplate);
12
+ get data(): TransactionData;
13
+ get hash(): string;
14
+ get locktime(): {
15
+ hex: string;
16
+ data: import("../types/index.js").LocktimeInfo | null;
17
+ value: number;
18
+ };
19
+ get return(): import("../types/index.js").TxOutput | null;
20
+ get size(): {
21
+ segwit: number;
22
+ vin: number;
23
+ vout: number;
24
+ witness: number;
25
+ base: number;
26
+ real: number;
27
+ weight: number;
28
+ vsize: number;
29
+ };
30
+ get spends(): TransactionOutput[];
31
+ get txid(): string;
32
+ get value(): TxValue;
33
+ get version(): number;
34
+ get vin(): TransactionInput[];
35
+ get vout(): TransactionOutput[];
36
+ toJSON(): TransactionData;
37
+ toString(): string;
38
+ }
@@ -0,0 +1,73 @@
1
+ import { TransactionInput } from './txin.js';
2
+ import { TransactionOutput } from './txout.js';
3
+ import { decode_tx_data, get_txid, is_return_script, parse_tx_data, Locktime, get_txsize, get_tx_value, get_txhash, encode_tx_locktime, } from '../lib/tx/index.js';
4
+ export class Transaction {
5
+ constructor(txdata) {
6
+ this._tx = (typeof txdata !== 'string')
7
+ ? parse_tx_data(txdata)
8
+ : decode_tx_data(txdata);
9
+ this._vin = this._tx.vin.map(txin => new TransactionInput(txin));
10
+ this._vout = this._tx.vout.map(txout => new TransactionOutput(txout));
11
+ this._size = get_txsize(this._tx);
12
+ this._hash = get_txhash(this._tx);
13
+ this._value = get_tx_value(this._tx);
14
+ }
15
+ get data() {
16
+ return {
17
+ hash: this.hash,
18
+ locktime: this.locktime,
19
+ return: this.return,
20
+ size: this.size,
21
+ spends: this.spends,
22
+ txid: this.txid,
23
+ value: this.value,
24
+ version: this.version,
25
+ vin: this.vin.map(txin => txin.data),
26
+ vout: this.vout.map(txout => txout.data)
27
+ };
28
+ }
29
+ get hash() {
30
+ return this._hash;
31
+ }
32
+ get locktime() {
33
+ return {
34
+ hex: encode_tx_locktime(this._tx.locktime).hex,
35
+ data: Locktime.decode(this._tx.locktime),
36
+ value: this._tx.locktime
37
+ };
38
+ }
39
+ get return() {
40
+ return this._tx.vout.find(txout => is_return_script(txout.script_pk)) || null;
41
+ }
42
+ get size() {
43
+ return {
44
+ ...this._size,
45
+ segwit: this._vin.reduce((acc, txin) => acc + (txin.witness?.vsize ?? 0), 0),
46
+ vin: this._vin.reduce((acc, txin) => acc + txin.size, 0),
47
+ vout: this._vout.reduce((acc, txout) => acc + txout.size, 0),
48
+ witness: this._vin.reduce((acc, txin) => acc + (txin.witness?.size ?? 0), 0)
49
+ };
50
+ }
51
+ get spends() {
52
+ return this._tx.vin
53
+ .filter(txin => txin.prevout !== null)
54
+ .map(txin => new TransactionOutput(txin.prevout));
55
+ }
56
+ get txid() {
57
+ return get_txid(this._tx);
58
+ }
59
+ get value() {
60
+ return this._value;
61
+ }
62
+ get version() {
63
+ return this._tx.version;
64
+ }
65
+ get vin() {
66
+ return this._vin;
67
+ }
68
+ get vout() {
69
+ return this._vout;
70
+ }
71
+ toJSON() { return this.data; }
72
+ toString() { return JSON.stringify(this.data); }
73
+ }
@@ -0,0 +1,29 @@
1
+ import { TransactionOutput } from './txout.js';
2
+ import { TransactionWitness } from './witness.js';
3
+ import type { TxInput, TxInputField } from '../types/index.js';
4
+ export declare class TransactionInput {
5
+ private readonly _size;
6
+ private readonly _txin;
7
+ private readonly _witness;
8
+ constructor(txin: TxInput);
9
+ get coinbase(): string | null;
10
+ get data(): TxInputField;
11
+ get has_prevout(): boolean;
12
+ get is_coinbase(): boolean;
13
+ get prevout(): TransactionOutput | null;
14
+ get script_sig(): {
15
+ asm: string[];
16
+ hex: string;
17
+ } | null;
18
+ get sequence(): {
19
+ hex: string;
20
+ data: import("../types/index.js").SequenceInfo | null;
21
+ value: number;
22
+ };
23
+ get size(): number;
24
+ get txid(): string;
25
+ get vout(): number;
26
+ get witness(): TransactionWitness | null;
27
+ toJSON(): TxInputField;
28
+ toString(): string;
29
+ }
@@ -0,0 +1,68 @@
1
+ import { decode_script } from '../lib/script/index.js';
2
+ import { TransactionOutput } from './txout.js';
3
+ import { TransactionWitness } from './witness.js';
4
+ import { encode_txin_sequence, get_txin_size, Sequence } from '../lib/tx/index.js';
5
+ export class TransactionInput {
6
+ constructor(txin) {
7
+ this._size = get_txin_size(txin);
8
+ this._txin = txin;
9
+ this._witness = txin.witness.length > 0
10
+ ? new TransactionWitness(txin.witness)
11
+ : null;
12
+ }
13
+ get coinbase() {
14
+ return this._txin.coinbase;
15
+ }
16
+ get data() {
17
+ return {
18
+ coinbase: this.coinbase,
19
+ prevout: this.prevout?.data ?? null,
20
+ script_sig: this.script_sig,
21
+ sequence: this.sequence,
22
+ size: this.size,
23
+ txid: this.txid,
24
+ vout: this.vout,
25
+ witness: this.witness?.data ?? null
26
+ };
27
+ }
28
+ get has_prevout() {
29
+ return this._txin.prevout !== null;
30
+ }
31
+ get is_coinbase() {
32
+ return this._txin.coinbase !== null;
33
+ }
34
+ get prevout() {
35
+ return this._txin.prevout
36
+ ? new TransactionOutput(this._txin.prevout)
37
+ : null;
38
+ }
39
+ get script_sig() {
40
+ if (this._txin.script_sig === null)
41
+ return null;
42
+ return {
43
+ asm: decode_script(this._txin.script_sig),
44
+ hex: this._txin.script_sig
45
+ };
46
+ }
47
+ get sequence() {
48
+ return {
49
+ hex: encode_txin_sequence(this._txin.sequence).hex,
50
+ data: Sequence.decode(this._txin.sequence),
51
+ value: this._txin.sequence
52
+ };
53
+ }
54
+ get size() {
55
+ return this._size;
56
+ }
57
+ get txid() {
58
+ return this._txin.txid;
59
+ }
60
+ get vout() {
61
+ return this._txin.vout;
62
+ }
63
+ get witness() {
64
+ return this._witness;
65
+ }
66
+ toJSON() { return this.data; }
67
+ toString() { return JSON.stringify(this.data); }
68
+ }
@@ -0,0 +1,18 @@
1
+ import type { TxOutput, TxOutputField } from '../types/index.js';
2
+ export declare class TransactionOutput {
3
+ private readonly _info;
4
+ private readonly _size;
5
+ private readonly _txout;
6
+ constructor(txout: TxOutput);
7
+ get data(): TxOutputField;
8
+ get script_pk(): {
9
+ hex: string;
10
+ asm: string[];
11
+ };
12
+ get size(): number;
13
+ get type(): import("../types/index.js").TxOutputType;
14
+ get value(): bigint;
15
+ get version(): import("../types/index.js").WitnessVersion;
16
+ toJSON(): TxOutputField;
17
+ toString(): string;
18
+ }
@@ -0,0 +1,38 @@
1
+ import { decode_script } from '../lib/script/index.js';
2
+ import { get_txout_size, get_vout_info } from '../lib/tx/index.js';
3
+ export class TransactionOutput {
4
+ constructor(txout) {
5
+ this._info = get_vout_info(txout);
6
+ this._size = get_txout_size(txout);
7
+ this._txout = txout;
8
+ }
9
+ get data() {
10
+ return {
11
+ script_pk: this.script_pk,
12
+ size: this.size,
13
+ type: this.type,
14
+ value: this.value,
15
+ version: this.version
16
+ };
17
+ }
18
+ get script_pk() {
19
+ return {
20
+ hex: this._txout.script_pk,
21
+ asm: decode_script(this._txout.script_pk)
22
+ };
23
+ }
24
+ get size() {
25
+ return this._size;
26
+ }
27
+ get type() {
28
+ return this._info.type;
29
+ }
30
+ get value() {
31
+ return this._txout.value;
32
+ }
33
+ get version() {
34
+ return this._info.version;
35
+ }
36
+ toJSON() { return this.data; }
37
+ toString() { return JSON.stringify(this.data); }
38
+ }
@@ -0,0 +1,20 @@
1
+ import { Bytes } from '@vbyte/buff';
2
+ import type { ScriptField, WitnessField, WitnessType } from '../types/index.js';
3
+ export declare class TransactionWitness {
4
+ private readonly _data;
5
+ private readonly _meta;
6
+ private readonly _size;
7
+ constructor(witness: Bytes[]);
8
+ get annex(): string | null;
9
+ get cblock(): string | null;
10
+ get data(): WitnessField;
11
+ get params(): string[];
12
+ get script(): ScriptField | null;
13
+ get size(): number;
14
+ get stack(): string[];
15
+ get type(): WitnessType;
16
+ get version(): number | null;
17
+ get vsize(): number;
18
+ toJSON(): WitnessField;
19
+ toString(): string;
20
+ }
@@ -0,0 +1,57 @@
1
+ import { Buff } from '@vbyte/buff';
2
+ import { decode_script } from '../lib/script/index.js';
3
+ import { parse_witness_data, get_witness_size, } from '../lib/tx/index.js';
4
+ export class TransactionWitness {
5
+ constructor(witness) {
6
+ this._data = witness.map(e => Buff.bytes(e));
7
+ this._meta = parse_witness_data(witness);
8
+ this._size = get_witness_size(witness);
9
+ }
10
+ get annex() {
11
+ return this._meta.annex;
12
+ }
13
+ get cblock() {
14
+ return this._meta.cblock;
15
+ }
16
+ get data() {
17
+ return {
18
+ annex: this.annex,
19
+ cblock: this.cblock,
20
+ params: this.params,
21
+ script: this.script,
22
+ size: this.size,
23
+ stack: this.stack,
24
+ type: this.type,
25
+ version: this.version,
26
+ vsize: this.vsize
27
+ };
28
+ }
29
+ get params() {
30
+ return this._meta.params;
31
+ }
32
+ get script() {
33
+ if (this._meta.script === null)
34
+ return null;
35
+ return {
36
+ hex: this._meta.script,
37
+ asm: decode_script(this._meta.script)
38
+ };
39
+ }
40
+ get size() {
41
+ return this._size.size;
42
+ }
43
+ get stack() {
44
+ return this._data.map(e => e.hex);
45
+ }
46
+ get type() {
47
+ return this._meta.type;
48
+ }
49
+ get version() {
50
+ return this._meta.version;
51
+ }
52
+ get vsize() {
53
+ return this._size.vsize;
54
+ }
55
+ toJSON() { return this.data; }
56
+ toString() { return JSON.stringify(this.data); }
57
+ }
@@ -0,0 +1,22 @@
1
+ export declare const COINBASE: {
2
+ TXID: string;
3
+ VOUT: number;
4
+ };
5
+ export declare const DEFAULT: {
6
+ LOCKTIME: number;
7
+ SEQUENCE: number;
8
+ VERSION: number;
9
+ };
10
+ export declare const TAPLEAF_VERSIONS: number[];
11
+ export declare const TAPLEAF_DEFAULT_VERSION = 192;
12
+ export declare const LOCK_SCRIPT_REGEX: Record<string, RegExp>;
13
+ export declare const SCRIPT_INT_KEY = "";
14
+ export declare const TX_SIZE: {
15
+ GLOBAL_BASE: number;
16
+ GLOBAL_WIT: number;
17
+ TXIN_BASE: number;
18
+ TXOUT_BASE: number;
19
+ };
20
+ export declare const SIGHASH_DEFAULT = 1;
21
+ export declare const SIGHASH_SEGWIT: number[];
22
+ export declare const SIGHASH_TAPROOT: number[];
package/dist/const.js ADDED
@@ -0,0 +1,36 @@
1
+ export const COINBASE = {
2
+ TXID: '00'.repeat(32),
3
+ VOUT: 0xFFFFFFFF,
4
+ };
5
+ export const DEFAULT = {
6
+ LOCKTIME: 0,
7
+ SEQUENCE: 0xFFFFFFFF,
8
+ VERSION: 2,
9
+ };
10
+ export const TAPLEAF_VERSIONS = [
11
+ 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce,
12
+ 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
13
+ 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee,
14
+ 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
15
+ 0x66, 0x7e, 0x80, 0x84, 0x96, 0x98, 0xba, 0xbc,
16
+ 0xbe
17
+ ];
18
+ export const TAPLEAF_DEFAULT_VERSION = 0xc0;
19
+ export const LOCK_SCRIPT_REGEX = {
20
+ 'p2pkh': /^76a914[0-9a-f]{40}88ac$/i,
21
+ 'p2sh': /^a914[0-9a-f]{40}87$/i,
22
+ 'p2wpkh': /^0014[0-9a-f]{40}$/i,
23
+ 'p2wsh': /^0020[0-9a-f]{64}$/i,
24
+ 'p2tr': /^5120[0-9a-f]{64}$/i,
25
+ 'opreturn': /^6a[0-9a-f]{2,}$/i,
26
+ };
27
+ export const SCRIPT_INT_KEY = '';
28
+ export const TX_SIZE = {
29
+ GLOBAL_BASE: 8,
30
+ GLOBAL_WIT: 10,
31
+ TXIN_BASE: 32 + 4 + 4,
32
+ TXOUT_BASE: 8,
33
+ };
34
+ export const SIGHASH_DEFAULT = 0x01;
35
+ export const SIGHASH_SEGWIT = [0x01, 0x02, 0x03, 0x81, 0x82, 0x83];
36
+ export const SIGHASH_TAPROOT = [0x00, ...SIGHASH_SEGWIT];
@@ -0,0 +1,11 @@
1
+ export * from './class/index.js';
2
+ export * as Address from './lib/address/index.js';
3
+ export * as Meta from './lib/meta/index.js';
4
+ export * as PSBT from './lib/psbt/index.js';
5
+ export * as Script from './lib/script/index.js';
6
+ export * as Sighash from './lib/sighash/index.js';
7
+ export * as Taproot from './lib/taproot/index.js';
8
+ export * as Tx from './lib/tx/index.js';
9
+ export * as CONST from './const.js';
10
+ export * as SCHEMA from './schema/index.js';
11
+ export type * from './types/index.js';
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ export * from './class/index.js';
2
+ export * as Address from './lib/address/index.js';
3
+ export * as Meta from './lib/meta/index.js';
4
+ export * as PSBT from './lib/psbt/index.js';
5
+ export * as Script from './lib/script/index.js';
6
+ export * as Sighash from './lib/sighash/index.js';
7
+ export * as Taproot from './lib/taproot/index.js';
8
+ export * as Tx from './lib/tx/index.js';
9
+ export * as CONST from './const.js';
10
+ export * as SCHEMA from './schema/index.js';
@@ -0,0 +1,3 @@
1
+ import type { DecodedAddress } from '../../types/address.js';
2
+ export declare function decode_address(address: string): DecodedAddress;
3
+ export declare function encode_address(config: DecodedAddress): string;