@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,335 @@
1
+ 'use strict';
2
+
3
+ import _ from '../../util/_.js';
4
+ import inherits from 'inherits';
5
+ import Input from './input.js';
6
+ import Output from '../output.js';
7
+ import $ from '../../util/preconditions.js';
8
+ import Script from '../../script/index.js';
9
+ import Signature from '../../crypto/signature.js';
10
+ import Sighash from '../sighash.js';
11
+ import TransactionSignature from '../signature.js';
12
+ import PublicKey from '../../publickey.js';
13
+ import Varint from '../../encoding/varint.js';
14
+
15
+
16
+ /**
17
+ * Represents a MultiSigInput for a transaction.
18
+ * @constructor
19
+ * @param {Object} input - The input object containing publicKeys, threshold, and signatures.
20
+ * @param {Array} pubkeys - Array of public keys (optional, defaults to input.publicKeys).
21
+ * @param {number} threshold - Required number of signatures (optional, defaults to input.threshold).
22
+ * @param {Array} signatures - Array of signatures (optional, defaults to input.signatures).
23
+ * @description Validates that provided public keys match the output script and initializes signatures.
24
+ */
25
+ function MultiSigInput(input, pubkeys, threshold, signatures) {
26
+ Input.apply(this, arguments);
27
+ var self = this;
28
+ pubkeys = pubkeys || input.publicKeys;
29
+ threshold = threshold || input.threshold;
30
+ signatures = signatures || input.signatures;
31
+ this.publicKeys = pubkeys
32
+ .map((k) => k.toString('hex'))
33
+ .sort()
34
+ .map((k) => new PublicKey(k));
35
+ $.checkState(
36
+ Script.buildMultisigOut(this.publicKeys, threshold).equals(this.output.script),
37
+ "Provided public keys don't match to the provided output script",
38
+ );
39
+ this.publicKeyIndex = {};
40
+ _.each(this.publicKeys, function (publicKey, index) {
41
+ self.publicKeyIndex[publicKey.toString()] = index;
42
+ });
43
+ this.threshold = threshold;
44
+ // Empty array of signatures
45
+ this.signatures = signatures
46
+ ? this._deserializeSignatures(signatures)
47
+ : new Array(this.publicKeys.length);
48
+ }
49
+ inherits(MultiSigInput, Input);
50
+
51
+ /**
52
+ * Converts the MultiSigInput instance to a plain object representation.
53
+ * Includes threshold, publicKeys (converted to strings), and serialized signatures.
54
+ * @returns {Object} The plain object representation of the MultiSigInput.
55
+ */
56
+ MultiSigInput.prototype.toObject = function () {
57
+ var obj = Input.prototype.toObject.apply(this, arguments);
58
+ obj.threshold = this.threshold;
59
+ obj.publicKeys = _.map(this.publicKeys, function (publicKey) {
60
+ return publicKey.toString();
61
+ });
62
+ obj.signatures = this._serializeSignatures();
63
+ return obj;
64
+ };
65
+
66
+ /**
67
+ * Deserializes an array of signature strings into TransactionSignature objects.
68
+ * @private
69
+ * @param {Array<string>} signatures - Array of signature strings to deserialize
70
+ * @returns {Array<TransactionSignature|undefined>} Array of TransactionSignature objects (undefined for null/empty signatures)
71
+ */
72
+ MultiSigInput.prototype._deserializeSignatures = function (signatures) {
73
+ return _.map(signatures, function (signature) {
74
+ if (!signature) {
75
+ return undefined;
76
+ }
77
+ return new TransactionSignature(signature);
78
+ });
79
+ };
80
+
81
+ /**
82
+ * Serializes the signatures array by converting each signature to a plain object.
83
+ * @returns {Array<Object|undefined>} An array of signature objects or undefined values.
84
+ * @private
85
+ */
86
+ MultiSigInput.prototype._serializeSignatures = function () {
87
+ return _.map(this.signatures, function (signature) {
88
+ if (!signature) {
89
+ return undefined;
90
+ }
91
+ return signature.toObject();
92
+ });
93
+ };
94
+
95
+ /**
96
+ * Gets signatures for a MultiSigInput by signing the transaction with the provided private key.
97
+ * Only signs for public keys that match the private key's public key.
98
+ *
99
+ * @param {Transaction} transaction - The transaction to sign
100
+ * @param {PrivateKey} privateKey - The private key used for signing
101
+ * @param {number} index - The input index
102
+ * @param {number} [sigtype=Signature.SIGHASH_ALL] - The signature type
103
+ * @returns {TransactionSignature[]} Array of transaction signatures
104
+ */
105
+ MultiSigInput.prototype.getSignatures = function (transaction, privateKey, index, sigtype) {
106
+ $.checkState(this.output instanceof Output);
107
+ sigtype = sigtype || Signature.SIGHASH_ALL;
108
+
109
+ var self = this;
110
+ var results = [];
111
+ _.each(this.publicKeys, function (publicKey) {
112
+ if (publicKey.toString() === privateKey.publicKey.toString()) {
113
+ results.push(
114
+ new TransactionSignature({
115
+ publicKey: privateKey.publicKey,
116
+ prevTxId: self.prevTxId,
117
+ outputIndex: self.outputIndex,
118
+ inputIndex: index,
119
+ signature: Sighash.sign(
120
+ transaction,
121
+ privateKey,
122
+ sigtype,
123
+ index,
124
+ ),
125
+ sigtype: sigtype,
126
+ }),
127
+ );
128
+ }
129
+ });
130
+
131
+ return results;
132
+ };
133
+
134
+ /**
135
+ * Adds a signature to the MultiSigInput if valid and not already fully signed.
136
+ * @param {Object} transaction - The transaction to validate the signature against.
137
+ * @param {Object} signature - The signature object containing publicKey and signature data.
138
+ * @throws {Error} If already fully signed, no matching public key, or invalid signature.
139
+ * @returns {MultiSigInput} Returns the instance for chaining.
140
+ */
141
+ MultiSigInput.prototype.addSignature = function (transaction, signature) {
142
+ $.checkState(!this.isFullySigned(), 'All needed signatures have already been added');
143
+ $.checkArgument(
144
+ !_.isUndefined(this.publicKeyIndex[signature.publicKey.toString()]),
145
+ 'Signature has no matching public key',
146
+ );
147
+ $.checkState(this.isValidSignature(transaction, signature));
148
+ this.signatures[this.publicKeyIndex[signature.publicKey.toString()]] = signature;
149
+ this._updateScript();
150
+ return this;
151
+ };
152
+
153
+ /**
154
+ * Updates the multisig input script by rebuilding it with current public keys, threshold, and signatures.
155
+ * @returns {MultiSigInput} Returns the instance for chaining.
156
+ */
157
+ MultiSigInput.prototype._updateScript = function () {
158
+ this.setScript(Script.buildMultisigIn(this.publicKeys, this.threshold, this._createSignatures()));
159
+ return this;
160
+ };
161
+
162
+ /**
163
+ * Creates DER-encoded signatures from the input's signature data.
164
+ * Filters out undefined signatures and converts each valid signature to a Buffer
165
+ * containing the DER-encoded signature followed by its sigtype byte.
166
+ * @returns {Buffer[]} Array of signature Buffers
167
+ */
168
+ MultiSigInput.prototype._createSignatures = function () {
169
+ return _.map(
170
+ _.filter(this.signatures, function (signature) {
171
+ return !_.isUndefined(signature);
172
+ }),
173
+ function (signature) {
174
+ return Buffer.concat([signature.signature.toDER(), Buffer.from([signature.sigtype & 0xff])]);
175
+ },
176
+ );
177
+ };
178
+
179
+ /**
180
+ * Clears all signatures from the MultiSigInput by resetting the signatures array
181
+ * and updating the script. The signatures array length matches the publicKeys array.
182
+ */
183
+ MultiSigInput.prototype.clearSignatures = function () {
184
+ this.signatures = new Array(this.publicKeys.length);
185
+ this._updateScript();
186
+ };
187
+
188
+ /**
189
+ * Checks if the MultiSigInput is fully signed by comparing the number of signatures
190
+ * with the required threshold.
191
+ * @returns {boolean} True if the input has enough signatures, false otherwise.
192
+ */
193
+ MultiSigInput.prototype.isFullySigned = function () {
194
+ return this.countSignatures() === this.threshold;
195
+ };
196
+
197
+ /**
198
+ * Returns the number of missing signatures required to meet the threshold.
199
+ * @returns {number} The count of missing signatures.
200
+ */
201
+ MultiSigInput.prototype.countMissingSignatures = function () {
202
+ return this.threshold - this.countSignatures();
203
+ };
204
+
205
+ /**
206
+ * Counts the number of valid signatures in the MultiSigInput.
207
+ * @returns {number} The count of non-null/undefined signatures.
208
+ */
209
+ MultiSigInput.prototype.countSignatures = function () {
210
+ return _.reduce(
211
+ this.signatures,
212
+ function (sum, signature) {
213
+ return sum + !!signature;
214
+ },
215
+ 0,
216
+ );
217
+ };
218
+
219
+ /**
220
+ * Returns an array of public keys that haven't been signed yet in this MultiSigInput.
221
+ * @returns {Array} Array of unsigned public keys
222
+ */
223
+ MultiSigInput.prototype.publicKeysWithoutSignature = function () {
224
+ var self = this;
225
+ return _.filter(this.publicKeys, function (publicKey) {
226
+ return !self.signatures[self.publicKeyIndex[publicKey.toString()]];
227
+ });
228
+ };
229
+
230
+ /**
231
+ * Verifies a signature for a MultiSigInput transaction.
232
+ *
233
+ * @param {Object} transaction - The transaction to verify.
234
+ * @param {Object} signature - The signature object containing signature data.
235
+ * @param {Buffer} signature.signature - The signature to verify.
236
+ * @param {Buffer} signature.publicKey - The public key corresponding to the signature.
237
+ * @param {number} signature.inputIndex - The index of the input being signed.
238
+ * @param {number} signature.sigtype - The signature type (assigned to nhashtype as a workaround).
239
+ * @returns {boolean} True if the signature is valid, false otherwise.
240
+ */
241
+ MultiSigInput.prototype.isValidSignature = function (transaction, signature) {
242
+ // FIXME: Refactor signature so this is not necessary
243
+ signature.signature.nhashtype = signature.sigtype;
244
+ return Sighash.verify(
245
+ transaction,
246
+ signature.signature,
247
+ signature.publicKey,
248
+ signature.inputIndex,
249
+ this.output.script,
250
+ this.output.satoshisBN,
251
+ );
252
+ };
253
+
254
+ /**
255
+ * Normalizes signatures for a MultiSigInput by matching each public key with its corresponding signature.
256
+ * Filters and validates signatures against the provided public keys and transaction.
257
+ *
258
+ * @param {Object} transaction - The transaction to verify against.
259
+ * @param {Object} input - The input containing prevTxId and outputIndex.
260
+ * @param {number} inputIndex - The index of the input in the transaction.
261
+ * @param {Array<Buffer>} signatures - Array of signature buffers to normalize.
262
+ * @param {Array<PublicKey>} publicKeys - Array of public keys to match signatures against.
263
+ * @returns {Array<TransactionSignature|null>} Array of matched signatures or null for unmatched keys.
264
+ */
265
+ MultiSigInput.normalizeSignatures = function (
266
+ transaction,
267
+ input,
268
+ inputIndex,
269
+ signatures,
270
+ publicKeys,
271
+ ) {
272
+ return publicKeys.map(function (pubKey) {
273
+ var signatureMatch = null;
274
+ signatures = signatures.filter(function (signatureBuffer) {
275
+ if (signatureMatch) {
276
+ return true;
277
+ }
278
+
279
+ var signature = new TransactionSignature({
280
+ signature: Signature.fromTxFormat(signatureBuffer),
281
+ publicKey: pubKey,
282
+ prevTxId: input.prevTxId,
283
+ outputIndex: input.outputIndex,
284
+ inputIndex: inputIndex,
285
+ sigtype: Signature.SIGHASH_ALL,
286
+ });
287
+
288
+ signature.signature.nhashtype = signature.sigtype;
289
+ var isMatch = Sighash.verify(
290
+ transaction,
291
+ signature.signature,
292
+ signature.publicKey,
293
+ signature.inputIndex,
294
+ );
295
+
296
+ if (isMatch) {
297
+ signatureMatch = signature;
298
+ return false;
299
+ }
300
+
301
+ return true;
302
+ });
303
+
304
+ return signatureMatch || null;
305
+ });
306
+ };
307
+
308
+ // 32 txid
309
+ // 4 output index
310
+ // --- script ---
311
+ // ??? script size (VARINT)
312
+ // 1 OP_0
313
+ // --- signature list ---
314
+ // 1 signature size (OP_PUSHDATA)
315
+ // <=72 signature (DER + SIGHASH type)
316
+ //
317
+ // 4 sequence number
318
+ /**
319
+ * The byte size of a single signature in a MultiSig input.
320
+ * @constant
321
+ */
322
+ MultiSigInput.SIGNATURE_SIZE = 73;
323
+
324
+ /**
325
+ * Estimates the byte size of a MultiSigInput, including the base input size,
326
+ * script size (with threshold-based signature count), and varint overhead.
327
+ * @returns {number} The estimated size in bytes.
328
+ * @private
329
+ */
330
+ MultiSigInput.prototype._estimateSize = function () {
331
+ var scriptSize = 1 + this.threshold * MultiSigInput.SIGNATURE_SIZE;
332
+ return Input.BASE_SIZE + Varint(scriptSize).toBuffer().length + scriptSize;
333
+ };
334
+
335
+ export default MultiSigInput;
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ import inherits from 'inherits';
4
+ import $ from '../../util/preconditions.js';
5
+ import Input from './input.js';
6
+ import Output from '../output.js';
7
+ import Sighash from '../sighash.js';
8
+ import Script from '../../script/index.js';
9
+ import Signature from '../../crypto/signature.js';
10
+ import TransactionSignature from '../signature.js';
11
+
12
+ /**
13
+ * Represents a special kind of input of PayToPublicKey kind.
14
+ * @constructor
15
+ */
16
+ function PublicKeyInput() {
17
+ Input.apply(this, arguments);
18
+ }
19
+ inherits(PublicKeyInput, Input);
20
+
21
+ /**
22
+ * @param {Transaction} transaction - the transaction to be signed
23
+ * @param {PrivateKey} privateKey - the private key with which to sign the transaction
24
+ * @param {number} index - the index of the input in the transaction input vector
25
+ * @param {number} [sigtype] - the type of signature, defaults to Signature.SIGHASH_ALL
26
+ * @return {Array} of objects that can be
27
+ */
28
+ PublicKeyInput.prototype.getSignatures = function (transaction, privateKey, index, sigtype) {
29
+ $.checkState(this.output instanceof Output);
30
+ sigtype = sigtype || Signature.SIGHASH_ALL;
31
+ var publicKey = privateKey.toPublicKey();
32
+ if (publicKey.toString() === this.output.script.getPublicKey().toString('hex')) {
33
+ return [
34
+ new TransactionSignature({
35
+ publicKey: publicKey,
36
+ prevTxId: this.prevTxId,
37
+ outputIndex: this.outputIndex,
38
+ inputIndex: index,
39
+ signature: Sighash.sign(
40
+ transaction,
41
+ privateKey,
42
+ sigtype,
43
+ index,
44
+ ),
45
+ sigtype: sigtype,
46
+ }),
47
+ ];
48
+ }
49
+ return [];
50
+ };
51
+
52
+
53
+ /**
54
+ * Adds a signature to the public key input after validating it.
55
+ * @param {Object} transaction - The transaction to validate against.
56
+ * @param {TransactionSignature} signature - The signature object containing signature data and type.
57
+ * @returns {PublicKeyInput} Returns the instance for chaining.
58
+ * @throws {Error} Throws if the signature is invalid.
59
+ */
60
+ PublicKeyInput.prototype.addSignature = function (transaction, signature) {
61
+ $.checkState(this.isValidSignature(transaction, signature), 'Signature is invalid');
62
+ this.setScript(Script.buildPublicKeyIn(signature.signature.toDER(), signature.sigtype));
63
+ return this;
64
+ };
65
+
66
+
67
+ /**
68
+ * Clears all signatures from this input by setting an empty script.
69
+ * @returns {PublicKeyInput} The instance for chaining.
70
+ */
71
+ PublicKeyInput.prototype.clearSignatures = function () {
72
+ this.setScript(Script.empty());
73
+ return this;
74
+ };
75
+
76
+
77
+ /**
78
+ * Checks if the public key input is fully signed by verifying the script contains a public key.
79
+ * @returns {boolean} True if the script contains a public key, false otherwise.
80
+ */
81
+ PublicKeyInput.prototype.isFullySigned = function () {
82
+ return this.script.isPublicKeyIn();
83
+ };
84
+
85
+ // 32 txid
86
+ // 4 output index
87
+ // ---
88
+ // 1 script size (VARINT)
89
+ // 1 signature size (OP_PUSHDATA)
90
+ // <=72 signature (DER + SIGHASH type)
91
+ // ---
92
+ // 4 sequence number
93
+ /**
94
+ * The maximum allowed size (in bytes) for a public key script in a transaction input.
95
+ * @constant {number}
96
+ */
97
+ PublicKeyInput.SCRIPT_MAX_SIZE = 74;
98
+
99
+ /**
100
+ * Estimates the byte size required for this public key input.
101
+ * @returns {number} The estimated size in bytes (base input size + max script size).
102
+ * @private
103
+ */
104
+ PublicKeyInput.prototype._estimateSize = function () {
105
+ return Input.BASE_SIZE + PublicKeyInput.SCRIPT_MAX_SIZE;
106
+ };
107
+
108
+ export default PublicKeyInput;
@@ -0,0 +1,124 @@
1
+ 'use strict';
2
+
3
+ import inherits from 'inherits';
4
+ import $ from '../../util/preconditions.js';
5
+ import Hash from '../../crypto/hash.js';
6
+ import Input from './input.js';
7
+ import Output from '../output.js';
8
+ import Sighash from '../sighash.js';
9
+ import Script from '../../script/index.js';
10
+ import Signature from '../../crypto/signature.js';
11
+ import TransactionSignature from '../signature.js';
12
+
13
+ /**
14
+ * Represents a special kind of input of PayToPublicKeyHash kind.
15
+ * @constructor
16
+ */
17
+ function PublicKeyHashInput() {
18
+ Input.apply(this, arguments);
19
+ }
20
+ inherits(PublicKeyHashInput, Input);
21
+
22
+ /**
23
+ * @param {Transaction} transaction - the transaction to be signed
24
+ * @param {PrivateKey} privateKey - the private key with which to sign the transaction
25
+ * @param {number} index - the index of the input in the transaction input vector
26
+ * @param {number} [sigtype] - the type of signature, defaults to Signature.SIGHASH_ALL
27
+ * @param {Buffer} [hashData] - the precalculated hash of the public key associated with the privateKey provided
28
+ * @return {Array} of objects that can be
29
+ */
30
+ PublicKeyHashInput.prototype.getSignatures = function (
31
+ transaction,
32
+ privateKey,
33
+ index,
34
+ sigtype,
35
+ hashData,
36
+ ) {
37
+ $.checkState(this.output instanceof Output);
38
+ hashData = hashData || Hash.sha256ripemd160(privateKey.publicKey.toBuffer());
39
+ sigtype = sigtype || Signature.SIGHASH_ALL;
40
+
41
+ if (hashData.equals(this.output.script.getPublicKeyHash())) {
42
+ return [
43
+ new TransactionSignature({
44
+ publicKey: privateKey.publicKey,
45
+ prevTxId: this.prevTxId,
46
+ outputIndex: this.outputIndex,
47
+ inputIndex: index,
48
+ signature: Sighash.sign(
49
+ transaction,
50
+ privateKey,
51
+ sigtype,
52
+ index,
53
+ ),
54
+ sigtype: sigtype,
55
+ }),
56
+ ];
57
+ }
58
+ return [];
59
+ };
60
+
61
+
62
+ /**
63
+ * Adds a signature to the input and updates the script.
64
+ * @param {Transaction} transaction - The transaction to validate against.
65
+ * @param {TransactionSignature} signature - The signature object containing publicKey, signature (DER format), and sigtype.
66
+ * @returns {PublicKeyHashInput} Returns the instance for chaining.
67
+ * @throws {Error} Throws if the signature is invalid.
68
+ */
69
+ PublicKeyHashInput.prototype.addSignature = function (transaction, signature) {
70
+ $.checkState(this.isValidSignature(transaction, signature), 'Signature is invalid');
71
+
72
+ this.setScript(
73
+ Script.buildPublicKeyHashIn(
74
+ signature.publicKey,
75
+ signature.signature.toDER(),
76
+ signature.sigtype,
77
+ ),
78
+ );
79
+ return this;
80
+ };
81
+
82
+ /**
83
+ * Clear the input's signature
84
+ * @return {PublicKeyHashInput} this, for chaining
85
+ */
86
+ PublicKeyHashInput.prototype.clearSignatures = function () {
87
+ this.setScript(Script.empty());
88
+ return this;
89
+ };
90
+
91
+ /**
92
+ * Query whether the input is signed
93
+ * @return {boolean}
94
+ */
95
+ PublicKeyHashInput.prototype.isFullySigned = function () {
96
+ return this.script.isPublicKeyHashIn();
97
+ };
98
+
99
+ // 32 txid
100
+ // 4 output index
101
+ // --- script ---
102
+ // 1 script size (VARINT)
103
+ // 1 signature size (OP_PUSHDATA)
104
+ // <=72 signature (DER + SIGHASH type)
105
+ // 1 public key size (OP_PUSHDATA)
106
+ // 65 uncompressed public key
107
+ //
108
+ // 4 sequence number
109
+ /**
110
+ * The maximum allowed size (in bytes) for a public key hash script.
111
+ * @constant
112
+ */
113
+ PublicKeyHashInput.SCRIPT_MAX_SIZE = 140;
114
+
115
+ /**
116
+ * Estimates the byte size of this public key hash input.
117
+ * @returns {number} The estimated size in bytes (base input size + max script size).
118
+ * @private
119
+ */
120
+ PublicKeyHashInput.prototype._estimateSize = function () {
121
+ return Input.BASE_SIZE + PublicKeyHashInput.SCRIPT_MAX_SIZE;
122
+ };
123
+
124
+ export default PublicKeyHashInput;