@opcat-labs/scrypt-ts-opcat 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 (537) hide show
  1. package/assets/.templates/smart-contract/builtin-libs/TxHashPreimageUtils.scrypt.map +1 -0
  2. package/assets/.templates/smart-contract/builtin-libs/TxHashPreimageUtils.scrypt.tpl +18 -0
  3. package/assets/.templates/smart-contract/builtin-libs/TxHashPreimageUtils.transformer.json +7 -0
  4. package/assets/.templates/smart-contract/builtin-libs/backtrace.scrypt.map +1 -0
  5. package/assets/.templates/smart-contract/builtin-libs/backtrace.scrypt.tpl +36 -0
  6. package/assets/.templates/smart-contract/builtin-libs/backtrace.transformer.json +7 -0
  7. package/assets/.templates/smart-contract/builtin-libs/byteStringReader.scrypt.map +1 -0
  8. package/assets/.templates/smart-contract/builtin-libs/byteStringReader.scrypt.tpl +64 -0
  9. package/assets/.templates/smart-contract/builtin-libs/byteStringReader.transformer.json +7 -0
  10. package/assets/.templates/smart-contract/builtin-libs/byteStringWriter.scrypt.map +1 -0
  11. package/assets/.templates/smart-contract/builtin-libs/byteStringWriter.scrypt.tpl +48 -0
  12. package/assets/.templates/smart-contract/builtin-libs/byteStringWriter.transformer.json +7 -0
  13. package/assets/.templates/smart-contract/builtin-libs/contextUtils.scrypt.map +1 -0
  14. package/assets/.templates/smart-contract/builtin-libs/contextUtils.scrypt.tpl +101 -0
  15. package/assets/.templates/smart-contract/builtin-libs/contextUtils.transformer.json +7 -0
  16. package/assets/.templates/smart-contract/builtin-libs/p2pk.scrypt.map +1 -0
  17. package/assets/.templates/smart-contract/builtin-libs/p2pk.scrypt.tpl +7 -0
  18. package/assets/.templates/smart-contract/builtin-libs/p2pk.transformer.json +7 -0
  19. package/assets/.templates/smart-contract/builtin-libs/p2pkh.scrypt.map +1 -0
  20. package/assets/.templates/smart-contract/builtin-libs/p2pkh.scrypt.tpl +8 -0
  21. package/assets/.templates/smart-contract/builtin-libs/p2pkh.transformer.json +7 -0
  22. package/assets/.templates/smart-contract/builtin-libs/stateLib.scrypt.map +1 -0
  23. package/assets/.templates/smart-contract/builtin-libs/stateLib.scrypt.tpl +3 -0
  24. package/assets/.templates/smart-contract/builtin-libs/stateLib.transformer.json +7 -0
  25. package/assets/.templates/smart-contract/builtin-libs/stateUtils.scrypt.map +1 -0
  26. package/assets/.templates/smart-contract/builtin-libs/stateUtils.scrypt.tpl +7 -0
  27. package/assets/.templates/smart-contract/builtin-libs/stateUtils.transformer.json +7 -0
  28. package/assets/.templates/smart-contract/builtin-libs/stdUtils.scrypt.map +1 -0
  29. package/assets/.templates/smart-contract/builtin-libs/stdUtils.scrypt.tpl +89 -0
  30. package/assets/.templates/smart-contract/builtin-libs/stdUtils.transformer.json +7 -0
  31. package/assets/.templates/smart-contract/builtin-libs/txUtils.scrypt.map +1 -0
  32. package/assets/.templates/smart-contract/builtin-libs/txUtils.scrypt.tpl +42 -0
  33. package/assets/.templates/smart-contract/builtin-libs/txUtils.transformer.json +7 -0
  34. package/assets/.templates/smart-contract/types/structs.scrypt.map +1 -0
  35. package/assets/.templates/smart-contract/types/structs.scrypt.tpl +48 -0
  36. package/assets/.templates/smart-contract/types/structs.transformer.json +7 -0
  37. package/assets/smart-contract/builtin-libs/TxHashPreimageUtils.scrypt +20 -0
  38. package/assets/smart-contract/builtin-libs/backtrace.scrypt +38 -0
  39. package/assets/smart-contract/builtin-libs/byteStringReader.scrypt +66 -0
  40. package/assets/smart-contract/builtin-libs/byteStringWriter.scrypt +50 -0
  41. package/assets/smart-contract/builtin-libs/contextUtils.scrypt +103 -0
  42. package/assets/smart-contract/builtin-libs/p2pk.scrypt +9 -0
  43. package/assets/smart-contract/builtin-libs/p2pkh.scrypt +10 -0
  44. package/assets/smart-contract/builtin-libs/stateLib.scrypt +5 -0
  45. package/assets/smart-contract/builtin-libs/stateUtils.scrypt +9 -0
  46. package/assets/smart-contract/builtin-libs/stdUtils.scrypt +91 -0
  47. package/assets/smart-contract/builtin-libs/txUtils.scrypt +44 -0
  48. package/assets/smart-contract/types/structs.scrypt +50 -0
  49. package/dist/cjs/features/call.cjs +38 -0
  50. package/dist/cjs/features/call.js.map +1 -0
  51. package/dist/cjs/features/deploy.cjs +33 -0
  52. package/dist/cjs/features/deploy.js.map +1 -0
  53. package/dist/cjs/features/index.cjs +8 -0
  54. package/dist/cjs/features/index.js.map +1 -0
  55. package/dist/cjs/globalTypes.cjs +3 -0
  56. package/dist/cjs/globalTypes.js.map +1 -0
  57. package/dist/cjs/index.cjs +55 -0
  58. package/dist/cjs/index.js.map +1 -0
  59. package/dist/cjs/networks.cjs +69 -0
  60. package/dist/cjs/networks.js.map +1 -0
  61. package/dist/cjs/providers/chainProvider.cjs +3 -0
  62. package/dist/cjs/providers/chainProvider.js.map +1 -0
  63. package/dist/cjs/providers/dummyProvider.cjs +87 -0
  64. package/dist/cjs/providers/dummyProvider.js.map +1 -0
  65. package/dist/cjs/providers/index.cjs +13 -0
  66. package/dist/cjs/providers/index.js.map +1 -0
  67. package/dist/cjs/providers/mempoolProvider.cjs +207 -0
  68. package/dist/cjs/providers/mempoolProvider.js.map +1 -0
  69. package/dist/cjs/providers/rpcProvider.cjs +296 -0
  70. package/dist/cjs/providers/rpcProvider.js.map +1 -0
  71. package/dist/cjs/providers/utxoProvider.cjs +18 -0
  72. package/dist/cjs/providers/utxoProvider.js.map +1 -0
  73. package/dist/cjs/psbt/bip66.cjs +182 -0
  74. package/dist/cjs/psbt/bip66.js.map +1 -0
  75. package/dist/cjs/psbt/bufferutils.cjs +186 -0
  76. package/dist/cjs/psbt/bufferutils.js.map +1 -0
  77. package/dist/cjs/psbt/contextProvider.cjs +111 -0
  78. package/dist/cjs/psbt/contextProvider.js.map +1 -0
  79. package/dist/cjs/psbt/extPsbt.cjs +589 -0
  80. package/dist/cjs/psbt/extPsbt.js.map +1 -0
  81. package/dist/cjs/psbt/psbt.cjs +973 -0
  82. package/dist/cjs/psbt/psbt.js.map +1 -0
  83. package/dist/cjs/psbt/psbtutils.cjs +140 -0
  84. package/dist/cjs/psbt/psbtutils.js.map +1 -0
  85. package/dist/cjs/psbt/signatureutils.cjs +230 -0
  86. package/dist/cjs/psbt/signatureutils.js.map +1 -0
  87. package/dist/cjs/psbt/types.cjs +3 -0
  88. package/dist/cjs/psbt/types.js.map +1 -0
  89. package/dist/cjs/signer.cjs +3 -0
  90. package/dist/cjs/signer.js.map +1 -0
  91. package/dist/cjs/signers/defaultSigner.cjs +68 -0
  92. package/dist/cjs/signers/defaultSigner.js.map +1 -0
  93. package/dist/cjs/signers/index.cjs +8 -0
  94. package/dist/cjs/signers/index.js.map +1 -0
  95. package/dist/cjs/signers/unisatSigner.cjs +38 -0
  96. package/dist/cjs/signers/unisatSigner.js.map +1 -0
  97. package/dist/cjs/smart-contract/abi.cjs +380 -0
  98. package/dist/cjs/smart-contract/abi.js.map +1 -0
  99. package/dist/cjs/smart-contract/abiutils.cjs +183 -0
  100. package/dist/cjs/smart-contract/abiutils.js.map +1 -0
  101. package/dist/cjs/smart-contract/abstractContract.cjs +25 -0
  102. package/dist/cjs/smart-contract/abstractContract.js.map +1 -0
  103. package/dist/cjs/smart-contract/builtin-libs/backtrace.cjs +102 -0
  104. package/dist/cjs/smart-contract/builtin-libs/backtrace.js.map +1 -0
  105. package/dist/cjs/smart-contract/builtin-libs/byteStringReader.cjs +138 -0
  106. package/dist/cjs/smart-contract/builtin-libs/byteStringReader.js.map +1 -0
  107. package/dist/cjs/smart-contract/builtin-libs/byteStringWriter.cjs +109 -0
  108. package/dist/cjs/smart-contract/builtin-libs/byteStringWriter.js.map +1 -0
  109. package/dist/cjs/smart-contract/builtin-libs/contextUtils.cjs +304 -0
  110. package/dist/cjs/smart-contract/builtin-libs/contextUtils.js.map +1 -0
  111. package/dist/cjs/smart-contract/builtin-libs/index.cjs +18 -0
  112. package/dist/cjs/smart-contract/builtin-libs/index.js.map +1 -0
  113. package/dist/cjs/smart-contract/builtin-libs/p2pk.cjs +74 -0
  114. package/dist/cjs/smart-contract/builtin-libs/p2pk.js.map +1 -0
  115. package/dist/cjs/smart-contract/builtin-libs/p2pkh.cjs +80 -0
  116. package/dist/cjs/smart-contract/builtin-libs/p2pkh.js.map +1 -0
  117. package/dist/cjs/smart-contract/builtin-libs/stateLib.cjs +65 -0
  118. package/dist/cjs/smart-contract/builtin-libs/stateLib.js.map +1 -0
  119. package/dist/cjs/smart-contract/builtin-libs/stateUtils.cjs +42 -0
  120. package/dist/cjs/smart-contract/builtin-libs/stateUtils.js.map +1 -0
  121. package/dist/cjs/smart-contract/builtin-libs/stdUtils.cjs +192 -0
  122. package/dist/cjs/smart-contract/builtin-libs/stdUtils.js.map +1 -0
  123. package/dist/cjs/smart-contract/builtin-libs/txHashPreimageUtils.cjs +56 -0
  124. package/dist/cjs/smart-contract/builtin-libs/txHashPreimageUtils.js.map +1 -0
  125. package/dist/cjs/smart-contract/builtin-libs/txUtils.cjs +183 -0
  126. package/dist/cjs/smart-contract/builtin-libs/txUtils.js.map +1 -0
  127. package/dist/cjs/smart-contract/consts.cjs +100 -0
  128. package/dist/cjs/smart-contract/consts.js.map +1 -0
  129. package/dist/cjs/smart-contract/decorators.cjs +83 -0
  130. package/dist/cjs/smart-contract/decorators.js.map +1 -0
  131. package/dist/cjs/smart-contract/fns/assert.cjs +17 -0
  132. package/dist/cjs/smart-contract/fns/assert.js.map +1 -0
  133. package/dist/cjs/smart-contract/fns/bitwise.cjs +141 -0
  134. package/dist/cjs/smart-contract/fns/bitwise.js.map +1 -0
  135. package/dist/cjs/smart-contract/fns/byteString.cjs +106 -0
  136. package/dist/cjs/smart-contract/fns/byteString.js.map +1 -0
  137. package/dist/cjs/smart-contract/fns/equals.cjs +66 -0
  138. package/dist/cjs/smart-contract/fns/equals.js.map +1 -0
  139. package/dist/cjs/smart-contract/fns/fill.cjs +19 -0
  140. package/dist/cjs/smart-contract/fns/fill.js.map +1 -0
  141. package/dist/cjs/smart-contract/fns/hashes.cjs +81 -0
  142. package/dist/cjs/smart-contract/fns/hashes.js.map +1 -0
  143. package/dist/cjs/smart-contract/fns/index.cjs +37 -0
  144. package/dist/cjs/smart-contract/fns/index.js.map +1 -0
  145. package/dist/cjs/smart-contract/fns/math.cjs +52 -0
  146. package/dist/cjs/smart-contract/fns/math.js.map +1 -0
  147. package/dist/cjs/smart-contract/fns/pubKey2Addr.cjs +17 -0
  148. package/dist/cjs/smart-contract/fns/pubKey2Addr.js.map +1 -0
  149. package/dist/cjs/smart-contract/methods/backtraceToGenensis.cjs +41 -0
  150. package/dist/cjs/smart-contract/methods/backtraceToGenensis.js.map +1 -0
  151. package/dist/cjs/smart-contract/methods/buildOutput.cjs +41 -0
  152. package/dist/cjs/smart-contract/methods/buildOutput.js.map +1 -0
  153. package/dist/cjs/smart-contract/methods/checkCtx.cjs +58 -0
  154. package/dist/cjs/smart-contract/methods/checkCtx.js.map +1 -0
  155. package/dist/cjs/smart-contract/methods/checkInputState.cjs +16 -0
  156. package/dist/cjs/smart-contract/methods/checkInputState.js.map +1 -0
  157. package/dist/cjs/smart-contract/methods/checkInputStateHashes.cjs +16 -0
  158. package/dist/cjs/smart-contract/methods/checkInputStateHashes.js.map +1 -0
  159. package/dist/cjs/smart-contract/methods/checkSHPreimage.cjs +17 -0
  160. package/dist/cjs/smart-contract/methods/checkSHPreimage.js.map +1 -0
  161. package/dist/cjs/smart-contract/methods/checkSig.cjs +77 -0
  162. package/dist/cjs/smart-contract/methods/checkSig.js.map +1 -0
  163. package/dist/cjs/smart-contract/serializer.cjs +145 -0
  164. package/dist/cjs/smart-contract/serializer.js.map +1 -0
  165. package/dist/cjs/smart-contract/smartContract.cjs +568 -0
  166. package/dist/cjs/smart-contract/smartContract.js.map +1 -0
  167. package/dist/cjs/smart-contract/smartContractLib.cjs +46 -0
  168. package/dist/cjs/smart-contract/smartContractLib.js.map +1 -0
  169. package/dist/cjs/smart-contract/stateSerializer.cjs +134 -0
  170. package/dist/cjs/smart-contract/stateSerializer.js.map +1 -0
  171. package/dist/cjs/smart-contract/types/abi.cjs +69 -0
  172. package/dist/cjs/smart-contract/types/abi.js.map +1 -0
  173. package/dist/cjs/smart-contract/types/artifact.cjs +16 -0
  174. package/dist/cjs/smart-contract/types/artifact.js.map +1 -0
  175. package/dist/cjs/smart-contract/types/bn.cjs +117 -0
  176. package/dist/cjs/smart-contract/types/bn.js.map +1 -0
  177. package/dist/cjs/smart-contract/types/context.cjs +3 -0
  178. package/dist/cjs/smart-contract/types/context.js.map +1 -0
  179. package/dist/cjs/smart-contract/types/index.cjs +16 -0
  180. package/dist/cjs/smart-contract/types/index.js.map +1 -0
  181. package/dist/cjs/smart-contract/types/opCode.cjs +754 -0
  182. package/dist/cjs/smart-contract/types/opCode.js.map +1 -0
  183. package/dist/cjs/smart-contract/types/primitives.cjs +108 -0
  184. package/dist/cjs/smart-contract/types/primitives.js.map +1 -0
  185. package/dist/cjs/smart-contract/types/script.cjs +43 -0
  186. package/dist/cjs/smart-contract/types/script.js.map +1 -0
  187. package/dist/cjs/smart-contract/types/structs.cjs +3 -0
  188. package/dist/cjs/smart-contract/types/structs.js.map +1 -0
  189. package/dist/cjs/smart-contract/types/utils.cjs +21 -0
  190. package/dist/cjs/smart-contract/types/utils.js.map +1 -0
  191. package/dist/cjs/utils/bvm.cjs +30 -0
  192. package/dist/cjs/utils/bvm.js.map +1 -0
  193. package/dist/cjs/utils/checkIntegrity.cjs +49 -0
  194. package/dist/cjs/utils/checkIntegrity.js.map +1 -0
  195. package/dist/cjs/utils/common.cjs +228 -0
  196. package/dist/cjs/utils/common.js.map +1 -0
  197. package/dist/cjs/utils/constants.cjs +5 -0
  198. package/dist/cjs/utils/constants.js.map +1 -0
  199. package/dist/cjs/utils/index.cjs +45 -0
  200. package/dist/cjs/utils/index.js.map +1 -0
  201. package/dist/cjs/utils/preimage.cjs +93 -0
  202. package/dist/cjs/utils/preimage.js.map +1 -0
  203. package/dist/cjs/utils/proof.cjs +110 -0
  204. package/dist/cjs/utils/proof.js.map +1 -0
  205. package/dist/cjs/utils/script_number.cjs +107 -0
  206. package/dist/cjs/utils/script_number.js.map +1 -0
  207. package/dist/esm/features/call.js +34 -0
  208. package/dist/esm/features/call.js.map +1 -0
  209. package/dist/esm/features/deploy.js +29 -0
  210. package/dist/esm/features/deploy.js.map +1 -0
  211. package/dist/esm/features/index.js +3 -0
  212. package/dist/esm/features/index.js.map +1 -0
  213. package/dist/esm/globalTypes.js +2 -0
  214. package/dist/esm/globalTypes.js.map +1 -0
  215. package/dist/esm/index.js +23 -0
  216. package/dist/esm/index.js.map +1 -0
  217. package/dist/esm/networks.js +41 -0
  218. package/dist/esm/networks.js.map +1 -0
  219. package/dist/esm/providers/chainProvider.js +2 -0
  220. package/dist/esm/providers/chainProvider.js.map +1 -0
  221. package/dist/esm/providers/dummyProvider.js +60 -0
  222. package/dist/esm/providers/dummyProvider.js.map +1 -0
  223. package/dist/esm/providers/index.js +5 -0
  224. package/dist/esm/providers/index.js.map +1 -0
  225. package/dist/esm/providers/mempoolProvider.js +200 -0
  226. package/dist/esm/providers/mempoolProvider.js.map +1 -0
  227. package/dist/esm/providers/rpcProvider.js +266 -0
  228. package/dist/esm/providers/rpcProvider.js.map +1 -0
  229. package/dist/esm/providers/utxoProvider.js +13 -0
  230. package/dist/esm/providers/utxoProvider.js.map +1 -0
  231. package/dist/esm/psbt/bip66.js +175 -0
  232. package/dist/esm/psbt/bip66.js.map +1 -0
  233. package/dist/esm/psbt/bufferutils.js +156 -0
  234. package/dist/esm/psbt/bufferutils.js.map +1 -0
  235. package/dist/esm/psbt/contextProvider.js +84 -0
  236. package/dist/esm/psbt/contextProvider.js.map +1 -0
  237. package/dist/esm/psbt/extPsbt.js +562 -0
  238. package/dist/esm/psbt/extPsbt.js.map +1 -0
  239. package/dist/esm/psbt/psbt.js +945 -0
  240. package/dist/esm/psbt/psbt.js.map +1 -0
  241. package/dist/esm/psbt/psbtutils.js +110 -0
  242. package/dist/esm/psbt/psbtutils.js.map +1 -0
  243. package/dist/esm/psbt/signatureutils.js +199 -0
  244. package/dist/esm/psbt/signatureutils.js.map +1 -0
  245. package/dist/esm/psbt/types.js +2 -0
  246. package/dist/esm/psbt/types.js.map +1 -0
  247. package/dist/esm/signer.js +2 -0
  248. package/dist/esm/signer.js.map +1 -0
  249. package/dist/esm/signers/defaultSigner.js +63 -0
  250. package/dist/esm/signers/defaultSigner.js.map +1 -0
  251. package/dist/esm/signers/index.js +3 -0
  252. package/dist/esm/signers/index.js.map +1 -0
  253. package/dist/esm/signers/unisatSigner.js +34 -0
  254. package/dist/esm/signers/unisatSigner.js.map +1 -0
  255. package/dist/esm/smart-contract/abi.js +373 -0
  256. package/dist/esm/smart-contract/abi.js.map +1 -0
  257. package/dist/esm/smart-contract/abiutils.js +171 -0
  258. package/dist/esm/smart-contract/abiutils.js.map +1 -0
  259. package/dist/esm/smart-contract/abstractContract.js +21 -0
  260. package/dist/esm/smart-contract/abstractContract.js.map +1 -0
  261. package/dist/esm/smart-contract/builtin-libs/backtrace.js +98 -0
  262. package/dist/esm/smart-contract/builtin-libs/backtrace.js.map +1 -0
  263. package/dist/esm/smart-contract/builtin-libs/byteStringReader.js +134 -0
  264. package/dist/esm/smart-contract/builtin-libs/byteStringReader.js.map +1 -0
  265. package/dist/esm/smart-contract/builtin-libs/byteStringWriter.js +105 -0
  266. package/dist/esm/smart-contract/builtin-libs/byteStringWriter.js.map +1 -0
  267. package/dist/esm/smart-contract/builtin-libs/contextUtils.js +300 -0
  268. package/dist/esm/smart-contract/builtin-libs/contextUtils.js.map +1 -0
  269. package/dist/esm/smart-contract/builtin-libs/index.js +8 -0
  270. package/dist/esm/smart-contract/builtin-libs/index.js.map +1 -0
  271. package/dist/esm/smart-contract/builtin-libs/p2pk.js +70 -0
  272. package/dist/esm/smart-contract/builtin-libs/p2pk.js.map +1 -0
  273. package/dist/esm/smart-contract/builtin-libs/p2pkh.js +76 -0
  274. package/dist/esm/smart-contract/builtin-libs/p2pkh.js.map +1 -0
  275. package/dist/esm/smart-contract/builtin-libs/stateLib.js +61 -0
  276. package/dist/esm/smart-contract/builtin-libs/stateLib.js.map +1 -0
  277. package/dist/esm/smart-contract/builtin-libs/stateUtils.js +38 -0
  278. package/dist/esm/smart-contract/builtin-libs/stateUtils.js.map +1 -0
  279. package/dist/esm/smart-contract/builtin-libs/stdUtils.js +188 -0
  280. package/dist/esm/smart-contract/builtin-libs/stdUtils.js.map +1 -0
  281. package/dist/esm/smart-contract/builtin-libs/txHashPreimageUtils.js +52 -0
  282. package/dist/esm/smart-contract/builtin-libs/txHashPreimageUtils.js.map +1 -0
  283. package/dist/esm/smart-contract/builtin-libs/txUtils.js +179 -0
  284. package/dist/esm/smart-contract/builtin-libs/txUtils.js.map +1 -0
  285. package/dist/esm/smart-contract/consts.js +97 -0
  286. package/dist/esm/smart-contract/consts.js.map +1 -0
  287. package/dist/esm/smart-contract/decorators.js +78 -0
  288. package/dist/esm/smart-contract/decorators.js.map +1 -0
  289. package/dist/esm/smart-contract/fns/assert.js +13 -0
  290. package/dist/esm/smart-contract/fns/assert.js.map +1 -0
  291. package/dist/esm/smart-contract/fns/bitwise.js +109 -0
  292. package/dist/esm/smart-contract/fns/bitwise.js.map +1 -0
  293. package/dist/esm/smart-contract/fns/byteString.js +97 -0
  294. package/dist/esm/smart-contract/fns/byteString.js.map +1 -0
  295. package/dist/esm/smart-contract/fns/equals.js +62 -0
  296. package/dist/esm/smart-contract/fns/equals.js.map +1 -0
  297. package/dist/esm/smart-contract/fns/fill.js +15 -0
  298. package/dist/esm/smart-contract/fns/fill.js.map +1 -0
  299. package/dist/esm/smart-contract/fns/hashes.js +73 -0
  300. package/dist/esm/smart-contract/fns/hashes.js.map +1 -0
  301. package/dist/esm/smart-contract/fns/index.js +9 -0
  302. package/dist/esm/smart-contract/fns/index.js.map +1 -0
  303. package/dist/esm/smart-contract/fns/math.js +45 -0
  304. package/dist/esm/smart-contract/fns/math.js.map +1 -0
  305. package/dist/esm/smart-contract/fns/pubKey2Addr.js +13 -0
  306. package/dist/esm/smart-contract/fns/pubKey2Addr.js.map +1 -0
  307. package/dist/esm/smart-contract/methods/backtraceToGenensis.js +36 -0
  308. package/dist/esm/smart-contract/methods/backtraceToGenensis.js.map +1 -0
  309. package/dist/esm/smart-contract/methods/buildOutput.js +36 -0
  310. package/dist/esm/smart-contract/methods/buildOutput.js.map +1 -0
  311. package/dist/esm/smart-contract/methods/checkCtx.js +31 -0
  312. package/dist/esm/smart-contract/methods/checkCtx.js.map +1 -0
  313. package/dist/esm/smart-contract/methods/checkInputState.js +12 -0
  314. package/dist/esm/smart-contract/methods/checkInputState.js.map +1 -0
  315. package/dist/esm/smart-contract/methods/checkInputStateHashes.js +12 -0
  316. package/dist/esm/smart-contract/methods/checkInputStateHashes.js.map +1 -0
  317. package/dist/esm/smart-contract/methods/checkSHPreimage.js +13 -0
  318. package/dist/esm/smart-contract/methods/checkSHPreimage.js.map +1 -0
  319. package/dist/esm/smart-contract/methods/checkSig.js +73 -0
  320. package/dist/esm/smart-contract/methods/checkSig.js.map +1 -0
  321. package/dist/esm/smart-contract/serializer.js +112 -0
  322. package/dist/esm/smart-contract/serializer.js.map +1 -0
  323. package/dist/esm/smart-contract/smartContract.js +564 -0
  324. package/dist/esm/smart-contract/smartContract.js.map +1 -0
  325. package/dist/esm/smart-contract/smartContractLib.js +42 -0
  326. package/dist/esm/smart-contract/smartContractLib.js.map +1 -0
  327. package/dist/esm/smart-contract/stateSerializer.js +129 -0
  328. package/dist/esm/smart-contract/stateSerializer.js.map +1 -0
  329. package/dist/esm/smart-contract/types/abi.js +63 -0
  330. package/dist/esm/smart-contract/types/abi.js.map +1 -0
  331. package/dist/esm/smart-contract/types/artifact.js +13 -0
  332. package/dist/esm/smart-contract/types/artifact.js.map +1 -0
  333. package/dist/esm/smart-contract/types/bn.js +89 -0
  334. package/dist/esm/smart-contract/types/bn.js.map +1 -0
  335. package/dist/esm/smart-contract/types/context.js +2 -0
  336. package/dist/esm/smart-contract/types/context.js.map +1 -0
  337. package/dist/esm/smart-contract/types/index.js +3 -0
  338. package/dist/esm/smart-contract/types/index.js.map +1 -0
  339. package/dist/esm/smart-contract/types/opCode.js +750 -0
  340. package/dist/esm/smart-contract/types/opCode.js.map +1 -0
  341. package/dist/esm/smart-contract/types/primitives.js +95 -0
  342. package/dist/esm/smart-contract/types/primitives.js.map +1 -0
  343. package/dist/esm/smart-contract/types/script.js +16 -0
  344. package/dist/esm/smart-contract/types/script.js.map +1 -0
  345. package/dist/esm/smart-contract/types/structs.js +2 -0
  346. package/dist/esm/smart-contract/types/structs.js.map +1 -0
  347. package/dist/esm/smart-contract/types/utils.js +17 -0
  348. package/dist/esm/smart-contract/types/utils.js.map +1 -0
  349. package/dist/esm/utils/bvm.js +26 -0
  350. package/dist/esm/utils/bvm.js.map +1 -0
  351. package/dist/esm/utils/checkIntegrity.js +41 -0
  352. package/dist/esm/utils/checkIntegrity.js.map +1 -0
  353. package/dist/esm/utils/common.js +188 -0
  354. package/dist/esm/utils/common.js.map +1 -0
  355. package/dist/esm/utils/constants.js +2 -0
  356. package/dist/esm/utils/constants.js.map +1 -0
  357. package/dist/esm/utils/index.js +7 -0
  358. package/dist/esm/utils/index.js.map +1 -0
  359. package/dist/esm/utils/preimage.js +65 -0
  360. package/dist/esm/utils/preimage.js.map +1 -0
  361. package/dist/esm/utils/proof.js +98 -0
  362. package/dist/esm/utils/proof.js.map +1 -0
  363. package/dist/esm/utils/script_number.js +79 -0
  364. package/dist/esm/utils/script_number.js.map +1 -0
  365. package/dist/types/features/call.d.ts +21 -0
  366. package/dist/types/features/call.d.ts.map +1 -0
  367. package/dist/types/features/deploy.d.ts +16 -0
  368. package/dist/types/features/deploy.d.ts.map +1 -0
  369. package/dist/types/features/index.d.ts +3 -0
  370. package/dist/types/features/index.d.ts.map +1 -0
  371. package/dist/types/globalTypes.d.ts +37 -0
  372. package/dist/types/globalTypes.d.ts.map +1 -0
  373. package/dist/types/index.d.ts +18 -0
  374. package/dist/types/index.d.ts.map +1 -0
  375. package/dist/types/networks.d.ts +19 -0
  376. package/dist/types/networks.d.ts.map +1 -0
  377. package/dist/types/providers/chainProvider.d.ts +31 -0
  378. package/dist/types/providers/chainProvider.d.ts.map +1 -0
  379. package/dist/types/providers/dummyProvider.d.ts +23 -0
  380. package/dist/types/providers/dummyProvider.d.ts.map +1 -0
  381. package/dist/types/providers/index.d.ts +6 -0
  382. package/dist/types/providers/index.d.ts.map +1 -0
  383. package/dist/types/providers/mempoolProvider.d.ts +28 -0
  384. package/dist/types/providers/mempoolProvider.d.ts.map +1 -0
  385. package/dist/types/providers/rpcProvider.d.ts +34 -0
  386. package/dist/types/providers/rpcProvider.d.ts.map +1 -0
  387. package/dist/types/providers/utxoProvider.d.ts +43 -0
  388. package/dist/types/providers/utxoProvider.d.ts.map +1 -0
  389. package/dist/types/psbt/bip66.d.ts +28 -0
  390. package/dist/types/psbt/bip66.d.ts.map +1 -0
  391. package/dist/types/psbt/bufferutils.d.ts +47 -0
  392. package/dist/types/psbt/bufferutils.d.ts.map +1 -0
  393. package/dist/types/psbt/contextProvider.d.ts +15 -0
  394. package/dist/types/psbt/contextProvider.d.ts.map +1 -0
  395. package/dist/types/psbt/extPsbt.d.ts +131 -0
  396. package/dist/types/psbt/extPsbt.d.ts.map +1 -0
  397. package/dist/types/psbt/psbt.d.ts +194 -0
  398. package/dist/types/psbt/psbt.d.ts.map +1 -0
  399. package/dist/types/psbt/psbtutils.d.ts +37 -0
  400. package/dist/types/psbt/psbtutils.d.ts.map +1 -0
  401. package/dist/types/psbt/signatureutils.d.ts +52 -0
  402. package/dist/types/psbt/signatureutils.d.ts.map +1 -0
  403. package/dist/types/psbt/types.d.ts +63 -0
  404. package/dist/types/psbt/types.d.ts.map +1 -0
  405. package/dist/types/signer.d.ts +48 -0
  406. package/dist/types/signer.d.ts.map +1 -0
  407. package/dist/types/signers/defaultSigner.d.ts +30 -0
  408. package/dist/types/signers/defaultSigner.d.ts.map +1 -0
  409. package/dist/types/signers/index.d.ts +4 -0
  410. package/dist/types/signers/index.d.ts.map +1 -0
  411. package/dist/types/signers/unisatSigner.d.ts +32 -0
  412. package/dist/types/signers/unisatSigner.d.ts.map +1 -0
  413. package/dist/types/smart-contract/abi.d.ts +43 -0
  414. package/dist/types/smart-contract/abi.d.ts.map +1 -0
  415. package/dist/types/smart-contract/abiutils.d.ts +51 -0
  416. package/dist/types/smart-contract/abiutils.d.ts.map +1 -0
  417. package/dist/types/smart-contract/abstractContract.d.ts +115 -0
  418. package/dist/types/smart-contract/abstractContract.d.ts.map +1 -0
  419. package/dist/types/smart-contract/builtin-libs/backtrace.d.ts +43 -0
  420. package/dist/types/smart-contract/builtin-libs/backtrace.d.ts.map +1 -0
  421. package/dist/types/smart-contract/builtin-libs/byteStringReader.d.ts +32 -0
  422. package/dist/types/smart-contract/builtin-libs/byteStringReader.d.ts.map +1 -0
  423. package/dist/types/smart-contract/builtin-libs/byteStringWriter.d.ts +29 -0
  424. package/dist/types/smart-contract/builtin-libs/byteStringWriter.d.ts.map +1 -0
  425. package/dist/types/smart-contract/builtin-libs/contextUtils.d.ts +53 -0
  426. package/dist/types/smart-contract/builtin-libs/contextUtils.d.ts.map +1 -0
  427. package/dist/types/smart-contract/builtin-libs/index.d.ts +8 -0
  428. package/dist/types/smart-contract/builtin-libs/index.d.ts.map +1 -0
  429. package/dist/types/smart-contract/builtin-libs/p2pk.d.ts +8 -0
  430. package/dist/types/smart-contract/builtin-libs/p2pk.d.ts.map +1 -0
  431. package/dist/types/smart-contract/builtin-libs/p2pkh.d.ts +8 -0
  432. package/dist/types/smart-contract/builtin-libs/p2pkh.d.ts.map +1 -0
  433. package/dist/types/smart-contract/builtin-libs/stateLib.d.ts +27 -0
  434. package/dist/types/smart-contract/builtin-libs/stateLib.d.ts.map +1 -0
  435. package/dist/types/smart-contract/builtin-libs/stateUtils.d.ts +18 -0
  436. package/dist/types/smart-contract/builtin-libs/stateUtils.d.ts.map +1 -0
  437. package/dist/types/smart-contract/builtin-libs/stdUtils.d.ts +44 -0
  438. package/dist/types/smart-contract/builtin-libs/stdUtils.d.ts.map +1 -0
  439. package/dist/types/smart-contract/builtin-libs/txHashPreimageUtils.d.ts +8 -0
  440. package/dist/types/smart-contract/builtin-libs/txHashPreimageUtils.d.ts.map +1 -0
  441. package/dist/types/smart-contract/builtin-libs/txUtils.d.ts +69 -0
  442. package/dist/types/smart-contract/builtin-libs/txUtils.d.ts.map +1 -0
  443. package/dist/types/smart-contract/consts.d.ts +97 -0
  444. package/dist/types/smart-contract/consts.d.ts.map +1 -0
  445. package/dist/types/smart-contract/decorators.d.ts +40 -0
  446. package/dist/types/smart-contract/decorators.d.ts.map +1 -0
  447. package/dist/types/smart-contract/fns/assert.d.ts +8 -0
  448. package/dist/types/smart-contract/fns/assert.d.ts.map +1 -0
  449. package/dist/types/smart-contract/fns/bitwise.d.ts +50 -0
  450. package/dist/types/smart-contract/fns/bitwise.d.ts.map +1 -0
  451. package/dist/types/smart-contract/fns/byteString.d.ts +56 -0
  452. package/dist/types/smart-contract/fns/byteString.d.ts.map +1 -0
  453. package/dist/types/smart-contract/fns/equals.d.ts +8 -0
  454. package/dist/types/smart-contract/fns/equals.d.ts.map +1 -0
  455. package/dist/types/smart-contract/fns/fill.d.ts +11 -0
  456. package/dist/types/smart-contract/fns/fill.d.ts.map +1 -0
  457. package/dist/types/smart-contract/fns/hashes.d.ts +60 -0
  458. package/dist/types/smart-contract/fns/hashes.d.ts.map +1 -0
  459. package/dist/types/smart-contract/fns/index.d.ts +9 -0
  460. package/dist/types/smart-contract/fns/index.d.ts.map +1 -0
  461. package/dist/types/smart-contract/fns/math.d.ts +38 -0
  462. package/dist/types/smart-contract/fns/math.d.ts.map +1 -0
  463. package/dist/types/smart-contract/fns/pubKey2Addr.d.ts +11 -0
  464. package/dist/types/smart-contract/fns/pubKey2Addr.d.ts.map +1 -0
  465. package/dist/types/smart-contract/methods/backtraceToGenensis.d.ts +20 -0
  466. package/dist/types/smart-contract/methods/backtraceToGenensis.d.ts.map +1 -0
  467. package/dist/types/smart-contract/methods/buildOutput.d.ts +25 -0
  468. package/dist/types/smart-contract/methods/buildOutput.d.ts.map +1 -0
  469. package/dist/types/smart-contract/methods/checkCtx.d.ts +18 -0
  470. package/dist/types/smart-contract/methods/checkCtx.d.ts.map +1 -0
  471. package/dist/types/smart-contract/methods/checkInputState.d.ts +9 -0
  472. package/dist/types/smart-contract/methods/checkInputState.d.ts.map +1 -0
  473. package/dist/types/smart-contract/methods/checkInputStateHashes.d.ts +10 -0
  474. package/dist/types/smart-contract/methods/checkInputStateHashes.d.ts.map +1 -0
  475. package/dist/types/smart-contract/methods/checkSHPreimage.d.ts +10 -0
  476. package/dist/types/smart-contract/methods/checkSHPreimage.d.ts.map +1 -0
  477. package/dist/types/smart-contract/methods/checkSig.d.ts +11 -0
  478. package/dist/types/smart-contract/methods/checkSig.d.ts.map +1 -0
  479. package/dist/types/smart-contract/serializer.d.ts +48 -0
  480. package/dist/types/smart-contract/serializer.d.ts.map +1 -0
  481. package/dist/types/smart-contract/smartContract.d.ts +274 -0
  482. package/dist/types/smart-contract/smartContract.d.ts.map +1 -0
  483. package/dist/types/smart-contract/smartContractLib.d.ts +26 -0
  484. package/dist/types/smart-contract/smartContractLib.d.ts.map +1 -0
  485. package/dist/types/smart-contract/stateSerializer.d.ts +8 -0
  486. package/dist/types/smart-contract/stateSerializer.d.ts.map +1 -0
  487. package/dist/types/smart-contract/types/abi.d.ts +55 -0
  488. package/dist/types/smart-contract/types/abi.d.ts.map +1 -0
  489. package/dist/types/smart-contract/types/artifact.d.ts +108 -0
  490. package/dist/types/smart-contract/types/artifact.d.ts.map +1 -0
  491. package/dist/types/smart-contract/types/bn.d.ts +3 -0
  492. package/dist/types/smart-contract/types/bn.d.ts.map +1 -0
  493. package/dist/types/smart-contract/types/context.d.ts +126 -0
  494. package/dist/types/smart-contract/types/context.d.ts.map +1 -0
  495. package/dist/types/smart-contract/types/index.d.ts +4 -0
  496. package/dist/types/smart-contract/types/index.d.ts.map +1 -0
  497. package/dist/types/smart-contract/types/opCode.d.ts +747 -0
  498. package/dist/types/smart-contract/types/opCode.d.ts.map +1 -0
  499. package/dist/types/smart-contract/types/primitives.d.ts +217 -0
  500. package/dist/types/smart-contract/types/primitives.d.ts.map +1 -0
  501. package/dist/types/smart-contract/types/script.d.ts +14 -0
  502. package/dist/types/smart-contract/types/script.d.ts.map +1 -0
  503. package/dist/types/smart-contract/types/structs.d.ts +206 -0
  504. package/dist/types/smart-contract/types/structs.d.ts.map +1 -0
  505. package/dist/types/smart-contract/types/utils.d.ts +5 -0
  506. package/dist/types/smart-contract/types/utils.d.ts.map +1 -0
  507. package/dist/types/utils/bvm.d.ts +9 -0
  508. package/dist/types/utils/bvm.d.ts.map +1 -0
  509. package/dist/types/utils/checkIntegrity.d.ts +46 -0
  510. package/dist/types/utils/checkIntegrity.d.ts.map +1 -0
  511. package/dist/types/utils/common.d.ts +61 -0
  512. package/dist/types/utils/common.d.ts.map +1 -0
  513. package/dist/types/utils/constants.d.ts +3 -0
  514. package/dist/types/utils/constants.d.ts.map +1 -0
  515. package/dist/types/utils/index.d.ts +7 -0
  516. package/dist/types/utils/index.d.ts.map +1 -0
  517. package/dist/types/utils/preimage.d.ts +31 -0
  518. package/dist/types/utils/preimage.d.ts.map +1 -0
  519. package/dist/types/utils/proof.d.ts +31 -0
  520. package/dist/types/utils/proof.d.ts.map +1 -0
  521. package/dist/types/utils/script_number.d.ts +19 -0
  522. package/dist/types/utils/script_number.d.ts.map +1 -0
  523. package/package.json +79 -0
  524. package/scrypt.index.json +85 -0
  525. package/src/smart-contract/builtin-libs/backtrace.ts +116 -0
  526. package/src/smart-contract/builtin-libs/byteStringReader.ts +111 -0
  527. package/src/smart-contract/builtin-libs/byteStringWriter.ts +86 -0
  528. package/src/smart-contract/builtin-libs/contextUtils.ts +270 -0
  529. package/src/smart-contract/builtin-libs/index.ts +7 -0
  530. package/src/smart-contract/builtin-libs/p2pk.ts +58 -0
  531. package/src/smart-contract/builtin-libs/p2pkh.ts +67 -0
  532. package/src/smart-contract/builtin-libs/stateLib.ts +82 -0
  533. package/src/smart-contract/builtin-libs/stateUtils.ts +37 -0
  534. package/src/smart-contract/builtin-libs/stdUtils.ts +156 -0
  535. package/src/smart-contract/builtin-libs/txHashPreimageUtils.ts +34 -0
  536. package/src/smart-contract/builtin-libs/txUtils.ts +152 -0
  537. package/src/smart-contract/types/structs.ts +244 -0
@@ -0,0 +1,270 @@
1
+ import { prop, method } from '../decorators.js';
2
+ import { assert } from '../fns/assert.js';
3
+ import { byteStringToInt, intToByteString, len, reverseByteString, slice, toByteString } from '../fns/byteString.js';
4
+ import { hash256 } from '../fns/hashes.js';
5
+ import { SmartContractLib } from '../smartContractLib.js';
6
+ import { PubKey, ByteString, Sig, Int32, UInt32, PrivKey, SigHashPreimage } from '../types/primitives.js';
7
+ import { SHPreimage, SpentScriptHashes, SpentAmounts, Prevouts, Outpoint, SpentDataHashes, TxHashPreimage } from '../types/structs.js';
8
+ import { StdUtils } from './stdUtils.js';
9
+ import { TxUtils } from './txUtils.js';
10
+
11
+ /**
12
+ * Library for verifying preimage.
13
+ * @category Library
14
+ * @onchain
15
+ */
16
+ export class ContextUtils extends SmartContractLib {
17
+
18
+ // The following arguments can be generated using sample code at
19
+ // https://gist.github.com/scrypt-sv/f6882be580780a88984cee75dd1564c4.js
20
+ @prop()
21
+ static readonly privKey: PrivKey = PrivKey(0x26f00fe2340a84335ebdf30f57e9bb58487117b29355718f5e46bf5168d7df97n);
22
+ @prop()
23
+ static readonly pubKey: PubKey = PubKey(toByteString('02ba79df5f8ae7604a9830f03c7933028186aede0675a16f025dc4f8be8eec0382'));
24
+ // invK is the modular inverse of k, the ephemeral key
25
+ @prop()
26
+ static readonly invK: bigint = 0xc8ffdbaa05d93aa4ede79ec58f06a72562048b775a3507c2bf44bde4f007c40an;
27
+ // r is x coordinate of R, which is kG
28
+ @prop()
29
+ static readonly r: bigint = 0x1008ce7480da41702918d1ec8e6849ba32b4d65b1e40dc669c31a1e6306b266cn;
30
+ // rBigEndian is the signed magnitude representation of r, in big endian
31
+ @prop()
32
+ static readonly rBigEndian: ByteString = toByteString('1008ce7480da41702918d1ec8e6849ba32b4d65b1e40dc669c31a1e6306b266c');
33
+
34
+
35
+ @method()
36
+ static normalize(k: bigint, modulus: bigint): bigint {
37
+ const res: bigint = k % modulus;
38
+ // ensure it's positive
39
+ return (res < 0) ? res + modulus : res;
40
+ }
41
+ @method()
42
+ static sign(h: bigint, privKey: PrivKey, inverseK: bigint, r: bigint, rBigEndian: ByteString, sigHashType: ByteString): Sig {
43
+ // TODO: r * privKey can also be precomputed
44
+ let s: bigint = inverseK * (h + r * (privKey as bigint));
45
+ const N: bigint = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n;
46
+ s = ContextUtils.normalize(s, N);
47
+ // lower S
48
+ if (s > N / 2n) {
49
+ s = N - s;
50
+ }
51
+ // require(s != 0); // check offchain
52
+ /*
53
+ * DER: h + l + rh + rl + r + sh + sl + s + hashtype
54
+ * note: r & s are at most 33 bytes, thus no need to convert endian of rl & sl
55
+ */
56
+ const rlen: bigint = BigInt(len(rBigEndian));
57
+ const slen = len(intToByteString(s));
58
+ // we convert s to 32 bytes, otherwise reverseByteString(, 32) fails when s is strictly less than 31 bytes (note: 31 bytes works)
59
+ // slice it after reversing to remove extra leading zeros, otherwise strict DER rule fails it due to not minimally encoded
60
+ const sBigEndian: ByteString = slice(reverseByteString(intToByteString(s, 32n), BigInt(32)), 32n - slen);
61
+
62
+ const l: bigint = 4n + rlen + BigInt(slen);
63
+ // rBigEndian must be mininally encoded, to conform to strict DER rule
64
+ const rb: ByteString = toByteString('30')
65
+ + intToByteString(l)
66
+ + toByteString('02')
67
+ + intToByteString(rlen)
68
+ + rBigEndian
69
+ + toByteString('02')
70
+ + intToByteString(slen)
71
+ + sBigEndian
72
+ + toByteString(sigHashType);
73
+ return Sig(rb);
74
+ }
75
+
76
+ @method()
77
+ static fromBEUnsigned(b: ByteString): Int32 {
78
+ // change endian first
79
+ // append positive sign byte. This does not hurt even when sign bit is already positive
80
+ return byteStringToInt(reverseByteString(b, 32n) + toByteString('00'));
81
+ }
82
+
83
+ /**
84
+ * sign the transaction preimage
85
+ * @param shPreimage - the transaction preimage
86
+ * @returns a signature
87
+ */
88
+ @method()
89
+ static checkSHPreimage(shPreimage: SHPreimage, sigHashType: ByteString): Sig {
90
+ assert(len(shPreimage.nVersion) == 4n, 'invalid length of nVersion');
91
+ assert(len(shPreimage.hashPrevouts) == 32n, 'invalid length of hashPrevouts');
92
+ assert(len(shPreimage.spentScriptHash) == 32n, 'invalid length of spentScriptHash');
93
+ assert(len(shPreimage.spentDataHash) == 32n, 'invalid length of spentDataHash');
94
+ assert(shPreimage.value >= 0n, 'invalid value of value');
95
+ assert(len(shPreimage.nSequence) == 4n, 'invalid length of nSequence');
96
+ assert(len(shPreimage.hashSpentAmounts) == 32n, 'invalid length of hashSpentAmounts');
97
+ assert(len(shPreimage.hashSpentScriptHashes) == 32n, 'invalid length of hashSpentScriptHashes');
98
+ assert(len(shPreimage.hashSpentDataHashes) == 32n, 'invalid length of hashSpentDataHashes');
99
+ assert(len(shPreimage.hashSequences) == 32n, 'invalid length of hashSequences');
100
+ assert(len(shPreimage.hashOutputs) == 32n, 'invalid length of hashOutputs');
101
+ assert(shPreimage.inputIndex >= 0n, 'invalid value of inputIndex');
102
+ assert(shPreimage.nLockTime >= 0n, 'invalid value of nLockTime');
103
+ assert(shPreimage.sigHashType == 1n
104
+ || shPreimage.sigHashType == 2n
105
+ || shPreimage.sigHashType == 3n
106
+ || shPreimage.sigHashType == 0x81n
107
+ || shPreimage.sigHashType == 0x82n
108
+ || shPreimage.sigHashType == 0x83n
109
+ , 'invalid value of sigHashType');
110
+
111
+ const preimage = shPreimage.nVersion
112
+ + shPreimage.hashPrevouts
113
+ + shPreimage.spentScriptHash
114
+ + shPreimage.spentDataHash
115
+ + TxUtils.satoshisToByteString(shPreimage.value)
116
+ + shPreimage.nSequence
117
+ + shPreimage.hashSpentAmounts
118
+ + shPreimage.hashSpentScriptHashes
119
+ + shPreimage.hashSpentDataHashes
120
+ + shPreimage.hashSequences
121
+ + shPreimage.hashOutputs
122
+ + StdUtils.toLEUnsigned(shPreimage.inputIndex, 4n)
123
+ + StdUtils.toLEUnsigned(shPreimage.nLockTime, 4n)
124
+ + intToByteString(shPreimage.sigHashType, 4n);
125
+
126
+ const h: ByteString = hash256(preimage);
127
+ const sig: Sig = ContextUtils.sign(
128
+ ContextUtils.fromBEUnsigned(h),
129
+ ContextUtils.privKey,
130
+ ContextUtils.invK,
131
+ ContextUtils.r,
132
+ ContextUtils.rBigEndian,
133
+ sigHashType);
134
+ return sig;
135
+ }
136
+
137
+ @method()
138
+ static serializeSHPreimage(shPreimage: SHPreimage): SigHashPreimage {
139
+ assert(len(shPreimage.nVersion) == 4n, 'invalid length of nVersion');
140
+ assert(len(shPreimage.hashPrevouts) == 32n, 'invalid length of hashPrevouts');
141
+ assert(len(shPreimage.spentScriptHash) == 32n, 'invalid length of spentScriptHash');
142
+ assert(len(shPreimage.spentDataHash) == 32n, 'invalid length of spentDataHash');
143
+ assert(shPreimage.value >= 0n, 'invalid value of value');
144
+ assert(len(shPreimage.nSequence) == 4n, 'invalid length of nSequence');
145
+ assert(len(shPreimage.hashSpentAmounts) == 32n, 'invalid length of hashSpentAmounts');
146
+ assert(len(shPreimage.hashSpentScriptHashes) == 32n, 'invalid length of hashSpentScriptHashes');
147
+ assert(len(shPreimage.hashSpentDataHashes) == 32n, 'invalid length of hashSpentDataHashes');
148
+ assert(len(shPreimage.hashSequences) == 32n, 'invalid length of hashSequences');
149
+ assert(len(shPreimage.hashOutputs) == 32n, 'invalid length of hashOutputs');
150
+ assert(shPreimage.inputIndex >= 0n, 'invalid value of inputIndex');
151
+ assert(shPreimage.nLockTime >= 0n, 'invalid value of nLockTime');
152
+ assert(shPreimage.sigHashType == 1n
153
+ || shPreimage.sigHashType == 2n
154
+ || shPreimage.sigHashType == 3n
155
+ || shPreimage.sigHashType == 0x81n
156
+ || shPreimage.sigHashType == 0x82n
157
+ || shPreimage.sigHashType == 0x83n
158
+ , 'invalid value of sigHashType');
159
+
160
+ const preimage = shPreimage.nVersion
161
+ + shPreimage.hashPrevouts
162
+ + shPreimage.spentScriptHash
163
+ + shPreimage.spentDataHash
164
+ + TxUtils.satoshisToByteString(shPreimage.value)
165
+ + shPreimage.nSequence
166
+ + shPreimage.hashSpentAmounts
167
+ + shPreimage.hashSpentScriptHashes
168
+ + shPreimage.hashSpentDataHashes
169
+ + shPreimage.hashSequences
170
+ + shPreimage.hashOutputs
171
+ + StdUtils.toLEUnsigned(shPreimage.inputIndex, 4n)
172
+ + StdUtils.toLEUnsigned(shPreimage.nLockTime, 4n)
173
+ + intToByteString(shPreimage.sigHashType, 4n);
174
+ return SigHashPreimage(preimage);
175
+ }
176
+
177
+ /**
178
+ * Verify that the prevouts context passed in by the user is authentic
179
+ * @param prevouts prevouts context passed in by the user that need to be verified
180
+ * @param prevout prevout context passed in by the user that need to be verified
181
+ * @param t_hashPrevouts hashPrevouts in preimage which is trustable
182
+ * @param t_inputIndex the index of the input, which is trustable
183
+ * @returns the number of inputs, which is trustable
184
+ */
185
+ @method()
186
+ static checkPrevouts(
187
+ prevouts: Prevouts,
188
+ t_hashPrevouts: ByteString,
189
+ t_inputIndex: UInt32,
190
+ t_inputCount: Int32,
191
+ ): Outpoint {
192
+ // check prevouts
193
+ assert(hash256(prevouts) == t_hashPrevouts, 'hashPrevouts mismatch');
194
+ assert(t_inputIndex < t_inputCount, 'invalid prevouts');
195
+ assert(t_inputCount == StdUtils.checkLenDivisibleBy(prevouts, 36n), 'invalid prevouts');
196
+
197
+ const b = slice(prevouts, t_inputIndex * 36n, (t_inputIndex + 1n) * 36n);
198
+ return {
199
+ txHash: slice(b, 0n, 32n),
200
+ outputIndex: StdUtils.byteStringToUInt32(slice(b, 32n, 36n)),
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Check if the spent scripts array passed in matches the shaSpentScripts
206
+ * @param spentScriptHashes array of spent scripts passed in that need to be verified
207
+ * @param t_hashSpentScripts the hash of the merged spent scripts, which comes from preimage and is trustable
208
+ * @param t_inputCount must be trustable, the number of inputs
209
+ */
210
+ @method()
211
+ static checkSpentScripts(
212
+ spentScriptHashes: SpentScriptHashes,
213
+ t_hashSpentScripts: ByteString,
214
+ t_inputCount: bigint,
215
+ ): void {
216
+ assert(hash256(spentScriptHashes) == t_hashSpentScripts, 'hashSpentScripts mismatch');
217
+ assert(t_inputCount == StdUtils.checkLenDivisibleBy(spentScriptHashes, 32n), 'invalid spentScriptHashes');
218
+ }
219
+
220
+ /**
221
+ * Check if the spent amounts array passed in matches the shaSpentAmounts
222
+ * @param spentAmounts array of spent amounts passed in that need to be verified
223
+ * @param t_hashSpentAmounts the hash of the merged spent amounts, which comes from preimage and is trustable
224
+ * @param t_inputCount must be trustable, the number of inputs
225
+ */
226
+ @method()
227
+ static checkSpentAmounts(
228
+ spentAmounts: SpentAmounts,
229
+ t_hashSpentAmounts: ByteString,
230
+ ): Int32 {
231
+ assert(hash256(spentAmounts) == t_hashSpentAmounts, 'hashSpentAmounts mismatch');
232
+ return StdUtils.checkLenDivisibleBy(spentAmounts, 8n);
233
+ }
234
+
235
+ @method()
236
+ static checkSpentDataHashes(
237
+ spentDataHashes: SpentDataHashes,
238
+ t_hashSpentDataHashes: ByteString,
239
+ t_inputCount: bigint,
240
+ ): void {
241
+ assert(hash256(spentDataHashes) == t_hashSpentDataHashes, 'hashSpentDataHashes mismatch');
242
+ assert(t_inputCount == StdUtils.checkLenDivisibleBy(spentDataHashes, 32n), 'invalid spentDataHashes');
243
+ }
244
+
245
+
246
+ @method()
247
+ static getSpentScriptHash(
248
+ spentScriptHashes: SpentScriptHashes,
249
+ inputIndex: Int32,
250
+ ): ByteString {
251
+ return slice(spentScriptHashes, inputIndex * 32n, (inputIndex + 1n) * 32n);
252
+ }
253
+
254
+ @method()
255
+ static getSpentAmount(
256
+ spentAmounts: SpentAmounts,
257
+ inputIndex: Int32,
258
+ ): Int32 {
259
+ return StdUtils.fromLEUnsigned(slice(spentAmounts, inputIndex * 8n, (inputIndex + 1n) * 8n));
260
+ }
261
+
262
+ @method()
263
+ static getSpentDataHash(
264
+ spentDataHashes: SpentDataHashes,
265
+ inputIndex: Int32,
266
+ ): ByteString {
267
+ return slice(spentDataHashes, inputIndex * 32n, (inputIndex + 1n) * 32n);
268
+ }
269
+
270
+ }
@@ -0,0 +1,7 @@
1
+ export { ContextUtils } from './contextUtils.js';
2
+ export { TxUtils } from './txUtils.js';
3
+ export { StateUtils } from './stateUtils.js';
4
+ export { StdUtils } from './stdUtils.js';
5
+ export { Backtrace, type ChainTxVerifyResponse } from './backtrace.js';
6
+ export { StateLib } from './stateLib.js';
7
+ export { TxHashPreimageUtils } from './txHashPreimageUtils.js';
@@ -0,0 +1,58 @@
1
+ import { method, prop } from "../decorators.js"
2
+ import { assert } from "../fns/assert.js"
3
+ import { PubKey, Sig } from "../types/primitives.js"
4
+ import { SmartContract } from "../smartContract.js"
5
+
6
+ export class P2PK extends SmartContract {
7
+ @prop()
8
+ readonly pubKey: PubKey
9
+
10
+ constructor(pubKey: PubKey) {
11
+ super(...arguments)
12
+ this.pubKey = pubKey
13
+ }
14
+
15
+ @method()
16
+ public unlock(sig: Sig) {
17
+ assert(this.checkSig(sig, this.pubKey), 'signature check failed')
18
+ }
19
+ }
20
+
21
+ const desc = {
22
+ version: 9,
23
+ compilerVersion: '1.19.0+commit.72eaeba',
24
+ contract: 'P2PK',
25
+ md5: '0c046dfb1f1a91cf72b9a852537bdfe1',
26
+ structs: [],
27
+ library: [],
28
+ alias: [],
29
+ abi: [
30
+ {
31
+ type: 'function',
32
+ name: 'unlock',
33
+ index: 0,
34
+ params: [
35
+ {
36
+ name: 'sig',
37
+ type: 'Sig',
38
+ },
39
+ ],
40
+ },
41
+ {
42
+ type: 'constructor',
43
+ params: [
44
+ {
45
+ name: 'pubKey',
46
+ type: 'PubKey',
47
+ },
48
+ ],
49
+ },
50
+ ],
51
+ stateProps: [],
52
+ buildType: 'release',
53
+ file: '',
54
+ hex: '<pubKey>ac',
55
+ sourceMapFile: '',
56
+ }
57
+
58
+ P2PK.loadArtifact(desc)
@@ -0,0 +1,67 @@
1
+ import { method, prop } from "../decorators.js"
2
+ import { assert } from "../fns/assert.js"
3
+ import { Addr, PubKey, Sig } from "../types/primitives.js"
4
+ import { SmartContract } from "../smartContract.js"
5
+ import { pubKey2Addr } from "../fns/pubKey2Addr.js"
6
+
7
+ export class P2PKH extends SmartContract {
8
+ @prop()
9
+ readonly addr: Addr
10
+
11
+ constructor(addr: Addr) {
12
+ super(...arguments)
13
+ this.addr = addr
14
+ }
15
+
16
+ @method()
17
+ public unlock(sig: Sig, pubkey: PubKey) {
18
+ assert(
19
+ pubKey2Addr(pubkey) == this.addr,
20
+ 'public key hashes are not equal'
21
+ )
22
+ assert(this.checkSig(sig, pubkey), 'signature check failed')
23
+ }
24
+ }
25
+
26
+ const desc = {
27
+ version: 9,
28
+ compilerVersion: '1.19.0+commit.72eaeba',
29
+ contract: 'P2PKH',
30
+ md5: '0c046dfb1f1a91cf72b9a852537bdfe1',
31
+ structs: [],
32
+ library: [],
33
+ alias: [],
34
+ abi: [
35
+ {
36
+ type: 'function',
37
+ name: 'unlock',
38
+ index: 0,
39
+ params: [
40
+ {
41
+ name: 'sig',
42
+ type: 'Sig',
43
+ },
44
+ {
45
+ name: 'pubkey',
46
+ type: 'PubKey',
47
+ },
48
+ ],
49
+ },
50
+ {
51
+ type: 'constructor',
52
+ params: [
53
+ {
54
+ name: 'addr',
55
+ type: 'Ripemd160',
56
+ },
57
+ ],
58
+ },
59
+ ],
60
+ stateProps: [],
61
+ buildType: 'release',
62
+ file: '',
63
+ hex: '76a9<addr>88ac',
64
+ sourceMapFile: '',
65
+ }
66
+
67
+ P2PKH.loadArtifact(desc)
@@ -0,0 +1,82 @@
1
+ import { SmartContractLib } from '../smartContractLib.js';
2
+ import { ByteString, OpcatState, StructObject } from '../types/primitives.js';
3
+ import { ABICoder } from '../abi.js';
4
+ import { deserializeState, serializeState } from '../stateSerializer.js';
5
+ import { getUnRenamedSymbol } from '../abiutils.js';
6
+ import { sha256 } from '../fns/index.js';
7
+
8
+ /**
9
+ * Library for computing the hash of a state.
10
+ * @category Library
11
+ * @onchain
12
+ */
13
+ export class StateLib<ST extends OpcatState> extends SmartContractLib {
14
+ /**
15
+ * Calculate the hash of the state object
16
+ * @param state the state object
17
+ * @returns the hash byte string of the state object
18
+ * @onchain
19
+ * @category State
20
+ */
21
+ static serializeState<T extends OpcatState>(
22
+ this: { new (...args: any[]): StateLib<T> },
23
+ state: T,
24
+ ): ByteString {
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
+ const libraryClazz = this as any as typeof SmartContractLib;
27
+ const artifact = libraryClazz.artifact;
28
+ if (!artifact) {
29
+ throw new Error(`Artifact is not loaded for the library: ${this.name}`);
30
+ }
31
+
32
+ const abiCoder = new ABICoder(artifact);
33
+ const library = abiCoder.artifact.library.find(
34
+ (lib) => getUnRenamedSymbol(lib.name) === getUnRenamedSymbol(this.name),
35
+ );
36
+ if (!library) {
37
+ throw new Error(`Library ${this.name} is not found in the artifact`);
38
+ }
39
+ if (!library.stateType) {
40
+ throw new Error(`State type is not defined for the library: ${this.name}`);
41
+ }
42
+
43
+ return serializeState(artifact, libraryClazz.stateType, state);
44
+ }
45
+
46
+ static deserializeState<T extends OpcatState>(
47
+ this: { new (...args: any[]): StateLib<T> },
48
+ serializedState: ByteString,
49
+ ): T {
50
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
+ const libraryClazz = this as any as typeof SmartContractLib;
52
+ const artifact = libraryClazz.artifact;
53
+ if (!artifact) {
54
+ throw new Error(`Artifact is not loaded for the library: ${this.name}`);
55
+ }
56
+
57
+ const abiCoder = new ABICoder(artifact);
58
+ const library = abiCoder.artifact.library.find(
59
+ (lib) => getUnRenamedSymbol(lib.name) === getUnRenamedSymbol(this.name),
60
+ );
61
+ if (!library) {
62
+ throw new Error(`Library ${this.name} is not found in the artifact`);
63
+ }
64
+ if (!library.stateType) {
65
+ throw new Error(`State type is not defined for the library: ${this.name}`);
66
+ }
67
+
68
+ return deserializeState(artifact, libraryClazz.stateType, serializedState);
69
+ }
70
+
71
+ static stateHash<T extends OpcatState>(
72
+ this: { new (...args: any[]): StateLib<T> },
73
+ state: T,
74
+ ): ByteString {
75
+ return sha256((this as any).serializeState(state));
76
+ }
77
+
78
+ // keep this method to enable the typecheck for stateHash method in ts
79
+ private _id(_state: ST): ST {
80
+ return _state;
81
+ }
82
+ }
@@ -0,0 +1,37 @@
1
+ import { assert } from '../fns/assert.js';
2
+ import { method } from '../decorators.js';
3
+ import { slice } from '../fns/byteString.js';
4
+ import { SmartContractLib } from '../smartContractLib.js';
5
+ import { ByteString, Int32, UInt32 } from '../types/primitives.js';
6
+ import { SpentDataHashes } from '../types/structs.js';
7
+ import { TX_OUTPUT_DATA_HASH_LEN } from '../consts.js';
8
+ import { sha256 } from '../fns/hashes.js';
9
+
10
+ /**
11
+ * spentDatas library
12
+ * @category Library
13
+ * @onchain
14
+ */
15
+ export class StateUtils extends SmartContractLib {
16
+ /**
17
+ * Check if the dataHash of the passed-in raw state matches the spent data hash
18
+ * @param rawState passed-in raw state after serialization
19
+ * @param t_spentDataHashes trustable spent data hashes
20
+ * @param t_inputIndex trustable input index
21
+ */
22
+ @method()
23
+ static checkInputState(
24
+ t_inputIndex: UInt32,
25
+ rawState: ByteString,
26
+ t_spentDataHashes: SpentDataHashes,
27
+ ): void {
28
+ assert(
29
+ slice(
30
+ t_spentDataHashes,
31
+ t_inputIndex * TX_OUTPUT_DATA_HASH_LEN,
32
+ (t_inputIndex + 1n) * TX_OUTPUT_DATA_HASH_LEN
33
+ ) == sha256(rawState),
34
+ 'dataHash of state mismatch'
35
+ );
36
+ }
37
+ }
@@ -0,0 +1,156 @@
1
+ import { method } from "../decorators.js";
2
+ import { assert } from "../fns/assert.js";
3
+ import { byteStringToInt, intToByteString, len, slice, toByteString } from "../fns/byteString.js";
4
+ import { SmartContractLib } from "../smartContractLib.js";
5
+ import { ByteString, PrivKey, UInt32, UInt64 } from "../types/primitives.js";
6
+
7
+
8
+ export const UINT64_MAX = 0xffffffffffffffffn;
9
+ export const UINT64_MIN = 0n;
10
+ export const UINT32_MAX = 0xffffffffn;
11
+ export const UINT32_MIN = 0n;
12
+
13
+ type ReadVarintResult = {
14
+ data: ByteString;
15
+ nextPos: bigint;
16
+ }
17
+
18
+ export class StdUtils extends SmartContractLib {
19
+ @method()
20
+ static checkLenDivisibleBy(b: ByteString, n: bigint): bigint {
21
+ const l = len(b);
22
+ assert(l % n == 0n, 'length of b is not divisible by n');
23
+ return l / n;
24
+ }
25
+
26
+
27
+ @method()
28
+ static uint64ToByteString(n: UInt64): ByteString {
29
+ assert(n >= UINT64_MIN && n <= UINT64_MAX, 'uint64 out of range');
30
+ return StdUtils.toLEUnsigned(n, 8n);
31
+ }
32
+
33
+ @method()
34
+ static uint32ToByteString(n: UInt32): ByteString {
35
+ assert(n >= UINT32_MIN && n <= UINT32_MAX, 'uint32 out of range');
36
+ return StdUtils.toLEUnsigned(n, 4n)
37
+ }
38
+
39
+ @method()
40
+ static byteStringToUInt32(b: ByteString): UInt32 {
41
+ assert(len(b) == 4n, 'byteStringToUInt32: byteString length is not 4');
42
+ return StdUtils.fromLEUnsigned(b);
43
+ }
44
+
45
+ /**
46
+ * convert signed integer `n` to unsigned integer of `l` string, in little endian
47
+ * @param {bigint} n the number to be converted
48
+ * @param {bigint} l expected length
49
+ * @returns {ByteString} returns a `ByteString`
50
+ */
51
+ @method()
52
+ static toLEUnsigned(n: bigint, l: bigint): ByteString {
53
+ const m = intToByteString(n, l + 1n);
54
+ // remove sign byte
55
+ return slice(m, 0n, l);
56
+ }
57
+
58
+ /**
59
+ * convert `ByteString` to unsigned integer, in sign-magnitude little endian
60
+ * @param {ByteString} bytes the `ByteString` to be converted
61
+ * @returns {bigint} returns a number
62
+ */
63
+ @method()
64
+ static fromLEUnsigned(b: ByteString): bigint {
65
+ return byteStringToInt(b + toByteString('00'));
66
+ }
67
+
68
+ @method()
69
+ static writeVarInt(n: bigint): ByteString {
70
+ let b: ByteString = toByteString('');
71
+ let size = 0n;
72
+ if (n < 0xfdn) {
73
+ size = 1n;
74
+ }
75
+ else if (n < 0x10000n) {
76
+ b = toByteString('fd')
77
+ size = 2n;
78
+ }
79
+ else if (n < 0x100000000n) {
80
+ b = toByteString('fe')
81
+ size = 4n;
82
+ }
83
+ else {
84
+ b = toByteString('ff')
85
+ size = 8n;
86
+ }
87
+ return b + StdUtils.toLEUnsigned(n, size);
88
+ }
89
+
90
+
91
+ /**
92
+ * serializes `ByteString` with `VarInt` encoding
93
+ * @param buf a `ByteString`
94
+ * @returns serialized `ByteString`
95
+ */
96
+ @method()
97
+ static pushData(buf: ByteString): ByteString {
98
+ let n = len(buf);
99
+
100
+ let header: ByteString = toByteString('');
101
+
102
+ if (n < 0x4c) {
103
+ header = StdUtils.toLEUnsigned(n, 1n);
104
+ }
105
+ else if (n < 0x100) {
106
+ header = toByteString('4c') + StdUtils.toLEUnsigned(n, 1n);
107
+ }
108
+ else if (n < 0x10000) {
109
+ header = toByteString('4d') + StdUtils.toLEUnsigned(n, 2n);
110
+ }
111
+ else if (n < 0x100000000) {
112
+ header = toByteString('4e') + StdUtils.toLEUnsigned(n, 4n);
113
+ }
114
+ else {
115
+ // shall not reach here
116
+ assert(false);
117
+ }
118
+
119
+ return header + buf;
120
+ }
121
+
122
+ /**
123
+ * read [VarInt (variable integer)]{@link https://learnmeabitcoin.com/technical/general/compact-size/}-encoded data from the beginning of 'buf'
124
+ * @param {ByteString} buf a buffer `ByteString` of format: [prefix FD/FE/FF +] length + data
125
+ * @returns return data
126
+ */
127
+ @method()
128
+ static readVarint(buf: ByteString, pos: bigint): ReadVarintResult {
129
+ let l: bigint = 0n;
130
+ let ret: ByteString = toByteString('');
131
+ let nextPos: bigint = pos;
132
+ const header: ByteString = slice(buf, pos, pos + 1n);
133
+
134
+ if (header == toByteString('fd')) {
135
+ l = StdUtils.fromLEUnsigned(slice(buf, pos + 1n, pos + 3n));
136
+ ret = slice(buf, 3n, 3n + l);
137
+ nextPos = pos + 3n + l;
138
+ }
139
+ else if (header == toByteString('fe')) {
140
+ l = StdUtils.fromLEUnsigned(slice(buf, pos + 1n, pos + 5n));
141
+ ret = slice(buf, pos + 5n, pos + 5n + l);
142
+ nextPos = pos + 5n + l;
143
+ }
144
+ else if (header == toByteString('ff')) {
145
+ l = StdUtils.fromLEUnsigned(slice(buf, pos + 1n, pos + 9n));
146
+ ret = slice(buf, pos + 9n, pos + 9n + l);
147
+ nextPos = pos + 9n + l;
148
+ } else {
149
+ l = StdUtils.fromLEUnsigned(slice(buf, pos, pos + 1n));
150
+ ret = slice(buf, pos + 1n, pos + 1n + l);
151
+ nextPos = pos + 1n + l;
152
+ }
153
+
154
+ return { data: ret, nextPos: nextPos };
155
+ }
156
+ }