@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
@@ -1,21 +1,21 @@
1
1
  'use strict';
2
2
 
3
- var _ = require('./util/_');
4
- var Address = require('./address');
5
- var Base58Check = require('./encoding/base58check');
6
- var BN = require('./crypto/bn');
7
- var JSUtil = require('./util/js');
8
- var Networks = require('./networks');
9
- var Point = require('./crypto/point');
10
- var PublicKey = require('./publickey');
11
- var Random = require('./crypto/random');
12
- var $ = require('./util/preconditions');
3
+ import _ from './util/_.js';
4
+ import Address from './address.js';
5
+ import Base58Check from './encoding/base58check.js';
6
+ import BN from './crypto/bn.js';
7
+ import JSUtil from './util/js.js';
8
+ import Networks from './networks.js';
9
+ import PublicKey from './publickey.js';
10
+ import Point from './crypto/point.js';
11
+ import Random from './crypto/random.js';
12
+ import $ from './util/preconditions.js';
13
13
 
14
14
  /**
15
15
  * Instantiate a PrivateKey from a BN, Buffer or WIF string.
16
16
  *
17
- * @param {string} data - The encoded data in various formats
18
- * @param {Network|string=} network - a {@link Network} object, or a string with the network name
17
+ * @param {string|BN|Buffer|Object} data - The encoded data in various formats
18
+ * @param {Network|string} [network] - a {@link Network} object, or a string with the network name
19
19
  * @returns {PrivateKey} A new valid instance of an PrivateKey
20
20
  * @constructor
21
21
  */
@@ -46,22 +46,60 @@ function PrivateKey(data, network) {
46
46
  network: info.network,
47
47
  });
48
48
 
49
- Object.defineProperty(this, 'publicKey', {
50
- configurable: false,
51
- enumerable: true,
52
- get: this.toPublicKey.bind(this),
53
- });
54
-
55
49
  return this;
56
50
  }
57
51
 
52
+ /**
53
+ * Gets the public key associated with this private key.
54
+ * @memberof PrivateKey.prototype
55
+ * @type {PublicKey}
56
+ * @readonly
57
+ */
58
+ Object.defineProperty(PrivateKey.prototype, 'publicKey', {
59
+ configurable: false,
60
+ enumerable: true,
61
+ get: function() {
62
+ return this.toPublicKey()
63
+ },
64
+ });
65
+
66
+ /**
67
+ * Gets the network associated with this private key.
68
+ * @memberof PrivateKey.prototype
69
+ * @type {Network}
70
+ * @readonly
71
+ */
72
+ Object.defineProperty(PrivateKey.prototype, 'network', {
73
+ configurable: false,
74
+ enumerable: true,
75
+ get: function () {
76
+ return this.network;
77
+ }
78
+ });
79
+
80
+
81
+ /**
82
+ * Indicates whether the private key is in compressed format.
83
+ * @memberof PrivateKey.prototype
84
+ * @type {boolean}
85
+ * @readonly
86
+ */
87
+ Object.defineProperty(PrivateKey.prototype, 'compressed', {
88
+ configurable: false,
89
+ enumerable: true,
90
+ get: function () {
91
+ return this.compressed;
92
+ }
93
+ });
94
+
58
95
  /**
59
96
  * Internal helper to instantiate PrivateKey internal `info` object from
60
97
  * different kinds of arguments passed to the constructor.
61
98
  *
62
- * @param {*} data
63
- * @param {Network|string=} network - a {@link Network} object, or a string with the network name
99
+ * @param {string|BN|Buffer|Object} data
100
+ * @param {Network|string} [network] - a {@link Network} object, or a string with the network name
64
101
  * @return {Object}
102
+ * @private
65
103
  */
66
104
  PrivateKey.prototype._classifyArguments = function (data, network) {
67
105
  var info = {
@@ -114,7 +152,7 @@ PrivateKey._getRandomBN = function () {
114
152
  * Internal function to transform a WIF Buffer into a private key
115
153
  *
116
154
  * @param {Buffer} buf - An WIF string
117
- * @param {Network|string=} network - a {@link Network} object, or a string with the network name
155
+ * @param {Network|string} [network] - a {@link Network} object, or a string with the network name
118
156
  * @returns {Object} An object with keys: bn, network and compressed
119
157
  * @private
120
158
  */
@@ -186,6 +224,12 @@ PrivateKey.fromBuffer = function (buf, network) {
186
224
  return new PrivateKey(buf, network);
187
225
  };
188
226
 
227
+ /**
228
+ * Creates a PrivateKey instance from a hexadecimal string.
229
+ * @param {string} hex - The hexadecimal string representation of the private key.
230
+ * @param {Network} network - The network associated with the private key.
231
+ * @returns {PrivateKey} A PrivateKey instance.
232
+ */
189
233
  PrivateKey.fromHex = function (hex, network) {
190
234
  return PrivateKey.fromBuffer(Buffer.from(hex, 'hex'), network);
191
235
  };
@@ -232,7 +276,7 @@ PrivateKey.fromObject = PrivateKey.fromJSON = function (obj) {
232
276
  /**
233
277
  * Instantiate a PrivateKey from random bytes
234
278
  *
235
- * @param {string=} network - Either "livenet" or "testnet"
279
+ * @param {string|Network} [network] - Either "livenet" or "testnet"
236
280
  * @returns {PrivateKey} A new valid instance of PrivateKey
237
281
  */
238
282
  PrivateKey.fromRandom = function (network) {
@@ -244,7 +288,7 @@ PrivateKey.fromRandom = function (network) {
244
288
  * Check if there would be any errors when initializing a PrivateKey
245
289
  *
246
290
  * @param {string} data - The encoded data in various formats
247
- * @param {string=} network - Either "livenet" or "testnet"
291
+ * @param {string|Network} [network] - Either "livenet" or "testnet"
248
292
  * @returns {null|Error} An error if exists
249
293
  */
250
294
 
@@ -262,7 +306,7 @@ PrivateKey.getValidationError = function (data, network) {
262
306
  * Check if the parameters are valid
263
307
  *
264
308
  * @param {string} data - The encoded data in various formats
265
- * @param {string=} network - Either "livenet" or "testnet"
309
+ * @param {string|Network} [network] - Either "livenet" or "testnet"
266
310
  * @returns {Boolean} If the private key is would be valid
267
311
  */
268
312
  PrivateKey.isValid = function (data, network) {
@@ -322,6 +366,10 @@ PrivateKey.prototype.toBuffer = function () {
322
366
  return this.bn.toBuffer({ size: 32 });
323
367
  };
324
368
 
369
+ /**
370
+ * Converts the private key to a hexadecimal string representation.
371
+ * @returns {string} Hexadecimal string of the private key.
372
+ */
325
373
  PrivateKey.prototype.toHex = function () {
326
374
  return this.toBuffer().toString('hex');
327
375
  };
@@ -333,20 +381,20 @@ PrivateKey.prototype.toHex = function () {
333
381
  */
334
382
  PrivateKey.prototype.toPublicKey = function () {
335
383
  if (!this._pubkey) {
336
- this._pubkey = PublicKey.fromPrivateKey(this);
384
+ this._pubkey = PublicKey.fromPrivateKey(this);
337
385
  }
338
386
  return this._pubkey;
339
387
  };
340
388
 
341
389
  /**
342
390
  * Will return an address for the private key
343
- * @param {Network=} network - optional parameter specifying
391
+ * @param {Network|string} [network] - optional parameter specifying
344
392
  * the desired network for the address
345
393
  *
346
394
  * @returns {Address} An address generated from the private key
347
395
  */
348
396
  PrivateKey.prototype.toAddress = function (network) {
349
- var pubkey = this.toPublicKey();
397
+ var pubkey = this.toPublicKey().toBuffer();
350
398
  return Address.fromPublicKey(pubkey, network || this.network);
351
399
  };
352
400
 
@@ -371,4 +419,4 @@ PrivateKey.prototype.inspect = function () {
371
419
  return '<PrivateKey: ' + this.toHex() + ', network: ' + this.network + uncompressed + '>';
372
420
  };
373
421
 
374
- module.exports = PrivateKey;
422
+ export default PrivateKey;
@@ -0,0 +1,384 @@
1
+ 'use strict';
2
+
3
+ import BN from './crypto/bn.js';
4
+ import Point from './crypto/point.js';
5
+ import Hash from './crypto/hash.js';
6
+ import JSUtil from './util/js.js';
7
+ import Networks from './networks.js';
8
+ import _ from './util/_.js';
9
+ import $ from './util/preconditions.js';
10
+ import Address from './address.js';
11
+ /**
12
+ * Instantiate a PublicKey from a {@link PrivateKey}, {@link Point}, `string`, or `Buffer`.
13
+ *
14
+ * There are two internal properties, `network` and `compressed`, that deal with importing
15
+ * a PublicKey from a PrivateKey in WIF format. More details described on {@link PrivateKey}
16
+ *
17
+ * @example
18
+ * ```javascript
19
+ * // instantiate from a private key
20
+ * var key = PublicKey(privateKey, true);
21
+ *
22
+ * // export to as a DER hex encoded string
23
+ * var exported = key.toString();
24
+ *
25
+ * // import the public key
26
+ * var imported = PublicKey.fromString(exported);
27
+ * ```
28
+ *
29
+ * @param {string} data - The encoded data in various formats
30
+ * @param {Object} extra - additional options
31
+ * @param {Network} extra.network - Which network should the address for this public key be for
32
+ * @param {String=} extra.compressed - If the public key is compressed
33
+ * @returns {PublicKey} A new valid instance of an PublicKey
34
+ * @constructor
35
+ */
36
+ function PublicKey(data, extra) {
37
+ if (!(this instanceof PublicKey)) {
38
+ return new PublicKey(data, extra);
39
+ }
40
+
41
+ $.checkArgument(data, 'First argument is required, please include public key data.');
42
+
43
+ if (data instanceof PublicKey) {
44
+ // Return copy, but as it's an immutable object, return same argument
45
+ return data;
46
+ }
47
+ extra = extra || {};
48
+
49
+ var info = this._classifyArgs(data, extra);
50
+
51
+ // validation
52
+ info.point.validate();
53
+
54
+ JSUtil.defineImmutable(this, {
55
+ point: info.point,
56
+ compressed: info.compressed,
57
+ network: info.network || Networks.defaultNetwork,
58
+ });
59
+
60
+ return this;
61
+ }
62
+
63
+ /**
64
+ * Internal function to differentiate between arguments passed to the constructor
65
+ * @param {*} data
66
+ * @param {Object} extra
67
+ */
68
+ PublicKey.prototype._classifyArgs = function (data, extra) {
69
+ var info = {
70
+ compressed: _.isUndefined(extra.compressed) || extra.compressed,
71
+ };
72
+
73
+ // detect type of data
74
+ if (data instanceof Point) {
75
+ info.point = data;
76
+ } else if (data.x && data.y) {
77
+ info = PublicKey._transformObject(data);
78
+ } else if (typeof data === 'string') {
79
+ info = PublicKey._transformDER(Buffer.from(data, 'hex'));
80
+ } else if (PublicKey._isBuffer(data)) {
81
+ info = PublicKey._transformDER(data);
82
+ } else if (PublicKey._isPrivateKey(data)) {
83
+ info = PublicKey._transformPrivateKey(data);
84
+ } else {
85
+ throw new TypeError('First argument is an unrecognized data format.');
86
+ }
87
+ if (!info.network) {
88
+ info.network = _.isUndefined(extra.network) ? undefined : Networks.get(extra.network);
89
+ }
90
+ return info;
91
+ };
92
+
93
+ /**
94
+ * Internal function to detect if an object is a {@link PrivateKey}
95
+ *
96
+ * @param {*} param - object to test
97
+ * @returns {boolean}
98
+ * @private
99
+ */
100
+ PublicKey._isPrivateKey = function (param) {
101
+ return param && param.toPublicKey;
102
+ };
103
+
104
+ /**
105
+ * Internal function to detect if an object is a Buffer
106
+ *
107
+ * @param {*} param - object to test
108
+ * @returns {boolean}
109
+ * @private
110
+ */
111
+ PublicKey._isBuffer = function (param) {
112
+ return param instanceof Buffer || param instanceof Uint8Array;
113
+ };
114
+
115
+ /**
116
+ * Internal function to transform a private key into a public key point
117
+ *
118
+ * @param {PrivateKey} privkey - An instance of PrivateKey
119
+ * @returns {Object} An object with keys: point and compressed
120
+ * @private
121
+ */
122
+ PublicKey._transformPrivateKey = function (privkey) {
123
+ $.checkArgument(PublicKey._isPrivateKey(privkey), 'Must be an instance of PrivateKey');
124
+ var info = {};
125
+ info.point = Point.getG().mul(privkey.bn);
126
+ info.compressed = privkey.compressed;
127
+ info.network = privkey.network;
128
+ return info;
129
+ };
130
+
131
+ /**
132
+ * Internal function to transform DER into a public key point
133
+ *
134
+ * @param {Buffer} buf - An DER buffer
135
+ * @param {bool=} strict - if set to false, will loosen some conditions
136
+ * @returns {Object} An object with keys: point and compressed
137
+ * @private
138
+ */
139
+ PublicKey._transformDER = function (buf, strict) {
140
+ $.checkArgument(PublicKey._isBuffer(buf), 'Must be a buffer of DER encoded public key');
141
+ var info = {};
142
+
143
+ strict = _.isUndefined(strict) ? true : strict;
144
+
145
+ var x;
146
+ var y;
147
+ var xbuf;
148
+ var ybuf;
149
+
150
+ if (buf[0] === 0x04 || (!strict && (buf[0] === 0x06 || buf[0] === 0x07))) {
151
+ xbuf = buf.slice(1, 33);
152
+ ybuf = buf.slice(33, 65);
153
+ if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) {
154
+ throw new TypeError('Length of x and y must be 32 bytes');
155
+ }
156
+ x = new BN(xbuf);
157
+ y = new BN(ybuf);
158
+ info.point = new Point(x, y);
159
+ info.compressed = false;
160
+ } else if (buf[0] === 0x03) {
161
+ xbuf = buf.slice(1);
162
+ x = new BN(xbuf);
163
+ info = PublicKey._transformX(true, x);
164
+ info.compressed = true;
165
+ } else if (buf[0] === 0x02) {
166
+ xbuf = buf.slice(1);
167
+ x = new BN(xbuf);
168
+ info = PublicKey._transformX(false, x);
169
+ info.compressed = true;
170
+ } else {
171
+ throw new TypeError('Invalid DER format public key');
172
+ }
173
+ return info;
174
+ };
175
+
176
+ /**
177
+ * Internal function to transform X into a public key point
178
+ *
179
+ * @param {Boolean} odd - If the point is above or below the x axis
180
+ * @param {Point} x - The x point
181
+ * @returns {Object} An object with keys: point and compressed
182
+ * @private
183
+ */
184
+ PublicKey._transformX = function (odd, x) {
185
+ $.checkArgument(typeof odd === 'boolean', 'Must specify whether y is odd or not (true or false)');
186
+ var info = {};
187
+ info.point = Point.fromX(odd, x);
188
+ return info;
189
+ };
190
+
191
+ /**
192
+ * Internal function to transform a JSON into a public key point
193
+ *
194
+ * @param {String|Object} json - a JSON string or plain object
195
+ * @returns {Object} An object with keys: point and compressed
196
+ * @private
197
+ */
198
+ PublicKey._transformObject = function (json) {
199
+ var x = new BN(json.x, 'hex');
200
+ var y = new BN(json.y, 'hex');
201
+ var point = new Point(x, y);
202
+ return new PublicKey(point, {
203
+ compressed: json.compressed,
204
+ });
205
+ };
206
+
207
+ /**
208
+ * Instantiate a PublicKey from a PrivateKey
209
+ *
210
+ * @param {PrivateKey} privkey - An instance of PrivateKey
211
+ * @returns {PublicKey} A new valid instance of PublicKey
212
+ */
213
+ PublicKey.fromPrivateKey = function (privkey) {
214
+ $.checkArgument(PublicKey._isPrivateKey(privkey), 'Must be an instance of PrivateKey');
215
+ var info = PublicKey._transformPrivateKey(privkey);
216
+ return new PublicKey(info.point, {
217
+ compressed: info.compressed,
218
+ network: info.network,
219
+ });
220
+ };
221
+
222
+ /**
223
+ * Instantiate a PublicKey from a Buffer
224
+ * @param {Buffer} buf - A DER buffer
225
+ * @param {bool=} strict - if set to false, will loosen some conditions
226
+ * @returns {PublicKey} A new valid instance of PublicKey
227
+ */
228
+ PublicKey.fromDER = PublicKey.fromBuffer = function (buf, strict) {
229
+ $.checkArgument(PublicKey._isBuffer(buf), 'Must be a buffer of DER encoded public key');
230
+ var info = PublicKey._transformDER(buf, strict);
231
+ return new PublicKey(info.point, {
232
+ compressed: info.compressed,
233
+ });
234
+ };
235
+
236
+ /**
237
+ * Instantiate a PublicKey from a Point
238
+ *
239
+ * @param {Point} point - A Point instance
240
+ * @param {boolean=} compressed - whether to store this public key as compressed format
241
+ * @returns {PublicKey} A new valid instance of PublicKey
242
+ */
243
+ PublicKey.fromPoint = function (point, compressed) {
244
+ $.checkArgument(point instanceof Point, 'First argument must be an instance of Point.');
245
+ return new PublicKey(point, {
246
+ compressed: compressed,
247
+ });
248
+ };
249
+
250
+ /**
251
+ * Instantiate a PublicKey from a DER hex encoded string
252
+ *
253
+ * @param {string} str - A DER hex string
254
+ * @param {String=} encoding - The type of string encoding
255
+ * @returns {PublicKey} A new valid instance of PublicKey
256
+ */
257
+ PublicKey.fromHex = PublicKey.fromString = function (str, encoding) {
258
+ var buf = Buffer.from(str, encoding || 'hex');
259
+ var info = PublicKey._transformDER(buf);
260
+ return new PublicKey(info.point, {
261
+ compressed: info.compressed,
262
+ });
263
+ };
264
+
265
+ /**
266
+ * Instantiate a PublicKey from an X Point
267
+ *
268
+ * @param {Boolean} odd - If the point is above or below the x axis
269
+ * @param {Point} x - The x point
270
+ * @returns {PublicKey} A new valid instance of PublicKey
271
+ */
272
+ PublicKey.fromX = function (odd, x) {
273
+ var info = PublicKey._transformX(odd, x);
274
+ return new PublicKey(info.point, {
275
+ compressed: info.compressed,
276
+ });
277
+ };
278
+
279
+ /**
280
+ * Check if there would be any errors when initializing a PublicKey
281
+ *
282
+ * @param {string} data - The encoded data in various formats
283
+ * @returns {null|Error} An error if exists
284
+ */
285
+ PublicKey.getValidationError = function (data) {
286
+ var error;
287
+ try {
288
+ new PublicKey(data);
289
+ } catch (e) {
290
+ error = e;
291
+ }
292
+ return error;
293
+ };
294
+
295
+ /**
296
+ * Check if the parameters are valid
297
+ *
298
+ * @param {string} data - The encoded data in various formats
299
+ * @returns {Boolean} If the public key would be valid
300
+ */
301
+ PublicKey.isValid = function (data) {
302
+ return !PublicKey.getValidationError(data);
303
+ };
304
+
305
+ /**
306
+ * @returns {Object} A plain object of the PublicKey
307
+ */
308
+ PublicKey.prototype.toObject = PublicKey.prototype.toJSON = function toObject() {
309
+ return {
310
+ x: this.point.getX().toString('hex', 2),
311
+ y: this.point.getY().toString('hex', 2),
312
+ compressed: this.compressed,
313
+ };
314
+ };
315
+
316
+ /**
317
+ * Will output the PublicKey to a DER Buffer
318
+ *
319
+ * @returns {Buffer} A DER hex encoded buffer
320
+ */
321
+ PublicKey.prototype.toBuffer = PublicKey.prototype.toDER = function () {
322
+ var x = this.point.getX();
323
+ var y = this.point.getY();
324
+
325
+ var xbuf = x.toBuffer({
326
+ size: 32,
327
+ });
328
+ var ybuf = y.toBuffer({
329
+ size: 32,
330
+ });
331
+
332
+ var prefix;
333
+ if (!this.compressed) {
334
+ prefix = Buffer.from([0x04]);
335
+ return Buffer.concat([prefix, xbuf, ybuf]);
336
+ } else {
337
+ var odd = ybuf[ybuf.length - 1] % 2;
338
+ if (odd) {
339
+ prefix = Buffer.from([0x03]);
340
+ } else {
341
+ prefix = Buffer.from([0x02]);
342
+ }
343
+ return Buffer.concat([prefix, xbuf]);
344
+ }
345
+ };
346
+
347
+ /**
348
+ * Will return a sha256 + ripemd160 hash of the serialized public key
349
+ * @see https://github.com/bitcoin/bitcoin/blob/master/src/pubkey.h#L141
350
+ * @returns {Buffer}
351
+ */
352
+ PublicKey.prototype._getID = function _getID() {
353
+ return Hash.sha256ripemd160(this.toBuffer());
354
+ };
355
+
356
+ /**
357
+ * Will return an address for the public key
358
+ *
359
+ * @param {string|Network} [network] - Which network should the address be for
360
+ * @returns {Address} An address generated from the public key
361
+ */
362
+ PublicKey.prototype.toAddress = function (network) {
363
+ return Address.fromPublicKey(this.toBuffer(), network || this.network);
364
+ };
365
+
366
+ /**
367
+ * Will output the PublicKey to a DER encoded hex string
368
+ *
369
+ * @returns {string} A DER hex encoded string
370
+ */
371
+ PublicKey.prototype.toString = PublicKey.prototype.toHex = function () {
372
+ return this.toDER().toString('hex');
373
+ };
374
+
375
+ /**
376
+ * Will return a string formatted for the console
377
+ *
378
+ * @returns {string} Public key
379
+ */
380
+ PublicKey.prototype.inspect = function () {
381
+ return '<PublicKey: ' + this.toHex() + (this.compressed ? '' : ', uncompressed') + '>';
382
+ };
383
+
384
+ export default PublicKey;
@@ -0,0 +1,2 @@
1
+ import Script from './script.js';
2
+ export default Script;