@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,321 @@
1
+ var _ = require('./util/_.cjs');
2
+
3
+ var JSUtil = require('./util/js.cjs');
4
+ var Network = require('./network.cjs');
5
+ var networks = [];
6
+ var networkMaps = {};
7
+
8
+ /**
9
+ * A tool class for managing all supported networks
10
+ * @constructor
11
+ */
12
+ var Networks = function Networks() { }
13
+
14
+ /**
15
+ * @function
16
+ * @member Networks#get
17
+ * Retrieves the network associated with a magic number or string.
18
+ * @param {string|number|Network} arg
19
+ * @param {string|Array} keys - if set, only check if the magic number associated with this name matches
20
+ * @return Network
21
+ */
22
+ Networks.get = function get(arg, keys) {
23
+ if (~networks.indexOf(arg)) {
24
+ return arg;
25
+ }
26
+ if (keys) {
27
+ if (!_.isArray(keys)) {
28
+ keys = [keys];
29
+ }
30
+ for (var i = 0; i < networks.length; i++) {
31
+ var network = networks[i];
32
+ var filteredNet = _.pick(network, keys);
33
+ var netValues = _.values(filteredNet);
34
+ if (~netValues.indexOf(arg)) {
35
+ return network;
36
+ }
37
+ }
38
+ return undefined;
39
+ }
40
+ return networkMaps[arg];
41
+ }
42
+
43
+ /***
44
+ * Derives an array from the given cashAddrPrefix to be used in the computation
45
+ * of the address' checksum.
46
+ *
47
+ * @param {string} cashAddrPrefix Network cashAddrPrefix. E.g.: 'bitcoincash'.
48
+ */
49
+ function cashAddrPrefixToArray(cashAddrPrefix) {
50
+ var result = [];
51
+ for (var i = 0; i < cashAddrPrefix.length; i++) {
52
+ result.push(cashAddrPrefix.charCodeAt(i) & 31);
53
+ }
54
+ return result;
55
+ }
56
+
57
+
58
+ function indexNetworkBy(network, keys) {
59
+ for (var i = 0; i < keys.length; i++) {
60
+ var key = keys[i];
61
+ var networkValue = network[key];
62
+ if (!_.isUndefined(networkValue) && !_.isObject(networkValue)) {
63
+ networkMaps[networkValue] = network;
64
+ }
65
+ }
66
+ }
67
+
68
+ function unindexNetworkBy(network, values) {
69
+ for (var index = 0; index < values.length; index++) {
70
+ var value = values[index];
71
+ if (networkMaps[value] === network) {
72
+ delete networkMaps[value];
73
+ }
74
+ }
75
+ }
76
+
77
+
78
+ /**
79
+ * @function
80
+ * @member Networks#add
81
+ * Will add a custom Network
82
+ * @param {Object} data
83
+ * @param {string} data.name - The name of the network
84
+ * @param {string} data.alias - The aliased name of the network
85
+ * @param {Number} data.pubkeyhash - The publickey hash cashAddrPrefix
86
+ * @param {Number} data.privatekey - The privatekey cashAddrPrefix
87
+ * @param {Number} data.scripthash - The scripthash cashAddrPrefix
88
+ * @param {Number} data.xpubkey - The extended public key magic
89
+ * @param {Number} data.xprivkey - The extended private key magic
90
+ * @param {Number} data.networkMagic - The network magic number
91
+ * @param {Number} data.port - The network port
92
+ * @param {Array} data.dnsSeeds - An array of dns seeds
93
+ * @return Network
94
+ */
95
+ Networks.add = function add(data) {
96
+
97
+ var network = new Network();
98
+
99
+ JSUtil.defineImmutable(network, {
100
+ name: data.name,
101
+ alias: data.alias,
102
+ pubkeyhash: data.pubkeyhash,
103
+ privatekey: data.privatekey,
104
+ scripthash: data.scripthash,
105
+ xpubkey: data.xpubkey,
106
+ xprivkey: data.xprivkey,
107
+ });
108
+
109
+ var indexBy = data.indexBy || Object.keys(data);
110
+
111
+ if (data.cashAddrPrefix) {
112
+ _.extend(network, {
113
+ cashAddrPrefix: data.cashAddrPrefix,
114
+ cashAddrPrefixArray: cashAddrPrefixToArray(data.cashAddrPrefix),
115
+ });
116
+ }
117
+
118
+ if (data.networkMagic) {
119
+ _.extend(network, {
120
+ networkMagic: JSUtil.integerAsBuffer(data.networkMagic),
121
+ });
122
+ }
123
+
124
+ if (data.port) {
125
+ _.extend(network, {
126
+ port: data.port,
127
+ });
128
+ }
129
+
130
+ if (data.dnsSeeds) {
131
+ _.extend(network, {
132
+ dnsSeeds: data.dnsSeeds,
133
+ });
134
+ }
135
+ networks.push(network);
136
+ indexNetworkBy(network, indexBy);
137
+ return network;
138
+ }
139
+
140
+
141
+
142
+ /**
143
+ * @function
144
+ * @member Networks#remove
145
+ * Will remove a custom network
146
+ * @param {Network} network
147
+ */
148
+ Networks.remove = function remove(network) {
149
+ for (var i = 0; i < networks.length; i++) {
150
+ if (networks[i] === network) {
151
+ networks.splice(i, 1);
152
+ }
153
+ }
154
+ unindexNetworkBy(network, Object.keys(networkMaps));
155
+ }
156
+
157
+ var networkMagic = {
158
+ livenet: 0xe3e1f3e8,
159
+ testnet: 0xf4e5f3f4,
160
+ regtest: 0xdab5bffa,
161
+ };
162
+
163
+ var dnsSeeds = [''];
164
+
165
+ var TESTNET = {
166
+ PORT: 18333,
167
+ NETWORK_MAGIC: networkMagic.testnet,
168
+ DNS_SEEDS: dnsSeeds,
169
+ PREFIX: 'testnet',
170
+ CASHADDRPREFIX: 'opcattest',
171
+ };
172
+
173
+ var REGTEST = {
174
+ PORT: 18444,
175
+ NETWORK_MAGIC: networkMagic.regtest,
176
+ DNS_SEEDS: [],
177
+ PREFIX: 'regtest',
178
+ CASHADDRPREFIX: 'opcatreg',
179
+ };
180
+
181
+ var liveNetwork = {
182
+ name: 'livenet',
183
+ alias: 'mainnet',
184
+ prefix: 'bitcoin',
185
+ cashAddrPrefix: 'opcat',
186
+ pubkeyhash: 0x00,
187
+ privatekey: 0x80,
188
+ scripthash: 0x05,
189
+ xpubkey: 0x0488b21e,
190
+ xprivkey: 0x0488ade4,
191
+ networkMagic: networkMagic.livenet,
192
+ port: 8333,
193
+ dnsSeeds: dnsSeeds,
194
+ };
195
+
196
+ // network magic, port, cashAddrPrefix, and dnsSeeds are overloaded by enableRegtest
197
+ var testNetwork = {
198
+ name: 'testnet',
199
+ prefix: TESTNET.PREFIX,
200
+ cashAddrPrefix: TESTNET.CASHADDRPREFIX,
201
+ pubkeyhash: 0x6f,
202
+ privatekey: 0xef,
203
+ scripthash: 0xc4,
204
+ xpubkey: 0x043587cf,
205
+ xprivkey: 0x04358394,
206
+ networkMagic: TESTNET.NETWORK_MAGIC,
207
+ };
208
+
209
+ var regtestNetwork = {
210
+ name: 'regtest',
211
+ prefix: REGTEST.PREFIX,
212
+ cashAddrPrefix: REGTEST.CASHADDRPREFIX,
213
+ pubkeyhash: 0x6f,
214
+ privatekey: 0xef,
215
+ scripthash: 0xc4,
216
+ xpubkey: 0x043587cf,
217
+ xprivkey: 0x04358394,
218
+ networkMagic: REGTEST.NETWORK_MAGIC,
219
+ port: REGTEST.PORT,
220
+ dnsSeeds: [],
221
+ indexBy: ['port', 'name', 'cashAddrPrefix', 'networkMagic'],
222
+ };
223
+
224
+
225
+ Networks.add(testNetwork);
226
+ Networks.add(regtestNetwork);
227
+ Networks.add(liveNetwork);
228
+
229
+ var livenet = Networks.get('livenet');
230
+ var regtest = Networks.get('regtest');
231
+ var testnet = Networks.get('testnet');
232
+
233
+ Networks.livenet = livenet;
234
+ Networks.regtest = regtest;
235
+ Networks.testnet = testnet;
236
+ Networks.defaultNetwork = livenet;
237
+
238
+ Object.defineProperty(Networks.testnet, 'port', {
239
+ enumerable: true,
240
+ configurable: false,
241
+ get: function () {
242
+ if (this.regtestEnabled) {
243
+ return REGTEST.PORT;
244
+ } else {
245
+ return TESTNET.PORT;
246
+ }
247
+ },
248
+ });
249
+
250
+ Object.defineProperty(Networks.testnet, 'networkMagic', {
251
+ enumerable: true,
252
+ configurable: false,
253
+ get: function () {
254
+ if (this.regtestEnabled) {
255
+ return JSUtil.integerAsBuffer(REGTEST.NETWORK_MAGIC);
256
+ } else {
257
+ return JSUtil.integerAsBuffer(TESTNET.NETWORK_MAGIC);
258
+ }
259
+ },
260
+ });
261
+
262
+ Object.defineProperty(Networks.testnet, 'dnsSeeds', {
263
+ enumerable: true,
264
+ configurable: false,
265
+ get: function () {
266
+ if (this.regtestEnabled) {
267
+ return REGTEST.DNS_SEEDS;
268
+ } else {
269
+ return TESTNET.DNS_SEEDS;
270
+ }
271
+ },
272
+ });
273
+
274
+ Object.defineProperty(Networks.testnet, 'cashAddrPrefix', {
275
+ enumerable: true,
276
+ configurable: false,
277
+ get: function () {
278
+ if (this.regtestEnabled) {
279
+ return REGTEST.CASHADDRPREFIX;
280
+ } else {
281
+ return TESTNET.CASHADDRPREFIX;
282
+ }
283
+ },
284
+ });
285
+
286
+ Object.defineProperty(Networks.testnet, 'cashAddrPrefixArray', {
287
+ enumerable: true,
288
+ configurable: false,
289
+ get: function () {
290
+ if (this.regtestEnabled) {
291
+ return cashAddrPrefixToArray(REGTEST.CASHADDRPREFIX);
292
+ } else {
293
+ return cashAddrPrefixToArray(TESTNET.CASHADDRPREFIX);
294
+ }
295
+ },
296
+ });
297
+
298
+
299
+ /**
300
+ * Enables regtest network mode for testing purposes.
301
+ * @member Networks#enableRegtest
302
+ * @function
303
+ */
304
+ Networks.enableRegtest = function enableRegtest() {
305
+ testnet.regtestEnabled = true;
306
+ }
307
+
308
+
309
+ /**
310
+ * @function
311
+ * @member Networks#disableRegtest
312
+ * Disables the regtest network configuration.
313
+ * This sets the `regtestEnabled` flag to false in the testnet configuration.
314
+ */
315
+ Networks.disableRegtest = function disableRegtest() {
316
+ testnet.regtestEnabled = false;
317
+ }
318
+
319
+
320
+
321
+ module.exports = Networks
@@ -1,9 +1,16 @@
1
1
  'use strict';
2
2
 
3
- var _ = require('./util/_');
4
- var $ = require('./util/preconditions');
5
- var JSUtil = require('./util/js');
3
+ var _ = require('./util/_.cjs');
4
+ var $ = require('./util/preconditions.cjs');
5
+ var JSUtil = require('./util/js.cjs');
6
6
 
7
+ /**
8
+ * Creates an Opcode instance from a number or string representation.
9
+ * @constructor
10
+ * @param {number|string} num - The numeric value or string name of the opcode
11
+ * @throws {TypeError} If the input type is not recognized
12
+ * @returns {Opcode} A new Opcode instance
13
+ */
7
14
  function Opcode(num) {
8
15
  if (!(this instanceof Opcode)) {
9
16
  return new Opcode(num);
@@ -26,16 +33,34 @@ function Opcode(num) {
26
33
  return this;
27
34
  }
28
35
 
36
+ /**
37
+ * Creates an Opcode instance from a Buffer.
38
+ * @param {Buffer} buf - The buffer containing the opcode data.
39
+ * @returns {Opcode} The constructed Opcode instance.
40
+ * @throws {Error} If the input is not a Buffer.
41
+ */
29
42
  Opcode.fromBuffer = function (buf) {
30
43
  $.checkArgument(Buffer.isBuffer(buf));
31
44
  return new Opcode(Number('0x' + buf.toString('hex')));
32
45
  };
33
46
 
47
+ /**
48
+ * Creates an Opcode instance from a number.
49
+ * @param {number} num - The numeric value to convert to an Opcode.
50
+ * @returns {Opcode} A new Opcode instance.
51
+ * @throws {Error} If the input is not a number.
52
+ */
34
53
  Opcode.fromNumber = function (num) {
35
54
  $.checkArgument(_.isNumber(num));
36
55
  return new Opcode(num);
37
56
  };
38
57
 
58
+ /**
59
+ * Creates an Opcode instance from a string representation.
60
+ * @param {string} str - The string representation of the opcode.
61
+ * @returns {Opcode} A new Opcode instance corresponding to the input string.
62
+ * @throws {TypeError} If the input string is not a valid opcode representation.
63
+ */
39
64
  Opcode.fromString = function (str) {
40
65
  $.checkArgument(_.isString(str));
41
66
  var value = Opcode.map[str];
@@ -45,18 +70,35 @@ Opcode.fromString = function (str) {
45
70
  return new Opcode(value);
46
71
  };
47
72
 
73
+ /**
74
+ * Converts the opcode number to its hexadecimal string representation.
75
+ * @returns {string} Hexadecimal string of the opcode number.
76
+ */
48
77
  Opcode.prototype.toHex = function () {
49
78
  return this.num.toString(16);
50
79
  };
51
80
 
81
+ /**
82
+ * Converts the opcode to a Buffer by first converting it to a hex string.
83
+ * @returns {Buffer} The opcode represented as a Buffer.
84
+ */
52
85
  Opcode.prototype.toBuffer = function () {
53
86
  return Buffer.from(this.toHex(), 'hex');
54
87
  };
55
88
 
89
+ /**
90
+ * Gets the numeric value of the opcode.
91
+ * @returns {number} The numeric representation of the opcode.
92
+ */
56
93
  Opcode.prototype.toNumber = function () {
57
94
  return this.num;
58
95
  };
59
96
 
97
+ /**
98
+ * Converts the opcode number to its string representation.
99
+ * @throws {Error} If the opcode number has no corresponding string mapping.
100
+ * @returns {string} The string representation of the opcode.
101
+ */
60
102
  Opcode.prototype.toString = function () {
61
103
  var str = Opcode.reverseMap[this.num];
62
104
  if (typeof str === 'undefined') {
@@ -65,6 +107,12 @@ Opcode.prototype.toString = function () {
65
107
  return str;
66
108
  };
67
109
 
110
+ /**
111
+ * Converts the opcode to a human-readable string representation.
112
+ * If the opcode has a known mnemonic, returns that string.
113
+ * Otherwise, returns the hexadecimal representation of the opcode.
114
+ * @returns {string} The safe string representation of the opcode.
115
+ */
68
116
  Opcode.prototype.toSafeString = function () {
69
117
  var str = Opcode.reverseMap[this.num];
70
118
  if (typeof str === 'undefined') {
@@ -73,6 +121,12 @@ Opcode.prototype.toSafeString = function () {
73
121
  return str;
74
122
  };
75
123
 
124
+ /**
125
+ * Converts a small integer (0-16) to its corresponding opcode.
126
+ * @param {number} n - The integer to convert (must be between 0 and 16)
127
+ * @returns {Opcode} The corresponding opcode (OP_0 for 0, OP_1+n-1 for 1-16)
128
+ * @throws {Error} If n is not a number or outside valid range
129
+ */
76
130
  Opcode.smallInt = function (n) {
77
131
  $.checkArgument(_.isNumber(n), 'Invalid Argument: n should be number');
78
132
  $.checkArgument(n >= 0 && n <= 16, 'Invalid Argument: n must be between 0 and 16');
@@ -82,8 +136,12 @@ Opcode.smallInt = function (n) {
82
136
  return new Opcode(Opcode.map.OP_1 + n - 1);
83
137
  };
84
138
 
139
+ /**
140
+ * A map of opcode names to their corresponding numeric values.
141
+ * @type {Object.<string, number>}
142
+ */
85
143
  Opcode.map = {
86
- // push value
144
+ // push value
87
145
  OP_FALSE: 0,
88
146
  OP_0: 0,
89
147
  OP_PUSHDATA1: 76,
@@ -224,6 +282,9 @@ Opcode.map = {
224
282
  OP_INVALIDOPCODE: 255,
225
283
  };
226
284
 
285
+ /**
286
+ * Reverse mapping array for opcode lookup.
287
+ */
227
288
  Opcode.reverseMap = [];
228
289
 
229
290
  for (var k in Opcode.map) {
@@ -233,8 +294,11 @@ for (var k in Opcode.map) {
233
294
  // Easier access to opcodes
234
295
  _.extend(Opcode, Opcode.map);
235
296
 
297
+
236
298
  /**
237
- * @returns true if opcode is one of OP_0, OP_1, ..., OP_16
299
+ * Checks if the given opcode is a small integer opcode (OP_0 to OP_16).
300
+ * @param {number|Opcode} opcode - The opcode to check, either as a number or Opcode instance.
301
+ * @returns {boolean} True if the opcode is a small integer opcode, false otherwise.
238
302
  */
239
303
  Opcode.isSmallIntOp = function (opcode) {
240
304
  if (opcode instanceof Opcode) {