@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
@@ -2,14 +2,14 @@
2
2
 
3
3
  var inherits = require('inherits');
4
4
 
5
- var $ = require('../../util/preconditions');
5
+ var $ = require('../../util/preconditions.cjs');
6
6
 
7
- var Input = require('./input');
8
- var Output = require('../output');
9
- var Sighash = require('../sighash');
10
- var Script = require('../../script');
11
- var Signature = require('../../crypto/signature');
12
- var TransactionSignature = require('../signature');
7
+ var Input = require('./input.cjs');
8
+ var Output = require('../output.cjs');
9
+ var Sighash = require('../sighash.cjs');
10
+ var Script = require('../../script/index.cjs');
11
+ var Signature = require('../../crypto/signature.cjs');
12
+ var TransactionSignature = require('../signature.cjs');
13
13
 
14
14
  /**
15
15
  * Represents a special kind of input of PayToPublicKey kind.
@@ -24,7 +24,7 @@ inherits(PublicKeyInput, Input);
24
24
  * @param {Transaction} transaction - the transaction to be signed
25
25
  * @param {PrivateKey} privateKey - the private key with which to sign the transaction
26
26
  * @param {number} index - the index of the input in the transaction input vector
27
- * @param {number=} sigtype - the type of signature, defaults to Signature.SIGHASH_ALL
27
+ * @param {number} [sigtype] - the type of signature, defaults to Signature.SIGHASH_ALL
28
28
  * @return {Array} of objects that can be
29
29
  */
30
30
  PublicKeyInput.prototype.getSignatures = function (transaction, privateKey, index, sigtype) {
@@ -51,14 +51,13 @@ PublicKeyInput.prototype.getSignatures = function (transaction, privateKey, inde
51
51
  return [];
52
52
  };
53
53
 
54
+
54
55
  /**
55
- * Add the provided signature
56
- *
57
- * @param {Object} signature
58
- * @param {PublicKey} signature.publicKey
59
- * @param {Signature} signature.signature
60
- * @param {number=} signature.sigtype
61
- * @return {PublicKeyInput} this, for chaining
56
+ * Adds a signature to the public key input after validating it.
57
+ * @param {Object} transaction - The transaction to validate against.
58
+ * @param {TransactionSignature} signature - The signature object containing signature data and type.
59
+ * @returns {PublicKeyInput} Returns the instance for chaining.
60
+ * @throws {Error} Throws if the signature is invalid.
62
61
  */
63
62
  PublicKeyInput.prototype.addSignature = function (transaction, signature) {
64
63
  $.checkState(this.isValidSignature(transaction, signature), 'Signature is invalid');
@@ -66,18 +65,20 @@ PublicKeyInput.prototype.addSignature = function (transaction, signature) {
66
65
  return this;
67
66
  };
68
67
 
68
+
69
69
  /**
70
- * Clear the input's signature
71
- * @return {PublicKeyHashInput} this, for chaining
70
+ * Clears all signatures from this input by setting an empty script.
71
+ * @returns {PublicKeyInput} The instance for chaining.
72
72
  */
73
73
  PublicKeyInput.prototype.clearSignatures = function () {
74
74
  this.setScript(Script.empty());
75
75
  return this;
76
76
  };
77
77
 
78
+
78
79
  /**
79
- * Query whether the input is signed
80
- * @return {boolean}
80
+ * Checks if the public key input is fully signed by verifying the script contains a public key.
81
+ * @returns {boolean} True if the script contains a public key, false otherwise.
81
82
  */
82
83
  PublicKeyInput.prototype.isFullySigned = function () {
83
84
  return this.script.isPublicKeyIn();
@@ -91,8 +92,17 @@ PublicKeyInput.prototype.isFullySigned = function () {
91
92
  // <=72 signature (DER + SIGHASH type)
92
93
  // ---
93
94
  // 4 sequence number
95
+ /**
96
+ * The maximum allowed size (in bytes) for a public key script in a transaction input.
97
+ * @constant {number}
98
+ */
94
99
  PublicKeyInput.SCRIPT_MAX_SIZE = 74;
95
100
 
101
+ /**
102
+ * Estimates the byte size required for this public key input.
103
+ * @returns {number} The estimated size in bytes (base input size + max script size).
104
+ * @private
105
+ */
96
106
  PublicKeyInput.prototype._estimateSize = function () {
97
107
  return Input.BASE_SIZE + PublicKeyInput.SCRIPT_MAX_SIZE;
98
108
  };
@@ -2,15 +2,15 @@
2
2
 
3
3
  var inherits = require('inherits');
4
4
 
5
- var $ = require('../../util/preconditions');
5
+ var $ = require('../../util/preconditions.cjs');
6
6
 
7
- var Hash = require('../../crypto/hash');
8
- var Input = require('./input');
9
- var Output = require('../output');
10
- var Sighash = require('../sighash');
11
- var Script = require('../../script');
12
- var Signature = require('../../crypto/signature');
13
- var TransactionSignature = require('../signature');
7
+ var Hash = require('../../crypto/hash.cjs');
8
+ var Input = require('./input.cjs');
9
+ var Output = require('../output.cjs');
10
+ var Sighash = require('../sighash.cjs');
11
+ var Script = require('../../script/index.cjs');
12
+ var Signature = require('../../crypto/signature.cjs');
13
+ var TransactionSignature = require('../signature.cjs');
14
14
 
15
15
  /**
16
16
  * Represents a special kind of input of PayToPublicKeyHash kind.
@@ -25,8 +25,8 @@ inherits(PublicKeyHashInput, Input);
25
25
  * @param {Transaction} transaction - the transaction to be signed
26
26
  * @param {PrivateKey} privateKey - the private key with which to sign the transaction
27
27
  * @param {number} index - the index of the input in the transaction input vector
28
- * @param {number=} sigtype - the type of signature, defaults to Signature.SIGHASH_ALL
29
- * @param {Buffer=} hashData - the precalculated hash of the public key associated with the privateKey provided
28
+ * @param {number} [sigtype] - the type of signature, defaults to Signature.SIGHASH_ALL
29
+ * @param {Buffer} [hashData] - the precalculated hash of the public key associated with the privateKey provided
30
30
  * @return {Array} of objects that can be
31
31
  */
32
32
  PublicKeyHashInput.prototype.getSignatures = function (
@@ -60,14 +60,13 @@ PublicKeyHashInput.prototype.getSignatures = function (
60
60
  return [];
61
61
  };
62
62
 
63
+
63
64
  /**
64
- * Add the provided signature
65
- *
66
- * @param {Object} signature
67
- * @param {PublicKey} signature.publicKey
68
- * @param {Signature} signature.signature
69
- * @param {number=} signature.sigtype
70
- * @return {PublicKeyHashInput} this, for chaining
65
+ * Adds a signature to the input and updates the script.
66
+ * @param {Transaction} transaction - The transaction to validate against.
67
+ * @param {TransactionSignature} signature - The signature object containing publicKey, signature (DER format), and sigtype.
68
+ * @returns {PublicKeyHashInput} Returns the instance for chaining.
69
+ * @throws {Error} Throws if the signature is invalid.
71
70
  */
72
71
  PublicKeyHashInput.prototype.addSignature = function (transaction, signature) {
73
72
  $.checkState(this.isValidSignature(transaction, signature), 'Signature is invalid');
@@ -109,8 +108,17 @@ PublicKeyHashInput.prototype.isFullySigned = function () {
109
108
  // 65 uncompressed public key
110
109
  //
111
110
  // 4 sequence number
111
+ /**
112
+ * The maximum allowed size (in bytes) for a public key hash script.
113
+ * @constant
114
+ */
112
115
  PublicKeyHashInput.SCRIPT_MAX_SIZE = 140;
113
116
 
117
+ /**
118
+ * Estimates the byte size of this public key hash input.
119
+ * @returns {number} The estimated size in bytes (base input size + max script size).
120
+ * @private
121
+ */
114
122
  PublicKeyHashInput.prototype._estimateSize = function () {
115
123
  return Input.BASE_SIZE + PublicKeyHashInput.SCRIPT_MAX_SIZE;
116
124
  };
@@ -1,17 +1,26 @@
1
1
  'use strict';
2
2
 
3
- var _ = require('../util/_');
4
- var BN = require('../crypto/bn');
5
- var JSUtil = require('../util/js');
6
- var BufferWriter = require('../encoding/bufferwriter');
7
- var Varint = require('../encoding/varint');
8
- var Script = require('../script');
9
- var $ = require('../util/preconditions');
10
- var errors = require('../errors');
11
- var Hash = require('../crypto/hash')
3
+ var _ = require('../util/_.cjs');
4
+ var BN = require('../crypto/bn.cjs');
5
+ var JSUtil = require('../util/js.cjs');
6
+ var BufferWriter = require('../encoding/bufferwriter.cjs');
7
+ var Varint = require('../encoding/varint.cjs');
8
+ var Script = require('../script/index.cjs');
9
+ var $ = require('../util/preconditions.cjs');
10
+ var errors = require('../errors/index.cjs');
11
+ var Hash = require('../crypto/hash.cjs')
12
12
 
13
13
  var MAX_SAFE_INTEGER = 0x1fffffffffffff;
14
14
 
15
+ /**
16
+ * Represents a transaction output in the Bitcoin protocol.
17
+ * @constructor
18
+ * @param {Object} args - The arguments to create an Output.
19
+ * @param {number} args.satoshis - The amount in satoshis.
20
+ * @param {Buffer|string|Script} args.script - The output script (either as Buffer or hex string).
21
+ * @param {Buffer|string} [args.data] - Additional data associated with the output.
22
+ * @throws {TypeError} If arguments are invalid or unrecognized.
23
+ */
15
24
  function Output(args) {
16
25
  if (!(this instanceof Output)) {
17
26
  return new Output(args);
@@ -34,7 +43,11 @@ function Output(args) {
34
43
  throw new TypeError('Unrecognized argument for Output');
35
44
  }
36
45
  }
37
-
46
+ /**
47
+ * Gets or sets the script associated with this Output instance.
48
+ * @memberof Output.prototype
49
+ * @name script
50
+ */
38
51
  Object.defineProperty(Output.prototype, 'script', {
39
52
  configurable: false,
40
53
  enumerable: true,
@@ -43,6 +56,12 @@ Object.defineProperty(Output.prototype, 'script', {
43
56
  },
44
57
  });
45
58
 
59
+ /**
60
+ * Gets or sets the transaction output data.
61
+ * @memberof Output.prototype
62
+ * @name data
63
+ * @type {Buffer|string}
64
+ */
46
65
  Object.defineProperty(Output.prototype, 'data', {
47
66
  configurable: false,
48
67
  enumerable: true,
@@ -50,7 +69,12 @@ Object.defineProperty(Output.prototype, 'data', {
50
69
  return this._data;
51
70
  },
52
71
  });
53
-
72
+ /**
73
+ * Gets the satoshis value of the Output instance.
74
+ * @memberof Output.prototype
75
+ * @name satoshis
76
+ * @type {BN|string|number}
77
+ */
54
78
  Object.defineProperty(Output.prototype, 'satoshis', {
55
79
  configurable: false,
56
80
  enumerable: true,
@@ -73,6 +97,11 @@ Object.defineProperty(Output.prototype, 'satoshis', {
73
97
  },
74
98
  });
75
99
 
100
+ /**
101
+ * Checks if the satoshis value in this output is invalid.
102
+ * @returns {string|boolean} Returns an error message string if invalid (satoshis exceed max safe integer,
103
+ * corrupted value, or negative), otherwise returns false.
104
+ */
76
105
  Output.prototype.invalidSatoshis = function () {
77
106
  if (this._satoshis > MAX_SAFE_INTEGER) {
78
107
  return 'transaction txout satoshis greater than max safe integer';
@@ -86,6 +115,12 @@ Output.prototype.invalidSatoshis = function () {
86
115
  return false;
87
116
  };
88
117
 
118
+ /**
119
+ * Gets the satoshis value as a BN (BigNumber) instance.
120
+ * @memberof Output.prototype
121
+ * @name satoshisBN
122
+ * @type {BN}
123
+ */
89
124
  Object.defineProperty(Output.prototype, 'satoshisBN', {
90
125
  configurable: false,
91
126
  enumerable: true,
@@ -99,6 +134,11 @@ Object.defineProperty(Output.prototype, 'satoshisBN', {
99
134
  },
100
135
  });
101
136
 
137
+ /**
138
+ * Converts the Output instance to a plain object representation.
139
+ * The resulting object includes satoshis, script (as hex string), and data (as hex string).
140
+ * @returns {Object} - An object with satoshis, script, and data properties.
141
+ */
102
142
  Output.prototype.toObject = Output.prototype.toJSON = function toObject() {
103
143
  var obj = {
104
144
  satoshis: this.satoshis,
@@ -108,6 +148,11 @@ Output.prototype.toObject = Output.prototype.toJSON = function toObject() {
108
148
  return obj;
109
149
  };
110
150
 
151
+ /**
152
+ * Sets the output data.
153
+ * @param {Buffer|string} data - The data to set. Can be a Buffer or hex string.
154
+ * @throws {TypeError} If data is not a Buffer or valid hex string.
155
+ */
111
156
  Output.prototype.setData = function (data) {
112
157
  if (!data) {
113
158
  this._data = Buffer.from([])
@@ -122,10 +167,21 @@ Output.prototype.setData = function (data) {
122
167
  }
123
168
  }
124
169
 
170
+ /**
171
+ * Creates an Output instance from a plain JavaScript object.
172
+ * @param {Object} data - The input object to convert to an Output
173
+ * @returns {Output} A new Output instance
174
+ * @static
175
+ */
125
176
  Output.fromObject = function (data) {
126
177
  return new Output(data);
127
178
  };
128
179
 
180
+ /**
181
+ * Sets the script for this output from a buffer.
182
+ * @param {Buffer} buffer - The buffer containing the script data.
183
+ * @throws {errors.Script.InvalidBuffer} If the buffer is invalid.
184
+ */
129
185
  Output.prototype.setScriptFromBuffer = function (buffer) {
130
186
  try {
131
187
  this._script = Script.fromBuffer(buffer);
@@ -139,6 +195,12 @@ Output.prototype.setScriptFromBuffer = function (buffer) {
139
195
  }
140
196
  };
141
197
 
198
+ /**
199
+ * Sets the script for this output.
200
+ * @param {Script|string|Buffer} script - The script to set, which can be a Script instance, hex string, or Buffer.
201
+ * @returns {Output} Returns the output instance for chaining.
202
+ * @throws {TypeError} Throws if the script type is invalid.
203
+ */
142
204
  Output.prototype.setScript = function (script) {
143
205
  if (script instanceof Script) {
144
206
  this._script = script;
@@ -154,6 +216,11 @@ Output.prototype.setScript = function (script) {
154
216
  return this;
155
217
  };
156
218
 
219
+ /**
220
+ * Returns a human-readable string representation of the Output object.
221
+ * Format: '<Output (satoshis sats) scriptString>'
222
+ * @returns {string} Formatted string showing satoshis and script inspection result
223
+ */
157
224
  Output.prototype.inspect = function () {
158
225
  var scriptStr;
159
226
  if (this.script) {
@@ -162,6 +229,13 @@ Output.prototype.inspect = function () {
162
229
  return '<Output (' + this.satoshis + ' sats) ' + scriptStr + '>';
163
230
  };
164
231
 
232
+ /**
233
+ * Creates an Output instance from a BufferReader.
234
+ * @param {BufferReader} br - The buffer reader containing output data
235
+ * @returns {Output} A new Output instance
236
+ * @throws {TypeError} If the buffer contains unrecognized output format
237
+ * @static
238
+ */
165
239
  Output.fromBufferReader = function (br) {
166
240
  var obj = {}
167
241
  obj.satoshis = br.readUInt64LEBN()
@@ -186,8 +260,14 @@ Output.fromBufferReader = function (br) {
186
260
  return new Output(obj)
187
261
  }
188
262
 
263
+ /**
264
+ * Converts the Output instance to a buffer writer format.
265
+ * @param {boolean} hashScriptPubkey - If true, hashes script and data with SHA256; otherwise writes them directly.
266
+ * @param {BufferWriter} [writer] - Optional BufferWriter instance. If not provided, a new one is created.
267
+ * @returns {BufferWriter} The buffer writer containing the serialized output data.
268
+ */
189
269
  Output.prototype.toBufferWriter = function (hashScriptPubkey, writer) {
190
- $.checkArgument(typeof hashScriptPubkey === 'boolean', 'hashScriptSig should be boolean')
270
+ $.checkArgument(typeof hashScriptPubkey === 'boolean', 'hashScriptSig should be boolean')
191
271
  if (!writer) {
192
272
  writer = new BufferWriter();
193
273
  }
@@ -208,13 +288,14 @@ Output.prototype.toBufferWriter = function (hashScriptPubkey, writer) {
208
288
  return writer
209
289
  };
210
290
 
211
- // 8 value
212
- // ??? script size (VARINT)
213
- // ??? script
214
291
  /**
215
292
  * Calculates the total size of the output in bytes.
216
293
  * Includes the script size, data size, and their respective varint sizes,
217
294
  * plus a fixed 8-byte overhead.
295
+ * 8 value
296
+ * ??? script+data size (VARINT)
297
+ * script size
298
+ * data size
218
299
  * @returns {number} The total output size in bytes.
219
300
  */
220
301
  Output.prototype.getSize = function () {
@@ -224,6 +305,10 @@ Output.prototype.getSize = function () {
224
305
  return 8 + varintSize + scriptSize + dataSize
225
306
  };
226
307
 
308
+ /**
309
+ * Creates a shallow clone of the Output instance.
310
+ * @returns {Output} A new Output instance with the same properties as the original.
311
+ */
227
312
  Output.prototype.clone = function () {
228
313
  return Output.fromObject(this.toObject());
229
314
  }
@@ -0,0 +1,187 @@
1
+ 'use strict'
2
+ var Signature = require('../crypto/signature.cjs')
3
+ var Script = require('../script/index.cjs')
4
+ var Output = require('./output.cjs')
5
+ var BufferReader = require('../encoding/bufferreader.cjs')
6
+ var BufferWriter = require('../encoding/bufferwriter.cjs')
7
+ var Hash = require('../crypto/hash.cjs')
8
+ var ECDSA = require('../crypto/ecdsa.cjs')
9
+ var $ = require('../util/preconditions.cjs')
10
+ var _ = require('../util/_.cjs')
11
+
12
+ var SIGHASH_SINGLE_BUG = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
13
+
14
+ /**
15
+ * Represents a Sighash utility for cryptographic signature operations.
16
+ * @constructor
17
+ */
18
+ function Sighash() {
19
+
20
+ };
21
+
22
+ /**
23
+ * Returns a buffer with the which is hashed with sighash that needs to be signed
24
+ * for OP_CHECKSIG.
25
+ *
26
+ * @name Signing.sighash
27
+ * @param {Transaction} transaction the transaction to sign
28
+ * @param {number} sighashType the type of the hash
29
+ * @param {number} inputNumber the input index for the signature
30
+ * @param {Script} subscript the script that will be signed
31
+ * @param {satoshisBN} input's amount (for ForkId signatures)
32
+ *
33
+ */
34
+ Sighash.sighashPreimage = function (transaction, sighashType, inputNumber) {
35
+ // Check that all inputs have an output, prevent shallow transaction
36
+ _.each(transaction.inputs, function (input) {
37
+ $.checkState(input.output instanceof Output, 'input.output must be an instance of Output')
38
+ })
39
+ $.checkArgument(sighashType === Signature.SIGHASH_ALL, 'only SIGHASH_ALL is supported')
40
+ $.checkArgument(inputNumber < transaction.inputs.length, 'inputNumber must be less than the number of inputs')
41
+
42
+ var nVersion
43
+ var prevouts = []
44
+ var spentScriptHash
45
+ var spentDataHash
46
+ var spentAmount
47
+ var sequence
48
+ var spentAmounts = []
49
+ var spentScriptHashes = []
50
+ var spentDataHashes = []
51
+ var sequences = []
52
+ var outputs = []
53
+ var inputIndex
54
+ var nLockTime
55
+ var sighashTypeBuf
56
+
57
+ const getSeparatedScript = function (script) {
58
+ const separatedScript = new Script(script)
59
+ separatedScript.removeCodeseparators()
60
+ return separatedScript
61
+ }
62
+
63
+ // all inputs
64
+ _.each(transaction.inputs, function (input) {
65
+ prevouts.push(input.toPrevout())
66
+ spentAmounts.push(new BufferWriter().writeUInt64LEBN(input.output.satoshisBN).toBuffer())
67
+ spentScriptHashes.push(Hash.sha256(getSeparatedScript(input.output.script).toBuffer()))
68
+ spentDataHashes.push(Hash.sha256(input.output.data))
69
+ sequences.push(new BufferWriter().writeUInt32LE(input.sequenceNumber).toBuffer())
70
+ })
71
+
72
+ // current input
73
+ spentScriptHash = Hash.sha256(getSeparatedScript(transaction.inputs[inputNumber].output.script).toBuffer())
74
+ spentDataHash = Hash.sha256(transaction.inputs[inputNumber].output.data)
75
+ spentAmount = new BufferWriter().writeUInt64LEBN(transaction.inputs[inputNumber].output.satoshisBN).toBuffer()
76
+ sequence = new BufferWriter().writeUInt32LE(transaction.inputs[inputNumber].sequenceNumber).toBuffer()
77
+ inputIndex = new BufferWriter().writeUInt32LE(inputNumber).toBuffer()
78
+ sighashTypeBuf = new BufferWriter().writeUInt32LE(sighashType).toBuffer()
79
+
80
+ // all outputs
81
+ _.each(transaction.outputs, function (output) {
82
+ outputs.push(output.toBufferWriter(true).toBuffer())
83
+ })
84
+
85
+ // tx.version
86
+ nVersion = new BufferWriter().writeUInt32LE(transaction.version).toBuffer()
87
+ // tx.nLockTime
88
+ nLockTime = new BufferWriter().writeUInt32LE(transaction.nLockTime).toBuffer()
89
+
90
+ let bw = new BufferWriter()
91
+
92
+ bw.write(nVersion)
93
+ bw.write(Hash.sha256sha256(Buffer.concat([...prevouts])))
94
+ bw.write(spentScriptHash)
95
+ bw.write(spentDataHash)
96
+ bw.write(spentAmount)
97
+ bw.write(sequence)
98
+
99
+ bw.write(Hash.sha256sha256(Buffer.concat([...spentAmounts])))
100
+ bw.write(Hash.sha256sha256(Buffer.concat([...spentScriptHashes])))
101
+ bw.write(Hash.sha256sha256(Buffer.concat([...spentDataHashes])))
102
+ bw.write(Hash.sha256sha256(Buffer.concat([...sequences])))
103
+ bw.write(Hash.sha256sha256(Buffer.concat([...outputs])))
104
+
105
+ bw.write(inputIndex)
106
+ bw.write(nLockTime)
107
+ bw.write(sighashTypeBuf)
108
+
109
+ return bw.toBuffer()
110
+ }
111
+
112
+ Sighash.getLowSSighashPreimage = function(tx, sigtype, inputIndex) {
113
+ var i = 0;
114
+ do {
115
+ var preimage = Sighash.sighashPreimage(tx, sigtype, inputIndex);
116
+
117
+ var sighash = Hash.sha256sha256(preimage);
118
+
119
+ if (_.isPositiveNumber(sighash.readUInt8()) && _.isPositiveNumber(sighash.readUInt8(31))) {
120
+ return preimage;
121
+ }
122
+
123
+ tx.nLockTime++;
124
+ } while (i < Number.MAX_SAFE_INTEGER);
125
+ }
126
+
127
+
128
+ /**
129
+ * Returns a buffer of length 32 bytes with the hash that needs to be signed
130
+ * for OP_CHECKSIG.
131
+ *
132
+ * @name Signing.sighash
133
+ * @param {Transaction} transaction the transaction to sign
134
+ * @param {number} sighashType the type of the hash
135
+ * @param {number} inputNumber the input index for the signature
136
+ *
137
+ */
138
+ Sighash.sighash = function (transaction, sighashType, inputNumber) {
139
+ var preimage = Sighash.sighashPreimage(transaction, sighashType, inputNumber)
140
+ if (preimage.compare(SIGHASH_SINGLE_BUG) === 0) return preimage
141
+ var ret = Hash.sha256sha256(preimage)
142
+ ret = new BufferReader(ret).readReverse()
143
+ return ret
144
+ }
145
+ /**
146
+ * Create a signature
147
+ *
148
+ * @name Signing.sign
149
+ * @param {Transaction} transaction
150
+ * @param {PrivateKey} privateKey
151
+ * @param {number} sighash
152
+ * @param {number} inputIndex
153
+ * @return {Signature}
154
+ */
155
+ Sighash.sign = function (transaction, privateKey, sighashType, inputIndex) {
156
+ var hashbuf = Sighash.sighash(transaction, sighashType, inputIndex)
157
+
158
+ var sig = ECDSA.sign(hashbuf, privateKey, 'little').set({
159
+ nhashtype: sighashType
160
+ })
161
+ return sig
162
+ }
163
+
164
+ /**
165
+ * Verify a signature
166
+ *
167
+ * @name Signing.verify
168
+ * @param {Transaction} transaction
169
+ * @param {Signature} signature
170
+ * @param {PublicKey} publicKey
171
+ * @param {number} inputIndex
172
+ * @param {Script} subscript
173
+ * @param {satoshisBN} input's amount
174
+ * @param {flags} verification flags
175
+ * @return {boolean}
176
+ */
177
+ Sighash.verify = function (transaction, signature, publicKey, inputIndex) {
178
+ $.checkArgument(!_.isUndefined(transaction))
179
+ $.checkArgument(!_.isUndefined(signature) && !_.isUndefined(signature.nhashtype))
180
+ var hashbuf = Sighash.sighash(transaction, signature.nhashtype, inputIndex)
181
+ return ECDSA.verify(hashbuf, signature, publicKey, 'little')
182
+ }
183
+
184
+ /**
185
+ * @namespace Signing
186
+ */
187
+ module.exports = Sighash
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var _ = require('../util/_');
4
- var $ = require('../util/preconditions');
3
+ var _ = require('../util/_.cjs');
4
+ var $ = require('../util/preconditions.cjs');
5
5
  var inherits = require('inherits');
6
- var JSUtil = require('../util/js');
6
+ var JSUtil = require('../util/js.cjs');
7
7
 
8
- var PublicKey = require('../publickey');
9
- var errors = require('../errors');
10
- var Signature = require('../crypto/signature');
8
+ var PublicKey = require('../publickey.cjs');
9
+ var errors = require('../errors/index.cjs');
10
+ var Signature = require('../crypto/signature.cjs');
11
11
 
12
12
  /**
13
13
  * @desc
@@ -30,6 +30,18 @@ function TransactionSignature(arg) {
30
30
  }
31
31
  inherits(TransactionSignature, Signature);
32
32
 
33
+ /**
34
+ * Initializes the TransactionSignature instance from a plain object.
35
+ * Validates and converts object properties into appropriate types.
36
+ * @param {Object} arg - The source object containing signature data
37
+ * @param {Buffer|string} arg.prevTxId - Previous transaction ID (buffer or hex string)
38
+ * @param {number} arg.outputIndex - Output index
39
+ * @param {number} arg.inputIndex - Input index
40
+ * @param {Signature|Buffer|string} arg.signature - Signature in various formats
41
+ * @param {number} arg.sigtype - Signature type
42
+ * @returns {TransactionSignature} Returns the instance for chaining
43
+ * @private
44
+ */
33
45
  TransactionSignature.prototype._fromObject = function (arg) {
34
46
  this._checkObjectArgs(arg);
35
47
  this.publicKey = new PublicKey(arg.publicKey);
@@ -46,6 +58,18 @@ TransactionSignature.prototype._fromObject = function (arg) {
46
58
  return this;
47
59
  };
48
60
 
61
+ /**
62
+ * Validates the arguments for a transaction signature object.
63
+ * @private
64
+ * @param {Object} arg - The signature object to validate
65
+ * @param {PublicKey} arg.publicKey - The public key
66
+ * @param {number} arg.inputIndex - The input index (must be a number)
67
+ * @param {number} arg.outputIndex - The output index (must be a number)
68
+ * @param {Buffer|string|Signature} arg.signature - The signature (must be Buffer, hex string, or Signature)
69
+ * @param {Buffer|string} arg.prevTxId - Previous transaction ID (must be Buffer or hex string)
70
+ * @param {number} arg.sigtype - Signature type (must be a number)
71
+ * @throws {Error} If any argument fails validation
72
+ */
49
73
  TransactionSignature.prototype._checkObjectArgs = function (arg) {
50
74
  $.checkArgument(PublicKey(arg.publicKey), 'publicKey');
51
75
  $.checkArgument(!_.isUndefined(arg.inputIndex), 'inputIndex');