@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,187 @@
1
+ export = ECDSA;
2
+ /**
3
+ * Creates an ECDSA instance.
4
+ * @constructor
5
+ * @param {Object} [obj] - Optional object containing properties to initialize the instance.
6
+ */
7
+ declare function ECDSA(obj?: any): ECDSA;
8
+ declare class ECDSA {
9
+ /**
10
+ * Creates an ECDSA instance.
11
+ * @constructor
12
+ * @param {Object} [obj] - Optional object containing properties to initialize the instance.
13
+ */
14
+ constructor(obj?: any);
15
+ /**
16
+ * Updates the ECDSA instance properties with provided values.
17
+ * @param {Object} obj - Object containing properties to update
18
+ * @param {Buffer} [obj.hashbuf] - Hash buffer
19
+ * @param {string} [obj.endian] - Endianness of hashbuf
20
+ * @param {PrivateKey} [obj.privkey] - Private key
21
+ * @param {PublicKey} [obj.pubkey] - Public key (derived from privkey if not provided)
22
+ * @param {Signature} [obj.sig] - Signature
23
+ * @param {BigInteger} [obj.k] - Random number k
24
+ * @param {boolean} [obj.verified] - Verification status
25
+ * @returns {ECDSA} Returns the updated ECDSA instance
26
+ */
27
+ set(obj: {
28
+ hashbuf?: Buffer;
29
+ endian?: string;
30
+ privkey?: PrivateKey;
31
+ pubkey?: PublicKey;
32
+ sig?: Signature;
33
+ k?: BigInteger;
34
+ verified?: boolean;
35
+ }): ECDSA;
36
+ hashbuf: any;
37
+ endian: any;
38
+ privkey: any;
39
+ pubkey: any;
40
+ sig: any;
41
+ k: any;
42
+ verified: any;
43
+ /**
44
+ * Converts the private key to a public key and stores it in the `pubkey` property.
45
+ */
46
+ privkey2pubkey(): void;
47
+ /**
48
+ * Calculates the recovery factor (i) for ECDSA signature verification.
49
+ * Iterates through possible recovery factors (0-3) to find the one that
50
+ * reconstructs the correct public key from the signature.
51
+ *
52
+ * @returns {ECDSA} Returns the instance with updated signature properties if successful.
53
+ * @throws {Error} Throws if no valid recovery factor is found after all iterations.
54
+ */
55
+ calci(): ECDSA;
56
+ /**
57
+ * Generates a random value `k` for ECDSA signing.
58
+ * The value is generated within the range (0, N) where N is the curve order.
59
+ * The generated `k` is stored in the instance and returned for chaining.
60
+ */
61
+ randomK(): this;
62
+ /**
63
+ * Generates a deterministic K value for ECDSA signing as per RFC 6979.
64
+ * See:
65
+ * https://tools.ietf.org/html/rfc6979#section-3.2
66
+ * Handles invalid r/s cases by incrementing badrs counter and regenerating K.
67
+ * @param {number} [badrs=0] - Counter for invalid r/s cases (default: 0)
68
+ * @returns {ECDSA} Returns the ECDSA instance for chaining
69
+ */
70
+ deterministicK(badrs?: number): ECDSA;
71
+ /**
72
+ * Converts an ECDSA signature to its corresponding public key.
73
+ *
74
+ * The method follows the ECDSA public key recovery process:
75
+ * 1. Validates the recovery parameter `i` (must be 0-3)
76
+ * 2. Derives the public key point Q using the formula: Q = r⁻¹(sR - eG)
77
+ * 3. Validates the derived curve point
78
+ *
79
+ * see:
80
+ * https://bitcointalk.org/index.php?topic=6430.0
81
+ * http://stackoverflow.com/questions/19665491/how-do-i-get-an-ecdsa-public-key-from-just-a-bitcoin-signature-sec1-4-1-6-k
82
+ * @returns {PublicKey} The recovered public key
83
+ * @throws {Error} If recovery parameter is invalid or derived point is invalid
84
+ */
85
+ toPublicKey(): PublicKey;
86
+ /**
87
+ * Validates an ECDSA signature and returns an error message if invalid.
88
+ * Checks:
89
+ * - hashbuf is a 32-byte buffer
90
+ * - r and s values are within valid range
91
+ * - Signature verification against public key
92
+ * @returns {string|boolean} Error message if invalid, false if valid
93
+ */
94
+ sigError(): string | boolean;
95
+ /**
96
+ * Finds a valid ECDSA signature (r, s) for the given private key `d` and message hash `e`.
97
+ * Uses deterministic k-value generation if initial attempts fail.
98
+ *
99
+ * @param {BN} d - Private key as a big number.
100
+ * @param {BN} e - Message hash as a big number.
101
+ * @returns {Object} Signature object with properties `r` and `s` (big numbers).
102
+ * @throws Will throw if unable to find valid signature after multiple attempts.
103
+ */
104
+ _findSignature(d: BN, e: BN): any;
105
+ /**
106
+ * Signs a message using ECDSA.
107
+ *
108
+ * @param {Buffer} hashbuf - 32-byte buffer containing the hash of the message to sign.
109
+ * @param {PrivateKey} privkey - Private key used for signing.
110
+ * @returns {ECDSA} Returns the instance for chaining.
111
+ * @throws {Error} Throws if parameters are invalid or hashbuf is not a 32-byte buffer.
112
+ */
113
+ sign(): ECDSA;
114
+ /**
115
+ * Signs the message using a randomly generated k value.
116
+ *
117
+ * @returns The signature object containing r and s values.
118
+ */
119
+ signRandomK(): ECDSA;
120
+ /**
121
+ * Converts the ECDSA instance to a JSON string representation.
122
+ * Includes hash buffer, private key, public key, signature, and k value if present.
123
+ * Each property is converted to a string format (hex for hashbuf, toString() for others).
124
+ * @returns {string} JSON string containing the ECDSA instance properties
125
+ */
126
+ toString(): string;
127
+ /**
128
+ * Verifies the ECDSA signature and updates the `verified` property.
129
+ * @returns {ECDSA} The current instance for chaining.
130
+ */
131
+ verify(): ECDSA;
132
+ }
133
+ declare namespace ECDSA {
134
+ /**
135
+ * Creates an ECDSA instance from a JSON string representation.
136
+ * @param {string} str - JSON string containing ECDSA parameters.
137
+ * @returns {ECDSA} New ECDSA instance initialized with parsed data.
138
+ */
139
+ function fromString(str: string): ECDSA;
140
+ /**
141
+ * Converts the signature `s` value to its low-S form to comply with BIP 62.
142
+ * This prevents signature malleability by ensuring `s` is not greater than half the curve order.
143
+ * @param {BN} s - The signature `s` value as a big number.
144
+ * @returns {BN} The low-S normalized value.
145
+ * @static
146
+ */
147
+ function toLowS(s: BN): BN;
148
+ /**
149
+ * Signs a message hash using ECDSA with the given private key.
150
+ * @param {Buffer} hashbuf - The hash of the message to sign
151
+ * @param {PrivateKey} privkey - The private key to sign with
152
+ * @param {string} [endian] - Endianness of the input/output (optional)
153
+ * @returns {Signature} The ECDSA signature
154
+ */
155
+ function sign(hashbuf: Buffer, privkey: PrivateKey, endian?: string): Signature;
156
+ /**
157
+ * Signs a hash buffer with a private key and calculates the 'i' value.
158
+ * @param {Buffer} hashbuf - The hash buffer to sign.
159
+ * @param {Buffer} privkey - The private key used for signing.
160
+ * @param {string} [endian] - The endianness of the input data (optional).
161
+ * @returns {Buffer} The resulting signature.
162
+ * @static
163
+ */
164
+ function signWithCalcI(hashbuf: Buffer, privkey: Buffer, endian?: string): Buffer;
165
+ /**
166
+ * Signs a message hash using ECDSA with a randomly generated K value.
167
+ * @param {Buffer} hashbuf - The message hash to sign.
168
+ * @param {Buffer} privkey - The private key used for signing.
169
+ * @param {string} [endian] - The endianness of the input/output (default: 'big').
170
+ * @returns {Buffer} The generated ECDSA signature.
171
+ * @static
172
+ */
173
+ function signRandomK(hashbuf: Buffer, privkey: Buffer, endian?: string): Buffer;
174
+ /**
175
+ * Verifies an ECDSA signature against a hash and public key.
176
+ * @param {Buffer} hashbuf - The hash buffer to verify against.
177
+ * @param {Signature} sig - The signature to verify.
178
+ * @param {PublicKey} pubkey - The public key to verify with.
179
+ * @param {string} [endian] - The endianness of the input data (optional).
180
+ * @returns {boolean} True if the signature is valid, false otherwise.
181
+ * @static
182
+ */
183
+ function verify(hashbuf: Buffer, sig: Signature, pubkey: PublicKey, endian?: string): boolean;
184
+ }
185
+ import PublicKey = require("../publickey.cjs");
186
+ import Signature = require("./signature.cjs");
187
+ import BN = require("../bn.cjs");
@@ -0,0 +1,118 @@
1
+ export = Hash;
2
+ /**
3
+ * Represents a hash utility class.
4
+ * @constructor
5
+ */
6
+ declare function Hash(): Hash;
7
+ declare class Hash {
8
+ }
9
+ declare namespace Hash {
10
+ /**
11
+ * A SHA or SHA1 hash, which is always 160 bits or 20 bytes long.
12
+ *
13
+ * See:
14
+ * https://en.wikipedia.org/wiki/SHA-1
15
+ *
16
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
17
+ * @returns {Buffer} The hash in the form of a buffer.
18
+ */
19
+ function sha1(buf: Buffer): Buffer;
20
+ namespace sha1 {
21
+ let blocksize: number;
22
+ }
23
+ /**
24
+ * A SHA256 hash, which is always 256 bits or 32 bytes long.
25
+ *
26
+ * See:
27
+ * https://www.movable-type.co.uk/scripts/sha256.html
28
+ *
29
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
30
+ * @returns {Buffer} The hash in the form of a buffer.
31
+ */
32
+ function sha256(buf: Buffer): Buffer;
33
+ namespace sha256 {
34
+ let blocksize_1: number;
35
+ export { blocksize_1 as blocksize };
36
+ }
37
+ /**
38
+ * A double SHA256 hash, which is always 256 bits or 32 bytes bytes long. This
39
+ * hash function is commonly used inside Bitcoin, particularly for the hash of a
40
+ * block and the hash of a transaction.
41
+ *
42
+ * See:
43
+ * https://www.movable-type.co.uk/scripts/sha256.html
44
+ *
45
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
46
+ * @returns {Buffer} The hash in the form of a buffer.
47
+ */
48
+ function sha256sha256(buf: Buffer): Buffer;
49
+ /**
50
+ * A RIPEMD160 hash, which is always 160 bits or 20 bytes long.
51
+ *
52
+ * See:
53
+ * https://en.wikipedia.org/wiki/RIPEMD
54
+ *
55
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
56
+ * @returns {Buffer} The hash in the form of a buffer.
57
+ */
58
+ function ripemd160(buf: Buffer): Buffer;
59
+ /**
60
+ * A RIPEMD160 hash of a SHA256 hash, which is always 160 bits or 20 bytes long.
61
+ * This value is commonly used inside Bitcoin, particularly for Bitcoin
62
+ * addresses.
63
+ *
64
+ * See:
65
+ * https://en.wikipedia.org/wiki/RIPEMD
66
+ *
67
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
68
+ * @returns {Buffer} The hash in the form of a buffer.
69
+ */
70
+ function sha256ripemd160(buf: Buffer): Buffer;
71
+ /**
72
+ * A SHA512 hash, which is always 512 bits or 64 bytes long.
73
+ *
74
+ * See:
75
+ * https://en.wikipedia.org/wiki/SHA-2
76
+ *
77
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
78
+ * @returns {Buffer} The hash in the form of a buffer.
79
+ */
80
+ function sha512(buf: Buffer): Buffer;
81
+ namespace sha512 {
82
+ let blocksize_2: number;
83
+ export { blocksize_2 as blocksize };
84
+ }
85
+ /**
86
+ * A way to do HMAC using any underlying hash function. If you ever find that
87
+ * you want to hash two pieces of data together, you should use HMAC instead of
88
+ * just using a hash function. Rather than doing hash(data1 + data2) you should
89
+ * do HMAC(data1, data2). Actually, rather than use HMAC directly, we recommend
90
+ * you use either sha256hmac or sha515hmac provided below.
91
+ *
92
+ * See:
93
+ * https://en.wikipedia.org/wiki/Length_extension_attack
94
+ * https://blog.skullsecurity.org/2012/everything-you-need-to-know-about-hash-length-extension-attacks
95
+ *
96
+ * @param {function} hashf Which hash function to use.
97
+ * @param {Buffer} data Data, which can be any size.
98
+ * @param {Buffer} key Key, which can be any size.
99
+ * @returns {Buffer} The HMAC in the form of a buffer.
100
+ */
101
+ function hmac(hashf: Function, data: Buffer, key: Buffer): Buffer;
102
+ /**
103
+ * A SHA256 HMAC.
104
+ *
105
+ * @param {Buffer} data Data, which can be any size.
106
+ * @param {Buffer} key Key, which can be any size.
107
+ * @returns {Buffer} The HMAC in the form of a buffer.
108
+ */
109
+ function sha256hmac(data: Buffer, key: Buffer): Buffer;
110
+ /**
111
+ * A SHA512 HMAC.
112
+ *
113
+ * @param {Buffer} data Data, which can be any size.
114
+ * @param {Buffer} key Key, which can be any size.
115
+ * @returns {Buffer} The HMAC in the form of a buffer.
116
+ */
117
+ function sha512hmac(data: Buffer, key: Buffer): Buffer;
118
+ }
@@ -0,0 +1,7 @@
1
+ import BN = require("../bn.cjs");
2
+ import ECDSA = require("./ecdsa.cjs");
3
+ import Hash = require("./hash.cjs");
4
+ import Random = require("./random.cjs");
5
+ import Point = require("./point.cjs");
6
+ import Signature = require("./signature.cjs");
7
+ export { BN, ECDSA, Hash, Random, Point, Signature };
@@ -0,0 +1,134 @@
1
+ export = Point;
2
+ /**
3
+ * Instantiate a valid secp256k1 Point from the X and Y coordinates. This class
4
+ * is just an extension of the secp256k1 code from the library "elliptic" by
5
+ * Fedor Indutny. It includes a few extra features that are useful in Bitcoin.
6
+ *
7
+ * @param {BN|String} x - The X coordinate
8
+ * @param {BN|String} y - The Y coordinate
9
+ * @link https://github.com/indutny/elliptic
10
+ * @augments elliptic.curve.point
11
+ * @throws {Error} A validation error if exists
12
+ * @returns {Point} An instance of Point
13
+ * @constructor
14
+ */
15
+ declare function Point(x: BN | string, y: BN | string, isRed: any): Point;
16
+ declare class Point {
17
+ /**
18
+ * Instantiate a valid secp256k1 Point from the X and Y coordinates. This class
19
+ * is just an extension of the secp256k1 code from the library "elliptic" by
20
+ * Fedor Indutny. It includes a few extra features that are useful in Bitcoin.
21
+ *
22
+ * @param {BN|String} x - The X coordinate
23
+ * @param {BN|String} y - The Y coordinate
24
+ * @link https://github.com/indutny/elliptic
25
+ * @augments elliptic.curve.point
26
+ * @throws {Error} A validation error if exists
27
+ * @returns {Point} An instance of Point
28
+ * @constructor
29
+ */
30
+ constructor(x: BN | string, y: BN | string, isRed: any);
31
+ _getX: any;
32
+ /**
33
+ * Will return the X coordinate of the Point.
34
+ *
35
+ * @returns {BN} A BN instance of the X coordinate
36
+ */
37
+ getX(): BN;
38
+ _getY: any;
39
+ /**
40
+ * Will return the Y coordinate of the Point.
41
+ *
42
+ * @returns {BN} A BN instance of the Y coordinate
43
+ */
44
+ getY(): BN;
45
+ /**
46
+ * Will determine if the point is valid.
47
+ *
48
+ * @link https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf
49
+ * @throws {Error} A validation error if exists
50
+ * @returns {Point} An instance of the same Point
51
+ */
52
+ validate(): Point;
53
+ /**
54
+ * Convert point to a compressed buffer.
55
+ *
56
+ * @returns {Buffer} A compressed point.
57
+ */
58
+ toBuffer(): Buffer;
59
+ /**
60
+ * Convert point to a compressed hex string.
61
+ *
62
+ * @returns {string} A compressed point as a hex string.
63
+ */
64
+ toHex(): string;
65
+ }
66
+ declare namespace Point {
67
+ /**
68
+ *
69
+ * Instantiate a valid secp256k1 Point from only the X coordinate. This is
70
+ * useful to rederive a full point from the compressed form of a point.
71
+ *
72
+ * @param {boolean} odd - If the Y coordinate is odd
73
+ * @param {BN|String} x - The X coordinate
74
+ * @throws {Error} A validation error if exists
75
+ * @returns {Point} An instance of Point
76
+ * @static
77
+ */
78
+ function fromX(odd: boolean, x: string | BN): Point;
79
+ /**
80
+ *
81
+ * Will return a secp256k1 ECDSA base point.
82
+ *
83
+ * @link https://en.bitcoin.it/wiki/Secp256k1
84
+ * @returns {Point} An instance of the base point.
85
+ * @static
86
+ */
87
+ function getG(): Point;
88
+ /**
89
+ *
90
+ * Will return the max of range of valid private keys as governed by the
91
+ * secp256k1 ECDSA standard.
92
+ *
93
+ * @link https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys
94
+ * @returns {BN} A BN instance of the number of points on the curve
95
+ * @static
96
+ */
97
+ function getN(): BN;
98
+ /**
99
+ * A "compressed" format point is the X part of the (X, Y) point plus an extra
100
+ * bit (which takes an entire byte) to indicate whether the Y value is odd or
101
+ * not. Storing points this way takes a bit less space, but requires a bit more
102
+ * computation to rederive the full point.
103
+ *
104
+ * @param {Point} point An instance of Point.
105
+ * @returns {Buffer} A compressed point in the form of a buffer.
106
+ * @static
107
+ */
108
+ function pointToCompressed(point: Point): Buffer;
109
+ /**
110
+ * Converts a compressed buffer into a point.
111
+ *
112
+ * @param {Buffer} buf A compressed point.
113
+ * @returns {Point} A Point.
114
+ * @static
115
+ */
116
+ function pointFromCompressed(buf: Buffer): Point;
117
+ /**
118
+ * Converts a compressed buffer into a point.
119
+ *
120
+ * @param {Buffer} buf A compressed point.
121
+ * @returns {Point} A Point.
122
+ * @static
123
+ */
124
+ function fromBuffer(buf: Buffer): Point;
125
+ /**
126
+ * Converts a compressed buffer into a point.
127
+ *
128
+ * @param {Buffer} hex A compressed point as a hex string.
129
+ * @returns {Point} A Point.
130
+ * @static
131
+ */
132
+ function fromHex(hex: Buffer): Point;
133
+ }
134
+ import BN = require("../bn.cjs");
@@ -0,0 +1,13 @@
1
+ export = Random;
2
+ /**
3
+ * A utility class for generating random values.
4
+ */
5
+ declare function Random(): void;
6
+ declare namespace Random {
7
+ /**
8
+ * Generates a cryptographically secure random buffer of the specified size.
9
+ * @param {number} size - The number of bytes to generate.
10
+ * @returns {Buffer} A buffer filled with cryptographically secure random bytes.
11
+ */
12
+ function getRandomBuffer(size: number): Buffer;
13
+ }
@@ -0,0 +1,160 @@
1
+ export = Signature;
2
+ /**
3
+ * Creates a new Signature instance from BN values or an object.
4
+ * @constructor
5
+ * @param {BN|Object} r - Either a BN instance for the r value or an object containing r and s properties.
6
+ * @param {BN} [s] - The s value (required if r is a BN instance).
7
+ */
8
+ declare function Signature(r: BN | any, s?: BN): Signature;
9
+ declare class Signature {
10
+ /**
11
+ * Creates a new Signature instance from BN values or an object.
12
+ * @constructor
13
+ * @param {BN|Object} r - Either a BN instance for the r value or an object containing r and s properties.
14
+ * @param {BN} [s] - The s value (required if r is a BN instance).
15
+ */
16
+ constructor(r: BN | any, s?: BN);
17
+ /**
18
+ * Sets signature properties from an object.
19
+ * @param {Object} obj - Object containing signature properties
20
+ * @param {Buffer} [obj.r] - r value
21
+ * @param {Buffer} [obj.s] - s value
22
+ * @param {number} [obj.i] - Public key recovery parameter (0-3)
23
+ * @param {boolean} [obj.compressed] - Whether recovered pubkey is compressed
24
+ * @param {number} [obj.nhashtype] - Hash type
25
+ * @returns {Signature} Returns the signature instance for chaining
26
+ */
27
+ set(obj: {
28
+ r?: Buffer;
29
+ s?: Buffer;
30
+ i?: number;
31
+ compressed?: boolean;
32
+ nhashtype?: number;
33
+ }): Signature;
34
+ r: any;
35
+ s: any;
36
+ i: any;
37
+ compressed: any;
38
+ nhashtype: any;
39
+ /**
40
+ * Converts the signature to a compact format.
41
+ * @param {number} [i] - The recovery ID (0, 1, 2, or 3). Defaults to the instance's `i` value.
42
+ * @param {boolean} [compressed] - Whether the signature is compressed. Defaults to the instance's `compressed` value.
43
+ * @returns {Buffer} - The compact signature as a Buffer (1 byte recovery ID + 32 bytes r + 32 bytes s).
44
+ * @throws {Error} - If `i` is not 0, 1, 2, or 3.
45
+ */
46
+ toCompact(i?: number, compressed?: boolean): Buffer;
47
+ /**
48
+ * Converts the signature to DER format.
49
+ * Handles negative values by prepending a zero byte if necessary.
50
+ *
51
+ * @returns {Buffer} The DER-encoded signature.
52
+ */
53
+ toBuffer: () => Buffer;
54
+ toDER(): Buffer;
55
+ /**
56
+ * Converts the signature to a hexadecimal string representation.
57
+ * @returns {string} The DER-encoded signature in hexadecimal format.
58
+ */
59
+ toString(): string;
60
+ /**
61
+ * Checks if the signature's S value is within the valid range (low-S).
62
+ * See also ECDSA signature algorithm which enforces this.
63
+ * See also BIP 62, "low S values in signatures"
64
+ * @returns {boolean} True if S is between 1 and the upper bound (0x7F...A0), false otherwise.
65
+ */
66
+ hasLowS(): boolean;
67
+ /**
68
+ * Checks if the signature has a defined hashtype.
69
+ * - Validates that nhashtype is a natural number
70
+ * - Accepts with or without Signature.SIGHASH_ANYONECANPAY by ignoring the bit
71
+ * - Verifies the hashtype is between SIGHASH_ALL and SIGHASH_SINGLE
72
+ * @returns {boolean} True if the hashtype is valid, false otherwise
73
+ */
74
+ hasDefinedHashtype(): boolean;
75
+ /**
76
+ * Converts the signature to transaction format by concatenating the DER-encoded signature
77
+ * with the hash type byte.
78
+ * @returns {Buffer} The signature in transaction format (DER + hash type byte).
79
+ */
80
+ toTxFormat(): Buffer;
81
+ }
82
+ declare namespace Signature {
83
+ /**
84
+ * Creates a Signature instance from a compact ECDSA signature buffer.
85
+ * @param {Buffer} buf - The compact signature buffer (65 bytes).
86
+ * @returns {Signature} The parsed signature object.
87
+ * @throws {Error} If the input is invalid (not a Buffer, wrong length, or invalid recovery param).
88
+ * @static
89
+ */
90
+ export function fromCompact(buf: Buffer): Signature;
91
+ export function fromDER(buf: Buffer, strict?: boolean): Signature;
92
+ export function fromBuffer(buf: Buffer, strict?: boolean): Signature;
93
+ /**
94
+ * Converts a transaction-format signature buffer to a Signature object.
95
+ * @param {Buffer} buf - The signature buffer in transaction format (DER + hash type byte)
96
+ * @returns {Signature} The parsed Signature object with nhashtype property set
97
+ * @static
98
+ */
99
+ export function fromTxFormat(buf: Buffer): Signature;
100
+ /**
101
+ * Creates a Signature instance from a hex-encoded string.
102
+ * @param {string} str - Hex-encoded signature string
103
+ * @returns {Signature} Signature instance parsed from DER format
104
+ * @static
105
+ */
106
+ export function fromString(str: string): Signature;
107
+ /**
108
+ * Parses a DER formatted signature buffer into its components.
109
+ * In order to mimic the non-strict DER encoding of OpenSSL, set strict = false.
110
+ * @param {Buffer} buf - The DER formatted signature buffer to parse
111
+ * @param {boolean} [strict=true] - Whether to perform strict length validation
112
+ * @returns {Object} An object containing the parsed signature components:
113
+ * - header: The DER header byte (0x30)
114
+ * - length: The total length of the signature components
115
+ * - rheader: The R component header byte (0x02)
116
+ * - rlength: The length of the R component
117
+ * - rneg: Whether R is negative
118
+ * - rbuf: The R component buffer
119
+ * - r: The R component as a BN
120
+ * - sheader: The S component header byte (0x02)
121
+ * - slength: The length of the S component
122
+ * - sneg: Whether S is negative
123
+ * - sbuf: The S component buffer
124
+ * - s: The S component as a BN
125
+ * @throws {Error} If the buffer is not valid DER format or length checks fail
126
+ * @static
127
+ */
128
+ export function parseDER(buf: Buffer, strict?: boolean): any;
129
+ /**
130
+ * This function is translated from bitcoind's IsDERSignature and is used in
131
+ * the script interpreter. This "DER" format actually includes an extra byte,
132
+ * the nhashtype, at the end. It is really the tx format, not DER format.
133
+ *
134
+ * A canonical signature exists of: [30] [total len] [02] [len R] [R] [02] [len S] [S] [hashtype]
135
+ * Where R and S are not negative (their first byte has its highest bit not set), and not
136
+ * excessively padded (do not start with a 0 byte, unless an otherwise negative number follows,
137
+ * in which case a single 0 byte is necessary and even required).
138
+ *
139
+ * See https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623
140
+ *
141
+ * @param {Buffer} buf - The buffer containing the signature to verify
142
+ * @returns {boolean} True if the signature is valid DER-encoded, false otherwise
143
+ * @static
144
+ */
145
+ export function isTxDER(buf: Buffer): boolean;
146
+ export let SIGHASH_ALL: number;
147
+ export let SIGHASH_NONE: number;
148
+ export let SIGHASH_SINGLE: number;
149
+ export let SIGHASH_ANYONECANPAY: number;
150
+ import ALL = SIGHASH_ALL;
151
+ export { ALL };
152
+ import NONE = SIGHASH_NONE;
153
+ export { NONE };
154
+ import SINGLE = SIGHASH_SINGLE;
155
+ export { SINGLE };
156
+ export let ANYONECANPAY_ALL: number;
157
+ export let ANYONECANPAY_NONE: number;
158
+ export let ANYONECANPAY_SINGLE: number;
159
+ }
160
+ import BN = require("../bn.cjs");