@opcat-labs/opcat 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (293) hide show
  1. package/.cjs2esm.json +18 -0
  2. package/.mocharc.yaml +1 -1
  3. package/CHANGELOG.md +6 -0
  4. package/README.md +6 -0
  5. package/{lib/address.js → cjs/address.cjs} +77 -72
  6. package/cjs/block/block.cjs +332 -0
  7. package/{lib/block/blockheader.js → cjs/block/blockheader.cjs} +8 -7
  8. package/cjs/block/index.cjs +2 -0
  9. package/{lib/block/merkleblock.js → cjs/block/merkleblock.cjs} +23 -15
  10. package/cjs/bn.cjs +3411 -0
  11. package/{lib/crypto/bn.js → cjs/crypto/bn.cjs} +3 -3
  12. package/{lib/crypto/ecdsa.js → cjs/crypto/ecdsa.cjs} +150 -14
  13. package/{lib/crypto/hash.node.js → cjs/crypto/hash.cjs} +13 -2
  14. package/cjs/crypto/index.cjs +16 -0
  15. package/{lib/crypto/point.js → cjs/crypto/point.cjs} +11 -4
  16. package/cjs/crypto/random.cjs +18 -0
  17. package/{lib/crypto/signature.js → cjs/crypto/signature.cjs} +158 -8
  18. package/{lib/encoding/base58.js → cjs/encoding/base58.cjs} +58 -2
  19. package/cjs/encoding/base58check.cjs +192 -0
  20. package/cjs/encoding/bufferreader.cjs +333 -0
  21. package/cjs/encoding/bufferwriter.cjs +244 -0
  22. package/{lib/encoding/decode-asm.js → cjs/encoding/decode-asm.cjs} +4 -4
  23. package/{lib/encoding/decode-hex.js → cjs/encoding/decode-hex.cjs} +1 -1
  24. package/cjs/encoding/index.cjs +14 -0
  25. package/cjs/encoding/varint.cjs +116 -0
  26. package/{lib/errors/index.js → cjs/errors/index.cjs} +9 -9
  27. package/{lib/errors/spec.js → cjs/errors/spec.cjs} +2 -2
  28. package/cjs/hash-cache.cjs +98 -0
  29. package/{lib/hdprivatekey.js → cjs/hdprivatekey.cjs} +232 -140
  30. package/{lib/hdpublickey.js → cjs/hdpublickey.cjs} +120 -93
  31. package/cjs/index.cjs +94 -0
  32. package/cjs/interpreter/index.cjs +2 -0
  33. package/cjs/interpreter/interpreter.cjs +1988 -0
  34. package/{lib/script/stack.js → cjs/interpreter/stack.cjs} +9 -2
  35. package/{lib/message/message.js → cjs/message/message.cjs} +62 -25
  36. package/cjs/mnemonic/index.cjs +3 -0
  37. package/{lib/mnemonic/mnemonic.js → cjs/mnemonic/mnemonic.cjs} +44 -13
  38. package/{lib/mnemonic/pbkdf2.node.js → cjs/mnemonic/pbkdf2.cjs} +9 -2
  39. package/cjs/mnemonic/words/index.cjs +66 -0
  40. package/cjs/network.cjs +13 -0
  41. package/cjs/networks.cjs +321 -0
  42. package/{lib/opcode.js → cjs/opcode.cjs} +69 -5
  43. package/cjs/privatekey.cjs +422 -0
  44. package/{lib/publickey.js → cjs/publickey.cjs} +14 -16
  45. package/cjs/script/index.cjs +2 -0
  46. package/{lib/script/script.js → cjs/script/script.cjs} +322 -67
  47. package/cjs/transaction/index.cjs +5 -0
  48. package/cjs/transaction/input/index.cjs +34 -0
  49. package/cjs/transaction/input/input.cjs +396 -0
  50. package/{lib/transaction/input/multisig.js → cjs/transaction/input/multisig.cjs} +112 -18
  51. package/{lib/transaction/input/publickey.js → cjs/transaction/input/publickey.cjs} +29 -19
  52. package/{lib/transaction/input/publickeyhash.js → cjs/transaction/input/publickeyhash.cjs} +25 -17
  53. package/{lib/transaction/output.js → cjs/transaction/output.cjs} +100 -15
  54. package/cjs/transaction/sighash.cjs +187 -0
  55. package/{lib/transaction/signature.js → cjs/transaction/signature.cjs} +30 -6
  56. package/cjs/transaction/transaction.cjs +2000 -0
  57. package/{lib/transaction/unspentoutput.js → cjs/transaction/unspentoutput.cjs} +5 -5
  58. package/cjs/util/derivation.cjs +53 -0
  59. package/cjs/util/index.cjs +11 -0
  60. package/cjs/util/js.cjs +95 -0
  61. package/{lib/util/preconditions.js → cjs/util/preconditions.cjs} +2 -2
  62. package/esm/address.js +483 -0
  63. package/{lib → esm}/block/block.js +82 -27
  64. package/esm/block/blockheader.js +296 -0
  65. package/esm/block/index.js +2 -0
  66. package/esm/block/merkleblock.js +331 -0
  67. package/esm/bn.js +3411 -0
  68. package/esm/crypto/bn.js +278 -0
  69. package/esm/crypto/ecdsa.js +475 -0
  70. package/{lib/crypto/hash.browser.js → esm/crypto/hash.js} +18 -7
  71. package/esm/crypto/index.js +16 -0
  72. package/esm/crypto/point.js +228 -0
  73. package/esm/crypto/random.js +18 -0
  74. package/esm/crypto/signature.js +475 -0
  75. package/esm/encoding/base58.js +167 -0
  76. package/esm/encoding/base58check.js +192 -0
  77. package/esm/encoding/bufferreader.js +333 -0
  78. package/esm/encoding/bufferwriter.js +243 -0
  79. package/esm/encoding/decode-asm.js +24 -0
  80. package/esm/encoding/decode-hex.js +32 -0
  81. package/esm/encoding/decode-script-chunks.js +43 -0
  82. package/esm/encoding/encode-hex.js +284 -0
  83. package/esm/encoding/index.js +14 -0
  84. package/esm/encoding/is-hex.js +7 -0
  85. package/esm/encoding/varint.js +116 -0
  86. package/esm/errors/index.js +54 -0
  87. package/esm/errors/spec.js +314 -0
  88. package/esm/hash-cache.js +98 -0
  89. package/esm/hdprivatekey.js +768 -0
  90. package/esm/hdpublickey.js +549 -0
  91. package/esm/index.js +66 -0
  92. package/esm/interpreter/index.js +2 -0
  93. package/{lib/script → esm/interpreter}/interpreter.js +219 -66
  94. package/esm/interpreter/stack.js +116 -0
  95. package/esm/message/message.js +228 -0
  96. package/esm/mnemonic/index.js +3 -0
  97. package/esm/mnemonic/mnemonic.js +332 -0
  98. package/{lib/mnemonic/pbkdf2.browser.js → esm/mnemonic/pbkdf2.js} +13 -6
  99. package/esm/mnemonic/words/chinese.js +2054 -0
  100. package/esm/mnemonic/words/english.js +2054 -0
  101. package/esm/mnemonic/words/french.js +2054 -0
  102. package/esm/mnemonic/words/index.js +66 -0
  103. package/esm/mnemonic/words/italian.js +2054 -0
  104. package/esm/mnemonic/words/japanese.js +2054 -0
  105. package/esm/mnemonic/words/spanish.js +2054 -0
  106. package/esm/network.js +13 -0
  107. package/{lib → esm}/networks.js +61 -120
  108. package/esm/opcode.js +319 -0
  109. package/{lib → esm}/privatekey.js +76 -28
  110. package/esm/publickey.js +384 -0
  111. package/esm/script/index.js +2 -0
  112. package/esm/script/script.js +1329 -0
  113. package/esm/script/write-i32-le.js +17 -0
  114. package/esm/script/write-push-data.js +35 -0
  115. package/esm/script/write-u16-le.js +12 -0
  116. package/esm/script/write-u32-le.js +16 -0
  117. package/esm/script/write-u64-le.js +24 -0
  118. package/esm/script/write-u8-le.js +8 -0
  119. package/esm/script/write-varint.js +46 -0
  120. package/esm/transaction/index.js +5 -0
  121. package/esm/transaction/input/index.js +33 -0
  122. package/{lib → esm}/transaction/input/input.js +132 -90
  123. package/esm/transaction/input/multisig.js +335 -0
  124. package/esm/transaction/input/publickey.js +108 -0
  125. package/esm/transaction/input/publickeyhash.js +124 -0
  126. package/esm/transaction/output.js +316 -0
  127. package/{lib → esm}/transaction/sighash.js +42 -22
  128. package/esm/transaction/signature.js +120 -0
  129. package/{lib → esm}/transaction/transaction.js +522 -163
  130. package/esm/transaction/unspentoutput.js +112 -0
  131. package/esm/util/_.js +47 -0
  132. package/esm/util/derivation.js +53 -0
  133. package/esm/util/index.js +12 -0
  134. package/esm/util/js.js +95 -0
  135. package/esm/util/preconditions.js +33 -0
  136. package/fixup.cjs +17 -0
  137. package/package.json +18 -4
  138. package/test/{address.js → address.cjs} +14 -43
  139. package/test/block/{block.js → block.cjs} +3 -5
  140. package/test/block/{blockheader.js → blockheader.cjs} +2 -2
  141. package/test/block/{merklebloack.js → merklebloack.cjs} +2 -2
  142. package/test/crypto/{ecdsa.js → ecdsa.cjs} +9 -9
  143. package/test/crypto/{hash.browser.js → hash.browser.cjs} +2 -1
  144. package/test/crypto/{signature.js → signature.cjs} +2 -2
  145. package/test/data/bitcoind/script_tests.json +5 -5
  146. package/test/{hashCache.js → hashCache.cjs} +2 -1
  147. package/test/{hdkeys.js → hdkeys.cjs} +4 -2
  148. package/test/{hdprivatekey.js → hdprivatekey.cjs} +7 -6
  149. package/test/{hdpublickey.js → hdpublickey.cjs} +2 -7
  150. package/test/mnemonic/{pbkdf2.test.js → pbkdf2.test.cjs} +2 -2
  151. package/test/{networks.js → networks.cjs} +12 -31
  152. package/test/{publickey.js → publickey.cjs} +2 -2
  153. package/test/script/{interpreter.js → interpreter.cjs} +5 -5
  154. package/test/script/{script.js → script.cjs} +8 -2
  155. package/test/transaction/{deserialize.js → deserialize.cjs} +2 -2
  156. package/test/transaction/input/{input.js → input.cjs} +1 -1
  157. package/test/transaction/input/{multisig.js → multisig.cjs} +2 -1
  158. package/test/transaction/input/{publickeyhash.js → publickeyhash.cjs} +1 -1
  159. package/test/transaction/{sighash.js → sighash.cjs} +1 -1
  160. package/test/transaction/{transaction.js → transaction.cjs} +2 -2
  161. package/tsconfig.json +13 -0
  162. package/types/address.d.cts +252 -0
  163. package/types/block/block.d.cts +139 -0
  164. package/types/block/blockheader.d.cts +125 -0
  165. package/types/block/index.d.cts +2 -0
  166. package/types/block/merkleblock.d.cts +95 -0
  167. package/types/bn.d.cts +202 -0
  168. package/types/crypto/bn.d.cts +2 -0
  169. package/types/crypto/ecdsa.d.cts +187 -0
  170. package/types/crypto/hash.d.cts +118 -0
  171. package/types/crypto/index.d.cts +7 -0
  172. package/types/crypto/point.d.cts +134 -0
  173. package/types/crypto/random.d.cts +13 -0
  174. package/types/crypto/signature.d.cts +160 -0
  175. package/types/encoding/base58.d.cts +106 -0
  176. package/types/encoding/base58check.d.cts +107 -0
  177. package/types/encoding/bufferreader.d.cts +164 -0
  178. package/types/encoding/bufferwriter.d.cts +126 -0
  179. package/types/encoding/decode-asm.d.cts +2 -0
  180. package/types/encoding/decode-hex.d.cts +2 -0
  181. package/types/encoding/decode-script-chunks.d.cts +14 -0
  182. package/types/encoding/encode-hex.d.cts +2 -0
  183. package/types/encoding/index.d.cts +6 -0
  184. package/types/encoding/is-hex.d.cts +2 -0
  185. package/types/encoding/varint.d.cts +66 -0
  186. package/types/errors/index.d.cts +4 -0
  187. package/types/errors/spec.d.cts +22 -0
  188. package/types/hash-cache.d.cts +65 -0
  189. package/types/hdprivatekey.d.cts +281 -0
  190. package/types/hdpublickey.d.cts +240 -0
  191. package/types/index.d.cts +26 -0
  192. package/types/interpreter/index.d.cts +2 -0
  193. package/types/interpreter/interpreter.d.cts +228 -0
  194. package/types/interpreter/stack.d.cts +35 -0
  195. package/types/message/message.d.cts +110 -0
  196. package/types/mnemonic/index.d.cts +2 -0
  197. package/types/mnemonic/mnemonic.d.cts +171 -0
  198. package/types/mnemonic/pbkdf2.d.cts +14 -0
  199. package/types/mnemonic/words/chinese.d.cts +2 -0
  200. package/types/mnemonic/words/english.d.cts +2 -0
  201. package/types/mnemonic/words/french.d.cts +2 -0
  202. package/types/mnemonic/words/index.d.cts +22 -0
  203. package/types/mnemonic/words/italian.d.cts +2 -0
  204. package/types/mnemonic/words/japanese.d.cts +2 -0
  205. package/types/mnemonic/words/spanish.d.cts +2 -0
  206. package/types/network.d.cts +11 -0
  207. package/types/networks.d.cts +76 -0
  208. package/types/opcode.d.cts +93 -0
  209. package/types/privatekey.d.cts +169 -0
  210. package/types/publickey.d.cts +202 -0
  211. package/types/script/index.d.cts +2 -0
  212. package/types/script/script.d.cts +449 -0
  213. package/types/script/write-i32-le.d.cts +2 -0
  214. package/types/script/write-push-data.d.cts +2 -0
  215. package/types/script/write-u16-le.d.cts +2 -0
  216. package/types/script/write-u32-le.d.cts +2 -0
  217. package/types/script/write-u64-le.d.cts +2 -0
  218. package/types/script/write-u8-le.d.cts +2 -0
  219. package/types/script/write-varint.d.cts +2 -0
  220. package/types/transaction/index.d.cts +2 -0
  221. package/types/transaction/input/index.d.cts +2 -0
  222. package/types/transaction/input/input.d.cts +178 -0
  223. package/types/transaction/input/multisig.d.cts +127 -0
  224. package/types/transaction/input/publickey.d.cts +44 -0
  225. package/types/transaction/input/publickeyhash.d.cts +45 -0
  226. package/types/transaction/output.d.cts +118 -0
  227. package/types/transaction/sighash.d.cts +61 -0
  228. package/types/transaction/signature.d.cts +43 -0
  229. package/types/transaction/transaction.d.cts +716 -0
  230. package/types/transaction/unspentoutput.d.cts +83 -0
  231. package/types/util/_.d.cts +26 -0
  232. package/types/util/derivation.d.cts +21 -0
  233. package/types/util/index.d.cts +5 -0
  234. package/types/util/js.d.cts +50 -0
  235. package/types/util/preconditions.d.cts +3 -0
  236. package/index.d.ts +0 -1541
  237. package/index.js +0 -74
  238. package/lib/block/index.js +0 -4
  239. package/lib/bn.js +0 -3423
  240. package/lib/crypto/hash.js +0 -2
  241. package/lib/crypto/random.browser.js +0 -28
  242. package/lib/crypto/random.js +0 -2
  243. package/lib/crypto/random.node.js +0 -11
  244. package/lib/encoding/base58check.js +0 -121
  245. package/lib/encoding/bufferreader.js +0 -212
  246. package/lib/encoding/bufferwriter.js +0 -140
  247. package/lib/encoding/varint.js +0 -75
  248. package/lib/hash-cache.js +0 -50
  249. package/lib/mnemonic/pbkdf2.js +0 -2
  250. package/lib/mnemonic/words/index.js +0 -8
  251. package/lib/script/index.js +0 -5
  252. package/lib/transaction/index.js +0 -7
  253. package/lib/transaction/input/index.js +0 -5
  254. package/lib/util/js.js +0 -90
  255. /package/{lib/encoding/decode-script-chunks.js → cjs/encoding/decode-script-chunks.cjs} +0 -0
  256. /package/{lib/encoding/encode-hex.js → cjs/encoding/encode-hex.cjs} +0 -0
  257. /package/{lib/encoding/is-hex.js → cjs/encoding/is-hex.cjs} +0 -0
  258. /package/{lib/mnemonic/words/chinese.js → cjs/mnemonic/words/chinese.cjs} +0 -0
  259. /package/{lib/mnemonic/words/english.js → cjs/mnemonic/words/english.cjs} +0 -0
  260. /package/{lib/mnemonic/words/french.js → cjs/mnemonic/words/french.cjs} +0 -0
  261. /package/{lib/mnemonic/words/italian.js → cjs/mnemonic/words/italian.cjs} +0 -0
  262. /package/{lib/mnemonic/words/japanese.js → cjs/mnemonic/words/japanese.cjs} +0 -0
  263. /package/{lib/mnemonic/words/spanish.js → cjs/mnemonic/words/spanish.cjs} +0 -0
  264. /package/{lib/script/write-i32-le.js → cjs/script/write-i32-le.cjs} +0 -0
  265. /package/{lib/script/write-push-data.js → cjs/script/write-push-data.cjs} +0 -0
  266. /package/{lib/script/write-u16-le.js → cjs/script/write-u16-le.cjs} +0 -0
  267. /package/{lib/script/write-u32-le.js → cjs/script/write-u32-le.cjs} +0 -0
  268. /package/{lib/script/write-u64-le.js → cjs/script/write-u64-le.cjs} +0 -0
  269. /package/{lib/script/write-u8-le.js → cjs/script/write-u8-le.cjs} +0 -0
  270. /package/{lib/script/write-varint.js → cjs/script/write-varint.cjs} +0 -0
  271. /package/{lib/util/_.js → cjs/util/_.cjs} +0 -0
  272. /package/test/crypto/{bn.js → bn.cjs} +0 -0
  273. /package/test/crypto/{hash.js → hash.cjs} +0 -0
  274. /package/test/crypto/{point.js → point.cjs} +0 -0
  275. /package/test/crypto/{random.js → random.cjs} +0 -0
  276. /package/test/data/{blk86756-testnet.js → blk86756-testnet.cjs} +0 -0
  277. /package/test/data/{merkleblocks.js → merkleblocks.cjs} +0 -0
  278. /package/test/encoding/{base58.js → base58.cjs} +0 -0
  279. /package/test/encoding/{base58check.js → base58check.cjs} +0 -0
  280. /package/test/encoding/{bufferreader.js → bufferreader.cjs} +0 -0
  281. /package/test/encoding/{bufferwriter.js → bufferwriter.cjs} +0 -0
  282. /package/test/encoding/{varint.js → varint.cjs} +0 -0
  283. /package/test/{index.js → index.cjs} +0 -0
  284. /package/test/message/{message.js → message.cjs} +0 -0
  285. /package/test/mnemonic/{mnemonic.js → mnemonic.cjs} +0 -0
  286. /package/test/{opcode.js → opcode.cjs} +0 -0
  287. /package/test/{privatekey.js → privatekey.cjs} +0 -0
  288. /package/test/transaction/input/{publickey.js → publickey.cjs} +0 -0
  289. /package/test/transaction/{output.js → output.cjs} +0 -0
  290. /package/test/transaction/{signature.js → signature.cjs} +0 -0
  291. /package/test/transaction/{unspentoutput.js → unspentoutput.cjs} +0 -0
  292. /package/test/util/{js.js → js.cjs} +0 -0
  293. /package/test/util/{preconditions.js → preconditions.cjs} +0 -0
@@ -0,0 +1,112 @@
1
+ 'use strict';
2
+
3
+ import _ from '../util/_.js';
4
+ import $ from '../util/preconditions.js';
5
+ import JSUtil from '../util/js.js';
6
+ import Script from '../script/index.js';
7
+ import Address from '../address.js';
8
+
9
+ /**
10
+ * Represents an unspent output information: its script, associated amount and address,
11
+ * transaction id and output index.
12
+ *
13
+ * @constructor
14
+ * @param {object} data
15
+ * @param {string} data.txid the previous transaction id
16
+ * @param {string=} data.txId alias for `txid`
17
+ * @param {number} data.vout the index in the transaction
18
+ * @param {number=} data.outputIndex alias for `vout`
19
+ * @param {string|Script} data.scriptPubKey the script that must be resolved to release the funds
20
+ * @param {string|Script=} data.script alias for `scriptPubKey`
21
+ * @param {number} data.amount amount of bitcoins associated
22
+ * @param {number=} data.satoshis alias for `amount`, but expressed in satoshis (1 OPCAT = 1e8 satoshis)
23
+ * @param {string|Address=} data.address the associated address to the script, if provided
24
+ */
25
+ function UnspentOutput(data) {
26
+ if (!(this instanceof UnspentOutput)) {
27
+ return new UnspentOutput(data);
28
+ }
29
+ $.checkArgument(_.isObject(data), 'Must provide an object from where to extract data');
30
+ var address = data.address ? new Address(data.address) : undefined;
31
+ var txId = data.txid ? data.txid : data.txId;
32
+ if (!txId || !JSUtil.isHexaString(txId) || txId.length > 64) {
33
+ // TODO: Use the errors library
34
+ throw new Error('Invalid TXID in object', data);
35
+ }
36
+ var outputIndex = _.isUndefined(data.vout) ? data.outputIndex : data.vout;
37
+ if (!_.isNumber(outputIndex)) {
38
+ throw new Error('Invalid outputIndex, received ' + outputIndex);
39
+ }
40
+ $.checkArgument(
41
+ !_.isUndefined(data.scriptPubKey) || !_.isUndefined(data.script),
42
+ 'Must provide the scriptPubKey for that output!',
43
+ );
44
+ var script = new Script(data.scriptPubKey || data.script);
45
+ $.checkArgument(
46
+ !_.isUndefined(data.amount) || !_.isUndefined(data.satoshis),
47
+ 'Must provide an amount for the output',
48
+ );
49
+ var amount = !_.isUndefined(data.amount) ? Math.round(data.amount * 1e8) : data.satoshis;
50
+ var outputData = !_.isUndefined(data.data) ? Buffer.from(data.data, 'hex') : Buffer.from([])
51
+ $.checkArgument(_.isNumber(amount), 'Amount must be a number');
52
+ JSUtil.defineImmutable(this, {
53
+ address: address,
54
+ txId: txId,
55
+ outputIndex: outputIndex,
56
+ script: script,
57
+ satoshis: amount,
58
+ data: outputData,
59
+ });
60
+ }
61
+
62
+ /**
63
+ * Provide an informative output when displaying this object in the console
64
+ * @returns string
65
+ */
66
+ UnspentOutput.prototype.inspect = function () {
67
+ return (
68
+ '<UnspentOutput: ' +
69
+ this.txId +
70
+ ':' +
71
+ this.outputIndex +
72
+ ', satoshis: ' +
73
+ this.satoshis +
74
+ ', address: ' +
75
+ this.address +
76
+ '>'
77
+ );
78
+ };
79
+
80
+ /**
81
+ * String representation: just "txid:index"
82
+ * @returns string
83
+ */
84
+ UnspentOutput.prototype.toString = function () {
85
+ return this.txId + ':' + this.outputIndex;
86
+ };
87
+
88
+ /**
89
+ * Deserialize an UnspentOutput from an object
90
+ * @param {object|string} data
91
+ * @return UnspentOutput
92
+ */
93
+ UnspentOutput.fromObject = function (data) {
94
+ return new UnspentOutput(data);
95
+ };
96
+
97
+ /**
98
+ * Returns a plain object (no prototype or methods) with the associated info for this output
99
+ * @return {object}
100
+ */
101
+ UnspentOutput.prototype.toObject = UnspentOutput.prototype.toJSON = function toObject() {
102
+ return {
103
+ address: this.address ? this.address.toString() : undefined,
104
+ txid: this.txId,
105
+ vout: this.outputIndex,
106
+ scriptPubKey: this.script.toBuffer().toString('hex'),
107
+ amount: Number.parseFloat((this.satoshis / 1e8).toFixed(8)),
108
+ data: this.data.toString('hex')
109
+ };
110
+ };
111
+
112
+ export default UnspentOutput;
package/esm/util/_.js ADDED
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ var _ = {};
4
+
5
+ _.isArray = (t) => Array.isArray(t);
6
+ _.isNumber = (t) => typeof t === 'number';
7
+ _.isObject = (t) => t && typeof t === 'object';
8
+ _.isString = (t) => typeof t === 'string';
9
+ _.isUndefined = (t) => typeof t === 'undefined';
10
+ _.isFunction = (t) => typeof t === 'function';
11
+ _.isNull = (t) => t === null;
12
+ _.isDate = (t) => t instanceof Date;
13
+ _.extend = (a, b) => Object.assign(a, b);
14
+ _.noop = () => {};
15
+ _.every = (a, f) => a.every(f || ((t) => t));
16
+ _.map = (a, f) => Array.from(a).map(f || ((t) => t));
17
+ _.includes = (a, e) => a.includes(e);
18
+ _.each = (a, f) => a.forEach(f);
19
+ _.clone = (o) => Object.assign({}, o);
20
+ _.pick = (object, keys) => {
21
+ const obj = {};
22
+ keys.forEach((key) => {
23
+ if (typeof object[key] !== 'undefined') {
24
+ obj[key] = object[key];
25
+ }
26
+ });
27
+ return obj;
28
+ };
29
+ _.values = (o) => Object.values(o);
30
+ _.filter = (a, f) => a.filter(f);
31
+ _.reduce = (a, f, s) => a.reduce(f, s);
32
+ _.without = (a, n) => a.filter((t) => t !== n);
33
+ _.shuffle = (a) => {
34
+ const result = a.slice(0);
35
+ for (let i = result.length - 1; i > 0; i--) {
36
+ const j = Math.floor(Math.random() * (i + 1));
37
+ [result[i], result[j]] = [result[j], result[i]];
38
+ }
39
+ return result;
40
+ };
41
+ _.difference = (a, b) => a.filter((t) => !b.includes(t));
42
+ _.findIndex = (a, f) => a.findIndex(f);
43
+ _.some = (a, f) => a.some(f);
44
+ _.range = (n) => [...Array(n).keys()];
45
+ _.isPositiveNumber = (n) => n < 0x7e;
46
+
47
+ export default _;
@@ -0,0 +1,53 @@
1
+
2
+ import _ from './_.js';
3
+
4
+ /**
5
+ * Represents a derivation function or class (purpose to be determined based on implementation).
6
+ * @constructor
7
+ */
8
+ function Derivation() {
9
+ }
10
+
11
+ Derivation.RootElementAlias = ['m', 'M', "m'", "M'"]
12
+ Derivation.Hardened = 0x80000000;
13
+
14
+ /**
15
+ * function that splits a string path into a derivation index array.
16
+ * It will return null if the string path is malformed.
17
+ * It does not validate if indexes are in bounds.
18
+ *
19
+ * @param {string} path
20
+ * @return {Array}
21
+ */
22
+ Derivation.getDerivationIndexes = function getDerivationIndexes(path) {
23
+ var steps = path.split('/');
24
+
25
+ // Special cases:
26
+ if (_.includes(Derivation.RootElementAlias, path)) {
27
+ return [];
28
+ }
29
+
30
+ if (!_.includes(Derivation.RootElementAlias, steps[0])) {
31
+ return null;
32
+ }
33
+
34
+ var indexes = steps.slice(1).map(function (step) {
35
+ var isHardened = step.slice(-1) === "'";
36
+ if (isHardened) {
37
+ step = step.slice(0, -1);
38
+ }
39
+ if (!step || step[0] === '-') {
40
+ return NaN;
41
+ }
42
+ var index = +step; // cast to number
43
+ if (isHardened) {
44
+ index += Derivation.Hardened;
45
+ }
46
+
47
+ return index;
48
+ });
49
+
50
+ return _.some(indexes, isNaN) ? null : indexes;
51
+ }
52
+
53
+ export default Derivation;
@@ -0,0 +1,12 @@
1
+ import js from './js.js';
2
+ import preconditions from './preconditions.js';
3
+ import _ from './_.js';
4
+ import derivation from './derivation.js';
5
+
6
+ export default {
7
+ js,
8
+ preconditions,
9
+ _,
10
+ derivation,
11
+ };
12
+
package/esm/util/js.js ADDED
@@ -0,0 +1,95 @@
1
+ 'use strict';
2
+
3
+ import _ from './_.js';
4
+ import $ from './preconditions.js';
5
+
6
+ /**
7
+ * Utility functions for JavaScript operations.
8
+ * @constructor
9
+ */
10
+ function JSUtil() {
11
+ }
12
+
13
+ /**
14
+ * Determines whether a string contains only hexadecimal values
15
+ *
16
+ * @name JSUtil.isHexa
17
+ * @param {string} value
18
+ * @return {boolean} true if the string is the hexa representation of a number
19
+ */
20
+ JSUtil.isHexa = function isHexa(value) {
21
+ if (!_.isString(value)) {
22
+ return false;
23
+ }
24
+ return /^[0-9a-fA-F]+$/.test(value);
25
+ };
26
+
27
+ JSUtil.isHexaString = JSUtil.isHexa;
28
+
29
+ /**
30
+ * Checks that a value is a natural number, a positive integer or zero.
31
+ *
32
+ * @param {*} value
33
+ * @return {Boolean}
34
+ */
35
+ JSUtil.isNaturalNumber = function isNaturalNumber(value) {
36
+ return (
37
+ typeof value === 'number' && isFinite(value) && Math.floor(value) === value && value >= 0
38
+ );
39
+ }
40
+
41
+ /**
42
+ * Transform a 4-byte integer (unsigned value) into a Buffer of length 4 (Big Endian Byte Order)
43
+ *
44
+ * @param {number} integer
45
+ * @return {Buffer}
46
+ */
47
+ JSUtil.integerAsBuffer = function integerAsBuffer(integer) {
48
+ $.checkArgumentType(integer, 'number', 'integer');
49
+ const buf = Buffer.allocUnsafe(4);
50
+ buf.writeUInt32BE(integer, 0);
51
+ return buf;
52
+ }
53
+
54
+ /**
55
+ * Test if an argument is a valid JSON object. If it is, returns a truthy
56
+ * value (the json object decoded), so no double JSON.parse call is necessary
57
+ *
58
+ * @param {string} arg
59
+ * @return {Object|boolean} false if the argument is not a JSON string.
60
+ */
61
+ JSUtil.isValidJSON = function isValidJSON(arg) {
62
+ var parsed;
63
+ if (!_.isString(arg)) {
64
+ return false;
65
+ }
66
+ try {
67
+ parsed = JSON.parse(arg);
68
+ } catch (e) {
69
+ return false;
70
+ }
71
+ if (typeof parsed === 'object') {
72
+ return true;
73
+ }
74
+ return false;
75
+ }
76
+
77
+ /**
78
+ * Define immutable properties on a target object
79
+ *
80
+ * @param {Object} target - An object to be extended
81
+ * @param {Object} values - An object of properties
82
+ * @return {Object} The target object
83
+ */
84
+ JSUtil.defineImmutable = function defineImmutable(target, values) {
85
+ Object.keys(values).forEach(function (key) {
86
+ Object.defineProperty(target, key, {
87
+ configurable: false,
88
+ enumerable: true,
89
+ value: values[key],
90
+ });
91
+ });
92
+ return target;
93
+ }
94
+
95
+ export default JSUtil;
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ import errors from '../errors/index.js';
4
+ import _ from './_.js';
5
+
6
+ export default {
7
+ checkState: function (condition, message) {
8
+ if (!condition) {
9
+ throw new errors.InvalidState(message);
10
+ }
11
+ },
12
+ checkArgument: function (condition, argumentName, message, docsPath) {
13
+ if (!condition) {
14
+ throw new errors.InvalidArgument(argumentName, message, docsPath);
15
+ }
16
+ },
17
+ checkArgumentType: function (argument, type, argumentName) {
18
+ argumentName = argumentName || '(unknown name)';
19
+ if (_.isString(type)) {
20
+ if (type === 'Buffer') {
21
+ if (!Buffer.isBuffer(argument)) {
22
+ throw new errors.InvalidArgumentType(argument, type, argumentName);
23
+ }
24
+ } else if (typeof argument !== type) {
25
+ throw new errors.InvalidArgumentType(argument, type, argumentName);
26
+ }
27
+ } else {
28
+ if (!(argument instanceof type)) {
29
+ throw new errors.InvalidArgumentType(argument, type.name, argumentName);
30
+ }
31
+ }
32
+ },
33
+ };
package/fixup.cjs ADDED
@@ -0,0 +1,17 @@
1
+ const glob = require('glob');
2
+ const fs = require('fs');
3
+
4
+ const updateRequires = (filePath) => {
5
+ let content = fs.readFileSync(filePath, 'utf8');
6
+ content = content.replace(/.cjs/g, ".js");
7
+ fs.writeFileSync(filePath, content, 'utf8');
8
+ };
9
+
10
+ async function main() {
11
+ const files = await glob.glob('./esm/**/*.js', { nodir: true });
12
+ files.forEach((file) => {
13
+ updateRequires(file);
14
+ });
15
+ }
16
+
17
+ main();
package/package.json CHANGED
@@ -1,11 +1,23 @@
1
1
  {
2
2
  "name": "@opcat-labs/opcat",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "opcat base SDK",
5
- "main": "./index.js",
6
- "types": "./index.d.ts",
5
+ "main": "./cjs/index.cjs",
6
+ "module": "./esm/index.js",
7
+ "types": "./types/index.d.cts",
8
+ "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "require": "./cjs/index.cjs",
12
+ "import": "./esm/index.js",
13
+ "types": "./types/index.d.cts"
14
+ }
15
+ },
7
16
  "scripts": {
8
- "lint": "standard --fix",
17
+ "gen:esm": "cjs2esm",
18
+ "postgen:esm": "npm run fixup",
19
+ "fixup": "node fixup.cjs",
20
+ "gen:types": "tsc",
9
21
  "test": "mocha"
10
22
  },
11
23
  "dependencies": {
@@ -18,6 +30,8 @@
18
30
  "unorm": "1.4.1"
19
31
  },
20
32
  "devDependencies": {
33
+ "cjs2esm": "^5.0.0",
34
+ "clone-deep": "^4.0.1",
21
35
  "parcel": "^2.15.2",
22
36
  "sinon": "^20.0.0"
23
37
  },
@@ -265,13 +265,7 @@ describe('Address', function () {
265
265
  it('should error because of incorrect type for pubkey transform', function () {
266
266
  (function () {
267
267
  return Address._transformPublicKey(Buffer.alloc(20));
268
- }).should.throw('Address must be an instance of PublicKey.');
269
- });
270
-
271
- it('should error because of incorrect type for script transform', function () {
272
- (function () {
273
- return Address._transformScript(Buffer.alloc(20));
274
- }).should.throw('Invalid Argument: script must be a Script instance');
268
+ }).should.throw('Pubkey supplied is not a buffer with 33 or 65 bytes.');
275
269
  });
276
270
 
277
271
  it('should error because of incorrect type for string transform', function () {
@@ -315,7 +309,7 @@ describe('Address', function () {
315
309
  var pubkey = new PublicKey(
316
310
  '0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004',
317
311
  );
318
- var address = Address.fromPublicKey(pubkey, 'livenet');
312
+ var address = Address.fromPublicKey(pubkey.toBuffer(), 'livenet');
319
313
  address.toString().should.equal('19gH5uhqY6DKrtkU66PsZPUZdzTd11Y7ke');
320
314
  });
321
315
 
@@ -323,7 +317,7 @@ describe('Address', function () {
323
317
  var pubkey = new PublicKey(
324
318
  '0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004',
325
319
  );
326
- var address = Address.fromPublicKey(pubkey);
320
+ var address = Address.fromPublicKey(pubkey.toBuffer());
327
321
  address.network.should.equal(Networks.defaultNetwork);
328
322
  });
329
323
 
@@ -332,9 +326,9 @@ describe('Address', function () {
332
326
  '0485e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b00' +
333
327
  '4833fef26c8be4c4823754869ff4e46755b85d851077771c220e2610496a29d98',
334
328
  );
335
- var a = Address.fromPublicKey(pubkey, 'livenet');
329
+ var a = Address.fromPublicKey(pubkey.toBuffer(), 'livenet');
336
330
  a.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX');
337
- var b = new Address(pubkey, 'livenet', 'pubkeyhash');
331
+ var b = new Address(pubkey.toBuffer(), 'livenet', 'pubkeyhash');
338
332
  b.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX');
339
333
  });
340
334
 
@@ -358,30 +352,6 @@ describe('Address', function () {
358
352
  Networks.remove(network);
359
353
  });
360
354
 
361
- describe('from a script', function () {
362
- it('should fail to build address from a non p2sh,p2pkh script', function () {
363
- var s = new Script('OP_CHECKMULTISIG');
364
- (function () {
365
- return new Address(s);
366
- }).should.throw('needs to be p2pkh in, p2pkh out, p2sh in, or p2sh out');
367
- });
368
- it('should make this address from a p2pkh output script', function () {
369
- var s = new Script(
370
- 'OP_DUP OP_HASH160 20 ' +
371
- '0xc8e11b0eb0d2ad5362d894f048908341fa61b6e1 OP_EQUALVERIFY OP_CHECKSIG',
372
- );
373
- var a = Address.fromScript(s, 'livenet');
374
- a.toString().should.equal('1KK9oz4bFH8c1t6LmighHaoSEGx3P3FEmc');
375
- var b = new Address(s, 'livenet');
376
- b.toString().should.equal('1KK9oz4bFH8c1t6LmighHaoSEGx3P3FEmc');
377
- });
378
-
379
- it('returns the same address if the script is a pay to public key hash out', function () {
380
- var address = '16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX';
381
- var script = Script.buildPublicKeyHashOut(new Address(address));
382
- Address(script, Networks.livenet).toString().should.equal(address);
383
- });
384
- });
385
355
 
386
356
  it('should derive from this known address string livenet', function () {
387
357
  var address = new Address(str);
@@ -475,34 +445,35 @@ describe('Address', function () {
475
445
  it('should derive from public key', function () {
476
446
  let privateKey = PrivateKey.fromRandom();
477
447
  let publicKey = PublicKey.fromPrivateKey(privateKey);
478
- let address = Address.fromPublicKey(publicKey);
448
+ let address = Address.fromPublicKey(publicKey.toBuffer(), publicKey.network);
479
449
  address.toString()[0].should.equal('1');
480
450
  });
481
451
 
482
452
  it('should derive from public key testnet', function () {
483
453
  let privateKey = PrivateKey.fromRandom('testnet');
484
- let publicKey = PublicKey.fromPrivateKey(privateKey);
485
- let address = Address.fromPublicKey(publicKey, 'testnet');
486
- (address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true);
454
+ let address = privateKey.toAddress()
455
+ let addresStr = address.toString();
456
+ (addresStr[0] === 'm' || addresStr[0] === 'n').should.equal(true);
487
457
  });
488
458
  });
489
459
 
490
460
  describe('#fromPrivateKey', function () {
491
461
  it('should derive from public key', function () {
492
462
  let privateKey = PrivateKey.fromRandom();
493
- let address = Address.fromPrivateKey(privateKey);
463
+ let address = privateKey.toAddress()
494
464
  address.toString()[0].should.equal('1');
495
465
  });
496
466
 
497
467
  it('should derive from public key testnet', function () {
498
468
  let privateKey = PrivateKey.fromRandom('testnet');
499
- let address = Address.fromPrivateKey(privateKey, 'testnet');
500
- (address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true);
469
+ let address = privateKey.toAddress()
470
+ let addresStr = address.toString();
471
+ (addresStr[0] === 'm' || addresStr[0] === 'n').should.equal(true);
501
472
  });
502
473
 
503
474
  it('should derive from public key testnet', function () {
504
475
  let privateKey = PrivateKey.fromRandom('testnet');
505
- let address = Address.fromPrivateKey(privateKey);
476
+ let address = privateKey.toAddress();
506
477
  (address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true);
507
478
  });
508
479
  });
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var opcat = require('../..');
4
- var BN = require('../../lib/crypto/bn');
4
+ var BN = opcat.crypto.BN
5
5
  var BufferReader = opcat.encoding.BufferReader;
6
6
  var BufferWriter = opcat.encoding.BufferWriter;
7
7
  var BlockHeader = opcat.BlockHeader;
@@ -11,11 +11,9 @@ var fs = require('fs');
11
11
  var should = chai.should();
12
12
  var Transaction = opcat.Transaction;
13
13
 
14
- // https://test-insight.bitpay.com/block/000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11
15
- var dataRawBlockBuffer = fs.readFileSync('test/data/blk86756-testnet.dat');
16
- var dataRawBlockBinary = fs.readFileSync('test/data/blk86756-testnet.dat', 'binary');
14
+
17
15
  var dataJson = fs.readFileSync('test/data/blk86756-testnet.json').toString();
18
- var data = require('../data/blk86756-testnet');
16
+ var data = require('../data/blk86756-testnet.cjs');
19
17
  var dataBlocks = require('../data/bitcoind/blocks');
20
18
  var bockHex = fs.readFileSync('test/data/block.hex').toString();
21
19
 
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var opcat = require('../..');
4
- var BN = require('../../lib/crypto/bn');
4
+ var BN = opcat.crypto.BN;
5
5
  var BufferReader = opcat.encoding.BufferReader;
6
6
  var BufferWriter = opcat.encoding.BufferWriter;
7
7
 
@@ -13,7 +13,7 @@ var should = require('chai').should();
13
13
  var dataRawBlockBuffer = fs.readFileSync('test/data/blk86756-testnet.dat');
14
14
  var dataRawBlockBinary = fs.readFileSync('test/data/blk86756-testnet.dat', 'binary');
15
15
  var dataRawId = '000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11';
16
- var data = require('../data/blk86756-testnet');
16
+ var data = require('../data/blk86756-testnet.cjs');
17
17
 
18
18
  describe('BlockHeader', function () {
19
19
  var version = data.version;
@@ -2,12 +2,12 @@
2
2
 
3
3
  var should = require('chai').should();
4
4
 
5
- var opcat = require('../../index.js');
5
+ var opcat = require('../../');
6
6
  var MerkleBlock = opcat.MerkleBlock;
7
7
  var BufferReader = opcat.encoding.BufferReader;
8
8
  var BufferWriter = opcat.encoding.BufferWriter;
9
9
  var Transaction = opcat.Transaction;
10
- var data = require('../data/merkleblocks.js');
10
+ var data = require('../data/merkleblocks.cjs');
11
11
  var transactionVector = require('../data/tx_creation');
12
12
 
13
13
  describe('MerkleBlock', function () {
@@ -1,14 +1,14 @@
1
1
  'use strict';
2
-
3
- var ECDSA = require('../../lib/crypto/ecdsa');
4
- var Hash = require('../../lib/crypto/hash');
5
- var Privkey = require('../../lib/privatekey');
6
- var Pubkey = require('../../lib/publickey');
7
- var Signature = require('../../lib/crypto/signature');
8
- var BN = require('../../lib/crypto/bn');
9
- var point = require('../../lib/crypto/point');
2
+ var opcat = require('../../')
3
+ var ECDSA = opcat.crypto.ECDSA;
4
+ var Hash = opcat.crypto.Hash;
5
+ var Privkey = opcat.PrivateKey;
6
+ var Pubkey = opcat.PublicKey;
7
+ var Signature = opcat.crypto.Signature;
8
+ var BN = opcat.crypto.BN;
9
+ var point = opcat.crypto.Point;
10
10
  var should = require('chai').should();
11
- var vectors = require('../data/ecdsa');
11
+ var vectors = require('../data/ecdsa.json');
12
12
 
13
13
  describe('ECDSA', function () {
14
14
  it('instantiation', function () {
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  require('chai').should();
4
- var Hash = require('../../lib/crypto/hash.browser');
4
+ var opcat = require('../../')
5
+ var Hash = opcat.crypto.Hash;
5
6
 
6
7
  describe('Hash', function () {
7
8
  var buf = Buffer.from([0, 1, 2, 3, 253, 254, 255]);
@@ -1,12 +1,12 @@
1
1
  'use strict';
2
2
 
3
- var _ = require('../../lib/util/_');
4
3
  var should = require('chai').should();
5
4
  var opcat = require('../..');
5
+ var _ = opcat.util._;
6
6
  var BN = opcat.crypto.BN;
7
7
  var Signature = opcat.crypto.Signature;
8
8
  var JSUtil = opcat.util.js;
9
- var Interpreter = opcat.Script.Interpreter;
9
+ var Interpreter = opcat.Interpreter;
10
10
 
11
11
  var sigCanonical = require('../data/bitcoind/sig_canonical');
12
12
  var sigNonCanonical = require('../data/bitcoind/sig_noncanonical');