@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
@@ -1,33 +1,41 @@
1
1
  'use strict';
2
2
 
3
3
  var assert = require('assert');
4
- var _ = require('./util/_');
5
- var $ = require('./util/preconditions');
6
-
7
- var BN = require('./crypto/bn');
8
- var Base58 = require('./encoding/base58');
9
- var Base58Check = require('./encoding/base58check');
10
- var Hash = require('./crypto/hash');
11
- var Network = require('./networks');
12
- var Point = require('./crypto/point');
13
- var PrivateKey = require('./privatekey');
14
- var Random = require('./crypto/random');
15
-
16
- var errors = require('./errors');
4
+ var _ = require('./util/_.cjs');
5
+ var $ = require('./util/preconditions.cjs');
6
+ var Derivation = require('./util/derivation.cjs');
7
+
8
+ var BN = require('./crypto/bn.cjs');
9
+ var Base58 = require('./encoding/base58.cjs');
10
+ var Base58Check = require('./encoding/base58check.cjs');
11
+ var Hash = require('./crypto/hash.cjs');
12
+ var Networks = require('./networks.cjs');
13
+ var Point = require('./crypto/point.cjs');
14
+ var PrivateKey = require('./privatekey.cjs');
15
+ var Random = require('./crypto/random.cjs');
16
+ var HDPublicKey = require('./hdpublickey.cjs');
17
+ var JSUtil = require('./util/js.cjs');
18
+
19
+ var errors = require('./errors/index.cjs');
20
+
17
21
  var hdErrors = errors.HDPrivateKey;
18
- var JSUtil = require('./util/js');
19
22
 
20
23
  var MINIMUM_ENTROPY_BITS = 128;
21
24
  var BITS_TO_BYTES = 1 / 8;
22
25
  var MAXIMUM_ENTROPY_BITS = 512;
23
26
 
24
27
  /**
25
- * Represents an instance of an hierarchically derived private key.
26
- *
28
+ * Creates a new HDPrivateKey instance from various input formats.
27
29
  * More info on https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
28
- *
29
30
  * @constructor
30
- * @param {string|Buffer|Object} arg
31
+ * @param {HDPrivateKey|string|Buffer|Object} arg - Input can be:
32
+ * - Existing HDPrivateKey instance (returns same instance)
33
+ * - Network name (generates random key for that network)
34
+ * - Serialized string/Buffer (base58 encoded)
35
+ * - JSON string
36
+ * - Object with key properties
37
+ * @throws {hdErrors.UnrecognizedArgument} If input format is not recognized
38
+ * @throws {Error} If serialized input is invalid
31
39
  */
32
40
  function HDPrivateKey(arg) {
33
41
  if (arg instanceof HDPrivateKey) {
@@ -40,7 +48,7 @@ function HDPrivateKey(arg) {
40
48
  return this._generateRandomly();
41
49
  }
42
50
 
43
- if (Network.get(arg)) {
51
+ if (Networks.get(arg)) {
44
52
  return this._generateRandomly(arg);
45
53
  } else if (_.isString(arg) || Buffer.isBuffer(arg)) {
46
54
  if (HDPrivateKey.isValidSerialized(arg)) {
@@ -59,6 +67,41 @@ function HDPrivateKey(arg) {
59
67
  }
60
68
  }
61
69
 
70
+ /**
71
+ * Gets the hdPublicKey of the HDPrivateKey.
72
+ * @name HDPrivateKey.prototype.hdPublicKey
73
+ * @type {HDPublicKey}
74
+ * @memberof HDPrivateKey
75
+ */
76
+ Object.defineProperty(HDPrivateKey.prototype, 'hdPublicKey', {
77
+ configurable: false,
78
+ enumerable: true,
79
+ get: function () {
80
+ this._calcHDPublicKey();
81
+ return this._hdPublicKey;
82
+ },
83
+ });
84
+
85
+ /**
86
+ * Gets the xpubkey of the HDPrivateKey.
87
+ * @name HDPrivateKey.prototype.xpubkey
88
+ * @type {string}
89
+ * @memberof HDPrivateKey
90
+ */
91
+ Object.defineProperty(HDPrivateKey.prototype, 'xpubkey', {
92
+ configurable: false,
93
+ enumerable: true,
94
+ get: function () {
95
+ this._calcHDPublicKey();
96
+ return this._hdPublicKey.xpubkey;
97
+ },
98
+ });
99
+
100
+
101
+ /**
102
+ * Creates a new HDPrivateKey instance with random values.
103
+ * @returns {HDPrivateKey} A new HDPrivateKey object with randomly generated properties.
104
+ */
62
105
  HDPrivateKey.fromRandom = function () {
63
106
  return new HDPrivateKey();
64
107
  };
@@ -67,12 +110,12 @@ HDPrivateKey.fromRandom = function () {
67
110
  * Verifies that a given path is valid.
68
111
  *
69
112
  * @param {string|number} arg
70
- * @param {boolean?} hardened
113
+ * @param {boolean} [hardened]
71
114
  * @return {boolean}
72
115
  */
73
116
  HDPrivateKey.isValidPath = function (arg, hardened) {
74
117
  if (_.isString(arg)) {
75
- var indexes = HDPrivateKey._getDerivationIndexes(arg);
118
+ var indexes = Derivation.getDerivationIndexes(arg);
76
119
  return indexes !== null && _.every(indexes, HDPrivateKey.isValidPath);
77
120
  }
78
121
 
@@ -86,76 +129,7 @@ HDPrivateKey.isValidPath = function (arg, hardened) {
86
129
  return false;
87
130
  };
88
131
 
89
- /**
90
- * Internal function that splits a string path into a derivation index array.
91
- * It will return null if the string path is malformed.
92
- * It does not validate if indexes are in bounds.
93
- *
94
- * @param {string} path
95
- * @return {Array}
96
- */
97
- HDPrivateKey._getDerivationIndexes = function (path) {
98
- var steps = path.split('/');
99
-
100
- // Special cases:
101
- if (_.includes(HDPrivateKey.RootElementAlias, path)) {
102
- return [];
103
- }
104
-
105
- if (!_.includes(HDPrivateKey.RootElementAlias, steps[0])) {
106
- return null;
107
- }
108
-
109
- var indexes = steps.slice(1).map(function (step) {
110
- var isHardened = step.slice(-1) === "'";
111
- if (isHardened) {
112
- step = step.slice(0, -1);
113
- }
114
- if (!step || step[0] === '-') {
115
- return NaN;
116
- }
117
- var index = +step; // cast to number
118
- if (isHardened) {
119
- index += HDPrivateKey.Hardened;
120
- }
121
-
122
- return index;
123
- });
124
-
125
- return _.some(indexes, isNaN) ? null : indexes;
126
- };
127
132
 
128
- /**
129
- * WARNING: This method is deprecated. Use deriveChild or deriveNonCompliantChild instead. This is not BIP32 compliant
130
- *
131
- *
132
- * Get a derived child based on a string or number.
133
- *
134
- * If the first argument is a string, it's parsed as the full path of
135
- * derivation. Valid values for this argument include "m" (which returns the
136
- * same private key), "m/0/1/40/2'/1000", where the ' quote means a hardened
137
- * derivation.
138
- *
139
- * If the first argument is a number, the child with that index will be
140
- * derived. If the second argument is truthy, the hardened version will be
141
- * derived. See the example usage for clarification.
142
- *
143
- * @example
144
- * ```javascript
145
- * var parent = new HDPrivateKey('xprv...');
146
- * var child_0_1_2h = parent.derive(0).derive(1).derive(2, true);
147
- * var copy_of_child_0_1_2h = parent.derive("m/0/1/2'");
148
- * assert(child_0_1_2h.xprivkey === copy_of_child_0_1_2h);
149
- * ```
150
- *
151
- * @param {string|number} arg
152
- * @param {boolean?} hardened
153
- */
154
- HDPrivateKey.prototype.derive = function () {
155
- throw new Error(
156
- 'derive has been deprecated. use deriveChild or, for the old way, deriveNonCompliantChild.',
157
- );
158
- };
159
133
 
160
134
  /**
161
135
  * WARNING: This method will not be officially supported until v1.0.0.
@@ -184,7 +158,7 @@ HDPrivateKey.prototype.derive = function () {
184
158
  * ```
185
159
  *
186
160
  * @param {string|number} arg
187
- * @param {boolean?} hardened
161
+ * @param {boolean} [hardened]
188
162
  */
189
163
  HDPrivateKey.prototype.deriveChild = function (arg, hardened) {
190
164
  if (_.isNumber(arg)) {
@@ -209,8 +183,10 @@ HDPrivateKey.prototype.deriveChild = function (arg, hardened) {
209
183
  * serialization of a private key, such that it's still possible to derive the privateKey
210
184
  * to recover those funds.
211
185
  *
212
- * @param {string|number} arg
213
- * @param {boolean?} hardened
186
+ * @param {number|string} arg - Either a child index number or derivation path string
187
+ * @param {boolean} [hardened] - Whether to create hardened derivation (only used with number arg)
188
+ * @returns {HDPrivateKey} The derived child private key
189
+ * @throws {hdErrors.InvalidDerivationArgument} If argument type is invalid
214
190
  */
215
191
  HDPrivateKey.prototype.deriveNonCompliantChild = function (arg, hardened) {
216
192
  if (_.isNumber(arg)) {
@@ -222,6 +198,17 @@ HDPrivateKey.prototype.deriveNonCompliantChild = function (arg, hardened) {
222
198
  }
223
199
  };
224
200
 
201
+ /**
202
+ * Derives a child HDPrivateKey from the current key using the specified index.
203
+ * Handles both hardened and non-hardened derivation according to BIP32.
204
+ *
205
+ * @param {number} index - The child index to derive
206
+ * @param {boolean} [hardened] - Whether to use hardened derivation
207
+ * @param {boolean} [nonCompliant=false] - If true, uses non-zero-padded private key serialization
208
+ * @returns {HDPrivateKey} The derived child private key
209
+ * @throws {hdErrors.InvalidPath} If the derivation path is invalid
210
+ * @private
211
+ */
225
212
  HDPrivateKey.prototype._deriveWithNumber = function (index, hardened, nonCompliant) {
226
213
  if (!HDPrivateKey.isValidPath(index, hardened)) {
227
214
  throw new hdErrors.InvalidPath(index);
@@ -277,12 +264,20 @@ HDPrivateKey.prototype._deriveWithNumber = function (index, hardened, nonComplia
277
264
  return derived;
278
265
  };
279
266
 
267
+ /**
268
+ * Derives a child HDPrivateKey from a string path.
269
+ * @param {string} path - The derivation path (e.g. "m/44'/0'/0'")
270
+ * @param {boolean} [nonCompliant] - Whether to use non-compliant derivation
271
+ * @returns {HDPrivateKey} The derived private key
272
+ * @throws {hdErrors.InvalidPath} If the path is invalid
273
+ * @private
274
+ */
280
275
  HDPrivateKey.prototype._deriveFromString = function (path, nonCompliant) {
281
276
  if (!HDPrivateKey.isValidPath(path)) {
282
277
  throw new hdErrors.InvalidPath(path);
283
278
  }
284
279
 
285
- var indexes = HDPrivateKey._getDerivationIndexes(path);
280
+ var indexes = Derivation.getDerivationIndexes(path);
286
281
  var derived = indexes.reduce(function (prev, index) {
287
282
  return prev._deriveWithNumber(index, null, nonCompliant);
288
283
  }, this);
@@ -295,7 +290,7 @@ HDPrivateKey.prototype._deriveFromString = function (path, nonCompliant) {
295
290
  * is valid.
296
291
  *
297
292
  * @param {string|Buffer} data - the serialized private key
298
- * @param {string|Network=} network - optional, if present, checks that the
293
+ * @param {string|Network} network - optional, if present, checks that the
299
294
  * network provided matches the network serialized.
300
295
  * @return {boolean}
301
296
  */
@@ -308,7 +303,7 @@ HDPrivateKey.isValidSerialized = function (data, network) {
308
303
  * in base58 with checksum to fail.
309
304
  *
310
305
  * @param {string|Buffer} data - the serialized private key
311
- * @param {string|Network=} network - optional, if present, checks that the
306
+ * @param {string|Network} network - optional, if present, checks that the
312
307
  * network provided matches the network serialized.
313
308
  * @return {errors.InvalidArgument|null}
314
309
  */
@@ -336,8 +331,15 @@ HDPrivateKey.getSerializedError = function (data, network) {
336
331
  return null;
337
332
  };
338
333
 
334
+ /**
335
+ * Validates if the provided data matches the expected network's extended private key version.
336
+ * @param {Buffer} data - The data buffer to validate (must include version bytes).
337
+ * @param {string|Network} networkArg - Network identifier or Network object to validate against.
338
+ * @returns {Error|null} Returns error if validation fails, otherwise null.
339
+ * @private
340
+ */
339
341
  HDPrivateKey._validateNetwork = function (data, networkArg) {
340
- var network = Network.get(networkArg);
342
+ var network = Networks.get(networkArg);
341
343
  if (!network) {
342
344
  return new errors.InvalidNetworkArgument(networkArg);
343
345
  }
@@ -348,24 +350,49 @@ HDPrivateKey._validateNetwork = function (data, networkArg) {
348
350
  return null;
349
351
  };
350
352
 
353
+ /**
354
+ * Creates an HDPrivateKey instance from a string representation.
355
+ * @param {string} arg - The string to convert to an HDPrivateKey
356
+ * @returns {HDPrivateKey} A new HDPrivateKey instance
357
+ * @throws {Error} If the input is not a valid string
358
+ */
351
359
  HDPrivateKey.fromString = function (arg) {
352
360
  $.checkArgument(_.isString(arg), 'No valid string was provided');
353
361
  return new HDPrivateKey(arg);
354
362
  };
355
363
 
364
+ /**
365
+ * Creates an HDPrivateKey instance from a plain object.
366
+ * @param {Object} arg - The object containing HDPrivateKey properties.
367
+ * @throws {Error} Throws if argument is not a valid object.
368
+ * @returns {HDPrivateKey} A new HDPrivateKey instance.
369
+ */
356
370
  HDPrivateKey.fromObject = function (arg) {
357
371
  $.checkArgument(_.isObject(arg), 'No valid argument was provided');
358
372
  return new HDPrivateKey(arg);
359
373
  };
360
374
 
375
+ /**
376
+ * Builds an HDPrivateKey instance from a JSON string.
377
+ * @private
378
+ * @param {string} arg - JSON string to parse and build from.
379
+ * @returns {HDPrivateKey} The constructed HDPrivateKey instance.
380
+ */
361
381
  HDPrivateKey.prototype._buildFromJSON = function (arg) {
362
382
  return this._buildFromObject(JSON.parse(arg));
363
383
  };
364
384
 
385
+ /**
386
+ * Builds an HDPrivateKey from an object by converting its properties to buffers.
387
+ * Handles type conversion for version, depth, parentFingerPrint, childIndex, chainCode, privateKey, and checksum.
388
+ * @private
389
+ * @param {Object} arg - The source object containing key properties
390
+ * @returns {HDPrivateKey} The constructed HDPrivateKey instance
391
+ */
365
392
  HDPrivateKey.prototype._buildFromObject = function (arg) {
366
393
  // TODO: Type validation
367
394
  var buffers = {
368
- version: arg.network ? JSUtil.integerAsBuffer(Network.get(arg.network).xprivkey) : arg.version,
395
+ version: arg.network ? JSUtil.integerAsBuffer(Networks.get(arg.network).xprivkey) : arg.version,
369
396
  depth: _.isNumber(arg.depth) ? Buffer.from([arg.depth & 0xff]) : arg.depth,
370
397
  parentFingerPrint: _.isNumber(arg.parentFingerPrint)
371
398
  ? JSUtil.integerAsBuffer(arg.parentFingerPrint)
@@ -387,6 +414,12 @@ HDPrivateKey.prototype._buildFromObject = function (arg) {
387
414
  return this._buildFromBuffers(buffers);
388
415
  };
389
416
 
417
+ /**
418
+ * Builds an HDPrivateKey instance from a serialized Base58Check encoded string.
419
+ * @private
420
+ * @param {string} arg - The Base58Check encoded extended private key (xprivkey)
421
+ * @returns {HDPrivateKey} The instance built from the decoded buffers
422
+ */
390
423
  HDPrivateKey.prototype._buildFromSerialized = function (arg) {
391
424
  var decoded = Base58Check.decode(arg);
392
425
  var buffers = {
@@ -405,6 +438,12 @@ HDPrivateKey.prototype._buildFromSerialized = function (arg) {
405
438
  return this._buildFromBuffers(buffers);
406
439
  };
407
440
 
441
+ /**
442
+ * Generates a new HDPrivateKey instance with a randomly generated seed.
443
+ * @param {Network} network - The network to use for the HDPrivateKey.
444
+ * @returns {HDPrivateKey} A new HDPrivateKey instance with random seed.
445
+ * @private
446
+ */
408
447
  HDPrivateKey.prototype._generateRandomly = function (network) {
409
448
  return HDPrivateKey.fromSeed(Random.getRandomBuffer(64), network);
410
449
  };
@@ -413,8 +452,9 @@ HDPrivateKey.prototype._generateRandomly = function (network) {
413
452
  * Generate a private key from a seed, as described in BIP32
414
453
  *
415
454
  * @param {string|Buffer} hexa
416
- * @param {*} network
455
+ * @param {Network} [network]
417
456
  * @return HDPrivateKey
457
+ * @static
418
458
  */
419
459
  HDPrivateKey.fromSeed = function (hexa, network) {
420
460
  if (JSUtil.isHexaString(hexa)) {
@@ -432,7 +472,7 @@ HDPrivateKey.fromSeed = function (hexa, network) {
432
472
  var hash = Hash.sha512hmac(hexa, Buffer.from('Bitcoin seed'));
433
473
 
434
474
  return new HDPrivateKey({
435
- network: Network.get(network) || Network.defaultNetwork,
475
+ network: Networks.get(network) || Networks.defaultNetwork,
436
476
  depth: 0,
437
477
  parentFingerPrint: 0,
438
478
  childIndex: 0,
@@ -441,13 +481,44 @@ HDPrivateKey.fromSeed = function (hexa, network) {
441
481
  });
442
482
  };
443
483
 
484
+ /**
485
+ * Calculates and caches the HD public key from the private key.
486
+ * This is an internal method that lazily computes the public key only when needed.
487
+ * The result is stored in `this._hdPublicKey` to avoid repeated calculations.
488
+ * @private
489
+ */
444
490
  HDPrivateKey.prototype._calcHDPublicKey = function () {
445
491
  if (!this._hdPublicKey) {
446
- var HDPublicKey = require('./hdpublickey');
447
- this._hdPublicKey = new HDPublicKey(this);
492
+ var args = _.clone(this._buffers);
493
+ var point = Point.getG().mul(BN.fromBuffer(args.privateKey));
494
+ args.publicKey = Point.pointToCompressed(point);
495
+ args.version = JSUtil.integerAsBuffer(Networks.get(args.version.readUInt32BE(0)).xpubkey);
496
+ args.privateKey = undefined;
497
+ args.checksum = undefined;
498
+ args.xprivkey = undefined;
499
+ this._hdPublicKey = new HDPublicKey(args);
448
500
  }
449
501
  };
450
502
 
503
+ /**
504
+ * Converts the HDPrivateKey instance to its corresponding HDPublicKey.
505
+ * @returns {HDPublicKey} The derived HD public key.
506
+ */
507
+ HDPrivateKey.prototype.toHDPublicKey = function () {
508
+ this._calcHDPublicKey();
509
+ return this._hdPublicKey;
510
+ };
511
+
512
+
513
+ /**
514
+ * Returns the private key associated with this HD private key.
515
+ * @returns {PrivateKey} The private key instance.
516
+ */
517
+ HDPrivateKey.prototype.toPrivateKey = function () {
518
+ return this.privateKey;
519
+ };
520
+
521
+
451
522
  /**
452
523
  * Receives a object with buffers in all the properties and populates the
453
524
  * internal structure
@@ -463,6 +534,7 @@ HDPrivateKey.prototype._calcHDPublicKey = function () {
463
534
  * @param {string=} arg.xprivkey - if set, don't recalculate the base58
464
535
  * representation
465
536
  * @return {HDPrivateKey} this
537
+ * @private
466
538
  */
467
539
  HDPrivateKey.prototype._buildFromBuffers = function (arg) {
468
540
  HDPrivateKey._validateBufferArguments(arg);
@@ -489,7 +561,7 @@ HDPrivateKey.prototype._buildFromBuffers = function (arg) {
489
561
  }
490
562
  }
491
563
 
492
- var network = Network.get(arg.version.readUInt32BE(0));
564
+ var network = Networks.get(arg.version.readUInt32BE(0));
493
565
  var xprivkey;
494
566
  xprivkey = Base58Check.encode(Buffer.concat(sequence));
495
567
  arg.xprivkey = Buffer.from(xprivkey);
@@ -509,26 +581,22 @@ HDPrivateKey.prototype._buildFromBuffers = function (arg) {
509
581
  });
510
582
 
511
583
  this._hdPublicKey = null;
512
-
513
- Object.defineProperty(this, 'hdPublicKey', {
514
- configurable: false,
515
- enumerable: true,
516
- get: function () {
517
- this._calcHDPublicKey();
518
- return this._hdPublicKey;
519
- },
520
- });
521
- Object.defineProperty(this, 'xpubkey', {
522
- configurable: false,
523
- enumerable: true,
524
- get: function () {
525
- this._calcHDPublicKey();
526
- return this._hdPublicKey.xpubkey;
527
- },
528
- });
529
584
  return this;
530
585
  };
531
586
 
587
+ /**
588
+ * Validates buffer arguments for HDPrivateKey.
589
+ * Checks that each required buffer field exists and has the correct size.
590
+ * @private
591
+ * @param {Object} arg - Object containing buffer fields to validate
592
+ * @param {Buffer} arg.version - Version buffer
593
+ * @param {Buffer} arg.depth - Depth buffer
594
+ * @param {Buffer} arg.parentFingerPrint - Parent fingerprint buffer
595
+ * @param {Buffer} arg.childIndex - Child index buffer
596
+ * @param {Buffer} arg.chainCode - Chain code buffer
597
+ * @param {Buffer} arg.privateKey - Private key buffer
598
+ * @param {Buffer} [arg.checksum] - Optional checksum buffer
599
+ */
532
600
  HDPrivateKey._validateBufferArguments = function (arg) {
533
601
  var checkBuffer = function (name, size) {
534
602
  var buff = arg[name];
@@ -550,10 +618,9 @@ HDPrivateKey._validateBufferArguments = function (arg) {
550
618
  };
551
619
 
552
620
  /**
553
- * Returns the string representation of this private key (a string starting
554
- * with "xprv..."
555
- *
556
- * @return string
621
+ * Returns the extended private key string representation of this HDPrivateKey.
622
+ * (a string starting with "xprv...")
623
+ * @returns {string} The extended private key in base58 string format.
557
624
  */
558
625
  HDPrivateKey.prototype.toString = function () {
559
626
  return this.xprivkey;
@@ -570,25 +637,23 @@ HDPrivateKey.prototype.inspect = function () {
570
637
  /**
571
638
  * Returns a plain object with a representation of this private key.
572
639
  *
573
- * Fields include:<ul>
574
- * <li> network: either 'livenet' or 'testnet'
575
- * <li> depth: a number ranging from 0 to 255
576
- * <li> fingerPrint: a number ranging from 0 to 2^32-1, taken from the hash of the
577
- * <li> associated public key
578
- * <li> parentFingerPrint: a number ranging from 0 to 2^32-1, taken from the hash
579
- * <li> of this parent's associated public key or zero.
580
- * <li> childIndex: the index from which this child was derived (or zero)
581
- * <li> chainCode: an hexa string representing a number used in the derivation
582
- * <li> privateKey: the private key associated, in hexa representation
583
- * <li> xprivkey: the representation of this extended private key in checksum
584
- * <li> base58 format
585
- * <li> checksum: the base58 checksum of xprivkey
640
+ * Fields include:
641
+ * <ul>
642
+ * <li> network: either 'livenet' or 'testnet' </li>
643
+ * <li> depth: a number ranging from 0 to 255 </li>
644
+ * <li> fingerPrint: a number ranging from 0 to 2^32-1, taken from the hash of the associated public key </li>
645
+ * <li> parentFingerPrint: a number ranging from 0 to 2^32-1, taken from the hash of this parent's associated public key or zero. </li>
646
+ * <li> childIndex: the index from which this child was derived (or zero) </li>
647
+ * <li> chainCode: an hexa string representing a number used in the derivation </li>
648
+ * <li> privateKey: the private key associated, in hexa representation </li>
649
+ * <li> xprivkey: the representation of this extended private key in checksum base58 format </li>
650
+ * <li> checksum: the base58 checksum of xprivkey </li>
586
651
  * </ul>
587
652
  * @return {Object}
588
653
  */
589
654
  HDPrivateKey.prototype.toObject = HDPrivateKey.prototype.toJSON = function toObject() {
590
655
  return {
591
- network: Network.get(this._buffers.version.readUInt32BE(0), 'xprivkey').name,
656
+ network: Networks.get(this._buffers.version.readUInt32BE(0), 'xprivkey').name,
592
657
  depth: this._buffers.depth[0],
593
658
  fingerPrint: this.fingerPrint.readUInt32BE(0),
594
659
  parentFingerPrint: this._buffers.parentFingerPrint.readUInt32BE(0),
@@ -638,13 +703,40 @@ HDPrivateKey.prototype.toHex = function () {
638
703
  return this.toBuffer().toString('hex');
639
704
  };
640
705
 
706
+ /**
707
+ * Sets the default depth for hierarchical deterministic (HD) private keys.
708
+ * @type {number}
709
+ * @default 0
710
+ */
641
711
  HDPrivateKey.DefaultDepth = 0;
712
+ /**
713
+ * Sets the default fingerprint value for HDPrivateKey instances.
714
+ * @type {number}
715
+ * @default 0
716
+ */
642
717
  HDPrivateKey.DefaultFingerprint = 0;
718
+ /**
719
+ * Default child index used for deriving child keys in the HD (Hierarchical Deterministic) key derivation path.
720
+ * @type {number}
721
+ * @default 0
722
+ */
643
723
  HDPrivateKey.DefaultChildIndex = 0;
644
- HDPrivateKey.Hardened = 0x80000000;
724
+ /**
725
+ * Sets the hardened derivation flag for HDPrivateKey (inherited from Derivation.Hardened).
726
+ * @type {number}
727
+ */
728
+ HDPrivateKey.Hardened = Derivation.Hardened;
729
+ /**
730
+ * Maximum index value for HD private keys (2 * Hardened value).
731
+ * @type {number}
732
+ */
645
733
  HDPrivateKey.MaxIndex = 2 * HDPrivateKey.Hardened;
646
734
 
647
- HDPrivateKey.RootElementAlias = ['m', 'M', "m'", "M'"];
735
+ /**
736
+ * Sets the root element alias for HDPrivateKey to match Derivation's root element alias.
737
+ * @type {string}
738
+ */
739
+ HDPrivateKey.RootElementAlias = Derivation.RootElementAlias;
648
740
 
649
741
  HDPrivateKey.VersionSize = 4;
650
742
  HDPrivateKey.DepthSize = 1;