@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
@@ -0,0 +1,549 @@
1
+ 'use strict';
2
+
3
+ import _ from './util/_.js';
4
+ import $ from './util/preconditions.js';
5
+ import BN from './crypto/bn.js';
6
+ import Base58 from './encoding/base58.js';
7
+ import Base58Check from './encoding/base58check.js';
8
+ import Hash from './crypto/hash.js';
9
+ import Networks from './networks.js';
10
+ import Point from './crypto/point.js';
11
+ import PublicKey from './publickey.js';
12
+ import Derivation from './util/derivation.js';
13
+ import opcatErrors from './errors/index.js';
14
+ var errors = opcatErrors;
15
+ var hdErrors = opcatErrors.HDPublicKey;
16
+ import assert from 'assert';
17
+ import JSUtil from './util/js.js';
18
+
19
+ /**
20
+ * The representation of an hierarchically derived public key.
21
+ *
22
+ * See https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
23
+ *
24
+ * @constructor
25
+ * @param {Object|string|Buffer} arg
26
+ */
27
+ function HDPublicKey(arg) {
28
+ if (arg instanceof HDPublicKey) {
29
+ return arg;
30
+ }
31
+ if (!(this instanceof HDPublicKey)) {
32
+ return new HDPublicKey(arg);
33
+ }
34
+ if (arg) {
35
+ if (_.isString(arg) || Buffer.isBuffer(arg)) {
36
+ var error = HDPublicKey.getSerializedError(arg);
37
+ if (!error) {
38
+ return this._buildFromSerialized(arg);
39
+ } else if (Buffer.isBuffer(arg) && !HDPublicKey.getSerializedError(arg.toString())) {
40
+ return this._buildFromSerialized(arg.toString());
41
+ } else {
42
+ throw error;
43
+ }
44
+ } else {
45
+ if (_.isObject(arg)) {
46
+ return this._buildFromObject(arg);
47
+ } else {
48
+ throw new hdErrors.UnrecognizedArgument(arg);
49
+ }
50
+ }
51
+ } else {
52
+ throw new hdErrors.MustSupplyArgument();
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Converts an HDPrivateKey to an HDPublicKey.
58
+ * @param {HDPrivateKey} hdPrivateKey - The HD private key to convert.
59
+ * @returns {HDPublicKey} The corresponding HD public key.
60
+ */
61
+ HDPublicKey.fromHDPrivateKey = function (hdPrivateKey) {
62
+ return hdPrivateKey.toHDPublicKey()
63
+ };
64
+
65
+
66
+ /**
67
+ * Checks if a given argument is a valid HD public key derivation path.
68
+ * @param {string|number} arg - The path to validate (either as string like "m/0/1" or as a single index number).
69
+ * @returns {boolean} True if the path is valid, false otherwise.
70
+ * @description Validates both string paths (e.g., "m/0/1") and individual derivation indexes.
71
+ * String paths must contain valid indexes separated by '/', and each index must be a non-negative number less than HDPublicKey.Hardened.
72
+ */
73
+ HDPublicKey.isValidPath = function (arg) {
74
+ if (_.isString(arg)) {
75
+ var indexes = Derivation.getDerivationIndexes(arg);
76
+ return indexes !== null && _.every(indexes, HDPublicKey.isValidPath);
77
+ }
78
+
79
+ if (_.isNumber(arg)) {
80
+ return arg >= 0 && arg < HDPublicKey.Hardened;
81
+ }
82
+
83
+ return false;
84
+ };
85
+
86
+
87
+ /**
88
+ * WARNING: This method will not be officially supported until v1.0.0.
89
+ *
90
+ *
91
+ * Get a derivated child based on a string or number.
92
+ *
93
+ * If the first argument is a string, it's parsed as the full path of
94
+ * derivation. Valid values for this argument include "m" (which returns the
95
+ * same public key), "m/0/1/40/2/1000".
96
+ *
97
+ * Note that hardened keys can't be derived from a public extended key.
98
+ *
99
+ * If the first argument is a number, the child with that index will be
100
+ * derived. See the example usage for clarification.
101
+ *
102
+ * @example
103
+ * ```javascript
104
+ * var parent = new HDPublicKey('xpub...');
105
+ * var child_0_1_2 = parent.deriveChild(0).deriveChild(1).deriveChild(2);
106
+ * var copy_of_child_0_1_2 = parent.deriveChild("m/0/1/2");
107
+ * assert(child_0_1_2.xprivkey === copy_of_child_0_1_2);
108
+ * ```
109
+ *
110
+ * @param {string|number} arg - The index or path to derive
111
+ * @param {boolean} [hardened=false] - Whether to use hardened derivation
112
+ * @returns {HDPublicKey} The derived child public key
113
+ */
114
+ HDPublicKey.prototype.deriveChild = function (arg, hardened) {
115
+ if (_.isNumber(arg)) {
116
+ return this._deriveWithNumber(arg, hardened);
117
+ } else if (_.isString(arg)) {
118
+ return this._deriveFromString(arg);
119
+ } else {
120
+ throw new hdErrors.InvalidDerivationArgument(arg);
121
+ }
122
+ };
123
+
124
+ /**
125
+ * Derives a child public key from the current HD public key using the specified index.
126
+ * @param {number} index - The child index to derive (must be less than HDPublicKey.Hardened)
127
+ * @param {boolean} hardened - Whether to derive a hardened key (not supported for public keys)
128
+ * @returns {HDPublicKey} The derived child public key
129
+ * @throws {hdErrors.InvalidIndexCantDeriveHardened} If attempting to derive a hardened key
130
+ * @throws {hdErrors.InvalidPath} If index is negative
131
+ * @private
132
+ */
133
+ HDPublicKey.prototype._deriveWithNumber = function (index, hardened) {
134
+ if (index >= HDPublicKey.Hardened || hardened) {
135
+ throw new hdErrors.InvalidIndexCantDeriveHardened();
136
+ }
137
+ if (index < 0) {
138
+ throw new hdErrors.InvalidPath(index);
139
+ }
140
+
141
+ var indexBuffer = JSUtil.integerAsBuffer(index);
142
+ var data = Buffer.concat([this.publicKey.toBuffer(), indexBuffer]);
143
+ var hash = Hash.sha512hmac(data, this._buffers.chainCode);
144
+ var leftPart = BN.fromBuffer(hash.slice(0, 32), { size: 32 });
145
+ var chainCode = hash.slice(32, 64);
146
+
147
+ var publicKey;
148
+ try {
149
+ publicKey = PublicKey.fromPoint(Point.getG().mul(leftPart).add(this.publicKey.point));
150
+ } catch (e) {
151
+ return this._deriveWithNumber(index + 1);
152
+ }
153
+
154
+ var derived = new HDPublicKey({
155
+ network: this.network,
156
+ depth: this.depth + 1,
157
+ parentFingerPrint: this.fingerPrint,
158
+ childIndex: index,
159
+ chainCode: chainCode,
160
+ publicKey: publicKey,
161
+ });
162
+
163
+ return derived;
164
+ };
165
+
166
+ /**
167
+ * Derives a child public key from the current HD public key using the specified path.
168
+ * @param {string} path - The derivation path (must not contain hardened derivation markers)
169
+ * @returns {HDPublicKey} The derived public key
170
+ * @throws {hdErrors.InvalidIndexCantDeriveHardened} If path contains hardened derivation markers
171
+ * @throws {hdErrors.InvalidPath} If path is not a valid derivation path
172
+ * @private
173
+ */
174
+ HDPublicKey.prototype._deriveFromString = function (path) {
175
+ if (_.includes(path, "'")) {
176
+ throw new hdErrors.InvalidIndexCantDeriveHardened();
177
+ } else if (!HDPublicKey.isValidPath(path)) {
178
+ throw new hdErrors.InvalidPath(path);
179
+ }
180
+
181
+ var indexes = Derivation.getDerivationIndexes(path);
182
+ var derived = indexes.reduce(function (prev, index) {
183
+ return prev._deriveWithNumber(index);
184
+ }, this);
185
+
186
+ return derived;
187
+ };
188
+
189
+ /**
190
+ * Verifies that a given serialized public key in base58 with checksum format
191
+ * is valid.
192
+ *
193
+ * @param {string|Buffer} data - the serialized public key
194
+ * @param {string|Network} [network] - optional, if present, checks that the
195
+ * network provided matches the network serialized.
196
+ * @return {boolean}
197
+ */
198
+ HDPublicKey.isValidSerialized = function (data, network) {
199
+ return _.isNull(HDPublicKey.getSerializedError(data, network));
200
+ };
201
+
202
+ /**
203
+ * Checks what's the error that causes the validation of a serialized public key
204
+ * in base58 with checksum to fail.
205
+ *
206
+ * @param {string|Buffer} data - the serialized public key
207
+ * @param {string|Network} [network] - optional, if present, checks that the
208
+ * network provided matches the network serialized.
209
+ * @return {Error|null}
210
+ */
211
+ HDPublicKey.getSerializedError = function (data, network) {
212
+ if (!(_.isString(data) || Buffer.isBuffer(data))) {
213
+ return new hdErrors.UnrecognizedArgument('expected buffer or string');
214
+ }
215
+ if (!Base58.validCharacters(data)) {
216
+ return new errors.InvalidB58Char('(unknown)', data);
217
+ }
218
+ try {
219
+ data = Base58Check.decode(data);
220
+ } catch (e) {
221
+ return new errors.InvalidB58Checksum(data);
222
+ }
223
+ if (data.length !== HDPublicKey.DataSize) {
224
+ return new hdErrors.InvalidLength(data);
225
+ }
226
+ if (!_.isUndefined(network)) {
227
+ var error = HDPublicKey._validateNetwork(data, network);
228
+ if (error) {
229
+ return error;
230
+ }
231
+ }
232
+ var version = data.readUInt32BE(0);
233
+ if (version === Networks.livenet.xprivkey || version === Networks.testnet.xprivkey) {
234
+ throw new hdErrors.ArgumentIsPrivateExtended();
235
+ }
236
+ return null;
237
+ };
238
+
239
+ /**
240
+ * Validates if the provided data matches the expected network version.
241
+ * @param {Buffer} data - The data containing the version to validate.
242
+ * @param {string|Network} networkArg - The network or network identifier to validate against.
243
+ * @returns {InvalidNetworkArgument|InvalidNetwork|null} Returns an error if validation fails, otherwise null.
244
+ * @private
245
+ */
246
+ HDPublicKey._validateNetwork = function (data, networkArg) {
247
+ var network = Networks.get(networkArg);
248
+ if (!network) {
249
+ return new errors.InvalidNetworkArgument(networkArg);
250
+ }
251
+ var version = data.slice(HDPublicKey.VersionStart, HDPublicKey.VersionEnd);
252
+ if (version.readUInt32BE(0) !== network.xpubkey) {
253
+ return new errors.InvalidNetwork(version);
254
+ }
255
+ return null;
256
+ };
257
+
258
+ /**
259
+ * Builds an HDPublicKey instance from an object containing key components.
260
+ * Handles type conversion for various input formats (numbers, strings, buffers).
261
+ * @param {Object} arg - Object containing key components (version, depth, parentFingerPrint, etc.)
262
+ * @returns {HDPublicKey} The constructed public key instance
263
+ * @private
264
+ */
265
+ HDPublicKey.prototype._buildFromObject = function (arg) {
266
+ // TODO: Type validation
267
+ var buffers = {
268
+ version: arg.network ? JSUtil.integerAsBuffer(Networks.get(arg.network).xpubkey) : arg.version,
269
+ depth: _.isNumber(arg.depth) ? Buffer.from([arg.depth & 0xff]) : arg.depth,
270
+ parentFingerPrint: _.isNumber(arg.parentFingerPrint)
271
+ ? JSUtil.integerAsBuffer(arg.parentFingerPrint)
272
+ : arg.parentFingerPrint,
273
+ childIndex: _.isNumber(arg.childIndex)
274
+ ? JSUtil.integerAsBuffer(arg.childIndex)
275
+ : arg.childIndex,
276
+ chainCode: _.isString(arg.chainCode) ? Buffer.from(arg.chainCode, 'hex') : arg.chainCode,
277
+ publicKey: _.isString(arg.publicKey)
278
+ ? Buffer.from(arg.publicKey, 'hex')
279
+ : Buffer.isBuffer(arg.publicKey)
280
+ ? arg.publicKey
281
+ : arg.publicKey.toBuffer(),
282
+ checksum: _.isNumber(arg.checksum) ? JSUtil.integerAsBuffer(arg.checksum) : arg.checksum,
283
+ };
284
+ return this._buildFromBuffers(buffers);
285
+ };
286
+
287
+ /**
288
+ * Builds an HDPublicKey instance from a serialized Base58Check encoded string.
289
+ * @private
290
+ * @param {string} arg - The Base58Check encoded extended public key (xpub)
291
+ * @returns {HDPublicKey} The constructed HDPublicKey instance
292
+ * @description
293
+ * Decodes the input string into buffers for version, depth, parent fingerprint,
294
+ * child index, chain code, public key, and checksum, then builds the key from buffers.
295
+ */
296
+ HDPublicKey.prototype._buildFromSerialized = function (arg) {
297
+ var decoded = Base58Check.decode(arg);
298
+ var buffers = {
299
+ version: decoded.slice(HDPublicKey.VersionStart, HDPublicKey.VersionEnd),
300
+ depth: decoded.slice(HDPublicKey.DepthStart, HDPublicKey.DepthEnd),
301
+ parentFingerPrint: decoded.slice(
302
+ HDPublicKey.ParentFingerPrintStart,
303
+ HDPublicKey.ParentFingerPrintEnd,
304
+ ),
305
+ childIndex: decoded.slice(HDPublicKey.ChildIndexStart, HDPublicKey.ChildIndexEnd),
306
+ chainCode: decoded.slice(HDPublicKey.ChainCodeStart, HDPublicKey.ChainCodeEnd),
307
+ publicKey: decoded.slice(HDPublicKey.PublicKeyStart, HDPublicKey.PublicKeyEnd),
308
+ checksum: decoded.slice(HDPublicKey.ChecksumStart, HDPublicKey.ChecksumEnd),
309
+ xpubkey: arg,
310
+ };
311
+ return this._buildFromBuffers(buffers);
312
+ };
313
+
314
+ /**
315
+ * Receives a object with buffers in all the properties and populates the
316
+ * internal structure
317
+ *
318
+ * @param {Object} arg
319
+ * @param {buffer.Buffer} arg.version
320
+ * @param {buffer.Buffer} arg.depth
321
+ * @param {buffer.Buffer} arg.parentFingerPrint
322
+ * @param {buffer.Buffer} arg.childIndex
323
+ * @param {buffer.Buffer} arg.chainCode
324
+ * @param {buffer.Buffer} arg.publicKey
325
+ * @param {buffer.Buffer} arg.checksum
326
+ * @param {string=} arg.xpubkey - if set, don't recalculate the base58
327
+ * representation
328
+ * @return {HDPublicKey} this
329
+ * @private
330
+ */
331
+ HDPublicKey.prototype._buildFromBuffers = function (arg) {
332
+ HDPublicKey._validateBufferArguments(arg);
333
+
334
+ JSUtil.defineImmutable(this, {
335
+ _buffers: arg,
336
+ });
337
+
338
+ var sequence = [
339
+ arg.version,
340
+ arg.depth,
341
+ arg.parentFingerPrint,
342
+ arg.childIndex,
343
+ arg.chainCode,
344
+ arg.publicKey,
345
+ ];
346
+ var concat = Buffer.concat(sequence);
347
+ var checksum = Base58Check.checksum(concat);
348
+ if (!arg.checksum || !arg.checksum.length) {
349
+ arg.checksum = checksum;
350
+ } else {
351
+ if (arg.checksum.toString('hex') !== checksum.toString('hex')) {
352
+ throw new errors.InvalidB58Checksum(concat, checksum);
353
+ }
354
+ }
355
+ var network = Networks.get(arg.version.readUInt32BE(0));
356
+
357
+ var xpubkey;
358
+ xpubkey = Base58Check.encode(Buffer.concat(sequence));
359
+ arg.xpubkey = Buffer.from(xpubkey);
360
+
361
+ var publicKey = new PublicKey(arg.publicKey, { network: network });
362
+ var size = HDPublicKey.ParentFingerPrintSize;
363
+ var fingerPrint = Hash.sha256ripemd160(publicKey.toBuffer()).slice(0, size);
364
+
365
+ JSUtil.defineImmutable(this, {
366
+ xpubkey: xpubkey,
367
+ network: network,
368
+ depth: arg.depth[0],
369
+ publicKey: publicKey,
370
+ fingerPrint: fingerPrint,
371
+ });
372
+
373
+ return this;
374
+ };
375
+
376
+ /**
377
+ * Validates buffer arguments for HDPublicKey.
378
+ * @private
379
+ * @param {Object} arg - The argument object containing buffer fields to validate
380
+ * @param {Buffer} arg.version - Version buffer (must be HDPublicKey.VersionSize bytes)
381
+ * @param {Buffer} arg.depth - Depth buffer (must be HDPublicKey.DepthSize bytes)
382
+ * @param {Buffer} arg.parentFingerPrint - Parent fingerprint buffer (must be HDPublicKey.ParentFingerPrintSize bytes)
383
+ * @param {Buffer} arg.childIndex - Child index buffer (must be HDPublicKey.ChildIndexSize bytes)
384
+ * @param {Buffer} arg.chainCode - Chain code buffer (must be HDPublicKey.ChainCodeSize bytes)
385
+ * @param {Buffer} arg.publicKey - Public key buffer (must be HDPublicKey.PublicKeySize bytes)
386
+ * @param {Buffer} [arg.checksum] - Optional checksum buffer (must be HDPublicKey.CheckSumSize bytes if provided)
387
+ * @throws {Error} If any buffer is invalid or has incorrect size
388
+ */
389
+ HDPublicKey._validateBufferArguments = function (arg) {
390
+ var checkBuffer = function (name, size) {
391
+ var buff = arg[name];
392
+ assert(Buffer.isBuffer(buff), name + " argument is not a buffer, it's " + typeof buff);
393
+ assert(
394
+ buff.length === size,
395
+ name + ' has not the expected size: found ' + buff.length + ', expected ' + size,
396
+ );
397
+ };
398
+ checkBuffer('version', HDPublicKey.VersionSize);
399
+ checkBuffer('depth', HDPublicKey.DepthSize);
400
+ checkBuffer('parentFingerPrint', HDPublicKey.ParentFingerPrintSize);
401
+ checkBuffer('childIndex', HDPublicKey.ChildIndexSize);
402
+ checkBuffer('chainCode', HDPublicKey.ChainCodeSize);
403
+ checkBuffer('publicKey', HDPublicKey.PublicKeySize);
404
+ if (arg.checksum && arg.checksum.length) {
405
+ checkBuffer('checksum', HDPublicKey.CheckSumSize);
406
+ }
407
+ };
408
+
409
+ /**
410
+ * Creates an HDPublicKey instance from a string representation.
411
+ * @param {string} arg - The string to convert to an HDPublicKey.
412
+ * @returns {HDPublicKey} A new HDPublicKey instance.
413
+ * @throws {Error} Throws if the input is not a valid string.
414
+ */
415
+ HDPublicKey.fromString = function (arg) {
416
+ $.checkArgument(_.isString(arg), 'No valid string was provided');
417
+ return new HDPublicKey(arg);
418
+ };
419
+
420
+ /**
421
+ * Creates an HDPublicKey instance from an object.
422
+ * @param {Object} arg - The object containing public key data
423
+ * @returns {HDPublicKey} A new HDPublicKey instance
424
+ * @throws {Error} Will throw if no valid object argument is provided
425
+ */
426
+ HDPublicKey.fromObject = function (arg) {
427
+ $.checkArgument(_.isObject(arg), 'No valid argument was provided');
428
+ return new HDPublicKey(arg);
429
+ };
430
+
431
+ /**
432
+ * Returns the base58 checked representation of the public key
433
+ * @return {string} a string starting with "xpub..." in livenet
434
+ */
435
+ HDPublicKey.prototype.toString = function () {
436
+ return this.xpubkey;
437
+ };
438
+
439
+ /**
440
+ * Returns the console representation of this extended public key.
441
+ * @return string
442
+ */
443
+ HDPublicKey.prototype.inspect = function () {
444
+ return '<HDPublicKey: ' + this.xpubkey + '>';
445
+ };
446
+
447
+ /**
448
+ * Returns a plain JavaScript object with information to reconstruct a key.
449
+ *
450
+ * Fields are:
451
+ * <ul>
452
+ * <li> network: 'livenet' or 'testnet' </li>
453
+ * <li> depth: a number from 0 to 255, the depth to the master extended key </li>
454
+ * <li> fingerPrint: a number of 32 bits taken from the hash of the public key </li>
455
+ * <li> fingerPrint: a number of 32 bits taken from the hash of this key's parent's public key </li>
456
+ * <li> childIndex: index with which this key was derived </li>
457
+ * <li> chainCode: string in hexa encoding used for derivation </li>
458
+ * <li> publicKey: string, hexa encoded, in compressed key format </li>
459
+ * <li> checksum: this._buffers.checksum.readUInt32BE(0) </li>
460
+ * <li> xpubkey: the string with the base58 representation of this extended key </li>
461
+ * <li> checksum: the base58 checksum of xpubkey </li>
462
+ * </ul>
463
+ */
464
+ HDPublicKey.prototype.toObject = HDPublicKey.prototype.toJSON = function toObject() {
465
+ return {
466
+ network: Networks.get(this._buffers.version.readUInt32BE(0)).name,
467
+ depth: this._buffers.depth[0],
468
+ fingerPrint: this.fingerPrint.readUInt32BE(0),
469
+ parentFingerPrint: this._buffers.parentFingerPrint.readUInt32BE(0),
470
+ childIndex: this._buffers.childIndex.readUInt32BE(0),
471
+ chainCode: this._buffers.chainCode.toString('hex'),
472
+ publicKey: this.publicKey.toString(),
473
+ checksum: this._buffers.checksum.readUInt32BE(0),
474
+ xpubkey: this.xpubkey,
475
+ };
476
+ };
477
+
478
+ /**
479
+ * Create a HDPublicKey from a buffer argument
480
+ *
481
+ * @param {Buffer} arg
482
+ * @return {HDPublicKey}
483
+ */
484
+ HDPublicKey.fromBuffer = function (arg) {
485
+ return new HDPublicKey(arg);
486
+ };
487
+
488
+ /**
489
+ * Create a HDPublicKey from a hex string argument
490
+ *
491
+ * @param {Buffer} arg
492
+ * @return {HDPublicKey}
493
+ */
494
+ HDPublicKey.fromHex = function (hex) {
495
+ return HDPublicKey.fromBuffer(Buffer.from(hex, 'hex'));
496
+ };
497
+
498
+ /**
499
+ * Return a buffer representation of the xpubkey
500
+ *
501
+ * @return {Buffer}
502
+ */
503
+ HDPublicKey.prototype.toBuffer = function () {
504
+ return Buffer.from(this._buffers.xpubkey);
505
+ };
506
+
507
+ /**
508
+ * Return a hex string representation of the xpubkey
509
+ *
510
+ * @return {Buffer}
511
+ */
512
+ HDPublicKey.prototype.toHex = function () {
513
+ return this.toBuffer().toString('hex');
514
+ };
515
+
516
+ HDPublicKey.Hardened = 0x80000000;
517
+ HDPublicKey.RootElementAlias = ['m', 'M'];
518
+
519
+ HDPublicKey.VersionSize = 4;
520
+ HDPublicKey.DepthSize = 1;
521
+ HDPublicKey.ParentFingerPrintSize = 4;
522
+ HDPublicKey.ChildIndexSize = 4;
523
+ HDPublicKey.ChainCodeSize = 32;
524
+ HDPublicKey.PublicKeySize = 33;
525
+ HDPublicKey.CheckSumSize = 4;
526
+
527
+ HDPublicKey.DataSize = 78;
528
+ HDPublicKey.SerializedByteSize = 82;
529
+
530
+ HDPublicKey.VersionStart = 0;
531
+ HDPublicKey.VersionEnd = HDPublicKey.VersionStart + HDPublicKey.VersionSize;
532
+ HDPublicKey.DepthStart = HDPublicKey.VersionEnd;
533
+ HDPublicKey.DepthEnd = HDPublicKey.DepthStart + HDPublicKey.DepthSize;
534
+ HDPublicKey.ParentFingerPrintStart = HDPublicKey.DepthEnd;
535
+ HDPublicKey.ParentFingerPrintEnd =
536
+ HDPublicKey.ParentFingerPrintStart + HDPublicKey.ParentFingerPrintSize;
537
+ HDPublicKey.ChildIndexStart = HDPublicKey.ParentFingerPrintEnd;
538
+ HDPublicKey.ChildIndexEnd = HDPublicKey.ChildIndexStart + HDPublicKey.ChildIndexSize;
539
+ HDPublicKey.ChainCodeStart = HDPublicKey.ChildIndexEnd;
540
+ HDPublicKey.ChainCodeEnd = HDPublicKey.ChainCodeStart + HDPublicKey.ChainCodeSize;
541
+ HDPublicKey.PublicKeyStart = HDPublicKey.ChainCodeEnd;
542
+ HDPublicKey.PublicKeyEnd = HDPublicKey.PublicKeyStart + HDPublicKey.PublicKeySize;
543
+ HDPublicKey.ChecksumStart = HDPublicKey.PublicKeyEnd;
544
+ HDPublicKey.ChecksumEnd = HDPublicKey.ChecksumStart + HDPublicKey.CheckSumSize;
545
+
546
+ assert(HDPublicKey.PublicKeyEnd === HDPublicKey.DataSize);
547
+ assert(HDPublicKey.ChecksumEnd === HDPublicKey.SerializedByteSize);
548
+
549
+ export default HDPublicKey;
package/esm/index.js ADDED
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+ import util from './util/index.js';
3
+ import encoding from './encoding/index.js';
4
+ import crypto from './crypto/index.js';
5
+ import errors from './errors/index.js';
6
+ import Address from './address.js';
7
+ import Block from './block/index.js';
8
+ import MerkleBlock from './block/merkleblock.js';
9
+ import BlockHeader from './block/blockheader.js';
10
+ import HDPrivateKey from './hdprivatekey.js';
11
+ import HDPublicKey from './hdpublickey.js';
12
+ import Networks from './networks.js';
13
+ import Network from './network.js';
14
+ import Opcode from './opcode.js';
15
+ import PrivateKey from './privatekey.js';
16
+ import PublicKey from './publickey.js';
17
+ import Script from './script/index.js';
18
+ import Transaction from './transaction/index.js';
19
+ import Output from './transaction/output.js';
20
+ import Input from './transaction/input/index.js';
21
+ import Sighash from './transaction/sighash.js';
22
+ import TransactionSignature from './transaction/signature.js';
23
+ import HashCache from './hash-cache.js';
24
+ import Message from './message/message.js';
25
+ import Mnemonic from './mnemonic/index.js';
26
+ import Interpreter from './interpreter/index.js';
27
+
28
+
29
+ export default {
30
+ Address,
31
+ Block,
32
+ MerkleBlock,
33
+ BlockHeader,
34
+ HDPrivateKey,
35
+ HDPublicKey,
36
+ Sighash,
37
+ TransactionSignature,
38
+ Output,
39
+ Input,
40
+ PublicKeyInput: Input.PublicKey,
41
+ PublicKeyHashInput: Input.PublicKeyHash,
42
+ MultiSigInput: Input.MultiSig,
43
+ Networks,
44
+ Network,
45
+ Opcode,
46
+ PrivateKey,
47
+ PublicKey,
48
+ Script,
49
+ Interpreter,
50
+ Transaction,
51
+ HashCache,
52
+ Message,
53
+ Mnemonic,
54
+ errors,
55
+ util,
56
+ encoding,
57
+ crypto,
58
+ };
59
+
60
+ export const PublicKeyInput = Input.PublicKey;
61
+ export const PublicKeyHashInput = Input.PublicKeyHash;
62
+ export const MultiSigInput = Input.MultiSig;
63
+ export { Address, Block, MerkleBlock, BlockHeader, HDPrivateKey, HDPublicKey, Networks, Network, Opcode, PrivateKey, PublicKey, Script, Interpreter, Transaction, Input, Sighash, TransactionSignature, Output, HashCache, Message, Mnemonic, errors, util, encoding, crypto };
64
+
65
+
66
+
@@ -0,0 +1,2 @@
1
+ import Interpreter from './interpreter.js';
2
+ export default Interpreter;