react-native-nitro-bsx 0.0.0-stage → 0.2.35

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 (931) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +7 -2
  3. package/android/CMakeLists.txt +28 -0
  4. package/android/build.gradle +99 -0
  5. package/android/src/main/AndroidManifest.xml +35 -0
  6. package/android/src/main/cpp/cpp-adapter.cpp +12 -0
  7. package/android/src/main/java/com/nitrobsx/BSXAlarmReceiver.kt +29 -0
  8. package/android/src/main/java/com/nitrobsx/BSXAlarmScheduler.kt +112 -0
  9. package/android/src/main/java/com/nitrobsx/BSXForegroundService.kt +78 -0
  10. package/android/src/main/java/com/nitrobsx/BSXNotifications.kt +47 -0
  11. package/android/src/main/java/com/nitrobsx/BSXServiceController.kt +90 -0
  12. package/android/src/main/java/com/nitrobsx/BSXWakeWorker.kt +136 -0
  13. package/android/src/main/java/com/nitrobsx/HeadlessJsTaskSupportShim.kt +41 -0
  14. package/android/src/main/java/com/nitrobsx/NitroBSXPackage.kt +28 -0
  15. package/android/src/main/res/drawable/bsx_ic_stat_swap.xml +9 -0
  16. package/cpp/BSXContext.cpp +263 -0
  17. package/cpp/BSXContext.h +49 -0
  18. package/cpp/BSXModule.cpp +21191 -0
  19. package/cpp/BSXModule.h +1523 -0
  20. package/cpp/CMakeLists.txt +463 -0
  21. package/cpp/HybridNitroBSX.cpp +1483 -0
  22. package/cpp/HybridNitroBSX.hpp +173 -0
  23. package/cpp/arith_uint256.cpp +232 -0
  24. package/cpp/arith_uint256.h +277 -0
  25. package/cpp/bsx/event.cpp +48 -0
  26. package/cpp/bsx/event.h +29 -0
  27. package/cpp/bsx/swap.h +293 -0
  28. package/cpp/bsx/types.cpp +486 -0
  29. package/cpp/bsx/types.h +308 -0
  30. package/cpp/bsx_db.cpp +4951 -0
  31. package/cpp/bsx_db.h +465 -0
  32. package/cpp/chain_client.h +143 -0
  33. package/cpp/cmake/GenVersion.cmake +52 -0
  34. package/cpp/coin_config.h.in +12 -0
  35. package/cpp/coins/block_index.cpp +33 -0
  36. package/cpp/coins/block_index.h +35 -0
  37. package/cpp/coins/btc/chainparams.cpp +70 -0
  38. package/cpp/coins/btc/chainparams.h +23 -0
  39. package/cpp/coins/btc/interface.h +30 -0
  40. package/cpp/coins/btc_base/interface.cpp +2200 -0
  41. package/cpp/coins/btc_base/interface.h +225 -0
  42. package/cpp/coins/btc_base/primitives.cpp +560 -0
  43. package/cpp/coins/btc_base/primitives.h +157 -0
  44. package/cpp/coins/btc_base/psbt.cpp +150 -0
  45. package/cpp/coins/btc_base/psbt.h +52 -0
  46. package/cpp/coins/btc_base/script.cpp +304 -0
  47. package/cpp/coins/btc_base/script.h +181 -0
  48. package/cpp/coins/chainparams.cpp +47 -0
  49. package/cpp/coins/chainparams.h +58 -0
  50. package/cpp/coins/firo/chainparams.cpp +52 -0
  51. package/cpp/coins/firo/chainparams.h +24 -0
  52. package/cpp/coins/firo/firo.cpp +118 -0
  53. package/cpp/coins/firo/firo.h +79 -0
  54. package/cpp/coins/firo/primitives.cpp +109 -0
  55. package/cpp/coins/firo/primitives.h +47 -0
  56. package/cpp/coins/interface.cpp +1815 -0
  57. package/cpp/coins/interface.h +554 -0
  58. package/cpp/coins/ltc/chainparams.cpp +70 -0
  59. package/cpp/coins/ltc/chainparams.h +22 -0
  60. package/cpp/coins/ltc/ltc.cpp +69 -0
  61. package/cpp/coins/ltc/ltc.h +82 -0
  62. package/cpp/coins/ltc/primitives.cpp +20 -0
  63. package/cpp/coins/ltc/primitives.h +18 -0
  64. package/cpp/coins/part/chainparams.cpp +89 -0
  65. package/cpp/coins/part/chainparams.h +47 -0
  66. package/cpp/coins/part/part.cpp +905 -0
  67. package/cpp/coins/part/part.h +164 -0
  68. package/cpp/coins/part/primitives.cpp +548 -0
  69. package/cpp/coins/part/primitives.h +187 -0
  70. package/cpp/coins/part/stake.cpp +330 -0
  71. package/cpp/coins/part/stake.h +92 -0
  72. package/cpp/coins/primitives.h +125 -0
  73. package/cpp/coins/registry.cpp +99 -0
  74. package/cpp/coins/registry.h +32 -0
  75. package/cpp/coins/script.h +18 -0
  76. package/cpp/coins/types.cpp +60 -0
  77. package/cpp/coins/types.h +41 -0
  78. package/cpp/coins/xmr/chainparams.cpp +28 -0
  79. package/cpp/coins/xmr/chainparams.h +24 -0
  80. package/cpp/coins/xmr/tx.cpp +670 -0
  81. package/cpp/coins/xmr/tx.h +77 -0
  82. package/cpp/coins/xmr/xmr.cpp +639 -0
  83. package/cpp/coins/xmr/xmr.h +223 -0
  84. package/cpp/compat/byteswap.h +74 -0
  85. package/cpp/compat/endian.h +98 -0
  86. package/cpp/compat/mbedtls/threading_alt.h +20 -0
  87. package/cpp/compat/sockets.h +160 -0
  88. package/cpp/contrib/mbedtls/CMakeLists.txt +566 -0
  89. package/cpp/contrib/mbedtls/LICENSE +553 -0
  90. package/cpp/contrib/mbedtls/cmake/MbedTLSConfig.cmake.in +3 -0
  91. package/cpp/contrib/mbedtls/doxygen/input/doc_encdec.h +52 -0
  92. package/cpp/contrib/mbedtls/doxygen/input/doc_hashing.h +30 -0
  93. package/cpp/contrib/mbedtls/doxygen/input/doc_mainpage.h +52 -0
  94. package/cpp/contrib/mbedtls/doxygen/input/doc_rng.h +27 -0
  95. package/cpp/contrib/mbedtls/doxygen/input/doc_ssltls.h +37 -0
  96. package/cpp/contrib/mbedtls/doxygen/input/doc_tcpip.h +32 -0
  97. package/cpp/contrib/mbedtls/doxygen/input/doc_x509.h +31 -0
  98. package/cpp/contrib/mbedtls/doxygen/mbedtls.doxyfile +56 -0
  99. package/cpp/contrib/mbedtls/framework/CMakeLists.txt +1 -0
  100. package/cpp/contrib/mbedtls/framework/LICENSE +553 -0
  101. package/cpp/contrib/mbedtls/include/CMakeLists.txt +21 -0
  102. package/cpp/contrib/mbedtls/include/mbedtls/build_info.h +93 -0
  103. package/cpp/contrib/mbedtls/include/mbedtls/debug.h +120 -0
  104. package/cpp/contrib/mbedtls/include/mbedtls/error.h +39 -0
  105. package/cpp/contrib/mbedtls/include/mbedtls/mbedtls_config.h +1189 -0
  106. package/cpp/contrib/mbedtls/include/mbedtls/net_sockets.h +299 -0
  107. package/cpp/contrib/mbedtls/include/mbedtls/oid.h +304 -0
  108. package/cpp/contrib/mbedtls/include/mbedtls/pkcs7.h +250 -0
  109. package/cpp/contrib/mbedtls/include/mbedtls/private/config_adjust_ssl.h +84 -0
  110. package/cpp/contrib/mbedtls/include/mbedtls/private/config_adjust_x509.h +35 -0
  111. package/cpp/contrib/mbedtls/include/mbedtls/ssl.h +5622 -0
  112. package/cpp/contrib/mbedtls/include/mbedtls/ssl_cache.h +187 -0
  113. package/cpp/contrib/mbedtls/include/mbedtls/ssl_ciphersuites.h +303 -0
  114. package/cpp/contrib/mbedtls/include/mbedtls/ssl_cookie.h +90 -0
  115. package/cpp/contrib/mbedtls/include/mbedtls/ssl_ticket.h +186 -0
  116. package/cpp/contrib/mbedtls/include/mbedtls/timing.h +105 -0
  117. package/cpp/contrib/mbedtls/include/mbedtls/version.h +69 -0
  118. package/cpp/contrib/mbedtls/include/mbedtls/x509.h +503 -0
  119. package/cpp/contrib/mbedtls/include/mbedtls/x509_crl.h +180 -0
  120. package/cpp/contrib/mbedtls/include/mbedtls/x509_crt.h +1174 -0
  121. package/cpp/contrib/mbedtls/include/mbedtls/x509_csr.h +368 -0
  122. package/cpp/contrib/mbedtls/library/CMakeLists.txt +375 -0
  123. package/cpp/contrib/mbedtls/library/Makefile +334 -0
  124. package/cpp/contrib/mbedtls/library/debug.c +285 -0
  125. package/cpp/contrib/mbedtls/library/debug_internal.h +108 -0
  126. package/cpp/contrib/mbedtls/library/error.c +505 -0
  127. package/cpp/contrib/mbedtls/library/mbedtls_check_config.h +376 -0
  128. package/cpp/contrib/mbedtls/library/mbedtls_common.h +51 -0
  129. package/cpp/contrib/mbedtls/library/mbedtls_config.c +40 -0
  130. package/cpp/contrib/mbedtls/library/mbedtls_config_check_before.h +1769 -0
  131. package/cpp/contrib/mbedtls/library/mbedtls_config_check_final.h +12 -0
  132. package/cpp/contrib/mbedtls/library/mbedtls_config_check_user.h +2436 -0
  133. package/cpp/contrib/mbedtls/library/mbedtls_platform_requirements.h +55 -0
  134. package/cpp/contrib/mbedtls/library/mbedtls_utils.h +23 -0
  135. package/cpp/contrib/mbedtls/library/mps_common.h +181 -0
  136. package/cpp/contrib/mbedtls/library/mps_error.h +89 -0
  137. package/cpp/contrib/mbedtls/library/mps_reader.c +538 -0
  138. package/cpp/contrib/mbedtls/library/mps_reader.h +366 -0
  139. package/cpp/contrib/mbedtls/library/mps_trace.c +112 -0
  140. package/cpp/contrib/mbedtls/library/mps_trace.h +154 -0
  141. package/cpp/contrib/mbedtls/library/net_sockets.c +684 -0
  142. package/cpp/contrib/mbedtls/library/pkcs7.c +784 -0
  143. package/cpp/contrib/mbedtls/library/ssl_cache.c +409 -0
  144. package/cpp/contrib/mbedtls/library/ssl_ciphersuites.c +996 -0
  145. package/cpp/contrib/mbedtls/library/ssl_ciphersuites_internal.h +108 -0
  146. package/cpp/contrib/mbedtls/library/ssl_client.c +1019 -0
  147. package/cpp/contrib/mbedtls/library/ssl_client.h +18 -0
  148. package/cpp/contrib/mbedtls/library/ssl_cookie.c +251 -0
  149. package/cpp/contrib/mbedtls/library/ssl_debug_helpers.h +83 -0
  150. package/cpp/contrib/mbedtls/library/ssl_debug_helpers_generated.c +243 -0
  151. package/cpp/contrib/mbedtls/library/ssl_misc.h +2851 -0
  152. package/cpp/contrib/mbedtls/library/ssl_msg.c +6275 -0
  153. package/cpp/contrib/mbedtls/library/ssl_ticket.c +453 -0
  154. package/cpp/contrib/mbedtls/library/ssl_tls.c +9048 -0
  155. package/cpp/contrib/mbedtls/library/ssl_tls12_client.c +3054 -0
  156. package/cpp/contrib/mbedtls/library/ssl_tls12_server.c +3546 -0
  157. package/cpp/contrib/mbedtls/library/ssl_tls13_client.c +3202 -0
  158. package/cpp/contrib/mbedtls/library/ssl_tls13_generic.c +1773 -0
  159. package/cpp/contrib/mbedtls/library/ssl_tls13_invasive.h +23 -0
  160. package/cpp/contrib/mbedtls/library/ssl_tls13_keys.c +1860 -0
  161. package/cpp/contrib/mbedtls/library/ssl_tls13_keys.h +668 -0
  162. package/cpp/contrib/mbedtls/library/ssl_tls13_server.c +3602 -0
  163. package/cpp/contrib/mbedtls/library/timing.c +76 -0
  164. package/cpp/contrib/mbedtls/library/version.c +30 -0
  165. package/cpp/contrib/mbedtls/library/version_features.c +244 -0
  166. package/cpp/contrib/mbedtls/library/x509.c +1848 -0
  167. package/cpp/contrib/mbedtls/library/x509_create.c +747 -0
  168. package/cpp/contrib/mbedtls/library/x509_crl.c +701 -0
  169. package/cpp/contrib/mbedtls/library/x509_crt.c +3278 -0
  170. package/cpp/contrib/mbedtls/library/x509_csr.c +642 -0
  171. package/cpp/contrib/mbedtls/library/x509_internal.h +97 -0
  172. package/cpp/contrib/mbedtls/library/x509_oid.c +603 -0
  173. package/cpp/contrib/mbedtls/library/x509_oid.h +150 -0
  174. package/cpp/contrib/mbedtls/library/x509write.c +171 -0
  175. package/cpp/contrib/mbedtls/library/x509write_crt.c +637 -0
  176. package/cpp/contrib/mbedtls/library/x509write_csr.c +317 -0
  177. package/cpp/contrib/mbedtls/pkgconfig/CMakeLists.txt +25 -0
  178. package/cpp/contrib/mbedtls/pkgconfig/JoinPaths.cmake +27 -0
  179. package/cpp/contrib/mbedtls/pkgconfig/mbedcrypto.pc.in +10 -0
  180. package/cpp/contrib/mbedtls/pkgconfig/mbedtls.pc.in +11 -0
  181. package/cpp/contrib/mbedtls/pkgconfig/mbedx509.pc.in +11 -0
  182. package/cpp/contrib/mbedtls/tf-psa-crypto/CMakeLists.txt +584 -0
  183. package/cpp/contrib/mbedtls/tf-psa-crypto/LICENSE +552 -0
  184. package/cpp/contrib/mbedtls/tf-psa-crypto/cmake/TF-PSA-CryptoConfig.cmake.in +3 -0
  185. package/cpp/contrib/mbedtls/tf-psa-crypto/core/CMakeLists.txt +193 -0
  186. package/cpp/contrib/mbedtls/tf-psa-crypto/core/alignment.h +700 -0
  187. package/cpp/contrib/mbedtls/tf-psa-crypto/core/check_crypto_config.h +111 -0
  188. package/cpp/contrib/mbedtls/tf-psa-crypto/core/crypto-library.make +57 -0
  189. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto.c +9820 -0
  190. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_client.c +22 -0
  191. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_core.h +972 -0
  192. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_core_common.h +52 -0
  193. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers.h +3395 -0
  194. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers_no_static.c +247 -0
  195. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_invasive.h +85 -0
  196. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_its.h +131 -0
  197. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_random.c +168 -0
  198. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_random.h +72 -0
  199. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_random_impl.h +215 -0
  200. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_slot_management.c +998 -0
  201. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_slot_management.h +330 -0
  202. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_storage.c +400 -0
  203. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_storage.h +201 -0
  204. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_its_file.c +254 -0
  205. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_util.c +295 -0
  206. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_check_config.h +598 -0
  207. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_common.h +628 -0
  208. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config.c +72 -0
  209. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config_check_before.h +12 -0
  210. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config_check_final.h +12 -0
  211. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config_check_user.h +1220 -0
  212. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_platform_requirements.h +62 -0
  213. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_version.c +28 -0
  214. package/cpp/contrib/mbedtls/tf-psa-crypto/dispatch/psa_crypto_driver_wrappers_no_static.h +31 -0
  215. package/cpp/contrib/mbedtls/tf-psa-crypto/doxygen/input/doc_mainpage.h.in +51 -0
  216. package/cpp/contrib/mbedtls/tf-psa-crypto/doxygen/tfpsacrypto.doxyfile.in +54 -0
  217. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/CMakeLists.txt +142 -0
  218. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/CMakeLists.txt +3 -0
  219. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/aes.h +607 -0
  220. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/aria.h +342 -0
  221. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/bignum.h +1137 -0
  222. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/block_cipher.h +76 -0
  223. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/camellia.h +302 -0
  224. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ccm.h +523 -0
  225. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/chacha20.h +230 -0
  226. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/chachapoly.h +339 -0
  227. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/cipher.h +1173 -0
  228. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/cmac.h +209 -0
  229. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/config_adjust_test_accelerators.h +111 -0
  230. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_adjust_config_enable_builtins.h +820 -0
  231. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_adjust_config_tweak_builtins.h +203 -0
  232. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_builtin_composites.h +278 -0
  233. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_builtin_key_derivation.h +120 -0
  234. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_builtin_primitives.h +136 -0
  235. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ctr_drbg.h +554 -0
  236. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ecdsa.h +667 -0
  237. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ecjpake.h +296 -0
  238. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ecp.h +1452 -0
  239. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/entropy.h +253 -0
  240. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/error_common.h +79 -0
  241. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/gcm.h +382 -0
  242. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/hmac_drbg.h +426 -0
  243. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/md5.h +169 -0
  244. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/pkcs5.h +123 -0
  245. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/poly1305.h +165 -0
  246. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ripemd160.h +120 -0
  247. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/rsa.h +821 -0
  248. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha1.h +196 -0
  249. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha256.h +180 -0
  250. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha3.h +189 -0
  251. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha512.h +188 -0
  252. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private_access.h +53 -0
  253. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aes.c +2236 -0
  254. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.c +624 -0
  255. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.h +135 -0
  256. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesni.c +846 -0
  257. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesni.h +162 -0
  258. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aria.c +966 -0
  259. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum.c +2635 -0
  260. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.c +1242 -0
  261. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.h +885 -0
  262. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core_invasive.h +38 -0
  263. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_internal.h +164 -0
  264. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.c +394 -0
  265. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.h +452 -0
  266. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod_raw.c +276 -0
  267. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod_raw.h +416 -0
  268. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod_raw_invasive.h +34 -0
  269. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/block_cipher.c +207 -0
  270. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/block_cipher_internal.h +99 -0
  271. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bn_mul.h +1094 -0
  272. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/camellia.c +1055 -0
  273. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ccm.c +773 -0
  274. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chacha20.c +576 -0
  275. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chacha20_neon.c +294 -0
  276. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chacha20_neon.h +62 -0
  277. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chachapoly.c +479 -0
  278. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher.c +1126 -0
  279. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher_invasive.h +28 -0
  280. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher_wrap.c +1937 -0
  281. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher_wrap.h +173 -0
  282. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cmac.c +911 -0
  283. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ctr.h +35 -0
  284. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ctr_drbg.c +1017 -0
  285. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecdsa.c +826 -0
  286. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecjpake.c +1206 -0
  287. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp.c +3478 -0
  288. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_curves.c +4798 -0
  289. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_invasive.h +220 -0
  290. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/entropy.c +677 -0
  291. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/entropy_poll.c +89 -0
  292. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/entropy_poll.h +44 -0
  293. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/gcm.c +1314 -0
  294. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/hmac_drbg.c +639 -0
  295. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/md5.c +419 -0
  296. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/md_psa.h +26 -0
  297. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/poly1305.c +488 -0
  298. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_aead.c +648 -0
  299. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_aead.h +499 -0
  300. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_cipher.c +752 -0
  301. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_cipher.h +314 -0
  302. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c +921 -0
  303. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h +516 -0
  304. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ffdh.c +781 -0
  305. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ffdh.h +131 -0
  306. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_hash.c +458 -0
  307. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_hash.h +211 -0
  308. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_mac.c +495 -0
  309. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_mac.h +264 -0
  310. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_pake.c +568 -0
  311. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_pake.h +159 -0
  312. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.c +698 -0
  313. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.h +321 -0
  314. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_xof.c +149 -0
  315. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_xof.h +155 -0
  316. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_util_internal.c +282 -0
  317. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_util_internal.h +132 -0
  318. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ripemd160.c +482 -0
  319. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa.c +2720 -0
  320. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_alt_helpers.c +294 -0
  321. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_alt_helpers.h +185 -0
  322. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_internal.h +172 -0
  323. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_invasive.h +57 -0
  324. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha1.c +473 -0
  325. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha256.c +956 -0
  326. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha3.c +951 -0
  327. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha512.c +1089 -0
  328. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/driver.cmake +54 -0
  329. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/CMakeLists.txt +45 -0
  330. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/Makefile.inc +5 -0
  331. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/README.md +5 -0
  332. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/Hacl_Curve25519.h +25 -0
  333. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlib/FStar_UInt128.h +124 -0
  334. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlib/FStar_UInt64_FStar_UInt32_FStar_UInt16_FStar_UInt8.h +280 -0
  335. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlib.h +29 -0
  336. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/c_endianness.h +206 -0
  337. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/builtin.h +16 -0
  338. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/callconv.h +48 -0
  339. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/compat.h +34 -0
  340. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/debug.h +57 -0
  341. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/target.h +102 -0
  342. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/types.h +61 -0
  343. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/wasmsupport.h +5 -0
  344. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/vs2013/Hacl_Curve25519.h +21 -0
  345. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/x25519.h +48 -0
  346. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/Hacl_Curve25519.c +760 -0
  347. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/Hacl_Curve25519_joined.c +50 -0
  348. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/kremlib/FStar_UInt128_extracted.c +413 -0
  349. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/kremlib/FStar_UInt64_FStar_UInt32_FStar_UInt16_FStar_UInt8.c +100 -0
  350. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/legacy/Hacl_Curve25519.c +805 -0
  351. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/x25519.c +52 -0
  352. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/CMakeLists.txt +36 -0
  353. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/Makefile.inc +5 -0
  354. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/README.md +4 -0
  355. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m/README.md +544 -0
  356. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m/p256-m.c +1514 -0
  357. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m/p256-m.h +135 -0
  358. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m_driver_entrypoints.c +312 -0
  359. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m_driver_entrypoints.h +219 -0
  360. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/CMakeLists.txt +14 -0
  361. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/Makefile.inc +6 -0
  362. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/include/tf-psa-crypto/private/crypto_struct_pqcp.h +52 -0
  363. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/fips202_psa.h +117 -0
  364. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/pqcp-config.h +68 -0
  365. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/psa_crypto_mldsa.c +528 -0
  366. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/psa_crypto_mldsa.h +301 -0
  367. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/wrap_mldsa_native.c +66 -0
  368. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/wrap_mldsa_native.h +37 -0
  369. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/CMakeLists.txt +3 -0
  370. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/lmots.c +768 -0
  371. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/lmots.h +272 -0
  372. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/lms.c +772 -0
  373. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/md.c +1182 -0
  374. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/md_wrap.h +46 -0
  375. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/nist_kw.c +458 -0
  376. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk.c +1464 -0
  377. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_ecc.c +175 -0
  378. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_internal.h +188 -0
  379. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_rsa.c +106 -0
  380. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_wrap.c +667 -0
  381. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_wrap.h +101 -0
  382. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pkparse.c +1351 -0
  383. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pkwrite.c +574 -0
  384. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pkwrite.h +101 -0
  385. package/cpp/contrib/mbedtls/tf-psa-crypto/framework/CMakeLists.txt +1 -0
  386. package/cpp/contrib/mbedtls/tf-psa-crypto/framework/LICENSE +553 -0
  387. package/cpp/contrib/mbedtls/tf-psa-crypto/include/CMakeLists.txt +41 -0
  388. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/asn1.h +623 -0
  389. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/asn1write.h +397 -0
  390. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/base64.h +81 -0
  391. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/compat-3-crypto.h +43 -0
  392. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/constant_time.h +36 -0
  393. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/lms.h +431 -0
  394. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/md.h +538 -0
  395. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/memory_buffer_alloc.h +142 -0
  396. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/nist_kw.h +106 -0
  397. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/pem.h +157 -0
  398. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/pk.h +888 -0
  399. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/platform.h +513 -0
  400. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/platform_time.h +79 -0
  401. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/platform_util.h +197 -0
  402. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/private/pk_private.h +150 -0
  403. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/psa_util.h +199 -0
  404. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/threading.h +412 -0
  405. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto.h +6082 -0
  406. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_compat.h +74 -0
  407. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_config.h +2073 -0
  408. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_common.h +42 -0
  409. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_contexts_composites.h +182 -0
  410. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_contexts_key_derivation.h +54 -0
  411. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_contexts_primitives.h +135 -0
  412. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_random.h +49 -0
  413. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_extra.h +2115 -0
  414. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_platform.h +94 -0
  415. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_sizes.h +1301 -0
  416. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_struct.h +682 -0
  417. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_types.h +466 -0
  418. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_values.h +2827 -0
  419. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/build_info.h +193 -0
  420. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_auto_enabled.h +23 -0
  421. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_dependencies.h +58 -0
  422. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_derived.h +135 -0
  423. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_key_pair_types.h +93 -0
  424. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_support.h +211 -0
  425. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_synonyms.h +41 -0
  426. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/version.h +44 -0
  427. package/cpp/contrib/mbedtls/tf-psa-crypto/objlib.cmake +71 -0
  428. package/cpp/contrib/mbedtls/tf-psa-crypto/pkgconfig/CMakeLists.txt +15 -0
  429. package/cpp/contrib/mbedtls/tf-psa-crypto/pkgconfig/JoinPaths.cmake +27 -0
  430. package/cpp/contrib/mbedtls/tf-psa-crypto/pkgconfig/tfpsacrypto.pc.in +10 -0
  431. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/CMakeLists.txt +3 -0
  432. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/memory_buffer_alloc.c +751 -0
  433. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/platform.c +362 -0
  434. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/platform_util.c +428 -0
  435. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/threading.c +311 -0
  436. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/threading_internal.h +75 -0
  437. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/CMakeLists.txt +3 -0
  438. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/asn1parse.c +483 -0
  439. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/asn1write.c +465 -0
  440. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/base64.c +322 -0
  441. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/base64_internal.h +45 -0
  442. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/constant_time.c +247 -0
  443. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/constant_time_impl.h +514 -0
  444. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/constant_time_internal.h +540 -0
  445. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/crypto_oid.h +354 -0
  446. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/oid.c +469 -0
  447. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/pem.c +458 -0
  448. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/pkcs5.c +475 -0
  449. package/cpp/contrib/monero/LICENSE +41 -0
  450. package/cpp/contrib/monero/bsx/common/perf_timer.h +15 -0
  451. package/cpp/contrib/monero/bsx/common/threadpool.h +43 -0
  452. package/cpp/contrib/monero/bsx/hex.h +25 -0
  453. package/cpp/contrib/monero/bsx/misc_log_ex.h +61 -0
  454. package/cpp/contrib/monero/bsx/random.cpp +20 -0
  455. package/cpp/contrib/monero/contrib/epee/include/int-util.h +320 -0
  456. package/cpp/contrib/monero/contrib/epee/include/memwipe.h +81 -0
  457. package/cpp/contrib/monero/contrib/epee/include/misc_language.h +109 -0
  458. package/cpp/contrib/monero/contrib/epee/include/mlocker.h +89 -0
  459. package/cpp/contrib/monero/contrib/epee/include/span.h +189 -0
  460. package/cpp/contrib/monero/contrib/epee/include/warnings.h +31 -0
  461. package/cpp/contrib/monero/contrib/epee/src/memwipe.c +115 -0
  462. package/cpp/contrib/monero/contrib/epee/src/mlocker.cpp +197 -0
  463. package/cpp/contrib/monero/src/common/aligned.c +139 -0
  464. package/cpp/contrib/monero/src/common/aligned.h +42 -0
  465. package/cpp/contrib/monero/src/common/base58.cpp +259 -0
  466. package/cpp/contrib/monero/src/common/base58.h +46 -0
  467. package/cpp/contrib/monero/src/common/pod-class.h +43 -0
  468. package/cpp/contrib/monero/src/common/varint.h +138 -0
  469. package/cpp/contrib/monero/src/crypto/chacha.h +96 -0
  470. package/cpp/contrib/monero/src/crypto/crypto-ops-data.c +881 -0
  471. package/cpp/contrib/monero/src/crypto/crypto-ops.c +3891 -0
  472. package/cpp/contrib/monero/src/crypto/crypto-ops.h +167 -0
  473. package/cpp/contrib/monero/src/crypto/crypto.cpp +792 -0
  474. package/cpp/contrib/monero/src/crypto/crypto.h +359 -0
  475. package/cpp/contrib/monero/src/crypto/generic-ops.h +103 -0
  476. package/cpp/contrib/monero/src/crypto/hash-ops.h +105 -0
  477. package/cpp/contrib/monero/src/crypto/hash.c +57 -0
  478. package/cpp/contrib/monero/src/crypto/hash.h +110 -0
  479. package/cpp/contrib/monero/src/crypto/keccak.c +239 -0
  480. package/cpp/contrib/monero/src/crypto/keccak.h +40 -0
  481. package/cpp/contrib/monero/src/crypto/random.h +46 -0
  482. package/cpp/contrib/monero/src/crypto/tree-hash.c +257 -0
  483. package/cpp/contrib/monero/src/crypto/wallet/crypto.h +57 -0
  484. package/cpp/contrib/monero/src/crypto/wallet/ops.h +32 -0
  485. package/cpp/contrib/monero/src/cryptonote_config.h +365 -0
  486. package/cpp/contrib/monero/src/device/device.cpp +105 -0
  487. package/cpp/contrib/monero/src/device/device.hpp +274 -0
  488. package/cpp/contrib/monero/src/device/device_default.cpp +343 -0
  489. package/cpp/contrib/monero/src/device/device_default.hpp +156 -0
  490. package/cpp/contrib/monero/src/ringct/bulletproofs.cc +1097 -0
  491. package/cpp/contrib/monero/src/ringct/bulletproofs.h +51 -0
  492. package/cpp/contrib/monero/src/ringct/bulletproofs_plus.cc +1120 -0
  493. package/cpp/contrib/monero/src/ringct/bulletproofs_plus.h +49 -0
  494. package/cpp/contrib/monero/src/ringct/multiexp.cc +708 -0
  495. package/cpp/contrib/monero/src/ringct/multiexp.h +72 -0
  496. package/cpp/contrib/monero/src/ringct/rctCryptoOps.c +221 -0
  497. package/cpp/contrib/monero/src/ringct/rctCryptoOps.h +37 -0
  498. package/cpp/contrib/monero/src/ringct/rctOps.cpp +727 -0
  499. package/cpp/contrib/monero/src/ringct/rctOps.h +191 -0
  500. package/cpp/contrib/monero/src/ringct/rctSigs.cpp +1652 -0
  501. package/cpp/contrib/monero/src/ringct/rctSigs.h +143 -0
  502. package/cpp/contrib/monero/src/ringct/rctTypes.cpp +338 -0
  503. package/cpp/contrib/monero/src/ringct/rctTypes.h +842 -0
  504. package/cpp/contrib/monero/src/serialization/binary_archive.h +243 -0
  505. package/cpp/contrib/monero/src/serialization/container.h +146 -0
  506. package/cpp/contrib/monero/src/serialization/containers.h +127 -0
  507. package/cpp/contrib/monero/src/serialization/crypto.h +97 -0
  508. package/cpp/contrib/monero/src/serialization/debug_archive.h +53 -0
  509. package/cpp/contrib/monero/src/serialization/json_archive.h +193 -0
  510. package/cpp/contrib/monero/src/serialization/serialization.h +328 -0
  511. package/cpp/contrib/monero/src/serialization/variant.h +50 -0
  512. package/cpp/contrib/nlohmann/LICENSE.MIT +21 -0
  513. package/cpp/contrib/nlohmann/json.hpp +25526 -0
  514. package/cpp/contrib/nlohmann/json_fwd.hpp +187 -0
  515. package/cpp/contrib/notes.txt +96 -0
  516. package/cpp/contrib/secp256k1/CHANGELOG.md +214 -0
  517. package/cpp/contrib/secp256k1/CMakeLists.txt +392 -0
  518. package/cpp/contrib/secp256k1/CMakePresets.json +18 -0
  519. package/cpp/contrib/secp256k1/CONTRIBUTING.md +111 -0
  520. package/cpp/contrib/secp256k1/COPYING +19 -0
  521. package/cpp/contrib/secp256k1/Makefile.am +332 -0
  522. package/cpp/contrib/secp256k1/README.md +176 -0
  523. package/cpp/contrib/secp256k1/SECURITY.md +15 -0
  524. package/cpp/contrib/secp256k1/autogen.sh +3 -0
  525. package/cpp/contrib/secp256k1/ci/ci.sh +147 -0
  526. package/cpp/contrib/secp256k1/ci/linux-debian.Dockerfile +84 -0
  527. package/cpp/contrib/secp256k1/ci/travis.sh +68 -0
  528. package/cpp/contrib/secp256k1/cmake/CheckArm32Assembly.cmake +6 -0
  529. package/cpp/contrib/secp256k1/cmake/CheckMemorySanitizer.cmake +18 -0
  530. package/cpp/contrib/secp256k1/cmake/CheckStringOptionValue.cmake +10 -0
  531. package/cpp/contrib/secp256k1/cmake/CheckX86_64Assembly.cmake +15 -0
  532. package/cpp/contrib/secp256k1/cmake/DiscoverTests.cmake +71 -0
  533. package/cpp/contrib/secp256k1/cmake/FindValgrind.cmake +41 -0
  534. package/cpp/contrib/secp256k1/cmake/GeneratePkgConfigFile.cmake +8 -0
  535. package/cpp/contrib/secp256k1/cmake/TryAppendCFlags.cmake +24 -0
  536. package/cpp/contrib/secp256k1/cmake/arm-linux-gnueabihf.toolchain.cmake +3 -0
  537. package/cpp/contrib/secp256k1/cmake/config.cmake.in +5 -0
  538. package/cpp/contrib/secp256k1/cmake/source_arm32.s +9 -0
  539. package/cpp/contrib/secp256k1/cmake/x86_64-w64-mingw32.toolchain.cmake +3 -0
  540. package/cpp/contrib/secp256k1/configure.ac +590 -0
  541. package/cpp/contrib/secp256k1/contrib/lax_der_parsing.c +148 -0
  542. package/cpp/contrib/secp256k1/contrib/lax_der_parsing.h +97 -0
  543. package/cpp/contrib/secp256k1/contrib/lax_der_privatekey_parsing.c +112 -0
  544. package/cpp/contrib/secp256k1/contrib/lax_der_privatekey_parsing.h +95 -0
  545. package/cpp/contrib/secp256k1/doc/ellswift.md +483 -0
  546. package/cpp/contrib/secp256k1/doc/musig.md +54 -0
  547. package/cpp/contrib/secp256k1/doc/release-process.md +94 -0
  548. package/cpp/contrib/secp256k1/doc/safegcd_implementation.md +819 -0
  549. package/cpp/contrib/secp256k1/examples/CMakeLists.txt +33 -0
  550. package/cpp/contrib/secp256k1/examples/EXAMPLES_COPYING +121 -0
  551. package/cpp/contrib/secp256k1/examples/ecdh.c +121 -0
  552. package/cpp/contrib/secp256k1/examples/ecdsa.c +138 -0
  553. package/cpp/contrib/secp256k1/examples/ellswift.c +122 -0
  554. package/cpp/contrib/secp256k1/examples/examples_util.h +108 -0
  555. package/cpp/contrib/secp256k1/examples/musig.c +261 -0
  556. package/cpp/contrib/secp256k1/examples/schnorr.c +154 -0
  557. package/cpp/contrib/secp256k1/include/secp256k1.h +877 -0
  558. package/cpp/contrib/secp256k1/include/secp256k1_dleag.h +150 -0
  559. package/cpp/contrib/secp256k1/include/secp256k1_ecdh.h +63 -0
  560. package/cpp/contrib/secp256k1/include/secp256k1_ecdsaotves.h +55 -0
  561. package/cpp/contrib/secp256k1/include/secp256k1_ed25519.h +239 -0
  562. package/cpp/contrib/secp256k1/include/secp256k1_ellswift.h +200 -0
  563. package/cpp/contrib/secp256k1/include/secp256k1_extrakeys.h +250 -0
  564. package/cpp/contrib/secp256k1/include/secp256k1_generator.h +106 -0
  565. package/cpp/contrib/secp256k1/include/secp256k1_hash.h +45 -0
  566. package/cpp/contrib/secp256k1/include/secp256k1_musig.h +588 -0
  567. package/cpp/contrib/secp256k1/include/secp256k1_preallocated.h +134 -0
  568. package/cpp/contrib/secp256k1/include/secp256k1_recovery.h +123 -0
  569. package/cpp/contrib/secp256k1/include/secp256k1_schnorrsig.h +190 -0
  570. package/cpp/contrib/secp256k1/include/visibility.h +58 -0
  571. package/cpp/contrib/secp256k1/libsecp256k1.pc.in +12 -0
  572. package/cpp/contrib/secp256k1/sage/gen_exhaustive_groups.sage +156 -0
  573. package/cpp/contrib/secp256k1/sage/gen_split_lambda_constants.sage +123 -0
  574. package/cpp/contrib/secp256k1/sage/group_prover.sage +353 -0
  575. package/cpp/contrib/secp256k1/sage/prove_group_implementations.sage +285 -0
  576. package/cpp/contrib/secp256k1/sage/secp256k1_params.sage +39 -0
  577. package/cpp/contrib/secp256k1/sage/weierstrass_prover.sage +275 -0
  578. package/cpp/contrib/secp256k1/src/CMakeLists.txt +253 -0
  579. package/cpp/contrib/secp256k1/src/asm/field_10x26_arm.s +916 -0
  580. package/cpp/contrib/secp256k1/src/assumptions.h +87 -0
  581. package/cpp/contrib/secp256k1/src/checkmem.h +117 -0
  582. package/cpp/contrib/secp256k1/src/ecdsa.h +21 -0
  583. package/cpp/contrib/secp256k1/src/ecdsa_impl.h +312 -0
  584. package/cpp/contrib/secp256k1/src/eckey.h +28 -0
  585. package/cpp/contrib/secp256k1/src/eckey_impl.h +94 -0
  586. package/cpp/contrib/secp256k1/src/ecmult.h +64 -0
  587. package/cpp/contrib/secp256k1/src/ecmult_compute_table.h +16 -0
  588. package/cpp/contrib/secp256k1/src/ecmult_compute_table_impl.h +49 -0
  589. package/cpp/contrib/secp256k1/src/ecmult_const.h +38 -0
  590. package/cpp/contrib/secp256k1/src/ecmult_const_impl.h +402 -0
  591. package/cpp/contrib/secp256k1/src/ecmult_gen.h +144 -0
  592. package/cpp/contrib/secp256k1/src/ecmult_gen_compute_table.h +14 -0
  593. package/cpp/contrib/secp256k1/src/ecmult_gen_compute_table_impl.h +108 -0
  594. package/cpp/contrib/secp256k1/src/ecmult_gen_impl.h +341 -0
  595. package/cpp/contrib/secp256k1/src/ecmult_impl.h +869 -0
  596. package/cpp/contrib/secp256k1/src/field.h +354 -0
  597. package/cpp/contrib/secp256k1/src/field_10x26.h +57 -0
  598. package/cpp/contrib/secp256k1/src/field_10x26_impl.h +1234 -0
  599. package/cpp/contrib/secp256k1/src/field_5x52.h +62 -0
  600. package/cpp/contrib/secp256k1/src/field_5x52_impl.h +524 -0
  601. package/cpp/contrib/secp256k1/src/field_5x52_int128_impl.h +274 -0
  602. package/cpp/contrib/secp256k1/src/field_impl.h +462 -0
  603. package/cpp/contrib/secp256k1/src/group.h +224 -0
  604. package/cpp/contrib/secp256k1/src/group_impl.h +1024 -0
  605. package/cpp/contrib/secp256k1/src/hash.h +54 -0
  606. package/cpp/contrib/secp256k1/src/hash_impl.h +360 -0
  607. package/cpp/contrib/secp256k1/src/hsort.h +33 -0
  608. package/cpp/contrib/secp256k1/src/hsort_impl.h +125 -0
  609. package/cpp/contrib/secp256k1/src/int128.h +90 -0
  610. package/cpp/contrib/secp256k1/src/int128_impl.h +18 -0
  611. package/cpp/contrib/secp256k1/src/int128_native.h +19 -0
  612. package/cpp/contrib/secp256k1/src/int128_native_impl.h +94 -0
  613. package/cpp/contrib/secp256k1/src/int128_struct.h +14 -0
  614. package/cpp/contrib/secp256k1/src/int128_struct_impl.h +205 -0
  615. package/cpp/contrib/secp256k1/src/modinv32.h +43 -0
  616. package/cpp/contrib/secp256k1/src/modinv32_impl.h +725 -0
  617. package/cpp/contrib/secp256k1/src/modinv64.h +47 -0
  618. package/cpp/contrib/secp256k1/src/modinv64_impl.h +780 -0
  619. package/cpp/contrib/secp256k1/src/modules/dleag/Makefile.am.include +3 -0
  620. package/cpp/contrib/secp256k1/src/modules/dleag/main_impl.h +1073 -0
  621. package/cpp/contrib/secp256k1/src/modules/dleag/tests_impl.h +68 -0
  622. package/cpp/contrib/secp256k1/src/modules/ecdh/Makefile.am.include +5 -0
  623. package/cpp/contrib/secp256k1/src/modules/ecdh/bench_impl.h +54 -0
  624. package/cpp/contrib/secp256k1/src/modules/ecdh/main_impl.h +79 -0
  625. package/cpp/contrib/secp256k1/src/modules/ecdh/tests_impl.h +218 -0
  626. package/cpp/contrib/secp256k1/src/modules/ecdsaotves/Makefile.am.include +3 -0
  627. package/cpp/contrib/secp256k1/src/modules/ecdsaotves/main_impl.h +365 -0
  628. package/cpp/contrib/secp256k1/src/modules/ecdsaotves/tests_impl.h +49 -0
  629. package/cpp/contrib/secp256k1/src/modules/ed25519/Makefile.am.include +16 -0
  630. package/cpp/contrib/secp256k1/src/modules/ed25519/common.h +68 -0
  631. package/cpp/contrib/secp256k1/src/modules/ed25519/core_ed25519.h +229 -0
  632. package/cpp/contrib/secp256k1/src/modules/ed25519/main_impl.h +98 -0
  633. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/common.h +21 -0
  634. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/ed25519_ref10.h +2698 -0
  635. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/ed25519_ref10_fe_25_5.h +1056 -0
  636. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/ed25519_ref10_fe_51.h +518 -0
  637. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/base.h +1344 -0
  638. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/base2.h +40 -0
  639. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/constants.h +40 -0
  640. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/fe.h +220 -0
  641. package/cpp/contrib/secp256k1/src/modules/ed25519/scalarmult_ed25519_ref10.h +417 -0
  642. package/cpp/contrib/secp256k1/src/modules/ed25519/sha512/hash_sha512.h +14 -0
  643. package/cpp/contrib/secp256k1/src/modules/ed25519/sha512/hash_sha512_cp.h +287 -0
  644. package/cpp/contrib/secp256k1/src/modules/ed25519/tests_impl.h +109 -0
  645. package/cpp/contrib/secp256k1/src/modules/ed25519/utils.h +97 -0
  646. package/cpp/contrib/secp256k1/src/modules/ellswift/Makefile.am.include +5 -0
  647. package/cpp/contrib/secp256k1/src/modules/ellswift/bench_impl.h +106 -0
  648. package/cpp/contrib/secp256k1/src/modules/ellswift/main_impl.h +581 -0
  649. package/cpp/contrib/secp256k1/src/modules/ellswift/tests_exhaustive_impl.h +39 -0
  650. package/cpp/contrib/secp256k1/src/modules/ellswift/tests_impl.h +544 -0
  651. package/cpp/contrib/secp256k1/src/modules/extrakeys/Makefile.am.include +4 -0
  652. package/cpp/contrib/secp256k1/src/modules/extrakeys/main_impl.h +285 -0
  653. package/cpp/contrib/secp256k1/src/modules/extrakeys/tests_exhaustive_impl.h +68 -0
  654. package/cpp/contrib/secp256k1/src/modules/extrakeys/tests_impl.h +484 -0
  655. package/cpp/contrib/secp256k1/src/modules/generator/Makefile.am.include +9 -0
  656. package/cpp/contrib/secp256k1/src/modules/generator/main_impl.h +271 -0
  657. package/cpp/contrib/secp256k1/src/modules/generator/tests_impl.h +227 -0
  658. package/cpp/contrib/secp256k1/src/modules/musig/Makefile.am.include +8 -0
  659. package/cpp/contrib/secp256k1/src/modules/musig/keyagg.h +32 -0
  660. package/cpp/contrib/secp256k1/src/modules/musig/keyagg_impl.h +275 -0
  661. package/cpp/contrib/secp256k1/src/modules/musig/main_impl.h +12 -0
  662. package/cpp/contrib/secp256k1/src/modules/musig/session.h +24 -0
  663. package/cpp/contrib/secp256k1/src/modules/musig/session_impl.h +795 -0
  664. package/cpp/contrib/secp256k1/src/modules/musig/tests_impl.h +1161 -0
  665. package/cpp/contrib/secp256k1/src/modules/musig/vectors.h +346 -0
  666. package/cpp/contrib/secp256k1/src/modules/recovery/Makefile.am.include +5 -0
  667. package/cpp/contrib/secp256k1/src/modules/recovery/bench_impl.h +62 -0
  668. package/cpp/contrib/secp256k1/src/modules/recovery/main_impl.h +159 -0
  669. package/cpp/contrib/secp256k1/src/modules/recovery/tests_exhaustive_impl.h +148 -0
  670. package/cpp/contrib/secp256k1/src/modules/recovery/tests_impl.h +339 -0
  671. package/cpp/contrib/secp256k1/src/modules/schnorrsig/Makefile.am.include +5 -0
  672. package/cpp/contrib/secp256k1/src/modules/schnorrsig/bench_impl.h +104 -0
  673. package/cpp/contrib/secp256k1/src/modules/schnorrsig/main_impl.h +263 -0
  674. package/cpp/contrib/secp256k1/src/modules/schnorrsig/tests_exhaustive_impl.h +214 -0
  675. package/cpp/contrib/secp256k1/src/modules/schnorrsig/tests_impl.h +1010 -0
  676. package/cpp/contrib/secp256k1/src/precompute_ecmult.c +91 -0
  677. package/cpp/contrib/secp256k1/src/precompute_ecmult_gen.c +101 -0
  678. package/cpp/contrib/secp256k1/src/precomputed_ecmult.c +16456 -0
  679. package/cpp/contrib/secp256k1/src/precomputed_ecmult.h +40 -0
  680. package/cpp/contrib/secp256k1/src/precomputed_ecmult_gen.c +1779 -0
  681. package/cpp/contrib/secp256k1/src/precomputed_ecmult_gen.h +28 -0
  682. package/cpp/contrib/secp256k1/src/scalar.h +108 -0
  683. package/cpp/contrib/secp256k1/src/scalar_4x64.h +19 -0
  684. package/cpp/contrib/secp256k1/src/scalar_4x64_impl.h +1010 -0
  685. package/cpp/contrib/secp256k1/src/scalar_8x32.h +19 -0
  686. package/cpp/contrib/secp256k1/src/scalar_8x32_impl.h +830 -0
  687. package/cpp/contrib/secp256k1/src/scalar_impl.h +321 -0
  688. package/cpp/contrib/secp256k1/src/scalar_low.h +24 -0
  689. package/cpp/contrib/secp256k1/src/scalar_low_impl.h +215 -0
  690. package/cpp/contrib/secp256k1/src/scratch.h +44 -0
  691. package/cpp/contrib/secp256k1/src/scratch_impl.h +99 -0
  692. package/cpp/contrib/secp256k1/src/secp256k1.c +896 -0
  693. package/cpp/contrib/secp256k1/src/selftest.h +35 -0
  694. package/cpp/contrib/secp256k1/src/testrand.h +45 -0
  695. package/cpp/contrib/secp256k1/src/testrand_impl.h +162 -0
  696. package/cpp/contrib/secp256k1/src/testutil.h +161 -0
  697. package/cpp/contrib/secp256k1/src/unit_test.c +479 -0
  698. package/cpp/contrib/secp256k1/src/unit_test.h +147 -0
  699. package/cpp/contrib/secp256k1/src/util.h +469 -0
  700. package/cpp/contrib/secp256k1/src/util_local_visibility.h +12 -0
  701. package/cpp/contrib/secp256k1/tools/check-abi.sh +67 -0
  702. package/cpp/contrib/secp256k1/tools/symbol-check.py +72 -0
  703. package/cpp/contrib/secp256k1/tools/test_vectors_musig2_generate.py +656 -0
  704. package/cpp/contrib/secp256k1/tools/tests_wycheproof_generate_ecdh.py +166 -0
  705. package/cpp/contrib/secp256k1/tools/tests_wycheproof_generate_ecdsa.py +111 -0
  706. package/cpp/contrib/secp256k1/tools/wycheproof_utils.py +12 -0
  707. package/cpp/contrib/secp256k1/wasm/test_module.html +320 -0
  708. package/cpp/contrib/secp256k1/wasm/wrap.cpp +331 -0
  709. package/cpp/contrib/sqlite3.h +14349 -0
  710. package/cpp/contrib/sqlite3mc_amalgamation.c +369890 -0
  711. package/cpp/contrib/sqlite3mc_amalgamation.h +15548 -0
  712. package/cpp/contrib/unilib/uninorms.cpp +393 -0
  713. package/cpp/contrib/unilib/uninorms.h +56 -0
  714. package/cpp/contrib/unilib/utf8.cpp +78 -0
  715. package/cpp/contrib/unilib/utf8.h +260 -0
  716. package/cpp/contrib/update_mbedtls.sh +85 -0
  717. package/cpp/crypto/aes.cpp +141 -0
  718. package/cpp/crypto/aes.h +67 -0
  719. package/cpp/crypto/chacha20.cpp +342 -0
  720. package/cpp/crypto/chacha20.h +82 -0
  721. package/cpp/crypto/chacha20poly1305.cpp +91 -0
  722. package/cpp/crypto/chacha20poly1305.h +46 -0
  723. package/cpp/crypto/common.h +85 -0
  724. package/cpp/crypto/ctaes/COPYING +21 -0
  725. package/cpp/crypto/ctaes/README.md +41 -0
  726. package/cpp/crypto/ctaes/bench.c +170 -0
  727. package/cpp/crypto/ctaes/ctaes.c +556 -0
  728. package/cpp/crypto/ctaes/ctaes.h +41 -0
  729. package/cpp/crypto/ctaes/test.c +110 -0
  730. package/cpp/crypto/extkey.cpp +257 -0
  731. package/cpp/crypto/extkey.h +66 -0
  732. package/cpp/crypto/hmac_sha512.cpp +37 -0
  733. package/cpp/crypto/hmac_sha512.h +32 -0
  734. package/cpp/crypto/merkle.cpp +47 -0
  735. package/cpp/crypto/merkle.h +20 -0
  736. package/cpp/crypto/poly1305.cpp +223 -0
  737. package/cpp/crypto/poly1305.h +59 -0
  738. package/cpp/crypto/ripemd160.cpp +292 -0
  739. package/cpp/crypto/ripemd160.h +28 -0
  740. package/cpp/crypto/scrypt-sse2.cpp +141 -0
  741. package/cpp/crypto/scrypt.cpp +336 -0
  742. package/cpp/crypto/scrypt.h +49 -0
  743. package/cpp/crypto/sealed_key.cpp +62 -0
  744. package/cpp/crypto/sealed_key.h +34 -0
  745. package/cpp/crypto/secure_bytes.h +16 -0
  746. package/cpp/crypto/sha512.cpp +206 -0
  747. package/cpp/crypto/sha512.h +29 -0
  748. package/cpp/crypto/util.cpp +77 -0
  749. package/cpp/crypto/util.h +32 -0
  750. package/cpp/debug_rpc.cpp +703 -0
  751. package/cpp/debug_rpc.h +63 -0
  752. package/cpp/electrum.cpp +931 -0
  753. package/cpp/electrum.h +169 -0
  754. package/cpp/engine/xmr_engine.cpp +463 -0
  755. package/cpp/engine/xmr_engine.h +84 -0
  756. package/cpp/engine/xmr_engine_c.cpp +141 -0
  757. package/cpp/engine/xmr_engine_c.h +35 -0
  758. package/cpp/fees.cpp +270 -0
  759. package/cpp/fees.h +60 -0
  760. package/cpp/height_search.cpp +64 -0
  761. package/cpp/height_search.h +27 -0
  762. package/cpp/main.cpp +146 -0
  763. package/cpp/messages_npb.cpp +701 -0
  764. package/cpp/messages_npb.h +667 -0
  765. package/cpp/mnemonic/mnemonic.cpp +715 -0
  766. package/cpp/mnemonic/mnemonic.h +52 -0
  767. package/cpp/mnemonic/wordlists/english.h +1103 -0
  768. package/cpp/mnemonic/wordlists/japanese.h +2212 -0
  769. package/cpp/network/bridge.cpp +232 -0
  770. package/cpp/network/bridge.h +115 -0
  771. package/cpp/network/ca_roots.cpp +114 -0
  772. package/cpp/network/ca_roots.h +12 -0
  773. package/cpp/network/http_client.cpp +261 -0
  774. package/cpp/network/http_client.h +33 -0
  775. package/cpp/network/pem.cpp +84 -0
  776. package/cpp/network/pem.h +22 -0
  777. package/cpp/network/psa_user_config.h +15 -0
  778. package/cpp/network/socks5.cpp +180 -0
  779. package/cpp/network/socks5.h +26 -0
  780. package/cpp/network/tls_transport.cpp +502 -0
  781. package/cpp/network/tls_transport.h +62 -0
  782. package/cpp/network/transport.cpp +169 -0
  783. package/cpp/network/transport.h +115 -0
  784. package/cpp/network/url.cpp +88 -0
  785. package/cpp/network/url.h +25 -0
  786. package/cpp/network/websocket.cpp +253 -0
  787. package/cpp/network/websocket.h +68 -0
  788. package/cpp/network/ws_transport.cpp +361 -0
  789. package/cpp/network/ws_transport.h +72 -0
  790. package/cpp/node_client.cpp +1196 -0
  791. package/cpp/node_client.h +209 -0
  792. package/cpp/node_scan.h +35 -0
  793. package/cpp/schema.cpp +1249 -0
  794. package/cpp/schema.h +132 -0
  795. package/cpp/smsg.cpp +453 -0
  796. package/cpp/smsg.h +43 -0
  797. package/cpp/support/allocators/secure.h +85 -0
  798. package/cpp/support/cleanse.cpp +35 -0
  799. package/cpp/support/cleanse.h +15 -0
  800. package/cpp/support/coop.cpp +319 -0
  801. package/cpp/support/coop.h +157 -0
  802. package/cpp/support/lockedpool.cpp +406 -0
  803. package/cpp/support/lockedpool.h +240 -0
  804. package/cpp/uint256.cpp +62 -0
  805. package/cpp/uint256.h +121 -0
  806. package/cpp/util/base58.cpp +181 -0
  807. package/cpp/util/base58.h +45 -0
  808. package/cpp/util/bech32.cpp +578 -0
  809. package/cpp/util/bech32.h +96 -0
  810. package/cpp/util/debug.cpp +23 -0
  811. package/cpp/util/debug.h +12 -0
  812. package/cpp/util/int.cpp +118 -0
  813. package/cpp/util/int.h +18 -0
  814. package/cpp/util/json.h +26 -0
  815. package/cpp/util/log.cpp +137 -0
  816. package/cpp/util/log.h +37 -0
  817. package/cpp/util/mutex.h +89 -0
  818. package/cpp/util/platform.cpp +90 -0
  819. package/cpp/util/platform.h +17 -0
  820. package/cpp/util/random.cpp +136 -0
  821. package/cpp/util/random.h +28 -0
  822. package/cpp/util/rpc.cpp +192 -0
  823. package/cpp/util/rpc.h +454 -0
  824. package/cpp/util/str.cpp +254 -0
  825. package/cpp/util/str.h +54 -0
  826. package/cpp/util/thread.cpp +17 -0
  827. package/cpp/util/thread.h +12 -0
  828. package/cpp/util/time.cpp +30 -0
  829. package/cpp/util/time.h +30 -0
  830. package/cpp/util/tinyformat.h +1163 -0
  831. package/cpp/util/vector.h +83 -0
  832. package/cpp/version.h.in +12 -0
  833. package/cpp/view_key_chain.h +283 -0
  834. package/cpp/wallet/account_wallet.cpp +668 -0
  835. package/cpp/wallet/account_wallet.h +136 -0
  836. package/cpp/wallet/base_wallet.cpp +302 -0
  837. package/cpp/wallet/base_wallet.h +129 -0
  838. package/cpp/wallet/debug_wallet.cpp +40 -0
  839. package/cpp/wallet/debug_wallet.h +36 -0
  840. package/cpp/wallet/encrypted_seed.cpp +96 -0
  841. package/cpp/wallet/encrypted_seed.h +41 -0
  842. package/cpp/wallet/host_wallet.cpp +85 -0
  843. package/cpp/wallet/host_wallet.h +51 -0
  844. package/cpp/wallet/native_wallet.cpp +34 -0
  845. package/cpp/wallet/native_wallet.h +28 -0
  846. package/cpp/wallet/xmr/account_wallet.cpp +803 -0
  847. package/cpp/wallet/xmr/account_wallet.h +139 -0
  848. package/cpp/wallet/xmr/builder.cpp +568 -0
  849. package/cpp/wallet/xmr/builder.h +65 -0
  850. package/cpp/wallet/xmr/chain.cpp +1437 -0
  851. package/cpp/wallet/xmr/chain.h +213 -0
  852. package/cpp/wallet/xmr/daemon.cpp +583 -0
  853. package/cpp/wallet/xmr/daemon.h +153 -0
  854. package/cpp/wallet/xmr/decoys.cpp +197 -0
  855. package/cpp/wallet/xmr/decoys.h +64 -0
  856. package/cpp/wallet/xmr/epee.cpp +448 -0
  857. package/cpp/wallet/xmr/epee.h +25 -0
  858. package/cpp/wallet/xmr/fee.cpp +169 -0
  859. package/cpp/wallet/xmr/fee.h +61 -0
  860. package/cpp/wallet/xmr/reserve_proof.cpp +169 -0
  861. package/cpp/wallet/xmr/reserve_proof.h +24 -0
  862. package/cpp/wallet/xmr/scanner.cpp +860 -0
  863. package/cpp/wallet/xmr/scanner.h +239 -0
  864. package/lib/commonjs/NativeNitroBSX.js +128 -0
  865. package/lib/commonjs/NativeNitroBSX.js.map +1 -0
  866. package/lib/commonjs/format.js +21 -0
  867. package/lib/commonjs/format.js.map +1 -0
  868. package/lib/commonjs/index.js +50 -0
  869. package/lib/commonjs/index.js.map +1 -0
  870. package/lib/commonjs/service.js +75 -0
  871. package/lib/commonjs/service.js.map +1 -0
  872. package/lib/commonjs/specs/NitroBSX.nitro.js +6 -0
  873. package/lib/commonjs/specs/NitroBSX.nitro.js.map +1 -0
  874. package/lib/module/NativeNitroBSX.js +124 -0
  875. package/lib/module/NativeNitroBSX.js.map +1 -0
  876. package/lib/module/format.js +15 -0
  877. package/lib/module/format.js.map +1 -0
  878. package/lib/module/index.js +7 -0
  879. package/lib/module/index.js.map +1 -0
  880. package/lib/module/service.js +66 -0
  881. package/lib/module/service.js.map +1 -0
  882. package/lib/module/specs/NitroBSX.nitro.js +4 -0
  883. package/lib/module/specs/NitroBSX.nitro.js.map +1 -0
  884. package/lib/typescript/commonjs/package.json +1 -0
  885. package/lib/typescript/commonjs/src/NativeNitroBSX.d.ts +94 -0
  886. package/lib/typescript/commonjs/src/NativeNitroBSX.d.ts.map +1 -0
  887. package/lib/typescript/commonjs/src/format.d.ts +3 -0
  888. package/lib/typescript/commonjs/src/format.d.ts.map +1 -0
  889. package/lib/typescript/commonjs/src/index.d.ts +5 -0
  890. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  891. package/lib/typescript/commonjs/src/service.d.ts +28 -0
  892. package/lib/typescript/commonjs/src/service.d.ts.map +1 -0
  893. package/lib/typescript/commonjs/src/specs/NitroBSX.nitro.d.ts +251 -0
  894. package/lib/typescript/commonjs/src/specs/NitroBSX.nitro.d.ts.map +1 -0
  895. package/lib/typescript/module/package.json +1 -0
  896. package/lib/typescript/module/src/NativeNitroBSX.d.ts +94 -0
  897. package/lib/typescript/module/src/NativeNitroBSX.d.ts.map +1 -0
  898. package/lib/typescript/module/src/format.d.ts +3 -0
  899. package/lib/typescript/module/src/format.d.ts.map +1 -0
  900. package/lib/typescript/module/src/index.d.ts +5 -0
  901. package/lib/typescript/module/src/index.d.ts.map +1 -0
  902. package/lib/typescript/module/src/service.d.ts +28 -0
  903. package/lib/typescript/module/src/service.d.ts.map +1 -0
  904. package/lib/typescript/module/src/specs/NitroBSX.nitro.d.ts +251 -0
  905. package/lib/typescript/module/src/specs/NitroBSX.nitro.d.ts.map +1 -0
  906. package/nitro.json +19 -0
  907. package/nitrogen/generated/android/NitroBSX+autolinking.cmake +81 -0
  908. package/nitrogen/generated/android/NitroBSX+autolinking.gradle +27 -0
  909. package/nitrogen/generated/android/NitroBSXOnLoad.cpp +49 -0
  910. package/nitrogen/generated/android/NitroBSXOnLoad.hpp +34 -0
  911. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrobsx/NitroBSXOnLoad.kt +35 -0
  912. package/nitrogen/generated/ios/NitroBSX+autolinking.rb +62 -0
  913. package/nitrogen/generated/ios/NitroBSX-Swift-Cxx-Bridge.cpp +17 -0
  914. package/nitrogen/generated/ios/NitroBSX-Swift-Cxx-Bridge.hpp +27 -0
  915. package/nitrogen/generated/ios/NitroBSX-Swift-Cxx-Umbrella.hpp +38 -0
  916. package/nitrogen/generated/ios/NitroBSXAutolinking.mm +35 -0
  917. package/nitrogen/generated/ios/NitroBSXAutolinking.swift +16 -0
  918. package/nitrogen/generated/shared/c++/HybridNitroBSXSpec.cpp +91 -0
  919. package/nitrogen/generated/shared/c++/HybridNitroBSXSpec.hpp +142 -0
  920. package/nitrogen/generated/shared/c++/NotificationPair.hpp +87 -0
  921. package/nitrogen/generated/shared/c++/SignOutcome.hpp +80 -0
  922. package/nitrogen/generated/shared/c++/SignTxResult.hpp +89 -0
  923. package/package.json +172 -4
  924. package/react-native-nitro-bsx.podspec +33 -0
  925. package/react-native.config.js +26 -0
  926. package/scripts/build-ios.sh +49 -0
  927. package/src/NativeNitroBSX.ts +203 -0
  928. package/src/format.ts +14 -0
  929. package/src/index.ts +4 -0
  930. package/src/service.ts +77 -0
  931. package/src/specs/NitroBSX.nitro.ts +300 -0
@@ -0,0 +1,2200 @@
1
+ /*Copyright (c) 2025-2026 The Basicswap developers
2
+ Distributed under the MIT software license, see the accompanying
3
+ file LICENSE or http://www.opensource.org/licenses/mit-license.php.*/
4
+
5
+ #include "interface.h"
6
+ #include "BSXContext.h"
7
+ #include "bsx/types.h"
8
+ #include "crypto/util.h"
9
+ #include "primitives.h"
10
+ #include "script.h"
11
+ #include "util/base58.h"
12
+ #include "util/bech32.h"
13
+ #include "util/int.h"
14
+ #include "util/log.h"
15
+ #include "util/str.h"
16
+ #include "util/tinyformat.h"
17
+ #include <algorithm>
18
+ #include <cinttypes>
19
+ #include <cstdint>
20
+ #include <memory>
21
+ #include <string>
22
+
23
+ namespace bsx {
24
+ namespace btc_base {
25
+
26
+ namespace AddressTypes {
27
+ enum AddressTypes {
28
+ P2PKH = 1,
29
+ P2SH = 2,
30
+ P2WPKH = 3,
31
+ P2WSH = 4,
32
+ };
33
+ }; // namespace AddressTypes
34
+
35
+ static const int SEQUENCE_LOCKTIME_GRANULARITY = 9; // 512 seconds
36
+ static const int SEQUENCE_LOCKTIME_TYPE_FLAG = 1 << 22;
37
+ static const int SEQUENCE_LOCKTIME_MASK = 0x0000FFFF;
38
+
39
+ int Interface::getPubkey(std::vector<unsigned char> &out, std::span<const unsigned char> k) const
40
+ {
41
+ return m_context->getPubkey(out, k);
42
+ }
43
+
44
+ int Interface::getPubkeyHash(std::vector<unsigned char> &out,
45
+ const std::vector<unsigned char> &pk) const
46
+ {
47
+ if (pk.size() != 33) {
48
+ return 1;
49
+ }
50
+ out.resize(20);
51
+ return get_pubkeyid(out.data(), pk.data());
52
+ }
53
+
54
+ int Interface::sumKeys(secure_bytes &out, std::span<const unsigned char> a,
55
+ std::span<const unsigned char> b) const
56
+ {
57
+ if (a.size() != 32 || b.size() != 32) {
58
+ return 1;
59
+ }
60
+ out.resize(32);
61
+ return m_context->sumKeys(out.data(), a.data(), b.data());
62
+ }
63
+
64
+ int Interface::sumPubkeys(std::vector<unsigned char> &out, const std::vector<unsigned char> &A,
65
+ const std::vector<unsigned char> &B) const
66
+ {
67
+ if (A.size() != 33 || B.size() != 33) {
68
+ return 1;
69
+ }
70
+ out.resize(33);
71
+ return m_context->sumPubkeys(out.data(), A.data(), B.data());
72
+ }
73
+
74
+ int Interface::getExpectedSequence(uint32_t &sequence, int lock_type, int lock_value,
75
+ std::string &str_error) const
76
+ {
77
+ if (lock_value <= 0) {
78
+ str_error = "Invalid lock_value";
79
+ return 1;
80
+ }
81
+ if (lock_type == TxLockTypes::SEQUENCE_LOCK_BLOCKS) {
82
+ sequence = lock_value;
83
+ return 0;
84
+ }
85
+ if (lock_type == TxLockTypes::SEQUENCE_LOCK_TIME) {
86
+ int seconds_locked = lock_value;
87
+ // Ensure the locked time is never less than lock_value
88
+ if (seconds_locked % (1 << SEQUENCE_LOCKTIME_GRANULARITY) != 0) {
89
+ seconds_locked += 1 << SEQUENCE_LOCKTIME_GRANULARITY;
90
+ }
91
+ seconds_locked >>= SEQUENCE_LOCKTIME_GRANULARITY;
92
+ sequence = seconds_locked | SEQUENCE_LOCKTIME_TYPE_FLAG;
93
+ return 0;
94
+ }
95
+ str_error = "Unknown lock type";
96
+ return 1;
97
+ }
98
+
99
+ int Interface::decodeSequence(int &lock_value, uint32_t sequence) const
100
+ {
101
+ if (sequence & SEQUENCE_LOCKTIME_TYPE_FLAG) {
102
+ lock_value = (sequence & SEQUENCE_LOCKTIME_MASK) << SEQUENCE_LOCKTIME_GRANULARITY;
103
+ } else {
104
+ lock_value = sequence & SEQUENCE_LOCKTIME_MASK;
105
+ }
106
+ return 0;
107
+ }
108
+
109
+ int Interface::getScriptScriptSig(std::vector<unsigned char> &script_sig,
110
+ const std::vector<unsigned char> &script) const
111
+ {
112
+ script_sig.clear();
113
+ return 0;
114
+ }
115
+
116
+ int Interface::decodeAddress(int &address_type, std::vector<unsigned char> &address_data,
117
+ const std::string &address) const
118
+ {
119
+ address_data.resize(0);
120
+ if (DecodeBase58Check(address, address_data, 256)) {
121
+ int prefix_type{0}, prefix_len{0};
122
+ if (0 != m_chain_params->getAddressPrefixType(prefix_type, prefix_len, address_data) ||
123
+ prefix_type != Base58Type::PUBKEY_ADDRESS) {
124
+ LOGE("Unknown address prefix type.");
125
+ return 1;
126
+ }
127
+ address_data.erase(address_data.begin(), address_data.begin() + prefix_len);
128
+ address_type = AddressTypes::P2PKH;
129
+ return 0;
130
+ }
131
+
132
+ address_data.resize(0);
133
+ const auto dec = bech32::Decode(address);
134
+ if (dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) {
135
+ if (dec.hrp != m_chain_params->m_bech32_hrp) {
136
+ LOGE("Bech32 hrp mismatch: %s", dec.hrp.c_str());
137
+ return 1;
138
+ }
139
+ if (dec.data.empty()) {
140
+ LOGE("Failed to decode bech32 address");
141
+ return 1;
142
+ }
143
+ int version =
144
+ dec.data[0]; // The first 5 bit symbol is the witness version (0-16)address_data
145
+ address_data.reserve(1 + ((dec.data.size() - 1) * 5) / 8);
146
+ address_data.push_back(version);
147
+
148
+ if (!bech32::ConvertBits<5, 8, false>([&](unsigned char c) { address_data.push_back(c); },
149
+ dec.data.begin() + 1, dec.data.end())) {
150
+ LOGE("ConvertBits failed");
151
+ return 1;
152
+ }
153
+ } else {
154
+ LOGE("Failed to decode bech32 address");
155
+ return 1;
156
+ }
157
+ return 0;
158
+ }
159
+
160
+ int Interface::encodeSegwitAddress(std::string &address, int version,
161
+ const std::vector<unsigned char> &program) const
162
+ {
163
+ if (version < 0 || version > 16) {
164
+ return 1;
165
+ }
166
+ std::vector<unsigned char> data{static_cast<unsigned char>(version)};
167
+ bech32::ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, program.begin(),
168
+ program.end());
169
+ address = bech32::Encode(version == 0 ? bech32::Encoding::BECH32 : bech32::Encoding::BECH32M,
170
+ m_chain_params->m_bech32_hrp, data);
171
+ return address.empty() ? 1 : 0;
172
+ }
173
+
174
+ int Interface::findTxSpendableOutput(int &out_n, const std::vector<unsigned char> &tx_data)
175
+ {
176
+ auto tx = getTransaction();
177
+ if (tx->from_bytes(tx_data.data(), tx_data.size(), /* allow_witness */ true) <= 0) {
178
+ LOGE("findTxSpendableOutput from_bytes failed");
179
+ return 1;
180
+ }
181
+ for (size_t n = 0; n < tx->m_vout.size(); ++n) {
182
+ const auto *script = tx->m_vout[n]->script();
183
+ if (script && !script->empty() && (*script)[0] != OP_RETURN) {
184
+ out_n = (int)n;
185
+ return 0;
186
+ }
187
+ }
188
+ return 1;
189
+ }
190
+
191
+ // OP_RETURN, push "XBSW", push the 32 byte keyshare - the layout
192
+ // createALockRefundSwipeTx writes.
193
+ int Interface::getMercyPrevout(int &n, const std::vector<unsigned char> &swipe_tx_data)
194
+ {
195
+ // The payout is the only spendable output; a swipe from before the keyshare
196
+ // moved into a tx of its own also carries an OP_RETURN.
197
+ return findTxSpendableOutput(n, swipe_tx_data);
198
+ }
199
+
200
+ int Interface::getMercyWatchVouts(std::vector<int> &vouts,
201
+ const std::vector<unsigned char> &swipe_tx_data)
202
+ {
203
+ int n{-1};
204
+ if (0 != getMercyPrevout(n, swipe_tx_data)) {
205
+ return 1;
206
+ }
207
+ vouts.assign(1, n);
208
+ return 0;
209
+ }
210
+
211
+ int Interface::createMercyTx(std::vector<unsigned char> &out,
212
+ const std::vector<unsigned char> &swipe_tx_data,
213
+ std::span<const unsigned char> keyshare,
214
+ const std::vector<unsigned char> &script_to,
215
+ const std::span<const unsigned char> key, int64_t fee_rate,
216
+ std::string &str_error)
217
+ {
218
+ if (!keyshare.empty() && keyshare.size() != 32) {
219
+ str_error = "Invalid keyshare size";
220
+ return 1;
221
+ }
222
+ auto tx_swipe = getTransaction();
223
+ if (0 >= tx_swipe->from_bytes(swipe_tx_data.data(), swipe_tx_data.size(),
224
+ /* allow_witness */ true)) {
225
+ str_error = "swipe tx decode failed";
226
+ return 1;
227
+ }
228
+ int prevout_n{-1};
229
+ if (0 != getMercyPrevout(prevout_n, swipe_tx_data)) {
230
+ str_error = "No spendable output in the swipe tx";
231
+ return 1;
232
+ }
233
+ const int64_t prevout_value = tx_swipe->m_vout[prevout_n]->value();
234
+ const std::vector<unsigned char> prevout_script = *tx_swipe->m_vout[prevout_n]->script();
235
+
236
+ std::vector<unsigned char> txid_swipe(32);
237
+ if (0 != tx_swipe->getTxid(txid_swipe.data())) {
238
+ str_error = "Failed to get swipe txid";
239
+ return 1;
240
+ }
241
+
242
+ auto tx = getTransaction();
243
+ tx->setVersion(txVersion());
244
+ auto txin = tx->makeInput();
245
+ if (0 != txin->setPrevout(txid_swipe, prevout_n)) {
246
+ str_error = "setPrevout failed";
247
+ return 1;
248
+ }
249
+ tx->m_vin.push_back(std::move(txin));
250
+
251
+ if (!keyshare.empty()) {
252
+ auto txout_keyshare = tx->makeOutput();
253
+ txout_keyshare->setValue(0);
254
+ auto &keyshare_script = *txout_keyshare->script();
255
+ keyshare_script.clear();
256
+ keyshare_script.emplace_back(OP_RETURN);
257
+ push_script_data(keyshare_script, (const unsigned char *)"XBSW", 4);
258
+ push_script_data(keyshare_script, keyshare.data(), 32);
259
+ tx->m_vout.push_back(std::move(txout_keyshare));
260
+ }
261
+
262
+ auto txout_pay = tx->makeOutput();
263
+ txout_pay->setValue(0);
264
+ *txout_pay->script() = script_to;
265
+ tx->m_vout.push_back(std::move(txout_pay));
266
+
267
+ std::vector<std::vector<unsigned char>> dummy_witness_stack;
268
+ getP2WPKHDummyWitness(dummy_witness_stack);
269
+ size_t witness_bytes = getWitnessStackSerialisedLength(dummy_witness_stack);
270
+ size_t vsize = getTxVSize(tx.get(), 0, witness_bytes);
271
+ int64_t pay_fee = feeForVSize(fee_rate, vsize);
272
+ int64_t pay_value = prevout_value - pay_fee;
273
+ if (pay_value <= dustThreshold()) {
274
+ str_error = "Swipe output too small to send a mercy tx";
275
+ return 1;
276
+ }
277
+ tx->m_vout.back()->setValue(pay_value);
278
+
279
+ LOGD("createMercyTx fee_rate, vsize, fee: %" PRId64 ", %zu, %" PRId64, fee_rate, vsize,
280
+ pay_fee);
281
+
282
+ std::vector<unsigned char> tx_bytes;
283
+ if (0 >= tx->to_bytes(tx_bytes, false, false)) {
284
+ str_error = "mercy tx encode failed";
285
+ return 1;
286
+ }
287
+ // The swipe paid a key the module derived, so it signs the input itself
288
+ // rather than handing a psbt to a host that does not own it.
289
+ std::vector<unsigned char> script_code;
290
+ if (0 != getSignatureScript(script_code, prevout_script)) {
291
+ str_error = "getSignatureScript failed";
292
+ return 1;
293
+ }
294
+ SignatureHashes hash_cache;
295
+ std::vector<unsigned char> sig, pubkey;
296
+ if (0 != getTxSignature(sig, hash_cache, key, tx_bytes, 0, script_code, prevout_value)) {
297
+ str_error = "getTxSignature failed";
298
+ return 1;
299
+ }
300
+ if (0 != getPubkey(pubkey, key)) {
301
+ str_error = "getPubkey failed";
302
+ return 1;
303
+ }
304
+ if (0 != tx->m_vin[0]->setWitnessData({sig, pubkey})) {
305
+ str_error = "setWitnessData failed";
306
+ return 1;
307
+ }
308
+ if (0 >= tx->to_bytes(out, false, true)) {
309
+ str_error = "mercy tx encode failed";
310
+ return 1;
311
+ }
312
+ return 0;
313
+ }
314
+
315
+ int Interface::getMercyKeyshare(std::vector<unsigned char> &keyshare,
316
+ const std::vector<unsigned char> &tx_data)
317
+ {
318
+ const unsigned char tag[6] = {OP_RETURN, 0x04, 'X', 'B', 'S', 'W'};
319
+ auto tx = getTransaction();
320
+ if (tx->from_bytes(tx_data.data(), tx_data.size(), /* allow_witness */ true) <= 0) {
321
+ LOGE("getMercyKeyshare from_bytes failed");
322
+ return 1;
323
+ }
324
+ for (const auto &txout : tx->m_vout) {
325
+ const auto *script = txout->script();
326
+ if (!script || script->size() != sizeof(tag) + 1 + 32 ||
327
+ 0 != memcmp(script->data(), tag, sizeof(tag)) || (*script)[sizeof(tag)] != 0x20) {
328
+ continue;
329
+ }
330
+ keyshare.assign(script->begin() + sizeof(tag) + 1, script->end());
331
+ std::vector<unsigned char> pubkey;
332
+ if (0 != getPubkey(pubkey, keyshare)) {
333
+ LOGW("getMercyKeyshare: invalid keyshare");
334
+ keyshare.clear();
335
+ continue;
336
+ }
337
+ return 0;
338
+ }
339
+ return 1;
340
+ }
341
+
342
+ int Interface::getScriptForAddress(std::vector<unsigned char> &script_dest,
343
+ const std::string &address) const
344
+ {
345
+ std::vector<unsigned char> address_data;
346
+ int address_type{0};
347
+ int rv = decodeAddress(address_type, address_data, address);
348
+ if (rv != 0) {
349
+ return rv;
350
+ }
351
+
352
+ if (address_type == AddressTypes::P2PKH) {
353
+ if (0 != make_script_P2PKH(script_dest, address_data)) {
354
+ LOGE("Unexpected address data size for P2PKH %zu", address_data.size());
355
+ return 1;
356
+ }
357
+ return 0;
358
+ }
359
+ if (address_data.size() == 21 && address_data[0] == 0) {
360
+ script_dest.resize(22);
361
+ script_dest[0] = 0;
362
+ script_dest[1] = 20;
363
+ memcpy(script_dest.data() + 2, address_data.data() + 1, 20);
364
+ } else if (address_data.size() == 33 && address_data[0] == 0) {
365
+ script_dest.resize(34);
366
+ script_dest[0] = 0;
367
+ script_dest[1] = 32;
368
+ memcpy(script_dest.data() + 2, address_data.data() + 1, 32);
369
+ } else {
370
+ LOGE("Unknown address type");
371
+ return 1;
372
+ }
373
+
374
+ return 0;
375
+ }
376
+
377
+ int Interface::setInputSignature(btc_base::Input &txin, std::vector<unsigned char> &&sig,
378
+ const std::vector<unsigned char> &pubkey) const
379
+ {
380
+ return txin.setWitnessData({std::move(sig), pubkey});
381
+ }
382
+
383
+ int Interface::getAddressForScript(std::string &address,
384
+ const std::vector<unsigned char> &script) const
385
+ {
386
+ std::vector<unsigned char> pkh;
387
+ if (0 == get_P2PKH_pubkey_hash(pkh, script)) {
388
+ std::vector<unsigned char> data;
389
+ if (0 != m_chain_params->getBase58Prefix(data, Base58Type::PUBKEY_ADDRESS)) {
390
+ return 1;
391
+ }
392
+ data.insert(data.end(), pkh.begin(), pkh.end());
393
+ address = EncodeBase58Check(data);
394
+ return address.empty() ? 1 : 0;
395
+ }
396
+ if ((script.size() == 22 || script.size() == 34) && script[0] == OP_0 &&
397
+ script[1] == script.size() - 2) {
398
+ return encodeSegwitAddress(address, 0,
399
+ std::vector<unsigned char>(script.begin() + 2, script.end()));
400
+ }
401
+ return 1;
402
+ }
403
+
404
+ int Interface::getScriptDest(std::vector<unsigned char> &script_dest,
405
+ const std::vector<unsigned char> &script) const
406
+ {
407
+ script_dest.resize(34);
408
+ script_dest[0] = OP_0;
409
+ script_dest[1] = 32;
410
+ sha256(script_dest.data() + 2, script.data(), script.size());
411
+ return 0;
412
+ }
413
+
414
+ int Interface::getScriptForPubkeyHash(std::vector<unsigned char> &script_dest,
415
+ const std::vector<unsigned char> &pkh) const
416
+ {
417
+ if (pkh.size() != 20) {
418
+ LOGE("getScriptForPubkeyHash invalid pubkey hash size");
419
+ script_dest.clear();
420
+ return 1;
421
+ }
422
+ script_dest.resize(22);
423
+ script_dest[0] = OP_0;
424
+ script_dest[1] = 20;
425
+ memcpy(script_dest.data() + 2, pkh.data(), pkh.size());
426
+ return 0;
427
+ }
428
+
429
+ int Interface::getPubkeyHashFromScript(std::vector<unsigned char> &pkh,
430
+ const std::vector<unsigned char> &script) const
431
+ {
432
+ if (script.size() != 22 || script[0] != OP_0 || script[1] != 20) {
433
+ pkh.clear();
434
+ return 1;
435
+ }
436
+ pkh.assign(script.begin() + 2, script.end());
437
+ return 0;
438
+ }
439
+
440
+ int Interface::convertScriptForThisChain(std::vector<unsigned char> &script_dest,
441
+ const std::vector<unsigned char> &script) const
442
+ {
443
+ // P2PKH and P2WPKH both commit to hash160 of the pubkey, so either form gives the other
444
+ if (is_script_P2PKH(script)) {
445
+ return getScriptForPubkeyHash(
446
+ script_dest, std::vector<unsigned char>(script.begin() + 3, script.begin() + 23));
447
+ }
448
+ if (is_script_P2WPKH(script)) {
449
+ return getScriptForPubkeyHash(
450
+ script_dest, std::vector<unsigned char>(script.begin() + 2, script.begin() + 22));
451
+ }
452
+ script_dest = script;
453
+ return 0;
454
+ }
455
+
456
+ int Interface::getScriptLockTxScript(std::vector<unsigned char> &lock_script,
457
+ const unsigned char *Kal, const unsigned char *Kaf) const
458
+ {
459
+ lock_script.clear();
460
+ lock_script.reserve(71);
461
+ push_script_num(lock_script, 2);
462
+ push_script_data(lock_script, Kal, 33);
463
+ push_script_data(lock_script, Kaf, 33);
464
+ push_script_num(lock_script, 2);
465
+ lock_script.emplace_back(OP_CHECKMULTISIG);
466
+ assert(lock_script.size() == 71);
467
+ return 0;
468
+ }
469
+
470
+ int Interface::getScriptLockRefundTxScript(std::vector<unsigned char> &refund_script,
471
+ const unsigned char *Kal, const unsigned char *Kaf,
472
+ int csv_val) const
473
+ {
474
+ refund_script.clear();
475
+ refund_script.emplace_back(OP_IF);
476
+ push_script_num(refund_script, 1);
477
+ refund_script.emplace_back(OP_CHECKSEQUENCEVERIFY);
478
+ refund_script.emplace_back(OP_DROP);
479
+ push_script_num(refund_script, 2);
480
+ push_script_data(refund_script, Kal, 33);
481
+ push_script_data(refund_script, Kaf, 33);
482
+ push_script_num(refund_script, 2);
483
+ refund_script.emplace_back(OP_CHECKMULTISIG);
484
+ refund_script.emplace_back(OP_ELSE);
485
+ push_script_num(refund_script, csv_val);
486
+ refund_script.emplace_back(OP_CHECKSEQUENCEVERIFY);
487
+ refund_script.emplace_back(OP_DROP);
488
+ push_script_data(refund_script, Kaf, 33);
489
+ refund_script.emplace_back(OP_CHECKSIG);
490
+ refund_script.emplace_back(OP_ENDIF);
491
+ return 0;
492
+ }
493
+
494
+ int Interface::getScriptLockTxDummyWitness(std::vector<std::vector<unsigned char>> &witness_stack,
495
+ const std::vector<unsigned char> &script) const
496
+ {
497
+ witness_stack.resize(4);
498
+ witness_stack[0].clear();
499
+ witness_stack[1] = std::vector<unsigned char>(72, 0);
500
+ witness_stack[2] = std::vector<unsigned char>(72, 0);
501
+ witness_stack[3] = script;
502
+ return 0;
503
+ }
504
+
505
+ int Interface::getScriptLockRefundTxDummyWitness(
506
+ std::vector<std::vector<unsigned char>> &witness_stack,
507
+ const std::vector<unsigned char> &script) const
508
+ {
509
+ witness_stack.resize(5);
510
+ witness_stack[0].clear();
511
+ witness_stack[1] = std::vector<unsigned char>(72, 0);
512
+ witness_stack[2] = std::vector<unsigned char>(72, 0);
513
+ witness_stack[3].clear();
514
+ witness_stack[3].push_back(1);
515
+ witness_stack[4] = script;
516
+ return 0;
517
+ }
518
+
519
+ int Interface::getScriptLockRefundSwipeTxDummyWitness(
520
+ std::vector<std::vector<unsigned char>> &witness_stack,
521
+ const std::vector<unsigned char> &script) const
522
+ {
523
+ witness_stack.resize(3);
524
+ witness_stack[0] = std::vector<unsigned char>(72, 0);
525
+ witness_stack[1].clear();
526
+ witness_stack[2] = script;
527
+ return 0;
528
+ }
529
+
530
+ int Interface::getP2WPKHDummyWitness(std::vector<std::vector<unsigned char>> &witness_stack) const
531
+ {
532
+ witness_stack.resize(2);
533
+ witness_stack[0] = std::vector<unsigned char>(72, 0);
534
+ witness_stack[1] = std::vector<unsigned char>(33, 0);
535
+ return 0;
536
+ }
537
+
538
+ size_t Interface::getWitnessStackSerialisedLength(
539
+ std::vector<std::vector<unsigned char>> &witness_stack) const
540
+ {
541
+ // one input
542
+ size_t rv = bytes_compactsize(witness_stack.size());
543
+ for (const auto &e : witness_stack) {
544
+ rv += bytes_compactsize(e.size()) + e.size();
545
+ }
546
+ rv++; // vin dummy
547
+ rv++; // flags
548
+ return rv;
549
+ }
550
+
551
+ size_t Interface::getWitnessStackSerialisedLength(
552
+ std::vector<std::vector<std::vector<unsigned char>>> &witness_stacks) const
553
+ {
554
+ size_t rv{0};
555
+ for (const auto &witness_stack : witness_stacks) {
556
+ rv += bytes_compactsize(witness_stack.size());
557
+ for (const auto &e : witness_stack) {
558
+ rv += bytes_compactsize(e.size()) + e.size();
559
+ }
560
+ }
561
+ rv++; // vin dummy
562
+ rv++; // flags
563
+ return rv;
564
+ }
565
+
566
+ size_t Interface::getTxVSize(const bsx::Transaction *tx, size_t add_bytes,
567
+ size_t add_witness_bytes) const
568
+ {
569
+ int wsf = witnessScaleFactor();
570
+ size_t len_full = tx->get_bytes_size(true) + add_bytes + add_witness_bytes;
571
+ size_t len_nwit = tx->get_bytes_size(false) + add_bytes;
572
+ size_t weight = len_nwit * (wsf - 1) + len_full;
573
+ return (weight + wsf - 1) / wsf;
574
+ }
575
+
576
+ int Interface::getSignatureScript(std::vector<unsigned char> &prev_script,
577
+ const std::vector<unsigned char> &script_pubkey) const
578
+ {
579
+ if (is_script_P2WPKH(script_pubkey)) {
580
+ prev_script = {OP_DUP, OP_HASH160, 20};
581
+ prev_script.insert(prev_script.end(), script_pubkey.begin() + 2,
582
+ script_pubkey.begin() + 22);
583
+ prev_script.insert(prev_script.end(), {OP_EQUALVERIFY, OP_CHECKSIG});
584
+ return 0;
585
+ }
586
+
587
+ prev_script = script_pubkey;
588
+ return 0;
589
+ }
590
+
591
+ int Interface::setBLockSpendSignature(bsx::Input &txin, const std::vector<unsigned char> &sig,
592
+ const std::vector<unsigned char> &pubkey) const
593
+ {
594
+ return txin.setWitnessData({sig, pubkey});
595
+ }
596
+
597
+ int Interface::getSignatureHash(unsigned char *out,
598
+ const std::vector<unsigned char> &prevout_script,
599
+ const bsx::Transaction *ptx, unsigned int input_n, int hash_type,
600
+ int64_t prevout_value, SignatureHashes &hashes) const
601
+ {
602
+ const Transaction *tx = dynamic_cast<const Transaction *>(ptx);
603
+ if (!tx) {
604
+ LOGE("getSignatureHash: Invalid tx class type");
605
+ return 1;
606
+ }
607
+
608
+ return SegwitV0SignatureHash(out, prevout_script, *tx, input_n, hash_type, prevout_value,
609
+ hashes);
610
+ }
611
+
612
+ int Interface::getTxSignature(std::vector<unsigned char> &sig, SignatureHashes &hash_cache,
613
+ std::span<const unsigned char> vk,
614
+ const std::vector<unsigned char> &txb, int input_n,
615
+ const std::vector<unsigned char> &prevout_script,
616
+ int64_t prevout_value) const
617
+ {
618
+ unsigned char sig_hash[32];
619
+
620
+ auto tx = getTransaction();
621
+ int rv = tx->from_bytes(txb.data(), txb.size(), false);
622
+ if (rv < 1) {
623
+ LOGE("verify_tx: Failed to load tx %d", rv);
624
+ return 1;
625
+ }
626
+ if (vk.size() != 32) {
627
+ LOGE("get_tx_signature: Invalid key size %zu", vk.size());
628
+ return 3;
629
+ }
630
+ rv = getSignatureHash(sig_hash, prevout_script, tx.get(), input_n, SIGHASH_ALL, prevout_value,
631
+ hash_cache);
632
+ if (rv != 0) {
633
+ LOGE("get_tx_signature: getSignatureHash failed %d", rv);
634
+ return 4;
635
+ }
636
+ rv = m_context->signECDSA(sig, sig_hash, vk.data());
637
+ if (rv != 0) {
638
+ LOGE("get_tx_signature: signECDSA failed %d", rv);
639
+ return 5;
640
+ }
641
+ size_t siglen = sig.size();
642
+ sig.resize(siglen + 1);
643
+ sig[siglen] = SIGHASH_ALL;
644
+ return 0;
645
+ }
646
+
647
+ int Interface::verifyTxSignature(const std::vector<unsigned char> &txb,
648
+ const std::vector<unsigned char> &sig, const unsigned char *K,
649
+ int input_n, const std::vector<unsigned char> &prevout_script,
650
+ int64_t prevout_value) const
651
+ {
652
+ auto tx = getTransaction();
653
+ int rv = tx->from_bytes(txb.data(), txb.size(), false);
654
+ if (rv < 1) {
655
+ LOGE("verifyTxSignature: Failed to load tx %d", rv);
656
+ return 1;
657
+ }
658
+ if (sig.size() < 30) { // MIN_SIGNATURE_SIZE?
659
+ LOGE("verifyTxSignature: signature size too short");
660
+ return 2;
661
+ }
662
+ SignatureHashes hash_cache;
663
+ unsigned char sig_hash[32];
664
+ int hash_type = sig[sig.size() - 1];
665
+ if (hash_type != SIGHASH_ALL) {
666
+ LOGE("verifyTxSignature: unexpected hash type %d", hash_type);
667
+ return 4;
668
+ }
669
+ rv = getSignatureHash(sig_hash, prevout_script, tx.get(), input_n, hash_type, prevout_value,
670
+ hash_cache);
671
+ if (rv != 0) {
672
+ LOGE("get_tx_signature: getSignatureHash failed %d", rv);
673
+ return 3;
674
+ }
675
+ return m_context->verifyECDSA(sig_hash, K, sig.data(), sig.size() - 1);
676
+ }
677
+
678
+ int Interface::signTxOtVES(std::vector<unsigned char> &ct, const std::vector<unsigned char> &txb,
679
+ const unsigned char *ks, const unsigned char *Ke, int input_n,
680
+ const std::vector<unsigned char> &prevout_script,
681
+ int64_t prevout_value) const
682
+ {
683
+ auto tx = getTransaction();
684
+ int rv = tx->from_bytes(txb.data(), txb.size(), false);
685
+ if (rv < 1) {
686
+ LOGE("verifyTxOtVES: Failed to load tx %d", rv);
687
+ return 1;
688
+ }
689
+ SignatureHashes hash_cache;
690
+ unsigned char sig_hash[32];
691
+ rv = getSignatureHash(sig_hash, prevout_script, tx.get(), input_n, SIGHASH_ALL, prevout_value,
692
+ hash_cache);
693
+ if (rv != 0) {
694
+ LOGE("verifyTxOtVES: SegwitV0SignatureHash failed %d", rv);
695
+ return 3;
696
+ }
697
+ return m_context->signTxOtVES(ct, sig_hash, ks, Ke);
698
+ }
699
+
700
+ int Interface::verifyTxOtVES(const std::vector<unsigned char> &txb,
701
+ const std::vector<unsigned char> &ct, const unsigned char *Ks,
702
+ const unsigned char *Ke, int input_n,
703
+ const std::vector<unsigned char> &prevout_script,
704
+ int64_t prevout_value) const
705
+ {
706
+ auto tx = getTransaction();
707
+ int rv = tx->from_bytes(txb.data(), txb.size(), false);
708
+ if (rv < 1) {
709
+ LOGE("verifyTxOtVES: Failed to load tx %d", rv);
710
+ return 1;
711
+ }
712
+ SignatureHashes hash_cache;
713
+ unsigned char sig_hash[32];
714
+ rv = getSignatureHash(sig_hash, prevout_script, tx.get(), input_n, SIGHASH_ALL, prevout_value,
715
+ hash_cache);
716
+ if (rv != 0) {
717
+ LOGE("verifyTxOtVES: getSignatureHash failed %d", rv);
718
+ return 3;
719
+ }
720
+ return m_context->verifyTxOtVES(sig_hash, Ks, Ke, ct);
721
+ }
722
+
723
+ int Interface::decryptOtVES(std::vector<unsigned char> &sig, const unsigned char *ke,
724
+ const std::vector<unsigned char> &ct) const
725
+ {
726
+ return m_context->decryptOtVES(sig, ke, ct);
727
+ }
728
+
729
+ int Interface::recoverEncKey(secure_bytes &ke, const std::vector<unsigned char> &ct,
730
+ const std::vector<unsigned char> &sig, const unsigned char *Ke) const
731
+ {
732
+ return m_context->recoverEncKey(ke, ct, sig, Ke);
733
+ }
734
+
735
+ static void keyProofHash(unsigned char *hash, std::string_view message,
736
+ std::span<const unsigned char> pubkey,
737
+ std::span<const unsigned char> context)
738
+ {
739
+ std::vector<unsigned char> preimage(message.begin(), message.end());
740
+ preimage.insert(preimage.end(), pubkey.begin(), pubkey.end());
741
+ preimage.insert(preimage.end(), context.begin(), context.end());
742
+ sha256(hash, preimage.data(), preimage.size());
743
+ }
744
+
745
+ int Interface::signKeyProof(std::vector<unsigned char> &proof, std::span<const unsigned char> key,
746
+ std::string_view message, std::span<const unsigned char> context)
747
+ {
748
+ std::vector<unsigned char> pubkey, sig;
749
+ if (key.size() != 32 || 0 != m_context->getPubkey(pubkey, key)) {
750
+ return 1;
751
+ }
752
+ unsigned char hash[32];
753
+ keyProofHash(hash, message, pubkey, context);
754
+ if (0 != m_context->signCompact(sig, hash, key.data())) {
755
+ return 2;
756
+ }
757
+ proof = pubkey;
758
+ proof.insert(proof.end(), sig.begin(), sig.end());
759
+ return 0;
760
+ }
761
+
762
+ int Interface::verifyKeyProof(std::vector<unsigned char> &pubkey,
763
+ const std::vector<unsigned char> &proof, std::string_view message,
764
+ std::span<const unsigned char> context)
765
+ {
766
+ if (proof.size() != 33 + 64) {
767
+ return 1;
768
+ }
769
+ unsigned char hash[32];
770
+ keyProofHash(hash, message, std::span(proof.data(), 33), context);
771
+ if (0 != m_context->verifyCompact(hash, proof.data(), proof.data() + 33)) {
772
+ return 2;
773
+ }
774
+ pubkey.assign(proof.begin(), proof.begin() + 33);
775
+ return 0;
776
+ }
777
+
778
+ int Interface::getTxVout(const Transaction &tx, const std::vector<unsigned char> &script_dest)
779
+ {
780
+ for (size_t k = 0; k < tx.m_vout.size(); k++) {
781
+ const auto vout = dynamic_cast<Output *>(tx.m_vout[k].get());
782
+ if (vout->m_script == script_dest) {
783
+ return k;
784
+ }
785
+ }
786
+ return -1;
787
+ }
788
+
789
+ int Interface::getTxVout(const bsx::Transaction *tx, const std::vector<unsigned char> &script_dest)
790
+ {
791
+ for (size_t k = 0; k < tx->m_vout.size(); k++) {
792
+ if (tx->m_vout[k]->matchScript(script_dest)) {
793
+ return k;
794
+ }
795
+ }
796
+ return -1;
797
+ }
798
+
799
+ int Interface::getLockTxOutpoint(bsx::OutPoint *prevout, const SwapInfo &swap_info,
800
+ const bsx::Transaction *tx_lock, std::string &str_error)
801
+ {
802
+ if (0 != tx_lock->getTxid(prevout->txid_data(), /* le */ true)) {
803
+ str_error = "Failed to get lock txid";
804
+ return 1;
805
+ }
806
+ std::vector<unsigned char> lock_script, lock_script_dest;
807
+ if (0 != getScriptLockTxScript(lock_script, swap_info.m_Kal.data(), swap_info.m_Kaf.data())) {
808
+ str_error = "getScriptLockTxScript failed";
809
+ return 1;
810
+ }
811
+ if (0 != getScriptDest(lock_script_dest, lock_script)) {
812
+ str_error = "getScriptDest failed";
813
+ return 1;
814
+ }
815
+ int prevout_n = getTxVout(tx_lock, lock_script_dest);
816
+ if (prevout_n < 0) {
817
+ str_error = "Lock output not found";
818
+ return 1;
819
+ }
820
+ prevout->setVout(prevout_n);
821
+
822
+ return 0;
823
+ }
824
+
825
+ /* Return negative int if in error, else number of sigs added
826
+ */
827
+ int Interface::signTx(std::vector<unsigned char> &txb, std::map<int, SwapInput> &mprevouts,
828
+ const std::vector<std::span<const unsigned char>> &vkeys)
829
+ {
830
+ int rv, num_added{0};
831
+ auto ptx = getTransaction();
832
+ auto &tx = static_cast<Transaction &>(*ptx);
833
+ rv = tx.from_bytes(txb.data(), txb.size(), false);
834
+ if (rv < 1 || (size_t)rv != txb.size()) {
835
+ LOGE("sign_tx: Failed to load tx %d", rv);
836
+ return -1;
837
+ }
838
+
839
+ std::vector<std::vector<unsigned char>> pubkeys, key_hashes;
840
+ for (const auto &vkey : vkeys) {
841
+ if (vkey.size() != 32) {
842
+ LOGE("sign_tx: unexpected key length.");
843
+ return -1;
844
+ }
845
+ std::vector<unsigned char> pubkey, key_hash;
846
+ if (0 != m_context->getPubkey(pubkey, vkey)) {
847
+ LOGE("sign_tx: Failed to get pubkey.");
848
+ return -1;
849
+ }
850
+ key_hash.resize(20);
851
+ if (0 != get_pubkeyid(key_hash.data(), pubkey.data())) {
852
+ LOGE("sign_tx: Failed to get keyid.");
853
+ return -1;
854
+ }
855
+ pubkeys.emplace_back(std::move(pubkey));
856
+ key_hashes.emplace_back(std::move(key_hash));
857
+ }
858
+ SignatureHashes hash_cache;
859
+
860
+ // for (const auto &txin : tx.m_vin) {
861
+ for (size_t input_n = 0; input_n < tx.m_vin.size(); input_n++) {
862
+ auto txin = dynamic_cast<Input *>(tx.m_vin[input_n].get());
863
+ const auto it = mprevouts.find(input_n);
864
+ if (it == mprevouts.end()) {
865
+ continue;
866
+ }
867
+ const auto &txout = *static_pointer_cast<btc_base::Output>(it->second.m_output);
868
+ /*
869
+ const auto it = mprevouts.find(txin.m_prevout);
870
+ if (it == mprevouts.end()) {
871
+ continue;
872
+ }
873
+ const auto &txout = it->second;
874
+ */
875
+ std::vector<std::vector<unsigned char>> found_key_ids;
876
+ if (extract_signing_key_ids(found_key_ids, txout.m_script) < 1) {
877
+ continue;
878
+ }
879
+ for (size_t i = 0; i < key_hashes.size(); i++) {
880
+ for (const auto &keyid : found_key_ids) {
881
+ if (keyid.size() != key_hashes[i].size()) {
882
+ continue;
883
+ }
884
+
885
+ if (0 != memcmp(keyid.data(), key_hashes[i].data(), key_hashes[i].size())) {
886
+ continue;
887
+ }
888
+ std::vector<unsigned char> sig, prev_script;
889
+ if (0 != getSignatureScript(prev_script, txout.m_script)) {
890
+ LOGE("sign_tx: get_signature_script failed.");
891
+ return -2;
892
+ }
893
+ if (0 != getTxSignature(sig, hash_cache, vkeys[i], txb, input_n, prev_script,
894
+ txout.m_value)) {
895
+ LOGE("sign_tx: get_tx_signature failed.");
896
+ return -3;
897
+ }
898
+
899
+ if (0 != setInputSignature(*txin, std::move(sig), pubkeys[i])) {
900
+ LOGE("sign_tx: setInputSignature failed.");
901
+ return -3;
902
+ }
903
+ num_added++;
904
+ break;
905
+ }
906
+ }
907
+ }
908
+
909
+ if (num_added > 0) {
910
+ rv = tx.to_bytes(txb);
911
+ if (rv < 1) {
912
+ LOGE("sign_tx: tx.to_bytes failed.");
913
+ return -3;
914
+ }
915
+ }
916
+
917
+ return num_added;
918
+ }
919
+
920
+ int Interface::verifySCLockTx(SwapInfo &swap_info, std::string &str_error)
921
+ {
922
+ LOGD("verifySCLockTx");
923
+ auto tx_lock = getTransaction();
924
+ int rv = tx_lock->from_bytes(swap_info.m_lock_tx_a.data(), swap_info.m_lock_tx_a.size());
925
+ if (rv <= 0) {
926
+ str_error = "tx decode failed";
927
+ return 1;
928
+ }
929
+
930
+ if (swap_info.m_Kal.size() != 33) {
931
+ str_error = "invalid Kal size";
932
+ return 1;
933
+ }
934
+ if (swap_info.m_Kaf.size() != 33) {
935
+ str_error = "invalid Kaf size";
936
+ return 1;
937
+ }
938
+ std::vector<unsigned char> lock_script_dest;
939
+ rv = getScriptLockTxScript(swap_info.m_lock_tx_a_script, swap_info.m_Kal.data(),
940
+ swap_info.m_Kaf.data());
941
+
942
+ if (rv != 0) {
943
+ str_error = "getScriptLockTxScript failed";
944
+ return 1;
945
+ }
946
+ rv = getScriptDest(lock_script_dest, swap_info.m_lock_tx_a_script);
947
+ if (rv != 0) {
948
+ str_error = "getScriptDest failed";
949
+ return 1;
950
+ }
951
+ if (tx_lock->version() != txVersion()) {
952
+ str_error = "Bad tx version";
953
+ return 1;
954
+ }
955
+ if (tx_lock->locktime() != 0) {
956
+ int chain_height = getChainHeight();
957
+ if (chain_height < 0) {
958
+ str_error = "Unable to get current chain height";
959
+ return 1;
960
+ }
961
+ if (tx_lock->locktime() > (uint32_t)chain_height + 6) {
962
+ str_error = "Bad locktime";
963
+ return 1;
964
+ }
965
+ }
966
+
967
+ swap_info.m_lock_a_txo_n = getTxVout(tx_lock.get(), lock_script_dest);
968
+ if (swap_info.m_lock_a_txo_n < 0) {
969
+ str_error = "Lock output not found";
970
+ return 1;
971
+ }
972
+
973
+ if (tx_lock->m_vout[swap_info.m_lock_a_txo_n]->value() != swap_info.m_amount_from) {
974
+ str_error =
975
+ strprintf("Incorrect lock output value %d, %d",
976
+ tx_lock->m_vout[swap_info.m_lock_a_txo_n]->value(), swap_info.m_amount_from);
977
+ return 1;
978
+ }
979
+
980
+ return 0;
981
+ }
982
+
983
+ int Interface::verifySCLockTxOutputs(SwapInfo &swap_info,
984
+ const std::vector<SwapFeeOutput> &expected_fee_outputs,
985
+ const std::vector<SwapInput> &request_outputs,
986
+ int64_t lock_fee_rate, std::string &str_error)
987
+ {
988
+ LOGD("verifySCLockTxOutputs");
989
+ auto tx_lock = getTransaction();
990
+ int rv = tx_lock->from_bytes(swap_info.m_lock_tx_a.data(), swap_info.m_lock_tx_a.size());
991
+ if (rv <= 0) {
992
+ str_error = "tx decode failed";
993
+ return 1;
994
+ }
995
+
996
+ if (swap_info.m_lock_a_txo_n < 0) {
997
+ str_error = "Lock output not set";
998
+ return 1;
999
+ }
1000
+ if (swap_info.m_change_script.size() < 1) {
1001
+ str_error = "No change script";
1002
+ return 1;
1003
+ }
1004
+
1005
+ std::vector<bool> fee_matched(expected_fee_outputs.size(), false);
1006
+ size_t num_change_outputs{0};
1007
+ for (size_t n = 0; n < tx_lock->m_vout.size(); n++) {
1008
+ if ((int)n == swap_info.m_lock_a_txo_n) {
1009
+ // Script and value already checked by verifySCLockTx
1010
+ continue;
1011
+ }
1012
+ const auto &txout = tx_lock->m_vout[n];
1013
+ if (!txout->script()) {
1014
+ str_error = strprintf("Lock tx a output %d has no script", n);
1015
+ return 1;
1016
+ }
1017
+ if (*txout->script() == swap_info.m_change_script) {
1018
+ // Value is unconstrained, it returns to this node
1019
+ num_change_outputs++;
1020
+ continue;
1021
+ }
1022
+
1023
+ bool found{false};
1024
+ for (size_t i = 0; i < expected_fee_outputs.size(); i++) {
1025
+ if (fee_matched[i]) {
1026
+ continue;
1027
+ }
1028
+ if (*txout->script() == expected_fee_outputs[i].m_script &&
1029
+ txout->value() == expected_fee_outputs[i].m_value) {
1030
+ fee_matched[i] = true;
1031
+ found = true;
1032
+ break;
1033
+ }
1034
+ }
1035
+ if (!found) {
1036
+ str_error =
1037
+ strprintf("Unexpected lock tx a output %d, value %" PRId64, n, txout->value());
1038
+ return 1;
1039
+ }
1040
+ }
1041
+
1042
+ for (size_t i = 0; i < expected_fee_outputs.size(); i++) {
1043
+ if (!fee_matched[i]) {
1044
+ str_error = strprintf("Missing fee output %d, value %" PRId64, i,
1045
+ expected_fee_outputs[i].m_value);
1046
+ return 1;
1047
+ }
1048
+ }
1049
+ if (num_change_outputs > 1) {
1050
+ str_error = strprintf("Too many change outputs %d", num_change_outputs);
1051
+ return 1;
1052
+ }
1053
+
1054
+ // Whatever isn't in an output goes to miners. Bounding it stops the
1055
+ // counterparty shrinking the change output, or omitting it entirely, and
1056
+ // burning this client's value: an absent change output is only acceptable
1057
+ // when it would have held dust.
1058
+ int64_t total_out{0};
1059
+ const int64_t max_money = MaxMoney();
1060
+ for (const auto &txout : tx_lock->m_vout) {
1061
+ const int64_t value = txout->value();
1062
+ if (value < 0 || value > max_money - total_out) {
1063
+ str_error = strprintf("Lock tx a output value %" PRId64 " out of range", value);
1064
+ return 1;
1065
+ }
1066
+ total_out += value;
1067
+ }
1068
+
1069
+ // The swap request stores every output offered for funding, only those the
1070
+ // tx actually spends count towards the input value
1071
+ int64_t total_in{0};
1072
+ for (const auto &txin : tx_lock->m_vin) {
1073
+ bool found{false};
1074
+ for (const auto &request_output : request_outputs) {
1075
+ if (txin->matchPrevout(request_output.m_outpoint.get())) {
1076
+ total_in += request_output.m_output->value();
1077
+ found = true;
1078
+ break;
1079
+ }
1080
+ }
1081
+ if (!found) {
1082
+ str_error = "Lock tx a input not found in swap request outputs";
1083
+ return 1;
1084
+ }
1085
+ }
1086
+ if (total_out > total_in) {
1087
+ str_error =
1088
+ strprintf("Lock tx a outputs %" PRId64 " exceed inputs %" PRId64, total_out, total_in);
1089
+ return 1;
1090
+ }
1091
+
1092
+ std::vector<std::vector<std::vector<unsigned char>>> dummy_witness_stacks;
1093
+ for (size_t i = 0; i < tx_lock->m_vin.size(); i++) {
1094
+ std::vector<std::vector<unsigned char>> witness_stack;
1095
+ getP2WPKHDummyWitness(witness_stack);
1096
+ dummy_witness_stacks.push_back(std::move(witness_stack));
1097
+ }
1098
+ size_t witness_bytes = getWitnessStackSerialisedLength(dummy_witness_stacks);
1099
+ size_t vsize = getTxVSize(tx_lock.get(), 0, witness_bytes);
1100
+ int64_t expect_fee = feeForVSize(lock_fee_rate, vsize);
1101
+
1102
+ // Value the counterparty may leave to miners above the expected network
1103
+ // fee. Covers the vsize estimate differing by a few bytes from theirs, and
1104
+ // lets them drop a change output that would only be dust, with its fee.
1105
+ int64_t implicit_fee = total_in - total_out;
1106
+ int64_t allowance = dustThreshold();
1107
+ if (num_change_outputs == 0) {
1108
+ auto change_out = tx_lock->makeOutput();
1109
+ *change_out->script() = swap_info.m_change_script;
1110
+ allowance += feeForVSize(lock_fee_rate, change_out->get_bytes_size());
1111
+ }
1112
+ if (implicit_fee > expect_fee + allowance) {
1113
+ str_error = strprintf("Lock tx a leaves %" PRId64 " to miners, expected %" PRId64,
1114
+ implicit_fee, expect_fee);
1115
+ return 1;
1116
+ }
1117
+
1118
+ int64_t min_fee = feeForVSize(minFeeRate(), vsize);
1119
+ if (implicit_fee < min_fee) {
1120
+ str_error = strprintf("Lock tx a leaves %" PRId64 " to miners, minimum %" PRId64,
1121
+ implicit_fee, min_fee);
1122
+ return 1;
1123
+ }
1124
+
1125
+ return 0;
1126
+ }
1127
+
1128
+ int Interface::verifySCLockTxInputs(SwapInfo &swap_info,
1129
+ const std::vector<unsigned char> &utxo_data,
1130
+ std::string &str_error)
1131
+ {
1132
+ LOGD("verifySCLockTxInputs");
1133
+ auto tx_lock = getTransaction();
1134
+ int rv = tx_lock->from_bytes(swap_info.m_lock_tx_a.data(), swap_info.m_lock_tx_a.size());
1135
+ if (rv <= 0) {
1136
+ str_error = "tx decode failed";
1137
+ return 1;
1138
+ }
1139
+
1140
+ for (const auto &txin : tx_lock->m_vin) {
1141
+ // BIP68: a relative lock on an input keeps the tx out of every mempool
1142
+ // until it matures, and this side is about to sign it.
1143
+ if (!(txin->sequence() & (1u << 31))) {
1144
+ str_error = "Lock tx input enables a relative timelock";
1145
+ return 1;
1146
+ }
1147
+ }
1148
+
1149
+ const size_t outpoint_size = getOutpoint()->get_bytes_size();
1150
+ if (utxo_data.size() < outpoint_size || utxo_data.size() % outpoint_size != 0) {
1151
+ str_error = "Invalid utxo data size";
1152
+ return 1;
1153
+ }
1154
+
1155
+ const size_t num_utxos = utxo_data.size() / outpoint_size;
1156
+ std::vector<std::shared_ptr<bsx::OutPoint>> utxos;
1157
+ utxos.reserve(num_utxos);
1158
+ for (size_t i = 0; i < num_utxos; ++i) {
1159
+ auto outpoint = getOutpoint();
1160
+ if ((int)outpoint_size !=
1161
+ outpoint->from_bytes(utxo_data.data() + i * outpoint_size, outpoint_size)) {
1162
+ str_error = "utxo decode failed";
1163
+ return 1;
1164
+ }
1165
+ utxos.push_back(std::move(outpoint));
1166
+ }
1167
+
1168
+ if (tx_lock->m_vin.size() != num_utxos) {
1169
+ str_error = strprintf("Input count mismatch %d, %d", tx_lock->m_vin.size(), num_utxos);
1170
+ return 1;
1171
+ }
1172
+
1173
+ std::vector<bool> utxo_matched(num_utxos, false);
1174
+ for (const auto &txin : tx_lock->m_vin) {
1175
+ bool found = false;
1176
+ for (size_t i = 0; i < num_utxos; ++i) {
1177
+ if (utxo_matched[i]) {
1178
+ continue;
1179
+ }
1180
+ if (txin->matchPrevout(utxos[i].get())) {
1181
+ utxo_matched[i] = true;
1182
+ found = true;
1183
+ break;
1184
+ }
1185
+ }
1186
+ if (!found) {
1187
+ str_error = "Lock tx input not found in utxo data";
1188
+ return 1;
1189
+ }
1190
+ }
1191
+
1192
+ return 0;
1193
+ }
1194
+
1195
+ int Interface::getLockRefundTxFee(int64_t &fee, int64_t locked_coin,
1196
+ const std::vector<unsigned char> &tx_lock_refund_bytes,
1197
+ std::string &str_error) const
1198
+ {
1199
+ // The lock refund tx spends the lock tx output to its own single output
1200
+ auto tx_lock_refund = getTransaction();
1201
+ if (0 >= tx_lock_refund->from_bytes(tx_lock_refund_bytes.data(), tx_lock_refund_bytes.size())) {
1202
+ str_error = "tx_lock_refund decode failed";
1203
+ return 1;
1204
+ }
1205
+ if (tx_lock_refund->m_vout.size() != 1) {
1206
+ str_error = "Lock refund tx doesn't have one output";
1207
+ return 1;
1208
+ }
1209
+ fee = locked_coin - tx_lock_refund->m_vout[0]->value();
1210
+ if (fee <= 0) {
1211
+ str_error = "Zero or negative lock refund tx fee";
1212
+ return 1;
1213
+ }
1214
+ return 0;
1215
+ }
1216
+
1217
+ int Interface::verifyTxFeeOutputs(const std::vector<unsigned char> &tx_data,
1218
+ const std::vector<SwapFeeOutput> &fee_outputs,
1219
+ std::string &str_error)
1220
+ {
1221
+ auto tx = getTransaction();
1222
+ if (tx->from_bytes(tx_data.data(), tx_data.size()) <= 0) {
1223
+ str_error = "tx decode failed";
1224
+ return 1;
1225
+ }
1226
+ std::vector<bool> used(tx->m_vout.size(), false);
1227
+ for (size_t i = 0; i < fee_outputs.size(); i++) {
1228
+ bool found{false};
1229
+ for (size_t n = 0; n < tx->m_vout.size(); n++) {
1230
+ const auto &txout = tx->m_vout[n];
1231
+ if (used[n] || !txout->script()) {
1232
+ continue;
1233
+ }
1234
+ if (*txout->script() == fee_outputs[i].m_script &&
1235
+ txout->value() == fee_outputs[i].m_value) {
1236
+ used[n] = true;
1237
+ found = true;
1238
+ break;
1239
+ }
1240
+ }
1241
+ if (!found) {
1242
+ str_error =
1243
+ strprintf("Missing fee output %d, value %" PRId64, i, fee_outputs[i].m_value);
1244
+ return 1;
1245
+ }
1246
+ }
1247
+ return 0;
1248
+ }
1249
+
1250
+ int Interface::verifySCLockSpendTx(SwapInfo &swap_info,
1251
+ const std::vector<SwapFeeOutput> &fee_outputs,
1252
+ std::string &str_error)
1253
+ {
1254
+ LOGD("verifySCLockSpendTx");
1255
+ auto tx_lock = getTransaction();
1256
+ auto tx_spend = getTransaction();
1257
+ int rv = tx_lock->from_bytes(swap_info.m_lock_tx_a.data(), swap_info.m_lock_tx_a.size());
1258
+ if (rv <= 0) {
1259
+ str_error = "tx decode failed";
1260
+ return 1;
1261
+ }
1262
+ // Expand lock spend tx
1263
+ std::vector<unsigned char> lock_script, lock_script_dest;
1264
+ if (0 != getScriptLockTxScript(lock_script, swap_info.m_Kal.data(), swap_info.m_Kaf.data())) {
1265
+ str_error = "getScriptLockTxScript failed";
1266
+ return 1;
1267
+ }
1268
+ if (0 != getScriptDest(lock_script_dest, lock_script)) {
1269
+ str_error = "getScriptDest failed";
1270
+ return 1;
1271
+ }
1272
+ auto prevout = getOutpoint();
1273
+ if (0 != tx_lock->getTxid(prevout->txid_data(), /* le */ true)) {
1274
+ str_error = "Failed to get lock txid";
1275
+ return 1;
1276
+ }
1277
+ prevout->setVout(getTxVout(tx_lock.get(), lock_script_dest));
1278
+ if (prevout->vout() < 0) {
1279
+ str_error = "Lock output not found";
1280
+ return 1;
1281
+ }
1282
+ auto txin = tx_spend->makeInput();
1283
+ txin->setPrevout(prevout.get());
1284
+ tx_spend->m_vin.push_back(std::move(txin));
1285
+
1286
+ auto up_txout = tx_spend->makeOutput();
1287
+ rv = up_txout->from_bytes(swap_info.m_lock_tx_spend_a_vout.data(),
1288
+ swap_info.m_lock_tx_spend_a_vout.size());
1289
+ if (rv <= 0) {
1290
+ str_error = "lock spend tx a vout decode failed";
1291
+ return 1;
1292
+ }
1293
+ const std::vector<unsigned char> payout_script = *up_txout->script();
1294
+ tx_spend->m_vout.push_back(std::move(up_txout));
1295
+ std::vector<std::vector<unsigned char>> dummy_witness_stack;
1296
+ getScriptLockTxDummyWitness(dummy_witness_stack, lock_script);
1297
+ const size_t witness_bytes = getWitnessStackSerialisedLength(dummy_witness_stack);
1298
+ const size_t payout_vsize = getTxVSize(tx_spend.get(), 0, witness_bytes);
1299
+ for (const auto &fee_output : fee_outputs) {
1300
+ auto txout = tx_spend->makeOutput();
1301
+ txout->setValue(fee_output.m_value);
1302
+ *txout->script() = fee_output.m_script;
1303
+ tx_spend->m_vout.push_back(std::move(txout));
1304
+ }
1305
+
1306
+ rv = tx_spend->to_bytes(swap_info.m_lock_spend_tx_a, false, false);
1307
+ if (rv <= 0) {
1308
+ str_error = "lock spend tx a encode failed";
1309
+ return 1;
1310
+ }
1311
+
1312
+ if (swap_info.m_initiating) {
1313
+ // if sending the chain a lock tx, spend destination doesn't matter
1314
+ } else if (swap_info.m_participating) {
1315
+ if (payout_script != swap_info.m_script_to) {
1316
+ str_error = "Lock spend payout does not pay the destination";
1317
+ return 1;
1318
+ }
1319
+ } else {
1320
+ str_error = "Unknown role in swap";
1321
+ return 1;
1322
+ }
1323
+
1324
+ // NOTE: The fee shouldn't matter to the swap initiator, as it's paid by the participant
1325
+ int64_t redeem_amount = tx_spend->m_vout[0]->value();
1326
+ int64_t paid_out{0};
1327
+ for (const auto &txout : tx_spend->m_vout) {
1328
+ const int64_t value = txout->value();
1329
+ if (value < 0 || value > swap_info.m_amount_from - paid_out) {
1330
+ str_error = "tx_spend invalid money range";
1331
+ return 1;
1332
+ }
1333
+ paid_out += value;
1334
+ }
1335
+ int64_t fee_paid = swap_info.m_amount_from - paid_out;
1336
+
1337
+ size_t vsize = getTxVSize(tx_spend.get(), 0, witness_bytes);
1338
+ int64_t fee_rate_paid = (fee_paid * 1000) / vsize;
1339
+
1340
+ LOGD("Redeem tx amount, vsize, feerate: %" PRId64 ", %zu, %" PRId64, redeem_amount, vsize,
1341
+ fee_rate_paid);
1342
+
1343
+ // Not a fee rate check: the spend tx and the pre-refund tx are RBF conflicts, so
1344
+ // the spend tx must pay a fixed amount above whatever the refund tx pays. Both
1345
+ // peers derive the same figure, so anything else is a mismatch.
1346
+ int64_t refund_fee;
1347
+ if (0 != getLockRefundTxFee(refund_fee, swap_info.m_amount_from, swap_info.m_lock_refund_tx_a,
1348
+ str_error)) {
1349
+ return 1;
1350
+ }
1351
+ const int64_t outputs_vsize = static_cast<int64_t>(vsize - payout_vsize);
1352
+ const int64_t expect_fee = refund_fee + ADAPTOR_SIG_LOCK_SPEND_FEE_BUFFER +
1353
+ feeForVSize(swap_info.m_fee_rate_from, outputs_vsize);
1354
+ if (fee_paid != expect_fee) {
1355
+ str_error =
1356
+ strprintf("Bad fee, expected: %" PRId64 ", paid: %" PRId64, expect_fee, fee_paid);
1357
+ return 1;
1358
+ }
1359
+
1360
+ return 0;
1361
+ }
1362
+
1363
+ int Interface::verifySCLockRefundTx(const SwapInfo &swap_info, std::string &str_error)
1364
+ {
1365
+ LOGD("verifySCLockRefundTx");
1366
+ int rv;
1367
+ auto tx_lock = getTransaction();
1368
+ auto tx_refund = getTransaction();
1369
+ rv = tx_lock->from_bytes(swap_info.m_lock_tx_a.data(), swap_info.m_lock_tx_a.size());
1370
+ if (rv <= 0) {
1371
+ str_error = strprintf("tx_lock decode failed: %d", rv);
1372
+ return 1;
1373
+ }
1374
+ rv = tx_refund->from_bytes(swap_info.m_lock_refund_tx_a.data(),
1375
+ swap_info.m_lock_refund_tx_a.size());
1376
+ if (rv <= 0) {
1377
+ str_error = strprintf("tx_refund decode failed: %d", rv);
1378
+ return 1;
1379
+ }
1380
+ if (tx_refund->version() != txVersion()) {
1381
+ str_error = "Bad tx version";
1382
+ return 1;
1383
+ }
1384
+ if (tx_refund->locktime() != 0) {
1385
+ str_error = "Bad locktime";
1386
+ return 1;
1387
+ }
1388
+ if (tx_refund->m_vin.size() != 1) {
1389
+ str_error = "tx_refund must have one input";
1390
+ return 1;
1391
+ }
1392
+ if (tx_refund->m_vout.size() != 1) {
1393
+ str_error = "tx_refund must have one output";
1394
+ return 1;
1395
+ }
1396
+ uint32_t tx_refund_txi0_sequence = tx_refund->m_vin[0]->sequence();
1397
+ uint32_t expected_sequence;
1398
+ if (0 != getExpectedSequence(expected_sequence, swap_info.m_lock_type, swap_info.m_lock_time_1,
1399
+ str_error)) {
1400
+ return 1;
1401
+ }
1402
+ if (tx_refund_txi0_sequence != expected_sequence) {
1403
+ str_error = strprintf("Bad input sequence: %d, expected %d", tx_refund_txi0_sequence,
1404
+ expected_sequence);
1405
+ return 1;
1406
+ }
1407
+ std::vector<unsigned char> lock_script, lock_script_script_sig, refund_script,
1408
+ refund_script_dest;
1409
+ if (0 != getScriptLockTxScript(lock_script, swap_info.m_Kal.data(), swap_info.m_Kaf.data())) {
1410
+ str_error = "getScriptLockTxScript failed";
1411
+ return 1;
1412
+ }
1413
+ if (0 != getScriptScriptSig(lock_script_script_sig, lock_script)) {
1414
+ str_error = "getScriptScriptSig failed";
1415
+ return 1;
1416
+ }
1417
+ if (!tx_refund->m_vin[0]->matchScript(lock_script_script_sig)) {
1418
+ str_error = "Input scriptsig mismatch";
1419
+ return 1;
1420
+ }
1421
+ auto prevout = getOutpoint();
1422
+ if (0 != getLockTxOutpoint(prevout.get(), swap_info, tx_lock.get(), str_error)) {
1423
+ return 1;
1424
+ }
1425
+ if (!tx_refund->m_vin[0]->matchPrevout(prevout.get())) {
1426
+ str_error = "Input prevout mismatch";
1427
+ return 1;
1428
+ }
1429
+
1430
+ uint32_t swipe_sequence;
1431
+ if (0 != getExpectedSequence(swipe_sequence, swap_info.m_lock_type, swap_info.m_lock_time_2,
1432
+ str_error)) {
1433
+ return 1;
1434
+ }
1435
+ if (0 != getScriptLockRefundTxScript(refund_script, swap_info.m_Kal.data(),
1436
+ swap_info.m_Kaf.data(), swipe_sequence)) {
1437
+ str_error = "getScriptLockRefundTxScript failed";
1438
+ return 1;
1439
+ }
1440
+ if (0 != getScriptDest(refund_script_dest, refund_script)) {
1441
+ str_error = "getScriptDest refund_script failed";
1442
+ return 1;
1443
+ }
1444
+ int refund_tx_n = getTxVout(tx_refund.get(), refund_script_dest);
1445
+ if (0 != refund_tx_n) {
1446
+ str_error = "Output not found in tx";
1447
+ return 1;
1448
+ }
1449
+ int64_t refund_amount = tx_refund->m_vout[refund_tx_n]->value();
1450
+ if (!moneyRange(refund_amount, swap_info.m_amount_from)) {
1451
+ str_error = "tx_refund invalid money range";
1452
+ return 1;
1453
+ }
1454
+ int64_t fee_paid = swap_info.m_amount_from - refund_amount;
1455
+
1456
+ std::vector<std::vector<unsigned char>> dummy_witness_stack;
1457
+ getScriptLockTxDummyWitness(dummy_witness_stack, lock_script);
1458
+
1459
+ size_t witness_bytes = getWitnessStackSerialisedLength(dummy_witness_stack);
1460
+ size_t vsize = getTxVSize((bsx::Transaction *)tx_refund.get(), 0, witness_bytes);
1461
+ int64_t fee_rate_paid = (fee_paid * 1000) / vsize;
1462
+
1463
+ LOGD("Refund tx amount, vsize, feerate: %" PRId64 ", %zu, %" PRId64, refund_amount, vsize,
1464
+ fee_rate_paid);
1465
+
1466
+ if (!compareFeeRate(fee_rate_paid, swap_info.m_fee_rate_from)) {
1467
+ str_error = strprintf("Bad fee rate, expected: %d, actual %d", swap_info.m_fee_rate_from,
1468
+ fee_rate_paid);
1469
+ return 1;
1470
+ }
1471
+
1472
+ const std::vector<unsigned char> *psig;
1473
+ const unsigned char *pK;
1474
+ if (swap_info.m_initiating) {
1475
+ psig = &swap_info.m_lock_tx_refund_a_sig_f;
1476
+ pK = swap_info.m_Kaf.data();
1477
+ } else if (swap_info.m_participating) {
1478
+ psig = &swap_info.m_lock_tx_refund_a_sig_l;
1479
+ pK = swap_info.m_Kal.data();
1480
+ } else {
1481
+ str_error = "Unknown role in swap";
1482
+ return 1;
1483
+ }
1484
+ // Absent only on a bridged leader's setup, which tryProcessSwapSetup enforces.
1485
+ if (swap_info.m_initiating && psig->empty()) {
1486
+ return 0;
1487
+ }
1488
+ rv = verifyTxSignature(swap_info.m_lock_refund_tx_a, *psig, pK, 0, lock_script,
1489
+ swap_info.m_amount_from);
1490
+ if (rv != 0) {
1491
+ str_error =
1492
+ strprintf("lock refund tx a signature %c invalid", swap_info.m_initiating ? 'F' : 'L');
1493
+ return 1;
1494
+ }
1495
+
1496
+ return 0;
1497
+ }
1498
+
1499
+ int Interface::verifySCLockRefundSpendTx(const SwapInfo &swap_info, std::string &str_error)
1500
+ {
1501
+ LOGD("verifySCLockRefundSpendTx");
1502
+ int rv;
1503
+ auto tx_refund = getTransaction();
1504
+ auto tx_refund_spend = getTransaction();
1505
+
1506
+ if (0 >= (rv = tx_refund->from_bytes(swap_info.m_lock_refund_tx_a.data(),
1507
+ swap_info.m_lock_refund_tx_a.size()))) {
1508
+ str_error = strprintf("tx_refund decode failed: %d", rv);
1509
+ return 1;
1510
+ }
1511
+ if (0 >= (rv = tx_refund_spend->from_bytes(swap_info.m_lock_refund_spend_tx_a.data(),
1512
+ swap_info.m_lock_refund_spend_tx_a.size()))) {
1513
+ str_error = strprintf("tx_refund_spend decode failed: %d", rv);
1514
+ return 1;
1515
+ }
1516
+
1517
+ if (tx_refund_spend->version() != txVersion()) {
1518
+ str_error = "Bad tx version";
1519
+ return 1;
1520
+ }
1521
+ if (tx_refund_spend->locktime() != 0) {
1522
+ str_error = "Bad locktime";
1523
+ return 1;
1524
+ }
1525
+ if (tx_refund_spend->m_vin.size() != 1) {
1526
+ str_error = "tx_refund_spend must have one input";
1527
+ return 1;
1528
+ }
1529
+ if (tx_refund_spend->m_vin[0]->sequence() != 1) {
1530
+ str_error = "Bad input sequence";
1531
+ return 1;
1532
+ }
1533
+ uint32_t tx_refund_sequence;
1534
+ if (0 != getExpectedSequence(tx_refund_sequence, swap_info.m_lock_type, swap_info.m_lock_time_2,
1535
+ str_error)) {
1536
+ return 1;
1537
+ }
1538
+ std::vector<unsigned char> refund_script, refund_script_script_sig;
1539
+ if (0 != getScriptLockRefundTxScript(refund_script, swap_info.m_Kal.data(),
1540
+ swap_info.m_Kaf.data(), tx_refund_sequence)) {
1541
+ str_error = "getScriptLockRefundTxScript failed";
1542
+ return 1;
1543
+ }
1544
+ if (0 != getScriptScriptSig(refund_script_script_sig, refund_script)) {
1545
+ str_error = "getScriptScriptSig failed";
1546
+ return 1;
1547
+ }
1548
+ if (!tx_refund_spend->m_vin[0]->matchScript(refund_script_script_sig)) {
1549
+ str_error = "Input scriptsig mismatch";
1550
+ return 1;
1551
+ }
1552
+ auto prevout = getOutpoint();
1553
+ if (0 != tx_refund->getTxid(prevout->txid_data(), /* le */ true)) {
1554
+ str_error = "Failed to get lock refund txid";
1555
+ return 1;
1556
+ }
1557
+ prevout->setVout(0);
1558
+ if (!tx_refund_spend->m_vin[0]->matchPrevout(prevout.get())) {
1559
+ str_error = "Input prevout mismatch";
1560
+ return 1;
1561
+ }
1562
+
1563
+ if (tx_refund->m_vout.size() != 1) {
1564
+ str_error = "tx_refund must have one output";
1565
+ return 1;
1566
+ }
1567
+ if (tx_refund_spend->m_vout.size() != 1) {
1568
+ str_error = "tx_refund_spend must have one output";
1569
+ return 1;
1570
+ }
1571
+
1572
+ if (swap_info.m_initiating) {
1573
+ int refund_spend_txo_n = getTxVout(tx_refund_spend.get(), swap_info.m_change_script);
1574
+ if (refund_spend_txo_n < 0) {
1575
+ str_error = "tx_refund_spend txo script mismatch";
1576
+ return 1;
1577
+ }
1578
+ } else if (swap_info.m_participating) {
1579
+ // refund dest does not affect follower
1580
+ } else {
1581
+ str_error = "Unknown role in swap";
1582
+ return 1;
1583
+ }
1584
+
1585
+ int64_t tx_refund_value = tx_refund->m_vout[0]->value();
1586
+ int64_t tx_refund_spend_value = tx_refund_spend->m_vout[0]->value();
1587
+ if (!moneyRange(tx_refund_spend_value, tx_refund_value)) {
1588
+ str_error = "tx_refund_spend invalid money range";
1589
+ return 1;
1590
+ }
1591
+ int64_t fee_paid = tx_refund_value - tx_refund_spend_value;
1592
+
1593
+ std::vector<std::vector<unsigned char>> dummy_witness_stack;
1594
+ getScriptLockRefundTxDummyWitness(dummy_witness_stack, refund_script);
1595
+
1596
+ size_t witness_bytes = getWitnessStackSerialisedLength(dummy_witness_stack);
1597
+ size_t vsize = getTxVSize((bsx::Transaction *)tx_refund_spend.get(), 0, witness_bytes);
1598
+ int64_t fee_rate_paid = (fee_paid * 1000) / vsize;
1599
+
1600
+ LOGD("Refund spend tx amount, vsize, feerate: %" PRId64 ", %zu, %" PRId64,
1601
+ tx_refund_spend_value, vsize, fee_rate_paid);
1602
+
1603
+ if (!compareFeeRate(fee_rate_paid, swap_info.m_fee_rate_from)) {
1604
+ str_error = strprintf("Bad fee rate, expected: %d, actual %d", swap_info.m_fee_rate_from,
1605
+ fee_rate_paid);
1606
+ return 1;
1607
+ }
1608
+
1609
+ if (swap_info.m_Kaf.size() != 33) {
1610
+ str_error = "Pubkey Kaf invalid size";
1611
+ return 1;
1612
+ }
1613
+ if (swap_info.m_Kbl_a.size() != 33) {
1614
+ str_error = "Pubkey Kbf_a invalid size";
1615
+ return 1;
1616
+ }
1617
+ if (swap_info.m_initiating && !swap_info.m_lock_tx_refund_spend_a_otves_f.empty()) {
1618
+ rv = verifyTxOtVES(swap_info.m_lock_refund_spend_tx_a,
1619
+ swap_info.m_lock_tx_refund_spend_a_otves_f, swap_info.m_Kaf.data(),
1620
+ swap_info.m_Kbl_a.data(), 0, refund_script, tx_refund_value);
1621
+ if (rv != 0) {
1622
+ str_error = "lock refund spend tx otves invalid";
1623
+ return 1;
1624
+ }
1625
+ }
1626
+ return 0;
1627
+ }
1628
+
1629
+ int Interface::extractTxSig(std::vector<unsigned char> &out, size_t witness_element,
1630
+ const std::vector<unsigned char> &tx_data, std::string &str_error)
1631
+ {
1632
+ auto tx = getTransaction();
1633
+ int rv;
1634
+ if (0 >= (rv = tx->from_bytes(tx_data.data(), tx_data.size()))) {
1635
+ str_error = "load tx failed";
1636
+ return 1;
1637
+ }
1638
+
1639
+ if (tx->m_vin.size() != 1) {
1640
+ str_error = strprintf("Unexpected input size %d", tx->m_vin.size());
1641
+ return 1;
1642
+ }
1643
+ std::vector<std::vector<unsigned char>> script_witness;
1644
+ tx->m_vin[0]->getWitnessData(script_witness);
1645
+
1646
+ if (script_witness.size() <= witness_element) {
1647
+ str_error = strprintf("Witness data is not set for element %d", witness_element);
1648
+ return 1;
1649
+ }
1650
+ out = script_witness[witness_element];
1651
+ return 0;
1652
+ }
1653
+
1654
+ int Interface::setSCLockSpendTxSig(SwapTx &stx, const SwapInfo &swap_info,
1655
+ std::span<const unsigned char> ka,
1656
+ std::span<const unsigned char> kb, std::string &str_error) const
1657
+ {
1658
+ auto tx = getTransaction();
1659
+ int rv;
1660
+ if (0 >= (rv = tx->from_bytes(swap_info.m_lock_spend_tx_a.data(),
1661
+ swap_info.m_lock_spend_tx_a.size(), /* allow_witness */ false))) {
1662
+ str_error = "load tx failed";
1663
+ return 1;
1664
+ }
1665
+ if (tx->m_vin.size() != 1) {
1666
+ str_error = strprintf("Unexpected input size %d", tx->m_vin.size());
1667
+ return 1;
1668
+ }
1669
+
1670
+ std::vector<unsigned char> sig_l, sig_f;
1671
+ rv = decryptOtVES(sig_l, kb.data(), swap_info.m_lock_tx_spend_a_otves_l);
1672
+ if (rv != 0) {
1673
+ str_error = strprintf("decryptOtVES failed %d", rv);
1674
+ return 1;
1675
+ }
1676
+ sig_l.push_back(SIGHASH_ALL);
1677
+
1678
+ SignatureHashes hash_cache;
1679
+ if (0 != getTxSignature(sig_f, hash_cache, ka, swap_info.m_lock_spend_tx_a, 0,
1680
+ swap_info.m_lock_tx_a_script, swap_info.m_amount_from)) {
1681
+ str_error = "get_tx_signature failed";
1682
+ return 1;
1683
+ }
1684
+
1685
+ std::vector<std::vector<unsigned char>> script_witness = {
1686
+ {}, sig_l, sig_f, swap_info.m_lock_tx_a_script};
1687
+ tx->m_vin[0]->setWitnessData(script_witness);
1688
+ stx.m_txid.resize(32);
1689
+ if (0 != (rv = tx->getTxid(stx.m_txid.data()))) {
1690
+ str_error = "getTxid failed";
1691
+ return 1;
1692
+ }
1693
+ if (0 >= (rv = tx->to_bytes(stx.m_tx_data))) {
1694
+ str_error = "serialise tx failed";
1695
+ return 1;
1696
+ }
1697
+
1698
+ return 0;
1699
+ }
1700
+
1701
+ int Interface::setSCLockRefundTxSig(std::vector<unsigned char> &out, const SwapInfo &swap_info,
1702
+ std::string &str_error) const
1703
+ {
1704
+ auto tx = getTransaction();
1705
+ int rv;
1706
+ if (0 >= (rv = tx->from_bytes(swap_info.m_lock_refund_tx_a.data(),
1707
+ swap_info.m_lock_refund_tx_a.size()))) {
1708
+ str_error = "load tx failed";
1709
+ return 1;
1710
+ }
1711
+ if (tx->m_vin.size() != 1) {
1712
+ str_error = strprintf("Unexpected input size %d", tx->m_vin.size());
1713
+ return 1;
1714
+ }
1715
+
1716
+ std::vector<std::vector<unsigned char>> script_witness = {{},
1717
+ swap_info.m_lock_tx_refund_a_sig_l,
1718
+ swap_info.m_lock_tx_refund_a_sig_f,
1719
+ swap_info.m_lock_tx_a_script};
1720
+ tx->m_vin[0]->setWitnessData(script_witness);
1721
+
1722
+ if (0 >= (rv = tx->to_bytes(out))) {
1723
+ str_error = "serialise tx failed";
1724
+ return 1;
1725
+ }
1726
+ return 0;
1727
+ }
1728
+
1729
+ int Interface::setSCLockRefundSpendTxSig(std::vector<unsigned char> &out, const SwapInfo &swap_info,
1730
+ std::span<const unsigned char> ka,
1731
+ std::span<const unsigned char> kb,
1732
+ std::string &str_error) const
1733
+ {
1734
+ auto tx_refund = getTransaction();
1735
+ auto tx = getTransaction();
1736
+
1737
+ int rv;
1738
+ if (0 >= (rv = tx->from_bytes(swap_info.m_lock_refund_spend_tx_a.data(),
1739
+ swap_info.m_lock_refund_spend_tx_a.size()))) {
1740
+ str_error = "load tx_refund_spend failed";
1741
+ return 1;
1742
+ }
1743
+ if (0 >= (rv = tx_refund->from_bytes(swap_info.m_lock_refund_tx_a.data(),
1744
+ swap_info.m_lock_refund_tx_a.size()))) {
1745
+ str_error = "load tx_refund failed";
1746
+ return 1;
1747
+ }
1748
+ if (tx_refund->m_vout.size() < 1) {
1749
+ str_error = strprintf("Unexpected tx_refund outputs size %d", tx_refund->m_vout.size());
1750
+ return 1;
1751
+ }
1752
+ int64_t tx_refund_value = tx_refund->m_vout[0]->value();
1753
+ if (tx->m_vin.size() != 1) {
1754
+ str_error = strprintf("Unexpected input size %d", tx->m_vin.size());
1755
+ return 1;
1756
+ }
1757
+
1758
+ std::vector<unsigned char> sig_l, sig_f;
1759
+ rv = decryptOtVES(sig_f, kb.data(), swap_info.m_lock_tx_refund_spend_a_otves_f);
1760
+ if (rv != 0) {
1761
+ str_error = strprintf("decryptOtVES failed %d", rv);
1762
+ return 1;
1763
+ }
1764
+ sig_f.push_back(SIGHASH_ALL);
1765
+
1766
+ SignatureHashes hash_cache;
1767
+ std::vector<unsigned char> refund_script;
1768
+ uint32_t tx_refund_sequence;
1769
+ if (0 != getExpectedSequence(tx_refund_sequence, swap_info.m_lock_type, swap_info.m_lock_time_2,
1770
+ str_error)) {
1771
+ return 1;
1772
+ }
1773
+ if (0 != getScriptLockRefundTxScript(refund_script, swap_info.m_Kal.data(),
1774
+ swap_info.m_Kaf.data(), tx_refund_sequence)) {
1775
+ str_error = "getScriptLockRefundTxScript failed";
1776
+ return 1;
1777
+ }
1778
+ if (0 != getTxSignature(sig_l, hash_cache, ka, swap_info.m_lock_refund_spend_tx_a, 0,
1779
+ refund_script, tx_refund_value)) {
1780
+ str_error = "get_tx_signature failed";
1781
+ return 1;
1782
+ }
1783
+
1784
+ std::vector<std::vector<unsigned char>> script_witness = {{}, sig_l, sig_f, {1}, refund_script};
1785
+ tx->m_vin[0]->setWitnessData(script_witness);
1786
+
1787
+ if (0 >= (rv = tx->to_bytes(out))) {
1788
+ str_error = "serialise tx failed";
1789
+ return 1;
1790
+ }
1791
+
1792
+ return 0;
1793
+ }
1794
+
1795
+ int Interface::createALockRefundSwipeTx(SwapTx &stx_lock_a_refund_swipe, const SwapInfo &swap_info,
1796
+ std::span<const unsigned char> ka,
1797
+ const std::vector<unsigned char> &script_to,
1798
+ std::string &str_error)
1799
+ {
1800
+ LOGD("createALockRefundSwipeTx");
1801
+
1802
+ auto tx_refund = getTransaction();
1803
+ auto tx_swipe = getTransaction();
1804
+
1805
+ int rv;
1806
+ if (0 >= (rv = tx_refund->from_bytes(swap_info.m_lock_refund_tx_a.data(),
1807
+ swap_info.m_lock_refund_tx_a.size()))) {
1808
+ str_error = "load tx_refund failed";
1809
+ return 1;
1810
+ }
1811
+ if (tx_refund->m_vin.size() != 1) {
1812
+ str_error = strprintf("Unexpected tx_refund vin size %d", tx_refund->m_vin.size());
1813
+ return 1;
1814
+ }
1815
+ if (tx_refund->m_vout.size() != 1) {
1816
+ str_error = strprintf("Unexpected tx_refund vout size %d", tx_refund->m_vout.size());
1817
+ return 1;
1818
+ }
1819
+
1820
+ SignatureHashes hash_cache;
1821
+ std::vector<unsigned char> refund_script, sig_f;
1822
+ uint32_t tx_refund_sequence;
1823
+ if (0 != getExpectedSequence(tx_refund_sequence, swap_info.m_lock_type, swap_info.m_lock_time_2,
1824
+ str_error)) {
1825
+ return 1;
1826
+ }
1827
+ if (0 != getScriptLockRefundTxScript(refund_script, swap_info.m_Kal.data(),
1828
+ swap_info.m_Kaf.data(), tx_refund_sequence)) {
1829
+ str_error = "getScriptLockRefundTxScript failed";
1830
+ return 1;
1831
+ }
1832
+
1833
+ std::vector<unsigned char> txid_refund(32);
1834
+ auto txin = tx_swipe->makeInput();
1835
+ if (0 != tx_refund->getTxid(txid_refund.data())) {
1836
+ str_error = "Failed to get lock refund txid";
1837
+ return 1;
1838
+ }
1839
+ if (0 != txin->setPrevout(txid_refund, 0)) {
1840
+ str_error = "setPrevout failed";
1841
+ return 1;
1842
+ }
1843
+ txin->setSequence(tx_refund_sequence);
1844
+ tx_swipe->m_vin.push_back(std::move(txin));
1845
+
1846
+ int64_t refund_value = tx_refund->m_vout[0]->value();
1847
+ auto txout = tx_swipe->makeOutput();
1848
+ txout->setValue(refund_value);
1849
+ *txout->script() = script_to;
1850
+
1851
+ tx_swipe->m_vout.push_back(std::move(txout));
1852
+
1853
+ std::vector<std::vector<unsigned char>> dummy_witness_stack;
1854
+ getScriptLockRefundSwipeTxDummyWitness(dummy_witness_stack, refund_script);
1855
+
1856
+ size_t witness_bytes = getWitnessStackSerialisedLength(dummy_witness_stack);
1857
+ size_t vsize = getTxVSize(tx_swipe.get(), 0, witness_bytes);
1858
+ int64_t tx_fee = feeForVSize(swap_info.m_fee_rate_from, vsize);
1859
+ tx_swipe->m_vout[0]->setValue(refund_value - tx_fee);
1860
+
1861
+ LOGD("createALockRefundSwipeTx fee_rate, vsize, fee: %" PRId64 ", %zu, %" PRId64,
1862
+ swap_info.m_fee_rate_from, vsize, tx_fee);
1863
+
1864
+ if (0 >= (rv = tx_swipe->to_bytes(stx_lock_a_refund_swipe.m_tx_data, /* append */ false,
1865
+ /* with_witness */ false))) {
1866
+ str_error = "serialise tx failed";
1867
+ return 1;
1868
+ }
1869
+ if (0 != getTxSignature(sig_f, hash_cache, ka, stx_lock_a_refund_swipe.m_tx_data, 0,
1870
+ refund_script, refund_value)) {
1871
+ str_error = "get_tx_signature failed";
1872
+ return 1;
1873
+ }
1874
+
1875
+ std::vector<std::vector<unsigned char>> script_witness = {sig_f, {}, refund_script};
1876
+ tx_swipe->m_vin[0]->setWitnessData(script_witness);
1877
+
1878
+ if (0 >= (rv = tx_swipe->to_bytes(stx_lock_a_refund_swipe.m_tx_data))) {
1879
+ str_error = "serialise tx failed";
1880
+ return 1;
1881
+ }
1882
+
1883
+ stx_lock_a_refund_swipe.m_swap_id = swap_info.m_swap_id;
1884
+ stx_lock_a_refund_swipe.m_coin_type = coinType();
1885
+ stx_lock_a_refund_swipe.m_tx_type = TxTypes::A_LOCK_REFUND_SWIPE;
1886
+ // The destination output is always vout 0; the optional mercy output is
1887
+ // appended after it.
1888
+ stx_lock_a_refund_swipe.m_txout_n = 0;
1889
+ stx_lock_a_refund_swipe.m_txout_script = script_to;
1890
+ stx_lock_a_refund_swipe.m_txout_value = tx_swipe->m_vout[0]->value();
1891
+ stx_lock_a_refund_swipe.m_txid.resize(32);
1892
+ rv = tx_swipe->getTxid(stx_lock_a_refund_swipe.m_txid.data());
1893
+ if (rv != 0) {
1894
+ throw std::runtime_error("getTxid failed");
1895
+ }
1896
+
1897
+ return 0;
1898
+ }
1899
+
1900
+ int Interface::createBLockSpendTx(SwapTx &stx_lock_b_spend, const SwapInfo &swap_info,
1901
+ const SwapTx &lock_b_tx,
1902
+ const std::vector<std::span<const unsigned char>> &vkeys,
1903
+ std::string &str_error, bool refund,
1904
+ const std::vector<SwapFeeOutput> &fee_outputs)
1905
+ {
1906
+ LOGD("createBLockSpendTx%s", refund ? " refund" : "");
1907
+ auto tx = getTransaction();
1908
+
1909
+ if (lock_b_tx.m_txout_n < 0) {
1910
+ str_error = "Lock tx b output index not set";
1911
+ return 1;
1912
+ }
1913
+ auto txin = tx->makeInput();
1914
+ int rv = txin->setPrevout(lock_b_tx.m_txid, lock_b_tx.m_txout_n);
1915
+ if (rv != 0) {
1916
+ str_error = "setPrevout failed";
1917
+ return rv;
1918
+ }
1919
+ tx->m_vin.push_back(std::move(txin));
1920
+
1921
+ auto txout = tx->makeOutput();
1922
+
1923
+ txout->setValue(lock_b_tx.m_txout_value);
1924
+ std::vector<unsigned char> *pscript = txout->script();
1925
+
1926
+ // The leader's m_script_to is already chain B's; m_change_script is chain from
1927
+ if (!refund) {
1928
+ *pscript = swap_info.m_script_to;
1929
+ } else if (0 != convertScriptForThisChain(*pscript, swap_info.m_change_script)) {
1930
+ str_error = "createBLockSpendTx: destination script not for this chain";
1931
+ return 1;
1932
+ }
1933
+
1934
+ tx->m_vout.push_back(std::move(txout));
1935
+ int64_t fees_total{0};
1936
+ for (const auto &fee_output : fee_outputs) {
1937
+ auto fee_txout = tx->makeOutput();
1938
+ fee_txout->setValue(fee_output.m_value);
1939
+ *fee_txout->script() = fee_output.m_script;
1940
+ tx->m_vout.push_back(std::move(fee_txout));
1941
+ fees_total += fee_output.m_value;
1942
+ }
1943
+
1944
+ std::vector<std::vector<unsigned char>> dummy_witness_stack;
1945
+ getP2WPKHDummyWitness(dummy_witness_stack);
1946
+
1947
+ size_t witness_bytes = getWitnessStackSerialisedLength(dummy_witness_stack);
1948
+ size_t vsize = getTxVSize(tx.get(), 0, witness_bytes);
1949
+ int64_t tx_fee = feeForVSize(swap_info.m_fee_rate_to, vsize);
1950
+ const int64_t payout = lock_b_tx.m_txout_value - tx_fee - fees_total;
1951
+ tx->m_vout[0]->setValue(payout);
1952
+
1953
+ LOGD("createBLockSpendTx fee_rate, vsize, fee: %" PRId64 ", %zu, %" PRId64,
1954
+ swap_info.m_fee_rate_to, vsize, tx_fee);
1955
+
1956
+ std::vector<unsigned char> txb, sig, Kbs, prev_script;
1957
+ secure_bytes kbs;
1958
+ if (vkeys.size() < 2) {
1959
+ str_error = "Less than two keys provided";
1960
+ return 1;
1961
+ }
1962
+ rv = sumKeys(kbs, vkeys[0], vkeys[1]);
1963
+ if (rv != 0) {
1964
+ str_error = "sumKeys failed";
1965
+ return 1;
1966
+ }
1967
+
1968
+ SignatureHashes hash_cache;
1969
+ rv = tx->to_bytes(txb, false, false);
1970
+ if (rv < 0) {
1971
+ str_error = "tx to_bytes failed";
1972
+ return 1;
1973
+ }
1974
+
1975
+ if (0 != getSignatureScript(prev_script, lock_b_tx.m_txout_script)) {
1976
+ str_error = "createBLockSpendTx: get_signature_script failed";
1977
+ return 1;
1978
+ }
1979
+ if (0 != getTxSignature(sig, hash_cache, kbs, txb, 0, prev_script, lock_b_tx.m_txout_value)) {
1980
+ str_error = "createBLockSpendTx: get_tx_signature failed";
1981
+ return 1;
1982
+ }
1983
+ if (0 != getPubkey(Kbs, kbs)) {
1984
+ str_error = "Failed to get Kbs pubkey";
1985
+ return 1;
1986
+ }
1987
+
1988
+ if (0 != setBLockSpendSignature(*tx->m_vin[0], sig, Kbs)) {
1989
+ str_error = "createBLockSpendTx: setBLockSpendSignature failed";
1990
+ return 1;
1991
+ }
1992
+
1993
+ stx_lock_b_spend.m_swap_id = swap_info.m_swap_id;
1994
+ stx_lock_b_spend.m_coin_type = coinType();
1995
+ stx_lock_b_spend.m_tx_type = refund ? TxTypes::B_LOCK_REFUND : TxTypes::B_LOCK_SPEND;
1996
+ stx_lock_b_spend.m_txout_n = 0;
1997
+ stx_lock_b_spend.m_txout_value = payout;
1998
+ stx_lock_b_spend.m_txout_script = *tx->m_vout[0]->script();
1999
+ stx_lock_b_spend.m_txid.resize(32);
2000
+ rv = tx->getTxid(stx_lock_b_spend.m_txid.data());
2001
+ if (rv != 0) {
2002
+ str_error = strprintf("getTxid failed %d", rv);
2003
+ return 1;
2004
+ }
2005
+
2006
+ rv = tx->to_bytes(stx_lock_b_spend.m_tx_data, false, true);
2007
+ if (rv < 0) {
2008
+ str_error = strprintf("signed tx to_bytes failed %d", rv);
2009
+ return 1;
2010
+ }
2011
+
2012
+ return 0;
2013
+ }
2014
+
2015
+ bool Interface::checkProofOfWork(uint256 block_hash, int nbits) const
2016
+ {
2017
+ bool negative, overflow;
2018
+ arith_uint256 bn_target;
2019
+
2020
+ bn_target.SetCompact(nbits, &negative, &overflow);
2021
+
2022
+ // Check range
2023
+ if (negative || bn_target == 0 || overflow ||
2024
+ bn_target > UintToArith256(m_chain_params->m_pow_limit)) {
2025
+ return false;
2026
+ }
2027
+
2028
+ // Check proof of work matches claimed amount
2029
+ if (UintToArith256(block_hash) > bn_target) {
2030
+ return false;
2031
+ }
2032
+
2033
+ return true;
2034
+ }
2035
+
2036
+ bool Interface::dissentCarriesWork(int height, std::shared_ptr<bsx::BlockHeader> header,
2037
+ const bsx::BlockHeader &contradicted) const
2038
+ {
2039
+ if (!checkBlockHeader(height, header)) {
2040
+ return false;
2041
+ }
2042
+ bool negative, overflow;
2043
+ arith_uint256 target, contradicted_target;
2044
+ target.SetCompact(header->getBits(), &negative, &overflow);
2045
+ if (negative || overflow || target == 0) {
2046
+ return false;
2047
+ }
2048
+ contradicted_target.SetCompact(contradicted.getBits(), &negative, &overflow);
2049
+ return !negative && !overflow && target <= contradicted_target;
2050
+ }
2051
+
2052
+ bool Interface::checkBlockHeader(int height, std::shared_ptr<bsx::BlockHeader> header) const
2053
+ {
2054
+ uint256 block_hash = header->getHash();
2055
+
2056
+ return checkProofOfWork(block_hash, header->getBits());
2057
+ }
2058
+
2059
+ uint32_t Interface::calculateNextWorkRequired(uint32_t last_bits, int64_t last_time,
2060
+ int64_t first_time) const
2061
+ {
2062
+ const ChainParams &params = *m_chain_params;
2063
+ if (params.m_pow_no_retargeting) {
2064
+ return last_bits;
2065
+ }
2066
+ int64_t actual_timespan = last_time - first_time;
2067
+ actual_timespan = std::max(actual_timespan, params.m_pow_target_timespan / 4);
2068
+ actual_timespan = std::min(actual_timespan, params.m_pow_target_timespan * 4);
2069
+
2070
+ const arith_uint256 pow_limit = UintToArith256(params.m_pow_limit);
2071
+ arith_uint256 target;
2072
+ target.SetCompact(last_bits);
2073
+ const bool shift = retargetShiftsNearLimit() && target.bits() > pow_limit.bits() - 1;
2074
+ if (shift) {
2075
+ target >>= 1;
2076
+ }
2077
+ target *= actual_timespan;
2078
+ target /= params.m_pow_target_timespan;
2079
+ if (shift) {
2080
+ target <<= 1;
2081
+ }
2082
+ if (target > pow_limit) {
2083
+ target = pow_limit;
2084
+ }
2085
+ return target.GetCompact();
2086
+ }
2087
+
2088
+ int Interface::retargetLookback(int height) const { return m_chain_params->retargetInterval() - 1; }
2089
+
2090
+ int Interface::nextWorkRequired(uint32_t &bits, const BlockIndex &prev, int64_t block_time) const
2091
+ {
2092
+ const ChainParams &params = *m_chain_params;
2093
+ const int interval = params.retargetInterval();
2094
+ if (interval <= 0 || !prev.m_header) {
2095
+ return 1;
2096
+ }
2097
+ const uint32_t prev_bits = prev.m_header->getBits();
2098
+ if (params.m_pow_no_retargeting) {
2099
+ bits = prev_bits;
2100
+ return 0;
2101
+ }
2102
+ const int height = prev.m_height + 1;
2103
+ if (height % interval != 0) {
2104
+ if (!params.m_pow_allow_min_difficulty_blocks) {
2105
+ bits = prev_bits;
2106
+ return 0;
2107
+ }
2108
+ const uint32_t limit_bits = UintToArith256(params.m_pow_limit).GetCompact();
2109
+ if (block_time > prev.getBlockTime() + params.m_pow_target_spacing * 2) {
2110
+ bits = limit_bits;
2111
+ return 0;
2112
+ }
2113
+ const BlockIndex *pindex = &prev;
2114
+ while (pindex->m_prev && pindex->m_height % interval != 0 &&
2115
+ pindex->m_header->getBits() == limit_bits) {
2116
+ pindex = pindex->m_prev;
2117
+ }
2118
+ if (pindex->m_height % interval != 0 && pindex->m_header->getBits() == limit_bits) {
2119
+ return 1;
2120
+ }
2121
+ bits = pindex->m_header->getBits();
2122
+ return 0;
2123
+ }
2124
+
2125
+ const BlockIndex *first = &prev;
2126
+ for (int i = 0; first && i < retargetLookback(height); i++) {
2127
+ first = first->m_prev;
2128
+ }
2129
+ if (!first) {
2130
+ return 1;
2131
+ }
2132
+ bits = calculateNextWorkRequired(prev_bits, prev.getBlockTime(), first->getBlockTime());
2133
+ return 0;
2134
+ }
2135
+
2136
+ static int64_t medianTimePast(const BlockIndex &prev)
2137
+ {
2138
+ const size_t window{11};
2139
+ std::vector<int64_t> times;
2140
+ for (const BlockIndex *pindex = &prev; pindex && times.size() < window;
2141
+ pindex = pindex->m_prev) {
2142
+ times.push_back(pindex->getBlockTime());
2143
+ }
2144
+ if (times.size() < window) {
2145
+ return -1;
2146
+ }
2147
+ std::sort(times.begin(), times.end());
2148
+ return times[window / 2];
2149
+ }
2150
+
2151
+ bool Interface::checkBlockHeaderContext(const BlockIndex &prev,
2152
+ std::shared_ptr<bsx::BlockHeader> header) const
2153
+ {
2154
+ const int height = prev.m_height + 1;
2155
+ const int64_t block_time = header->getBlockTime();
2156
+ const int64_t median_time_past = medianTimePast(prev);
2157
+ if (median_time_past >= 0 && block_time <= median_time_past) {
2158
+ LOGE("%s block %d: time %" PRId64 " not after median time past %" PRId64, coinTicker(),
2159
+ height, block_time, median_time_past);
2160
+ return false;
2161
+ }
2162
+ uint32_t required_bits{0};
2163
+ if (0 != nextWorkRequired(required_bits, prev, block_time)) {
2164
+ LOGE("%s block %d: the ancestors its bits depend on are not held", coinTicker(), height);
2165
+ return false;
2166
+ }
2167
+ if (header->getBits() != required_bits) {
2168
+ LOGE("%s block %d: bits %08x, required %08x", coinTicker(), height, header->getBits(),
2169
+ required_bits);
2170
+ return false;
2171
+ }
2172
+ return true;
2173
+ }
2174
+
2175
+ int64_t Interface::dustThreshold() const
2176
+ {
2177
+ // A p2wpkh output, the shape the module's own change takes
2178
+ std::vector<unsigned char> p2wpkh(22, 0);
2179
+ p2wpkh[1] = 20;
2180
+ return std::max<int64_t>(CoinInterface::dustThreshold(), outputDustThreshold(p2wpkh));
2181
+ }
2182
+
2183
+ int64_t Interface::outputDustThreshold(const std::vector<unsigned char> &script) const
2184
+ {
2185
+ if (!script.empty() && script[0] == OP_RETURN) {
2186
+ return 0;
2187
+ }
2188
+ const size_t len = script.size();
2189
+ const bool witness = len >= 4 && len <= 42 &&
2190
+ (script[0] == OP_0 || (script[0] >= OP_1 && script[0] <= OP_16)) &&
2191
+ static_cast<size_t>(script[1]) + 2 == len;
2192
+ const int64_t input_size =
2193
+ witness ? 32 + 4 + 1 + 107 / witnessScaleFactor() + 4 : 32 + 4 + 1 + 107 + 4;
2194
+ const int64_t size = 8 + (len < 253 ? 1 : 3) + static_cast<int64_t>(len) + input_size;
2195
+ const int64_t fee = feeForVSize(dustRelayFee(), size);
2196
+ return fee == 0 && dustRelayFee() > 0 ? 1 : fee;
2197
+ }
2198
+
2199
+ } // namespace btc_base
2200
+ } // namespace bsx