@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,116 @@
1
+ 'use strict';
2
+
3
+ import BufferWriter from './bufferwriter.js';
4
+ import BufferReader from './bufferreader.js';
5
+ import BN from '../crypto/bn.js';
6
+
7
+ /**
8
+ * Creates a Varint instance from various input types.
9
+ * @constructor
10
+ * @param {Buffer|number|BN|Object} buf - Input can be a Buffer, number, BN instance, or object with buffer properties.
11
+ * @returns {Varint} New Varint instance when called without `new`.
12
+ */
13
+ function Varint(buf) {
14
+ if (!(this instanceof Varint)) {
15
+ return new Varint(buf);
16
+ }
17
+ if (Buffer.isBuffer(buf)) {
18
+ this.buf = buf;
19
+ } else if (typeof buf === 'number') {
20
+ var num = buf;
21
+ this.fromNumber(num);
22
+ } else if (buf instanceof BN) {
23
+ var bn = buf;
24
+ this.fromBN(bn);
25
+ } else if (buf) {
26
+ var obj = buf;
27
+ this.set(obj);
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Sets the buffer for the Varint instance.
33
+ * @param {Object} obj - The object containing the buffer to set.
34
+ * @param {Buffer} [obj.buf] - The buffer to assign. If not provided, keeps the current buffer.
35
+ * @returns {Varint} The Varint instance for chaining.
36
+ */
37
+ Varint.prototype.set = function (obj) {
38
+ this.buf = obj.buf || this.buf;
39
+ return this;
40
+ };
41
+
42
+ /**
43
+ * Converts a hex string to a Varint buffer and updates the instance.
44
+ * @param {string} str - Hex string to convert.
45
+ * @returns {Varint} The updated Varint instance.
46
+ */
47
+ Varint.prototype.fromString = function (str) {
48
+ this.set({
49
+ buf: Buffer.from(str, 'hex'),
50
+ });
51
+ return this;
52
+ };
53
+
54
+ /**
55
+ * Converts the Varint buffer to a hexadecimal string representation.
56
+ * @returns {string} Hexadecimal string of the buffer.
57
+ */
58
+ Varint.prototype.toString = function () {
59
+ return this.buf.toString('hex');
60
+ };
61
+
62
+ /**
63
+ * Sets the internal buffer to the provided buffer and returns the instance.
64
+ * @param {Buffer} buf - The buffer to set as the internal buffer.
65
+ * @returns {Varint} The instance for chaining.
66
+ */
67
+ Varint.prototype.fromBuffer = function (buf) {
68
+ this.buf = buf;
69
+ return this;
70
+ };
71
+
72
+ /**
73
+ * Reads a varint from a buffer reader and stores it in the instance.
74
+ * @param {Object} br - The buffer reader instance to read from.
75
+ * @returns {Varint} The current Varint instance for chaining.
76
+ */
77
+ Varint.prototype.fromBufferReader = function (br) {
78
+ this.buf = br.readVarintBuf();
79
+ return this;
80
+ };
81
+
82
+ /**
83
+ * Converts a BigNumber (BN) to a varint and stores it in the buffer.
84
+ * @param {BN} bn - The BigNumber to convert to varint format.
85
+ * @returns {Varint} Returns the Varint instance for chaining.
86
+ */
87
+ Varint.prototype.fromBN = function (bn) {
88
+ var bw = new BufferWriter();
89
+ this.buf = bw.writeVarintBN(bn).toBuffer();
90
+ return this;
91
+ };
92
+
93
+ /**
94
+ * Converts a number to a varint and stores it in the buffer.
95
+ * @param {number} num - The number to convert to varint format.
96
+ * @returns {Varint} Returns the instance for chaining.
97
+ */
98
+ Varint.prototype.fromNumber = function (num) {
99
+ var bw = new BufferWriter();
100
+ this.buf = bw.writeVarintNum(num).toBuffer();
101
+ return this;
102
+ };
103
+
104
+ Varint.prototype.toBuffer = function () {
105
+ return this.buf;
106
+ };
107
+
108
+ Varint.prototype.toBN = function () {
109
+ return BufferReader(this.buf).readVarintBN();
110
+ };
111
+
112
+ Varint.prototype.toNumber = function () {
113
+ return BufferReader(this.buf).readVarintNum();
114
+ };
115
+
116
+ export default Varint;
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ import _ from '../util/_.js';
4
+ import data from './spec.js';
5
+
6
+ function format(message, args) {
7
+ return message.replace('{0}', args[0]).replace('{1}', args[1]).replace('{2}', args[2]);
8
+ }
9
+ var traverseNode = function (parent, errorDefinition) {
10
+ var NodeError = function () {
11
+ if (_.isString(errorDefinition.message)) {
12
+ this.message = format(errorDefinition.message, arguments);
13
+ } else if (_.isFunction(errorDefinition.message)) {
14
+ this.message = errorDefinition.message.apply(null, arguments);
15
+ } else {
16
+ throw new Error('Invalid error definition for ' + errorDefinition.name);
17
+ }
18
+ this.stack = this.message + '\n' + new Error().stack;
19
+ };
20
+ NodeError.prototype = Object.create(parent.prototype);
21
+ NodeError.prototype.name = parent.prototype.name + errorDefinition.name;
22
+ parent[errorDefinition.name] = NodeError;
23
+ if (errorDefinition.errors) {
24
+ childDefinitions(NodeError, errorDefinition.errors);
25
+ }
26
+ return NodeError;
27
+ };
28
+
29
+ var childDefinitions = function (parent, childDefinitions) {
30
+ _.each(childDefinitions, function (childDefinition) {
31
+ traverseNode(parent, childDefinition);
32
+ });
33
+ };
34
+
35
+ var traverseRoot = function (parent, errorsDefinition) {
36
+ childDefinitions(parent, errorsDefinition);
37
+ return parent;
38
+ };
39
+
40
+ var OpcatError = function () {
41
+ this.message = 'Internal error';
42
+ this.stack = this.message + '\n' + new Error().stack;
43
+ };
44
+ OpcatError.prototype = Object.create(Error.prototype);
45
+ OpcatError.prototype.name = 'opcat.Error';
46
+
47
+
48
+ traverseRoot(OpcatError, data);
49
+
50
+ export default OpcatError;
51
+
52
+ export const extend = function (spec) {
53
+ return traverseNode(OpcatError, spec);
54
+ };
@@ -0,0 +1,314 @@
1
+ 'use strict';
2
+
3
+ var docsURL = 'https://docs.moneybutton.com/';
4
+
5
+ export default [
6
+ {
7
+ name: 'InvalidB58Char',
8
+ message: 'Invalid Base58 character: {0} in {1}',
9
+ },
10
+ {
11
+ name: 'InvalidB58Checksum',
12
+ message: 'Invalid Base58 checksum for {0}',
13
+ },
14
+ {
15
+ name: 'InvalidNetwork',
16
+ message: 'Invalid version for network: got {0}',
17
+ },
18
+ {
19
+ name: 'InvalidState',
20
+ message: 'Invalid state: {0}',
21
+ },
22
+ {
23
+ name: 'NotImplemented',
24
+ message: 'Function {0} was not implemented yet',
25
+ },
26
+ {
27
+ name: 'InvalidNetworkArgument',
28
+ message: 'Invalid network: must be "livenet" or "testnet", got {0}',
29
+ },
30
+ {
31
+ name: 'InvalidArgument',
32
+ message: function () {
33
+ return (
34
+ 'Invalid Argument' +
35
+ (arguments[0] ? ': ' + arguments[0] : '') +
36
+ (arguments[1] ? ' Documentation: ' + docsURL + arguments[1] : '')
37
+ );
38
+ },
39
+ },
40
+ {
41
+ name: 'AbstractMethodInvoked',
42
+ message: 'Abstract Method Invocation: {0}',
43
+ },
44
+ {
45
+ name: 'InvalidArgumentType',
46
+ message: function () {
47
+ return (
48
+ 'Invalid Argument for ' +
49
+ arguments[2] +
50
+ ', expected ' +
51
+ arguments[1] +
52
+ ' but got ' +
53
+ typeof arguments[0]
54
+ );
55
+ },
56
+ },
57
+ {
58
+ name: 'Unit',
59
+ message: 'Internal Error on Unit {0}',
60
+ errors: [
61
+ {
62
+ name: 'UnknownCode',
63
+ message: 'Unrecognized unit code: {0}',
64
+ },
65
+ {
66
+ name: 'InvalidRate',
67
+ message: 'Invalid exchange rate: {0}',
68
+ },
69
+ ],
70
+ },
71
+ {
72
+ name: 'MerkleBlock',
73
+ message: 'Internal Error on MerkleBlock {0}',
74
+ errors: [
75
+ {
76
+ name: 'InvalidMerkleTree',
77
+ message: 'This MerkleBlock contain an invalid Merkle Tree',
78
+ },
79
+ ],
80
+ },
81
+ {
82
+ name: 'Transaction',
83
+ message: 'Internal Error on Transaction {0}',
84
+ errors: [
85
+ {
86
+ name: 'Input',
87
+ message: 'Internal Error on Input {0}',
88
+ errors: [
89
+ {
90
+ name: 'MissingScript',
91
+ message: 'Need a script to create an input',
92
+ },
93
+ {
94
+ name: 'UnsupportedScript',
95
+ message: 'Unsupported input script type: {0}',
96
+ },
97
+ {
98
+ name: 'MissingPreviousOutput',
99
+ message: 'No previous output information.',
100
+ },
101
+ {
102
+ name: 'MissingInput',
103
+ message: 'Invalid inputIndex.',
104
+ },
105
+ {
106
+ name: 'InvalidParams',
107
+ message: 'Invalid params: {0}.',
108
+ },
109
+ ],
110
+ },
111
+ {
112
+ name: 'NeedMoreInfo',
113
+ message: '{0}',
114
+ },
115
+ {
116
+ name: 'InvalidSorting',
117
+ message:
118
+ 'The sorting function provided did not return the change output as one of the array elements',
119
+ },
120
+ {
121
+ name: 'InvalidOutputAmountSum',
122
+ message: '{0}',
123
+ },
124
+ {
125
+ name: 'MissingSignatures',
126
+ message: 'Some inputs have not been fully signed',
127
+ },
128
+ {
129
+ name: 'InvalidIndex',
130
+ message: 'Invalid index: {0} is not between 0, {1}',
131
+ },
132
+ {
133
+ name: 'UnableToVerifySignature',
134
+ message: 'Unable to verify signature: {0}',
135
+ },
136
+ {
137
+ name: 'DustOutputs',
138
+ message: 'Dust amount detected in one output',
139
+ },
140
+ {
141
+ name: 'MissingOutput',
142
+ message: 'Output not found',
143
+ },
144
+ {
145
+ name: 'InvalidSatoshis',
146
+ message: 'Output satoshis are invalid',
147
+ },
148
+ {
149
+ name: 'FeeError',
150
+ message: 'Internal Error on Fee {0}',
151
+ errors: [
152
+ {
153
+ name: 'TooSmall',
154
+ message: 'Fee is too small: {0}',
155
+ },
156
+ {
157
+ name: 'TooLarge',
158
+ message: 'Fee is too large: {0}',
159
+ },
160
+ {
161
+ name: 'Different',
162
+ message: 'Unspent value is different from specified fee: {0}',
163
+ },
164
+ ],
165
+ },
166
+ {
167
+ name: 'ChangeAddressMissing',
168
+ message: 'Change address is missing',
169
+ },
170
+ {
171
+ name: 'BlockHeightTooHigh',
172
+ message: 'Block Height can be at most 2^32 -1',
173
+ },
174
+ {
175
+ name: 'NLockTimeOutOfRange',
176
+ message: 'Block Height can only be between 0 and 499 999 999',
177
+ },
178
+ {
179
+ name: 'LockTimeTooEarly',
180
+ message: "Lock Time can't be earlier than UNIX date 500 000 000",
181
+ },
182
+ {
183
+ name: 'TransactionAlreadySealed',
184
+ message: 'Cannot update sealed transaction',
185
+ },
186
+ ],
187
+ },
188
+ {
189
+ name: 'Script',
190
+ message: 'Internal Error on Script {0}',
191
+ errors: [
192
+ {
193
+ name: 'UnrecognizedAddress',
194
+ message: 'Expected argument {0} to be an address',
195
+ },
196
+ {
197
+ name: 'CantDeriveAddress',
198
+ message:
199
+ "Can't derive address associated with script: {0}, needs to be p2pkh in, p2pkh out",
200
+ },
201
+ {
202
+ name: 'InvalidBuffer',
203
+ message: "Invalid script buffer: can't parse valid script from given buffer {0}",
204
+ },
205
+ {
206
+ name: 'InvalidOpcode',
207
+ message: 'Invalid Opcode: got "{0}"',
208
+ },
209
+ ],
210
+ },
211
+ {
212
+ name: 'HDPrivateKey',
213
+ message: 'Internal Error on HDPrivateKey {0}',
214
+ errors: [
215
+ {
216
+ name: 'InvalidDerivationArgument',
217
+ message: 'Invalid derivation argument {0}, expected string, or number and boolean',
218
+ },
219
+ {
220
+ name: 'InvalidEntropyArgument',
221
+ message: 'Invalid entropy: must be an hexa string or binary buffer, got {0}',
222
+ errors: [
223
+ {
224
+ name: 'TooMuchEntropy',
225
+ message: 'Invalid entropy: more than 512 bits is non standard, got "{0}"',
226
+ },
227
+ {
228
+ name: 'NotEnoughEntropy',
229
+ message: 'Invalid entropy: at least 128 bits needed, got "{0}"',
230
+ },
231
+ ],
232
+ },
233
+ {
234
+ name: 'InvalidLength',
235
+ message: 'Invalid length for xprivkey string in {0}',
236
+ },
237
+ {
238
+ name: 'InvalidPath',
239
+ message: 'Invalid derivation path: {0}',
240
+ },
241
+ {
242
+ name: 'UnrecognizedArgument',
243
+ message:
244
+ 'Invalid argument: creating a HDPrivateKey requires a string, buffer, json or object, got "{0}"',
245
+ },
246
+ ],
247
+ },
248
+ {
249
+ name: 'HDPublicKey',
250
+ message: 'Internal Error on HDPublicKey {0}',
251
+ errors: [
252
+ {
253
+ name: 'ArgumentIsPrivateExtended',
254
+ message: 'Argument is an extended private key',
255
+ },
256
+ {
257
+ name: 'InvalidDerivationArgument',
258
+ message: 'Invalid derivation argument: got {0}',
259
+ },
260
+ {
261
+ name: 'InvalidLength',
262
+ message: 'Invalid length for xpubkey: got "{0}"',
263
+ },
264
+ {
265
+ name: 'InvalidPath',
266
+ message: 'Invalid derivation path, it should look like: "m/1/100", got "{0}"',
267
+ },
268
+ {
269
+ name: 'InvalidIndexCantDeriveHardened',
270
+ message: 'Invalid argument: creating a hardened path requires an HDPrivateKey',
271
+ },
272
+ {
273
+ name: 'MustSupplyArgument',
274
+ message: 'Must supply an argument to create a HDPublicKey',
275
+ },
276
+ {
277
+ name: 'UnrecognizedArgument',
278
+ message: 'Invalid argument for creation, must be string, json, buffer, or object',
279
+ },
280
+ ],
281
+ },
282
+ {
283
+ name: 'ECIES',
284
+ message: 'Internal Error on opcat-ecies Module {0}',
285
+ errors: [
286
+ {
287
+ name: 'DecryptionError',
288
+ message: 'Invalid Message: {0}',
289
+ },
290
+ {
291
+ name: 'UnsupportAlgorithm',
292
+ message: 'Unsupport Algorithm: {0}',
293
+ },
294
+ ],
295
+ },
296
+ {
297
+ name: 'Mnemonic',
298
+ message: 'Internal Error on opcat-mnemonic module {0}',
299
+ errors: [
300
+ {
301
+ name: 'InvalidEntropy',
302
+ message: 'Entropy length must be an even multiple of 11 bits: {0}',
303
+ },
304
+ {
305
+ name: 'UnknownWordlist',
306
+ message: 'Could not detect the used word list: {0}',
307
+ },
308
+ {
309
+ name: 'InvalidMnemonic',
310
+ message: 'Mnemonic string is invalid: {0}',
311
+ },
312
+ ],
313
+ },
314
+ ];
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Hash Cache
3
+ * ==========
4
+ *
5
+ * For use in sighash.
6
+ */
7
+ 'use strict';
8
+
9
+ /**
10
+ * A class representing a cache for transaction hash buffers.
11
+ * Provides methods for serialization/deserialization between Buffer, JSON, and hex formats.
12
+ *
13
+ * @class
14
+ * @property {Buffer} prevoutsHashBuf - Hash buffer for transaction prevouts
15
+ * @property {Buffer} sequenceHashBuf - Hash buffer for transaction sequence
16
+ * @property {Buffer} outputsHashBuf - Hash buffer for transaction outputs
17
+ */
18
+ class HashCache {
19
+ /**
20
+ * Constructs a new hash cache instance with the provided hash buffers.
21
+ * @param {Buffer} prevoutsHashBuf - Hash buffer for prevouts
22
+ * @param {Buffer} sequenceHashBuf - Hash buffer for sequence
23
+ * @param {Buffer} outputsHashBuf - Hash buffer for outputs
24
+ */
25
+ constructor(prevoutsHashBuf, sequenceHashBuf, outputsHashBuf) {
26
+ this.prevoutsHashBuf = prevoutsHashBuf;
27
+ this.sequenceHashBuf = sequenceHashBuf;
28
+ this.outputsHashBuf = outputsHashBuf;
29
+ }
30
+
31
+ /**
32
+ * Creates a HashCache instance from a buffer by parsing it as JSON.
33
+ * @param {Buffer} buf - The input buffer containing JSON data.
34
+ * @returns {HashCache} A new HashCache instance created from the parsed JSON.
35
+ */
36
+ static fromBuffer(buf) {
37
+ return HashCache.fromJSON(JSON.parse(buf.toString()));
38
+ }
39
+
40
+ /**
41
+ * Converts the object to a Buffer containing its JSON string representation.
42
+ * @returns {Buffer} A Buffer containing the JSON string of the object.
43
+ */
44
+ toBuffer() {
45
+ return Buffer.from(JSON.stringify(this.toJSON()));
46
+ }
47
+
48
+ /**
49
+ * Creates a HashCache instance from a JSON object.
50
+ * @param {Object} json - The JSON object containing hash buffers in hex format.
51
+ * @param {string} [json.prevoutsHashBuf] - Hex string for prevouts hash buffer.
52
+ * @param {string} [json.sequenceHashBuf] - Hex string for sequence hash buffer.
53
+ * @param {string} [json.outputsHashBuf] - Hex string for outputs hash buffer.
54
+ * @returns {HashCache} A new HashCache instance with converted Buffer values.
55
+ */
56
+ static fromJSON(json) {
57
+ return new HashCache(
58
+ json.prevoutsHashBuf ? Buffer.from(json.prevoutsHashBuf, 'hex') : undefined,
59
+ json.sequenceHashBuf ? Buffer.from(json.sequenceHashBuf, 'hex') : undefined,
60
+ json.outputsHashBuf ? Buffer.from(json.outputsHashBuf, 'hex') : undefined,
61
+ );
62
+ }
63
+
64
+ /**
65
+ * Converts the hash cache object to a JSON representation.
66
+ * @returns {Object} An object containing hex string representations of the hash buffers:
67
+ * - prevoutsHashBuf: Hex string of prevouts hash buffer (if exists)
68
+ * - sequenceHashBuf: Hex string of sequence hash buffer (if exists)
69
+ * - outputsHashBuf: Hex string of outputs hash buffer (if exists)
70
+ */
71
+ toJSON() {
72
+ return {
73
+ prevoutsHashBuf: this.prevoutsHashBuf ? this.prevoutsHashBuf.toString('hex') : undefined,
74
+ sequenceHashBuf: this.sequenceHashBuf ? this.sequenceHashBuf.toString('hex') : undefined,
75
+ outputsHashBuf: this.outputsHashBuf ? this.outputsHashBuf.toString('hex') : undefined,
76
+ };
77
+ }
78
+
79
+ /**
80
+ * Converts the object's buffer representation to a hexadecimal string.
81
+ * @returns {string} Hexadecimal string representation of the buffer.
82
+ */
83
+ toHex() {
84
+ return this.toBuffer().toString('hex');
85
+ }
86
+
87
+ /**
88
+ * Creates a HashCache instance from a hex string.
89
+ * @param {string} hex - The hex string to convert to a buffer.
90
+ * @returns {HashCache} A HashCache instance created from the hex string buffer.
91
+ */
92
+ static fromHex(hex) {
93
+ const buf = Buffer.from(hex, 'hex');
94
+ return HashCache.fromBuffer(buf);
95
+ }
96
+ }
97
+
98
+ export default HashCache;