@bsv/sdk 1.0.0

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 (464) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +40 -0
  2. package/.github/ISSUE_TEMPLATE/discussion.md +24 -0
  3. package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +23 -0
  4. package/CHANGELOG.md +72 -0
  5. package/CONTRIBUTING.md +85 -0
  6. package/LICENSE.txt +28 -0
  7. package/README.md +87 -0
  8. package/ROADMAP.md +3 -0
  9. package/dist/cjs/mod.js +25 -0
  10. package/dist/cjs/mod.js.map +1 -0
  11. package/dist/cjs/package.json +42 -0
  12. package/dist/cjs/src/compat/BIP39.js +272 -0
  13. package/dist/cjs/src/compat/BIP39.js.map +1 -0
  14. package/dist/cjs/src/compat/BSM.js +77 -0
  15. package/dist/cjs/src/compat/BSM.js.map +1 -0
  16. package/dist/cjs/src/compat/ECIES.js +483 -0
  17. package/dist/cjs/src/compat/ECIES.js.map +1 -0
  18. package/dist/cjs/src/compat/HD.js +326 -0
  19. package/dist/cjs/src/compat/HD.js.map +1 -0
  20. package/dist/cjs/src/compat/Mnemonic.js +298 -0
  21. package/dist/cjs/src/compat/Mnemonic.js.map +1 -0
  22. package/dist/cjs/src/compat/bip-39-wordlist-en.js +2057 -0
  23. package/dist/cjs/src/compat/bip-39-wordlist-en.js.map +1 -0
  24. package/dist/cjs/src/compat/index.js +37 -0
  25. package/dist/cjs/src/compat/index.js.map +1 -0
  26. package/dist/cjs/src/messages/EncryptedMessage.js +69 -0
  27. package/dist/cjs/src/messages/EncryptedMessage.js.map +1 -0
  28. package/dist/cjs/src/messages/SignedMessage.js +85 -0
  29. package/dist/cjs/src/messages/SignedMessage.js.map +1 -0
  30. package/dist/cjs/src/messages/index.js +29 -0
  31. package/dist/cjs/src/messages/index.js.map +1 -0
  32. package/dist/cjs/src/primitives/AESGCM.js +384 -0
  33. package/dist/cjs/src/primitives/AESGCM.js.map +1 -0
  34. package/dist/cjs/src/primitives/BasePoint.js +19 -0
  35. package/dist/cjs/src/primitives/BasePoint.js.map +1 -0
  36. package/dist/cjs/src/primitives/BigNumber.js +4269 -0
  37. package/dist/cjs/src/primitives/BigNumber.js.map +1 -0
  38. package/dist/cjs/src/primitives/Curve.js +1126 -0
  39. package/dist/cjs/src/primitives/Curve.js.map +1 -0
  40. package/dist/cjs/src/primitives/DRBG.js +99 -0
  41. package/dist/cjs/src/primitives/DRBG.js.map +1 -0
  42. package/dist/cjs/src/primitives/ECDSA.js +169 -0
  43. package/dist/cjs/src/primitives/ECDSA.js.map +1 -0
  44. package/dist/cjs/src/primitives/Hash.js +1332 -0
  45. package/dist/cjs/src/primitives/Hash.js.map +1 -0
  46. package/dist/cjs/src/primitives/JacobianPoint.js +400 -0
  47. package/dist/cjs/src/primitives/JacobianPoint.js.map +1 -0
  48. package/dist/cjs/src/primitives/K256.js +111 -0
  49. package/dist/cjs/src/primitives/K256.js.map +1 -0
  50. package/dist/cjs/src/primitives/Mersenne.js +118 -0
  51. package/dist/cjs/src/primitives/Mersenne.js.map +1 -0
  52. package/dist/cjs/src/primitives/MontgomoryMethod.js +150 -0
  53. package/dist/cjs/src/primitives/MontgomoryMethod.js.map +1 -0
  54. package/dist/cjs/src/primitives/Point.js +819 -0
  55. package/dist/cjs/src/primitives/Point.js.map +1 -0
  56. package/dist/cjs/src/primitives/PrivateKey.js +190 -0
  57. package/dist/cjs/src/primitives/PrivateKey.js.map +1 -0
  58. package/dist/cjs/src/primitives/PublicKey.js +151 -0
  59. package/dist/cjs/src/primitives/PublicKey.js.map +1 -0
  60. package/dist/cjs/src/primitives/Random.js +57 -0
  61. package/dist/cjs/src/primitives/Random.js.map +1 -0
  62. package/dist/cjs/src/primitives/ReductionContext.js +490 -0
  63. package/dist/cjs/src/primitives/ReductionContext.js.map +1 -0
  64. package/dist/cjs/src/primitives/Signature.js +220 -0
  65. package/dist/cjs/src/primitives/Signature.js.map +1 -0
  66. package/dist/cjs/src/primitives/SymmetricKey.js +69 -0
  67. package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -0
  68. package/dist/cjs/src/primitives/TransactionSignature.js +172 -0
  69. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -0
  70. package/dist/cjs/src/primitives/index.js +45 -0
  71. package/dist/cjs/src/primitives/index.js.map +1 -0
  72. package/dist/cjs/src/primitives/utils.js +615 -0
  73. package/dist/cjs/src/primitives/utils.js.map +1 -0
  74. package/dist/cjs/src/script/LockingScript.js +35 -0
  75. package/dist/cjs/src/script/LockingScript.js.map +1 -0
  76. package/dist/cjs/src/script/OP.js +208 -0
  77. package/dist/cjs/src/script/OP.js.map +1 -0
  78. package/dist/cjs/src/script/Script.js +429 -0
  79. package/dist/cjs/src/script/Script.js.map +1 -0
  80. package/dist/cjs/src/script/ScriptChunk.js +3 -0
  81. package/dist/cjs/src/script/ScriptChunk.js.map +1 -0
  82. package/dist/cjs/src/script/ScriptTemplate.js +3 -0
  83. package/dist/cjs/src/script/ScriptTemplate.js.map +1 -0
  84. package/dist/cjs/src/script/Spend.js +1252 -0
  85. package/dist/cjs/src/script/Spend.js.map +1 -0
  86. package/dist/cjs/src/script/UnlockingScript.js +35 -0
  87. package/dist/cjs/src/script/UnlockingScript.js.map +1 -0
  88. package/dist/cjs/src/script/index.js +32 -0
  89. package/dist/cjs/src/script/index.js.map +1 -0
  90. package/dist/cjs/src/script/templates/P2PKH.js +98 -0
  91. package/dist/cjs/src/script/templates/P2PKH.js.map +1 -0
  92. package/dist/cjs/src/script/templates/RPuzzle.js +125 -0
  93. package/dist/cjs/src/script/templates/RPuzzle.js.map +1 -0
  94. package/dist/cjs/src/script/templates/index.js +11 -0
  95. package/dist/cjs/src/script/templates/index.js.map +1 -0
  96. package/dist/cjs/src/transaction/Broadcaster.js +3 -0
  97. package/dist/cjs/src/transaction/Broadcaster.js.map +1 -0
  98. package/dist/cjs/src/transaction/ChainTracker.js +3 -0
  99. package/dist/cjs/src/transaction/ChainTracker.js.map +1 -0
  100. package/dist/cjs/src/transaction/FeeModel.js +3 -0
  101. package/dist/cjs/src/transaction/FeeModel.js.map +1 -0
  102. package/dist/cjs/src/transaction/MerklePath.js +239 -0
  103. package/dist/cjs/src/transaction/MerklePath.js.map +1 -0
  104. package/dist/cjs/src/transaction/Transaction.js +557 -0
  105. package/dist/cjs/src/transaction/Transaction.js.map +1 -0
  106. package/dist/cjs/src/transaction/TransactionInput.js +3 -0
  107. package/dist/cjs/src/transaction/TransactionInput.js.map +1 -0
  108. package/dist/cjs/src/transaction/TransactionOutput.js +3 -0
  109. package/dist/cjs/src/transaction/TransactionOutput.js.map +1 -0
  110. package/dist/cjs/src/transaction/broadcasters/ARC.js +101 -0
  111. package/dist/cjs/src/transaction/broadcasters/ARC.js.map +1 -0
  112. package/dist/cjs/src/transaction/broadcasters/index.js +9 -0
  113. package/dist/cjs/src/transaction/broadcasters/index.js.map +1 -0
  114. package/dist/cjs/src/transaction/fee-models/SatoshisPerKilobyte.js +69 -0
  115. package/dist/cjs/src/transaction/fee-models/SatoshisPerKilobyte.js.map +1 -0
  116. package/dist/cjs/src/transaction/fee-models/index.js +9 -0
  117. package/dist/cjs/src/transaction/fee-models/index.js.map +1 -0
  118. package/dist/cjs/src/transaction/index.js +11 -0
  119. package/dist/cjs/src/transaction/index.js.map +1 -0
  120. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -0
  121. package/dist/esm/mod.js +9 -0
  122. package/dist/esm/mod.js.map +1 -0
  123. package/dist/esm/src/compat/BIP39.js +272 -0
  124. package/dist/esm/src/compat/BIP39.js.map +1 -0
  125. package/dist/esm/src/compat/BSM.js +45 -0
  126. package/dist/esm/src/compat/BSM.js.map +1 -0
  127. package/dist/esm/src/compat/ECIES.js +454 -0
  128. package/dist/esm/src/compat/ECIES.js.map +1 -0
  129. package/dist/esm/src/compat/HD.js +304 -0
  130. package/dist/esm/src/compat/HD.js.map +1 -0
  131. package/dist/esm/src/compat/Mnemonic.js +272 -0
  132. package/dist/esm/src/compat/Mnemonic.js.map +1 -0
  133. package/dist/esm/src/compat/bip-39-wordlist-en.js +2054 -0
  134. package/dist/esm/src/compat/bip-39-wordlist-en.js.map +1 -0
  135. package/dist/esm/src/compat/index.js +5 -0
  136. package/dist/esm/src/compat/index.js.map +1 -0
  137. package/dist/esm/src/messages/EncryptedMessage.js +61 -0
  138. package/dist/esm/src/messages/EncryptedMessage.js.map +1 -0
  139. package/dist/esm/src/messages/SignedMessage.js +77 -0
  140. package/dist/esm/src/messages/SignedMessage.js.map +1 -0
  141. package/dist/esm/src/messages/index.js +3 -0
  142. package/dist/esm/src/messages/index.js.map +1 -0
  143. package/dist/esm/src/primitives/AESGCM.js +371 -0
  144. package/dist/esm/src/primitives/AESGCM.js.map +1 -0
  145. package/dist/esm/src/primitives/BasePoint.js +16 -0
  146. package/dist/esm/src/primitives/BasePoint.js.map +1 -0
  147. package/dist/esm/src/primitives/BigNumber.js +4304 -0
  148. package/dist/esm/src/primitives/BigNumber.js.map +1 -0
  149. package/dist/esm/src/primitives/Curve.js +1141 -0
  150. package/dist/esm/src/primitives/Curve.js.map +1 -0
  151. package/dist/esm/src/primitives/DRBG.js +98 -0
  152. package/dist/esm/src/primitives/DRBG.js.map +1 -0
  153. package/dist/esm/src/primitives/ECDSA.js +161 -0
  154. package/dist/esm/src/primitives/ECDSA.js.map +1 -0
  155. package/dist/esm/src/primitives/Hash.js +1336 -0
  156. package/dist/esm/src/primitives/Hash.js.map +1 -0
  157. package/dist/esm/src/primitives/JacobianPoint.js +398 -0
  158. package/dist/esm/src/primitives/JacobianPoint.js.map +1 -0
  159. package/dist/esm/src/primitives/K256.js +105 -0
  160. package/dist/esm/src/primitives/K256.js.map +1 -0
  161. package/dist/esm/src/primitives/Mersenne.js +117 -0
  162. package/dist/esm/src/primitives/Mersenne.js.map +1 -0
  163. package/dist/esm/src/primitives/MontgomoryMethod.js +149 -0
  164. package/dist/esm/src/primitives/MontgomoryMethod.js.map +1 -0
  165. package/dist/esm/src/primitives/Point.js +816 -0
  166. package/dist/esm/src/primitives/Point.js.map +1 -0
  167. package/dist/esm/src/primitives/PrivateKey.js +184 -0
  168. package/dist/esm/src/primitives/PrivateKey.js.map +1 -0
  169. package/dist/esm/src/primitives/PublicKey.js +145 -0
  170. package/dist/esm/src/primitives/PublicKey.js.map +1 -0
  171. package/dist/esm/src/primitives/Random.js +56 -0
  172. package/dist/esm/src/primitives/Random.js.map +1 -0
  173. package/dist/esm/src/primitives/ReductionContext.js +486 -0
  174. package/dist/esm/src/primitives/ReductionContext.js.map +1 -0
  175. package/dist/esm/src/primitives/Signature.js +223 -0
  176. package/dist/esm/src/primitives/Signature.js.map +1 -0
  177. package/dist/esm/src/primitives/SymmetricKey.js +63 -0
  178. package/dist/esm/src/primitives/SymmetricKey.js.map +1 -0
  179. package/dist/esm/src/primitives/TransactionSignature.js +144 -0
  180. package/dist/esm/src/primitives/TransactionSignature.js.map +1 -0
  181. package/dist/esm/src/primitives/index.js +9 -0
  182. package/dist/esm/src/primitives/index.js.map +1 -0
  183. package/dist/esm/src/primitives/utils.js +601 -0
  184. package/dist/esm/src/primitives/utils.js.map +1 -0
  185. package/dist/esm/src/script/LockingScript.js +29 -0
  186. package/dist/esm/src/script/LockingScript.js.map +1 -0
  187. package/dist/esm/src/script/OP.js +206 -0
  188. package/dist/esm/src/script/OP.js.map +1 -0
  189. package/dist/esm/src/script/Script.js +424 -0
  190. package/dist/esm/src/script/Script.js.map +1 -0
  191. package/dist/esm/src/script/ScriptChunk.js +2 -0
  192. package/dist/esm/src/script/ScriptChunk.js.map +1 -0
  193. package/dist/esm/src/script/ScriptTemplate.js +2 -0
  194. package/dist/esm/src/script/ScriptTemplate.js.map +1 -0
  195. package/dist/esm/src/script/Spend.js +1240 -0
  196. package/dist/esm/src/script/Spend.js.map +1 -0
  197. package/dist/esm/src/script/UnlockingScript.js +29 -0
  198. package/dist/esm/src/script/UnlockingScript.js.map +1 -0
  199. package/dist/esm/src/script/index.js +7 -0
  200. package/dist/esm/src/script/index.js.map +1 -0
  201. package/dist/esm/src/script/templates/P2PKH.js +92 -0
  202. package/dist/esm/src/script/templates/P2PKH.js.map +1 -0
  203. package/dist/esm/src/script/templates/RPuzzle.js +119 -0
  204. package/dist/esm/src/script/templates/RPuzzle.js.map +1 -0
  205. package/dist/esm/src/script/templates/index.js +3 -0
  206. package/dist/esm/src/script/templates/index.js.map +1 -0
  207. package/dist/esm/src/transaction/Broadcaster.js +2 -0
  208. package/dist/esm/src/transaction/Broadcaster.js.map +1 -0
  209. package/dist/esm/src/transaction/ChainTracker.js +2 -0
  210. package/dist/esm/src/transaction/ChainTracker.js.map +1 -0
  211. package/dist/esm/src/transaction/FeeModel.js +2 -0
  212. package/dist/esm/src/transaction/FeeModel.js.map +1 -0
  213. package/dist/esm/src/transaction/MerklePath.js +237 -0
  214. package/dist/esm/src/transaction/MerklePath.js.map +1 -0
  215. package/dist/esm/src/transaction/Transaction.js +557 -0
  216. package/dist/esm/src/transaction/Transaction.js.map +1 -0
  217. package/dist/esm/src/transaction/TransactionInput.js +2 -0
  218. package/dist/esm/src/transaction/TransactionInput.js.map +1 -0
  219. package/dist/esm/src/transaction/TransactionOutput.js +2 -0
  220. package/dist/esm/src/transaction/TransactionOutput.js.map +1 -0
  221. package/dist/esm/src/transaction/broadcasters/ARC.js +100 -0
  222. package/dist/esm/src/transaction/broadcasters/ARC.js.map +1 -0
  223. package/dist/esm/src/transaction/broadcasters/index.js +2 -0
  224. package/dist/esm/src/transaction/broadcasters/index.js.map +1 -0
  225. package/dist/esm/src/transaction/fee-models/SatoshisPerKilobyte.js +71 -0
  226. package/dist/esm/src/transaction/fee-models/SatoshisPerKilobyte.js.map +1 -0
  227. package/dist/esm/src/transaction/fee-models/index.js +2 -0
  228. package/dist/esm/src/transaction/fee-models/index.js.map +1 -0
  229. package/dist/esm/src/transaction/index.js +3 -0
  230. package/dist/esm/src/transaction/index.js.map +1 -0
  231. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -0
  232. package/dist/types/mod.d.ts +9 -0
  233. package/dist/types/mod.d.ts.map +1 -0
  234. package/dist/types/src/compat/BIP39.d.ts +132 -0
  235. package/dist/types/src/compat/BIP39.d.ts.map +1 -0
  236. package/dist/types/src/compat/BSM.d.ts +28 -0
  237. package/dist/types/src/compat/BSM.d.ts.map +1 -0
  238. package/dist/types/src/compat/ECIES.d.ts +62 -0
  239. package/dist/types/src/compat/ECIES.d.ts.map +1 -0
  240. package/dist/types/src/compat/HD.d.ts +117 -0
  241. package/dist/types/src/compat/HD.d.ts.map +1 -0
  242. package/dist/types/src/compat/Mnemonic.d.ts +132 -0
  243. package/dist/types/src/compat/Mnemonic.d.ts.map +1 -0
  244. package/dist/types/src/compat/bip-39-wordlist-en.d.ts +5 -0
  245. package/dist/types/src/compat/bip-39-wordlist-en.d.ts.map +1 -0
  246. package/dist/types/src/compat/index.d.ts +5 -0
  247. package/dist/types/src/compat/index.d.ts.map +1 -0
  248. package/dist/types/src/messages/EncryptedMessage.d.ts +20 -0
  249. package/dist/types/src/messages/EncryptedMessage.d.ts.map +1 -0
  250. package/dist/types/src/messages/SignedMessage.d.ts +21 -0
  251. package/dist/types/src/messages/SignedMessage.d.ts.map +1 -0
  252. package/dist/types/src/messages/index.d.ts +3 -0
  253. package/dist/types/src/messages/index.d.ts.map +1 -0
  254. package/dist/types/src/primitives/AESGCM.d.ts +14 -0
  255. package/dist/types/src/primitives/AESGCM.d.ts.map +1 -0
  256. package/dist/types/src/primitives/BasePoint.d.ts +22 -0
  257. package/dist/types/src/primitives/BasePoint.d.ts.map +1 -0
  258. package/dist/types/src/primitives/BigNumber.d.ts +1895 -0
  259. package/dist/types/src/primitives/BigNumber.d.ts.map +1 -0
  260. package/dist/types/src/primitives/Curve.d.ts +55 -0
  261. package/dist/types/src/primitives/Curve.d.ts.map +1 -0
  262. package/dist/types/src/primitives/DRBG.d.ts +54 -0
  263. package/dist/types/src/primitives/DRBG.d.ts.map +1 -0
  264. package/dist/types/src/primitives/ECDSA.d.ts +39 -0
  265. package/dist/types/src/primitives/ECDSA.d.ts.map +1 -0
  266. package/dist/types/src/primitives/Hash.d.ts +411 -0
  267. package/dist/types/src/primitives/Hash.d.ts.map +1 -0
  268. package/dist/types/src/primitives/JacobianPoint.d.ts +164 -0
  269. package/dist/types/src/primitives/JacobianPoint.d.ts.map +1 -0
  270. package/dist/types/src/primitives/K256.d.ts +53 -0
  271. package/dist/types/src/primitives/K256.d.ts.map +1 -0
  272. package/dist/types/src/primitives/Mersenne.d.ts +72 -0
  273. package/dist/types/src/primitives/Mersenne.d.ts.map +1 -0
  274. package/dist/types/src/primitives/MontgomoryMethod.d.ts +96 -0
  275. package/dist/types/src/primitives/MontgomoryMethod.d.ts.map +1 -0
  276. package/dist/types/src/primitives/Point.d.ts +303 -0
  277. package/dist/types/src/primitives/Point.d.ts.map +1 -0
  278. package/dist/types/src/primitives/PrivateKey.d.ts +143 -0
  279. package/dist/types/src/primitives/PrivateKey.d.ts.map +1 -0
  280. package/dist/types/src/primitives/PublicKey.d.ts +108 -0
  281. package/dist/types/src/primitives/PublicKey.d.ts.map +1 -0
  282. package/dist/types/src/primitives/Random.d.ts +14 -0
  283. package/dist/types/src/primitives/Random.d.ts.map +1 -0
  284. package/dist/types/src/primitives/ReductionContext.d.ts +308 -0
  285. package/dist/types/src/primitives/ReductionContext.d.ts.map +1 -0
  286. package/dist/types/src/primitives/Signature.d.ts +100 -0
  287. package/dist/types/src/primitives/Signature.d.ts.map +1 -0
  288. package/dist/types/src/primitives/SymmetricKey.d.ts +44 -0
  289. package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -0
  290. package/dist/types/src/primitives/TransactionSignature.d.ts +36 -0
  291. package/dist/types/src/primitives/TransactionSignature.d.ts.map +1 -0
  292. package/dist/types/src/primitives/index.d.ts +9 -0
  293. package/dist/types/src/primitives/index.d.ts.map +1 -0
  294. package/dist/types/src/primitives/utils.d.ts +118 -0
  295. package/dist/types/src/primitives/utils.d.ts.map +1 -0
  296. package/dist/types/src/script/LockingScript.d.ts +25 -0
  297. package/dist/types/src/script/LockingScript.d.ts.map +1 -0
  298. package/dist/types/src/script/OP.d.ts +193 -0
  299. package/dist/types/src/script/OP.d.ts.map +1 -0
  300. package/dist/types/src/script/Script.d.ts +148 -0
  301. package/dist/types/src/script/Script.d.ts.map +1 -0
  302. package/dist/types/src/script/ScriptChunk.d.ts +8 -0
  303. package/dist/types/src/script/ScriptChunk.d.ts.map +1 -0
  304. package/dist/types/src/script/ScriptTemplate.d.ts +33 -0
  305. package/dist/types/src/script/ScriptTemplate.d.ts.map +1 -0
  306. package/dist/types/src/script/Spend.d.ts +103 -0
  307. package/dist/types/src/script/Spend.d.ts.map +1 -0
  308. package/dist/types/src/script/UnlockingScript.d.ts +25 -0
  309. package/dist/types/src/script/UnlockingScript.d.ts.map +1 -0
  310. package/dist/types/src/script/index.d.ts +8 -0
  311. package/dist/types/src/script/index.d.ts.map +1 -0
  312. package/dist/types/src/script/templates/P2PKH.d.ts +37 -0
  313. package/dist/types/src/script/templates/P2PKH.d.ts.map +1 -0
  314. package/dist/types/src/script/templates/RPuzzle.d.ts +47 -0
  315. package/dist/types/src/script/templates/RPuzzle.d.ts.map +1 -0
  316. package/dist/types/src/script/templates/index.d.ts +3 -0
  317. package/dist/types/src/script/templates/index.d.ts.map +1 -0
  318. package/dist/types/src/transaction/Broadcaster.d.ts +39 -0
  319. package/dist/types/src/transaction/Broadcaster.d.ts.map +1 -0
  320. package/dist/types/src/transaction/ChainTracker.d.ts +23 -0
  321. package/dist/types/src/transaction/ChainTracker.d.ts.map +1 -0
  322. package/dist/types/src/transaction/FeeModel.d.ts +12 -0
  323. package/dist/types/src/transaction/FeeModel.d.ts.map +1 -0
  324. package/dist/types/src/transaction/MerklePath.d.ts +91 -0
  325. package/dist/types/src/transaction/MerklePath.d.ts.map +1 -0
  326. package/dist/types/src/transaction/Transaction.d.ts +181 -0
  327. package/dist/types/src/transaction/Transaction.d.ts.map +1 -0
  328. package/dist/types/src/transaction/TransactionInput.d.ts +63 -0
  329. package/dist/types/src/transaction/TransactionInput.d.ts.map +1 -0
  330. package/dist/types/src/transaction/TransactionOutput.d.ts +36 -0
  331. package/dist/types/src/transaction/TransactionOutput.d.ts.map +1 -0
  332. package/dist/types/src/transaction/broadcasters/ARC.d.ts +28 -0
  333. package/dist/types/src/transaction/broadcasters/ARC.d.ts.map +1 -0
  334. package/dist/types/src/transaction/broadcasters/index.d.ts +2 -0
  335. package/dist/types/src/transaction/broadcasters/index.d.ts.map +1 -0
  336. package/dist/types/src/transaction/fee-models/SatoshisPerKilobyte.d.ts +26 -0
  337. package/dist/types/src/transaction/fee-models/SatoshisPerKilobyte.d.ts.map +1 -0
  338. package/dist/types/src/transaction/fee-models/index.d.ts +2 -0
  339. package/dist/types/src/transaction/fee-models/index.d.ts.map +1 -0
  340. package/dist/types/src/transaction/index.d.ts +7 -0
  341. package/dist/types/src/transaction/index.d.ts.map +1 -0
  342. package/dist/types/tsconfig.types.tsbuildinfo +1 -0
  343. package/docs/README.md +9 -0
  344. package/docs/compat.md +2856 -0
  345. package/docs/getting-started/COMMONJS.md +94 -0
  346. package/docs/getting-started/REACT-TS.md +131 -0
  347. package/docs/getting-started/TS-NODE.md +106 -0
  348. package/docs/getting-started/VUE.md +103 -0
  349. package/docs/messages.md +146 -0
  350. package/docs/primitives.md +7440 -0
  351. package/docs/script.md +766 -0
  352. package/docs/transaction.md +741 -0
  353. package/jest.config.js +6 -0
  354. package/mod.ts +8 -0
  355. package/package.json +137 -0
  356. package/src/compat/BSM.ts +51 -0
  357. package/src/compat/ECIES.ts +557 -0
  358. package/src/compat/HD.ts +348 -0
  359. package/src/compat/Mnemonic.ts +295 -0
  360. package/src/compat/__tests/BSM.test.ts +38 -0
  361. package/src/compat/__tests/ECIES.test.ts +90 -0
  362. package/src/compat/__tests/HD.test.ts +405 -0
  363. package/src/compat/__tests/Mnemonic.test.ts +177 -0
  364. package/src/compat/__tests/Mnemonic.vectors.ts +172 -0
  365. package/src/compat/bip-39-wordlist-en.ts +2053 -0
  366. package/src/compat/index.ts +4 -0
  367. package/src/messages/EncryptedMessage.ts +70 -0
  368. package/src/messages/SignedMessage.ts +87 -0
  369. package/src/messages/__tests/EncryptedMessage.test.ts +36 -0
  370. package/src/messages/__tests/SignedMessage.test.ts +53 -0
  371. package/src/messages/index.ts +2 -0
  372. package/src/primitives/AESGCM.ts +479 -0
  373. package/src/primitives/BasePoint.ts +21 -0
  374. package/src/primitives/BigNumber.ts +4619 -0
  375. package/src/primitives/Curve.ts +1163 -0
  376. package/src/primitives/DRBG.ts +102 -0
  377. package/src/primitives/ECDSA.ts +164 -0
  378. package/src/primitives/Hash.ts +1420 -0
  379. package/src/primitives/JacobianPoint.ts +410 -0
  380. package/src/primitives/K256.ts +116 -0
  381. package/src/primitives/Mersenne.ts +123 -0
  382. package/src/primitives/MontgomoryMethod.ts +160 -0
  383. package/src/primitives/Point.ts +852 -0
  384. package/src/primitives/PrivateKey.ts +195 -0
  385. package/src/primitives/PublicKey.ts +154 -0
  386. package/src/primitives/Random.ts +55 -0
  387. package/src/primitives/ReductionContext.ts +528 -0
  388. package/src/primitives/Signature.ts +235 -0
  389. package/src/primitives/SymmetricKey.ts +75 -0
  390. package/src/primitives/TransactionSignature.ts +189 -0
  391. package/src/primitives/__tests/AESGCM.test.ts +338 -0
  392. package/src/primitives/__tests/BRC42.private.vectors.ts +33 -0
  393. package/src/primitives/__tests/BRC42.public.vectors.ts +33 -0
  394. package/src/primitives/__tests/BigNumber.arithmatic.test.ts +572 -0
  395. package/src/primitives/__tests/BigNumber.binary.test.ts +203 -0
  396. package/src/primitives/__tests/BigNumber.constructor.test.ts +176 -0
  397. package/src/primitives/__tests/BigNumber.dhGroup.test.ts +18 -0
  398. package/src/primitives/__tests/BigNumber.fixtures.ts +264 -0
  399. package/src/primitives/__tests/BigNumber.serializers.test.ts +157 -0
  400. package/src/primitives/__tests/BigNumber.utils.test.ts +347 -0
  401. package/src/primitives/__tests/Curve.unit.test.ts +192 -0
  402. package/src/primitives/__tests/DRBG.test.ts +18 -0
  403. package/src/primitives/__tests/DRBG.vectors.ts +167 -0
  404. package/src/primitives/__tests/ECDH.test.ts +31 -0
  405. package/src/primitives/__tests/ECDSA.test.ts +58 -0
  406. package/src/primitives/__tests/HMAC.test.ts +59 -0
  407. package/src/primitives/__tests/Hash.test.ts +121 -0
  408. package/src/primitives/__tests/PBKDF2.vectors.ts +119 -0
  409. package/src/primitives/__tests/PrivateKey.test.ts +17 -0
  410. package/src/primitives/__tests/PublicKey.test.ts +66 -0
  411. package/src/primitives/__tests/Random.test.ts +14 -0
  412. package/src/primitives/__tests/Reader.test.ts +296 -0
  413. package/src/primitives/__tests/ReductionContext.test.ts +279 -0
  414. package/src/primitives/__tests/SymmetricKey.test.ts +58 -0
  415. package/src/primitives/__tests/SymmetricKey.vectors.ts +40 -0
  416. package/src/primitives/__tests/Writer.test.ts +198 -0
  417. package/src/primitives/__tests/sighash.vectors.ts +3503 -0
  418. package/src/primitives/__tests/utils.test.ts +108 -0
  419. package/src/primitives/index.ts +8 -0
  420. package/src/primitives/utils.ts +665 -0
  421. package/src/script/LockingScript.ts +30 -0
  422. package/src/script/OP.ts +219 -0
  423. package/src/script/Script.ts +426 -0
  424. package/src/script/ScriptChunk.ts +7 -0
  425. package/src/script/ScriptTemplate.ts +36 -0
  426. package/src/script/Spend.ts +1379 -0
  427. package/src/script/UnlockingScript.ts +30 -0
  428. package/src/script/__tests/Script.test.ts +369 -0
  429. package/src/script/__tests/Spend.test.ts +248 -0
  430. package/src/script/__tests/script.invalid.vectors.ts +925 -0
  431. package/src/script/__tests/script.valid.vectors.ts +1120 -0
  432. package/src/script/__tests/scriptFromVector.ts +42 -0
  433. package/src/script/__tests/spend.valid.vectors.ts +2288 -0
  434. package/src/script/index.ts +7 -0
  435. package/src/script/templates/P2PKH.ts +109 -0
  436. package/src/script/templates/RPuzzle.ts +140 -0
  437. package/src/script/templates/index.ts +2 -0
  438. package/src/transaction/Broadcaster.ts +42 -0
  439. package/src/transaction/ChainTracker.ts +22 -0
  440. package/src/transaction/FeeModel.ts +13 -0
  441. package/src/transaction/MerklePath.ts +259 -0
  442. package/src/transaction/Transaction.ts +602 -0
  443. package/src/transaction/TransactionInput.ts +63 -0
  444. package/src/transaction/TransactionOutput.ts +37 -0
  445. package/src/transaction/__tests/MerklePath.test.ts +181 -0
  446. package/src/transaction/__tests/Transaction.test.ts +413 -0
  447. package/src/transaction/__tests/bigtx.vectors.ts +4 -0
  448. package/src/transaction/__tests/bump.invalid.vectors.ts +8 -0
  449. package/src/transaction/__tests/bump.valid.vectors.ts +4 -0
  450. package/src/transaction/__tests/tx.invalid.vectors.ts +281 -0
  451. package/src/transaction/__tests/tx.valid.vectors.ts +364 -0
  452. package/src/transaction/broadcasters/ARC.ts +106 -0
  453. package/src/transaction/broadcasters/__tests/ARC.test.ts +115 -0
  454. package/src/transaction/broadcasters/index.ts +1 -0
  455. package/src/transaction/fee-models/SatoshisPerKilobyte.ts +71 -0
  456. package/src/transaction/fee-models/index.ts +1 -0
  457. package/src/transaction/index.ts +6 -0
  458. package/ts2md.json +5 -0
  459. package/tsconfig.base.json +26 -0
  460. package/tsconfig.cjs.json +11 -0
  461. package/tsconfig.eslint.json +12 -0
  462. package/tsconfig.esm.json +9 -0
  463. package/tsconfig.json +17 -0
  464. package/tsconfig.types.json +11 -0
@@ -0,0 +1,1252 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const Script_js_1 = __importDefault(require("./Script.js"));
30
+ const BigNumber_js_1 = __importDefault(require("../primitives/BigNumber.js"));
31
+ const OP_js_1 = __importDefault(require("./OP.js"));
32
+ const utils_js_1 = require("../primitives/utils.js");
33
+ const Hash = __importStar(require("../primitives/Hash.js"));
34
+ const TransactionSignature_js_1 = __importDefault(require("../primitives/TransactionSignature.js"));
35
+ const PublicKey_js_1 = __importDefault(require("../primitives/PublicKey.js"));
36
+ const ECDSA_js_1 = require("../primitives/ECDSA.js");
37
+ // These constants control the current behavior of the interpreter.
38
+ // In the future, all of them will go away.
39
+ const maxScriptElementSize = 1024 * 1024 * 1024;
40
+ const maxMultisigKeyCount = Math.pow(2, 31) - 1;
41
+ const requireMinimalPush = true;
42
+ const requirePushOnlyUnlockingScripts = true;
43
+ const requireLowSSignatures = true;
44
+ const requireCleanStack = true;
45
+ /**
46
+ * The Spend class represents a spend action within a Bitcoin SV transaction.
47
+ * It encapsulates all the necessary data required for spending a UTXO (Unspent Transaction Output)
48
+ * and includes details about the source transaction, output, and the spending transaction itself.
49
+ *
50
+ * @property {string} sourceTXID - The transaction ID of the source UTXO.
51
+ * @property {number} sourceOutputIndex - The index of the output in the source transaction.
52
+ * @property {BigNumber} sourceSatoshis - The amount of satoshis in the source UTXO.
53
+ * @property {LockingScript} lockingScript - The locking script associated with the UTXO.
54
+ * @property {number} transactionVersion - The version of the current transaction.
55
+ * @property {Array<{ sourceTXID: string, sourceOutputIndex: number, sequence: number }>} otherInputs -
56
+ * An array of other inputs in the transaction, each with a txid, outputIndex, and sequence number.
57
+ * @property {Array<{ satoshis: BigNumber, lockingScript: LockingScript }>} outputs -
58
+ * An array of outputs of the current transaction, including the satoshi value and locking script for each.
59
+ * @property {number} inputIndex - The index of this input in the current transaction.
60
+ * @property {UnlockingScript} unlockingScript - The unlocking script that unlocks the UTXO for spending.
61
+ * @property {number} inputSequence - The sequence number of this input.
62
+ */
63
+ class Spend {
64
+ /**
65
+ * @constructor
66
+ * Constructs the Spend object with necessary transaction details.
67
+ * @param {string} params.sourceTXID - The transaction ID of the source UTXO.
68
+ * @param {number} params.sourceOutputIndex - The index of the output in the source transaction.
69
+ * @param {BigNumber} params.sourceSatoshis - The amount of satoshis in the source UTXO.
70
+ * @param {LockingScript} params.lockingScript - The locking script associated with the UTXO.
71
+ * @param {number} params.transactionVersion - The version of the current transaction.
72
+ * @param {Array<{ sourceTXID: string, sourceOutputIndex: number, sequence: number }>} params.otherInputs -
73
+ * An array of other inputs in the transaction.
74
+ * @param {Array<{ satoshis: BigNumber, lockingScript: LockingScript }>} params.outputs -
75
+ * The outputs of the current transaction.
76
+ * @param {number} params.inputIndex - The index of this input in the current transaction.
77
+ * @param {UnlockingScript} params.unlockingScript - The unlocking script for this spend.
78
+ * @param {number} params.inputSequence - The sequence number of this input.
79
+ * @param {number} params.lockTime - The lock time of the transaction.
80
+ *
81
+ * @example
82
+ * const spend = new Spend({
83
+ * sourceTXID: "abcd1234", // sourceTXID
84
+ * sourceOutputIndex: 0, // sourceOutputIndex
85
+ * sourceSatoshis: new BigNumber(1000), // sourceSatoshis
86
+ * lockingScript: LockingScript.fromASM("OP_DUP OP_HASH160 abcd1234... OP_EQUALVERIFY OP_CHECKSIG"),
87
+ * transactionVersion: 1, // transactionVersion
88
+ * otherInputs: [{ sourceTXID: "abcd1234", sourceOutputIndex: 1, sequence: 0xffffffff }], // otherInputs
89
+ * outputs: [{ satoshis: new BigNumber(500), lockingScript: LockingScript.fromASM("OP_DUP...") }], // outputs
90
+ * inputIndex: 0, // inputIndex
91
+ * unlockingScript: UnlockingScript.fromASM("3045... 02ab..."),
92
+ * inputSequence: 0xffffffff // inputSequence
93
+ * });
94
+ */
95
+ constructor(params) {
96
+ this.sourceTXID = params.sourceTXID;
97
+ this.sourceOutputIndex = params.sourceOutputIndex;
98
+ this.sourceSatoshis = params.sourceSatoshis;
99
+ this.lockingScript = params.lockingScript;
100
+ this.transactionVersion = params.transactionVersion;
101
+ this.otherInputs = params.otherInputs;
102
+ this.outputs = params.outputs;
103
+ this.inputIndex = params.inputIndex;
104
+ this.unlockingScript = params.unlockingScript;
105
+ this.inputSequence = params.inputSequence;
106
+ this.lockTime = params.lockTime;
107
+ this.reset();
108
+ }
109
+ reset() {
110
+ this.context = 'UnlockingScript';
111
+ this.programCounter = 0;
112
+ this.lastCodeSeparator = null;
113
+ this.stack = [];
114
+ this.altStack = [];
115
+ this.ifStack = [];
116
+ }
117
+ step() {
118
+ // If the context is UnlockingScript and we have reached the end,
119
+ // set the context to LockingScript and zero the program counter
120
+ if (this.context === 'UnlockingScript' &&
121
+ this.programCounter >= this.unlockingScript.chunks.length) {
122
+ this.context = 'LockingScript';
123
+ this.programCounter = 0;
124
+ }
125
+ let operation;
126
+ if (this.context === 'UnlockingScript') {
127
+ operation = this.unlockingScript.chunks[this.programCounter];
128
+ }
129
+ else {
130
+ operation = this.lockingScript.chunks[this.programCounter];
131
+ }
132
+ const isOpcodeDisabled = (op) => {
133
+ return op === OP_js_1.default.OP_2MUL ||
134
+ op === OP_js_1.default.OP_2DIV ||
135
+ op === OP_js_1.default.OP_VERIF ||
136
+ op === OP_js_1.default.OP_VERNOTIF ||
137
+ op === OP_js_1.default.OP_VER;
138
+ };
139
+ const isChunkMinimal = (chunk) => {
140
+ const data = chunk.data;
141
+ const op = chunk.op;
142
+ if (!Array.isArray(data)) {
143
+ return true;
144
+ }
145
+ if (data.length === 0) {
146
+ // Could have used OP_0.
147
+ return op === OP_js_1.default.OP_0;
148
+ }
149
+ else if (data.length === 1 && data[0] >= 1 && data[0] <= 16) {
150
+ // Could have used OP_1 .. OP_16.
151
+ return op === OP_js_1.default.OP_1 + (data[0] - 1);
152
+ }
153
+ else if (data.length === 1 && data[0] === 0x81) {
154
+ // Could have used OP_1NEGATE.
155
+ return op === OP_js_1.default.OP_1NEGATE;
156
+ }
157
+ else if (data.length <= 75) {
158
+ // Could have used a direct push (opCode indicating number of bytes pushed + those bytes).
159
+ return op === data.length;
160
+ }
161
+ else if (data.length <= 255) {
162
+ // Could have used OP_PUSHDATA.
163
+ return op === OP_js_1.default.OP_PUSHDATA1;
164
+ }
165
+ else if (data.length <= 65535) {
166
+ // Could have used OP_PUSHDATA2.
167
+ return op === OP_js_1.default.OP_PUSHDATA2;
168
+ }
169
+ return true;
170
+ };
171
+ // NOTE: Is 4 still the default maximum number size?
172
+ // In Genesis, was it lifted to 750000?
173
+ const isMinimallyEncoded = (buf, maxNumSize = 4) => {
174
+ if (buf.length > maxNumSize) {
175
+ return false;
176
+ }
177
+ if (buf.length > 0) {
178
+ // Check that the number is encoded with the minimum possible number
179
+ // of bytes.
180
+ //
181
+ // If the most-significant-byte - excluding the sign bit - is zero
182
+ // then we're not minimal. Note how this test also rejects the
183
+ // negative-zero encoding, 0x80.
184
+ if ((buf[buf.length - 1] & 0x7f) === 0) {
185
+ // One exception: if there's more than one byte and the most
186
+ // significant bit of the second-most-significant-byte is set it
187
+ // would conflict with the sign bit. An example of this case is
188
+ // +-255, which encode to 0xff00 and 0xff80 respectively.
189
+ // (big-endian).
190
+ if (buf.length <= 1 || (buf[buf.length - 2] & 0x80) === 0) {
191
+ return false;
192
+ }
193
+ }
194
+ }
195
+ return true;
196
+ };
197
+ const minimallyEncode = (buf) => {
198
+ if (buf.length === 0) {
199
+ return buf;
200
+ }
201
+ // If the last byte is not 0x00 or 0x80, we are minimally encoded.
202
+ const last = buf[buf.length - 1];
203
+ if ((last & 0x7f) !== 0) {
204
+ return buf;
205
+ }
206
+ // If the script is one byte long, then we have a zero, which encodes as an
207
+ // empty array.
208
+ if (buf.length === 1) {
209
+ return [];
210
+ }
211
+ // If the next byte has it sign bit set, then we are minimaly encoded.
212
+ if ((buf[buf.length - 2] & 0x80) !== 0) {
213
+ return buf;
214
+ }
215
+ // We are not minimally encoded, we need to figure out how much to trim.
216
+ for (let i = buf.length - 1; i > 0; i--) {
217
+ // We found a non zero byte, time to encode.
218
+ if (buf[i - 1] !== 0) {
219
+ if ((buf[i - 1] & 0x80) !== 0) {
220
+ // We found a byte with it sign bit set so we need one more
221
+ // byte.
222
+ buf[i++] = last;
223
+ }
224
+ else {
225
+ // the sign bit is clear, we can use it.
226
+ buf[i - 1] |= last;
227
+ }
228
+ return buf.slice(0, i);
229
+ }
230
+ }
231
+ // If we found the whole thing is zeros, then we have a zero.
232
+ return [];
233
+ };
234
+ const padDataToSize = (buf, len) => {
235
+ let b = buf;
236
+ while (b.length < len) {
237
+ b = [0x00, ...b];
238
+ }
239
+ return b;
240
+ };
241
+ /**
242
+ * This function is translated from bitcoind's IsDERSignature and is used in
243
+ * the script interpreter. This "DER" format actually includes an extra byte,
244
+ * the nHashType, at the end. It is really the tx format, not DER format.
245
+ *
246
+ * A canonical signature exists of: [30] [total len] [02] [len R] [R] [02] [len S] [S] [hashtype]
247
+ * Where R and S are not negative (their first byte has its highest bit not set), and not
248
+ * excessively padded (do not start with a 0 byte, unless an otherwise negative number follows,
249
+ * in which case a single 0 byte is necessary and even required).
250
+ *
251
+ * See https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623
252
+ */
253
+ const isChecksigFormat = (buf) => {
254
+ if (buf.length < 9) {
255
+ // Non-canonical signature: too short
256
+ return false;
257
+ }
258
+ if (buf.length > 73) {
259
+ // Non-canonical signature: too long
260
+ return false;
261
+ }
262
+ if (buf[0] !== 0x30) {
263
+ // Non-canonical signature: wrong type
264
+ return false;
265
+ }
266
+ if (buf[1] !== buf.length - 3) {
267
+ // Non-canonical signature: wrong length marker
268
+ return false;
269
+ }
270
+ const nLEnR = buf[3];
271
+ if (5 + nLEnR >= buf.length) {
272
+ // Non-canonical signature: S length misplaced
273
+ return false;
274
+ }
275
+ const nLEnS = buf[5 + nLEnR];
276
+ if (nLEnR + nLEnS + 7 !== buf.length) {
277
+ // Non-canonical signature: R+S length mismatch
278
+ return false;
279
+ }
280
+ const R = buf.slice(4);
281
+ if (buf[4 - 2] !== 0x02) {
282
+ // Non-canonical signature: R value type mismatch
283
+ return false;
284
+ }
285
+ if (nLEnR === 0) {
286
+ // Non-canonical signature: R length is zero
287
+ return false;
288
+ }
289
+ if ((R[0] & 0x80) !== 0) {
290
+ // Non-canonical signature: R value negative
291
+ return false;
292
+ }
293
+ if (nLEnR > 1 && R[0] === 0x00 && (R[1] & 0x80) === 0) {
294
+ // Non-canonical signature: R value excessively padded
295
+ return false;
296
+ }
297
+ const S = buf.slice(6 + nLEnR);
298
+ if (buf[6 + nLEnR - 2] !== 0x02) {
299
+ // Non-canonical signature: S value type mismatch
300
+ return false;
301
+ }
302
+ if (nLEnS === 0) {
303
+ // Non-canonical signature: S length is zero
304
+ return false;
305
+ }
306
+ if ((S[0] & 0x80) !== 0) {
307
+ // Non-canonical signature: S value negative
308
+ return false;
309
+ }
310
+ if (nLEnS > 1 && S[0] === 0x00 && (S[1] & 0x80) === 0) {
311
+ // Non-canonical signature: S value excessively padded
312
+ return false;
313
+ }
314
+ return true;
315
+ };
316
+ const checkSignatureEncoding = (buf) => {
317
+ // Empty signature. Not strictly DER encoded, but allowed to provide a
318
+ // compact way to provide an invalid signature for use with CHECK(MULTI)SIG
319
+ if (buf.length === 0) {
320
+ return true;
321
+ }
322
+ if (!isChecksigFormat(buf)) {
323
+ this.scriptEvaluationError('The signature format is invalid.');
324
+ }
325
+ const sig = TransactionSignature_js_1.default.fromChecksigFormat(buf);
326
+ if (requireLowSSignatures && !sig.hasLowS()) {
327
+ this.scriptEvaluationError('The signature must have a low S value.');
328
+ }
329
+ if ((sig.scope & TransactionSignature_js_1.default.SIGHASH_FORKID) === 0) {
330
+ this.scriptEvaluationError('The signature must use SIGHASH_FORKID.');
331
+ return false;
332
+ }
333
+ return true;
334
+ };
335
+ const checkPublicKeyEncoding = (buf) => {
336
+ if (buf.length < 33) {
337
+ this.scriptEvaluationError('The public key is too short, it must be at least 33 bytes.');
338
+ }
339
+ if (buf[0] === 0x04) {
340
+ if (buf.length !== 65) {
341
+ this.scriptEvaluationError('The non-compressed public key must be 65 bytes.');
342
+ }
343
+ }
344
+ else if ((buf[0] === 0x02 || buf[0] === 0x03)) {
345
+ if (buf.length !== 33) {
346
+ this.scriptEvaluationError('The compressed public key must be 33 bytes.');
347
+ }
348
+ }
349
+ else {
350
+ this.scriptEvaluationError('The public key is in an unknown format.');
351
+ }
352
+ return true;
353
+ };
354
+ const verifySignature = (sig, pubkey, subscript) => {
355
+ const preimage = TransactionSignature_js_1.default.format({
356
+ sourceTXID: this.sourceTXID,
357
+ sourceOutputIndex: this.sourceOutputIndex,
358
+ sourceSatoshis: this.sourceSatoshis,
359
+ transactionVersion: this.transactionVersion,
360
+ otherInputs: this.otherInputs,
361
+ outputs: this.outputs,
362
+ inputIndex: this.inputIndex,
363
+ subscript,
364
+ inputSequence: this.inputSequence,
365
+ lockTime: this.lockTime,
366
+ scope: sig.scope
367
+ });
368
+ const hash = new BigNumber_js_1.default(Hash.hash256(preimage));
369
+ return (0, ECDSA_js_1.verify)(hash, sig, pubkey);
370
+ };
371
+ const isScriptExecuting = !this.ifStack.includes(false);
372
+ let buf, buf1, buf2, buf3, spliced, n, size, rawnum, num, signbit, x1, x2, x3, bn, bn1, bn2, bn3, bufSig, bufPubkey, subscript, bufHash;
373
+ let sig, pubkey, i, fOk, nKeysCount, ikey, ikey2, nSigsCount, isig;
374
+ let fValue, fEqual, fSuccess;
375
+ // Read instruction
376
+ const currentOpcode = operation.op;
377
+ if (typeof currentOpcode === 'undefined') {
378
+ this.scriptEvaluationError(`An opcode is missing in this chunk of the ${this.context}!`);
379
+ }
380
+ if (Array.isArray(operation.data) &&
381
+ operation.data.length > maxScriptElementSize) {
382
+ this.scriptEvaluationError(`It's not currently possible to push data larger than ${maxScriptElementSize} bytes.`);
383
+ }
384
+ if (isScriptExecuting && isOpcodeDisabled(currentOpcode)) {
385
+ this.scriptEvaluationError('This opcode is currently disabled.');
386
+ }
387
+ if (isScriptExecuting && currentOpcode >= 0 &&
388
+ currentOpcode <= OP_js_1.default.OP_PUSHDATA4) {
389
+ if (requireMinimalPush && !isChunkMinimal(operation)) {
390
+ this.scriptEvaluationError('This data is not minimally-encoded.');
391
+ }
392
+ if (!Array.isArray(operation.data)) {
393
+ this.stack.push([]);
394
+ }
395
+ else {
396
+ this.stack.push(operation.data);
397
+ }
398
+ }
399
+ else if (isScriptExecuting || (OP_js_1.default.OP_IF <= currentOpcode && currentOpcode <= OP_js_1.default.OP_ENDIF)) {
400
+ switch (currentOpcode) {
401
+ case OP_js_1.default.OP_1NEGATE:
402
+ case OP_js_1.default.OP_1:
403
+ case OP_js_1.default.OP_2:
404
+ case OP_js_1.default.OP_3:
405
+ case OP_js_1.default.OP_4:
406
+ case OP_js_1.default.OP_5:
407
+ case OP_js_1.default.OP_6:
408
+ case OP_js_1.default.OP_7:
409
+ case OP_js_1.default.OP_8:
410
+ case OP_js_1.default.OP_9:
411
+ case OP_js_1.default.OP_10:
412
+ case OP_js_1.default.OP_11:
413
+ case OP_js_1.default.OP_12:
414
+ case OP_js_1.default.OP_13:
415
+ case OP_js_1.default.OP_14:
416
+ case OP_js_1.default.OP_15:
417
+ case OP_js_1.default.OP_16:
418
+ n = currentOpcode - (OP_js_1.default.OP_1 - 1);
419
+ buf = new BigNumber_js_1.default(n).toScriptNum();
420
+ this.stack.push(buf);
421
+ break;
422
+ case OP_js_1.default.OP_NOP:
423
+ case OP_js_1.default.OP_NOP2:
424
+ case OP_js_1.default.OP_NOP3:
425
+ case OP_js_1.default.OP_NOP1:
426
+ case OP_js_1.default.OP_NOP4:
427
+ case OP_js_1.default.OP_NOP5:
428
+ case OP_js_1.default.OP_NOP6:
429
+ case OP_js_1.default.OP_NOP7:
430
+ case OP_js_1.default.OP_NOP8:
431
+ case OP_js_1.default.OP_NOP9:
432
+ case OP_js_1.default.OP_NOP10:
433
+ case OP_js_1.default.OP_NOP11:
434
+ case OP_js_1.default.OP_NOP12:
435
+ case OP_js_1.default.OP_NOP13:
436
+ case OP_js_1.default.OP_NOP14:
437
+ case OP_js_1.default.OP_NOP15:
438
+ case OP_js_1.default.OP_NOP16:
439
+ case OP_js_1.default.OP_NOP17:
440
+ case OP_js_1.default.OP_NOP18:
441
+ case OP_js_1.default.OP_NOP19:
442
+ case OP_js_1.default.OP_NOP20:
443
+ case OP_js_1.default.OP_NOP21:
444
+ case OP_js_1.default.OP_NOP22:
445
+ case OP_js_1.default.OP_NOP23:
446
+ case OP_js_1.default.OP_NOP24:
447
+ case OP_js_1.default.OP_NOP25:
448
+ case OP_js_1.default.OP_NOP26:
449
+ case OP_js_1.default.OP_NOP27:
450
+ case OP_js_1.default.OP_NOP28:
451
+ case OP_js_1.default.OP_NOP29:
452
+ case OP_js_1.default.OP_NOP30:
453
+ case OP_js_1.default.OP_NOP31:
454
+ case OP_js_1.default.OP_NOP32:
455
+ case OP_js_1.default.OP_NOP33:
456
+ case OP_js_1.default.OP_NOP34:
457
+ case OP_js_1.default.OP_NOP35:
458
+ case OP_js_1.default.OP_NOP36:
459
+ case OP_js_1.default.OP_NOP37:
460
+ case OP_js_1.default.OP_NOP38:
461
+ case OP_js_1.default.OP_NOP39:
462
+ case OP_js_1.default.OP_NOP40:
463
+ case OP_js_1.default.OP_NOP41:
464
+ case OP_js_1.default.OP_NOP42:
465
+ case OP_js_1.default.OP_NOP43:
466
+ case OP_js_1.default.OP_NOP44:
467
+ case OP_js_1.default.OP_NOP45:
468
+ case OP_js_1.default.OP_NOP46:
469
+ case OP_js_1.default.OP_NOP47:
470
+ case OP_js_1.default.OP_NOP48:
471
+ case OP_js_1.default.OP_NOP49:
472
+ case OP_js_1.default.OP_NOP50:
473
+ case OP_js_1.default.OP_NOP51:
474
+ case OP_js_1.default.OP_NOP52:
475
+ case OP_js_1.default.OP_NOP53:
476
+ case OP_js_1.default.OP_NOP54:
477
+ case OP_js_1.default.OP_NOP55:
478
+ case OP_js_1.default.OP_NOP56:
479
+ case OP_js_1.default.OP_NOP57:
480
+ case OP_js_1.default.OP_NOP58:
481
+ case OP_js_1.default.OP_NOP59:
482
+ case OP_js_1.default.OP_NOP60:
483
+ case OP_js_1.default.OP_NOP61:
484
+ case OP_js_1.default.OP_NOP62:
485
+ case OP_js_1.default.OP_NOP63:
486
+ case OP_js_1.default.OP_NOP64:
487
+ case OP_js_1.default.OP_NOP65:
488
+ case OP_js_1.default.OP_NOP66:
489
+ case OP_js_1.default.OP_NOP67:
490
+ case OP_js_1.default.OP_NOP68:
491
+ case OP_js_1.default.OP_NOP69:
492
+ case OP_js_1.default.OP_NOP70:
493
+ case OP_js_1.default.OP_NOP71:
494
+ case OP_js_1.default.OP_NOP72:
495
+ case OP_js_1.default.OP_NOP73:
496
+ case OP_js_1.default.OP_NOP77:
497
+ break;
498
+ case OP_js_1.default.OP_IF:
499
+ case OP_js_1.default.OP_NOTIF:
500
+ fValue = false;
501
+ if (isScriptExecuting) {
502
+ if (this.stack.length < 1) {
503
+ this.scriptEvaluationError('OP_IF and OP_NOTIF require at least one item on the stack when they are used!');
504
+ }
505
+ buf = this.stacktop(-1);
506
+ fValue = this.castToBool(buf);
507
+ if (currentOpcode === OP_js_1.default.OP_NOTIF) {
508
+ fValue = !fValue;
509
+ }
510
+ this.stack.pop();
511
+ }
512
+ this.ifStack.push(fValue);
513
+ break;
514
+ case OP_js_1.default.OP_ELSE:
515
+ if (this.ifStack.length === 0) {
516
+ this.scriptEvaluationError('OP_ELSE requires a preceeding OP_IF.');
517
+ }
518
+ this.ifStack[this.ifStack.length - 1] = !this.ifStack[this.ifStack.length - 1];
519
+ break;
520
+ case OP_js_1.default.OP_ENDIF:
521
+ if (this.ifStack.length === 0) {
522
+ this.scriptEvaluationError('OP_ENDIF requires a preceeding OP_IF.');
523
+ }
524
+ this.ifStack.pop();
525
+ break;
526
+ case OP_js_1.default.OP_VERIFY:
527
+ if (this.stack.length < 1) {
528
+ this.scriptEvaluationError('OP_VERIFY requires at least one item to be on the stack.');
529
+ }
530
+ buf = this.stacktop(-1);
531
+ fValue = this.castToBool(buf);
532
+ if (fValue) {
533
+ this.stack.pop();
534
+ }
535
+ else {
536
+ this.scriptEvaluationError('OP_VERIFY requires the top stack value to be truthy.');
537
+ }
538
+ break;
539
+ case OP_js_1.default.OP_RETURN:
540
+ if (this.context === 'UnlockingScript') {
541
+ this.programCounter = this.unlockingScript.chunks.length;
542
+ }
543
+ else {
544
+ this.programCounter = this.lockingScript.chunks.length;
545
+ }
546
+ break;
547
+ case OP_js_1.default.OP_TOALTSTACK:
548
+ if (this.stack.length < 1) {
549
+ this.scriptEvaluationError('OP_TOALTSTACK requires at oeast one item to be on the stack.');
550
+ }
551
+ this.altStack.push(this.stack.pop());
552
+ break;
553
+ case OP_js_1.default.OP_FROMALTSTACK:
554
+ if (this.altStack.length < 1) {
555
+ this.scriptEvaluationError('OP_FROMALTSTACK requires at least one item to be on the stack.');
556
+ }
557
+ this.stack.push(this.altStack.pop());
558
+ break;
559
+ case OP_js_1.default.OP_2DROP:
560
+ if (this.stack.length < 2) {
561
+ this.scriptEvaluationError('OP_2DROP requires at least two items to be on the stack.');
562
+ }
563
+ this.stack.pop();
564
+ this.stack.pop();
565
+ break;
566
+ case OP_js_1.default.OP_2DUP:
567
+ if (this.stack.length < 2) {
568
+ this.scriptEvaluationError('OP_2DUP requires at least two items to be on the stack.');
569
+ }
570
+ buf1 = this.stacktop(-2);
571
+ buf2 = this.stacktop(-1);
572
+ this.stack.push([...buf1]);
573
+ this.stack.push([...buf2]);
574
+ break;
575
+ case OP_js_1.default.OP_3DUP:
576
+ if (this.stack.length < 3) {
577
+ this.scriptEvaluationError('OP_3DUP requires at least three items to be on the stack.');
578
+ }
579
+ buf1 = this.stacktop(-3);
580
+ buf2 = this.stacktop(-2);
581
+ buf3 = this.stacktop(-1);
582
+ this.stack.push([...buf1]);
583
+ this.stack.push([...buf2]);
584
+ this.stack.push([...buf3]);
585
+ break;
586
+ case OP_js_1.default.OP_2OVER:
587
+ if (this.stack.length < 4) {
588
+ this.scriptEvaluationError('OP_2OVER requires at least four items to be on the stack.');
589
+ }
590
+ buf1 = this.stacktop(-4);
591
+ buf2 = this.stacktop(-3);
592
+ this.stack.push([...buf1]);
593
+ this.stack.push([...buf2]);
594
+ break;
595
+ case OP_js_1.default.OP_2ROT:
596
+ if (this.stack.length < 6) {
597
+ this.scriptEvaluationError('OP_2ROT requires at least six items to be on the stack.');
598
+ }
599
+ spliced = this.stack.splice(this.stack.length - 6, 2);
600
+ this.stack.push(spliced[0]);
601
+ this.stack.push(spliced[1]);
602
+ break;
603
+ case OP_js_1.default.OP_2SWAP:
604
+ if (this.stack.length < 4) {
605
+ this.scriptEvaluationError('OP_2SWAP requires at least four items to be on the stack.');
606
+ }
607
+ spliced = this.stack.splice(this.stack.length - 4, 2);
608
+ this.stack.push(spliced[0]);
609
+ this.stack.push(spliced[1]);
610
+ break;
611
+ case OP_js_1.default.OP_IFDUP:
612
+ if (this.stack.length < 1) {
613
+ this.scriptEvaluationError('OP_IFDUP requires at least one item to be on the stack.');
614
+ }
615
+ buf = this.stacktop(-1);
616
+ fValue = this.castToBool(buf);
617
+ if (fValue) {
618
+ this.stack.push([...buf]);
619
+ }
620
+ break;
621
+ case OP_js_1.default.OP_DEPTH:
622
+ buf = new BigNumber_js_1.default(this.stack.length).toScriptNum();
623
+ this.stack.push(buf);
624
+ break;
625
+ case OP_js_1.default.OP_DROP:
626
+ if (this.stack.length < 1) {
627
+ this.scriptEvaluationError('OP_DROP requires at least one item to be on the stack.');
628
+ }
629
+ this.stack.pop();
630
+ break;
631
+ case OP_js_1.default.OP_DUP:
632
+ if (this.stack.length < 1) {
633
+ this.scriptEvaluationError('OP_DUP requires at least one item to be on the stack.');
634
+ }
635
+ this.stack.push([...this.stacktop(-1)]);
636
+ break;
637
+ case OP_js_1.default.OP_NIP:
638
+ if (this.stack.length < 2) {
639
+ this.scriptEvaluationError('OP_NIP requires at least two items to be on the stack.');
640
+ }
641
+ this.stack.splice(this.stack.length - 2, 1);
642
+ break;
643
+ case OP_js_1.default.OP_OVER:
644
+ if (this.stack.length < 2) {
645
+ this.scriptEvaluationError('OP_OVER requires at least two items to be on the stack.');
646
+ }
647
+ this.stack.push([...this.stacktop(-2)]);
648
+ break;
649
+ case OP_js_1.default.OP_PICK:
650
+ case OP_js_1.default.OP_ROLL:
651
+ if (this.stack.length < 2) {
652
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires at least two items to be on the stack.`);
653
+ }
654
+ buf = this.stacktop(-1);
655
+ bn = BigNumber_js_1.default.fromScriptNum(buf, requireMinimalPush);
656
+ n = bn.toNumber();
657
+ this.stack.pop();
658
+ if (n < 0 || n >= this.stack.length) {
659
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires the top stack element to be 0 or a positive number less than the current size of the stack.`);
660
+ }
661
+ buf = this.stacktop(-n - 1);
662
+ if (currentOpcode === OP_js_1.default.OP_ROLL) {
663
+ this.stack.splice(this.stack.length - n - 1, 1);
664
+ }
665
+ this.stack.push([...buf]);
666
+ break;
667
+ case OP_js_1.default.OP_ROT:
668
+ if (this.stack.length < 3) {
669
+ this.scriptEvaluationError('OP_ROT requires at least three items to be on the stack.');
670
+ }
671
+ x1 = this.stacktop(-3);
672
+ x2 = this.stacktop(-2);
673
+ x3 = this.stacktop(-1);
674
+ this.stack[this.stack.length - 3] = x2;
675
+ this.stack[this.stack.length - 2] = x3;
676
+ this.stack[this.stack.length - 1] = x1;
677
+ break;
678
+ case OP_js_1.default.OP_SWAP:
679
+ if (this.stack.length < 2) {
680
+ this.scriptEvaluationError('OP_SWAP requires at least two items to be on the stack.');
681
+ }
682
+ x1 = this.stacktop(-2);
683
+ x2 = this.stacktop(-1);
684
+ this.stack[this.stack.length - 2] = x2;
685
+ this.stack[this.stack.length - 1] = x1;
686
+ break;
687
+ case OP_js_1.default.OP_TUCK:
688
+ if (this.stack.length < 2) {
689
+ this.scriptEvaluationError('OP_TUCK requires at least two items to be on the stack.');
690
+ }
691
+ this.stack.splice(this.stack.length - 2, 0, [...this.stacktop(-1)]);
692
+ break;
693
+ case OP_js_1.default.OP_SIZE:
694
+ if (this.stack.length < 1) {
695
+ this.scriptEvaluationError('OP_SIZE requires at least one item to be on the stack.');
696
+ }
697
+ bn = new BigNumber_js_1.default(this.stacktop(-1).length);
698
+ this.stack.push(bn.toScriptNum());
699
+ break;
700
+ case OP_js_1.default.OP_AND:
701
+ case OP_js_1.default.OP_OR:
702
+ case OP_js_1.default.OP_XOR:
703
+ if (this.stack.length < 2) {
704
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires at least one item to be on the stack.`);
705
+ }
706
+ buf1 = this.stacktop(-2);
707
+ buf2 = this.stacktop(-1);
708
+ if (buf1.length !== buf2.length) {
709
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires the top two stack items to be the same size.`);
710
+ }
711
+ switch (currentOpcode) {
712
+ case OP_js_1.default.OP_AND:
713
+ for (let i = 0; i < buf1.length; i++) {
714
+ buf1[i] &= buf2[i];
715
+ }
716
+ break;
717
+ case OP_js_1.default.OP_OR:
718
+ for (let i = 0; i < buf1.length; i++) {
719
+ buf1[i] |= buf2[i];
720
+ }
721
+ break;
722
+ case OP_js_1.default.OP_XOR:
723
+ for (let i = 0; i < buf1.length; i++) {
724
+ buf1[i] ^= buf2[i];
725
+ }
726
+ break;
727
+ }
728
+ // And pop vch2.
729
+ this.stack.pop();
730
+ break;
731
+ case OP_js_1.default.OP_INVERT:
732
+ if (this.stack.length < 1) {
733
+ this.scriptEvaluationError('OP_INVERT requires at least one item to be on the stack.');
734
+ }
735
+ buf = this.stacktop(-1);
736
+ for (let i = 0; i < buf.length; i++) {
737
+ buf[i] = ~buf[i];
738
+ }
739
+ break;
740
+ case OP_js_1.default.OP_LSHIFT:
741
+ case OP_js_1.default.OP_RSHIFT:
742
+ if (this.stack.length < 2) {
743
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires at least two items to be on the stack.`);
744
+ }
745
+ buf1 = this.stacktop(-2);
746
+ if (buf1.length === 0) {
747
+ this.stack.pop();
748
+ }
749
+ else {
750
+ bn1 = new BigNumber_js_1.default(buf1);
751
+ bn2 = BigNumber_js_1.default.fromScriptNum(this.stacktop(-1), requireMinimalPush);
752
+ n = bn2.toNumber();
753
+ if (n < 0) {
754
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires the top item on the stack not to be negative.`);
755
+ }
756
+ this.stack.pop();
757
+ this.stack.pop();
758
+ let shifted;
759
+ if (currentOpcode === OP_js_1.default.OP_LSHIFT) {
760
+ shifted = bn1.ushln(n);
761
+ }
762
+ if (currentOpcode === OP_js_1.default.OP_RSHIFT) {
763
+ shifted = bn1.ushrn(n);
764
+ }
765
+ const bufShifted = padDataToSize([...shifted.toArray().slice(buf1.length * -1)], buf1.length);
766
+ this.stack.push(bufShifted);
767
+ }
768
+ break;
769
+ case OP_js_1.default.OP_EQUAL:
770
+ case OP_js_1.default.OP_EQUALVERIFY:
771
+ if (this.stack.length < 2) {
772
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires at least two items to be on the stack.`);
773
+ }
774
+ buf1 = this.stacktop(-2);
775
+ buf2 = this.stacktop(-1);
776
+ fEqual = (0, utils_js_1.toHex)(buf1) === (0, utils_js_1.toHex)(buf2);
777
+ this.stack.pop();
778
+ this.stack.pop();
779
+ this.stack.push(fEqual ? [1] : []);
780
+ if (currentOpcode === OP_js_1.default.OP_EQUALVERIFY) {
781
+ if (fEqual) {
782
+ this.stack.pop();
783
+ }
784
+ else {
785
+ this.scriptEvaluationError('OP_EQUALVERIFY requires the top two stack items to be equal.');
786
+ }
787
+ }
788
+ break;
789
+ case OP_js_1.default.OP_1ADD:
790
+ case OP_js_1.default.OP_1SUB:
791
+ case OP_js_1.default.OP_NEGATE:
792
+ case OP_js_1.default.OP_ABS:
793
+ case OP_js_1.default.OP_NOT:
794
+ case OP_js_1.default.OP_0NOTEQUAL:
795
+ if (this.stack.length < 1) {
796
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires at least one items to be on the stack.`);
797
+ }
798
+ buf = this.stacktop(-1);
799
+ bn = BigNumber_js_1.default.fromScriptNum(buf, requireMinimalPush);
800
+ switch (currentOpcode) {
801
+ case OP_js_1.default.OP_1ADD:
802
+ bn = bn.addn(1);
803
+ break;
804
+ case OP_js_1.default.OP_1SUB:
805
+ bn = bn.subn(1);
806
+ break;
807
+ case OP_js_1.default.OP_NEGATE:
808
+ bn = bn.neg();
809
+ break;
810
+ case OP_js_1.default.OP_ABS:
811
+ if (bn.cmpn(0) < 0) {
812
+ bn = bn.neg();
813
+ }
814
+ break;
815
+ case OP_js_1.default.OP_NOT:
816
+ bn = new BigNumber_js_1.default((bn.cmpn(0) === 0) ? 1 : 0 + 0);
817
+ break;
818
+ case OP_js_1.default.OP_0NOTEQUAL:
819
+ bn = new BigNumber_js_1.default((bn.cmpn(0) !== 0) ? 1 : 0 + 0);
820
+ break;
821
+ }
822
+ this.stack.pop();
823
+ this.stack.push(bn.toScriptNum());
824
+ break;
825
+ case OP_js_1.default.OP_ADD:
826
+ case OP_js_1.default.OP_SUB:
827
+ case OP_js_1.default.OP_MUL:
828
+ case OP_js_1.default.OP_MOD:
829
+ case OP_js_1.default.OP_DIV:
830
+ case OP_js_1.default.OP_BOOLAND:
831
+ case OP_js_1.default.OP_BOOLOR:
832
+ case OP_js_1.default.OP_NUMEQUAL:
833
+ case OP_js_1.default.OP_NUMEQUALVERIFY:
834
+ case OP_js_1.default.OP_NUMNOTEQUAL:
835
+ case OP_js_1.default.OP_LESSTHAN:
836
+ case OP_js_1.default.OP_GREATERTHAN:
837
+ case OP_js_1.default.OP_LESSTHANOREQUAL:
838
+ case OP_js_1.default.OP_GREATERTHANOREQUAL:
839
+ case OP_js_1.default.OP_MIN:
840
+ case OP_js_1.default.OP_MAX:
841
+ if (this.stack.length < 2) {
842
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires at least two items to be on the stack.`);
843
+ }
844
+ bn1 = BigNumber_js_1.default.fromScriptNum(this.stacktop(-2), requireMinimalPush);
845
+ bn2 = BigNumber_js_1.default.fromScriptNum(this.stacktop(-1), requireMinimalPush);
846
+ bn = new BigNumber_js_1.default(0);
847
+ switch (currentOpcode) {
848
+ case OP_js_1.default.OP_ADD:
849
+ bn = bn1.add(bn2);
850
+ break;
851
+ case OP_js_1.default.OP_SUB:
852
+ bn = bn1.sub(bn2);
853
+ break;
854
+ case OP_js_1.default.OP_MUL:
855
+ bn = bn1.mul(bn2);
856
+ break;
857
+ case OP_js_1.default.OP_DIV:
858
+ if (bn2.cmpn(0) === 0) {
859
+ this.scriptEvaluationError('OP_DIV cannot divide by zero!');
860
+ }
861
+ bn = bn1.div(bn2);
862
+ break;
863
+ case OP_js_1.default.OP_MOD:
864
+ if (bn2.cmpn(0) === 0) {
865
+ this.scriptEvaluationError('OP_MOD cannot divide by zero!');
866
+ }
867
+ bn = bn1.mod(bn2);
868
+ break;
869
+ case OP_js_1.default.OP_BOOLAND:
870
+ bn = new BigNumber_js_1.default(((bn1.cmpn(0) !== 0) && (bn2.cmpn(0) !== 0)) ? 1 : 0 + 0);
871
+ break;
872
+ case OP_js_1.default.OP_BOOLOR:
873
+ bn = new BigNumber_js_1.default(((bn1.cmpn(0) !== 0) || (bn2.cmpn(0) !== 0)) ? 1 : 0 + 0);
874
+ break;
875
+ case OP_js_1.default.OP_NUMEQUAL:
876
+ bn = new BigNumber_js_1.default((bn1.cmp(bn2) === 0) ? 1 : 0 + 0);
877
+ break;
878
+ case OP_js_1.default.OP_NUMEQUALVERIFY:
879
+ bn = new BigNumber_js_1.default((bn1.cmp(bn2) === 0) ? 1 : 0 + 0);
880
+ break;
881
+ case OP_js_1.default.OP_NUMNOTEQUAL:
882
+ bn = new BigNumber_js_1.default((bn1.cmp(bn2) !== 0) ? 1 : 0 + 0);
883
+ break;
884
+ case OP_js_1.default.OP_LESSTHAN:
885
+ bn = new BigNumber_js_1.default((bn1.cmp(bn2) < 0) ? 1 : 0 + 0);
886
+ break;
887
+ case OP_js_1.default.OP_GREATERTHAN:
888
+ bn = new BigNumber_js_1.default((bn1.cmp(bn2) > 0) ? 1 : 0 + 0);
889
+ break;
890
+ case OP_js_1.default.OP_LESSTHANOREQUAL:
891
+ bn = new BigNumber_js_1.default((bn1.cmp(bn2) <= 0) ? 1 : 0 + 0);
892
+ break;
893
+ case OP_js_1.default.OP_GREATERTHANOREQUAL:
894
+ bn = new BigNumber_js_1.default((bn1.cmp(bn2) >= 0) ? 1 : 0 + 0);
895
+ break;
896
+ case OP_js_1.default.OP_MIN:
897
+ bn = (bn1.cmp(bn2) < 0 ? bn1 : bn2);
898
+ break;
899
+ case OP_js_1.default.OP_MAX:
900
+ bn = (bn1.cmp(bn2) > 0 ? bn1 : bn2);
901
+ break;
902
+ }
903
+ this.stack.pop();
904
+ this.stack.pop();
905
+ this.stack.push(bn.toScriptNum());
906
+ if (currentOpcode === OP_js_1.default.OP_NUMEQUALVERIFY) {
907
+ if (this.castToBool(this.stacktop(-1))) {
908
+ this.stack.pop();
909
+ }
910
+ else {
911
+ this.scriptEvaluationError('OP_NUMEQUALVERIFY requires the top stack item to be truthy.');
912
+ }
913
+ }
914
+ break;
915
+ case OP_js_1.default.OP_WITHIN:
916
+ if (this.stack.length < 3) {
917
+ this.scriptEvaluationError('OP_WITHIN requires at least three items to be on the stack.');
918
+ }
919
+ bn1 = BigNumber_js_1.default.fromScriptNum(this.stacktop(-3), requireMinimalPush);
920
+ bn2 = BigNumber_js_1.default.fromScriptNum(this.stacktop(-2), requireMinimalPush);
921
+ bn3 = BigNumber_js_1.default.fromScriptNum(this.stacktop(-1), requireMinimalPush);
922
+ fValue = (bn2.cmp(bn1) <= 0) && (bn1.cmp(bn3) < 0);
923
+ this.stack.pop();
924
+ this.stack.pop();
925
+ this.stack.pop();
926
+ this.stack.push(fValue ? [1] : []);
927
+ break;
928
+ case OP_js_1.default.OP_RIPEMD160:
929
+ case OP_js_1.default.OP_SHA1:
930
+ case OP_js_1.default.OP_SHA256:
931
+ case OP_js_1.default.OP_HASH160:
932
+ case OP_js_1.default.OP_HASH256:
933
+ if (this.stack.length < 1) {
934
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires at least one item to be on the stack.`);
935
+ }
936
+ buf = this.stacktop(-1);
937
+ if (currentOpcode === OP_js_1.default.OP_RIPEMD160) {
938
+ bufHash = Hash.ripemd160(buf);
939
+ }
940
+ else if (currentOpcode === OP_js_1.default.OP_SHA1) {
941
+ bufHash = Hash.sha1(buf);
942
+ }
943
+ else if (currentOpcode === OP_js_1.default.OP_SHA256) {
944
+ bufHash = Hash.sha256(buf);
945
+ }
946
+ else if (currentOpcode === OP_js_1.default.OP_HASH160) {
947
+ bufHash = Hash.hash160(buf);
948
+ }
949
+ else if (currentOpcode === OP_js_1.default.OP_HASH256) {
950
+ bufHash = Hash.hash256(buf);
951
+ }
952
+ this.stack.pop();
953
+ this.stack.push(bufHash);
954
+ break;
955
+ case OP_js_1.default.OP_CODESEPARATOR:
956
+ this.lastCodeSeparator = this.programCounter;
957
+ break;
958
+ case OP_js_1.default.OP_CHECKSIG:
959
+ case OP_js_1.default.OP_CHECKSIGVERIFY:
960
+ if (this.stack.length < 2) {
961
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires at least two items to be on the stack.`);
962
+ }
963
+ bufSig = this.stacktop(-2);
964
+ bufPubkey = this.stacktop(-1);
965
+ if (!checkSignatureEncoding(bufSig) ||
966
+ !checkPublicKeyEncoding(bufPubkey)) {
967
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires correct encoding for the public key and signature.`);
968
+ }
969
+ // Subset of script starting at the most recent codeseparator
970
+ // CScript scriptCode(pbegincodehash, pend);
971
+ if (this.context === 'UnlockingScript') {
972
+ subscript = new Script_js_1.default(this.unlockingScript.chunks.slice(this.lastCodeSeparator));
973
+ }
974
+ else {
975
+ subscript = new Script_js_1.default(this.lockingScript.chunks.slice(this.lastCodeSeparator));
976
+ }
977
+ // Drop the signature, since there's no way for a signature to sign itself
978
+ subscript.findAndDelete(new Script_js_1.default().writeBin(bufSig));
979
+ try {
980
+ sig = TransactionSignature_js_1.default.fromChecksigFormat(bufSig);
981
+ pubkey = PublicKey_js_1.default.fromString((0, utils_js_1.toHex)(bufPubkey));
982
+ fSuccess = verifySignature(sig, pubkey, subscript);
983
+ }
984
+ catch (e) {
985
+ // invalid sig or pubkey
986
+ fSuccess = false;
987
+ }
988
+ if (!fSuccess && bufSig.length > 0) {
989
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} failed to verify the signature, and requires an empty signature when verification fails.`);
990
+ }
991
+ this.stack.pop();
992
+ this.stack.pop();
993
+ // stack.push_back(fSuccess ? vchTrue : vchFalse);
994
+ this.stack.push(fSuccess ? [1] : []);
995
+ if (currentOpcode === OP_js_1.default.OP_CHECKSIGVERIFY) {
996
+ if (fSuccess) {
997
+ this.stack.pop();
998
+ }
999
+ else {
1000
+ this.scriptEvaluationError('OP_CHECKSIGVERIFY requires that a valid signature is provided.');
1001
+ }
1002
+ }
1003
+ break;
1004
+ case OP_js_1.default.OP_CHECKMULTISIG:
1005
+ case OP_js_1.default.OP_CHECKMULTISIGVERIFY:
1006
+ i = 1;
1007
+ if (this.stack.length < i) {
1008
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires at least 1 item to be on the stack.`);
1009
+ }
1010
+ nKeysCount = BigNumber_js_1.default.fromScriptNum(this.stacktop(-i), requireMinimalPush).toNumber();
1011
+ // TODO: Keys and opcount are parameterized in client. No magic numbers!
1012
+ if (nKeysCount < 0 || nKeysCount > maxMultisigKeyCount) {
1013
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires a key count between 0 and ${maxMultisigKeyCount}.`);
1014
+ }
1015
+ ikey = ++i;
1016
+ i += nKeysCount;
1017
+ // ikey2 is the position of last non-signature item in
1018
+ // the stack. Top stack item = 1. With
1019
+ // SCRIPT_VERIFY_NULLFAIL, this is used for cleanup if
1020
+ // operation fails.
1021
+ ikey2 = nKeysCount + 2;
1022
+ if (this.stack.length < i) {
1023
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires the number of stack items not to be less than the number of keys used.`);
1024
+ }
1025
+ nSigsCount = BigNumber_js_1.default.fromScriptNum(this.stacktop(-i), requireMinimalPush).toNumber();
1026
+ if (nSigsCount < 0 || nSigsCount > nKeysCount) {
1027
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires the number of signatures to be no greater than the number of keys.`);
1028
+ }
1029
+ isig = ++i;
1030
+ i += nSigsCount;
1031
+ if (this.stack.length < i) {
1032
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires the number of stack items not to be less than the number of signatures provided.`);
1033
+ }
1034
+ // Subset of script starting at the most recent codeseparator
1035
+ if (this.context === 'UnlockingScript') {
1036
+ subscript = new Script_js_1.default(this.unlockingScript.chunks.slice(this.lastCodeSeparator));
1037
+ }
1038
+ else {
1039
+ subscript = new Script_js_1.default(this.lockingScript.chunks.slice(this.lastCodeSeparator));
1040
+ }
1041
+ // Drop the signatures, since there's no way for a signature to sign itself
1042
+ for (let k = 0; k < nSigsCount; k++) {
1043
+ bufSig = this.stacktop(-isig - k);
1044
+ subscript.findAndDelete(new Script_js_1.default().writeBin(bufSig));
1045
+ }
1046
+ fSuccess = true;
1047
+ while (fSuccess && nSigsCount > 0) {
1048
+ // valtype& vchSig = this.stacktop(-isig);
1049
+ bufSig = this.stacktop(-isig);
1050
+ // valtype& vchPubKey = this.stacktop(-ikey);
1051
+ bufPubkey = this.stacktop(-ikey);
1052
+ if (!checkSignatureEncoding(bufSig) ||
1053
+ !checkPublicKeyEncoding(bufPubkey)) {
1054
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires correct encoding for the public key and signature.`);
1055
+ }
1056
+ try {
1057
+ sig = TransactionSignature_js_1.default.fromChecksigFormat(bufSig);
1058
+ pubkey = PublicKey_js_1.default.fromString((0, utils_js_1.toHex)(bufPubkey));
1059
+ fOk = verifySignature(sig, pubkey, subscript);
1060
+ }
1061
+ catch (e) {
1062
+ // invalid sig or pubkey
1063
+ fOk = false;
1064
+ }
1065
+ if (fOk) {
1066
+ isig++;
1067
+ nSigsCount--;
1068
+ }
1069
+ ikey++;
1070
+ nKeysCount--;
1071
+ // If there are more signatures left than keys left,
1072
+ // then too many signatures have failed
1073
+ if (nSigsCount > nKeysCount) {
1074
+ fSuccess = false;
1075
+ }
1076
+ }
1077
+ // Clean up stack of actual arguments
1078
+ while (i-- > 1) {
1079
+ if (!fSuccess && !ikey2 && (this.stacktop(-1).length > 0)) {
1080
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} failed to verify a signature, and requires an empty signature when verification fails.`);
1081
+ }
1082
+ if (ikey2 > 0) {
1083
+ ikey2--;
1084
+ }
1085
+ this.stack.pop();
1086
+ }
1087
+ // A bug causes CHECKMULTISIG to consume one extra argument
1088
+ // whose contents were not checked in any way.
1089
+ //
1090
+ // Unfortunately this is a potential source of mutability,
1091
+ // so optionally verify it is exactly equal to zero prior
1092
+ // to removing it from the stack.
1093
+ if (this.stack.length < 1) {
1094
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires an extra item to be on the stack.`);
1095
+ }
1096
+ if (this.stacktop(-1).length > 0) { // NOTE: Is this necessary? We don't care about malleability.
1097
+ this.scriptEvaluationError(`${OP_js_1.default[currentOpcode]} requires the extra stack item to be empty.`);
1098
+ }
1099
+ this.stack.pop();
1100
+ this.stack.push(fSuccess ? [1] : []);
1101
+ if (currentOpcode === OP_js_1.default.OP_CHECKMULTISIGVERIFY) {
1102
+ if (fSuccess) {
1103
+ this.stack.pop();
1104
+ }
1105
+ else {
1106
+ this.scriptEvaluationError('OP_CHECKMULTISIGVERIFY requires that a sufficient number of valid signatures are provided.');
1107
+ }
1108
+ }
1109
+ break;
1110
+ case OP_js_1.default.OP_CAT:
1111
+ if (this.stack.length < 2) {
1112
+ this.scriptEvaluationError('OP_CAT requires at least two items to be on the stack.');
1113
+ }
1114
+ buf1 = this.stacktop(-2);
1115
+ buf2 = this.stacktop(-1);
1116
+ if (buf1.length + buf2.length > maxScriptElementSize) {
1117
+ this.scriptEvaluationError(`It's not currently possible to push data larger than ${maxScriptElementSize} bytes.`);
1118
+ }
1119
+ this.stack[this.stack.length - 2] = [...buf1, ...buf2];
1120
+ this.stack.pop();
1121
+ break;
1122
+ case OP_js_1.default.OP_SPLIT:
1123
+ if (this.stack.length < 2) {
1124
+ this.scriptEvaluationError('OP_SPLIT requires at least two items to be on the stack.');
1125
+ }
1126
+ buf1 = this.stacktop(-2);
1127
+ // Make sure the split point is apropriate.
1128
+ n = BigNumber_js_1.default.fromScriptNum(this.stacktop(-1), requireMinimalPush).toNumber();
1129
+ if (n < 0 || n > buf1.length) {
1130
+ this.scriptEvaluationError('OP_SPLIT requires the first stack item to be a non-negative number less than or equal to the size of the second-from-top stack item.');
1131
+ }
1132
+ // Prepare the results in their own buffer as `data`
1133
+ // will be invalidated.
1134
+ // Copy buffer data, to slice it before
1135
+ buf2 = [...buf1];
1136
+ // Replace existing stack values by the new values.
1137
+ this.stack[this.stack.length - 2] = buf2.slice(0, n);
1138
+ this.stack[this.stack.length - 1] = buf2.slice(n);
1139
+ break;
1140
+ case OP_js_1.default.OP_NUM2BIN:
1141
+ if (this.stack.length < 2) {
1142
+ this.scriptEvaluationError('OP_NUM2BIN requires at least two items to be on the stack.');
1143
+ }
1144
+ size = BigNumber_js_1.default.fromScriptNum(this.stacktop(-1), requireMinimalPush).toNumber();
1145
+ if (size > maxScriptElementSize) {
1146
+ this.scriptEvaluationError(`It's not currently possible to push data larger than ${maxScriptElementSize} bytes.`);
1147
+ }
1148
+ this.stack.pop();
1149
+ rawnum = this.stacktop(-1);
1150
+ // Try to see if we can fit that number in the number of
1151
+ // byte requested.
1152
+ rawnum = minimallyEncode(rawnum);
1153
+ if (rawnum.length > size) {
1154
+ this.scriptEvaluationError('OP_NUM2BIN requires that the size expressed in the top stack item is large enough to hold the value expressed in the second-from-top stack item.');
1155
+ }
1156
+ // We already have an element of the right size, we
1157
+ // don't need to do anything.
1158
+ if (rawnum.length === size) {
1159
+ this.stack[this.stack.length - 1] = rawnum;
1160
+ break;
1161
+ }
1162
+ signbit = 0x00;
1163
+ if (rawnum.length > 0) {
1164
+ signbit = rawnum[rawnum.length - 1] & 0x80;
1165
+ rawnum[rawnum.length - 1] &= 0x7f;
1166
+ }
1167
+ num = new Array(size);
1168
+ num.fill(0);
1169
+ for (n = 0; n < size; n++) {
1170
+ num[n] = rawnum[n];
1171
+ }
1172
+ n = rawnum.length - 1;
1173
+ while (n++ < size - 2) {
1174
+ num[n] = 0x00;
1175
+ }
1176
+ num[n] = signbit;
1177
+ this.stack[this.stack.length - 1] = num;
1178
+ break;
1179
+ case OP_js_1.default.OP_BIN2NUM:
1180
+ if (this.stack.length < 1) {
1181
+ this.scriptEvaluationError('OP_BIN2NUM requires at least one item to be on the stack.');
1182
+ }
1183
+ buf1 = this.stacktop(-1);
1184
+ buf2 = minimallyEncode(buf1);
1185
+ this.stack[this.stack.length - 1] = buf2;
1186
+ // The resulting number must be a valid number.
1187
+ if (!isMinimallyEncoded(buf2)) {
1188
+ this.scriptEvaluationError('OP_BIN2NUM requires that the resulting number is valid.');
1189
+ }
1190
+ break;
1191
+ default:
1192
+ this.scriptEvaluationError('Invalid opcode!');
1193
+ }
1194
+ }
1195
+ // Finally, increment the program counter
1196
+ this.programCounter++;
1197
+ }
1198
+ /**
1199
+ * @method validate
1200
+ * Validates the spend action by interpreting the locking and unlocking scripts.
1201
+ * @returns {boolean} Returns true if the scripts are valid and the spend is legitimate, otherwise false.
1202
+ * @example
1203
+ * if (spend.validate()) {
1204
+ * console.log("Spend is valid!");
1205
+ * } else {
1206
+ * console.log("Invalid spend!");
1207
+ * }
1208
+ */
1209
+ validate() {
1210
+ if (requirePushOnlyUnlockingScripts && !this.unlockingScript.isPushOnly()) {
1211
+ this.scriptEvaluationError('Unlocking scripts can only contain push operations, and no other opcodes.');
1212
+ }
1213
+ while (true) {
1214
+ this.step();
1215
+ if (this.context === 'LockingScript' && this.programCounter >= this.lockingScript.chunks.length) {
1216
+ break;
1217
+ }
1218
+ }
1219
+ if (this.ifStack.length > 0) {
1220
+ this.scriptEvaluationError('Every OP_IF must be terminated prior to the end of the script.');
1221
+ }
1222
+ if (requireCleanStack) {
1223
+ if (this.stack.length !== 1) {
1224
+ this.scriptEvaluationError('The clean stack rule requires exactly one item to be on the stack after script execution.');
1225
+ }
1226
+ }
1227
+ if (!this.castToBool(this.stacktop(-1))) {
1228
+ this.scriptEvaluationError('The top stack element must be truthy after script evaluation.');
1229
+ }
1230
+ return true;
1231
+ }
1232
+ stacktop(i) {
1233
+ return this.stack[this.stack.length + i];
1234
+ }
1235
+ castToBool(val) {
1236
+ for (let i = 0; i < val.length; i++) {
1237
+ if (val[i] !== 0) {
1238
+ // can be negative zero
1239
+ if (i === val.length - 1 && val[i] === 0x80) {
1240
+ return false;
1241
+ }
1242
+ return true;
1243
+ }
1244
+ }
1245
+ return false;
1246
+ }
1247
+ scriptEvaluationError(str) {
1248
+ throw new Error(`Script evaluation error: ${str}\n\nSource TXID: ${this.sourceTXID}\nSource output index: ${this.sourceOutputIndex}\nContext: ${this.context}\nProgram counter: ${this.programCounter}\nStack size: ${this.stack.length}\nAlt stack size: ${this.altStack.length}`);
1249
+ }
1250
+ }
1251
+ exports.default = Spend;
1252
+ //# sourceMappingURL=Spend.js.map