@opcat-labs/opcat 1.0.1 → 1.0.2

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 +6 -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,333 @@
1
+ 'use strict';
2
+
3
+ var _ = require('../util/_.cjs');
4
+ var $ = require('../util/preconditions.cjs');
5
+ var BN = require('../crypto/bn.cjs');
6
+
7
+ /**
8
+ * Creates a BufferReader instance to read from various input types.
9
+ * @constructor
10
+ * @param {Buffer|string|Object} buf - Input source (Buffer, hex string, or object with buffer properties)
11
+ * @throws {TypeError} If input is invalid hex string or unrecognized type
12
+ * @example
13
+ * new BufferReader(Buffer.from('abc')) // from Buffer
14
+ * new BufferReader('616263') // from hex string
15
+ * new BufferReader({buf: buffer}) // from object
16
+ */
17
+ function BufferReader(buf) {
18
+ if (!(this instanceof BufferReader)) {
19
+ return new BufferReader(buf);
20
+ }
21
+ if (_.isUndefined(buf)) {
22
+ return;
23
+ }
24
+ if (Buffer.isBuffer(buf)) {
25
+ this.set({
26
+ buf: buf,
27
+ });
28
+ } else if (_.isString(buf)) {
29
+ var b = Buffer.from(buf, 'hex');
30
+ if (b.length * 2 !== buf.length) {
31
+ throw new TypeError('Invalid hex string');
32
+ }
33
+
34
+ this.set({
35
+ buf: b,
36
+ });
37
+ } else if (_.isObject(buf)) {
38
+ var obj = buf;
39
+ this.set(obj);
40
+ } else {
41
+ throw new TypeError('Unrecognized argument for BufferReader');
42
+ }
43
+ };
44
+
45
+ /**
46
+ * Updates the buffer and position from the given object.
47
+ * @param {Object} obj - The object containing buffer and position to set.
48
+ * @param {Buffer} [obj.buf] - The buffer to set (optional, keeps current if not provided).
49
+ * @param {number} [obj.pos] - The position to set (optional, keeps current if not provided).
50
+ * @returns {BufferReader} Returns the instance for chaining.
51
+ */
52
+ BufferReader.prototype.set = function (obj) {
53
+ this.buf = obj.buf || this.buf || undefined;
54
+ this.pos = obj.pos || this.pos || 0;
55
+ return this;
56
+ };
57
+
58
+ /**
59
+ * Checks if the reader has reached the end of the buffer.
60
+ * @returns {boolean} True if the current position is at or beyond the buffer length, false otherwise.
61
+ */
62
+ BufferReader.prototype.eof = function () {
63
+ return this.pos >= this.buf.length;
64
+ };
65
+
66
+ /**
67
+ * Alias for `eof` method - checks if the buffer reader has reached the end of data.
68
+ * @name BufferReader.prototype.finished
69
+ * @memberof BufferReader
70
+ * @instance
71
+ */
72
+ BufferReader.prototype.finished = BufferReader.prototype.eof;
73
+
74
+ /**
75
+ * Reads a specified number of bytes from the buffer and advances the position.
76
+ * @param {number} len - The number of bytes to read.
77
+ * @returns {Buffer} The read bytes as a Buffer.
78
+ * @throws {Error} If the length is undefined.
79
+ */
80
+ BufferReader.prototype.read = function (len) {
81
+ $.checkArgument(!_.isUndefined(len), 'Must specify a length');
82
+ var buf = this.buf.slice(this.pos, this.pos + len);
83
+ this.pos = this.pos + len;
84
+ return buf;
85
+ };
86
+
87
+ /**
88
+ * Reads and returns all remaining bytes from the buffer, advancing the position to the end.
89
+ * @returns {Buffer} The remaining bytes in the buffer.
90
+ */
91
+ BufferReader.prototype.readAll = function () {
92
+ var buf = this.buf.slice(this.pos, this.buf.length);
93
+ this.pos = this.buf.length;
94
+ return buf;
95
+ };
96
+
97
+ /**
98
+ * Reads an unsigned 8-bit integer from the buffer at the current position.
99
+ * @returns {number} The unsigned 8-bit integer value read.
100
+ * @this {BufferReader}
101
+ */
102
+ BufferReader.prototype.readUInt8 = function () {
103
+ var val = this.buf.readUInt8(this.pos);
104
+ this.pos = this.pos + 1;
105
+ return val;
106
+ };
107
+
108
+ /**
109
+ * Reads an unsigned 16-bit integer from the buffer in big-endian format.
110
+ * Advances the position by 2 bytes.
111
+ * @returns {number} The read unsigned 16-bit integer value.
112
+ */
113
+ BufferReader.prototype.readUInt16BE = function () {
114
+ var val = this.buf.readUInt16BE(this.pos);
115
+ this.pos = this.pos + 2;
116
+ return val;
117
+ };
118
+
119
+ /**
120
+ * Reads a 16-bit unsigned integer from the buffer in little-endian format
121
+ * and advances the position by 2 bytes.
122
+ * @returns {number} The read unsigned 16-bit integer value
123
+ */
124
+ BufferReader.prototype.readUInt16LE = function () {
125
+ var val = this.buf.readUInt16LE(this.pos);
126
+ this.pos = this.pos + 2;
127
+ return val;
128
+ };
129
+
130
+ /**
131
+ * Reads an unsigned 32-bit integer from the buffer in big-endian format.
132
+ * Advances the position by 4 bytes.
133
+ * @returns {number} The read unsigned 32-bit integer value.
134
+ */
135
+ BufferReader.prototype.readUInt32BE = function () {
136
+ var val = this.buf.readUInt32BE(this.pos);
137
+ this.pos = this.pos + 4;
138
+ return val;
139
+ };
140
+
141
+ /**
142
+ * Reads an unsigned 32-bit integer from the buffer in little-endian format.
143
+ * Advances the position by 4 bytes.
144
+ * @returns {number} The read unsigned 32-bit integer value.
145
+ */
146
+ BufferReader.prototype.readUInt32LE = function () {
147
+ var val = this.buf.readUInt32LE(this.pos);
148
+ this.pos = this.pos + 4;
149
+ return val;
150
+ };
151
+
152
+ /**
153
+ * Reads a 32-bit signed integer from the buffer in little-endian format.
154
+ * Advances the position by 4 bytes.
155
+ * @returns {number} The read 32-bit signed integer.
156
+ */
157
+ BufferReader.prototype.readInt32LE = function () {
158
+ var val = this.buf.readInt32LE(this.pos);
159
+ this.pos = this.pos + 4;
160
+ return val;
161
+ };
162
+
163
+ /**
164
+ * Reads an unsigned 64-bit integer in big-endian byte order from the buffer
165
+ * and returns it as a BN (BigNumber) object.
166
+ * Advances the position by 8 bytes.
167
+ * @returns {BN} The parsed 64-bit unsigned integer as a BigNumber
168
+ */
169
+ BufferReader.prototype.readUInt64BEBN = function () {
170
+ var buf = this.buf.slice(this.pos, this.pos + 8);
171
+ var bn = BN.fromBuffer(buf);
172
+ this.pos = this.pos + 8;
173
+ return bn;
174
+ };
175
+
176
+ /**
177
+ * Reads an unsigned 64-bit integer in little-endian byte order from the buffer and returns it as a BN (BigNumber).
178
+ * Optimizes for numbers <= 52 bits by using numeric constructor, falls back to buffer slice for larger numbers.
179
+ * Advances the buffer position by 8 bytes.
180
+ * @returns {BN} The parsed 64-bit unsigned integer as a BN instance.
181
+ */
182
+ BufferReader.prototype.readUInt64LEBN = function () {
183
+ var second = this.buf.readUInt32LE(this.pos);
184
+ var first = this.buf.readUInt32LE(this.pos + 4);
185
+ var combined = first * 0x100000000 + second;
186
+ // Instantiating an instance of BN with a number is faster than with an
187
+ // array or string. However, the maximum safe number for a double precision
188
+ // floating point is 2 ^ 52 - 1 (0x1fffffffffffff), thus we can safely use
189
+ // non-floating point numbers less than this amount (52 bits). And in the case
190
+ // that the number is larger, we can instatiate an instance of BN by passing
191
+ // an array from the buffer (slower) and specifying the endianness.
192
+ var bn;
193
+ if (combined <= 0x1fffffffffffff) {
194
+ bn = new BN(combined);
195
+ } else {
196
+ var data = Array.prototype.slice.call(this.buf, this.pos, this.pos + 8);
197
+ bn = new BN(data, 10, 'le');
198
+ }
199
+ this.pos = this.pos + 8;
200
+ return bn;
201
+ };
202
+
203
+ /**
204
+ * Reads a variable-length integer (varint) from the buffer and returns it as a number.
205
+ * Supports varints up to 53 bits (JavaScript's safe integer limit).
206
+ * For larger numbers, throws an error suggesting to use `readVarintBN` instead.
207
+ * @returns {number} The decoded integer value
208
+ * @throws {Error} If the number exceeds 53-bit precision
209
+ */
210
+ BufferReader.prototype.readVarintNum = function () {
211
+ var first = this.readUInt8();
212
+ switch (first) {
213
+ case 0xfd:
214
+ return this.readUInt16LE();
215
+ case 0xfe:
216
+ return this.readUInt32LE();
217
+ case 0xff:
218
+ var bn = this.readUInt64LEBN();
219
+ var n = bn.toNumber();
220
+ if (n <= Math.pow(2, 53)) {
221
+ return n;
222
+ } else {
223
+ throw new Error('number too large to retain precision - use readVarintBN');
224
+ }
225
+ // break // unreachable
226
+ default:
227
+ return first;
228
+ }
229
+ };
230
+
231
+
232
+ /**
233
+ * Reads a variable-length buffer from the current position.
234
+ * First reads a varint to determine the length, then reads the buffer of that size.
235
+ * @returns {Buffer} The read buffer.
236
+ * @throws {Error} If the actual read length doesn't match the expected length.
237
+ */
238
+ BufferReader.prototype.readVarLengthBuffer = function () {
239
+ var len = this.readVarintNum();
240
+ var buf = this.read(len);
241
+ $.checkState(
242
+ buf.length === len,
243
+ 'Invalid length while reading varlength buffer. ' +
244
+ 'Expected to read: ' +
245
+ len +
246
+ ' and read ' +
247
+ buf.length,
248
+ );
249
+ return buf;
250
+ };
251
+
252
+ /**
253
+ * Reads a variable-length integer (varint) from the buffer.
254
+ * The first byte determines the length of the varint:
255
+ * - 0xfd: 2-byte varint (plus 1 byte for the prefix)
256
+ * - 0xfe: 4-byte varint (plus 1 byte for the prefix)
257
+ * - 0xff: 8-byte varint (plus 1 byte for the prefix)
258
+ * - Otherwise: 1-byte varint (no prefix)
259
+ * @returns {Buffer} The varint bytes including the prefix (if any)
260
+ */
261
+ BufferReader.prototype.readVarintBuf = function () {
262
+ var first = this.buf.readUInt8(this.pos);
263
+ switch (first) {
264
+ case 0xfd:
265
+ return this.read(1 + 2);
266
+ case 0xfe:
267
+ return this.read(1 + 4);
268
+ case 0xff:
269
+ return this.read(1 + 8);
270
+ default:
271
+ return this.read(1);
272
+ }
273
+ };
274
+
275
+ /**
276
+ * Reads a variable-length integer (varint) from the buffer and returns it as a BN (BigNumber).
277
+ * Handles different varint sizes (1, 2, 4, or 8 bytes) based on the first byte's value:
278
+ * - 0xfd: reads next 2 bytes as uint16
279
+ * - 0xfe: reads next 4 bytes as uint32
280
+ * - 0xff: reads next 8 bytes as uint64
281
+ * - default: treats first byte as uint8
282
+ * @returns {BN} The parsed varint as a BigNumber
283
+ */
284
+ BufferReader.prototype.readVarintBN = function () {
285
+ var first = this.readUInt8();
286
+ switch (first) {
287
+ case 0xfd:
288
+ return new BN(this.readUInt16LE());
289
+ case 0xfe:
290
+ return new BN(this.readUInt32LE());
291
+ case 0xff:
292
+ return this.readUInt64LEBN();
293
+ default:
294
+ return new BN(first);
295
+ }
296
+ };
297
+
298
+ /**
299
+ * Reverses the order of bytes in the internal buffer and returns the instance.
300
+ * @returns {BufferReader} The modified BufferReader instance for chaining.
301
+ */
302
+ BufferReader.prototype.reverse = function () {
303
+ var buf = Buffer.alloc(this.buf.length);
304
+ for (var i = 0; i < buf.length; i++) {
305
+ buf[i] = this.buf[this.buf.length - 1 - i];
306
+ }
307
+ this.buf = buf;
308
+ return this;
309
+ };
310
+
311
+ /**
312
+ * Reads a specified number of bytes from the buffer in reverse order.
313
+ * @param {number} [len] - The number of bytes to read. If omitted, reads the entire buffer.
314
+ * @returns {Buffer} The reversed buffer segment.
315
+ */
316
+ BufferReader.prototype.readReverse = function (len) {
317
+ if (_.isUndefined(len)) {
318
+ len = this.buf.length;
319
+ }
320
+ var buf = this.buf.slice(this.pos, this.pos + len);
321
+ this.pos = this.pos + len;
322
+ return Buffer.from(buf).reverse();
323
+ };
324
+
325
+ /**
326
+ * Gets the number of bytes remaining to be read in the buffer.
327
+ * @returns {number} The remaining bytes count.
328
+ */
329
+ BufferReader.prototype.remaining = function () {
330
+ return this.buf.length - this.pos;
331
+ };
332
+
333
+ module.exports = BufferReader;
@@ -0,0 +1,244 @@
1
+ 'use strict';
2
+
3
+ var assert = require('assert');
4
+
5
+ const writeU8LE = require('../script/write-u8-le.cjs');
6
+ const writeU16LE = require('../script/write-u16-le.cjs');
7
+ const writeU32LE = require('../script/write-u32-le.cjs');
8
+ const writeI32LE = require('../script/write-i32-le.cjs');
9
+ const writeVarint = require('../script/write-varint.cjs');
10
+
11
+ /**
12
+ * BufferWriter is a utility class for efficiently writing and concatenating buffers.
13
+ * It provides methods for writing various numeric types in both little-endian and big-endian formats,
14
+ * as well as variable-length integers (varints). The class maintains an internal array of buffers
15
+ * and can efficiently concatenate them into a single buffer when needed.
16
+ *
17
+ * @class
18
+ * @example
19
+ * const writer = new BufferWriter();
20
+ * writer.writeUInt32LE(1234).writeUInt16BE(5678);
21
+ * const result = writer.toBuffer();
22
+ */
23
+ class BufferWriter {
24
+ /**
25
+ * Initializes a new BufferWriter instance.
26
+ * @param {Object} [obj] - Optional object to set initial buffer content. If not provided,
27
+ * creates an empty buffer writer with empty buffers array and length 0.
28
+ */
29
+ constructor(obj) {
30
+ if (obj) {
31
+ this.set(obj);
32
+ } else {
33
+ this.buffers = [];
34
+ this.length = 0;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Appends a buffer to the internal buffers array and updates the total length.
40
+ * @param {Buffer} buffer - The buffer to append.
41
+ * @returns {this} Returns the instance for chaining.
42
+ */
43
+ write(buffer) {
44
+ this.buffers.push(buffer);
45
+ this.length += buffer.length;
46
+ return this;
47
+ }
48
+
49
+ /**
50
+ * Sets the internal buffers and calculates total length.
51
+ * @param {Object} obj - Object containing buffers (either `buffers` or `bufs` property)
52
+ * @returns {Object} Returns the instance for chaining
53
+ */
54
+ set(obj) {
55
+ this.buffers = obj.buffers || obj.bufs || this.buffers || [];
56
+ this.length = this.buffers.reduce(function (prev, buf) {
57
+ return prev + buf.length;
58
+ }, 0);
59
+ return this;
60
+ }
61
+
62
+ /**
63
+ * Returns the buffer by concatenating all written data.
64
+ * @returns {Buffer} The concatenated buffer.
65
+ */
66
+ concat() {
67
+ return this.toBuffer();
68
+ }
69
+
70
+ /**
71
+ * Converts the internal buffer chunks into a single Buffer.
72
+ * If there's only one chunk, returns it directly. Otherwise,
73
+ * concatenates all chunks into a new Buffer.
74
+ * @returns {Buffer} The combined buffer
75
+ */
76
+ toBuffer() {
77
+ if (this.buffers.length === 1) {
78
+ return Buffer.from(this.buffers[0]);
79
+ }
80
+
81
+ const whole = new Uint8Array(this.length);
82
+
83
+ let offset = 0;
84
+ this.buffers.forEach((part) => {
85
+ whole.set(part, offset);
86
+ offset += part.length;
87
+ });
88
+
89
+ return Buffer.from(whole);
90
+ }
91
+
92
+ /**
93
+ * Writes a buffer in reverse order to the current buffer.
94
+ * @param {Buffer} buf - The buffer to be written in reverse.
95
+ * @returns {this} Returns the instance for chaining.
96
+ */
97
+ writeReverse(buf) {
98
+ assert(Buffer.isBuffer(buf));
99
+ this.write(Buffer.from(buf).reverse());
100
+ return this;
101
+ }
102
+
103
+ /**
104
+ * Writes a 16-bit unsigned integer in little-endian format.
105
+ * @param {number} n - The number to write.
106
+ * @returns {this} Returns the instance for chaining.
107
+ */
108
+ writeUInt16LE(n) {
109
+ writeU16LE(this, n);
110
+ return this;
111
+ }
112
+
113
+ /**
114
+ * Writes a 16-bit unsigned integer in big-endian byte order.
115
+ * Internally converts the value to little-endian and reverses the bytes.
116
+ * @param {number} n - The number to write (0-65535).
117
+ * @returns {BufferWriter} Returns the BufferWriter instance for chaining.
118
+ */
119
+ writeUInt16BE(n) {
120
+ var bw = new BufferWriter();
121
+ bw.writeUInt16LE(n);
122
+ this.writeReverse(bw.toBuffer());
123
+ return this;
124
+ }
125
+
126
+ /**
127
+ * Writes a 32-bit unsigned integer in little-endian format.
128
+ * @param {number} n - The number to write.
129
+ * @returns {this} Returns the instance for chaining.
130
+ */
131
+ writeUInt32LE(n) {
132
+ writeU32LE(this, n);
133
+ return this;
134
+ }
135
+
136
+ /**
137
+ * Writes a 32-bit unsigned integer in big-endian format.
138
+ * @param {number} n - The number to write.
139
+ * @returns {BufferWriter} Returns the BufferWriter instance for chaining.
140
+ */
141
+ writeUInt32BE(n) {
142
+ var bw = new BufferWriter();
143
+ bw.writeUInt32LE(n);
144
+ this.writeReverse(bw.toBuffer());
145
+ return this;
146
+ }
147
+
148
+ /**
149
+ * Writes an unsigned 8-bit integer to the buffer in little-endian format.
150
+ * @param {number} n - The number to write (0-255)
151
+ * @returns {this} Returns the BufferWriter instance for chaining
152
+ */
153
+ writeUInt8(n) {
154
+ writeU8LE(this, n);
155
+ return this;
156
+ }
157
+
158
+ /**
159
+ * Writes a 64-bit unsigned integer in little-endian byte order from a BigNumber.
160
+ * @param {Object} bn - The BigNumber to write.
161
+ * @returns {this} Returns the BufferWriter instance for chaining.
162
+ */
163
+ writeUInt64LEBN(bn) {
164
+ var buf = bn.toBuffer({ size: 8 });
165
+ this.writeReverse(buf);
166
+ return this;
167
+ }
168
+
169
+ /**
170
+ * Writes a 64-bit unsigned integer in big-endian byte order (as BN.js instance).
171
+ * Internally converts to little-endian and writes reversed for big-endian output.
172
+ * @param {BN} bn - The BigNumber to write as 64-bit big-endian
173
+ * @returns {BufferWriter} Returns this instance for chaining
174
+ */
175
+ writeUInt64BEBN(bn) {
176
+ var bw = new BufferWriter();
177
+ bw.writeUInt64LEBN(bn);
178
+ this.writeReverse(bw.toBuffer());
179
+ return this;
180
+ }
181
+
182
+ /**
183
+ * Writes a variable-length integer (varint) to the buffer.
184
+ * @param {number} n - The number to write as varint
185
+ * @returns {this} Returns the BufferWriter instance for chaining
186
+ */
187
+ writeVarintNum(n) {
188
+ writeVarint(this, n);
189
+ return this;
190
+ }
191
+
192
+ /**
193
+ * Writes a 32-bit signed integer in little-endian format to the buffer.
194
+ * @param {number} n - The integer to write.
195
+ * @returns {this} Returns the BufferWriter instance for chaining.
196
+ */
197
+ writeInt32LE(n) {
198
+ writeI32LE(this, n);
199
+ return this;
200
+ }
201
+
202
+ /**
203
+ * Converts a number to a varint-encoded Buffer.
204
+ * @param {number} n - The number to encode.
205
+ * @returns {Buffer} The varint-encoded Buffer.
206
+ */
207
+ static varintBufNum(n) {
208
+ var bw = new BufferWriter();
209
+ bw.writeVarintNum(n);
210
+ return bw.toBuffer();
211
+ }
212
+
213
+ /**
214
+ * Writes a variable-length integer (varint) to the buffer using BigNumber.
215
+ * Handles numbers of different sizes with appropriate encoding:
216
+ * - Numbers < 253: 1 byte
217
+ * - Numbers < 0x10000: 1 byte prefix (253) + 2 bytes
218
+ * - Numbers < 0x100000000: 1 byte prefix (254) + 4 bytes
219
+ * - Larger numbers: 1 byte prefix (255) + 8 bytes
220
+ * @param {BN} bn - BigNumber to write as varint
221
+ * @returns {BufferWriter} Returns this for chaining
222
+ */
223
+ writeVarintBN(bn) {
224
+ var n = bn.toNumber();
225
+ if (n < 253) {
226
+ writeU8LE(this, n);
227
+ } else if (n < 0x10000) {
228
+ writeU8LE(this, 253);
229
+ writeU16LE(this, n);
230
+ } else if (n < 0x100000000) {
231
+ writeU8LE(this, 254);
232
+ writeU32LE(this, n);
233
+ } else {
234
+ var bw = new BufferWriter();
235
+ bw.writeUInt8(255);
236
+ bw.writeUInt64LEBN(bn);
237
+ var buf = bw.toBuffer();
238
+ this.write(buf);
239
+ }
240
+ return this;
241
+ }
242
+ }
243
+
244
+ module.exports = BufferWriter;
@@ -1,7 +1,7 @@
1
- const decodeHex = require('./decode-hex');
2
- const opcodes = require('../opcode');
3
- const BufferWriter = require('./bufferwriter');
4
- const writePushData = require('../script/write-push-data');
1
+ const decodeHex = require('./decode-hex.cjs');
2
+ const opcodes = require('../opcode.cjs');
3
+ const BufferWriter = require('./bufferwriter.cjs');
4
+ const writePushData = require('../script/write-push-data.cjs');
5
5
 
6
6
  function decodeASM(script) {
7
7
  const parts = script.split(' ');
@@ -1,6 +1,6 @@
1
1
  /* global VARIANT */
2
2
 
3
- const isHex = require('./is-hex');
3
+ const isHex = require('./is-hex.cjs');
4
4
 
5
5
  // Prefer our implementation of decodeHex over Buffer when we don't know the VARIANT
6
6
  // to avoid accidentally importing the Buffer shim in the browser.
@@ -0,0 +1,14 @@
1
+ var Base58 = require('./base58.cjs');
2
+ var Base58Check = require('./base58check.cjs');
3
+ var BufferReader = require('./bufferreader.cjs');
4
+ var BufferWriter = require('./bufferwriter.cjs');
5
+ var Varint = require('./varint.cjs');
6
+
7
+
8
+ module.exports = {
9
+ Base58,
10
+ Base58Check,
11
+ BufferReader,
12
+ BufferWriter,
13
+ Varint,
14
+ }