@opcat-labs/opcat 1.0.0 → 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 +13 -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 +40 -26
  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
package/.cjs2esm.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "input": ["cjs"],
3
+ "ignore": [],
4
+ "output": "esm",
5
+ "forceDirectory": null,
6
+ "modules": [],
7
+ "extension": {
8
+ "use": "js",
9
+ "ignore": []
10
+ },
11
+ "addModuleEntry": false,
12
+ "addPackageJson": false,
13
+ "filesWithShebang": [],
14
+ "codemod": {
15
+ "path": "",
16
+ "files": ["cjs", "exports"]
17
+ }
18
+ }
package/.mocharc.yaml CHANGED
@@ -1,3 +1,3 @@
1
1
  recursive: true
2
2
  timeout: 5000
3
- spec: ['./test/**/*.js']
3
+ spec: ['./test/**/*.cjs']
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @opcat-labs/opcat
2
+
3
+ ## 1.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - fix esm, fix change amount, add decodePubFunctionCall
8
+
9
+ ## 1.0.1
10
+
11
+ ### Patch Changes
12
+
13
+ - add cli project command
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ opcat
2
+ =====
3
+
4
+ Javascript Opcat library.
5
+
6
+ Documentation is available on the [Opcat Docs](http://docs.opcatlabs.io/overview).
@@ -1,16 +1,15 @@
1
1
  'use strict';
2
2
 
3
- var _ = require('./util/_');
4
- var $ = require('./util/preconditions');
5
- var errors = require('./errors');
6
- var Base58Check = require('./encoding/base58check');
7
- var Networks = require('./networks');
8
- var Hash = require('./crypto/hash');
9
- var JSUtil = require('./util/js');
10
- var PublicKey = require('./publickey');
3
+ var _ = require('./util/_.cjs');
4
+ var $ = require('./util/preconditions.cjs');
5
+ var Base58Check = require('./encoding/base58check.cjs');
6
+ var Networks = require('./networks.cjs');
7
+ var Hash = require('./crypto/hash.cjs');
8
+ var JSUtil = require('./util/js.cjs');
9
+
11
10
  /**
12
11
  * Instantiate an address from an address String or Buffer, a public key hash Buffer,
13
- * or an instance of {@link PublicKey}.
12
+ * or a {@link PublicKey} Buffer.
14
13
  *
15
14
  * This is an immutable class, and if the first parameter provided to this constructor is an
16
15
  * `Address` instance, the same argument will be returned.
@@ -37,8 +36,8 @@ var PublicKey = require('./publickey');
37
36
  * ```
38
37
  *
39
38
  * @param {*} data - The encoded data in various formats
40
- * @param {Network|String|number=} network - The network: 'livenet' or 'testnet'
41
- * @param {string=} type - The type of address: 'pubkey'
39
+ * @param {Network|String|number} [network] - The network: 'livenet' or 'testnet'
40
+ * @param {string} [type] - The type of address: 'pubkey'
42
41
  * @returns {Address} A new valid and frozen instance of an Address
43
42
  * @constructor
44
43
  */
@@ -81,24 +80,64 @@ function Address(data, network, type) {
81
80
  return this;
82
81
  }
83
82
 
83
+ /**
84
+ * Gets the hash buffer of the Address instance.
85
+ * @memberof Address.prototype
86
+ * @type {Buffer}
87
+ * @readonly
88
+ */
89
+ Object.defineProperty(Address.prototype, 'hashBuffer', {
90
+ enumerable: true,
91
+ configurable: false,
92
+ get: function () {
93
+ return this.hashBuffer;
94
+ },
95
+ });
96
+
97
+ /**
98
+ * Gets or sets the network associated with this Address instance.
99
+ * @memberof Address.prototype
100
+ * @type {Network}
101
+ * @readonly
102
+ */
103
+ Object.defineProperty(Address.prototype, 'network', {
104
+ enumerable: true,
105
+ configurable: false,
106
+ get: function () {
107
+ return this.network;
108
+ },
109
+ });
110
+
111
+ /**
112
+ * Gets the address type (e.g. 'pubkeyhash').
113
+ * @memberof Address.prototype
114
+ * @type {string}
115
+ * @readonly
116
+ */
117
+ Object.defineProperty(Address.prototype, 'type', {
118
+ enumerable: true,
119
+ configurable: false,
120
+ get: function () {
121
+ return this.type;
122
+ },
123
+ });
124
+
84
125
  /**
85
126
  * Internal function used to split different kinds of arguments of the constructor
86
- * @param {*} data - The encoded data in various formats
87
- * @param {Network|String|number=} network - The network: 'livenet' or 'testnet'
88
- * @param {string=} type - The type of address: 'pubkey'
127
+ * @param {Buffer|Uint8Array|string|Object} data - The encoded data in various formats
128
+ * @param {Network|string} [network] - The network: 'livenet' or 'testnet'
129
+ * @param {string} [type] - The type of address: 'pubkey'
89
130
  * @returns {Object} An "info" object with "type", "network", and "hashBuffer"
131
+ * @private
90
132
  */
91
133
  Address.prototype._classifyArguments = function (data, network, type) {
92
- var Script = require('./script');
93
134
  // transform and validate input data
94
135
  if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 20) {
95
136
  return Address._transformHash(data);
96
137
  } else if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 21) {
97
138
  return Address._transformBuffer(data, network, type);
98
- } else if (data instanceof PublicKey) {
139
+ } else if ((data instanceof Buffer || data instanceof Uint8Array) && (data.length === 33 || data.length === 65)) {
99
140
  return Address._transformPublicKey(data);
100
- } else if (data instanceof Script) {
101
- return Address._transformScript(data, network);
102
141
  } else if (typeof data === 'string') {
103
142
  return Address._transformString(data, network, type);
104
143
  } else if (_.isObject(data)) {
@@ -108,7 +147,11 @@ Address.prototype._classifyArguments = function (data, network, type) {
108
147
  }
109
148
  };
110
149
 
111
- /** @static */
150
+ /**
151
+ * PayToPublicKeyHash address type identifier.
152
+ * @type {string}
153
+ * @static
154
+ */
112
155
  Address.PayToPublicKeyHash = 'pubkeyhash';
113
156
 
114
157
  /**
@@ -135,6 +178,7 @@ Address._transformHash = function (hash) {
135
178
  * @param {string} data.type - either 'pubkeyhash' or 'scripthash'
136
179
  * @param {Network=} data.network - the name of the network associated
137
180
  * @return {Address}
181
+ * @private
138
182
  */
139
183
  Address._transformObject = function (data) {
140
184
  $.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property');
@@ -215,31 +259,14 @@ Address._transformBuffer = function (buffer, network, type) {
215
259
  */
216
260
  Address._transformPublicKey = function (pubkey) {
217
261
  var info = {};
218
- if (!(pubkey instanceof PublicKey)) {
219
- throw new TypeError('Address must be an instance of PublicKey.');
262
+ if (!(pubkey instanceof Buffer) && !(pubkey instanceof Uint8Array) || (pubkey.length !== 33 && pubkey.length !== 65)) {
263
+ throw new TypeError('Pubkey supplied is not a buffer with 33 or 65 bytes.');
220
264
  }
221
- info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer());
265
+ info.hashBuffer = Hash.sha256ripemd160(pubkey);
222
266
  info.type = Address.PayToPublicKeyHash;
223
267
  return info;
224
268
  };
225
269
 
226
- /**
227
- * Internal function to transform a {@link Script} into a `info` object.
228
- *
229
- * @param {Script} script - An instance of Script
230
- * @returns {Object} An object with keys: hashBuffer, type
231
- * @private
232
- */
233
- Address._transformScript = function (script, network) {
234
- var Script = require('./script');
235
- $.checkArgument(script instanceof Script, 'script must be a Script instance');
236
- var info = script.getAddressInfo(network);
237
- if (!info) {
238
- throw new errors.Script.CantDeriveAddress(script);
239
- }
240
- return info;
241
- };
242
-
243
270
  /**
244
271
  * Internal function to transform a bitcoin cash address string
245
272
  *
@@ -268,9 +295,9 @@ Address._transformString = function (data, network, type) {
268
295
  };
269
296
 
270
297
  /**
271
- * Instantiate an address from a PublicKey instance
298
+ * Instantiate an address from a PublicKey buffer
272
299
  *
273
- * @param {PublicKey} data
300
+ * @param {Buffer} data - A buffer of the public key
274
301
  * @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
275
302
  * @returns {Address} A new valid and frozen instance of an Address
276
303
  */
@@ -280,18 +307,6 @@ Address.fromPublicKey = function (data, network) {
280
307
  return new Address(info.hashBuffer, network, info.type);
281
308
  };
282
309
 
283
- /**
284
- * Instantiate an address from a PrivateKey instance
285
- *
286
- * @param {PrivateKey} privateKey
287
- * @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
288
- * @returns {Address} A new valid and frozen instance of an Address
289
- */
290
- Address.fromPrivateKey = function (privateKey, network) {
291
- let publicKey = PublicKey.fromPrivateKey(privateKey);
292
- network = network || privateKey.network || Networks.defaultNetwork;
293
- return Address.fromPublicKey(publicKey, network);
294
- };
295
310
 
296
311
  /**
297
312
  * Instantiate an address from a ripemd160 public key hash
@@ -304,27 +319,10 @@ Address.fromPublicKeyHash = function (hash, network) {
304
319
  var info = Address._transformHash(hash);
305
320
  return new Address(info.hashBuffer, network, Address.PayToPublicKeyHash);
306
321
  };
307
- /**
308
- * Extract address from a Script. The script must be of one
309
- * of the following types: p2pkh input, p2pkh output, p2sh input
310
- * or p2sh output.
311
- * This will analyze the script and extract address information from it.
312
- * If you want to transform any script to a p2sh Address paying
313
- * to that script's hash instead, use {{Address#payingTo}}
314
- *
315
- * @param {Script} script - An instance of Script
316
- * @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
317
- * @returns {Address} A new valid and frozen instance of an Address
318
- */
319
- Address.fromScript = function (script, network) {
320
- var Script = require('./script');
321
- $.checkArgument(script instanceof Script, 'script must be a Script instance');
322
- var info = Address._transformScript(script, network);
323
- return new Address(info.hashBuffer, network, info.type);
324
- };
322
+
325
323
 
326
324
  /**
327
- * Instantiate an address from a buffer of the address
325
+ * Instantiate an address from a bitcoin address buffer
328
326
  *
329
327
  * @param {Buffer} buffer - An instance of buffer of the address
330
328
  * @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
@@ -336,6 +334,13 @@ Address.fromBuffer = function (buffer, network, type) {
336
334
  return new Address(info.hashBuffer, info.network, info.type);
337
335
  };
338
336
 
337
+ /**
338
+ * Creates an Address instance from a hex string.
339
+ * @param {string} hex - The hex string representation of the address.
340
+ * @param {Network} network - The network type (e.g., 'mainnet', 'testnet').
341
+ * @param {AddressType} [type] - Optional address type.
342
+ * @returns {Address} The Address instance created from the hex string.
343
+ */
339
344
  Address.fromHex = function (hex, network, type) {
340
345
  return Address.fromBuffer(Buffer.from(hex, 'hex'), network, type);
341
346
  };
@@ -0,0 +1,332 @@
1
+ 'use strict';
2
+
3
+ var _ = require('../util/_.cjs');
4
+ var BlockHeader = require('./blockheader.cjs');
5
+ var MerkleBlock = require('./merkleblock.cjs');
6
+ var BN = require('../crypto/bn.cjs');
7
+ var BufferReader = require('../encoding/bufferreader.cjs');
8
+ var BufferWriter = require('../encoding/bufferwriter.cjs');
9
+ var Hash = require('../crypto/hash.cjs');
10
+ var Transaction = require('../transaction/index.cjs');
11
+ var $ = require('../util/preconditions.cjs');
12
+
13
+ /**
14
+ * Instantiate a Block from a Buffer, JSON object, or Object with
15
+ * the properties of the Block
16
+ *
17
+ * @param {*} - A Buffer, JSON string, or Object
18
+ * @returns {Block}
19
+ * @constructor
20
+ */
21
+ function Block(arg) {
22
+ if (!(this instanceof Block)) {
23
+ return new Block(arg);
24
+ }
25
+ _.extend(this, Block._from(arg));
26
+ return this;
27
+ }
28
+
29
+ /**
30
+ * The maximum allowed size (in bytes) for a block.
31
+ * @type {number}
32
+ */
33
+ Block.MAX_BLOCK_SIZE = 128000000;
34
+
35
+
36
+ /**
37
+ * Creates a Block instance from the given argument.
38
+ * @param {*} arg - The input to convert into a Block.
39
+ * @returns {Block} A new Block instance.
40
+ * @throws {TypeError} - If the argument was not recognized
41
+ * @private
42
+ */
43
+ Block._from = function _from(arg) {
44
+ var info = {};
45
+ if (Buffer.isBuffer(arg)) {
46
+ info = Block._fromBufferReader(BufferReader(arg));
47
+ } else if (_.isObject(arg)) {
48
+ info = Block._fromObject(arg);
49
+ } else {
50
+ throw new TypeError('Unrecognized argument for Block');
51
+ }
52
+ return info;
53
+ };
54
+
55
+
56
+ /**
57
+ * Creates a Block instance from a plain object.
58
+ * @param {Object} data - The plain object containing block data.
59
+ * @returns {Block} The created Block instance.
60
+ * @private
61
+ */
62
+ Block._fromObject = function _fromObject(data) {
63
+ var transactions = [];
64
+ data.transactions.forEach(function (tx) {
65
+ if (tx instanceof Transaction) {
66
+ transactions.push(tx);
67
+ } else {
68
+ transactions.push(Transaction().fromObject(tx));
69
+ }
70
+ });
71
+ var info = {
72
+ header: BlockHeader.fromObject(data.header),
73
+ transactions: transactions,
74
+ };
75
+ return info;
76
+ };
77
+
78
+
79
+ /**
80
+ * Creates a Block instance from a plain JavaScript object.
81
+ * @param {Object} obj - The source object to convert to a Block.
82
+ * @returns {Block} A new Block instance.
83
+ */
84
+ Block.fromObject = function fromObject(obj) {
85
+ var info = Block._fromObject(obj);
86
+ return new Block(info);
87
+ };
88
+
89
+
90
+ /**
91
+ * Creates a Block instance from a BufferReader.
92
+ * @private
93
+ * @param {BufferReader} br - The buffer reader containing block data
94
+ * @returns {Block} The parsed Block instance
95
+ */
96
+ Block._fromBufferReader = function _fromBufferReader(br) {
97
+ var info = {};
98
+ $.checkState(!br.finished(), 'No block data received');
99
+ info.header = BlockHeader.fromBufferReader(br);
100
+ var transactions = br.readVarintNum();
101
+ info.transactions = [];
102
+ for (var i = 0; i < transactions; i++) {
103
+ info.transactions.push(Transaction().fromBufferReader(br));
104
+ }
105
+ return info;
106
+ };
107
+
108
+
109
+ /**
110
+ * Creates a Block instance from a BufferReader.
111
+ * @param {BufferReader} br - The buffer reader containing block data.
112
+ * @returns {Block} The parsed Block instance.
113
+ */
114
+ Block.fromBufferReader = function fromBufferReader(br) {
115
+ $.checkArgument(br, 'br is required');
116
+ var info = Block._fromBufferReader(br);
117
+ return new Block(info);
118
+ };
119
+
120
+
121
+ /**
122
+ * Creates a Block instance from a buffer.
123
+ * @param {Buffer} buf - The input buffer to create the block from.
124
+ * @returns {Block} The created Block instance.
125
+ */
126
+ Block.fromBuffer = function fromBuffer(buf) {
127
+ return Block.fromBufferReader(new BufferReader(buf));
128
+ };
129
+
130
+
131
+ /**
132
+ * Creates a Block instance from a string representation.
133
+ * @param {string} str - The string to parse into a Block.
134
+ * @returns {Block} The parsed Block instance.
135
+ */
136
+ Block.fromString = function fromString(str) {
137
+ var buf = Buffer.from(str, 'hex');
138
+ return Block.fromBuffer(buf);
139
+ };
140
+
141
+
142
+ /**
143
+ * Creates a Block instance from raw block data.
144
+ * @param {Object} data - The raw block data to convert.
145
+ * @returns {Block} A new Block instance.
146
+ */
147
+ Block.fromRawBlock = function fromRawBlock(data) {
148
+ if (!Buffer.isBuffer(data)) {
149
+ data = Buffer.from(data, 'binary');
150
+ }
151
+ var br = BufferReader(data);
152
+ br.pos = Block.Values.START_OF_BLOCK;
153
+ var info = Block._fromBufferReader(br);
154
+ return new Block(info);
155
+ };
156
+
157
+
158
+ /**
159
+ * Converts the Block instance to a plain object (also aliased as toJSON).
160
+ * @returns {Object} The plain object representation of the Block.
161
+ */
162
+ Block.prototype.toObject = Block.prototype.toJSON = function toObject() {
163
+ var transactions = [];
164
+ this.transactions.forEach(function (tx) {
165
+ transactions.push(tx.toObject());
166
+ });
167
+ return {
168
+ header: this.header.toObject(),
169
+ transactions: transactions,
170
+ };
171
+ };
172
+
173
+
174
+ /**
175
+ * Converts the block to a buffer representation.
176
+ * @returns {Buffer} The buffer containing the block data.
177
+ */
178
+ Block.prototype.toBuffer = function toBuffer() {
179
+ return this.toBufferWriter().concat();
180
+ };
181
+
182
+ /**
183
+ * Returns the string representation of the Block instance.
184
+ * @returns {string} - A hex encoded string of the block
185
+ */
186
+ Block.prototype.toString = function toString() {
187
+ return this.toBuffer().toString('hex');
188
+ };
189
+
190
+ /**
191
+ * @param {BufferWriter} - An existing instance of BufferWriter
192
+ * @returns {BufferWriter} - An instance of BufferWriter representation of the Block
193
+ */
194
+ Block.prototype.toBufferWriter = function toBufferWriter(bw) {
195
+ if (!bw) {
196
+ bw = new BufferWriter();
197
+ }
198
+ bw.write(this.header.toBuffer());
199
+ bw.writeVarintNum(this.transactions.length);
200
+ for (var i = 0; i < this.transactions.length; i++) {
201
+ this.transactions[i].toBufferWriter(false, bw);
202
+ }
203
+ return bw;
204
+ };
205
+
206
+ /**
207
+ * Will iterate through each transaction and return an array of hashes
208
+ * @returns {Array} - An array with transaction hashes
209
+ */
210
+ Block.prototype.getTransactionHashes = function getTransactionHashes() {
211
+ var hashes = [];
212
+ if (this.transactions.length === 0) {
213
+ return [Block.Values.NULL_HASH];
214
+ }
215
+ for (var t = 0; t < this.transactions.length; t++) {
216
+ hashes.push(this.transactions[t]._getHash());
217
+ }
218
+ return hashes;
219
+ };
220
+
221
+ /**
222
+ * Will build a merkle tree of all the transactions, ultimately arriving at
223
+ * a single point, the merkle root.
224
+ * @link https://en.bitcoin.it/wiki/Protocol_specification#Merkle_Trees
225
+ * @returns {Array} - An array with each level of the tree after the other.
226
+ */
227
+ Block.prototype.getMerkleTree = function getMerkleTree() {
228
+ var tree = this.getTransactionHashes();
229
+
230
+ var j = 0;
231
+ for (var size = this.transactions.length; size > 1; size = Math.floor((size + 1) / 2)) {
232
+ for (var i = 0; i < size; i += 2) {
233
+ var i2 = Math.min(i + 1, size - 1);
234
+ var buf = Buffer.concat([tree[j + i], tree[j + i2]]);
235
+ tree.push(Hash.sha256sha256(buf));
236
+ }
237
+ j += size;
238
+ }
239
+
240
+ return tree;
241
+ };
242
+
243
+ /**
244
+ * Calculates the merkleRoot from the transactions.
245
+ * @returns {Buffer} - A buffer of the merkle root hash
246
+ */
247
+ Block.prototype.getMerkleRoot = function getMerkleRoot() {
248
+ var tree = this.getMerkleTree();
249
+ return tree[tree.length - 1];
250
+ };
251
+
252
+ /**
253
+ * Verifies that the transactions in the block match the header merkle root
254
+ * @returns {Boolean} - If the merkle roots match
255
+ */
256
+ Block.prototype.validMerkleRoot = function validMerkleRoot() {
257
+ var h = new BN(this.header.merkleRoot.toString('hex'), 'hex');
258
+ var c = new BN(this.getMerkleRoot().toString('hex'), 'hex');
259
+
260
+ if (h.cmp(c) !== 0) {
261
+ return false;
262
+ }
263
+
264
+ return true;
265
+ };
266
+
267
+ /**
268
+ * @returns {Buffer} - The little endian hash buffer of the header
269
+ */
270
+ Block.prototype._getHash = function () {
271
+ return this.header._getHash();
272
+ };
273
+
274
+ var idProperty = {
275
+ configurable: false,
276
+ enumerable: true,
277
+ /**
278
+ * @returns {string} - The big endian hash buffer of the header
279
+ */
280
+ get: function () {
281
+ if (!this._id) {
282
+ this._id = this.header.id;
283
+ }
284
+ return this._id;
285
+ },
286
+ set: _.noop,
287
+ };
288
+ /**
289
+ * Defines the `id` property on the Block prototype using the provided `idProperty` descriptor.
290
+ * @memberof Block.prototype
291
+ * @name id
292
+ */
293
+ Object.defineProperty(Block.prototype, 'id', idProperty);
294
+ /**
295
+ * Defines a property 'hash' on Block.prototype using idProperty as the descriptor.
296
+ * @memberof Block.prototype
297
+ * @name hash
298
+ */
299
+ Object.defineProperty(Block.prototype, 'hash', idProperty);
300
+
301
+ /**
302
+ * @returns {string} - A string formatted for the console
303
+ */
304
+ Block.prototype.inspect = function inspect() {
305
+ return '<Block ' + this.id + '>';
306
+ };
307
+
308
+ /**
309
+ * Object containing constant values used by the Block module.
310
+ * @namespace Block.Values
311
+ */
312
+ Block.Values = {
313
+ START_OF_BLOCK: 8, // Start of block in raw block data
314
+ NULL_HASH: Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'),
315
+ };
316
+
317
+
318
+ /**
319
+ * Assigns the BlockHeader class to the Block namespace.
320
+ * @memberof Block
321
+ * @name BlockHeader
322
+ */
323
+ Block.BlockHeader = BlockHeader;
324
+
325
+ /**
326
+ * Assigns the MerkleBlock class to the Block namespace.
327
+ * @memberof Block
328
+ * @name MerkleBlock
329
+ */
330
+ Block.MerkleBlock = MerkleBlock;
331
+
332
+ module.exports = Block;
@@ -1,11 +1,11 @@
1
1
  'use strict';
2
2
 
3
- var _ = require('../util/_');
4
- var BN = require('../crypto/bn');
5
- var BufferReader = require('../encoding/bufferreader');
6
- var BufferWriter = require('../encoding/bufferwriter');
7
- var Hash = require('../crypto/hash');
8
- var $ = require('../util/preconditions');
3
+ var _ = require('../util/_.cjs');
4
+ var BN = require('../crypto/bn.cjs');
5
+ var BufferReader = require('../encoding/bufferreader.cjs');
6
+ var BufferWriter = require('../encoding/bufferwriter.cjs');
7
+ var Hash = require('../crypto/hash.cjs');
8
+ var $ = require('../util/preconditions.cjs');
9
9
 
10
10
  var GENESIS_BITS = 0x1d00ffff;
11
11
 
@@ -17,7 +17,7 @@ var GENESIS_BITS = 0x1d00ffff;
17
17
  * @returns {BlockHeader} - An instance of block header
18
18
  * @constructor
19
19
  */
20
- var BlockHeader = function BlockHeader(arg) {
20
+ function BlockHeader(arg) {
21
21
  if (!(this instanceof BlockHeader)) {
22
22
  return new BlockHeader(arg);
23
23
  }
@@ -232,6 +232,7 @@ BlockHeader.prototype.getDifficulty = function getDifficulty() {
232
232
 
233
233
  /**
234
234
  * @returns {Buffer} - The little endian hash buffer of the header
235
+ * @private
235
236
  */
236
237
  BlockHeader.prototype._getHash = function hash() {
237
238
  var buf = this.toBuffer();
@@ -0,0 +1,2 @@
1
+ var Block = require('./block.cjs');
2
+ module.exports = Block