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,1815 @@
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
+
7
+ #include <BSXContext.h>
8
+ #include <bsx_db.h>
9
+ #include <crypto/util.h>
10
+ #include <electrum.h>
11
+ #include <node_client.h>
12
+ #include <util/log.h>
13
+ #include <util/str.h>
14
+ #include <util/tinyformat.h>
15
+
16
+ #include <climits>
17
+
18
+ namespace bsx {
19
+
20
+ CoinInterface::CoinInterface(BSXContext *context, int chain_type,
21
+ std::unique_ptr<ChainParams> chain_params)
22
+ : m_context(context), m_chain_type(chain_type),
23
+ m_chain_params(chain_params ? std::move(chain_params)
24
+ : std::make_unique<bsx::ChainParams>(chain_type))
25
+ {
26
+ m_db = dynamic_cast<BSXDB *>(m_context);
27
+ }
28
+
29
+ std::string CoinInterface::formatAmount(int64_t amount, int display_scale) const
30
+ {
31
+ int scale = exp();
32
+ if (display_scale < 0) {
33
+ display_scale = scale;
34
+ }
35
+ return format_amount(amount, scale, display_scale);
36
+ }
37
+
38
+ // The inverse of formatAmount
39
+ // r: 0 rejects more decimals than the coin has, > 0 rounds off, < 0 floors.
40
+ int CoinInterface::amountFromString(int64_t &out, const std::string &value, int rounding) const
41
+ {
42
+ size_t i = 0;
43
+ int64_t sign = 1;
44
+ if (!value.empty() && value[0] == '-') {
45
+ sign = -1;
46
+ i = 1;
47
+ }
48
+ if (i >= value.size()) {
49
+ return 1;
50
+ }
51
+
52
+ int64_t ep = 1;
53
+ for (int n = 0; n < exp(); ++n) {
54
+ ep *= 10;
55
+ }
56
+
57
+ bool have_dp = false;
58
+ int64_t rv = 0;
59
+ for (; i < value.size(); ++i) {
60
+ const char c = value[i];
61
+ if (c == '.') {
62
+ if (have_dp || rv > std::numeric_limits<int64_t>::max() / ep) {
63
+ return 1;
64
+ }
65
+ rv *= ep;
66
+ have_dp = true;
67
+ continue;
68
+ }
69
+ if (c < '0' || c > '9') {
70
+ return 1;
71
+ }
72
+ const int64_t digit = c - '0';
73
+ if (have_dp) {
74
+ ep /= 10;
75
+ if (ep <= 0) {
76
+ if (rounding == 0) {
77
+ return 1;
78
+ }
79
+ if (rounding > 0 && digit > 4) {
80
+ if (rv == std::numeric_limits<int64_t>::max()) {
81
+ return 1;
82
+ }
83
+ rv += 1;
84
+ }
85
+ break;
86
+ }
87
+ if (rv > std::numeric_limits<int64_t>::max() - ep * digit) {
88
+ return 1;
89
+ }
90
+ rv += ep * digit;
91
+ } else {
92
+ if (rv > (std::numeric_limits<int64_t>::max() - digit) / 10) {
93
+ return 1;
94
+ }
95
+ rv = rv * 10 + digit;
96
+ }
97
+ }
98
+ if (!have_dp) {
99
+ if (rv > std::numeric_limits<int64_t>::max() / ep) {
100
+ return 1;
101
+ }
102
+ rv *= ep;
103
+ }
104
+
105
+ out = rv * sign;
106
+ return 0;
107
+ }
108
+
109
+ bool CoinInterface::checkFeeRate(int64_t fee_rate, int64_t now, std::string &str_error) const
110
+ {
111
+ if (fee_rate < minFeeRate() || fee_rate > maxFeeRate()) {
112
+ str_error = strprintf("%s fee rate %d outside %d..%d", coinTicker(), fee_rate, minFeeRate(),
113
+ maxFeeRate());
114
+ return false;
115
+ }
116
+ if (m_est_fee_rate > 0 && !m_est_fee_rate_from_relay &&
117
+ now - m_est_fee_rate_at < FEE_ESTIMATE_MAX_AGE &&
118
+ fee_rate > m_est_fee_rate * maxFeeRateMultiple()) {
119
+ str_error = strprintf("%s fee rate %d above %dx the estimated %d", coinTicker(), fee_rate,
120
+ maxFeeRateMultiple(), m_est_fee_rate);
121
+ return false;
122
+ }
123
+ return checkFeeRateFloor(fee_rate, now, str_error);
124
+ }
125
+
126
+ bool CoinInterface::checkFeeRateFloor(int64_t fee_rate, int64_t now, std::string &str_error) const
127
+ {
128
+ if (m_est_low_fee_rate > 0 && now - m_est_low_fee_rate_at < FEE_ESTIMATE_MAX_AGE &&
129
+ fee_rate < m_est_low_fee_rate * LOW_FEE_RATE_PERCENT / 100) {
130
+ str_error =
131
+ strprintf("%s fee rate %d below %d%% of the %d-block estimate %d", coinTicker(),
132
+ fee_rate, (int)LOW_FEE_RATE_PERCENT, LOW_FEE_CONF_TARGET, m_est_low_fee_rate);
133
+ return false;
134
+ }
135
+ return true;
136
+ }
137
+
138
+ void CoinInterface::dropStaleFeeRates(int64_t now)
139
+ {
140
+ for (auto *rates : {&m_server_fee_rates, &m_server_low_fee_rates}) {
141
+ std::erase_if(*rates, [now](const auto &entry) {
142
+ return now - entry.second.second >= FEE_ESTIMATE_MAX_AGE;
143
+ });
144
+ }
145
+ }
146
+
147
+ // The highest fresh estimate, capped at FEE_ESTIMATE_DISPUTE_RATIO times the lowest
148
+ static int64_t combinedFeeRate(const std::map<int, std::pair<int64_t, int64_t>> &rates,
149
+ bool &disputed)
150
+ {
151
+ int64_t lowest{std::numeric_limits<int64_t>::max()}, highest{0};
152
+ for (const auto &[id, entry] : rates) {
153
+ lowest = std::min(lowest, entry.first);
154
+ highest = std::max(highest, entry.first);
155
+ }
156
+ disputed = highest > lowest * FEE_ESTIMATE_DISPUTE_RATIO;
157
+ return disputed ? lowest * FEE_ESTIMATE_DISPUTE_RATIO : highest;
158
+ }
159
+
160
+ void CoinInterface::setServerLowFeeRate(int client_id, int64_t fee_rate, int64_t now)
161
+ {
162
+ m_server_low_fee_rates[client_id] = {fee_rate, now};
163
+ dropStaleFeeRates(now);
164
+ // The lowest, so a server reporting high can't refuse honest offers
165
+ int64_t lowest{fee_rate};
166
+ for (const auto &[id, entry] : m_server_low_fee_rates) {
167
+ lowest = std::min(lowest, entry.first);
168
+ }
169
+ m_est_low_fee_rate = lowest;
170
+ m_est_low_fee_rate_at = now;
171
+ }
172
+
173
+ void CoinInterface::setServerFeeRate(int client_id, int64_t fee_rate, int64_t now)
174
+ {
175
+ m_server_fee_rates[client_id] = {fee_rate, now};
176
+ dropStaleFeeRates(now);
177
+ bool disputed{false};
178
+ const int64_t combined = combinedFeeRate(m_server_fee_rates, disputed);
179
+ if (disputed && !m_fee_estimates_disputed) {
180
+ LOGE("%s: the servers' fee estimates differ by more than %dx, the higher is capped",
181
+ coinTicker(), (int)FEE_ESTIMATE_DISPUTE_RATIO);
182
+ }
183
+ m_fee_estimates_disputed = disputed;
184
+ m_est_fee_rate = combined;
185
+ m_est_fee_rate_at = now;
186
+ m_est_fee_rate_from_relay = false;
187
+ }
188
+
189
+ int CoinInterface::proveDLEAG(std::vector<unsigned char> &proof, std::span<const unsigned char> key,
190
+ const unsigned char *msg32) const
191
+ {
192
+ LOGE("%s: proveDLEAG not supported", coinTicker());
193
+ return 1;
194
+ }
195
+
196
+ int CoinInterface::dleagPubkeys(std::vector<unsigned char> &pk_secp256k1,
197
+ std::vector<unsigned char> &pk_ed25519,
198
+ const std::vector<unsigned char> &proof,
199
+ const unsigned char *msg32) const
200
+ {
201
+ LOGE("%s: dleagPubkeys not supported", coinTicker());
202
+ return 1;
203
+ }
204
+
205
+ int CoinInterface::getTxid(std::vector<unsigned char> &txid, std::vector<unsigned char> &tx_data,
206
+ bool allow_witness)
207
+ {
208
+ auto tx = getTransaction();
209
+ int rv = tx->from_bytes(tx_data.data(), tx_data.size(), allow_witness);
210
+ if (rv <= 0) {
211
+ LOGE("getTxid from_bytes failed");
212
+ return rv;
213
+ }
214
+ if ((size_t)rv != tx_data.size()) {
215
+ LOGE("getTxid: bytes after the tx");
216
+ return 1;
217
+ }
218
+ txid.resize(32);
219
+ rv = tx->getTxid(txid.data());
220
+ if (rv != 0) {
221
+ LOGE("getTxid failed");
222
+ return rv;
223
+ }
224
+ return 0;
225
+ }
226
+
227
+ int CoinInterface::getTxOutput(std::vector<unsigned char> &script, int64_t &value,
228
+ const std::vector<unsigned char> &tx_data, int n)
229
+ {
230
+ auto tx = getTransaction();
231
+ int rv = tx->from_bytes(tx_data.data(), tx_data.size(), /* allow_witness */ true);
232
+ if (rv <= 0 || (size_t)rv != tx_data.size()) {
233
+ LOGE("getTxOutput from_bytes failed");
234
+ return 1;
235
+ }
236
+ if (n < 0 || n >= (int)tx->m_vout.size()) {
237
+ LOGE("getTxOutput invalid output index %d of %zu", n, tx->m_vout.size());
238
+ return 1;
239
+ }
240
+ if (!tx->m_vout[n]->script()) {
241
+ LOGE("getTxOutput output %d has no script", n);
242
+ return 1;
243
+ }
244
+ script = *tx->m_vout[n]->script();
245
+ value = tx->m_vout[n]->value();
246
+ return 0;
247
+ }
248
+
249
+ int CoinInterface::findTxOutputForScript(int &out_n, const std::vector<unsigned char> &tx_data,
250
+ const std::vector<unsigned char> &script)
251
+ {
252
+ auto tx = getTransaction();
253
+ const int rv = tx->from_bytes(tx_data.data(), tx_data.size(), /* allow_witness */ true);
254
+ if (rv <= 0 || (size_t)rv != tx_data.size()) {
255
+ LOGE("findTxOutputForScript from_bytes failed");
256
+ return 1;
257
+ }
258
+ for (size_t n = 0; n < tx->m_vout.size(); ++n) {
259
+ const auto *out_script = tx->m_vout[n]->script();
260
+ if (out_script && *out_script == script) {
261
+ out_n = (int)n;
262
+ return 0;
263
+ }
264
+ }
265
+ return 1;
266
+ }
267
+
268
+ int CoinInterface::findTxOutputForTxOut(int &out_n, const std::vector<unsigned char> &tx_data,
269
+ const std::vector<unsigned char> &txout_data)
270
+ {
271
+ auto tx = getTransaction();
272
+ const int rv = tx->from_bytes(tx_data.data(), tx_data.size(), /* allow_witness */ true);
273
+ if (rv <= 0 || (size_t)rv != tx_data.size()) {
274
+ LOGE("findTxOutputForTxOut from_bytes failed");
275
+ return 1;
276
+ }
277
+ auto txout = tx->makeOutput();
278
+ if (txout->from_bytes(txout_data.data(), txout_data.size()) <= 0 || !txout->script()) {
279
+ LOGE("findTxOutputForTxOut txout from_bytes failed");
280
+ return 1;
281
+ }
282
+ for (size_t n = 0; n < tx->m_vout.size(); ++n) {
283
+ const auto *out_script = tx->m_vout[n]->script();
284
+ if (out_script && *out_script == *txout->script() &&
285
+ tx->m_vout[n]->value() == txout->value()) {
286
+ out_n = (int)n;
287
+ return 0;
288
+ }
289
+ }
290
+ return 1;
291
+ }
292
+
293
+ int CoinInterface::findTxInputSpending(int &input_n, const std::vector<unsigned char> &tx_data,
294
+ const std::vector<unsigned char> &prev_txid, int prev_n)
295
+ {
296
+ if (prev_txid.size() != 32) {
297
+ return 1;
298
+ }
299
+ auto tx = getTransaction();
300
+ const int rv = tx->from_bytes(tx_data.data(), tx_data.size(), /* allow_witness */ true);
301
+ if (rv <= 0 || (size_t)rv != tx_data.size()) {
302
+ LOGE("findTxInputSpending from_bytes failed");
303
+ return 1;
304
+ }
305
+ unsigned char prev_hash[32];
306
+ memcpy_reverse(prev_hash, prev_txid.data(), 32);
307
+ for (size_t n = 0; n < tx->m_vin.size(); ++n) {
308
+ const auto &input = tx->m_vin[n];
309
+ if (input->vout() == prev_n && 0 == memcmp(input->txid(), prev_hash, 32)) {
310
+ input_n = (int)n;
311
+ return 0;
312
+ }
313
+ }
314
+ return 1;
315
+ }
316
+
317
+ int CoinInterface::findTxSpendableOutput(int &out_n, const std::vector<unsigned char> &tx_data)
318
+ {
319
+ LOGE("findTxSpendableOutput is not implemented for %s", coinTicker());
320
+ return 1;
321
+ }
322
+
323
+ int CoinInterface::getMercyKeyshare(std::vector<unsigned char> &keyshare,
324
+ const std::vector<unsigned char> &tx_data)
325
+ {
326
+ LOGE("getMercyKeyshare is not implemented for %s", coinTicker());
327
+ return 1;
328
+ }
329
+
330
+ int CoinInterface::getMercyPrevout(int &n, const std::vector<unsigned char> &swipe_tx_data)
331
+ {
332
+ LOGE("getMercyPrevout is not implemented for %s", coinTicker());
333
+ return 1;
334
+ }
335
+
336
+ int CoinInterface::getMercyWatchVouts(std::vector<int> &vouts,
337
+ const std::vector<unsigned char> &swipe_tx_data)
338
+ {
339
+ LOGE("getMercyWatchVouts is not implemented for %s", coinTicker());
340
+ return 1;
341
+ }
342
+
343
+ int CoinInterface::createMercyTx(std::vector<unsigned char> &out,
344
+ const std::vector<unsigned char> &swipe_tx_data,
345
+ std::span<const unsigned char> keyshare,
346
+ const std::vector<unsigned char> &script_to,
347
+ const std::span<const unsigned char> key, int64_t fee_rate,
348
+ std::string &str_error)
349
+ {
350
+ str_error = strprintf("createMercyTx is not implemented for %s", coinTicker());
351
+ return 1;
352
+ }
353
+
354
+ int CoinInterface::getTxNumOutputs(const std::vector<unsigned char> &tx_data)
355
+ {
356
+ auto tx = getTransaction();
357
+ int rv = tx->from_bytes(tx_data.data(), tx_data.size(), /* allow_witness */ true);
358
+ if (rv <= 0 || (size_t)rv != tx_data.size()) {
359
+ LOGE("getTxNumOutputs from_bytes failed");
360
+ return -1;
361
+ }
362
+ return (int)tx->m_vout.size();
363
+ }
364
+
365
+ int64_t CoinInterface::feeForVSize(int64_t fee_rate, int64_t vsize)
366
+ {
367
+ if (fee_rate > 0 && vsize > INT64_MAX / fee_rate) {
368
+ return INT64_MAX / 1000 + 1;
369
+ }
370
+ const int64_t fee = fee_rate * vsize;
371
+ return fee / 1000 + (fee % 1000 > 0 ? 1 : 0);
372
+ }
373
+
374
+ bool CoinInterface::isValidTx(const unsigned char *data, size_t data_len) const
375
+ {
376
+ auto tx = getTransaction();
377
+ int rv = tx->from_bytes(data, data_len, /* allow_witness */ true);
378
+ return rv > 0 && (size_t)rv == data_len && !tx->m_vin.empty() && !tx->m_vout.empty();
379
+ }
380
+
381
+ int CoinInterface::getChainHeight() const
382
+ {
383
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
384
+ if (!m_best_block) {
385
+ return -1;
386
+ }
387
+ return m_best_block->m_height;
388
+ }
389
+
390
+ int64_t CoinInterface::getChainTime() const
391
+ {
392
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
393
+ if (!m_best_block || !m_best_block->m_header) {
394
+ return -1;
395
+ }
396
+ return m_best_block->getBlockTime();
397
+ }
398
+
399
+ int64_t CoinInterface::getMedianChainTimePast(int height) const
400
+ {
401
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
402
+ const int num_samples{11};
403
+ int64_t pmedian[num_samples];
404
+ int64_t *pbegin = &pmedian[num_samples];
405
+ int64_t *pend = &pmedian[num_samples];
406
+
407
+ const BlockIndex *pindex = m_best_block;
408
+ if (height > 0) {
409
+ while (pindex && pindex->m_height > height) {
410
+ pindex = pindex->m_prev;
411
+ }
412
+ // Headers are only kept back to the anchor, so an older height is
413
+ // unknown rather than mature
414
+ if (!pindex || pindex->m_height != height) {
415
+ return -1;
416
+ }
417
+ }
418
+ for (int i = 0; i < num_samples && pindex; i++, pindex = pindex->m_prev) {
419
+ *(--pbegin) = pindex->getBlockTime();
420
+ }
421
+ if (pbegin == pend) {
422
+ return -1;
423
+ }
424
+
425
+ std::sort(pbegin, pend);
426
+ return pbegin[(pend - pbegin) / 2];
427
+ }
428
+
429
+ void CoinInterface::setServerTipHeight(int client_id, int height)
430
+ {
431
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
432
+ m_server_tip_heights[client_id] = height;
433
+ }
434
+
435
+ bool CoinInterface::chainSynced(int64_t now)
436
+ {
437
+ std::vector<int> live_ids;
438
+ for (const auto &client : chainClients()) {
439
+ if (client->live(now)) {
440
+ live_ids.push_back(client->m_client_id);
441
+ }
442
+ }
443
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
444
+ // The highest live claim, so a server hiding blocks cannot end the wait early
445
+ int tip_height{0};
446
+ for (int client_id : live_ids) {
447
+ auto it = m_server_tip_heights.find(client_id);
448
+ if (it != m_server_tip_heights.end() && !tipUnbacked(client_id, it->second, now)) {
449
+ tip_height = std::max(tip_height, it->second);
450
+ }
451
+ }
452
+ return !m_anchor_disputed && m_best_block && tip_height > 0 &&
453
+ m_best_block->m_height >= tip_height;
454
+ }
455
+
456
+ BlockIndex *CoinInterface::newChainAnchor(int height, const uint256 &chain_work,
457
+ std::shared_ptr<BlockHeader> header,
458
+ std::shared_ptr<BlockHeader> parent)
459
+ {
460
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
461
+ if (m_anchor_block) {
462
+ LOGW("Chain anchor already set");
463
+ return nullptr;
464
+ }
465
+ uint256 block_hash;
466
+ if (0 != header->getBlockHash(block_hash)) {
467
+ LOGI("newChainAnchor getBlockHash failed");
468
+ return nullptr;
469
+ }
470
+
471
+ if (!checkBlockHeader(height, header)) {
472
+ LOGE("newChainAnchor: %s Block header %s - Failed validation", coinTicker(),
473
+ block_hash.ToString().c_str());
474
+ return nullptr;
475
+ }
476
+
477
+ const int interval = m_chain_params->retargetInterval();
478
+ const bool retargets = interval > 0 && !m_chain_params->m_pow_no_retargeting;
479
+ if (retargets && height % interval != 0) {
480
+ LOGE("New chain anchor %s: %d does not start a retarget period", coinTicker(), height);
481
+ return nullptr;
482
+ }
483
+ uint256 parent_hash;
484
+ if (height > 0 && parent) {
485
+ const auto p_prev_hash = header->getPrevHash();
486
+ if (!p_prev_hash || 0 != parent->getBlockHash(parent_hash) ||
487
+ parent_hash != uint256(p_prev_hash, 32)) {
488
+ parent_hash.SetNull();
489
+ }
490
+ }
491
+ if (retargets && height > 0 && parent_hash.IsNull()) {
492
+ LOGE("New chain anchor %s: %d, parent header missing or not its parent", coinTicker(),
493
+ height);
494
+ return nullptr;
495
+ }
496
+
497
+ if (m_chain_params->m_assume_valid_height > 0) {
498
+ // The glue may anchor above the shipped checkpoint but never below it, and
499
+ // never a different block at the checkpoint height: a lying glue cannot move
500
+ // the root earlier or swap the pinned block.
501
+ if (height < m_chain_params->m_assume_valid_height) {
502
+ LOGE("New chain anchor %s: %d below the pinned height %d", coinTicker(), height,
503
+ m_chain_params->m_assume_valid_height);
504
+ return nullptr;
505
+ }
506
+ if (height == m_chain_params->m_assume_valid_height &&
507
+ block_hash != m_chain_params->m_assume_valid_hash) {
508
+ LOGE("New chain anchor %s: %d is not the pinned block %s", coinTicker(), height,
509
+ m_chain_params->m_assume_valid_hash.ToString().c_str());
510
+ return nullptr;
511
+ }
512
+ // The claim must at least cover the checkpoint plus the anchor's own block,
513
+ // whose work is derived from its bits rather than taken on the server's word.
514
+ const arith_uint256 min_work = UintToArith256(m_chain_params->m_minimum_chainwork);
515
+ const arith_uint256 anchor_work = GetBlockProof(header->getBits());
516
+ if (UintToArith256(chain_work) < min_work + anchor_work) {
517
+ LOGE("New chain anchor %s: %d, %s chainwork below minimum %s plus the anchor's work %s",
518
+ coinTicker(), height, block_hash.ToString().c_str(),
519
+ ArithToUint256(min_work).ToString().c_str(),
520
+ ArithToUint256(anchor_work).ToString().c_str());
521
+ return nullptr;
522
+ }
523
+ // The target can ease by at most 4x per retarget, so over the gap from the
524
+ // checkpoint the anchor's work per block can't fall below the checkpoint's
525
+ // divided by that. Skipped where the min-difficulty rule applies (testnet),
526
+ // which can drop the target without a retarget.
527
+ // The height is the glue's claim, so the easing stops at two retargets.
528
+ if (m_chain_params->m_assume_valid_bits > 0 && interval > 0 &&
529
+ !m_chain_params->m_pow_allow_min_difficulty_blocks) {
530
+ const int retargets =
531
+ height / interval - m_chain_params->m_assume_valid_height / interval;
532
+ arith_uint256 checkpoint_work = GetBlockProof(m_chain_params->m_assume_valid_bits);
533
+ checkpoint_work >>= 2 * std::min(retargets, 2);
534
+ if (anchor_work < checkpoint_work) {
535
+ LOGE("New chain anchor %s: %d work %s below the checkpoint's %s after %d "
536
+ "retargets",
537
+ coinTicker(), height, ArithToUint256(anchor_work).ToString().c_str(),
538
+ ArithToUint256(checkpoint_work).ToString().c_str(), retargets);
539
+ return nullptr;
540
+ }
541
+ }
542
+ }
543
+
544
+ BlockIndex block_index;
545
+ block_index.m_header = header;
546
+
547
+ auto [mi, inserted] = m_block_index.try_emplace(block_hash, std::move(block_index));
548
+ if (!inserted) {
549
+ LOGD("Have %s block %s", coinTicker(), block_hash.ToString().c_str());
550
+ if (mi->second.m_chain_work.bits() == 0) {
551
+ LOGD("Updating chainwork");
552
+ mi->second.m_chain_work = UintToArith256(chain_work);
553
+ }
554
+ writeBlockIndexToDB(mi->second, true);
555
+ return &mi->second;
556
+ }
557
+ LOGI("New chain anchor %s: %d, %s", coinTicker(), height, block_hash.ToString().c_str());
558
+
559
+ mi->second.m_height = height;
560
+ mi->second.m_pblock_hash = &(mi->first);
561
+
562
+ mi->second.m_chain_work = UintToArith256(chain_work);
563
+ if (!m_best_block || mi->second.m_chain_work > m_best_block->m_chain_work) {
564
+ // TODO: Disconnect blocks
565
+ m_best_block = &mi->second;
566
+ }
567
+ m_anchor_block = &mi->second;
568
+
569
+ const BlockIndex *new_parent = nullptr;
570
+ if (!parent_hash.IsNull()) {
571
+ auto [pi, parent_inserted] = m_block_index.try_emplace(parent_hash);
572
+ if (parent_inserted) {
573
+ pi->second.m_header = parent;
574
+ pi->second.m_height = height - 1;
575
+ pi->second.m_pblock_hash = &pi->first;
576
+ new_parent = &pi->second;
577
+ }
578
+ mi->second.m_prev = &pi->second;
579
+ }
580
+ if (!m_loading_block_index && m_db) {
581
+ const bool open_here = !m_db->isDBOpen();
582
+ if (open_here && 0 != m_db->openDB()) {
583
+ LOGE("newChainAnchor %s %d, DB open failed", coinTicker(), height);
584
+ } else {
585
+ int rv = m_db->savepoint("chain_anchor");
586
+ if (rv == 0) {
587
+ rv |= writeBlockIndexToDB(mi->second, true);
588
+ if (new_parent) {
589
+ rv |= writeBlockIndexToDB(*new_parent);
590
+ }
591
+ if (rv != 0 || 0 != m_db->releaseSavepoint("chain_anchor")) {
592
+ m_db->rollbackToSavepoint("chain_anchor");
593
+ }
594
+ }
595
+ if (open_here) {
596
+ m_db->closeDB();
597
+ }
598
+ }
599
+ }
600
+
601
+ if (m_pending_tip_height > height) {
602
+ requestBlockHeaders(height + 1, m_pending_tip_height - height);
603
+ }
604
+ m_pending_tip_height = 0;
605
+
606
+ return &mi->second;
607
+ }
608
+
609
+ BlockIndex *CoinInterface::newBlockHeader(int height, std::shared_ptr<BlockHeader> header)
610
+ {
611
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
612
+ uint256 block_hash;
613
+ if (0 != header->getBlockHash(block_hash)) {
614
+ LOGI("newChainAnchor getBlockHash failed");
615
+ return nullptr;
616
+ }
617
+
618
+ const auto p_prev_hash = header->getPrevHash();
619
+ if (!p_prev_hash) {
620
+ LOGW("Could not get prev block hash");
621
+ return nullptr;
622
+ }
623
+ uint256 i_prev_block_hash(p_prev_hash, 32);
624
+
625
+ BlockIndex *block_index =
626
+ insertBlockHeader(height, header, block_hash, i_prev_block_hash, false);
627
+ if (!m_loading_block_index) {
628
+ requestStakeProofs();
629
+ }
630
+ return block_index;
631
+ }
632
+
633
+ BlockIndex *CoinInterface::newBlockHeaderDBLock(int height, std::shared_ptr<BlockHeader> header)
634
+ {
635
+ if (!m_db) {
636
+ LOGE("DB is not set");
637
+ return nullptr;
638
+ }
639
+ BSX_SCOPED_LOCK lock(m_db->m_mtx_db);
640
+ return newBlockHeader(height, header);
641
+ }
642
+
643
+ BlockIndex *CoinInterface::newChainAnchorDBLock(int height, const uint256 &chain_work,
644
+ std::shared_ptr<BlockHeader> header,
645
+ std::shared_ptr<BlockHeader> parent)
646
+ {
647
+ if (!m_db) {
648
+ LOGE("DB is not set");
649
+ return nullptr;
650
+ }
651
+ BSX_SCOPED_LOCK lock(m_db->m_mtx_db);
652
+ return newChainAnchor(height, chain_work, header, parent);
653
+ }
654
+
655
+ int CoinInterface::createOwnershipProofTx(std::vector<unsigned char> &tx_out,
656
+ const std::vector<SwapInput> &inputs,
657
+ const unsigned char *commitment) const
658
+ {
659
+ if (inputs.empty() || !inputs[0].m_output) {
660
+ return 1;
661
+ }
662
+ auto tx = getTransaction();
663
+ tx->setVersion(txVersion());
664
+ tx->setLocktime(OWNERSHIP_PROOF_LOCKTIME);
665
+ int64_t total_in{0};
666
+ for (const auto &input : inputs) {
667
+ auto txin = tx->makeInput();
668
+ if (!input.m_output || 0 != txin->setPrevout(input.m_outpoint.get())) {
669
+ return 1;
670
+ }
671
+ txin->setSequence(0xfffffffe);
672
+ tx->m_vin.push_back(std::move(txin));
673
+ total_in += input.m_output->value();
674
+ }
675
+ auto commitment_out = tx->makeOutput();
676
+ commitment_out->setValue(0);
677
+ *commitment_out->script() = {0x6a, 0x20};
678
+ commitment_out->script()->insert(commitment_out->script()->end(), commitment, commitment + 32);
679
+ tx->m_vout.push_back(std::move(commitment_out));
680
+ auto return_out = tx->makeOutput();
681
+ return_out->setValue(total_in);
682
+ *return_out->script() = *inputs[0].m_output->script();
683
+ tx->m_vout.push_back(std::move(return_out));
684
+ return tx->to_bytes(tx_out, false, false) > 0 ? 0 : 1;
685
+ }
686
+
687
+ BlockIndex *CoinInterface::getBlockIndex(const uint256 &block_hash)
688
+ {
689
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
690
+
691
+ auto it = m_block_index.find(block_hash);
692
+ if (it != m_block_index.end()) {
693
+ return &it->second;
694
+ }
695
+ return nullptr;
696
+ }
697
+
698
+ BlockIndex *CoinInterface::getBestChainBlockIndex(const uint256 &block_hash)
699
+ {
700
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
701
+
702
+ auto it = m_block_index.find(block_hash);
703
+ if (it == m_block_index.end() || blockIndexAtHeight(it->second.m_height) != &it->second) {
704
+ return nullptr;
705
+ }
706
+ return &it->second;
707
+ }
708
+
709
+ BlockIndex *CoinInterface::getBlockIndexAtHeight(int height)
710
+ {
711
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
712
+ return blockIndexAtHeight(height);
713
+ }
714
+
715
+ BlockIndex *CoinInterface::blockIndexAtHeight(int height)
716
+ {
717
+
718
+ // Linear walk from the tip; misses stop at the anchor. The returned pointer
719
+ // stays valid after the lock drops because m_block_index is node-based,
720
+ // same convention as getBlockIndex.
721
+ BlockIndex *pindex = m_best_block;
722
+ while (pindex && pindex->m_height > height) {
723
+ pindex = pindex->m_prev;
724
+ }
725
+ if (!pindex || pindex->m_height != height) {
726
+ return nullptr;
727
+ }
728
+ return pindex;
729
+ }
730
+
731
+ static std::string failedBlocksKey(int coin_type)
732
+ {
733
+ return strprintf("failed_blocks_%d", coin_type);
734
+ }
735
+
736
+ void CoinInterface::loadFailedBlocks()
737
+ {
738
+ if (m_failed_blocks_loaded || !m_db) {
739
+ return;
740
+ }
741
+ m_failed_blocks_loaded = true;
742
+ const bool open_here = !m_db->isDBOpen();
743
+ if (open_here && 0 != m_db->openDB()) {
744
+ LOGE("loadFailedBlocks %s: DB open failed", coinTicker());
745
+ return;
746
+ }
747
+ std::string text;
748
+ if (0 == m_db->getKVString(failedBlocksKey(coinType()), text)) {
749
+ const nlohmann::json j = nlohmann::json::parse(text, nullptr, false);
750
+ for (const auto &entry : j.is_array() ? j : nlohmann::json::array()) {
751
+ std::vector<unsigned char> hash;
752
+ if (entry.is_object() && 0 == decode_hex(hash, entry.value("hash", "")) &&
753
+ hash.size() == 32) {
754
+ m_failed_blocks[uint256(hash.data(), 32)] = entry.value("height", 0);
755
+ }
756
+ }
757
+ }
758
+ if (open_here) {
759
+ m_db->closeDB();
760
+ }
761
+ }
762
+
763
+ int CoinInterface::saveFailedBlocks()
764
+ {
765
+ if (!m_db) {
766
+ return 1;
767
+ }
768
+ const bool open_here = !m_db->isDBOpen();
769
+ if (open_here && 0 != m_db->openDB()) {
770
+ LOGE("saveFailedBlocks %s: DB open failed", coinTicker());
771
+ return 1;
772
+ }
773
+ nlohmann::json j = nlohmann::json::array();
774
+ for (const auto &[hash, height] : m_failed_blocks) {
775
+ j.push_back({{"hash", hex_str(hash.begin(), 32)}, {"height", height}});
776
+ }
777
+ const int rv = m_failed_blocks.empty()
778
+ ? m_db->deleteKVString(failedBlocksKey(coinType()))
779
+ : m_db->setKVString(failedBlocksKey(coinType()), j.dump());
780
+ if (open_here) {
781
+ m_db->closeDB();
782
+ }
783
+ return rv == 0 || m_failed_blocks.empty() ? 0 : 1;
784
+ }
785
+
786
+ bool CoinInterface::onFailedBranch(const BlockIndex *block_index)
787
+ {
788
+ loadFailedBlocks();
789
+ if (m_failed_blocks.empty()) {
790
+ return false;
791
+ }
792
+ int lowest = INT_MAX;
793
+ for (const auto &[hash, height] : m_failed_blocks) {
794
+ lowest = std::min(lowest, height);
795
+ }
796
+ for (const BlockIndex *p = block_index; p && p->m_height >= lowest; p = p->m_prev) {
797
+ if (p->m_pblock_hash && m_failed_blocks.count(*p->m_pblock_hash)) {
798
+ return true;
799
+ }
800
+ }
801
+ return false;
802
+ }
803
+
804
+ void CoinInterface::selectBestBlock()
805
+ {
806
+ BlockIndex *best{nullptr};
807
+ for (auto &[hash, index] : m_block_index) {
808
+ if (!index.m_pblock_hash || onFailedBranch(&index)) {
809
+ continue;
810
+ }
811
+ if (!best || index.m_chain_work > best->m_chain_work) {
812
+ best = &index;
813
+ }
814
+ }
815
+ if (!best || best == m_best_block) {
816
+ return;
817
+ }
818
+ // The stored rows are the best chain, one per height: rewrite them from where the chains meet
819
+ std::vector<BlockIndex *> connect;
820
+ for (BlockIndex *p = best; p; p = p->m_prev) {
821
+ BlockIndex *q = m_best_block;
822
+ while (q && q->m_height > p->m_height) {
823
+ q = q->m_prev;
824
+ }
825
+ if (q == p) {
826
+ break;
827
+ }
828
+ connect.push_back(p);
829
+ }
830
+ for (auto it = connect.rbegin(); it != connect.rend(); ++it) {
831
+ writeBlockIndexToDB(**it);
832
+ }
833
+ if (m_db && !m_loading_block_index) {
834
+ const bool open_here = !m_db->isDBOpen();
835
+ if (!open_here || 0 == m_db->openDB()) {
836
+ if (0 != m_db->deleteBlockIndicesAbove(coinType(), best->m_height)) {
837
+ LOGE("selectBestBlock %s: removing rows above %d failed", coinTicker(),
838
+ best->m_height);
839
+ }
840
+ if (open_here) {
841
+ m_db->closeDB();
842
+ }
843
+ }
844
+ }
845
+ LOGW("%s best block now %d %s", coinTicker(), best->m_height, best->getHashHex().c_str());
846
+ m_best_block = best;
847
+ }
848
+
849
+ int CoinInterface::markBlockFailed(const uint256 &block_hash)
850
+ {
851
+ loadFailedBlocks();
852
+ const auto it = m_block_index.find(block_hash);
853
+ if (it == m_block_index.end()) {
854
+ return 1;
855
+ }
856
+ if (!m_failed_blocks.emplace(block_hash, it->second.m_height).second) {
857
+ return 0;
858
+ }
859
+ LOGW("%s block %d %s failed, it and the blocks built on it can't be best", coinTicker(),
860
+ it->second.m_height, block_hash.ToString().c_str());
861
+ if (0 != saveFailedBlocks()) {
862
+ LOGE("markBlockFailed %s: storing the failed block failed", coinTicker());
863
+ }
864
+ if (m_best_block && onFailedBranch(m_best_block)) {
865
+ selectBestBlock();
866
+ }
867
+ return 0;
868
+ }
869
+
870
+ int CoinInterface::clearFailedBlocks()
871
+ {
872
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
873
+ loadFailedBlocks();
874
+ const size_t cleared = m_failed_blocks.size();
875
+ m_failed_blocks.clear();
876
+ if (0 != saveFailedBlocks()) {
877
+ return 1;
878
+ }
879
+ selectBestBlock();
880
+ failedBlocksCleared();
881
+ LOGI("%s cleared %zu failed blocks", coinTicker(), cleared);
882
+ return 0;
883
+ }
884
+
885
+ size_t CoinInterface::numFailedBlocks()
886
+ {
887
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
888
+ loadFailedBlocks();
889
+ return m_failed_blocks.size();
890
+ }
891
+
892
+ int CoinInterface::getMemStats(nlohmann::json &ji)
893
+ {
894
+ {
895
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
896
+ ji["block_index"] = m_block_index.size();
897
+ ji["block_header_requests"] = m_block_header_requests.size();
898
+ size_t pending_headers{0};
899
+ for (const auto &bhr : m_block_header_requests) {
900
+ pending_headers += bhr.second->m_pending_headers.size();
901
+ }
902
+ ji["pending_headers"] = pending_headers;
903
+ }
904
+
905
+ // Outside m_mtx_chain: the listener thread holds the electrum mutexes and
906
+ // reaches the chain lock from processNewHeaderMessage.
907
+ nlohmann::json j_servers = nlohmann::json::array();
908
+ for (const auto &client : electrumServers()) {
909
+ nlohmann::json j_client;
910
+ j_client["host"] = client->m_host;
911
+ {
912
+ BSX_SCOPED_LOCK lock(client->m_mtx_req);
913
+ j_client["requests"] = client->m_request_map.size();
914
+ }
915
+ {
916
+ BSX_SCOPED_LOCK lock(client->m_mtx_response);
917
+ j_client["responses"] = client->m_responses.size();
918
+ }
919
+ j_servers.push_back(j_client);
920
+ }
921
+ ji["electrum"] = j_servers;
922
+ return 0;
923
+ }
924
+
925
+ int CoinInterface::processChainTip(std::shared_ptr<ElectrumResponse> response)
926
+ {
927
+ // The reply to blockchain.headers.subscribe, carrying the tip as it was when the
928
+ // subscription was made. Every block after it arrives as a notification instead.
929
+ if (!response->m_jdata.contains("result")) {
930
+ LOGE("processChainTip %s: Malformed json response", coinTicker());
931
+ return 1;
932
+ }
933
+ const auto &jheader = response->m_jdata["result"];
934
+ if (!jheader.is_object() || !jheader.contains("height") ||
935
+ !jheader["height"].is_number_integer() || !jheader.contains("hex") ||
936
+ !jheader["hex"].is_string()) {
937
+ LOGE("processChainTip %s: Malformed block header", coinTicker());
938
+ return 1;
939
+ }
940
+ int height = jheader["height"].get<int>();
941
+ std::string block_hex = jheader["hex"].get<std::string>();
942
+ std::vector<unsigned char> block_data;
943
+
944
+ auto block_header = getBlockHeader();
945
+ if (0 != bsx::decode_hex(block_data, block_hex) ||
946
+ 0 >= block_header->from_bytes(block_data.data(), block_data.size())) {
947
+ LOGE("processChainTip %s: Failed to decode block header", coinTicker());
948
+ return 1;
949
+ }
950
+ LOGI("processChainTip %s: height %d", coinTicker(), height);
951
+
952
+ setServerTipHeight(response->m_client_id, height);
953
+ // The caller already holds the db lock, so not the DBLock variant.
954
+ newBlockHeader(height, block_header);
955
+ return 0;
956
+ }
957
+
958
+ int CoinInterface::processBlockHeaders(std::shared_ptr<ElectrumResponse> response)
959
+ {
960
+ LOGD("processBlockHeaders %s %d", coinTicker(), response->m_request_id);
961
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
962
+
963
+ try {
964
+ const auto &jdata = response->m_jdata["result"];
965
+ auto it_bhr = m_block_header_requests.find(response->m_request.m_watched_id);
966
+ if (it_bhr == m_block_header_requests.end()) {
967
+ throw std::runtime_error("Header request not found");
968
+ }
969
+ if (!jdata.contains("count") || !jdata.contains("hex")) {
970
+ throw std::runtime_error("Malformed json response");
971
+ }
972
+ int count = jdata["count"].get<int>();
973
+ LOGD("processBlockHeaders %s %d, count %d", coinTicker(), response->m_request_id, count);
974
+ if (count < 0 || count > it_bhr->second->m_count) {
975
+ throw std::runtime_error(
976
+ strprintf("Header count %d outside 0..%d", count, it_bhr->second->m_count));
977
+ }
978
+ std::string hex_data = jdata["hex"].get<std::string>();
979
+ std::vector<unsigned char> data;
980
+ if (0 != bsx::decode_hex(data, hex_data)) {
981
+ throw std::runtime_error("Failed to decode data");
982
+ }
983
+
984
+ int start_height = it_bhr->second->m_start_height;
985
+
986
+ uint256 last_hash;
987
+ int request_previous{-1};
988
+ int nb;
989
+ size_t o{0};
990
+ for (int i = 0; i < count; i++) {
991
+ int height = start_height + i;
992
+ auto bh = getBlockHeader();
993
+ nb = bh->from_bytes(data.data() + o, data.size() - o);
994
+ if (nb <= 0) {
995
+ throw std::runtime_error(
996
+ strprintf("Failed to decode block %d, height %d", i, height));
997
+ }
998
+ o += nb;
999
+ auto block_hash = bh->getHash();
1000
+
1001
+ const auto p_prev_hash = bh->getPrevHash();
1002
+ if (!p_prev_hash) {
1003
+ throw std::runtime_error(strprintf("Could not get prev block hash %d", i));
1004
+ }
1005
+ uint256 i_prev_block_hash(p_prev_hash, 32);
1006
+
1007
+ if (request_previous > -1) {
1008
+ it_bhr->second->m_pending_headers[height] = bh;
1009
+ continue;
1010
+ }
1011
+
1012
+ if (last_hash.IsNull()) {
1013
+ // First header
1014
+ auto prev_bi = m_block_index.find(i_prev_block_hash);
1015
+ if (prev_bi == m_block_index.end()) {
1016
+ LOGW("Previous block %s not found for start block %d",
1017
+ i_prev_block_hash.ToString().c_str(), height);
1018
+
1019
+ it_bhr->second->m_pending_headers[height] = bh;
1020
+ int anchor_height = m_anchor_block ? m_anchor_block->m_height : 0;
1021
+
1022
+ request_previous =
1023
+ std::max(anchor_height + 1, start_height - m_num_request_prev);
1024
+ if (start_height - request_previous <= 0) {
1025
+ throw std::runtime_error("TODO: re-anchor block chain");
1026
+ }
1027
+ }
1028
+ } else {
1029
+ if (i_prev_block_hash != last_hash) {
1030
+ throw std::runtime_error(strprintf("Non contiguous headers %d", i));
1031
+ }
1032
+ }
1033
+ last_hash = block_hash;
1034
+
1035
+ if (request_previous > -1) {
1036
+ it_bhr->second->m_pending_headers[height] = bh;
1037
+ continue;
1038
+ }
1039
+ auto block_index = insertBlockHeader(height, bh, block_hash, i_prev_block_hash, true);
1040
+ if (!block_index) {
1041
+ throw std::runtime_error(strprintf("Failed to insert block header %s %d",
1042
+ block_hash.ToString().c_str(), height));
1043
+ }
1044
+ }
1045
+
1046
+ if (o != data.size()) {
1047
+ throw std::runtime_error(
1048
+ strprintf("Header data runs %zu bytes past %d headers", data.size() - o, count));
1049
+ }
1050
+
1051
+ if (request_previous > -1) {
1052
+ int count = start_height - request_previous;
1053
+ LOGW("Requesting %d older %s blocks from height %d", count, coinTicker(),
1054
+ request_previous);
1055
+ auto ec = electrumClient();
1056
+ if (!ec) {
1057
+ throw std::runtime_error("Could not get electrum client");
1058
+ }
1059
+ auto block_req = it_bhr->second;
1060
+
1061
+ block_req->m_time = ec->getTime();
1062
+ block_req->m_start_height = request_previous;
1063
+ block_req->m_count = count;
1064
+ block_req->m_block_source_id = ec->m_client_id;
1065
+ ec->getBlockHeaders(it_bhr->first, block_req->m_start_height, count);
1066
+ return 0;
1067
+ }
1068
+
1069
+ for (const auto &[height, bh] : it_bhr->second->m_pending_headers) {
1070
+ auto block_hash = bh->getHash();
1071
+ const auto p_prev_hash = bh->getPrevHash();
1072
+ if (!p_prev_hash) {
1073
+ throw std::runtime_error(strprintf("Could not get prev block hash %d", height));
1074
+ }
1075
+ uint256 i_prev_block_hash(p_prev_hash, 32);
1076
+ if (i_prev_block_hash != last_hash) {
1077
+ throw std::runtime_error(strprintf("Non contiguous headers, height %d", height));
1078
+ }
1079
+ last_hash = block_hash;
1080
+ auto block_index = insertBlockHeader(height, bh, block_hash, i_prev_block_hash, true);
1081
+ if (!block_index) {
1082
+ throw std::runtime_error(strprintf("Failed to insert block header %s %d",
1083
+ block_hash.ToString().c_str(), height));
1084
+ }
1085
+ }
1086
+ requestStakeProofs();
1087
+ } catch (const std::exception &e) {
1088
+ LOGE("processBlockHeaders %s exception %s", coinTicker(), e.what());
1089
+ m_block_header_requests.erase(response->m_request.m_watched_id);
1090
+ return 1;
1091
+ }
1092
+
1093
+ m_block_header_requests.erase(response->m_request.m_watched_id);
1094
+ return 0;
1095
+ }
1096
+
1097
+ // The glue server's chainAnchorHeight offset, rounded down as newChainAnchor requires
1098
+ int CoinInterface::anchorHeightForTip(int tip_height) const
1099
+ {
1100
+ const int height = std::max(tip_height - 11, 0);
1101
+ const int interval = m_chain_params->retargetInterval();
1102
+ const bool retargets = interval > 0 && !m_chain_params->m_pow_no_retargeting;
1103
+ return retargets ? height - height % interval : height;
1104
+ }
1105
+
1106
+ int CoinInterface::anchorHeight()
1107
+ {
1108
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
1109
+ return m_anchor_block ? m_anchor_block->m_height : -1;
1110
+ }
1111
+
1112
+ bool CoinInterface::anchorBlock(int &height, uint256 &hash)
1113
+ {
1114
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
1115
+ if (!m_anchor_block || !m_anchor_block->m_pblock_hash) {
1116
+ return false;
1117
+ }
1118
+ height = m_anchor_block->m_height;
1119
+ hash = *m_anchor_block->m_pblock_hash;
1120
+ return true;
1121
+ }
1122
+
1123
+ void CoinInterface::setAnchorDisputed(bool disputed)
1124
+ {
1125
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
1126
+ m_anchor_disputed = disputed;
1127
+ }
1128
+
1129
+ int CoinInterface::loadBlockIndex()
1130
+ {
1131
+
1132
+ if (m_height_lowest_active == -1) {
1133
+ // Nothing to do - get new anchor header from GS
1134
+ // TODO: Remove old index?
1135
+ return 0;
1136
+ }
1137
+ LOGI("loadBlockIndex %s lowest block height of active swaps %d", coinTicker(),
1138
+ m_height_lowest_active);
1139
+
1140
+ // Get highest anchor header below m_height_lowest_active
1141
+
1142
+ if (!m_db) {
1143
+ LOGE("DB is not set");
1144
+ return 1;
1145
+ }
1146
+ BSX_SCOPED_LOCK lock(m_db->m_mtx_db);
1147
+ if (0 != m_db->openDB()) {
1148
+ LOGE("DB open failed");
1149
+ return 1;
1150
+ }
1151
+ BlockIndexData bi_data;
1152
+ int rv = m_db->getAnchorHeader(bi_data, coinType(), m_height_lowest_active);
1153
+ if (rv != 0 || bi_data.m_header_data.empty() || bi_data.m_chain_work.size() != 32) {
1154
+ m_db->closeDB();
1155
+ LOGE("getAnchorHeader failed");
1156
+ return 1;
1157
+ }
1158
+
1159
+ auto block_header = getBlockHeader();
1160
+ if (0 >= block_header->from_bytes(bi_data.m_header_data.data(), bi_data.m_header_data.size())) {
1161
+ m_db->closeDB();
1162
+ LOGE("Failed to decode block header");
1163
+ return 1;
1164
+ }
1165
+
1166
+ uint256 chain_work(bi_data.m_chain_work.data(), 32);
1167
+ const int anchor_height = bi_data.m_block_height;
1168
+
1169
+ DBCursor cursor;
1170
+ QueryOptions query_opts;
1171
+ std::shared_ptr<BlockHeader> parent_header;
1172
+ query_opts.m_filters = {FilterData("block_height", ComparisonTypes::GTE, anchor_height - 1),
1173
+ FilterData("block_height", ComparisonTypes::LT, anchor_height)};
1174
+ if (anchor_height > 0 && 0 == m_db->prepareBlockIndexQuery(cursor, coinType(), query_opts) &&
1175
+ 0 == m_db->stepBlockIndexQuery(cursor, bi_data)) {
1176
+ parent_header = getBlockHeader();
1177
+ if (0 >=
1178
+ parent_header->from_bytes(bi_data.m_header_data.data(), bi_data.m_header_data.size())) {
1179
+ parent_header.reset();
1180
+ }
1181
+ }
1182
+ m_db->closeCursor(cursor);
1183
+
1184
+ m_loading_block_index = true;
1185
+
1186
+ BlockIndex *anchor = newChainAnchor(anchor_height, chain_work, block_header, parent_header);
1187
+ if (!anchor) {
1188
+ m_db->closeDB();
1189
+ m_loading_block_index = false;
1190
+ LOGE("Failed to set chain anchor");
1191
+ return 1;
1192
+ }
1193
+
1194
+ query_opts.m_filters = {FilterData("block_height", ComparisonTypes::GT, anchor_height)};
1195
+ query_opts.m_sorts = {SortData("block_height", SortTypes::ASC)};
1196
+ rv = m_db->prepareBlockIndexQuery(cursor, coinType(), query_opts);
1197
+ if (rv != 0) {
1198
+ LOGE("prepareBlockIndexQuery failed");
1199
+ m_db->closeCursor(cursor);
1200
+ m_db->closeDB();
1201
+ m_loading_block_index = false;
1202
+ return 1;
1203
+ }
1204
+ int headers_inserted{1}; // Count anchor
1205
+ while (0 == m_db->stepBlockIndexQuery(cursor, bi_data)) {
1206
+ auto block_header = getBlockHeader();
1207
+ if (0 >=
1208
+ block_header->from_bytes(bi_data.m_header_data.data(), bi_data.m_header_data.size())) {
1209
+ LOGE("failed to decode block header");
1210
+ continue;
1211
+ }
1212
+ auto block_index = newBlockHeader(bi_data.m_block_height, block_header);
1213
+ if (!block_index) {
1214
+ continue;
1215
+ }
1216
+ headers_inserted++;
1217
+ }
1218
+ LOGI("Loaded %d %s block headers", headers_inserted, coinTicker());
1219
+
1220
+ m_db->closeCursor(cursor);
1221
+ m_db->closeDB();
1222
+ m_loading_block_index = false;
1223
+ return 0;
1224
+ }
1225
+
1226
+ int CoinInterface::lowerAnchorHeight(int height) const
1227
+ {
1228
+ const int interval = m_chain_params->retargetInterval();
1229
+ const bool retargets = interval > 0 && !m_chain_params->m_pow_no_retargeting;
1230
+ int lower = retargets ? height - height % interval : height;
1231
+ const int pinned = m_chain_params->m_assume_valid_height;
1232
+ if (pinned > 0 && lower < pinned) {
1233
+ lower = retargets ? (pinned + interval - 1) / interval * interval : pinned;
1234
+ }
1235
+ return std::max(lower, 1);
1236
+ }
1237
+
1238
+ int CoinInterface::extendAnchorDown(int height,
1239
+ const std::vector<std::shared_ptr<BlockHeader>> &headers)
1240
+ {
1241
+ std::vector<BlockIndex *> nodes;
1242
+ {
1243
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
1244
+ BlockIndex *anchor = m_anchor_block;
1245
+ if (!anchor || !anchor->m_header || height < 1) {
1246
+ return 1;
1247
+ }
1248
+ const int anchor_height = anchor->m_height;
1249
+ if (height >= anchor_height) {
1250
+ return 0;
1251
+ }
1252
+ if ((int)headers.size() != anchor_height - height + 1) {
1253
+ LOGE("extendAnchorDown %s: %zu headers for %d to %d", coinTicker(), headers.size(),
1254
+ height - 1, anchor_height - 1);
1255
+ return 1;
1256
+ }
1257
+ std::vector<uint256> hashes(headers.size());
1258
+ for (size_t i = 0; i < headers.size(); ++i) {
1259
+ if (!headers[i] || 0 != headers[i]->getBlockHash(hashes[i])) {
1260
+ return 1;
1261
+ }
1262
+ }
1263
+ const auto links = [](const BlockHeader &child, const uint256 &parent_hash) {
1264
+ const uint8_t *prev = child.getPrevHash();
1265
+ return prev && uint256(prev, 32) == parent_hash;
1266
+ };
1267
+ if (!links(*anchor->m_header, hashes.back())) {
1268
+ LOGE("extendAnchorDown %s: the headers end below another block than the anchor's",
1269
+ coinTicker());
1270
+ return 1;
1271
+ }
1272
+ for (size_t i = headers.size() - 1; i > 0; --i) {
1273
+ if (!links(*headers[i], hashes[i - 1])) {
1274
+ LOGE("extendAnchorDown %s: header %d is not %d's parent", coinTicker(),
1275
+ height - 1 + (int)i - 1, height - 1 + (int)i);
1276
+ return 1;
1277
+ }
1278
+ }
1279
+ // Each block's chain work is its child's less the child's own work
1280
+ std::vector<arith_uint256> work(headers.size());
1281
+ arith_uint256 child_work = anchor->m_chain_work;
1282
+ std::shared_ptr<BlockHeader> child = anchor->m_header;
1283
+ for (size_t i = headers.size() - 1; i >= 1; --i) {
1284
+ const arith_uint256 proof = GetBlockProof(child->getBits());
1285
+ if (child_work <= proof) {
1286
+ LOGE("extendAnchorDown %s: the anchor's chain work is below its parents'",
1287
+ coinTicker());
1288
+ return 1;
1289
+ }
1290
+ child_work -= proof;
1291
+ work[i] = child_work;
1292
+ child = headers[i];
1293
+ }
1294
+ for (size_t i = 0; i < headers.size(); ++i) {
1295
+ const int node_height = height - 1 + (int)i;
1296
+ auto [it, inserted] = m_block_index.try_emplace(hashes[i]);
1297
+ BlockIndex &node = it->second;
1298
+ if (inserted) {
1299
+ node.m_header = headers[i];
1300
+ node.m_height = node_height;
1301
+ node.m_pblock_hash = &it->first;
1302
+ } else if (node.m_height != node_height) {
1303
+ return 1;
1304
+ }
1305
+ nodes.push_back(&node);
1306
+ }
1307
+ nodes[0]->m_prev = nullptr;
1308
+ for (size_t i = 1; i < nodes.size(); ++i) {
1309
+ nodes[i]->m_chain_work = work[i];
1310
+ nodes[i]->m_prev = nodes[i - 1];
1311
+ }
1312
+ anchor->m_prev = nodes.back();
1313
+ m_anchor_block = nodes[1];
1314
+ LOGI("New chain anchor %s: %d, below %d", coinTicker(), height, anchor_height);
1315
+ }
1316
+ if (!m_db) {
1317
+ LOGE("extendAnchorDown %s, DB is not set", coinTicker());
1318
+ return 1;
1319
+ }
1320
+ const bool open_here = !m_db->isDBOpen();
1321
+ if (open_here && 0 != m_db->openDB()) {
1322
+ LOGE("extendAnchorDown %s, DB open failed", coinTicker());
1323
+ return 1;
1324
+ }
1325
+ int rv = m_db->savepoint("anchor_down");
1326
+ if (rv == 0) {
1327
+ // The anchor row last: without it a restart roots where it did before
1328
+ for (size_t i = 0; rv == 0 && i < nodes.size(); ++i) {
1329
+ if (i != 1) {
1330
+ rv = writeBlockIndexToDB(*nodes[i]);
1331
+ }
1332
+ }
1333
+ if (rv == 0) {
1334
+ rv = writeBlockIndexToDB(*nodes[1], true);
1335
+ }
1336
+ if (rv != 0 || 0 != m_db->releaseSavepoint("anchor_down")) {
1337
+ m_db->rollbackToSavepoint("anchor_down");
1338
+ rv = 1;
1339
+ }
1340
+ }
1341
+ if (open_here) {
1342
+ m_db->closeDB();
1343
+ }
1344
+ return rv;
1345
+ }
1346
+
1347
+ int CoinInterface::resetChainState()
1348
+ {
1349
+ if (m_chain_type != ChainParams::REGTEST) {
1350
+ LOGE("resetChainState: %s is not on regtest", coinTicker());
1351
+ return 1;
1352
+ }
1353
+
1354
+ LOGI("Resetting %s chain state", coinTicker());
1355
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
1356
+
1357
+ m_best_block = nullptr;
1358
+ m_anchor_block = nullptr;
1359
+ m_anchor_disputed = false;
1360
+ m_block_index.clear();
1361
+ m_block_header_requests.clear();
1362
+ m_block_request_count = 0;
1363
+ m_loading_block_index = false;
1364
+ m_height_lowest_active = -1;
1365
+
1366
+ return 0;
1367
+ }
1368
+
1369
+ void CoinInterface::checkBlockRequests(int64_t now)
1370
+ {
1371
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
1372
+ int64_t max_ttl = 5 * 60;
1373
+
1374
+ for (auto it = m_block_header_requests.begin(); it != m_block_header_requests.end();) {
1375
+ if (it->second->m_time + max_ttl < now) {
1376
+ LOGW("%s, Expiring headers request from height %d, source %d", coinTicker(),
1377
+ it->second->m_start_height, it->second->m_block_source_id);
1378
+ m_block_header_requests.erase(it++);
1379
+ } else {
1380
+ ++it;
1381
+ }
1382
+ }
1383
+ }
1384
+
1385
+ std::vector<std::shared_ptr<ElectrumClient>> CoinInterface::electrumServers() const
1386
+ {
1387
+ BSX_SCOPED_LOCK lock(m_mtx_servers);
1388
+ return m_electrum_servers;
1389
+ }
1390
+
1391
+ void CoinInterface::addElectrumServer(std::shared_ptr<ElectrumClient> client)
1392
+ {
1393
+ BSX_SCOPED_LOCK lock(m_mtx_servers);
1394
+ m_electrum_servers.push_back(std::move(client));
1395
+ }
1396
+
1397
+ void CoinInterface::setNodeClient(std::shared_ptr<NodeClient> client)
1398
+ {
1399
+ BSX_SCOPED_LOCK lock(m_mtx_servers);
1400
+ m_node_client = std::move(client);
1401
+ }
1402
+
1403
+ std::shared_ptr<NodeClient> CoinInterface::nodeClient() const
1404
+ {
1405
+ BSX_SCOPED_LOCK lock(m_mtx_servers);
1406
+ return m_node_client;
1407
+ }
1408
+
1409
+ std::vector<std::shared_ptr<ChainClient>> CoinInterface::chainClients() const
1410
+ {
1411
+ BSX_SCOPED_LOCK lock(m_mtx_servers);
1412
+ if (m_node_client) {
1413
+ return {m_node_client};
1414
+ }
1415
+ return std::vector<std::shared_ptr<ChainClient>>(m_electrum_servers.begin(),
1416
+ m_electrum_servers.end());
1417
+ }
1418
+
1419
+ bool CoinInterface::tipUnbacked(int client_id, int claimed, int64_t now)
1420
+ {
1421
+ const int best = m_best_block ? m_best_block->m_height : -1;
1422
+ auto it = m_server_ahead.find(client_id);
1423
+ if (it == m_server_ahead.end()) {
1424
+ if (claimed <= best) {
1425
+ return false;
1426
+ }
1427
+ it = m_server_ahead.emplace(client_id, ServerAhead{now, claimed}).first;
1428
+ }
1429
+ ServerAhead &ahead = it->second;
1430
+ ahead.m_highest = std::max(ahead.m_highest, claimed);
1431
+ if (best < ahead.m_highest) {
1432
+ ahead.m_backed_since = -1;
1433
+ } else if (ahead.m_backed_since < 0) {
1434
+ ahead.m_backed_since = now;
1435
+ } else if (now - ahead.m_backed_since >= m_backed_tip_hold) {
1436
+ m_server_ahead.erase(it);
1437
+ return false;
1438
+ }
1439
+ return now - ahead.m_since >= m_unbacked_tip_timeout;
1440
+ }
1441
+
1442
+ bool CoinInterface::serverUnbacked(int client_id, int64_t now)
1443
+ {
1444
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
1445
+ auto it = m_server_tip_heights.find(client_id);
1446
+ return it != m_server_tip_heights.end() && tipUnbacked(client_id, it->second, now);
1447
+ }
1448
+
1449
+ void CoinInterface::forgetServerTip(int client_id)
1450
+ {
1451
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
1452
+ m_server_tip_heights.erase(client_id);
1453
+ }
1454
+
1455
+ std::set<int> CoinInterface::unbackedServers(int64_t now)
1456
+ {
1457
+ std::set<int> unbacked;
1458
+ for (const auto &[client_id, claimed] : m_server_tip_heights) {
1459
+ if (tipUnbacked(client_id, claimed, now)) {
1460
+ unbacked.insert(client_id);
1461
+ }
1462
+ }
1463
+ return unbacked;
1464
+ }
1465
+
1466
+ ChainClient *CoinInterface::getElectrumClient()
1467
+ {
1468
+ if (auto node = nodeClient()) {
1469
+ return node.get();
1470
+ }
1471
+ int best_height;
1472
+ std::map<int, int> tips;
1473
+ std::set<int> unbacked;
1474
+ {
1475
+ BSX_SCOPED_LOCK lock(m_mtx_chain);
1476
+ best_height = m_best_block ? m_best_block->m_height : -1;
1477
+ tips = m_server_tip_heights;
1478
+ unbacked = unbackedServers(m_db ? m_db->getTime() : 0);
1479
+ }
1480
+ return pickElectrumClient(best_height, tips, unbacked);
1481
+ }
1482
+
1483
+ ChainClient *CoinInterface::electrumClient()
1484
+ {
1485
+ if (auto node = nodeClient()) {
1486
+ return node.get();
1487
+ }
1488
+ return pickElectrumClient(m_best_block ? m_best_block->m_height : -1, m_server_tip_heights,
1489
+ unbackedServers(m_db ? m_db->getTime() : 0));
1490
+ }
1491
+
1492
+ ElectrumClient *CoinInterface::pickElectrumClient(int best_height, const std::map<int, int> &tips,
1493
+ const std::set<int> &unbacked)
1494
+ {
1495
+ const auto servers = electrumServers();
1496
+ if (servers.empty()) {
1497
+ LOGW("getElectrumClient: No server for coin %s", coinTicker());
1498
+ return nullptr;
1499
+ }
1500
+ if (!m_db) {
1501
+ return servers[0].get();
1502
+ }
1503
+ const int64_t now = m_db->getTime();
1504
+ int primary_id;
1505
+ {
1506
+ BSX_SCOPED_LOCK lock(m_mtx_servers);
1507
+ primary_id = m_primary_client_id;
1508
+ }
1509
+ const auto live = [&](const std::shared_ptr<ElectrumClient> &client) {
1510
+ return client->live(now);
1511
+ };
1512
+ const auto healthy = [&](const std::shared_ptr<ElectrumClient> &client) {
1513
+ if (!client->live(now) || client->failing(now) || unbacked.count(client->m_client_id) ||
1514
+ now - client->m_time_last_timeout <=
1515
+ client->m_ping_interval + client->m_request_timeout) {
1516
+ return false;
1517
+ }
1518
+ auto tip = tips.find(client->m_client_id);
1519
+ return tip == tips.end() || tip->second + m_max_server_lag >= best_height;
1520
+ };
1521
+ const auto active = [](const std::shared_ptr<ElectrumClient> &client) {
1522
+ return client->active();
1523
+ };
1524
+ const auto any = [](const std::shared_ptr<ElectrumClient> &) { return true; };
1525
+ const auto choose = [&](const auto &usable) {
1526
+ auto chosen = std::find_if(servers.begin(), servers.end(), [&](const auto &client) {
1527
+ return client->m_client_id == primary_id && usable(client);
1528
+ });
1529
+ return chosen != servers.end() ? chosen
1530
+ : std::find_if(servers.begin(), servers.end(), usable);
1531
+ };
1532
+ auto chosen = choose(healthy);
1533
+ if (chosen == servers.end()) {
1534
+ chosen = choose(live);
1535
+ }
1536
+ if (chosen == servers.end()) {
1537
+ chosen = choose(active);
1538
+ }
1539
+ if (chosen == servers.end()) {
1540
+ chosen = choose(any);
1541
+ }
1542
+ if ((*chosen)->m_client_id != primary_id) {
1543
+ LOGI("%s electrum server %s in use", coinTicker(), (*chosen)->m_url.c_str());
1544
+ BSX_SCOPED_LOCK lock(m_mtx_servers);
1545
+ m_primary_client_id = (*chosen)->m_client_id;
1546
+ }
1547
+ return chosen->get();
1548
+ }
1549
+
1550
+ void CoinInterface::finalise()
1551
+ {
1552
+ for (auto &client : chainClients()) {
1553
+ client->stop();
1554
+ }
1555
+ }
1556
+
1557
+ int CoinInterface::requestBlockHeaders(int start_height, int count, int pending_height,
1558
+ std::shared_ptr<BlockHeader> pending_header)
1559
+ {
1560
+ auto ec = electrumClient();
1561
+ if (!ec) {
1562
+ LOGE("Could not get electrum client");
1563
+ return 1;
1564
+ }
1565
+
1566
+ auto block_req = std::make_shared<BlockHeadersRequest>();
1567
+ block_req->m_time = ec->getTime();
1568
+ block_req->m_start_height = start_height;
1569
+ block_req->m_count = count;
1570
+ block_req->m_block_source_id = ec->m_client_id;
1571
+ if (pending_header) {
1572
+ block_req->m_pending_headers[pending_height] = pending_header;
1573
+ }
1574
+ int block_req_id = m_block_request_count++;
1575
+ m_block_header_requests[block_req_id] = block_req;
1576
+ return ec->getBlockHeaders(block_req_id, start_height, count);
1577
+ }
1578
+
1579
+ BlockIndex *CoinInterface::insertBlockHeader(int height, std::shared_ptr<BlockHeader> header,
1580
+ const uint256 &block_hash, const uint256 &prev_hash,
1581
+ bool processing_batch)
1582
+ {
1583
+ // Called from newBlockHeader or processBlockHeaders
1584
+
1585
+ if (!checkBlockHeader(height, header)) {
1586
+ LOGE("insertBlockHeader: %s Block header %s - Failed validation", coinTicker(),
1587
+ block_hash.ToString().c_str());
1588
+ return nullptr;
1589
+ }
1590
+
1591
+ BlockIndex block_index;
1592
+ block_index.m_header = header;
1593
+
1594
+ if (prev_hash.IsNull() && height == 0) {
1595
+ // Genesis block
1596
+ block_index.m_chain_work = GetBlockProof(header->getBits());
1597
+ } else {
1598
+ auto prev_bi = m_block_index.find(prev_hash);
1599
+ if (prev_bi == m_block_index.end()) {
1600
+ LOGW("Could not find %s prev block %s in block index for block %s, height %d",
1601
+ coinTicker(), prev_hash.ToString().c_str(), block_hash.ToString().c_str(), height);
1602
+
1603
+ if (processing_batch == false) {
1604
+ if (!m_anchor_block) {
1605
+ // A request rooted at genesis cannot connect to the anchor that
1606
+ // follows it, so wait and record what to catch up to.
1607
+ if (height > m_pending_tip_height) {
1608
+ m_pending_tip_height = height;
1609
+ }
1610
+ LOGD("No %s chain anchor yet, not requesting block %d", coinTicker(), height);
1611
+ return nullptr;
1612
+ }
1613
+ int tip_height = m_best_block ? m_best_block->m_height : 0;
1614
+ int start_height = tip_height + 1;
1615
+ int count{0};
1616
+ if (tip_height >= height) {
1617
+ // LOGW("Not requesting %s block height %d less than tip %d", coinTicker(),
1618
+ // height, tip_height); return nullptr;
1619
+
1620
+ start_height = height - 1;
1621
+ int anchor_height = m_anchor_block ? m_anchor_block->m_height : 0;
1622
+ int request_previous =
1623
+ std::max(anchor_height + 1, start_height - m_num_request_prev);
1624
+ count = start_height - request_previous;
1625
+ if (count <= 0) {
1626
+ //throw std::runtime_error("TODO: re-anchor block chain");
1627
+ LOGE("TODO: re-anchor block chain");
1628
+ return nullptr;
1629
+ }
1630
+ } else {
1631
+ // Check if block appends to existing request
1632
+ for (auto &[req_id, block_req] : m_block_header_requests) {
1633
+ auto it = block_req->m_pending_headers.rbegin();
1634
+ if (it != block_req->m_pending_headers.rend()) {
1635
+ if (it->second->getHash() == prev_hash) {
1636
+ LOGD("Appending block %d %s, to request %d", height,
1637
+ block_hash.ToString().c_str(), req_id);
1638
+ block_req->m_pending_headers[height] = header;
1639
+ return nullptr;
1640
+ }
1641
+ }
1642
+ int req_height = block_req->m_start_height + block_req->m_count +
1643
+ block_req->m_pending_headers.size();
1644
+ if (start_height < req_height) {
1645
+ start_height = req_height;
1646
+ }
1647
+ }
1648
+
1649
+ count = height - start_height;
1650
+ if (count <= 0) {
1651
+ LOGD("Not requesting %s block %d, covered by a pending request",
1652
+ coinTicker(), height);
1653
+ return nullptr;
1654
+ }
1655
+ }
1656
+ requestBlockHeaders(start_height, count, height, header);
1657
+ }
1658
+ return nullptr;
1659
+ }
1660
+ if (prev_bi->second.m_height + 1 != height) {
1661
+ LOGE("insertBlockHeader: %s Block header %s at height %d, parent at %d", coinTicker(),
1662
+ block_hash.ToString().c_str(), height, prev_bi->second.m_height);
1663
+ return nullptr;
1664
+ }
1665
+ if (!checkBlockHeaderContext(prev_bi->second, header)) {
1666
+ LOGE("insertBlockHeader: %s Block header %s - Failed contextual validation",
1667
+ coinTicker(), block_hash.ToString().c_str());
1668
+ return nullptr;
1669
+ }
1670
+ block_index.m_prev = &prev_bi->second;
1671
+ block_index.m_chain_work = prev_bi->second.m_chain_work + GetBlockProof(header->getBits());
1672
+ }
1673
+
1674
+ auto [mi, inserted] = m_block_index.try_emplace(block_hash, std::move(block_index));
1675
+
1676
+ if (!inserted) {
1677
+ LOGD("Have %s block %s", coinTicker(), block_hash.ToString().c_str());
1678
+ return &mi->second;
1679
+ }
1680
+ auto &inserted_index = mi->second;
1681
+ if (!m_loading_block_index) {
1682
+ LOGI("New block header %s: %d, %s, work: %s", coinTicker(), height,
1683
+ block_hash.ToString().c_str(),
1684
+ ArithToUint256(mi->second.m_chain_work).ToString().c_str());
1685
+ }
1686
+ inserted_index.m_height = height;
1687
+ inserted_index.m_pblock_hash = &(mi->first);
1688
+
1689
+ if (!onFailedBranch(&inserted_index) &&
1690
+ (!m_best_block || inserted_index.m_chain_work > m_best_block->m_chain_work)) {
1691
+ auto old_best_block = m_best_block;
1692
+ auto new_best_block = &inserted_index;
1693
+ // LOGD("Old %s best work %s - %d %s", coinTicker(),
1694
+ // ArithToUint256(old_best_block->m_chain_work).ToString().c_str(),
1695
+ // old_best_block->m_height, old_best_block->getHashHex().c_str()); LOGD("New %s best work
1696
+ // %s - %d %s", coinTicker(),
1697
+ // ArithToUint256(new_best_block->m_chain_work).ToString().c_str(),
1698
+ // new_best_block->m_height, new_best_block->getHashHex().c_str());
1699
+
1700
+ if (old_best_block && old_best_block->m_pblock_hash &&
1701
+ *(old_best_block->m_pblock_hash) != prev_hash) {
1702
+ LOGW("%s chain reorg detected", coinTicker());
1703
+ LOGD("Old best %s block %d %s", coinTicker(), old_best_block->m_height,
1704
+ old_best_block->getHashHex().c_str());
1705
+ std::vector<BlockIndex *> new_chain;
1706
+ // Check if old best block is still in the main chain
1707
+ // Walk chains back to the same height, compare hashes, walk new and old chains back to
1708
+ // find fork
1709
+ auto new_prev_index = new_best_block->m_prev;
1710
+ auto old_prev_index = old_best_block;
1711
+ while (old_prev_index && old_prev_index->m_height > new_prev_index->m_height) {
1712
+ LOGW("Disconnecting %s block %d %s", coinTicker(), old_prev_index->m_height,
1713
+ old_prev_index->getHashHex().c_str());
1714
+ old_prev_index = old_prev_index->m_prev;
1715
+ }
1716
+
1717
+ while (new_prev_index) {
1718
+ if (new_prev_index->m_height == old_prev_index->m_height) {
1719
+ if (new_prev_index->m_pblock_hash &&
1720
+ *(new_prev_index->m_pblock_hash) != *(old_prev_index->m_pblock_hash)) {
1721
+
1722
+ while (old_prev_index) {
1723
+ LOGW("Disconnecting %s block %d %s", coinTicker(),
1724
+ old_prev_index->m_height, old_prev_index->getHashHex().c_str());
1725
+
1726
+ new_chain.push_back(new_prev_index);
1727
+ old_prev_index = old_prev_index->m_prev;
1728
+ new_prev_index = new_prev_index->m_prev;
1729
+
1730
+ if (!new_prev_index || !old_prev_index) {
1731
+ break;
1732
+ }
1733
+ if (new_prev_index == old_prev_index) {
1734
+ LOGD("Fork after block %d %s", new_prev_index->m_height,
1735
+ new_prev_index->getHashHex().c_str());
1736
+ break;
1737
+ }
1738
+ }
1739
+ } else {
1740
+ // old best_block is still in main chain
1741
+ break;
1742
+ }
1743
+ break;
1744
+ }
1745
+ new_chain.push_back(new_prev_index);
1746
+ new_prev_index = new_prev_index->m_prev;
1747
+ }
1748
+ for (auto it = new_chain.rbegin(); it != new_chain.rend(); ++it) {
1749
+ LOGW("Connecting %s block %d %s", coinTicker(), (*it)->m_height,
1750
+ (*it)->getHashHex().c_str());
1751
+ writeBlockIndexToDB(**it);
1752
+ }
1753
+ }
1754
+ // LOGW("Connecting %s block %d %s", coinTicker(), (*it)->m_height,
1755
+ // (*it)->getHashHex().c_str());
1756
+ if (!m_loading_block_index) {
1757
+ LOGI("New best block header %s: %d, %s", coinTicker(), height,
1758
+ block_hash.ToString().c_str());
1759
+ }
1760
+ writeBlockIndexToDB(*new_best_block);
1761
+ m_best_block = new_best_block;
1762
+ }
1763
+
1764
+ return &inserted_index;
1765
+ }
1766
+
1767
+ int CoinInterface::writeBlockIndexToDB(const BlockIndex &block_index, bool is_anchor)
1768
+ {
1769
+ if (m_loading_block_index) {
1770
+ return 0;
1771
+ }
1772
+ BlockIndexData bi_data;
1773
+
1774
+ if (!m_db) {
1775
+ LOGE("writeBlockIndexToDB %s %d, DB is not set", coinTicker(), block_index.m_height);
1776
+ return 1;
1777
+ }
1778
+ const bool open_here = !m_db->isDBOpen();
1779
+ if (open_here && 0 != m_db->openDB()) {
1780
+ LOGE("writeBlockIndexToDB %s %d, DB open failed", coinTicker(), block_index.m_height);
1781
+ return 1;
1782
+ }
1783
+
1784
+ bi_data.m_block_height = block_index.m_height;
1785
+
1786
+ if (is_anchor) {
1787
+ auto chain_work = ArithToUint256(block_index.m_chain_work);
1788
+ bi_data.m_chain_work.assign(chain_work.data(), chain_work.data() + 32);
1789
+ }
1790
+ if (!block_index.m_header) {
1791
+ LOGE("writeBlockIndexToDB %s %d, header data missing", coinTicker(), block_index.m_height);
1792
+ if (open_here) {
1793
+ m_db->closeDB();
1794
+ }
1795
+ return 1;
1796
+ }
1797
+ bi_data.m_block_time = block_index.getBlockTime();
1798
+ int bytes_written = block_index.m_header->to_bytes(bi_data.m_header_data);
1799
+ if (bytes_written < block_index.m_header->get_bytes_size()) {
1800
+ LOGE("writeBlockIndexToDB %s %d, header serialisation failed", coinTicker(),
1801
+ block_index.m_height);
1802
+ if (open_here) {
1803
+ m_db->closeDB();
1804
+ }
1805
+ return 1;
1806
+ }
1807
+
1808
+ int rv = m_db->insertBlockIndex(coinType(), bi_data);
1809
+ if (open_here) {
1810
+ m_db->closeDB();
1811
+ }
1812
+ return rv;
1813
+ }
1814
+
1815
+ } // namespace bsx