@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,23 +1,23 @@
1
1
  'use strict';
2
2
 
3
- var _ = require('./util/_');
4
- var $ = require('./util/preconditions');
5
-
6
- var BN = require('./crypto/bn');
7
- var Base58 = require('./encoding/base58');
8
- var Base58Check = require('./encoding/base58check');
9
- var Hash = require('./crypto/hash');
10
- var HDPrivateKey = require('./hdprivatekey');
11
- var Network = require('./networks');
12
- var Point = require('./crypto/point');
13
- var PublicKey = require('./publickey');
14
-
15
- var opcatErrors = require('./errors');
3
+ var _ = require('./util/_.cjs');
4
+ var $ = require('./util/preconditions.cjs');
5
+
6
+ var BN = require('./crypto/bn.cjs');
7
+ var Base58 = require('./encoding/base58.cjs');
8
+ var Base58Check = require('./encoding/base58check.cjs');
9
+ var Hash = require('./crypto/hash.cjs');
10
+ var Networks = require('./networks.cjs');
11
+ var Point = require('./crypto/point.cjs');
12
+ var PublicKey = require('./publickey.cjs');
13
+ var Derivation = require('./util/derivation.cjs');
14
+
15
+ var opcatErrors = require('./errors/index.cjs');
16
16
  var errors = opcatErrors;
17
17
  var hdErrors = opcatErrors.HDPublicKey;
18
18
  var assert = require('assert');
19
19
 
20
- var JSUtil = require('./util/js');
20
+ var JSUtil = require('./util/js.cjs');
21
21
 
22
22
  /**
23
23
  * The representation of an hierarchically derived public key.
@@ -42,18 +42,11 @@ function HDPublicKey(arg) {
42
42
  } else if (Buffer.isBuffer(arg) && !HDPublicKey.getSerializedError(arg.toString())) {
43
43
  return this._buildFromSerialized(arg.toString());
44
44
  } else {
45
- if (error instanceof hdErrors.ArgumentIsPrivateExtended) {
46
- return new HDPrivateKey(arg).hdPublicKey;
47
- }
48
45
  throw error;
49
46
  }
50
47
  } else {
51
48
  if (_.isObject(arg)) {
52
- if (arg instanceof HDPrivateKey) {
53
- return this._buildFromPrivate(arg);
54
- } else {
55
- return this._buildFromObject(arg);
56
- }
49
+ return this._buildFromObject(arg);
57
50
  } else {
58
51
  throw new hdErrors.UnrecognizedArgument(arg);
59
52
  }
@@ -63,19 +56,26 @@ function HDPublicKey(arg) {
63
56
  }
64
57
  }
65
58
 
59
+ /**
60
+ * Converts an HDPrivateKey to an HDPublicKey.
61
+ * @param {HDPrivateKey} hdPrivateKey - The HD private key to convert.
62
+ * @returns {HDPublicKey} The corresponding HD public key.
63
+ */
66
64
  HDPublicKey.fromHDPrivateKey = function (hdPrivateKey) {
67
- return new HDPublicKey(hdPrivateKey);
65
+ return hdPrivateKey.toHDPublicKey()
68
66
  };
69
67
 
68
+
70
69
  /**
71
- * Verifies that a given path is valid.
72
- *
73
- * @param {string|number} arg
74
- * @return {boolean}
70
+ * Checks if a given argument is a valid HD public key derivation path.
71
+ * @param {string|number} arg - The path to validate (either as string like "m/0/1" or as a single index number).
72
+ * @returns {boolean} True if the path is valid, false otherwise.
73
+ * @description Validates both string paths (e.g., "m/0/1") and individual derivation indexes.
74
+ * String paths must contain valid indexes separated by '/', and each index must be a non-negative number less than HDPublicKey.Hardened.
75
75
  */
76
76
  HDPublicKey.isValidPath = function (arg) {
77
77
  if (_.isString(arg)) {
78
- var indexes = HDPrivateKey._getDerivationIndexes(arg);
78
+ var indexes = Derivation.getDerivationIndexes(arg);
79
79
  return indexes !== null && _.every(indexes, HDPublicKey.isValidPath);
80
80
  }
81
81
 
@@ -86,36 +86,6 @@ HDPublicKey.isValidPath = function (arg) {
86
86
  return false;
87
87
  };
88
88
 
89
- /**
90
- * WARNING: This method is deprecated. Use deriveChild instead.
91
- *
92
- *
93
- * Get a derivated child based on a string or number.
94
- *
95
- * If the first argument is a string, it's parsed as the full path of
96
- * derivation. Valid values for this argument include "m" (which returns the
97
- * same public key), "m/0/1/40/2/1000".
98
- *
99
- * Note that hardened keys can't be derived from a public extended key.
100
- *
101
- * If the first argument is a number, the child with that index will be
102
- * derived. See the example usage for clarification.
103
- *
104
- * @example
105
- * ```javascript
106
- * var parent = new HDPublicKey('xpub...');
107
- * var child_0_1_2 = parent.derive(0).derive(1).derive(2);
108
- * var copy_of_child_0_1_2 = parent.derive("m/0/1/2");
109
- * assert(child_0_1_2.xprivkey === copy_of_child_0_1_2);
110
- * ```
111
- *
112
- * @param {string|number} arg
113
- */
114
- HDPublicKey.prototype.derive = function () {
115
- throw new Error(
116
- 'derive has been deprecated. use deriveChild or, for the old way, deriveNonCompliantChild.',
117
- );
118
- };
119
89
 
120
90
  /**
121
91
  * WARNING: This method will not be officially supported until v1.0.0.
@@ -140,7 +110,9 @@ HDPublicKey.prototype.derive = function () {
140
110
  * assert(child_0_1_2.xprivkey === copy_of_child_0_1_2);
141
111
  * ```
142
112
  *
143
- * @param {string|number} arg
113
+ * @param {string|number} arg - The index or path to derive
114
+ * @param {boolean} [hardened=false] - Whether to use hardened derivation
115
+ * @returns {HDPublicKey} The derived child public key
144
116
  */
145
117
  HDPublicKey.prototype.deriveChild = function (arg, hardened) {
146
118
  if (_.isNumber(arg)) {
@@ -152,6 +124,15 @@ HDPublicKey.prototype.deriveChild = function (arg, hardened) {
152
124
  }
153
125
  };
154
126
 
127
+ /**
128
+ * Derives a child public key from the current HD public key using the specified index.
129
+ * @param {number} index - The child index to derive (must be less than HDPublicKey.Hardened)
130
+ * @param {boolean} hardened - Whether to derive a hardened key (not supported for public keys)
131
+ * @returns {HDPublicKey} The derived child public key
132
+ * @throws {hdErrors.InvalidIndexCantDeriveHardened} If attempting to derive a hardened key
133
+ * @throws {hdErrors.InvalidPath} If index is negative
134
+ * @private
135
+ */
155
136
  HDPublicKey.prototype._deriveWithNumber = function (index, hardened) {
156
137
  if (index >= HDPublicKey.Hardened || hardened) {
157
138
  throw new hdErrors.InvalidIndexCantDeriveHardened();
@@ -185,6 +166,14 @@ HDPublicKey.prototype._deriveWithNumber = function (index, hardened) {
185
166
  return derived;
186
167
  };
187
168
 
169
+ /**
170
+ * Derives a child public key from the current HD public key using the specified path.
171
+ * @param {string} path - The derivation path (must not contain hardened derivation markers)
172
+ * @returns {HDPublicKey} The derived public key
173
+ * @throws {hdErrors.InvalidIndexCantDeriveHardened} If path contains hardened derivation markers
174
+ * @throws {hdErrors.InvalidPath} If path is not a valid derivation path
175
+ * @private
176
+ */
188
177
  HDPublicKey.prototype._deriveFromString = function (path) {
189
178
  if (_.includes(path, "'")) {
190
179
  throw new hdErrors.InvalidIndexCantDeriveHardened();
@@ -192,7 +181,7 @@ HDPublicKey.prototype._deriveFromString = function (path) {
192
181
  throw new hdErrors.InvalidPath(path);
193
182
  }
194
183
 
195
- var indexes = HDPrivateKey._getDerivationIndexes(path);
184
+ var indexes = Derivation.getDerivationIndexes(path);
196
185
  var derived = indexes.reduce(function (prev, index) {
197
186
  return prev._deriveWithNumber(index);
198
187
  }, this);
@@ -205,7 +194,7 @@ HDPublicKey.prototype._deriveFromString = function (path) {
205
194
  * is valid.
206
195
  *
207
196
  * @param {string|Buffer} data - the serialized public key
208
- * @param {string|Network=} network - optional, if present, checks that the
197
+ * @param {string|Network} [network] - optional, if present, checks that the
209
198
  * network provided matches the network serialized.
210
199
  * @return {boolean}
211
200
  */
@@ -218,9 +207,9 @@ HDPublicKey.isValidSerialized = function (data, network) {
218
207
  * in base58 with checksum to fail.
219
208
  *
220
209
  * @param {string|Buffer} data - the serialized public key
221
- * @param {string|Network=} network - optional, if present, checks that the
210
+ * @param {string|Network} [network] - optional, if present, checks that the
222
211
  * network provided matches the network serialized.
223
- * @return {errors|null}
212
+ * @return {Error|null}
224
213
  */
225
214
  HDPublicKey.getSerializedError = function (data, network) {
226
215
  if (!(_.isString(data) || Buffer.isBuffer(data))) {
@@ -244,14 +233,21 @@ HDPublicKey.getSerializedError = function (data, network) {
244
233
  }
245
234
  }
246
235
  var version = data.readUInt32BE(0);
247
- if (version === Network.livenet.xprivkey || version === Network.testnet.xprivkey) {
248
- return new hdErrors.ArgumentIsPrivateExtended();
236
+ if (version === Networks.livenet.xprivkey || version === Networks.testnet.xprivkey) {
237
+ throw new hdErrors.ArgumentIsPrivateExtended();
249
238
  }
250
239
  return null;
251
240
  };
252
241
 
242
+ /**
243
+ * Validates if the provided data matches the expected network version.
244
+ * @param {Buffer} data - The data containing the version to validate.
245
+ * @param {string|Network} networkArg - The network or network identifier to validate against.
246
+ * @returns {InvalidNetworkArgument|InvalidNetwork|null} Returns an error if validation fails, otherwise null.
247
+ * @private
248
+ */
253
249
  HDPublicKey._validateNetwork = function (data, networkArg) {
254
- var network = Network.get(networkArg);
250
+ var network = Networks.get(networkArg);
255
251
  if (!network) {
256
252
  return new errors.InvalidNetworkArgument(networkArg);
257
253
  }
@@ -262,21 +258,17 @@ HDPublicKey._validateNetwork = function (data, networkArg) {
262
258
  return null;
263
259
  };
264
260
 
265
- HDPublicKey.prototype._buildFromPrivate = function (arg) {
266
- var args = _.clone(arg._buffers);
267
- var point = Point.getG().mul(BN.fromBuffer(args.privateKey));
268
- args.publicKey = Point.pointToCompressed(point);
269
- args.version = JSUtil.integerAsBuffer(Network.get(args.version.readUInt32BE(0)).xpubkey);
270
- args.privateKey = undefined;
271
- args.checksum = undefined;
272
- args.xprivkey = undefined;
273
- return this._buildFromBuffers(args);
274
- };
275
-
261
+ /**
262
+ * Builds an HDPublicKey instance from an object containing key components.
263
+ * Handles type conversion for various input formats (numbers, strings, buffers).
264
+ * @param {Object} arg - Object containing key components (version, depth, parentFingerPrint, etc.)
265
+ * @returns {HDPublicKey} The constructed public key instance
266
+ * @private
267
+ */
276
268
  HDPublicKey.prototype._buildFromObject = function (arg) {
277
- // TODO: Type validation
269
+ // TODO: Type validation
278
270
  var buffers = {
279
- version: arg.network ? JSUtil.integerAsBuffer(Network.get(arg.network).xpubkey) : arg.version,
271
+ version: arg.network ? JSUtil.integerAsBuffer(Networks.get(arg.network).xpubkey) : arg.version,
280
272
  depth: _.isNumber(arg.depth) ? Buffer.from([arg.depth & 0xff]) : arg.depth,
281
273
  parentFingerPrint: _.isNumber(arg.parentFingerPrint)
282
274
  ? JSUtil.integerAsBuffer(arg.parentFingerPrint)
@@ -295,6 +287,15 @@ HDPublicKey.prototype._buildFromObject = function (arg) {
295
287
  return this._buildFromBuffers(buffers);
296
288
  };
297
289
 
290
+ /**
291
+ * Builds an HDPublicKey instance from a serialized Base58Check encoded string.
292
+ * @private
293
+ * @param {string} arg - The Base58Check encoded extended public key (xpub)
294
+ * @returns {HDPublicKey} The constructed HDPublicKey instance
295
+ * @description
296
+ * Decodes the input string into buffers for version, depth, parent fingerprint,
297
+ * child index, chain code, public key, and checksum, then builds the key from buffers.
298
+ */
298
299
  HDPublicKey.prototype._buildFromSerialized = function (arg) {
299
300
  var decoded = Base58Check.decode(arg);
300
301
  var buffers = {
@@ -328,6 +329,7 @@ HDPublicKey.prototype._buildFromSerialized = function (arg) {
328
329
  * @param {string=} arg.xpubkey - if set, don't recalculate the base58
329
330
  * representation
330
331
  * @return {HDPublicKey} this
332
+ * @private
331
333
  */
332
334
  HDPublicKey.prototype._buildFromBuffers = function (arg) {
333
335
  HDPublicKey._validateBufferArguments(arg);
@@ -353,7 +355,7 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) {
353
355
  throw new errors.InvalidB58Checksum(concat, checksum);
354
356
  }
355
357
  }
356
- var network = Network.get(arg.version.readUInt32BE(0));
358
+ var network = Networks.get(arg.version.readUInt32BE(0));
357
359
 
358
360
  var xpubkey;
359
361
  xpubkey = Base58Check.encode(Buffer.concat(sequence));
@@ -374,6 +376,19 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) {
374
376
  return this;
375
377
  };
376
378
 
379
+ /**
380
+ * Validates buffer arguments for HDPublicKey.
381
+ * @private
382
+ * @param {Object} arg - The argument object containing buffer fields to validate
383
+ * @param {Buffer} arg.version - Version buffer (must be HDPublicKey.VersionSize bytes)
384
+ * @param {Buffer} arg.depth - Depth buffer (must be HDPublicKey.DepthSize bytes)
385
+ * @param {Buffer} arg.parentFingerPrint - Parent fingerprint buffer (must be HDPublicKey.ParentFingerPrintSize bytes)
386
+ * @param {Buffer} arg.childIndex - Child index buffer (must be HDPublicKey.ChildIndexSize bytes)
387
+ * @param {Buffer} arg.chainCode - Chain code buffer (must be HDPublicKey.ChainCodeSize bytes)
388
+ * @param {Buffer} arg.publicKey - Public key buffer (must be HDPublicKey.PublicKeySize bytes)
389
+ * @param {Buffer} [arg.checksum] - Optional checksum buffer (must be HDPublicKey.CheckSumSize bytes if provided)
390
+ * @throws {Error} If any buffer is invalid or has incorrect size
391
+ */
377
392
  HDPublicKey._validateBufferArguments = function (arg) {
378
393
  var checkBuffer = function (name, size) {
379
394
  var buff = arg[name];
@@ -394,11 +409,23 @@ HDPublicKey._validateBufferArguments = function (arg) {
394
409
  }
395
410
  };
396
411
 
412
+ /**
413
+ * Creates an HDPublicKey instance from a string representation.
414
+ * @param {string} arg - The string to convert to an HDPublicKey.
415
+ * @returns {HDPublicKey} A new HDPublicKey instance.
416
+ * @throws {Error} Throws if the input is not a valid string.
417
+ */
397
418
  HDPublicKey.fromString = function (arg) {
398
419
  $.checkArgument(_.isString(arg), 'No valid string was provided');
399
420
  return new HDPublicKey(arg);
400
421
  };
401
422
 
423
+ /**
424
+ * Creates an HDPublicKey instance from an object.
425
+ * @param {Object} arg - The object containing public key data
426
+ * @returns {HDPublicKey} A new HDPublicKey instance
427
+ * @throws {Error} Will throw if no valid object argument is provided
428
+ */
402
429
  HDPublicKey.fromObject = function (arg) {
403
430
  $.checkArgument(_.isObject(arg), 'No valid argument was provided');
404
431
  return new HDPublicKey(arg);
@@ -423,23 +450,23 @@ HDPublicKey.prototype.inspect = function () {
423
450
  /**
424
451
  * Returns a plain JavaScript object with information to reconstruct a key.
425
452
  *
426
- * Fields are: <ul>
427
- * <li> network: 'livenet' or 'testnet'
428
- * <li> depth: a number from 0 to 255, the depth to the master extended key
429
- * <li> fingerPrint: a number of 32 bits taken from the hash of the public key
430
- * <li> fingerPrint: a number of 32 bits taken from the hash of this key's
431
- * <li> parent's public key
432
- * <li> childIndex: index with which this key was derived
433
- * <li> chainCode: string in hexa encoding used for derivation
434
- * <li> publicKey: string, hexa encoded, in compressed key format
435
- * <li> checksum: this._buffers.checksum.readUInt32BE(0),
436
- * <li> xpubkey: the string with the base58 representation of this extended key
437
- * <li> checksum: the base58 checksum of xpubkey
453
+ * Fields are:
454
+ * <ul>
455
+ * <li> network: 'livenet' or 'testnet' </li>
456
+ * <li> depth: a number from 0 to 255, the depth to the master extended key </li>
457
+ * <li> fingerPrint: a number of 32 bits taken from the hash of the public key </li>
458
+ * <li> fingerPrint: a number of 32 bits taken from the hash of this key's parent's public key </li>
459
+ * <li> childIndex: index with which this key was derived </li>
460
+ * <li> chainCode: string in hexa encoding used for derivation </li>
461
+ * <li> publicKey: string, hexa encoded, in compressed key format </li>
462
+ * <li> checksum: this._buffers.checksum.readUInt32BE(0) </li>
463
+ * <li> xpubkey: the string with the base58 representation of this extended key </li>
464
+ * <li> checksum: the base58 checksum of xpubkey </li>
438
465
  * </ul>
439
466
  */
440
467
  HDPublicKey.prototype.toObject = HDPublicKey.prototype.toJSON = function toObject() {
441
468
  return {
442
- network: Network.get(this._buffers.version.readUInt32BE(0)).name,
469
+ network: Networks.get(this._buffers.version.readUInt32BE(0)).name,
443
470
  depth: this._buffers.depth[0],
444
471
  fingerPrint: this.fingerPrint.readUInt32BE(0),
445
472
  parentFingerPrint: this._buffers.parentFingerPrint.readUInt32BE(0),
package/cjs/index.cjs ADDED
@@ -0,0 +1,94 @@
1
+ 'use strict';
2
+ var util = require('./util/index.cjs');
3
+ var encoding = require('./encoding/index.cjs');
4
+ var crypto = require('./crypto/index.cjs');
5
+ var errors = require('./errors/index.cjs');
6
+
7
+ var Address = require('./address.cjs');
8
+ var Block = require('./block/index.cjs');
9
+ var MerkleBlock = require('./block/merkleblock.cjs');
10
+ var BlockHeader = require('./block/blockheader.cjs');
11
+ var HDPrivateKey = require('./hdprivatekey.cjs');
12
+ var HDPublicKey = require('./hdpublickey.cjs');
13
+ var Networks = require('./networks.cjs');
14
+ var Network = require('./network.cjs');
15
+ var Opcode = require('./opcode.cjs');
16
+ var PrivateKey = require('./privatekey.cjs');
17
+ var PublicKey = require('./publickey.cjs');
18
+ var Script = require('./script/index.cjs');
19
+ var Transaction = require('./transaction/index.cjs');
20
+ var Output = require('./transaction/output.cjs');
21
+ var Input = require('./transaction/input/index.cjs');
22
+ var Sighash = require('./transaction/sighash.cjs');
23
+ var TransactionSignature = require('./transaction/signature.cjs');
24
+ var HashCache = require('./hash-cache.cjs');
25
+ var Message = require('./message/message.cjs');
26
+ var Mnemonic = require('./mnemonic/index.cjs');
27
+ var Interpreter = require('./interpreter/index.cjs');
28
+
29
+
30
+ module.exports = {
31
+ Address,
32
+ Block,
33
+ MerkleBlock,
34
+ BlockHeader,
35
+ HDPrivateKey,
36
+ HDPublicKey,
37
+ Sighash,
38
+ TransactionSignature,
39
+ Output,
40
+ Input,
41
+ PublicKeyInput: Input.PublicKey,
42
+ PublicKeyHashInput: Input.PublicKeyHash,
43
+ MultiSigInput: Input.MultiSig,
44
+ Networks,
45
+ Network,
46
+ Opcode,
47
+ PrivateKey,
48
+ PublicKey,
49
+ Script,
50
+ Interpreter,
51
+ Transaction,
52
+ HashCache,
53
+ Message,
54
+ Mnemonic,
55
+ errors,
56
+ util,
57
+ encoding,
58
+ crypto,
59
+ }
60
+
61
+
62
+
63
+
64
+ module.exports.Address = Address;
65
+ module.exports.Block = Block;
66
+ module.exports.MerkleBlock = MerkleBlock;
67
+ module.exports.BlockHeader = BlockHeader;
68
+ module.exports.HDPrivateKey = HDPrivateKey;
69
+ module.exports.HDPublicKey = HDPublicKey;
70
+ module.exports.Networks = Networks;
71
+ module.exports.Network = Network;
72
+ module.exports.Opcode = Opcode;
73
+ module.exports.PrivateKey = PrivateKey;
74
+ module.exports.PublicKey = PublicKey;
75
+ module.exports.Script = Script;
76
+ module.exports.Interpreter = Interpreter;
77
+ module.exports.Transaction = Transaction;
78
+ module.exports.Input = Input;
79
+ module.exports.PublicKeyInput = Input.PublicKey;
80
+ module.exports.PublicKeyHashInput = Input.PublicKeyHash;
81
+ module.exports.MultiSigInput = Input.MultiSig;
82
+ module.exports.Sighash = Sighash;
83
+ module.exports.TransactionSignature = TransactionSignature;
84
+ module.exports.Output = Output
85
+ module.exports.HashCache = HashCache;
86
+ module.exports.Message = Message;
87
+ module.exports.Mnemonic = Mnemonic;
88
+ module.exports.errors = errors;
89
+ module.exports.util = util;
90
+ module.exports.encoding = encoding;
91
+ module.exports.crypto = crypto;
92
+
93
+
94
+
@@ -0,0 +1,2 @@
1
+ var Interpreter = require('./interpreter.cjs');
2
+ module.exports = Interpreter