@opcat-labs/opcat 1.0.1 → 1.0.3

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 +12 -0
  4. package/README.md +6 -0
  5. package/{lib/address.js → cjs/address.cjs} +77 -72
  6. package/cjs/block/block.cjs +332 -0
  7. package/{lib/block/blockheader.js → cjs/block/blockheader.cjs} +8 -7
  8. package/cjs/block/index.cjs +2 -0
  9. package/{lib/block/merkleblock.js → cjs/block/merkleblock.cjs} +23 -15
  10. package/cjs/bn.cjs +3411 -0
  11. package/{lib/crypto/bn.js → cjs/crypto/bn.cjs} +3 -3
  12. package/{lib/crypto/ecdsa.js → cjs/crypto/ecdsa.cjs} +150 -14
  13. package/{lib/crypto/hash.node.js → cjs/crypto/hash.cjs} +13 -2
  14. package/cjs/crypto/index.cjs +16 -0
  15. package/{lib/crypto/point.js → cjs/crypto/point.cjs} +11 -4
  16. package/cjs/crypto/random.cjs +18 -0
  17. package/{lib/crypto/signature.js → cjs/crypto/signature.cjs} +158 -8
  18. package/{lib/encoding/base58.js → cjs/encoding/base58.cjs} +58 -2
  19. package/cjs/encoding/base58check.cjs +192 -0
  20. package/cjs/encoding/bufferreader.cjs +333 -0
  21. package/cjs/encoding/bufferwriter.cjs +244 -0
  22. package/{lib/encoding/decode-asm.js → cjs/encoding/decode-asm.cjs} +4 -4
  23. package/{lib/encoding/decode-hex.js → cjs/encoding/decode-hex.cjs} +1 -1
  24. package/cjs/encoding/index.cjs +14 -0
  25. package/cjs/encoding/varint.cjs +116 -0
  26. package/{lib/errors/index.js → cjs/errors/index.cjs} +9 -9
  27. package/{lib/errors/spec.js → cjs/errors/spec.cjs} +2 -2
  28. package/cjs/hash-cache.cjs +98 -0
  29. package/{lib/hdprivatekey.js → cjs/hdprivatekey.cjs} +232 -140
  30. package/{lib/hdpublickey.js → cjs/hdpublickey.cjs} +120 -93
  31. package/cjs/index.cjs +94 -0
  32. package/cjs/interpreter/index.cjs +2 -0
  33. package/cjs/interpreter/interpreter.cjs +1988 -0
  34. package/{lib/script/stack.js → cjs/interpreter/stack.cjs} +9 -2
  35. package/{lib/message/message.js → cjs/message/message.cjs} +62 -25
  36. package/cjs/mnemonic/index.cjs +3 -0
  37. package/{lib/mnemonic/mnemonic.js → cjs/mnemonic/mnemonic.cjs} +44 -13
  38. package/{lib/mnemonic/pbkdf2.node.js → cjs/mnemonic/pbkdf2.cjs} +9 -2
  39. package/cjs/mnemonic/words/index.cjs +66 -0
  40. package/cjs/network.cjs +13 -0
  41. package/cjs/networks.cjs +321 -0
  42. package/{lib/opcode.js → cjs/opcode.cjs} +69 -5
  43. package/cjs/privatekey.cjs +422 -0
  44. package/{lib/publickey.js → cjs/publickey.cjs} +14 -16
  45. package/cjs/script/index.cjs +2 -0
  46. package/{lib/script/script.js → cjs/script/script.cjs} +322 -67
  47. package/cjs/transaction/index.cjs +5 -0
  48. package/cjs/transaction/input/index.cjs +34 -0
  49. package/cjs/transaction/input/input.cjs +396 -0
  50. package/{lib/transaction/input/multisig.js → cjs/transaction/input/multisig.cjs} +112 -18
  51. package/{lib/transaction/input/publickey.js → cjs/transaction/input/publickey.cjs} +29 -19
  52. package/{lib/transaction/input/publickeyhash.js → cjs/transaction/input/publickeyhash.cjs} +25 -17
  53. package/{lib/transaction/output.js → cjs/transaction/output.cjs} +100 -15
  54. package/cjs/transaction/sighash.cjs +187 -0
  55. package/{lib/transaction/signature.js → cjs/transaction/signature.cjs} +30 -6
  56. package/cjs/transaction/transaction.cjs +2000 -0
  57. package/{lib/transaction/unspentoutput.js → cjs/transaction/unspentoutput.cjs} +5 -5
  58. package/cjs/util/derivation.cjs +53 -0
  59. package/cjs/util/index.cjs +11 -0
  60. package/cjs/util/js.cjs +95 -0
  61. package/{lib/util/preconditions.js → cjs/util/preconditions.cjs} +2 -2
  62. package/esm/address.js +483 -0
  63. package/{lib → esm}/block/block.js +82 -27
  64. package/esm/block/blockheader.js +296 -0
  65. package/esm/block/index.js +2 -0
  66. package/esm/block/merkleblock.js +331 -0
  67. package/esm/bn.js +3411 -0
  68. package/esm/crypto/bn.js +278 -0
  69. package/esm/crypto/ecdsa.js +475 -0
  70. package/{lib/crypto/hash.browser.js → esm/crypto/hash.js} +18 -7
  71. package/esm/crypto/index.js +16 -0
  72. package/esm/crypto/point.js +228 -0
  73. package/esm/crypto/random.js +18 -0
  74. package/esm/crypto/signature.js +475 -0
  75. package/esm/encoding/base58.js +167 -0
  76. package/esm/encoding/base58check.js +192 -0
  77. package/esm/encoding/bufferreader.js +333 -0
  78. package/esm/encoding/bufferwriter.js +243 -0
  79. package/esm/encoding/decode-asm.js +24 -0
  80. package/esm/encoding/decode-hex.js +32 -0
  81. package/esm/encoding/decode-script-chunks.js +43 -0
  82. package/esm/encoding/encode-hex.js +284 -0
  83. package/esm/encoding/index.js +14 -0
  84. package/esm/encoding/is-hex.js +7 -0
  85. package/esm/encoding/varint.js +116 -0
  86. package/esm/errors/index.js +54 -0
  87. package/esm/errors/spec.js +314 -0
  88. package/esm/hash-cache.js +98 -0
  89. package/esm/hdprivatekey.js +768 -0
  90. package/esm/hdpublickey.js +549 -0
  91. package/esm/index.js +66 -0
  92. package/esm/interpreter/index.js +2 -0
  93. package/{lib/script → esm/interpreter}/interpreter.js +219 -66
  94. package/esm/interpreter/stack.js +116 -0
  95. package/esm/message/message.js +228 -0
  96. package/esm/mnemonic/index.js +3 -0
  97. package/esm/mnemonic/mnemonic.js +332 -0
  98. package/{lib/mnemonic/pbkdf2.browser.js → esm/mnemonic/pbkdf2.js} +13 -6
  99. package/esm/mnemonic/words/chinese.js +2054 -0
  100. package/esm/mnemonic/words/english.js +2054 -0
  101. package/esm/mnemonic/words/french.js +2054 -0
  102. package/esm/mnemonic/words/index.js +66 -0
  103. package/esm/mnemonic/words/italian.js +2054 -0
  104. package/esm/mnemonic/words/japanese.js +2054 -0
  105. package/esm/mnemonic/words/spanish.js +2054 -0
  106. package/esm/network.js +13 -0
  107. package/{lib → esm}/networks.js +61 -120
  108. package/esm/opcode.js +319 -0
  109. package/{lib → esm}/privatekey.js +76 -28
  110. package/esm/publickey.js +384 -0
  111. package/esm/script/index.js +2 -0
  112. package/esm/script/script.js +1329 -0
  113. package/esm/script/write-i32-le.js +17 -0
  114. package/esm/script/write-push-data.js +35 -0
  115. package/esm/script/write-u16-le.js +12 -0
  116. package/esm/script/write-u32-le.js +16 -0
  117. package/esm/script/write-u64-le.js +24 -0
  118. package/esm/script/write-u8-le.js +8 -0
  119. package/esm/script/write-varint.js +46 -0
  120. package/esm/transaction/index.js +5 -0
  121. package/esm/transaction/input/index.js +33 -0
  122. package/{lib → esm}/transaction/input/input.js +132 -90
  123. package/esm/transaction/input/multisig.js +335 -0
  124. package/esm/transaction/input/publickey.js +108 -0
  125. package/esm/transaction/input/publickeyhash.js +124 -0
  126. package/esm/transaction/output.js +316 -0
  127. package/{lib → esm}/transaction/sighash.js +42 -22
  128. package/esm/transaction/signature.js +120 -0
  129. package/{lib → esm}/transaction/transaction.js +522 -163
  130. package/esm/transaction/unspentoutput.js +112 -0
  131. package/esm/util/_.js +47 -0
  132. package/esm/util/derivation.js +53 -0
  133. package/esm/util/index.js +12 -0
  134. package/esm/util/js.js +95 -0
  135. package/esm/util/preconditions.js +33 -0
  136. package/fixup.cjs +17 -0
  137. package/package.json +18 -4
  138. package/test/{address.js → address.cjs} +14 -43
  139. package/test/block/{block.js → block.cjs} +3 -5
  140. package/test/block/{blockheader.js → blockheader.cjs} +2 -2
  141. package/test/block/{merklebloack.js → merklebloack.cjs} +2 -2
  142. package/test/crypto/{ecdsa.js → ecdsa.cjs} +9 -9
  143. package/test/crypto/{hash.browser.js → hash.browser.cjs} +2 -1
  144. package/test/crypto/{signature.js → signature.cjs} +2 -2
  145. package/test/data/bitcoind/script_tests.json +5 -5
  146. package/test/{hashCache.js → hashCache.cjs} +2 -1
  147. package/test/{hdkeys.js → hdkeys.cjs} +4 -2
  148. package/test/{hdprivatekey.js → hdprivatekey.cjs} +7 -6
  149. package/test/{hdpublickey.js → hdpublickey.cjs} +2 -7
  150. package/test/mnemonic/{pbkdf2.test.js → pbkdf2.test.cjs} +2 -2
  151. package/test/{networks.js → networks.cjs} +12 -31
  152. package/test/{publickey.js → publickey.cjs} +2 -2
  153. package/test/script/{interpreter.js → interpreter.cjs} +5 -5
  154. package/test/script/{script.js → script.cjs} +8 -2
  155. package/test/transaction/{deserialize.js → deserialize.cjs} +2 -2
  156. package/test/transaction/input/{input.js → input.cjs} +1 -1
  157. package/test/transaction/input/{multisig.js → multisig.cjs} +2 -1
  158. package/test/transaction/input/{publickeyhash.js → publickeyhash.cjs} +1 -1
  159. package/test/transaction/{sighash.js → sighash.cjs} +1 -1
  160. package/test/transaction/{transaction.js → transaction.cjs} +2 -2
  161. package/tsconfig.json +13 -0
  162. package/types/address.d.cts +252 -0
  163. package/types/block/block.d.cts +139 -0
  164. package/types/block/blockheader.d.cts +125 -0
  165. package/types/block/index.d.cts +2 -0
  166. package/types/block/merkleblock.d.cts +95 -0
  167. package/types/bn.d.cts +202 -0
  168. package/types/crypto/bn.d.cts +2 -0
  169. package/types/crypto/ecdsa.d.cts +187 -0
  170. package/types/crypto/hash.d.cts +118 -0
  171. package/types/crypto/index.d.cts +7 -0
  172. package/types/crypto/point.d.cts +134 -0
  173. package/types/crypto/random.d.cts +13 -0
  174. package/types/crypto/signature.d.cts +160 -0
  175. package/types/encoding/base58.d.cts +106 -0
  176. package/types/encoding/base58check.d.cts +107 -0
  177. package/types/encoding/bufferreader.d.cts +164 -0
  178. package/types/encoding/bufferwriter.d.cts +126 -0
  179. package/types/encoding/decode-asm.d.cts +2 -0
  180. package/types/encoding/decode-hex.d.cts +2 -0
  181. package/types/encoding/decode-script-chunks.d.cts +14 -0
  182. package/types/encoding/encode-hex.d.cts +2 -0
  183. package/types/encoding/index.d.cts +6 -0
  184. package/types/encoding/is-hex.d.cts +2 -0
  185. package/types/encoding/varint.d.cts +66 -0
  186. package/types/errors/index.d.cts +4 -0
  187. package/types/errors/spec.d.cts +22 -0
  188. package/types/hash-cache.d.cts +65 -0
  189. package/types/hdprivatekey.d.cts +281 -0
  190. package/types/hdpublickey.d.cts +240 -0
  191. package/types/index.d.cts +26 -0
  192. package/types/interpreter/index.d.cts +2 -0
  193. package/types/interpreter/interpreter.d.cts +228 -0
  194. package/types/interpreter/stack.d.cts +35 -0
  195. package/types/message/message.d.cts +110 -0
  196. package/types/mnemonic/index.d.cts +2 -0
  197. package/types/mnemonic/mnemonic.d.cts +171 -0
  198. package/types/mnemonic/pbkdf2.d.cts +14 -0
  199. package/types/mnemonic/words/chinese.d.cts +2 -0
  200. package/types/mnemonic/words/english.d.cts +2 -0
  201. package/types/mnemonic/words/french.d.cts +2 -0
  202. package/types/mnemonic/words/index.d.cts +22 -0
  203. package/types/mnemonic/words/italian.d.cts +2 -0
  204. package/types/mnemonic/words/japanese.d.cts +2 -0
  205. package/types/mnemonic/words/spanish.d.cts +2 -0
  206. package/types/network.d.cts +11 -0
  207. package/types/networks.d.cts +76 -0
  208. package/types/opcode.d.cts +93 -0
  209. package/types/privatekey.d.cts +169 -0
  210. package/types/publickey.d.cts +202 -0
  211. package/types/script/index.d.cts +2 -0
  212. package/types/script/script.d.cts +449 -0
  213. package/types/script/write-i32-le.d.cts +2 -0
  214. package/types/script/write-push-data.d.cts +2 -0
  215. package/types/script/write-u16-le.d.cts +2 -0
  216. package/types/script/write-u32-le.d.cts +2 -0
  217. package/types/script/write-u64-le.d.cts +2 -0
  218. package/types/script/write-u8-le.d.cts +2 -0
  219. package/types/script/write-varint.d.cts +2 -0
  220. package/types/transaction/index.d.cts +2 -0
  221. package/types/transaction/input/index.d.cts +2 -0
  222. package/types/transaction/input/input.d.cts +178 -0
  223. package/types/transaction/input/multisig.d.cts +127 -0
  224. package/types/transaction/input/publickey.d.cts +44 -0
  225. package/types/transaction/input/publickeyhash.d.cts +45 -0
  226. package/types/transaction/output.d.cts +118 -0
  227. package/types/transaction/sighash.d.cts +61 -0
  228. package/types/transaction/signature.d.cts +43 -0
  229. package/types/transaction/transaction.d.cts +716 -0
  230. package/types/transaction/unspentoutput.d.cts +83 -0
  231. package/types/util/_.d.cts +26 -0
  232. package/types/util/derivation.d.cts +21 -0
  233. package/types/util/index.d.cts +5 -0
  234. package/types/util/js.d.cts +50 -0
  235. package/types/util/preconditions.d.cts +3 -0
  236. package/index.d.ts +0 -1541
  237. package/index.js +0 -74
  238. package/lib/block/index.js +0 -4
  239. package/lib/bn.js +0 -3423
  240. package/lib/crypto/hash.js +0 -2
  241. package/lib/crypto/random.browser.js +0 -28
  242. package/lib/crypto/random.js +0 -2
  243. package/lib/crypto/random.node.js +0 -11
  244. package/lib/encoding/base58check.js +0 -121
  245. package/lib/encoding/bufferreader.js +0 -212
  246. package/lib/encoding/bufferwriter.js +0 -140
  247. package/lib/encoding/varint.js +0 -75
  248. package/lib/hash-cache.js +0 -50
  249. package/lib/mnemonic/pbkdf2.js +0 -2
  250. package/lib/mnemonic/words/index.js +0 -8
  251. package/lib/script/index.js +0 -5
  252. package/lib/transaction/index.js +0 -7
  253. package/lib/transaction/input/index.js +0 -5
  254. package/lib/util/js.js +0 -90
  255. /package/{lib/encoding/decode-script-chunks.js → cjs/encoding/decode-script-chunks.cjs} +0 -0
  256. /package/{lib/encoding/encode-hex.js → cjs/encoding/encode-hex.cjs} +0 -0
  257. /package/{lib/encoding/is-hex.js → cjs/encoding/is-hex.cjs} +0 -0
  258. /package/{lib/mnemonic/words/chinese.js → cjs/mnemonic/words/chinese.cjs} +0 -0
  259. /package/{lib/mnemonic/words/english.js → cjs/mnemonic/words/english.cjs} +0 -0
  260. /package/{lib/mnemonic/words/french.js → cjs/mnemonic/words/french.cjs} +0 -0
  261. /package/{lib/mnemonic/words/italian.js → cjs/mnemonic/words/italian.cjs} +0 -0
  262. /package/{lib/mnemonic/words/japanese.js → cjs/mnemonic/words/japanese.cjs} +0 -0
  263. /package/{lib/mnemonic/words/spanish.js → cjs/mnemonic/words/spanish.cjs} +0 -0
  264. /package/{lib/script/write-i32-le.js → cjs/script/write-i32-le.cjs} +0 -0
  265. /package/{lib/script/write-push-data.js → cjs/script/write-push-data.cjs} +0 -0
  266. /package/{lib/script/write-u16-le.js → cjs/script/write-u16-le.cjs} +0 -0
  267. /package/{lib/script/write-u32-le.js → cjs/script/write-u32-le.cjs} +0 -0
  268. /package/{lib/script/write-u64-le.js → cjs/script/write-u64-le.cjs} +0 -0
  269. /package/{lib/script/write-u8-le.js → cjs/script/write-u8-le.cjs} +0 -0
  270. /package/{lib/script/write-varint.js → cjs/script/write-varint.cjs} +0 -0
  271. /package/{lib/util/_.js → cjs/util/_.cjs} +0 -0
  272. /package/test/crypto/{bn.js → bn.cjs} +0 -0
  273. /package/test/crypto/{hash.js → hash.cjs} +0 -0
  274. /package/test/crypto/{point.js → point.cjs} +0 -0
  275. /package/test/crypto/{random.js → random.cjs} +0 -0
  276. /package/test/data/{blk86756-testnet.js → blk86756-testnet.cjs} +0 -0
  277. /package/test/data/{merkleblocks.js → merkleblocks.cjs} +0 -0
  278. /package/test/encoding/{base58.js → base58.cjs} +0 -0
  279. /package/test/encoding/{base58check.js → base58check.cjs} +0 -0
  280. /package/test/encoding/{bufferreader.js → bufferreader.cjs} +0 -0
  281. /package/test/encoding/{bufferwriter.js → bufferwriter.cjs} +0 -0
  282. /package/test/encoding/{varint.js → varint.cjs} +0 -0
  283. /package/test/{index.js → index.cjs} +0 -0
  284. /package/test/message/{message.js → message.cjs} +0 -0
  285. /package/test/mnemonic/{mnemonic.js → mnemonic.cjs} +0 -0
  286. /package/test/{opcode.js → opcode.cjs} +0 -0
  287. /package/test/{privatekey.js → privatekey.cjs} +0 -0
  288. /package/test/transaction/input/{publickey.js → publickey.cjs} +0 -0
  289. /package/test/transaction/{output.js → output.cjs} +0 -0
  290. /package/test/transaction/{signature.js → signature.cjs} +0 -0
  291. /package/test/transaction/{unspentoutput.js → unspentoutput.cjs} +0 -0
  292. /package/test/util/{js.js → js.cjs} +0 -0
  293. /package/test/util/{preconditions.js → preconditions.cjs} +0 -0
@@ -0,0 +1,5 @@
1
+ var Transaction = require('./transaction.cjs');
2
+
3
+ module.exports = Transaction
4
+
5
+
@@ -0,0 +1,34 @@
1
+
2
+ var Input = require('./input.cjs');
3
+
4
+ var PublicKeyInput = require('./publickey.cjs');
5
+ var PublicKeyHashInput = require('./publickeyhash.cjs');
6
+ var MultiSigInput = require('./multisig.cjs');
7
+
8
+
9
+ /**
10
+ * Attaches the PublicKeyInput class to the Input namespace.
11
+ * @memberof Input
12
+ * @name PublicKey
13
+ * @alias PublicKeyInput
14
+ */
15
+ Input.PublicKey = PublicKeyInput;
16
+
17
+
18
+ /**
19
+ * Attaches the PublicKeyHashInput class to the Input namespace.
20
+ * @memberof Input
21
+ * @name PublicKeyHash
22
+ * @alias PublicKeyHashInput
23
+ */
24
+ Input.PublicKeyHash = PublicKeyHashInput;
25
+ /**
26
+ * Attaches the PublicKeyHashInput class to the Input namespace.
27
+ * @memberof Input
28
+ * @name MultiSig
29
+ * @alias MultiSigInput
30
+ */
31
+ Input.MultiSig = MultiSigInput;
32
+
33
+
34
+ module.exports = Input
@@ -0,0 +1,396 @@
1
+ 'use strict';
2
+
3
+ var _ = require('../../util/_.cjs');
4
+ var $ = require('../../util/preconditions.cjs');
5
+ var errors = require('../../errors/index.cjs');
6
+ var BufferWriter = require('../../encoding/bufferwriter.cjs');
7
+ var JSUtil = require('../../util/js.cjs');
8
+ var Script = require('../../script/index.cjs');
9
+ var Sighash = require('../sighash.cjs');
10
+ var Output = require('../output.cjs');
11
+ var Signature = require('../../crypto/signature.cjs');
12
+ var TransactionSignature = require('../signature.cjs');
13
+ var Hash = require('../../crypto/hash.cjs');
14
+ var PrivateKey = require('../../privatekey.cjs');
15
+
16
+ var MAXINT = 0xffffffff; // Math.pow(2, 32) - 1;
17
+ var DEFAULT_RBF_SEQNUMBER = MAXINT - 2;
18
+ var DEFAULT_SEQNUMBER = MAXINT;
19
+ var DEFAULT_LOCKTIME_SEQNUMBER = MAXINT - 1;
20
+
21
+ /**
22
+ * Creates an Input instance from parameters.
23
+ * @constructor
24
+ * @param {Object} params - Input parameters object
25
+ * @param {string|Buffer} params.prevTxId - Previous transaction ID (hex string or Buffer)
26
+ * @param {number} params.outputIndex - Output index in previous transaction
27
+ * @param {Output} [params.output] - Output instance or output parameters
28
+ * @param {number} [params.sequenceNumber] - Sequence number (defaults to DEFAULT_SEQNUMBER)
29
+ * @param {Script|Buffer|string} [params.script] - Script instance, buffer or hex string
30
+ * @returns {Input} New Input instance or initialized instance if params provided.
31
+ */
32
+ function Input(params) {
33
+ if (!(this instanceof Input)) {
34
+ return new Input(params);
35
+ }
36
+ if (params) {
37
+ return this._fromObject(params);
38
+ }
39
+ }
40
+
41
+ Input.MAXINT = MAXINT;
42
+ Input.DEFAULT_SEQNUMBER = DEFAULT_SEQNUMBER;
43
+ Input.DEFAULT_LOCKTIME_SEQNUMBER = DEFAULT_LOCKTIME_SEQNUMBER;
44
+ Input.DEFAULT_RBF_SEQNUMBER = DEFAULT_RBF_SEQNUMBER;
45
+ // txid + output index + sequence number
46
+ Input.BASE_SIZE = 32 + 4 + 4;
47
+
48
+ /**
49
+ * Gets or sets the script associated with this input.
50
+ * @memberof Input.prototype
51
+ * @name script
52
+ * @return {Script}
53
+ */
54
+ Object.defineProperty(Input.prototype, 'script', {
55
+ configurable: false,
56
+ enumerable: true,
57
+ get: function () {
58
+ if (this.isNull()) {
59
+ return null;
60
+ }
61
+ if (!this._script) {
62
+ this._script = new Script(this._scriptBuffer);
63
+ this._script._isInput = true;
64
+ }
65
+ return this._script;
66
+ },
67
+ });
68
+
69
+ /**
70
+ * Creates an Input instance from a plain JavaScript object.
71
+ * @param {Object} params - Input parameters object
72
+ * @param {string|Buffer} params.prevTxId - Previous transaction ID (hex string or Buffer)
73
+ * @param {number} params.outputIndex - Output index in previous transaction
74
+ * @param {Output} [params.output] - Output instance or output parameters
75
+ * @param {number} [params.sequenceNumber] - Sequence number (defaults to DEFAULT_SEQNUMBER)
76
+ * @param {Script|Buffer|string} [params.script] - Script instance, buffer or hex string
77
+ * @returns {Input} The created Input instance.
78
+ * @throws {Error} Will throw if the argument is not an object.
79
+ */
80
+ Input.fromObject = function (params) {
81
+ $.checkArgument(_.isObject(params));
82
+ var input = new Input();
83
+ return input._fromObject(params);
84
+ };
85
+
86
+ /**
87
+ * Creates an Input instance from an object containing transaction input parameters.
88
+ * Validates required fields (prevTxId, outputIndex) and converts hex strings to Buffers.
89
+ * Handles optional parameters with defaults (sequenceNumber, script).
90
+ * @param {Object} params - Input parameters object
91
+ * @param {string|Buffer} params.prevTxId - Previous transaction ID (hex string or Buffer)
92
+ * @param {number} params.outputIndex - Output index in previous transaction
93
+ * @param {Output} [params.output] - Output instance or output parameters
94
+ * @param {number} [params.sequenceNumber] - Sequence number (defaults to DEFAULT_SEQNUMBER)
95
+ * @param {Script|Buffer|string} [params.script] - Script instance, buffer or hex string
96
+ * @throws {errors.Transaction.Input.InvalidParams} If required params are missing
97
+ * @returns {Input} Returns the Input instance for chaining
98
+ * @private
99
+ */
100
+ Input.prototype._fromObject = function (params) {
101
+
102
+ if(_.isUndefined(params.prevTxId)
103
+ || _.isUndefined(params.outputIndex)) {
104
+ throw new errors.Transaction.Input.InvalidParams('require prevTxId and outputIndex');
105
+ }
106
+ var prevTxId;
107
+ if (_.isString(params.prevTxId) && JSUtil.isHexa(params.prevTxId)) {
108
+ prevTxId = Buffer.from(params.prevTxId, 'hex');
109
+ } else {
110
+ prevTxId = params.prevTxId;
111
+ }
112
+ this.output = params.output
113
+ ? params.output instanceof Output
114
+ ? params.output
115
+ : new Output(params.output)
116
+ : undefined;
117
+ this.prevTxId = prevTxId || params.txidbuf;
118
+ this.outputIndex = _.isUndefined(params.outputIndex) ? params.txoutnum : params.outputIndex;
119
+ this.sequenceNumber = _.isUndefined(params.sequenceNumber)
120
+ ? _.isUndefined(params.seqnum)
121
+ ? DEFAULT_SEQNUMBER
122
+ : params.seqnum
123
+ : params.sequenceNumber;
124
+ this.setScript(params.scriptBuffer || params.script || Script.empty());
125
+ return this;
126
+ };
127
+
128
+
129
+ /**
130
+ * Converts the Input instance to a plain object for JSON serialization.
131
+ * Includes prevTxId, outputIndex, sequenceNumber, and script as hex strings.
132
+ * Optionally adds human-readable scriptString if script is valid,
133
+ * and includes the output object if present.
134
+ * @returns {Object} A plain object representation of the Input.
135
+ */
136
+ Input.prototype.toObject = Input.prototype.toJSON = function toObject() {
137
+ var obj = {
138
+ prevTxId: this.prevTxId.toString('hex'),
139
+ outputIndex: this.outputIndex,
140
+ sequenceNumber: this.sequenceNumber,
141
+ script: this._scriptBuffer.toString('hex'),
142
+ };
143
+ // add human readable form if input contains valid script
144
+ if (this.script) {
145
+ obj.scriptString = this.script.toString();
146
+ }
147
+ if (this.output) {
148
+ obj.output = this.output.toObject();
149
+ }
150
+ return obj;
151
+ };
152
+
153
+ /**
154
+ * Creates an Input instance from a BufferReader.
155
+ * @param {BufferReader} br - The buffer reader containing input data.
156
+ * @returns {Input} The parsed Input object with properties:
157
+ * - prevTxId: Reversed 32-byte previous transaction ID.
158
+ * - outputIndex: LE uint32 output index.
159
+ * - _scriptBuffer: Var-length script buffer.
160
+ * - sequenceNumber: LE uint32 sequence number.
161
+ * @note TODO: Return specialized input types (CoinbaseInput, PublicKeyHashInput, etc.).
162
+ * @static
163
+ */
164
+ Input.fromBufferReader = function (br) {
165
+ var input = new Input();
166
+ input.prevTxId = br.readReverse(32);
167
+ input.outputIndex = br.readUInt32LE();
168
+ input._scriptBuffer = br.readVarLengthBuffer();
169
+ input.sequenceNumber = br.readUInt32LE();
170
+ // TODO: return different classes according to which input it is
171
+ // e.g: CoinbaseInput, PublicKeyHashInput, etc.
172
+ return input;
173
+ };
174
+
175
+ /**
176
+ * Serializes the input to a BufferWriter.
177
+ * @param {boolean} hashScriptSig - Whether to hash the script (true) or include it directly (false).
178
+ * @param {BufferWriter} [writer] - Optional BufferWriter instance to write to.
179
+ * @returns {BufferWriter} The BufferWriter containing the serialized input.
180
+ */
181
+ Input.prototype.toBufferWriter = function (hashScriptSig, writer) {
182
+ $.checkArgument(typeof hashScriptSig === 'boolean', 'hashScriptSig should be boolean')
183
+ if (!writer) {
184
+ writer = new BufferWriter()
185
+ }
186
+ writer.writeReverse(this.prevTxId)
187
+ writer.writeUInt32LE(this.outputIndex)
188
+ var script = this._scriptBuffer
189
+ if(hashScriptSig) {
190
+ writer.write(Hash.sha256(script))
191
+ } else {
192
+ writer.writeVarintNum(script.length)
193
+ writer.write(script)
194
+ }
195
+ writer.writeUInt32LE(this.sequenceNumber)
196
+ return writer
197
+ }
198
+
199
+
200
+ /**
201
+ * Converts the input to a prevout format (txid + output index) as a buffer.
202
+ * @returns {Buffer} The serialized prevout data.
203
+ */
204
+ Input.prototype.toPrevout = function () {
205
+ let writer = new BufferWriter()
206
+ writer.writeReverse(this.prevTxId)
207
+ writer.writeUInt32LE(this.outputIndex)
208
+ return writer.toBuffer()
209
+ }
210
+
211
+
212
+ /**
213
+ * Sets the script for this input.
214
+ * @param {Script|string|Buffer|null} script - Can be a Script object, hex string, human-readable string, Buffer, or null (for empty script)
215
+ * @returns {Input} Returns the Input instance for chaining
216
+ * @throws {TypeError} If script is of invalid type
217
+ */
218
+ Input.prototype.setScript = function (script) {
219
+ this._script = null;
220
+ if (script instanceof Script) {
221
+ this._script = script;
222
+ this._script._isInput = true;
223
+ this._scriptBuffer = script.toBuffer();
224
+ } else if (script === null) {
225
+ this._script = Script.empty();
226
+ this._script._isInput = true;
227
+ this._scriptBuffer = this._script.toBuffer();
228
+ } else if (JSUtil.isHexa(script)) {
229
+ // hex string script
230
+ this._scriptBuffer = Buffer.from(script, 'hex');
231
+ } else if (_.isString(script)) {
232
+ // human readable string script
233
+ this._script = new Script(script);
234
+ this._script._isInput = true;
235
+ this._scriptBuffer = this._script.toBuffer();
236
+ } else if (Buffer.isBuffer(script)) {
237
+ // buffer script
238
+ this._scriptBuffer = Buffer.from(script);
239
+ } else {
240
+ throw new TypeError('Invalid argument type: script');
241
+ }
242
+ return this;
243
+ };
244
+
245
+ /**
246
+ * Retrieve signatures for the provided PrivateKey.
247
+ *
248
+ * @param {Transaction} transaction - the transaction to be signed
249
+ * @param {PrivateKey | Array} privateKeys - the private key to use when signing
250
+ * @param {number} inputIndex - the index of this input in the provided transaction
251
+ * @param {number} sigType - defaults to Signature.SIGHASH_ALL
252
+ */
253
+ Input.prototype.getSignatures = function (transaction, privateKeys, inputIndex, sigtype) {
254
+ $.checkState(this.output instanceof Output);
255
+ sigtype = sigtype || Signature.SIGHASH_ALL;
256
+ var results = [];
257
+ if (privateKeys instanceof PrivateKey) {
258
+ results.push(
259
+ new TransactionSignature({
260
+ publicKey: privateKeys.publicKey,
261
+ prevTxId: this.prevTxId,
262
+ outputIndex: this.outputIndex,
263
+ inputIndex: inputIndex,
264
+ signature: Sighash.sign(
265
+ transaction,
266
+ privateKeys,
267
+ sigtype,
268
+ inputIndex,
269
+ ),
270
+ sigtype: sigtype,
271
+ }),
272
+ );
273
+ } else if (_.isArray(privateKeys)) {
274
+ var self = this;
275
+
276
+ _.each(privateKeys, function (privateKey, index) {
277
+ var sigtype_ = sigtype;
278
+ if (_.isArray(sigtype)) {
279
+ sigtype_ = sigtype[index] || Signature.SIGHASH_ALL;
280
+ }
281
+ results.push(
282
+ new TransactionSignature({
283
+ publicKey: privateKey.publicKey,
284
+ prevTxId: self.prevTxId,
285
+ outputIndex: self.outputIndex,
286
+ inputIndex: inputIndex,
287
+ signature: Sighash.sign(
288
+ transaction,
289
+ privateKey,
290
+ sigtype_,
291
+ inputIndex,
292
+ ),
293
+ sigtype: sigtype_,
294
+ }),
295
+ );
296
+ });
297
+ }
298
+ return results;
299
+ };
300
+
301
+ /**
302
+ * Retrieve preimage for the Input.
303
+ *
304
+ * @param {Transaction} transaction - the transaction to be signed
305
+ * @param {number} inputIndex - the index of this input in the provided transaction
306
+ * @param {number} sigType - defaults to Signature.SIGHASH_ALL
307
+ * @param {boolean} isLowS - true if the sig hash is safe for low s.
308
+ */
309
+ Input.prototype.getPreimage = function (transaction, inputIndex, sigtype, isLowS) {
310
+ $.checkState(this.output instanceof Output);
311
+ sigtype = sigtype || Signature.SIGHASH_ALL;
312
+ isLowS = isLowS || false;
313
+ return isLowS
314
+ ? Sighash.getLowSSighashPreimage(transaction, sigtype, inputIndex)
315
+ : Sighash.sighashPreimage(transaction, sigtype, inputIndex);
316
+ };
317
+
318
+ /**
319
+ * Abstract method that throws an error when invoked. Must be implemented by subclasses
320
+ * to determine if all required signatures are present on this input.
321
+ * @throws {AbstractMethodInvoked} Always throws to indicate abstract method usage
322
+ * @abstract
323
+ */
324
+ Input.prototype.isFullySigned = function () {
325
+ throw new errors.AbstractMethodInvoked('Input#isFullySigned');
326
+ };
327
+
328
+ /**
329
+ * Checks if the input is final (has maximum sequence number).
330
+ * @returns {boolean} True if the input is final, false otherwise.
331
+ */
332
+ Input.prototype.isFinal = function () {
333
+ return this.sequenceNumber === Input.MAXINT;
334
+ };
335
+
336
+ /**
337
+ * Abstract method to add a signature to the transaction input.
338
+ * Must be implemented by concrete input types.
339
+ * @param {Object} transaction - The transaction to sign
340
+ * @param {Object} signature - The signature to add
341
+ * @abstract
342
+ */
343
+ Input.prototype.addSignature = function (_transaction, _signature) {
344
+ };
345
+
346
+ /**
347
+ * Clears all signatures from the input.
348
+ * @abstract
349
+ */
350
+ Input.prototype.clearSignatures = function () {
351
+
352
+ };
353
+
354
+ /**
355
+ * Verifies if a signature is valid for this input in the given transaction.
356
+ * Note: Temporarily modifies the signature object by setting nhashtype from sigtype.
357
+ *
358
+ * @param {Object} transaction - The transaction to verify against
359
+ * @param {TransactionSignature} signature - Signature object containing signature, publicKey, etc.
360
+ * @returns {boolean} True if the signature is valid, false otherwise
361
+ */
362
+ Input.prototype.isValidSignature = function (transaction, signature) {
363
+ // FIXME: Refactor signature so this is not necessary
364
+ signature.signature.nhashtype = signature.sigtype;
365
+ return Sighash.verify(
366
+ transaction,
367
+ signature.signature,
368
+ signature.publicKey,
369
+ signature.inputIndex,
370
+ this.output.script,
371
+ this.output.satoshisBN,
372
+ );
373
+ };
374
+
375
+ /**
376
+ * @returns true if this is a coinbase input (represents no input)
377
+ */
378
+ Input.prototype.isNull = function () {
379
+ return (
380
+ this.prevTxId.toString('hex') ===
381
+ '0000000000000000000000000000000000000000000000000000000000000000' &&
382
+ this.outputIndex === 0xffffffff
383
+ );
384
+ };
385
+
386
+ /**
387
+ * Estimates the size of the input in bytes by converting it to a buffer.
388
+ * @returns {number} The length of the serialized buffer in bytes.
389
+ * @private
390
+ */
391
+ Input.prototype._estimateSize = function () {
392
+ return this.toBufferWriter(false).toBuffer().length;
393
+ };
394
+
395
+
396
+ module.exports = Input;
@@ -1,20 +1,27 @@
1
1
  'use strict';
2
2
 
3
- var _ = require('../../util/_');
3
+ var _ = require('../../util/_.cjs');
4
4
  var inherits = require('inherits');
5
- var Input = require('./input');
6
- var Output = require('../output');
7
- var $ = require('../../util/preconditions');
5
+ var Input = require('./input.cjs');
6
+ var Output = require('../output.cjs');
7
+ var $ = require('../../util/preconditions.cjs');
8
+
9
+ var Script = require('../../script/index.cjs');
10
+ var Signature = require('../../crypto/signature.cjs');
11
+ var Sighash = require('../sighash.cjs');
12
+ var TransactionSignature = require('../signature.cjs');
13
+ var PublicKey = require('../../publickey.cjs');
14
+ var Varint = require('../../encoding/varint.cjs');
8
15
 
9
- var Script = require('../../script');
10
- var Signature = require('../../crypto/signature');
11
- var Sighash = require('../sighash');
12
- var TransactionSignature = require('../signature');
13
- var PublicKey = require('../../publickey');
14
- var Varint = require('../../encoding/varint');
15
16
 
16
17
  /**
18
+ * Represents a MultiSigInput for a transaction.
17
19
  * @constructor
20
+ * @param {Object} input - The input object containing publicKeys, threshold, and signatures.
21
+ * @param {Array} pubkeys - Array of public keys (optional, defaults to input.publicKeys).
22
+ * @param {number} threshold - Required number of signatures (optional, defaults to input.threshold).
23
+ * @param {Array} signatures - Array of signatures (optional, defaults to input.signatures).
24
+ * @description Validates that provided public keys match the output script and initializes signatures.
18
25
  */
19
26
  function MultiSigInput(input, pubkeys, threshold, signatures) {
20
27
  Input.apply(this, arguments);
@@ -42,6 +49,11 @@ function MultiSigInput(input, pubkeys, threshold, signatures) {
42
49
  }
43
50
  inherits(MultiSigInput, Input);
44
51
 
52
+ /**
53
+ * Converts the MultiSigInput instance to a plain object representation.
54
+ * Includes threshold, publicKeys (converted to strings), and serialized signatures.
55
+ * @returns {Object} The plain object representation of the MultiSigInput.
56
+ */
45
57
  MultiSigInput.prototype.toObject = function () {
46
58
  var obj = Input.prototype.toObject.apply(this, arguments);
47
59
  obj.threshold = this.threshold;
@@ -52,6 +64,12 @@ MultiSigInput.prototype.toObject = function () {
52
64
  return obj;
53
65
  };
54
66
 
67
+ /**
68
+ * Deserializes an array of signature strings into TransactionSignature objects.
69
+ * @private
70
+ * @param {Array<string>} signatures - Array of signature strings to deserialize
71
+ * @returns {Array<TransactionSignature|undefined>} Array of TransactionSignature objects (undefined for null/empty signatures)
72
+ */
55
73
  MultiSigInput.prototype._deserializeSignatures = function (signatures) {
56
74
  return _.map(signatures, function (signature) {
57
75
  if (!signature) {
@@ -61,6 +79,11 @@ MultiSigInput.prototype._deserializeSignatures = function (signatures) {
61
79
  });
62
80
  };
63
81
 
82
+ /**
83
+ * Serializes the signatures array by converting each signature to a plain object.
84
+ * @returns {Array<Object|undefined>} An array of signature objects or undefined values.
85
+ * @private
86
+ */
64
87
  MultiSigInput.prototype._serializeSignatures = function () {
65
88
  return _.map(this.signatures, function (signature) {
66
89
  if (!signature) {
@@ -70,6 +93,16 @@ MultiSigInput.prototype._serializeSignatures = function () {
70
93
  });
71
94
  };
72
95
 
96
+ /**
97
+ * Gets signatures for a MultiSigInput by signing the transaction with the provided private key.
98
+ * Only signs for public keys that match the private key's public key.
99
+ *
100
+ * @param {Transaction} transaction - The transaction to sign
101
+ * @param {PrivateKey} privateKey - The private key used for signing
102
+ * @param {number} index - The input index
103
+ * @param {number} [sigtype=Signature.SIGHASH_ALL] - The signature type
104
+ * @returns {TransactionSignature[]} Array of transaction signatures
105
+ */
73
106
  MultiSigInput.prototype.getSignatures = function (transaction, privateKey, index, sigtype) {
74
107
  $.checkState(this.output instanceof Output);
75
108
  sigtype = sigtype || Signature.SIGHASH_ALL;
@@ -99,6 +132,13 @@ MultiSigInput.prototype.getSignatures = function (transaction, privateKey, index
99
132
  return results;
100
133
  };
101
134
 
135
+ /**
136
+ * Adds a signature to the MultiSigInput if valid and not already fully signed.
137
+ * @param {Object} transaction - The transaction to validate the signature against.
138
+ * @param {Object} signature - The signature object containing publicKey and signature data.
139
+ * @throws {Error} If already fully signed, no matching public key, or invalid signature.
140
+ * @returns {MultiSigInput} Returns the instance for chaining.
141
+ */
102
142
  MultiSigInput.prototype.addSignature = function (transaction, signature) {
103
143
  $.checkState(!this.isFullySigned(), 'All needed signatures have already been added');
104
144
  $.checkArgument(
@@ -111,11 +151,21 @@ MultiSigInput.prototype.addSignature = function (transaction, signature) {
111
151
  return this;
112
152
  };
113
153
 
154
+ /**
155
+ * Updates the multisig input script by rebuilding it with current public keys, threshold, and signatures.
156
+ * @returns {MultiSigInput} Returns the instance for chaining.
157
+ */
114
158
  MultiSigInput.prototype._updateScript = function () {
115
159
  this.setScript(Script.buildMultisigIn(this.publicKeys, this.threshold, this._createSignatures()));
116
160
  return this;
117
161
  };
118
162
 
163
+ /**
164
+ * Creates DER-encoded signatures from the input's signature data.
165
+ * Filters out undefined signatures and converts each valid signature to a Buffer
166
+ * containing the DER-encoded signature followed by its sigtype byte.
167
+ * @returns {Buffer[]} Array of signature Buffers
168
+ */
119
169
  MultiSigInput.prototype._createSignatures = function () {
120
170
  return _.map(
121
171
  _.filter(this.signatures, function (signature) {
@@ -127,19 +177,36 @@ MultiSigInput.prototype._createSignatures = function () {
127
177
  );
128
178
  };
129
179
 
180
+ /**
181
+ * Clears all signatures from the MultiSigInput by resetting the signatures array
182
+ * and updating the script. The signatures array length matches the publicKeys array.
183
+ */
130
184
  MultiSigInput.prototype.clearSignatures = function () {
131
185
  this.signatures = new Array(this.publicKeys.length);
132
186
  this._updateScript();
133
187
  };
134
188
 
189
+ /**
190
+ * Checks if the MultiSigInput is fully signed by comparing the number of signatures
191
+ * with the required threshold.
192
+ * @returns {boolean} True if the input has enough signatures, false otherwise.
193
+ */
135
194
  MultiSigInput.prototype.isFullySigned = function () {
136
195
  return this.countSignatures() === this.threshold;
137
196
  };
138
197
 
198
+ /**
199
+ * Returns the number of missing signatures required to meet the threshold.
200
+ * @returns {number} The count of missing signatures.
201
+ */
139
202
  MultiSigInput.prototype.countMissingSignatures = function () {
140
203
  return this.threshold - this.countSignatures();
141
204
  };
142
205
 
206
+ /**
207
+ * Counts the number of valid signatures in the MultiSigInput.
208
+ * @returns {number} The count of non-null/undefined signatures.
209
+ */
143
210
  MultiSigInput.prototype.countSignatures = function () {
144
211
  return _.reduce(
145
212
  this.signatures,
@@ -150,6 +217,10 @@ MultiSigInput.prototype.countSignatures = function () {
150
217
  );
151
218
  };
152
219
 
220
+ /**
221
+ * Returns an array of public keys that haven't been signed yet in this MultiSigInput.
222
+ * @returns {Array} Array of unsigned public keys
223
+ */
153
224
  MultiSigInput.prototype.publicKeysWithoutSignature = function () {
154
225
  var self = this;
155
226
  return _.filter(this.publicKeys, function (publicKey) {
@@ -157,8 +228,19 @@ MultiSigInput.prototype.publicKeysWithoutSignature = function () {
157
228
  });
158
229
  };
159
230
 
231
+ /**
232
+ * Verifies a signature for a MultiSigInput transaction.
233
+ *
234
+ * @param {Object} transaction - The transaction to verify.
235
+ * @param {Object} signature - The signature object containing signature data.
236
+ * @param {Buffer} signature.signature - The signature to verify.
237
+ * @param {Buffer} signature.publicKey - The public key corresponding to the signature.
238
+ * @param {number} signature.inputIndex - The index of the input being signed.
239
+ * @param {number} signature.sigtype - The signature type (assigned to nhashtype as a workaround).
240
+ * @returns {boolean} True if the signature is valid, false otherwise.
241
+ */
160
242
  MultiSigInput.prototype.isValidSignature = function (transaction, signature) {
161
- // FIXME: Refactor signature so this is not necessary
243
+ // FIXME: Refactor signature so this is not necessary
162
244
  signature.signature.nhashtype = signature.sigtype;
163
245
  return Sighash.verify(
164
246
  transaction,
@@ -171,13 +253,15 @@ MultiSigInput.prototype.isValidSignature = function (transaction, signature) {
171
253
  };
172
254
 
173
255
  /**
174
- *
175
- * @param {Buffer[]} signatures
176
- * @param {PublicKey[]} publicKeys
177
- * @param {Transaction} transaction
178
- * @param {Integer} inputIndex
179
- * @param {Input} input
180
- * @returns {TransactionSignature[]}
256
+ * Normalizes signatures for a MultiSigInput by matching each public key with its corresponding signature.
257
+ * Filters and validates signatures against the provided public keys and transaction.
258
+ *
259
+ * @param {Object} transaction - The transaction to verify against.
260
+ * @param {Object} input - The input containing prevTxId and outputIndex.
261
+ * @param {number} inputIndex - The index of the input in the transaction.
262
+ * @param {Array<Buffer>} signatures - Array of signature buffers to normalize.
263
+ * @param {Array<PublicKey>} publicKeys - Array of public keys to match signatures against.
264
+ * @returns {Array<TransactionSignature|null>} Array of matched signatures or null for unmatched keys.
181
265
  */
182
266
  MultiSigInput.normalizeSignatures = function (
183
267
  transaction,
@@ -232,8 +316,18 @@ MultiSigInput.normalizeSignatures = function (
232
316
  // <=72 signature (DER + SIGHASH type)
233
317
  //
234
318
  // 4 sequence number
319
+ /**
320
+ * The byte size of a single signature in a MultiSig input.
321
+ * @constant
322
+ */
235
323
  MultiSigInput.SIGNATURE_SIZE = 73;
236
324
 
325
+ /**
326
+ * Estimates the byte size of a MultiSigInput, including the base input size,
327
+ * script size (with threshold-based signature count), and varint overhead.
328
+ * @returns {number} The estimated size in bytes.
329
+ * @private
330
+ */
237
331
  MultiSigInput.prototype._estimateSize = function () {
238
332
  var scriptSize = 1 + this.threshold * MultiSigInput.SIGNATURE_SIZE;
239
333
  return Input.BASE_SIZE + Varint(scriptSize).toBuffer().length + scriptSize;