@opcat-labs/opcat 1.0.1 → 1.0.3

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 (293) hide show
  1. package/.cjs2esm.json +18 -0
  2. package/.mocharc.yaml +1 -1
  3. package/CHANGELOG.md +12 -0
  4. package/README.md +6 -0
  5. package/{lib/address.js → cjs/address.cjs} +77 -72
  6. package/cjs/block/block.cjs +332 -0
  7. package/{lib/block/blockheader.js → cjs/block/blockheader.cjs} +8 -7
  8. package/cjs/block/index.cjs +2 -0
  9. package/{lib/block/merkleblock.js → cjs/block/merkleblock.cjs} +23 -15
  10. package/cjs/bn.cjs +3411 -0
  11. package/{lib/crypto/bn.js → cjs/crypto/bn.cjs} +3 -3
  12. package/{lib/crypto/ecdsa.js → cjs/crypto/ecdsa.cjs} +150 -14
  13. package/{lib/crypto/hash.node.js → cjs/crypto/hash.cjs} +13 -2
  14. package/cjs/crypto/index.cjs +16 -0
  15. package/{lib/crypto/point.js → cjs/crypto/point.cjs} +11 -4
  16. package/cjs/crypto/random.cjs +18 -0
  17. package/{lib/crypto/signature.js → cjs/crypto/signature.cjs} +158 -8
  18. package/{lib/encoding/base58.js → cjs/encoding/base58.cjs} +58 -2
  19. package/cjs/encoding/base58check.cjs +192 -0
  20. package/cjs/encoding/bufferreader.cjs +333 -0
  21. package/cjs/encoding/bufferwriter.cjs +244 -0
  22. package/{lib/encoding/decode-asm.js → cjs/encoding/decode-asm.cjs} +4 -4
  23. package/{lib/encoding/decode-hex.js → cjs/encoding/decode-hex.cjs} +1 -1
  24. package/cjs/encoding/index.cjs +14 -0
  25. package/cjs/encoding/varint.cjs +116 -0
  26. package/{lib/errors/index.js → cjs/errors/index.cjs} +9 -9
  27. package/{lib/errors/spec.js → cjs/errors/spec.cjs} +2 -2
  28. package/cjs/hash-cache.cjs +98 -0
  29. package/{lib/hdprivatekey.js → cjs/hdprivatekey.cjs} +232 -140
  30. package/{lib/hdpublickey.js → cjs/hdpublickey.cjs} +120 -93
  31. package/cjs/index.cjs +94 -0
  32. package/cjs/interpreter/index.cjs +2 -0
  33. package/cjs/interpreter/interpreter.cjs +1988 -0
  34. package/{lib/script/stack.js → cjs/interpreter/stack.cjs} +9 -2
  35. package/{lib/message/message.js → cjs/message/message.cjs} +62 -25
  36. package/cjs/mnemonic/index.cjs +3 -0
  37. package/{lib/mnemonic/mnemonic.js → cjs/mnemonic/mnemonic.cjs} +44 -13
  38. package/{lib/mnemonic/pbkdf2.node.js → cjs/mnemonic/pbkdf2.cjs} +9 -2
  39. package/cjs/mnemonic/words/index.cjs +66 -0
  40. package/cjs/network.cjs +13 -0
  41. package/cjs/networks.cjs +321 -0
  42. package/{lib/opcode.js → cjs/opcode.cjs} +69 -5
  43. package/cjs/privatekey.cjs +422 -0
  44. package/{lib/publickey.js → cjs/publickey.cjs} +14 -16
  45. package/cjs/script/index.cjs +2 -0
  46. package/{lib/script/script.js → cjs/script/script.cjs} +322 -67
  47. package/cjs/transaction/index.cjs +5 -0
  48. package/cjs/transaction/input/index.cjs +34 -0
  49. package/cjs/transaction/input/input.cjs +396 -0
  50. package/{lib/transaction/input/multisig.js → cjs/transaction/input/multisig.cjs} +112 -18
  51. package/{lib/transaction/input/publickey.js → cjs/transaction/input/publickey.cjs} +29 -19
  52. package/{lib/transaction/input/publickeyhash.js → cjs/transaction/input/publickeyhash.cjs} +25 -17
  53. package/{lib/transaction/output.js → cjs/transaction/output.cjs} +100 -15
  54. package/cjs/transaction/sighash.cjs +187 -0
  55. package/{lib/transaction/signature.js → cjs/transaction/signature.cjs} +30 -6
  56. package/cjs/transaction/transaction.cjs +2000 -0
  57. package/{lib/transaction/unspentoutput.js → cjs/transaction/unspentoutput.cjs} +5 -5
  58. package/cjs/util/derivation.cjs +53 -0
  59. package/cjs/util/index.cjs +11 -0
  60. package/cjs/util/js.cjs +95 -0
  61. package/{lib/util/preconditions.js → cjs/util/preconditions.cjs} +2 -2
  62. package/esm/address.js +483 -0
  63. package/{lib → esm}/block/block.js +82 -27
  64. package/esm/block/blockheader.js +296 -0
  65. package/esm/block/index.js +2 -0
  66. package/esm/block/merkleblock.js +331 -0
  67. package/esm/bn.js +3411 -0
  68. package/esm/crypto/bn.js +278 -0
  69. package/esm/crypto/ecdsa.js +475 -0
  70. package/{lib/crypto/hash.browser.js → esm/crypto/hash.js} +18 -7
  71. package/esm/crypto/index.js +16 -0
  72. package/esm/crypto/point.js +228 -0
  73. package/esm/crypto/random.js +18 -0
  74. package/esm/crypto/signature.js +475 -0
  75. package/esm/encoding/base58.js +167 -0
  76. package/esm/encoding/base58check.js +192 -0
  77. package/esm/encoding/bufferreader.js +333 -0
  78. package/esm/encoding/bufferwriter.js +243 -0
  79. package/esm/encoding/decode-asm.js +24 -0
  80. package/esm/encoding/decode-hex.js +32 -0
  81. package/esm/encoding/decode-script-chunks.js +43 -0
  82. package/esm/encoding/encode-hex.js +284 -0
  83. package/esm/encoding/index.js +14 -0
  84. package/esm/encoding/is-hex.js +7 -0
  85. package/esm/encoding/varint.js +116 -0
  86. package/esm/errors/index.js +54 -0
  87. package/esm/errors/spec.js +314 -0
  88. package/esm/hash-cache.js +98 -0
  89. package/esm/hdprivatekey.js +768 -0
  90. package/esm/hdpublickey.js +549 -0
  91. package/esm/index.js +66 -0
  92. package/esm/interpreter/index.js +2 -0
  93. package/{lib/script → esm/interpreter}/interpreter.js +219 -66
  94. package/esm/interpreter/stack.js +116 -0
  95. package/esm/message/message.js +228 -0
  96. package/esm/mnemonic/index.js +3 -0
  97. package/esm/mnemonic/mnemonic.js +332 -0
  98. package/{lib/mnemonic/pbkdf2.browser.js → esm/mnemonic/pbkdf2.js} +13 -6
  99. package/esm/mnemonic/words/chinese.js +2054 -0
  100. package/esm/mnemonic/words/english.js +2054 -0
  101. package/esm/mnemonic/words/french.js +2054 -0
  102. package/esm/mnemonic/words/index.js +66 -0
  103. package/esm/mnemonic/words/italian.js +2054 -0
  104. package/esm/mnemonic/words/japanese.js +2054 -0
  105. package/esm/mnemonic/words/spanish.js +2054 -0
  106. package/esm/network.js +13 -0
  107. package/{lib → esm}/networks.js +61 -120
  108. package/esm/opcode.js +319 -0
  109. package/{lib → esm}/privatekey.js +76 -28
  110. package/esm/publickey.js +384 -0
  111. package/esm/script/index.js +2 -0
  112. package/esm/script/script.js +1329 -0
  113. package/esm/script/write-i32-le.js +17 -0
  114. package/esm/script/write-push-data.js +35 -0
  115. package/esm/script/write-u16-le.js +12 -0
  116. package/esm/script/write-u32-le.js +16 -0
  117. package/esm/script/write-u64-le.js +24 -0
  118. package/esm/script/write-u8-le.js +8 -0
  119. package/esm/script/write-varint.js +46 -0
  120. package/esm/transaction/index.js +5 -0
  121. package/esm/transaction/input/index.js +33 -0
  122. package/{lib → esm}/transaction/input/input.js +132 -90
  123. package/esm/transaction/input/multisig.js +335 -0
  124. package/esm/transaction/input/publickey.js +108 -0
  125. package/esm/transaction/input/publickeyhash.js +124 -0
  126. package/esm/transaction/output.js +316 -0
  127. package/{lib → esm}/transaction/sighash.js +42 -22
  128. package/esm/transaction/signature.js +120 -0
  129. package/{lib → esm}/transaction/transaction.js +522 -163
  130. package/esm/transaction/unspentoutput.js +112 -0
  131. package/esm/util/_.js +47 -0
  132. package/esm/util/derivation.js +53 -0
  133. package/esm/util/index.js +12 -0
  134. package/esm/util/js.js +95 -0
  135. package/esm/util/preconditions.js +33 -0
  136. package/fixup.cjs +17 -0
  137. package/package.json +18 -4
  138. package/test/{address.js → address.cjs} +14 -43
  139. package/test/block/{block.js → block.cjs} +3 -5
  140. package/test/block/{blockheader.js → blockheader.cjs} +2 -2
  141. package/test/block/{merklebloack.js → merklebloack.cjs} +2 -2
  142. package/test/crypto/{ecdsa.js → ecdsa.cjs} +9 -9
  143. package/test/crypto/{hash.browser.js → hash.browser.cjs} +2 -1
  144. package/test/crypto/{signature.js → signature.cjs} +2 -2
  145. package/test/data/bitcoind/script_tests.json +5 -5
  146. package/test/{hashCache.js → hashCache.cjs} +2 -1
  147. package/test/{hdkeys.js → hdkeys.cjs} +4 -2
  148. package/test/{hdprivatekey.js → hdprivatekey.cjs} +7 -6
  149. package/test/{hdpublickey.js → hdpublickey.cjs} +2 -7
  150. package/test/mnemonic/{pbkdf2.test.js → pbkdf2.test.cjs} +2 -2
  151. package/test/{networks.js → networks.cjs} +12 -31
  152. package/test/{publickey.js → publickey.cjs} +2 -2
  153. package/test/script/{interpreter.js → interpreter.cjs} +5 -5
  154. package/test/script/{script.js → script.cjs} +8 -2
  155. package/test/transaction/{deserialize.js → deserialize.cjs} +2 -2
  156. package/test/transaction/input/{input.js → input.cjs} +1 -1
  157. package/test/transaction/input/{multisig.js → multisig.cjs} +2 -1
  158. package/test/transaction/input/{publickeyhash.js → publickeyhash.cjs} +1 -1
  159. package/test/transaction/{sighash.js → sighash.cjs} +1 -1
  160. package/test/transaction/{transaction.js → transaction.cjs} +2 -2
  161. package/tsconfig.json +13 -0
  162. package/types/address.d.cts +252 -0
  163. package/types/block/block.d.cts +139 -0
  164. package/types/block/blockheader.d.cts +125 -0
  165. package/types/block/index.d.cts +2 -0
  166. package/types/block/merkleblock.d.cts +95 -0
  167. package/types/bn.d.cts +202 -0
  168. package/types/crypto/bn.d.cts +2 -0
  169. package/types/crypto/ecdsa.d.cts +187 -0
  170. package/types/crypto/hash.d.cts +118 -0
  171. package/types/crypto/index.d.cts +7 -0
  172. package/types/crypto/point.d.cts +134 -0
  173. package/types/crypto/random.d.cts +13 -0
  174. package/types/crypto/signature.d.cts +160 -0
  175. package/types/encoding/base58.d.cts +106 -0
  176. package/types/encoding/base58check.d.cts +107 -0
  177. package/types/encoding/bufferreader.d.cts +164 -0
  178. package/types/encoding/bufferwriter.d.cts +126 -0
  179. package/types/encoding/decode-asm.d.cts +2 -0
  180. package/types/encoding/decode-hex.d.cts +2 -0
  181. package/types/encoding/decode-script-chunks.d.cts +14 -0
  182. package/types/encoding/encode-hex.d.cts +2 -0
  183. package/types/encoding/index.d.cts +6 -0
  184. package/types/encoding/is-hex.d.cts +2 -0
  185. package/types/encoding/varint.d.cts +66 -0
  186. package/types/errors/index.d.cts +4 -0
  187. package/types/errors/spec.d.cts +22 -0
  188. package/types/hash-cache.d.cts +65 -0
  189. package/types/hdprivatekey.d.cts +281 -0
  190. package/types/hdpublickey.d.cts +240 -0
  191. package/types/index.d.cts +26 -0
  192. package/types/interpreter/index.d.cts +2 -0
  193. package/types/interpreter/interpreter.d.cts +228 -0
  194. package/types/interpreter/stack.d.cts +35 -0
  195. package/types/message/message.d.cts +110 -0
  196. package/types/mnemonic/index.d.cts +2 -0
  197. package/types/mnemonic/mnemonic.d.cts +171 -0
  198. package/types/mnemonic/pbkdf2.d.cts +14 -0
  199. package/types/mnemonic/words/chinese.d.cts +2 -0
  200. package/types/mnemonic/words/english.d.cts +2 -0
  201. package/types/mnemonic/words/french.d.cts +2 -0
  202. package/types/mnemonic/words/index.d.cts +22 -0
  203. package/types/mnemonic/words/italian.d.cts +2 -0
  204. package/types/mnemonic/words/japanese.d.cts +2 -0
  205. package/types/mnemonic/words/spanish.d.cts +2 -0
  206. package/types/network.d.cts +11 -0
  207. package/types/networks.d.cts +76 -0
  208. package/types/opcode.d.cts +93 -0
  209. package/types/privatekey.d.cts +169 -0
  210. package/types/publickey.d.cts +202 -0
  211. package/types/script/index.d.cts +2 -0
  212. package/types/script/script.d.cts +449 -0
  213. package/types/script/write-i32-le.d.cts +2 -0
  214. package/types/script/write-push-data.d.cts +2 -0
  215. package/types/script/write-u16-le.d.cts +2 -0
  216. package/types/script/write-u32-le.d.cts +2 -0
  217. package/types/script/write-u64-le.d.cts +2 -0
  218. package/types/script/write-u8-le.d.cts +2 -0
  219. package/types/script/write-varint.d.cts +2 -0
  220. package/types/transaction/index.d.cts +2 -0
  221. package/types/transaction/input/index.d.cts +2 -0
  222. package/types/transaction/input/input.d.cts +178 -0
  223. package/types/transaction/input/multisig.d.cts +127 -0
  224. package/types/transaction/input/publickey.d.cts +44 -0
  225. package/types/transaction/input/publickeyhash.d.cts +45 -0
  226. package/types/transaction/output.d.cts +118 -0
  227. package/types/transaction/sighash.d.cts +61 -0
  228. package/types/transaction/signature.d.cts +43 -0
  229. package/types/transaction/transaction.d.cts +716 -0
  230. package/types/transaction/unspentoutput.d.cts +83 -0
  231. package/types/util/_.d.cts +26 -0
  232. package/types/util/derivation.d.cts +21 -0
  233. package/types/util/index.d.cts +5 -0
  234. package/types/util/js.d.cts +50 -0
  235. package/types/util/preconditions.d.cts +3 -0
  236. package/index.d.ts +0 -1541
  237. package/index.js +0 -74
  238. package/lib/block/index.js +0 -4
  239. package/lib/bn.js +0 -3423
  240. package/lib/crypto/hash.js +0 -2
  241. package/lib/crypto/random.browser.js +0 -28
  242. package/lib/crypto/random.js +0 -2
  243. package/lib/crypto/random.node.js +0 -11
  244. package/lib/encoding/base58check.js +0 -121
  245. package/lib/encoding/bufferreader.js +0 -212
  246. package/lib/encoding/bufferwriter.js +0 -140
  247. package/lib/encoding/varint.js +0 -75
  248. package/lib/hash-cache.js +0 -50
  249. package/lib/mnemonic/pbkdf2.js +0 -2
  250. package/lib/mnemonic/words/index.js +0 -8
  251. package/lib/script/index.js +0 -5
  252. package/lib/transaction/index.js +0 -7
  253. package/lib/transaction/input/index.js +0 -5
  254. package/lib/util/js.js +0 -90
  255. /package/{lib/encoding/decode-script-chunks.js → cjs/encoding/decode-script-chunks.cjs} +0 -0
  256. /package/{lib/encoding/encode-hex.js → cjs/encoding/encode-hex.cjs} +0 -0
  257. /package/{lib/encoding/is-hex.js → cjs/encoding/is-hex.cjs} +0 -0
  258. /package/{lib/mnemonic/words/chinese.js → cjs/mnemonic/words/chinese.cjs} +0 -0
  259. /package/{lib/mnemonic/words/english.js → cjs/mnemonic/words/english.cjs} +0 -0
  260. /package/{lib/mnemonic/words/french.js → cjs/mnemonic/words/french.cjs} +0 -0
  261. /package/{lib/mnemonic/words/italian.js → cjs/mnemonic/words/italian.cjs} +0 -0
  262. /package/{lib/mnemonic/words/japanese.js → cjs/mnemonic/words/japanese.cjs} +0 -0
  263. /package/{lib/mnemonic/words/spanish.js → cjs/mnemonic/words/spanish.cjs} +0 -0
  264. /package/{lib/script/write-i32-le.js → cjs/script/write-i32-le.cjs} +0 -0
  265. /package/{lib/script/write-push-data.js → cjs/script/write-push-data.cjs} +0 -0
  266. /package/{lib/script/write-u16-le.js → cjs/script/write-u16-le.cjs} +0 -0
  267. /package/{lib/script/write-u32-le.js → cjs/script/write-u32-le.cjs} +0 -0
  268. /package/{lib/script/write-u64-le.js → cjs/script/write-u64-le.cjs} +0 -0
  269. /package/{lib/script/write-u8-le.js → cjs/script/write-u8-le.cjs} +0 -0
  270. /package/{lib/script/write-varint.js → cjs/script/write-varint.cjs} +0 -0
  271. /package/{lib/util/_.js → cjs/util/_.cjs} +0 -0
  272. /package/test/crypto/{bn.js → bn.cjs} +0 -0
  273. /package/test/crypto/{hash.js → hash.cjs} +0 -0
  274. /package/test/crypto/{point.js → point.cjs} +0 -0
  275. /package/test/crypto/{random.js → random.cjs} +0 -0
  276. /package/test/data/{blk86756-testnet.js → blk86756-testnet.cjs} +0 -0
  277. /package/test/data/{merkleblocks.js → merkleblocks.cjs} +0 -0
  278. /package/test/encoding/{base58.js → base58.cjs} +0 -0
  279. /package/test/encoding/{base58check.js → base58check.cjs} +0 -0
  280. /package/test/encoding/{bufferreader.js → bufferreader.cjs} +0 -0
  281. /package/test/encoding/{bufferwriter.js → bufferwriter.cjs} +0 -0
  282. /package/test/encoding/{varint.js → varint.cjs} +0 -0
  283. /package/test/{index.js → index.cjs} +0 -0
  284. /package/test/message/{message.js → message.cjs} +0 -0
  285. /package/test/mnemonic/{mnemonic.js → mnemonic.cjs} +0 -0
  286. /package/test/{opcode.js → opcode.cjs} +0 -0
  287. /package/test/{privatekey.js → privatekey.cjs} +0 -0
  288. /package/test/transaction/input/{publickey.js → publickey.cjs} +0 -0
  289. /package/test/transaction/{output.js → output.cjs} +0 -0
  290. /package/test/transaction/{signature.js → signature.cjs} +0 -0
  291. /package/test/transaction/{unspentoutput.js → unspentoutput.cjs} +0 -0
  292. /package/test/util/{js.js → js.cjs} +0 -0
  293. /package/test/util/{preconditions.js → preconditions.cjs} +0 -0
@@ -0,0 +1,243 @@
1
+ 'use strict';
2
+
3
+ import assert from 'assert';
4
+ import writeU8LE from '../script/write-u8-le.js';
5
+ import writeU16LE from '../script/write-u16-le.js';
6
+ import writeU32LE from '../script/write-u32-le.js';
7
+ import writeI32LE from '../script/write-i32-le.js';
8
+ import writeVarint from '../script/write-varint.js';
9
+
10
+ /**
11
+ * BufferWriter is a utility class for efficiently writing and concatenating buffers.
12
+ * It provides methods for writing various numeric types in both little-endian and big-endian formats,
13
+ * as well as variable-length integers (varints). The class maintains an internal array of buffers
14
+ * and can efficiently concatenate them into a single buffer when needed.
15
+ *
16
+ * @class
17
+ * @example
18
+ * const writer = new BufferWriter();
19
+ * writer.writeUInt32LE(1234).writeUInt16BE(5678);
20
+ * const result = writer.toBuffer();
21
+ */
22
+ class BufferWriter {
23
+ /**
24
+ * Initializes a new BufferWriter instance.
25
+ * @param {Object} [obj] - Optional object to set initial buffer content. If not provided,
26
+ * creates an empty buffer writer with empty buffers array and length 0.
27
+ */
28
+ constructor(obj) {
29
+ if (obj) {
30
+ this.set(obj);
31
+ } else {
32
+ this.buffers = [];
33
+ this.length = 0;
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Appends a buffer to the internal buffers array and updates the total length.
39
+ * @param {Buffer} buffer - The buffer to append.
40
+ * @returns {this} Returns the instance for chaining.
41
+ */
42
+ write(buffer) {
43
+ this.buffers.push(buffer);
44
+ this.length += buffer.length;
45
+ return this;
46
+ }
47
+
48
+ /**
49
+ * Sets the internal buffers and calculates total length.
50
+ * @param {Object} obj - Object containing buffers (either `buffers` or `bufs` property)
51
+ * @returns {Object} Returns the instance for chaining
52
+ */
53
+ set(obj) {
54
+ this.buffers = obj.buffers || obj.bufs || this.buffers || [];
55
+ this.length = this.buffers.reduce(function (prev, buf) {
56
+ return prev + buf.length;
57
+ }, 0);
58
+ return this;
59
+ }
60
+
61
+ /**
62
+ * Returns the buffer by concatenating all written data.
63
+ * @returns {Buffer} The concatenated buffer.
64
+ */
65
+ concat() {
66
+ return this.toBuffer();
67
+ }
68
+
69
+ /**
70
+ * Converts the internal buffer chunks into a single Buffer.
71
+ * If there's only one chunk, returns it directly. Otherwise,
72
+ * concatenates all chunks into a new Buffer.
73
+ * @returns {Buffer} The combined buffer
74
+ */
75
+ toBuffer() {
76
+ if (this.buffers.length === 1) {
77
+ return Buffer.from(this.buffers[0]);
78
+ }
79
+
80
+ const whole = new Uint8Array(this.length);
81
+
82
+ let offset = 0;
83
+ this.buffers.forEach((part) => {
84
+ whole.set(part, offset);
85
+ offset += part.length;
86
+ });
87
+
88
+ return Buffer.from(whole);
89
+ }
90
+
91
+ /**
92
+ * Writes a buffer in reverse order to the current buffer.
93
+ * @param {Buffer} buf - The buffer to be written in reverse.
94
+ * @returns {this} Returns the instance for chaining.
95
+ */
96
+ writeReverse(buf) {
97
+ assert(Buffer.isBuffer(buf));
98
+ this.write(Buffer.from(buf).reverse());
99
+ return this;
100
+ }
101
+
102
+ /**
103
+ * Writes a 16-bit unsigned integer in little-endian format.
104
+ * @param {number} n - The number to write.
105
+ * @returns {this} Returns the instance for chaining.
106
+ */
107
+ writeUInt16LE(n) {
108
+ writeU16LE(this, n);
109
+ return this;
110
+ }
111
+
112
+ /**
113
+ * Writes a 16-bit unsigned integer in big-endian byte order.
114
+ * Internally converts the value to little-endian and reverses the bytes.
115
+ * @param {number} n - The number to write (0-65535).
116
+ * @returns {BufferWriter} Returns the BufferWriter instance for chaining.
117
+ */
118
+ writeUInt16BE(n) {
119
+ var bw = new BufferWriter();
120
+ bw.writeUInt16LE(n);
121
+ this.writeReverse(bw.toBuffer());
122
+ return this;
123
+ }
124
+
125
+ /**
126
+ * Writes a 32-bit unsigned integer in little-endian format.
127
+ * @param {number} n - The number to write.
128
+ * @returns {this} Returns the instance for chaining.
129
+ */
130
+ writeUInt32LE(n) {
131
+ writeU32LE(this, n);
132
+ return this;
133
+ }
134
+
135
+ /**
136
+ * Writes a 32-bit unsigned integer in big-endian format.
137
+ * @param {number} n - The number to write.
138
+ * @returns {BufferWriter} Returns the BufferWriter instance for chaining.
139
+ */
140
+ writeUInt32BE(n) {
141
+ var bw = new BufferWriter();
142
+ bw.writeUInt32LE(n);
143
+ this.writeReverse(bw.toBuffer());
144
+ return this;
145
+ }
146
+
147
+ /**
148
+ * Writes an unsigned 8-bit integer to the buffer in little-endian format.
149
+ * @param {number} n - The number to write (0-255)
150
+ * @returns {this} Returns the BufferWriter instance for chaining
151
+ */
152
+ writeUInt8(n) {
153
+ writeU8LE(this, n);
154
+ return this;
155
+ }
156
+
157
+ /**
158
+ * Writes a 64-bit unsigned integer in little-endian byte order from a BigNumber.
159
+ * @param {Object} bn - The BigNumber to write.
160
+ * @returns {this} Returns the BufferWriter instance for chaining.
161
+ */
162
+ writeUInt64LEBN(bn) {
163
+ var buf = bn.toBuffer({ size: 8 });
164
+ this.writeReverse(buf);
165
+ return this;
166
+ }
167
+
168
+ /**
169
+ * Writes a 64-bit unsigned integer in big-endian byte order (as BN.js instance).
170
+ * Internally converts to little-endian and writes reversed for big-endian output.
171
+ * @param {BN} bn - The BigNumber to write as 64-bit big-endian
172
+ * @returns {BufferWriter} Returns this instance for chaining
173
+ */
174
+ writeUInt64BEBN(bn) {
175
+ var bw = new BufferWriter();
176
+ bw.writeUInt64LEBN(bn);
177
+ this.writeReverse(bw.toBuffer());
178
+ return this;
179
+ }
180
+
181
+ /**
182
+ * Writes a variable-length integer (varint) to the buffer.
183
+ * @param {number} n - The number to write as varint
184
+ * @returns {this} Returns the BufferWriter instance for chaining
185
+ */
186
+ writeVarintNum(n) {
187
+ writeVarint(this, n);
188
+ return this;
189
+ }
190
+
191
+ /**
192
+ * Writes a 32-bit signed integer in little-endian format to the buffer.
193
+ * @param {number} n - The integer to write.
194
+ * @returns {this} Returns the BufferWriter instance for chaining.
195
+ */
196
+ writeInt32LE(n) {
197
+ writeI32LE(this, n);
198
+ return this;
199
+ }
200
+
201
+ /**
202
+ * Converts a number to a varint-encoded Buffer.
203
+ * @param {number} n - The number to encode.
204
+ * @returns {Buffer} The varint-encoded Buffer.
205
+ */
206
+ static varintBufNum(n) {
207
+ var bw = new BufferWriter();
208
+ bw.writeVarintNum(n);
209
+ return bw.toBuffer();
210
+ }
211
+
212
+ /**
213
+ * Writes a variable-length integer (varint) to the buffer using BigNumber.
214
+ * Handles numbers of different sizes with appropriate encoding:
215
+ * - Numbers < 253: 1 byte
216
+ * - Numbers < 0x10000: 1 byte prefix (253) + 2 bytes
217
+ * - Numbers < 0x100000000: 1 byte prefix (254) + 4 bytes
218
+ * - Larger numbers: 1 byte prefix (255) + 8 bytes
219
+ * @param {BN} bn - BigNumber to write as varint
220
+ * @returns {BufferWriter} Returns this for chaining
221
+ */
222
+ writeVarintBN(bn) {
223
+ var n = bn.toNumber();
224
+ if (n < 253) {
225
+ writeU8LE(this, n);
226
+ } else if (n < 0x10000) {
227
+ writeU8LE(this, 253);
228
+ writeU16LE(this, n);
229
+ } else if (n < 0x100000000) {
230
+ writeU8LE(this, 254);
231
+ writeU32LE(this, n);
232
+ } else {
233
+ var bw = new BufferWriter();
234
+ bw.writeUInt8(255);
235
+ bw.writeUInt64LEBN(bn);
236
+ var buf = bw.toBuffer();
237
+ this.write(buf);
238
+ }
239
+ return this;
240
+ }
241
+ }
242
+
243
+ export default BufferWriter;
@@ -0,0 +1,24 @@
1
+ import decodeHex from './decode-hex.js';
2
+ import opcodes from '../opcode.js';
3
+ import BufferWriter from './bufferwriter.js';
4
+ import writePushData from '../script/write-push-data.js';
5
+
6
+ function decodeASM(script) {
7
+ const parts = script.split(' ');
8
+ const writer = new BufferWriter();
9
+ parts.forEach((part) => {
10
+ if (part in opcodes) {
11
+ writer.write([opcodes[part]]);
12
+ } else if (part === '0') {
13
+ writer.write([opcodes.OP_0]);
14
+ } else if (part === '-1') {
15
+ writer.write([opcodes.OP_1NEGATE]);
16
+ } else {
17
+ const buf = decodeHex(part);
18
+ writePushData(writer, buf);
19
+ }
20
+ });
21
+ return writer.toBuffer();
22
+ }
23
+
24
+ export default decodeASM;
@@ -0,0 +1,32 @@
1
+ /* global VARIANT */
2
+
3
+ import isHex from './is-hex.js';
4
+
5
+ // Prefer our implementation of decodeHex over Buffer when we don't know the VARIANT
6
+ // to avoid accidentally importing the Buffer shim in the browser.
7
+
8
+ function decodeHex(hex) {
9
+ if (typeof hex !== 'string') throw new Error('not a string');
10
+
11
+ if (hex.startsWith('0x')) hex = hex.slice(2);
12
+
13
+ if (hex.length % 2 === 1) hex = '0' + hex;
14
+
15
+ if (!isHex(hex)) throw new Error('invalid hex string in script');
16
+
17
+ if (typeof VARIANT === 'undefined' || VARIANT === 'browser') {
18
+ const length = hex.length / 2;
19
+ const arr = new Uint8Array(length);
20
+ const isNaN = (x) => x !== x;
21
+ for (let i = 0; i < length; ++i) {
22
+ const byte = parseInt(hex.substr(i * 2, 2), 16);
23
+ if (isNaN(byte)) throw new Error('bad hex char');
24
+ arr[i] = byte;
25
+ }
26
+ return arr;
27
+ } else {
28
+ return Buffer.from(hex, 'hex');
29
+ }
30
+ }
31
+
32
+ export default decodeHex;
@@ -0,0 +1,43 @@
1
+ function decodeScriptChunks(script) {
2
+ const chunks = [];
3
+ let i = 0;
4
+ let len = 0;
5
+ while (i < script.length) {
6
+ const opcodenum = script[i];
7
+ i += 1;
8
+ if (opcodenum === 0) {
9
+ len = opcodenum;
10
+ chunks.push({ opcodenum: opcodenum, len });
11
+ } else if (opcodenum < 76) {
12
+ // OP_PUSHDATA1
13
+ len = opcodenum;
14
+ chunks.push({ opcodenum: opcodenum, buf: script.slice(i, i + opcodenum), len });
15
+ i += opcodenum;
16
+ } else if (opcodenum === 76) {
17
+ // OP_PUSHDATA1
18
+ len = script[i];
19
+ i += 1;
20
+ chunks.push({ opcodenum: opcodenum, buf: script.slice(i, i + len), len });
21
+ i += len;
22
+ } else if (opcodenum === 77) {
23
+ // OP_PUSHDATA2
24
+ len = script[i] | (script[i + 1] << 8);
25
+ i += 2;
26
+ chunks.push({ opcodenum: opcodenum, buf: script.slice(i, i + len), len });
27
+ i += len;
28
+ } else if (opcodenum === 78) {
29
+ // OP_PUSHDATA4
30
+ len =
31
+ script[i] + script[i + 1] * 0x0100 + script[i + 2] * 0x010000 + script[i + 3] * 0x01000000;
32
+ i += 4;
33
+ chunks.push({ opcodenum: opcodenum, buf: script.slice(i, i + len), len });
34
+ i += len;
35
+ } else {
36
+ chunks.push({ opcodenum: opcodenum });
37
+ }
38
+ }
39
+ // if (i !== script.length) throw new Error('bad script')
40
+ return chunks;
41
+ }
42
+
43
+ export default decodeScriptChunks;
@@ -0,0 +1,284 @@
1
+ /* global VARIANT */
2
+
3
+ let encodeHex = null;
4
+
5
+ // Prefer our implementation of encodeHex over Buffer when we don't know the VARIANT
6
+ // to avoid accidentally importing the Buffer shim in the browser.
7
+
8
+ if (typeof VARIANT === 'undefined' || VARIANT === 'browser') {
9
+ // In the browser, a lookup table is fastest, and faster than the Buffer toString shim.
10
+ const HEX = [
11
+ '00',
12
+ '01',
13
+ '02',
14
+ '03',
15
+ '04',
16
+ '05',
17
+ '06',
18
+ '07',
19
+ '08',
20
+ '09',
21
+ '0a',
22
+ '0b',
23
+ '0c',
24
+ '0d',
25
+ '0e',
26
+ '0f',
27
+ '10',
28
+ '11',
29
+ '12',
30
+ '13',
31
+ '14',
32
+ '15',
33
+ '16',
34
+ '17',
35
+ '18',
36
+ '19',
37
+ '1a',
38
+ '1b',
39
+ '1c',
40
+ '1d',
41
+ '1e',
42
+ '1f',
43
+ '20',
44
+ '21',
45
+ '22',
46
+ '23',
47
+ '24',
48
+ '25',
49
+ '26',
50
+ '27',
51
+ '28',
52
+ '29',
53
+ '2a',
54
+ '2b',
55
+ '2c',
56
+ '2d',
57
+ '2e',
58
+ '2f',
59
+ '30',
60
+ '31',
61
+ '32',
62
+ '33',
63
+ '34',
64
+ '35',
65
+ '36',
66
+ '37',
67
+ '38',
68
+ '39',
69
+ '3a',
70
+ '3b',
71
+ '3c',
72
+ '3d',
73
+ '3e',
74
+ '3f',
75
+ '40',
76
+ '41',
77
+ '42',
78
+ '43',
79
+ '44',
80
+ '45',
81
+ '46',
82
+ '47',
83
+ '48',
84
+ '49',
85
+ '4a',
86
+ '4b',
87
+ '4c',
88
+ '4d',
89
+ '4e',
90
+ '4f',
91
+ '50',
92
+ '51',
93
+ '52',
94
+ '53',
95
+ '54',
96
+ '55',
97
+ '56',
98
+ '57',
99
+ '58',
100
+ '59',
101
+ '5a',
102
+ '5b',
103
+ '5c',
104
+ '5d',
105
+ '5e',
106
+ '5f',
107
+ '60',
108
+ '61',
109
+ '62',
110
+ '63',
111
+ '64',
112
+ '65',
113
+ '66',
114
+ '67',
115
+ '68',
116
+ '69',
117
+ '6a',
118
+ '6b',
119
+ '6c',
120
+ '6d',
121
+ '6e',
122
+ '6f',
123
+ '70',
124
+ '71',
125
+ '72',
126
+ '73',
127
+ '74',
128
+ '75',
129
+ '76',
130
+ '77',
131
+ '78',
132
+ '79',
133
+ '7a',
134
+ '7b',
135
+ '7c',
136
+ '7d',
137
+ '7e',
138
+ '7f',
139
+ '80',
140
+ '81',
141
+ '82',
142
+ '83',
143
+ '84',
144
+ '85',
145
+ '86',
146
+ '87',
147
+ '88',
148
+ '89',
149
+ '8a',
150
+ '8b',
151
+ '8c',
152
+ '8d',
153
+ '8e',
154
+ '8f',
155
+ '90',
156
+ '91',
157
+ '92',
158
+ '93',
159
+ '94',
160
+ '95',
161
+ '96',
162
+ '97',
163
+ '98',
164
+ '99',
165
+ '9a',
166
+ '9b',
167
+ '9c',
168
+ '9d',
169
+ '9e',
170
+ '9f',
171
+ 'a0',
172
+ 'a1',
173
+ 'a2',
174
+ 'a3',
175
+ 'a4',
176
+ 'a5',
177
+ 'a6',
178
+ 'a7',
179
+ 'a8',
180
+ 'a9',
181
+ 'aa',
182
+ 'ab',
183
+ 'ac',
184
+ 'ad',
185
+ 'ae',
186
+ 'af',
187
+ 'b0',
188
+ 'b1',
189
+ 'b2',
190
+ 'b3',
191
+ 'b4',
192
+ 'b5',
193
+ 'b6',
194
+ 'b7',
195
+ 'b8',
196
+ 'b9',
197
+ 'ba',
198
+ 'bb',
199
+ 'bc',
200
+ 'bd',
201
+ 'be',
202
+ 'bf',
203
+ 'c0',
204
+ 'c1',
205
+ 'c2',
206
+ 'c3',
207
+ 'c4',
208
+ 'c5',
209
+ 'c6',
210
+ 'c7',
211
+ 'c8',
212
+ 'c9',
213
+ 'ca',
214
+ 'cb',
215
+ 'cc',
216
+ 'cd',
217
+ 'ce',
218
+ 'cf',
219
+ 'd0',
220
+ 'd1',
221
+ 'd2',
222
+ 'd3',
223
+ 'd4',
224
+ 'd5',
225
+ 'd6',
226
+ 'd7',
227
+ 'd8',
228
+ 'd9',
229
+ 'da',
230
+ 'db',
231
+ 'dc',
232
+ 'dd',
233
+ 'de',
234
+ 'df',
235
+ 'e0',
236
+ 'e1',
237
+ 'e2',
238
+ 'e3',
239
+ 'e4',
240
+ 'e5',
241
+ 'e6',
242
+ 'e7',
243
+ 'e8',
244
+ 'e9',
245
+ 'ea',
246
+ 'eb',
247
+ 'ec',
248
+ 'ed',
249
+ 'ee',
250
+ 'ef',
251
+ 'f0',
252
+ 'f1',
253
+ 'f2',
254
+ 'f3',
255
+ 'f4',
256
+ 'f5',
257
+ 'f6',
258
+ 'f7',
259
+ 'f8',
260
+ 'f9',
261
+ 'fa',
262
+ 'fb',
263
+ 'fc',
264
+ 'fd',
265
+ 'fe',
266
+ 'ff',
267
+ ];
268
+
269
+ encodeHex = function (buffer) {
270
+ const len = buffer.length;
271
+ const out = new Array(len);
272
+ for (let i = 0; i < len; ++i) {
273
+ out[i] = HEX[buffer[i]];
274
+ }
275
+ return out.join('');
276
+ };
277
+ } else {
278
+ // Buffer toString('hex') in Node is faster than any JavaScript we could write
279
+ encodeHex = function (buffer) {
280
+ return buffer instanceof Buffer ? buffer.toString('hex') : Buffer.from(buffer).toString('hex');
281
+ };
282
+ }
283
+
284
+ export default encodeHex;
@@ -0,0 +1,14 @@
1
+ import Base58 from './base58.js';
2
+ import Base58Check from './base58check.js';
3
+ import BufferReader from './bufferreader.js';
4
+ import BufferWriter from './bufferwriter.js';
5
+ import Varint from './varint.js';
6
+
7
+
8
+ export default {
9
+ Base58,
10
+ Base58Check,
11
+ BufferReader,
12
+ BufferWriter,
13
+ Varint,
14
+ };
@@ -0,0 +1,7 @@
1
+ const HEX_REGEX = /^(?:[a-fA-F0-9][a-fA-F0-9])*$/;
2
+
3
+ function isHex(s) {
4
+ return HEX_REGEX.test(s);
5
+ }
6
+
7
+ export default isHex;