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
package/cpp/bsx_db.cpp ADDED
@@ -0,0 +1,4951 @@
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 "bsx_db.h"
6
+ #include "bsx/types.h"
7
+ #include "coins/registry.h"
8
+ #include "contrib/sqlite3.h"
9
+ #include "schema.h"
10
+ #include "util/log.h"
11
+ #include "util/str.h"
12
+ #include "util/tinyformat.h"
13
+ #include <algorithm>
14
+ #include <cinttypes>
15
+ #include <cstring>
16
+ #include <string>
17
+
18
+ #include "util/thread.h"
19
+
20
+ namespace bsx {
21
+
22
+ const char *BSXDB::sqliteVersion() { return sqlite3_libversion(); }
23
+
24
+ int BSXDB::openDB()
25
+ {
26
+ assert(!isDBOpen());
27
+ sqlite3 *db{nullptr};
28
+ if (0 != db::open_db(&db, m_db_path, m_db_key)) {
29
+ return 1;
30
+ }
31
+ m_db = db;
32
+ return 0;
33
+ }
34
+
35
+ int BSXDB::closeDB()
36
+ {
37
+ int rc = sqlite3_close(m_db);
38
+ if (rc != SQLITE_OK) {
39
+ LOGE("closeDB: sqlite3_close failed (%d): %s, thread: %s", rc, sqlite3_errmsg(m_db),
40
+ thread_id_str().c_str());
41
+ return 1;
42
+ }
43
+ m_db = nullptr;
44
+ return 0;
45
+ }
46
+
47
+ int BSXDB::bindDataVector(sqlite3_stmt *stmt, size_t column_index,
48
+ const std::vector<unsigned char> &data)
49
+ {
50
+ return sqlite3_bind_blob(stmt, column_index, data.data(), data.size(), SQLITE_STATIC);
51
+ }
52
+
53
+ int BSXDB::bindDataVector(sqlite3_stmt *stmt, const char *column_name,
54
+ const std::vector<unsigned char> &data)
55
+ {
56
+ int column_index = sqlite3_bind_parameter_index(stmt, column_name);
57
+ if (column_index < 1) {
58
+ return 1;
59
+ }
60
+ return bindDataVector(stmt, column_index, data);
61
+ }
62
+
63
+ int BSXDB::bindInt(sqlite3_stmt *stmt, const char *column_name, int data)
64
+ {
65
+ int column_index = sqlite3_bind_parameter_index(stmt, column_name);
66
+ if (column_index < 1) {
67
+ return 1;
68
+ }
69
+ return sqlite3_bind_int(stmt, column_index, data);
70
+ }
71
+
72
+ int BSXDB::bindInt64(sqlite3_stmt *stmt, const char *column_name, int64_t data)
73
+ {
74
+ int column_index = sqlite3_bind_parameter_index(stmt, column_name);
75
+ if (column_index < 1) {
76
+ return 1;
77
+ }
78
+ return sqlite3_bind_int64(stmt, column_index, data);
79
+ }
80
+
81
+ int BSXDB::bindText(sqlite3_stmt *stmt, const char *column_name, const std::string &data)
82
+ {
83
+ int column_index = sqlite3_bind_parameter_index(stmt, column_name);
84
+ if (column_index < 1) {
85
+ return 1;
86
+ }
87
+ return sqlite3_bind_text(stmt, column_index, data.c_str(), -1, SQLITE_STATIC);
88
+ }
89
+
90
+ int BSXDB::setBool(sqlite3_stmt *stmt, const char *column_name, bool &data)
91
+ {
92
+ size_t num_cols = sqlite3_column_count(stmt);
93
+ for (size_t i = 0; i < num_cols; i++) {
94
+ const char *current_name = sqlite3_column_name(stmt, i);
95
+ if (current_name && strcmp(current_name, column_name) == 0) {
96
+ data = sqlite3_column_int(stmt, i);
97
+ return 0;
98
+ }
99
+ }
100
+ return 1;
101
+ }
102
+
103
+ int BSXDB::setDataVector(sqlite3_stmt *stmt, size_t column_index, std::vector<unsigned char> &data)
104
+ {
105
+ const void *data_p = sqlite3_column_blob(stmt, column_index);
106
+ size_t data_size = sqlite3_column_bytes(stmt, column_index);
107
+ data.resize(data_size);
108
+ if (data_size > 0) {
109
+ memcpy(data.data(), data_p, data_size);
110
+ }
111
+ return 0;
112
+ }
113
+
114
+ int BSXDB::setDataVector(sqlite3_stmt *stmt, const char *column_name,
115
+ std::vector<unsigned char> &data)
116
+ {
117
+ size_t num_cols = sqlite3_column_count(stmt);
118
+ for (size_t i = 0; i < num_cols; i++) {
119
+ const char *current_name = sqlite3_column_name(stmt, i);
120
+ if (current_name && strcmp(current_name, column_name) == 0) {
121
+ return setDataVector(stmt, i, data);
122
+ }
123
+ }
124
+ return 1;
125
+ }
126
+
127
+ int BSXDB::setString(sqlite3_stmt *stmt, size_t column_index, std::string &data)
128
+ {
129
+ const char *str = (const char *)sqlite3_column_text(stmt, column_index);
130
+ data.clear();
131
+ if (str) {
132
+ data = str;
133
+ }
134
+ return 0;
135
+ }
136
+
137
+ int BSXDB::setString(sqlite3_stmt *stmt, const char *column_name, std::string &data)
138
+ {
139
+ size_t num_cols = sqlite3_column_count(stmt);
140
+ for (size_t i = 0; i < num_cols; i++) {
141
+ const char *current_name = sqlite3_column_name(stmt, i);
142
+ if (current_name && strcmp(current_name, column_name) == 0) {
143
+ return setString(stmt, i, data);
144
+ }
145
+ }
146
+ return 1;
147
+ }
148
+
149
+ int BSXDB::setInt(sqlite3_stmt *stmt, const char *column_name, int &data)
150
+ {
151
+ size_t num_cols = sqlite3_column_count(stmt);
152
+ for (size_t i = 0; i < num_cols; i++) {
153
+ const char *current_name = sqlite3_column_name(stmt, i);
154
+ if (current_name && strcmp(current_name, column_name) == 0) {
155
+ data = sqlite3_column_int(stmt, i);
156
+ return 0;
157
+ }
158
+ }
159
+ return 1;
160
+ }
161
+
162
+ int BSXDB::setInt64(sqlite3_stmt *stmt, const char *column_name, int64_t &data)
163
+ {
164
+ size_t num_cols = sqlite3_column_count(stmt);
165
+ for (size_t i = 0; i < num_cols; i++) {
166
+ const char *current_name = sqlite3_column_name(stmt, i);
167
+ if (current_name && strcmp(current_name, column_name) == 0) {
168
+ data = sqlite3_column_int64(stmt, i);
169
+ return 0;
170
+ }
171
+ }
172
+ return 1;
173
+ }
174
+
175
+ int BSXDB::updateTableData(const char *table_name, const std::vector<FieldData> &field_data,
176
+ const std::vector<FilterData> &filters)
177
+ {
178
+
179
+ sqlite3_stmt *stmt;
180
+
181
+ std::string sql_query = strprintf("UPDATE %s SET ", table_name);
182
+
183
+ for (size_t i = 0; i < field_data.size(); i++) {
184
+ const auto &field = field_data[i];
185
+ sql_query += std::string(i ? "," : "") + field.m_column_name + " = :" + field.m_column_name;
186
+ }
187
+
188
+ std::string filter_columns;
189
+ if (0 != expandFilterColumns(filter_columns, filters)) {
190
+ LOGE("Failed to expand filter columns");
191
+ return 1;
192
+ }
193
+ sql_query += strprintf(" WHERE 1 = 1 %s", filter_columns);
194
+
195
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
196
+ if (rc != SQLITE_OK) {
197
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
198
+ return 1;
199
+ }
200
+ if (0 != bindFilterData(stmt, filters)) {
201
+ LOGE("Failed to bind filter data");
202
+ sqlite3_finalize(stmt);
203
+ return 1;
204
+ }
205
+
206
+ if (0 != bindRowData(stmt, field_data)) {
207
+ LOGE("Failed to bind row data");
208
+ sqlite3_finalize(stmt);
209
+ return 1;
210
+ }
211
+
212
+ rc = sqlite3_step(stmt);
213
+ sqlite3_finalize(stmt);
214
+ if (rc != SQLITE_DONE) {
215
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
216
+ return 1;
217
+ }
218
+ return 0;
219
+ }
220
+
221
+ int BSXDB::saveSwapRequest(int64_t &rowid, const db::SwapRequestData &data,
222
+ npb::SwapRequest &swap_request,
223
+ const std::vector<bsx::SwapInput> &swap_inputs)
224
+ {
225
+ sqlite3_stmt *stmt, *stmt_link;
226
+
227
+ const char *sql_insert =
228
+ "INSERT INTO swaprequests (protocol_version, created_at, expire_at, day_key_count, "
229
+ " swap_request_id, state, "
230
+ " coin_from, coin_to, amount_from, min_amount_to, script_to, change_script, spend_all,"
231
+ " fee_rate_from)"
232
+ " VALUES (@protocol_version, @created_at, @expire_at, @day_key_count, @swap_request_id, "
233
+ "@state, @coin_from, "
234
+ "@coin_to, @amount_from, @min_amount_to, @script_to, @change_script, @spend_all, "
235
+ "@fee_rate_from);";
236
+ int rc = sqlite3_prepare_v2(m_db, sql_insert, -1, &stmt, 0);
237
+ if (rc != SQLITE_OK) {
238
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
239
+ return 1;
240
+ }
241
+
242
+ sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@protocol_version"),
243
+ data.m_protocol_version);
244
+ sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@created_at"), data.m_created_at);
245
+ sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@expire_at"),
246
+ swap_request.expire_at);
247
+ sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@day_key_count"),
248
+ data.m_day_key_count);
249
+ sqlite3_bind_blob(stmt, sqlite3_bind_parameter_index(stmt, "@swap_request_id"),
250
+ data.m_request_id, 28, SQLITE_STATIC);
251
+ sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@state"), data.m_state);
252
+ sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@coin_from"),
253
+ swap_request.coin_type_from);
254
+ sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@coin_to"),
255
+ swap_request.coin_type_to);
256
+ sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@amount_from"),
257
+ swap_request.amount_from);
258
+ sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@min_amount_to"),
259
+ swap_request.min_amount_to);
260
+ sqlite3_bind_blob(stmt, sqlite3_bind_parameter_index(stmt, "@script_to"),
261
+ data.m_script_to.data(), data.m_script_to.size(), SQLITE_STATIC);
262
+ sqlite3_bind_blob(stmt, sqlite3_bind_parameter_index(stmt, "@change_script"),
263
+ data.m_change_script.data(), data.m_change_script.size(), SQLITE_STATIC);
264
+ sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@spend_all"),
265
+ swap_request.spend_all != 0);
266
+ sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@fee_rate_from"),
267
+ swap_request.fee_rate_from);
268
+ int rv = 0;
269
+ rc = sqlite3_step(stmt);
270
+ if (rc != SQLITE_DONE) {
271
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
272
+ rv = 1;
273
+ }
274
+
275
+ sqlite3_finalize(stmt);
276
+
277
+ if (rv != 0) {
278
+ return rv;
279
+ }
280
+
281
+ rowid = sqlite3_last_insert_rowid(m_db);
282
+ const char *sql_output = "INSERT INTO outputs (coin_type, txid, n, value, script)"
283
+ " VALUES (@coin_type, @txid, @n, @value, @script);";
284
+ rc = sqlite3_prepare_v2(m_db, sql_output, -1, &stmt, 0);
285
+ if (rc != SQLITE_OK) {
286
+ LOGE("Failed to prepare output statement: %s", sqlite3_errmsg(m_db));
287
+ return 1;
288
+ }
289
+ const char *sql_link = "INSERT INTO outputlinks (output_id, link_type, "
290
+ "linked_type, linked_id)"
291
+ " VALUES ((SELECT last_insert_rowid()), @link_type, "
292
+ "@linked_type, @linked_id);";
293
+ rc = sqlite3_prepare_v2(m_db, sql_link, -1, &stmt_link, 0);
294
+ if (rc != SQLITE_OK) {
295
+ LOGE("Failed to prepare link statement: %s", sqlite3_errmsg(m_db));
296
+ return 1;
297
+ }
298
+
299
+ for (const auto &si : swap_inputs) {
300
+ sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@coin_type"),
301
+ swap_request.coin_type_from);
302
+
303
+ sqlite3_bind_blob(stmt, sqlite3_bind_parameter_index(stmt, "@txid"), si.m_outpoint->txid(),
304
+ 32, SQLITE_STATIC);
305
+ sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, "@n"), si.m_outpoint->vout());
306
+ sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "@value"),
307
+ si.m_output->value());
308
+
309
+ const auto vscript = si.m_output->script();
310
+ sqlite3_bind_blob(stmt, sqlite3_bind_parameter_index(stmt, "@script"), vscript->data(),
311
+ vscript->size(), SQLITE_STATIC);
312
+ if (sqlite3_step(stmt) != SQLITE_DONE) {
313
+ LOGE("Failed to execute statement: %s", sqlite3_errmsg(m_db));
314
+ rv = 1;
315
+ break;
316
+ }
317
+
318
+ sqlite3_bind_int(stmt_link, sqlite3_bind_parameter_index(stmt_link, "@link_type"), 0);
319
+ sqlite3_bind_int(stmt_link, sqlite3_bind_parameter_index(stmt_link, "@linked_type"),
320
+ Concepts::SWAP_REQUEST);
321
+ sqlite3_bind_int64(stmt_link, sqlite3_bind_parameter_index(stmt_link, "@linked_id"), rowid);
322
+
323
+ if (sqlite3_step(stmt_link) != SQLITE_DONE) {
324
+ LOGE("Failed to execute statement: %s", sqlite3_errmsg(m_db));
325
+ rv = 1;
326
+ break;
327
+ }
328
+
329
+ sqlite3_reset(stmt);
330
+ sqlite3_reset(stmt_link);
331
+ }
332
+
333
+ sqlite3_finalize(stmt);
334
+ sqlite3_finalize(stmt_link);
335
+ return rv;
336
+ }
337
+
338
+ int BSXDB::loadSwapRequest(db::SwapRequestData &data,
339
+ const std::vector<unsigned char> &swap_request_id,
340
+ const std::vector<std::string> *fields)
341
+ {
342
+ sqlite3_stmt *stmt;
343
+ std::string retrieve_columns;
344
+ if (fields) {
345
+ for (const auto &col_name : *fields) {
346
+ retrieve_columns += std::string(retrieve_columns.size() ? "," : "") + col_name;
347
+ }
348
+ } else {
349
+ const db::Table *swap_info_schema = db::get_table_schema("swaprequests");
350
+ if (!swap_info_schema) {
351
+ LOGE("Failed to get table definition: %s", "swaprequests");
352
+ return 2;
353
+ }
354
+ for (const auto &col : swap_info_schema->m_columns) {
355
+ bool skip = false;
356
+ for (const auto &ignore_column : {"id", "swap_request_id"}) {
357
+ if (strcmp(col.m_name.c_str(), ignore_column) == 0) {
358
+ skip = true;
359
+ break;
360
+ }
361
+ }
362
+ if (skip) {
363
+ continue;
364
+ }
365
+ retrieve_columns += std::string(retrieve_columns.size() ? "," : "") + col.m_name;
366
+ }
367
+ }
368
+
369
+ std::string sql = strprintf("SELECT %s FROM "
370
+ " swaprequests WHERE swap_request_id = ?;",
371
+ retrieve_columns);
372
+
373
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
374
+ if (rc != SQLITE_OK) {
375
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
376
+ return 1;
377
+ }
378
+ sqlite3_bind_blob(stmt, 1, swap_request_id.data(), swap_request_id.size(), SQLITE_STATIC);
379
+
380
+ int rv = 1;
381
+ rc = sqlite3_step(stmt);
382
+ if (rc == SQLITE_ROW) {
383
+ setInt(stmt, "protocol_version", data.m_protocol_version);
384
+ setInt64(stmt, "created_at", data.m_created_at);
385
+ setInt64(stmt, "expire_at", data.m_expire_at);
386
+ setInt(stmt, "day_key_count", data.m_day_key_count);
387
+ setInt(stmt, "state", data.m_state);
388
+ setDataVector(stmt, "script_to", data.m_script_to);
389
+ setDataVector(stmt, "change_script", data.m_change_script);
390
+
391
+ rv = 0;
392
+ } else if (rc == SQLITE_ERROR) {
393
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
394
+ } else if (rc == SQLITE_DONE) {
395
+ LOGW("No row found.");
396
+ rv = 2;
397
+ }
398
+ sqlite3_finalize(stmt);
399
+ return rv;
400
+ }
401
+
402
+ int BSXDB::removeSwapRequestOutputs(const std::vector<unsigned char> &swap_request_id)
403
+ {
404
+ // The outputs go before the links that find them, and both before the
405
+ // swaprequests row they join on.
406
+ const char *statements[] = {
407
+ "DELETE FROM outputs WHERE id IN ("
408
+ " SELECT outputlinks.output_id FROM outputlinks"
409
+ " JOIN swaprequests ON swaprequests.id = outputlinks.linked_id"
410
+ " WHERE outputlinks.linked_type = ? AND swaprequests.swap_request_id = ?);",
411
+ "DELETE FROM outputlinks WHERE id IN ("
412
+ " SELECT outputlinks.id FROM outputlinks"
413
+ " JOIN swaprequests ON swaprequests.id = outputlinks.linked_id"
414
+ " WHERE outputlinks.linked_type = ? AND swaprequests.swap_request_id = ?);",
415
+ };
416
+
417
+ for (const char *sql : statements) {
418
+ sqlite3_stmt *stmt;
419
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
420
+ if (rc != SQLITE_OK) {
421
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
422
+ return 1;
423
+ }
424
+ sqlite3_bind_int(stmt, 1, Concepts::SWAP_REQUEST);
425
+ sqlite3_bind_blob(stmt, 2, swap_request_id.data(), swap_request_id.size(), SQLITE_STATIC);
426
+ rc = sqlite3_step(stmt);
427
+ sqlite3_finalize(stmt);
428
+ if (rc != SQLITE_DONE) {
429
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
430
+ return 1;
431
+ }
432
+ }
433
+ return 0;
434
+ }
435
+
436
+ int BSXDB::loadSwapRequestOutputs(std::vector<bsx::SwapInput> &swap_inputs,
437
+ const std::vector<unsigned char> &swap_request_id)
438
+ {
439
+ sqlite3_stmt *stmt;
440
+ const char *sql_outputs =
441
+ "SELECT coin_type, txid, n, value, script FROM outputs "
442
+ " JOIN outputlinks ON outputs.id = outputlinks.output_id "
443
+ " JOIN swaprequests ON swaprequests.id = outputlinks.linked_id "
444
+ " WHERE outputlinks.linked_type = ? AND swaprequests.swap_request_id = ?;";
445
+ int rc = sqlite3_prepare_v2(m_db, sql_outputs, -1, &stmt, 0);
446
+ if (rc != SQLITE_OK) {
447
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
448
+ return 1;
449
+ }
450
+
451
+ sqlite3_bind_int(stmt, 1, Concepts::SWAP_REQUEST);
452
+ sqlite3_bind_blob(stmt, 2, swap_request_id.data(), swap_request_id.size(), SQLITE_STATIC);
453
+
454
+ int rv = 0;
455
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
456
+ int coin_type = sqlite3_column_int(stmt, 0);
457
+ auto outpoint = makeOutpoint(coin_type);
458
+ auto output = makeOutput(coin_type);
459
+ if (outpoint && output) {
460
+ const void *txid_data = sqlite3_column_blob(stmt, 1);
461
+ size_t txid_size = sqlite3_column_bytes(stmt, 1);
462
+ if (txid_size != 32) {
463
+ LOGE("Invalid txid size: %zu", txid_size);
464
+ rv = 1;
465
+ break;
466
+ }
467
+ memcpy(outpoint->txid_data(), txid_data, txid_size);
468
+ outpoint->setVout(sqlite3_column_int(stmt, 2));
469
+ output->setValue(sqlite3_column_int64(stmt, 3));
470
+ const void *script_data = sqlite3_column_blob(stmt, 4);
471
+ size_t script_size = sqlite3_column_bytes(stmt, 4);
472
+ std::vector<unsigned char> *script = output->script();
473
+ script->resize(script_size);
474
+ if (script_size) {
475
+ memcpy(script->data(), script_data, script_size);
476
+ }
477
+ SwapInput swap_input;
478
+ swap_input.m_outpoint = outpoint;
479
+ swap_input.m_output = output;
480
+ swap_inputs.push_back(swap_input);
481
+ } else {
482
+ LOGE("No primitives for output coin type: %d", coin_type);
483
+ rv = 1;
484
+ break;
485
+ }
486
+ }
487
+ if (rc != SQLITE_DONE) {
488
+ rv = 1;
489
+ LOGE("Selecting from \"%s\": %s", "outputs", sqlite3_errmsg(m_db));
490
+ }
491
+
492
+ sqlite3_finalize(stmt);
493
+ return rv;
494
+ }
495
+
496
+ int BSXDB::loadSwapRequestSwaps(std::vector<std::vector<unsigned char>> &swap_ids,
497
+ const std::vector<unsigned char> &swap_request_id)
498
+ {
499
+ sqlite3_stmt *stmt;
500
+ const char *sql_outputs = "SELECT swap_id FROM swapinfo "
501
+ " WHERE swapinfo.request_id = ?;";
502
+ int rc = sqlite3_prepare_v2(m_db, sql_outputs, -1, &stmt, 0);
503
+ if (rc != SQLITE_OK) {
504
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
505
+ return 1;
506
+ }
507
+
508
+ sqlite3_bind_blob(stmt, 1, swap_request_id.data(), swap_request_id.size(), SQLITE_STATIC);
509
+
510
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
511
+ const void *swap_id_data = sqlite3_column_blob(stmt, 0);
512
+ size_t swap_id_size = sqlite3_column_bytes(stmt, 0);
513
+ size_t i = swap_ids.size();
514
+ swap_ids.resize(swap_ids.size() + 1);
515
+ swap_ids[i].resize(swap_id_size);
516
+ if (swap_id_size) {
517
+ memcpy(swap_ids[i].data(), swap_id_data, swap_id_size);
518
+ }
519
+ }
520
+ if (rc != SQLITE_DONE) {
521
+ LOGE("Selecting from \"%s\": %s", "swapinfo", sqlite3_errmsg(m_db));
522
+ }
523
+
524
+ sqlite3_finalize(stmt);
525
+ return rc == SQLITE_DONE ? 0 : 1;
526
+ }
527
+
528
+ int BSXDB::updateSwapRequestState(const std::vector<unsigned char> &swap_request_id, int new_state)
529
+ {
530
+ sqlite3_stmt *stmt;
531
+
532
+ std::string sql_query =
533
+ "UPDATE swaprequests SET state = @state WHERE swap_request_id = @swap_request_id";
534
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
535
+ if (rc != SQLITE_OK) {
536
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
537
+ return 1;
538
+ }
539
+ if (0 != bindInt(stmt, "@state", new_state) ||
540
+ 0 != bindDataVector(stmt, "@swap_request_id", swap_request_id)) {
541
+ LOGE("Failed to bind parameters.");
542
+ sqlite3_finalize(stmt);
543
+ return 1;
544
+ }
545
+
546
+ int rv = 0;
547
+ rc = sqlite3_step(stmt);
548
+ if (rc != SQLITE_DONE) {
549
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
550
+ rv = 1;
551
+ }
552
+
553
+ sqlite3_finalize(stmt);
554
+ return rv;
555
+ }
556
+
557
+ int BSXDB::getExpiredIds(std::vector<RequestId> &request_ids,
558
+ std::vector<std::vector<unsigned char>> &swap_ids, int64_t now)
559
+ {
560
+ request_ids.clear();
561
+ swap_ids.clear();
562
+ sqlite3_stmt *stmt;
563
+
564
+ const char *sql_requests =
565
+ "SELECT swap_request_id FROM swaprequests r"
566
+ " WHERE expire_at > 0 AND expire_at <= @now"
567
+ " AND (state IN (@state_sent, @state_offer_received)"
568
+ " OR (state = @state_accept_sent AND NOT EXISTS (SELECT 1"
569
+ " FROM swapinfo s WHERE s.request_id = r.swap_request_id"
570
+ " AND s.state NOT IN (@swap_received, @swap_rejected, @swap_expired))));";
571
+ int rc = sqlite3_prepare_v2(m_db, sql_requests, -1, &stmt, 0);
572
+ if (rc != SQLITE_OK) {
573
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
574
+ return 1;
575
+ }
576
+ if (0 != bindInt64(stmt, "@now", now) ||
577
+ 0 != bindInt(stmt, "@state_sent", SwapRequestStates::SENT) ||
578
+ 0 != bindInt(stmt, "@state_offer_received", SwapRequestStates::OFFER_RECEIVED) ||
579
+ 0 != bindInt(stmt, "@state_accept_sent", SwapRequestStates::ACCEPT_SENT) ||
580
+ 0 != bindInt(stmt, "@swap_received", SwapStates::RECEIVED) ||
581
+ 0 != bindInt(stmt, "@swap_rejected", SwapStates::REJECTED) ||
582
+ 0 != bindInt(stmt, "@swap_expired", SwapStates::EXPIRED)) {
583
+ LOGE("Failed to bind parameters.");
584
+ sqlite3_finalize(stmt);
585
+ return 1;
586
+ }
587
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
588
+ RequestId request_id;
589
+ setDataVector(stmt, size_t{0}, request_id);
590
+ request_ids.push_back(std::move(request_id));
591
+ }
592
+ sqlite3_finalize(stmt);
593
+ if (rc != SQLITE_DONE) {
594
+ LOGE("Selecting from \"%s\": %s", "swaprequests", sqlite3_errmsg(m_db));
595
+ return 1;
596
+ }
597
+
598
+ // Only unaccepted offers: a swap at RECEIVING_ACC or beyond is acting on an
599
+ // acceptance and runs to an ending of its own, as in basicswap.
600
+ const char *sql_swaps = "SELECT swap_id FROM swapinfo"
601
+ " WHERE expire_at > 0 AND expire_at <= @now"
602
+ " AND state IN (@state_receiving, @state_received);";
603
+ rc = sqlite3_prepare_v2(m_db, sql_swaps, -1, &stmt, 0);
604
+ if (rc != SQLITE_OK) {
605
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
606
+ return 1;
607
+ }
608
+ if (0 != bindInt64(stmt, "@now", now) ||
609
+ 0 != bindInt(stmt, "@state_receiving", SwapStates::RECEIVING) ||
610
+ 0 != bindInt(stmt, "@state_received", SwapStates::RECEIVED)) {
611
+ LOGE("Failed to bind parameters.");
612
+ sqlite3_finalize(stmt);
613
+ return 1;
614
+ }
615
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
616
+ std::vector<unsigned char> swap_id;
617
+ setDataVector(stmt, size_t{0}, swap_id);
618
+ swap_ids.push_back(std::move(swap_id));
619
+ }
620
+ sqlite3_finalize(stmt);
621
+ if (rc != SQLITE_DONE) {
622
+ LOGE("Selecting from \"%s\": %s", "swapinfo", sqlite3_errmsg(m_db));
623
+ return 1;
624
+ }
625
+ return 0;
626
+ }
627
+
628
+ int BSXDB::getOpenRequestIds(std::vector<RequestId> &request_ids, int64_t now)
629
+ {
630
+ request_ids.clear();
631
+ sqlite3_stmt *stmt;
632
+
633
+ const char *sql = "SELECT swap_request_id FROM swaprequests"
634
+ " WHERE expire_at > @now"
635
+ " AND state IN (@state_sent, @state_offer_received);";
636
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
637
+ if (rc != SQLITE_OK) {
638
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
639
+ return 1;
640
+ }
641
+ if (0 != bindInt64(stmt, "@now", now) ||
642
+ 0 != bindInt(stmt, "@state_sent", SwapRequestStates::SENT) ||
643
+ 0 != bindInt(stmt, "@state_offer_received", SwapRequestStates::OFFER_RECEIVED)) {
644
+ LOGE("Failed to bind parameters.");
645
+ sqlite3_finalize(stmt);
646
+ return 1;
647
+ }
648
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
649
+ RequestId request_id;
650
+ setDataVector(stmt, size_t{0}, request_id);
651
+ request_ids.push_back(std::move(request_id));
652
+ }
653
+ sqlite3_finalize(stmt);
654
+ if (rc != SQLITE_DONE) {
655
+ LOGE("Selecting from \"%s\": %s", "swaprequests", sqlite3_errmsg(m_db));
656
+ return 1;
657
+ }
658
+ return 0;
659
+ }
660
+
661
+ int BSXDB::getDeadRequestIds(std::vector<RequestId> &request_ids, int64_t now)
662
+ {
663
+ request_ids.clear();
664
+ sqlite3_stmt *stmt;
665
+
666
+ const char *sql = "SELECT swap_request_id FROM swaprequests r"
667
+ " WHERE r.expire_at > @now AND r.state = @state_offer_received"
668
+ " AND NOT EXISTS (SELECT 1 FROM offers o"
669
+ " WHERE o.request_id = r.swap_request_id"
670
+ " AND o.state = @offer_received AND o.expire_at > @now)"
671
+ " AND NOT EXISTS (SELECT 1 FROM swapinfo s"
672
+ " WHERE s.request_id = r.swap_request_id"
673
+ " AND s.state NOT IN (@swap_rejected, @swap_expired));";
674
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
675
+ if (rc != SQLITE_OK) {
676
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
677
+ return 1;
678
+ }
679
+ if (0 != bindInt64(stmt, "@now", now) ||
680
+ 0 != bindInt(stmt, "@state_offer_received", SwapRequestStates::OFFER_RECEIVED) ||
681
+ 0 != bindInt(stmt, "@offer_received", OfferStates::RECEIVED) ||
682
+ 0 != bindInt(stmt, "@swap_rejected", SwapStates::REJECTED) ||
683
+ 0 != bindInt(stmt, "@swap_expired", SwapStates::EXPIRED)) {
684
+ LOGE("Failed to bind parameters.");
685
+ sqlite3_finalize(stmt);
686
+ return 1;
687
+ }
688
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
689
+ RequestId request_id;
690
+ setDataVector(stmt, size_t{0}, request_id);
691
+ request_ids.push_back(std::move(request_id));
692
+ }
693
+ sqlite3_finalize(stmt);
694
+ if (rc != SQLITE_DONE) {
695
+ LOGE("Selecting from \"%s\": %s", "swaprequests", sqlite3_errmsg(m_db));
696
+ return 1;
697
+ }
698
+ return 0;
699
+ }
700
+
701
+ int BSXDB::requestHasActiveSwaps(bool &active, const RequestId &request_id, bool unaccepted)
702
+ {
703
+ active = false;
704
+ sqlite3_stmt *stmt;
705
+
706
+ const char *sql =
707
+ "SELECT COUNT(*) FROM swapinfo WHERE request_id = @request_id"
708
+ " AND (state IN (SELECT state_ind FROM swapstates WHERE in_progress != 0)"
709
+ " OR (@unaccepted AND state IN (@sent, @receiving, @received, @receiving_acc)));";
710
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
711
+ if (rc != SQLITE_OK) {
712
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
713
+ return 1;
714
+ }
715
+ if (0 != bindDataVector(stmt, "@request_id", request_id) ||
716
+ 0 != bindInt(stmt, "@unaccepted", unaccepted ? 1 : 0) ||
717
+ 0 != bindInt(stmt, "@sent", SwapStates::SENT) ||
718
+ 0 != bindInt(stmt, "@receiving", SwapStates::RECEIVING) ||
719
+ 0 != bindInt(stmt, "@received", SwapStates::RECEIVED) ||
720
+ 0 != bindInt(stmt, "@receiving_acc", SwapStates::RECEIVING_ACC)) {
721
+ LOGE("Failed to bind parameters.");
722
+ sqlite3_finalize(stmt);
723
+ return 1;
724
+ }
725
+ int rv = 0;
726
+ if ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
727
+ active = sqlite3_column_int(stmt, 0) > 0;
728
+ } else {
729
+ LOGE("Selecting from \"%s\": %s", "swapinfo", sqlite3_errmsg(m_db));
730
+ rv = 1;
731
+ }
732
+ sqlite3_finalize(stmt);
733
+ return rv;
734
+ }
735
+
736
+ int BSXDB::hasOpenRequestLocks(bool &has_locks, int64_t now)
737
+ {
738
+ has_locks = false;
739
+ sqlite3_stmt *stmt;
740
+
741
+ const char *sql = "SELECT COUNT(*) FROM walletlocks l"
742
+ " JOIN swaprequests r ON r.swap_request_id = l.request_id"
743
+ " WHERE l.released = 0 AND r.expire_at > @now"
744
+ " AND (r.state IN (@state_sent, @state_offer_received)"
745
+ " OR (r.state = @state_accept_sent AND NOT EXISTS (SELECT 1"
746
+ " FROM swapinfo s WHERE s.request_id = r.swap_request_id"
747
+ " AND s.state NOT IN (@swap_received, @swap_rejected, @swap_expired))));";
748
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
749
+ if (rc != SQLITE_OK) {
750
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
751
+ return 1;
752
+ }
753
+ if (0 != bindInt64(stmt, "@now", now) ||
754
+ 0 != bindInt(stmt, "@state_sent", SwapRequestStates::SENT) ||
755
+ 0 != bindInt(stmt, "@state_offer_received", SwapRequestStates::OFFER_RECEIVED) ||
756
+ 0 != bindInt(stmt, "@state_accept_sent", SwapRequestStates::ACCEPT_SENT) ||
757
+ 0 != bindInt(stmt, "@swap_received", SwapStates::RECEIVED) ||
758
+ 0 != bindInt(stmt, "@swap_rejected", SwapStates::REJECTED) ||
759
+ 0 != bindInt(stmt, "@swap_expired", SwapStates::EXPIRED)) {
760
+ LOGE("Failed to bind parameters.");
761
+ sqlite3_finalize(stmt);
762
+ return 1;
763
+ }
764
+ int rv = 0;
765
+ if ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
766
+ has_locks = sqlite3_column_int(stmt, 0) > 0;
767
+ } else {
768
+ LOGE("Selecting from \"%s\": %s", "walletlocks", sqlite3_errmsg(m_db));
769
+ rv = 1;
770
+ }
771
+ sqlite3_finalize(stmt);
772
+ return rv;
773
+ }
774
+
775
+ int BSXDB::getSwapIdsForAcceptTimeout(std::vector<std::vector<unsigned char>> &swap_ids,
776
+ int64_t now, int64_t timeout, int64_t long_timeout)
777
+ {
778
+ sqlite3_stmt *stmt;
779
+ // Anchored on the state-change event, not expire_at, so the counterparty's
780
+ // chosen expiry cannot extend it. An initiating swap whose lock tx a has
781
+ // been broadcast never times out here: seen or not, it can still confirm, and
782
+ // only the refund path may end the swap.
783
+ // A swap awaiting lock sigs has nothing on chain, so its expire_at ends it.
784
+ const char *sql_query =
785
+ "SELECT s.swap_id FROM swapinfo s"
786
+ " WHERE (s.state = @awaiting AND s.expire_at <= @now)"
787
+ " OR (s.state = @accepted"
788
+ " AND NOT (s.initiating != 0 AND EXISTS(SELECT 1 FROM swaptxns t"
789
+ " WHERE t.swap_id = s.swap_id AND t.tx_type = @a_lock AND t.state >= @publishing))"
790
+ " AND COALESCE((SELECT MAX(e.created_at) FROM eventlog e"
791
+ " WHERE e.swap_id = s.swap_id AND e.event_type = @state_changed"
792
+ " AND e.event_sub_type = @accepted), s.created_at)"
793
+ " <= @now - (CASE"
794
+ " WHEN EXISTS(SELECT 1 FROM swaptxns t WHERE t.swap_id = s.swap_id"
795
+ " AND t.tx_type = @a_lock AND t.state >= @in_mempool) THEN @long_timeout"
796
+ " WHEN s.initiating != 0 THEN @long_timeout"
797
+ " ELSE @timeout END));";
798
+
799
+ int rc = sqlite3_prepare_v2(m_db, sql_query, -1, &stmt, 0);
800
+ if (rc != SQLITE_OK) {
801
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
802
+ return 1;
803
+ }
804
+ if (0 != bindInt(stmt, "@accepted", SwapStates::ACCEPTED) ||
805
+ 0 != bindInt(stmt, "@awaiting", SwapStates::AWAITING_LOCK_SIGS) ||
806
+ 0 != bindInt(stmt, "@a_lock", TxTypes::A_LOCK) ||
807
+ 0 != bindInt(stmt, "@in_mempool", TxStates::IN_MEMPOOL) ||
808
+ 0 != bindInt(stmt, "@publishing", TxStates::PUBLISHING) ||
809
+ 0 != bindInt(stmt, "@state_changed", EventLogTypes::STATE_CHANGED) ||
810
+ 0 != bindInt64(stmt, "@now", now) || 0 != bindInt64(stmt, "@timeout", timeout) ||
811
+ 0 != bindInt64(stmt, "@long_timeout", long_timeout)) {
812
+ LOGE("Failed to bind parameters.");
813
+ sqlite3_finalize(stmt);
814
+ return 1;
815
+ }
816
+
817
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
818
+ const void *swap_id_data = sqlite3_column_blob(stmt, 0);
819
+ size_t swap_id_size = sqlite3_column_bytes(stmt, 0);
820
+ size_t i = swap_ids.size();
821
+ swap_ids.resize(swap_ids.size() + 1);
822
+ swap_ids[i].resize(swap_id_size);
823
+ if (swap_id_size) {
824
+ memcpy(swap_ids[i].data(), swap_id_data, swap_id_size);
825
+ }
826
+ }
827
+ if (rc != SQLITE_DONE) {
828
+ LOGE("Selecting from \"%s\": %s", "swapinfo", sqlite3_errmsg(m_db));
829
+ }
830
+ sqlite3_finalize(stmt);
831
+ return rc == SQLITE_DONE ? 0 : 1;
832
+ }
833
+
834
+ int BSXDB::getSwapRequestList(std::vector<SwapRequestInfo> &requests,
835
+ const std::vector<unsigned char> *swap_request_id)
836
+ {
837
+ sqlite3_stmt *stmt;
838
+ std::string sql_query =
839
+ "SELECT protocol_version, swap_request_id, created_at, expire_at, state, coin_from,"
840
+ " coin_to, amount_from, min_amount_to, spend_all, fee_rate_from"
841
+ " FROM swaprequests";
842
+ if (swap_request_id) {
843
+ sql_query += " WHERE swap_request_id = ?";
844
+ } else {
845
+ sql_query += " ORDER BY created_at DESC";
846
+ }
847
+
848
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
849
+ if (rc != SQLITE_OK) {
850
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
851
+ return 1;
852
+ }
853
+ if (swap_request_id) {
854
+ sqlite3_bind_blob(stmt, 1, swap_request_id->data(), swap_request_id->size(), SQLITE_STATIC);
855
+ }
856
+
857
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
858
+ SwapRequestInfo request_info;
859
+ setInt(stmt, "protocol_version", request_info.m_protocol_version);
860
+ setDataVector(stmt, "swap_request_id", request_info.m_request_id);
861
+ setInt64(stmt, "created_at", request_info.m_created_at);
862
+ setInt64(stmt, "expire_at", request_info.m_expire_at);
863
+ setInt(stmt, "state", request_info.m_state);
864
+ setInt(stmt, "coin_from", request_info.m_coin_from);
865
+ setInt(stmt, "coin_to", request_info.m_coin_to);
866
+ setInt64(stmt, "amount_from", request_info.m_amount_from);
867
+ setInt64(stmt, "min_amount_to", request_info.m_min_amount_to);
868
+ int spend_all = 0;
869
+ setInt(stmt, "spend_all", spend_all);
870
+ request_info.m_spend_all = spend_all != 0;
871
+ setInt64(stmt, "fee_rate_from", request_info.m_fee_rate_from);
872
+ requests.push_back(std::move(request_info));
873
+ }
874
+ if (rc != SQLITE_DONE) {
875
+ LOGE("Selecting from \"%s\": %s", "swaprequests", sqlite3_errmsg(m_db));
876
+ }
877
+
878
+ sqlite3_finalize(stmt);
879
+ return rc == SQLITE_DONE ? 0 : 1;
880
+ }
881
+
882
+ int BSXDB::saveSwapInfo(int64_t &rowid, const SwapInfo &swap_info)
883
+ {
884
+ sqlite3_stmt *stmt;
885
+
886
+ std::string insert_columns, insert_values;
887
+ const db::Table *swap_info_schema = db::get_table_schema("swapinfo");
888
+ if (!swap_info_schema) {
889
+ LOGE("Failed to get table definition: %s", "swapinfo");
890
+ return 2;
891
+ }
892
+ for (const auto &col : swap_info_schema->m_columns) {
893
+ if (col.m_name == "id") {
894
+ continue;
895
+ }
896
+ insert_columns += std::string(insert_columns.size() ? "," : "") + col.m_name;
897
+ insert_values += std::string(insert_values.size() ? "," : "") + "@" + col.m_name;
898
+ }
899
+
900
+ std::string sql_insert = strprintf("INSERT INTO swapinfo (%s)"
901
+ " VALUES (%s) ",
902
+ insert_columns, insert_values);
903
+ sql_insert += " ON CONFLICT(swap_id) DO UPDATE SET ";
904
+ size_t upserted_columns{0};
905
+ for (const auto &col : swap_info_schema->m_columns) {
906
+ if (col.m_name == "id" || col.m_name == "swap_id") {
907
+ continue;
908
+ }
909
+ if (upserted_columns) {
910
+ sql_insert += ",";
911
+ }
912
+ sql_insert += col.m_name + "=excluded." + col.m_name;
913
+ upserted_columns++;
914
+ }
915
+
916
+ int rc = sqlite3_prepare_v2(m_db, sql_insert.c_str(), -1, &stmt, 0);
917
+ if (rc != SQLITE_OK) {
918
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
919
+ return 1;
920
+ }
921
+
922
+ bindSwapInfoData(stmt, swap_info);
923
+
924
+ int rv = 0;
925
+ rc = sqlite3_step(stmt);
926
+ if (rc != SQLITE_DONE) {
927
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
928
+ rv = 1;
929
+ }
930
+
931
+ sqlite3_finalize(stmt);
932
+ return rv;
933
+ }
934
+
935
+ int BSXDB::loadSwapInfo(SwapInfo &swap_info, const std::vector<unsigned char> &swap_id,
936
+ const std::vector<std::string> *fields)
937
+ {
938
+ sqlite3_stmt *stmt;
939
+
940
+ std::string retrieve_columns;
941
+ if (fields) {
942
+ for (const auto &col_name : *fields) {
943
+ retrieve_columns += std::string(retrieve_columns.size() ? "," : "") + col_name;
944
+ }
945
+ } else {
946
+ const db::Table *swap_info_schema = db::get_table_schema("swapinfo");
947
+ if (!swap_info_schema) {
948
+ LOGE("Failed to get table definition: %s", "swapinfo");
949
+ return 2;
950
+ }
951
+ for (const auto &col : swap_info_schema->m_columns) {
952
+ bool skip = false;
953
+ for (const auto &ignore_column : {"id", "swap_id"}) {
954
+ if (strcmp(col.m_name.c_str(), ignore_column) == 0) {
955
+ skip = true;
956
+ break;
957
+ }
958
+ }
959
+ if (skip) {
960
+ continue;
961
+ }
962
+ retrieve_columns += std::string(retrieve_columns.size() ? "," : "") + col.m_name;
963
+ }
964
+ }
965
+ std::string sql = strprintf("SELECT %s FROM swapinfo"
966
+ " WHERE swap_id = ?",
967
+ retrieve_columns);
968
+
969
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
970
+ if (rc != SQLITE_OK) {
971
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
972
+ return 1;
973
+ }
974
+ sqlite3_bind_blob(stmt, 1, swap_id.data(), swap_id.size(), SQLITE_STATIC);
975
+ int rv = 1;
976
+ rc = sqlite3_step(stmt);
977
+ if (rc == SQLITE_ROW) {
978
+ swap_info.m_swap_id = swap_id;
979
+ setInt(stmt, "protocol_version", swap_info.m_protocol_version);
980
+ setInt64(stmt, "created_at", swap_info.m_created_at);
981
+ setInt(stmt, "day_key_count", swap_info.m_day_key_count);
982
+ setInt64(stmt, "expire_at", swap_info.m_expire_at);
983
+ setDataVector(stmt, "request_id", swap_info.m_request_id);
984
+ setDataVector(stmt, "offer_id", swap_info.m_offer_id);
985
+ setInt(stmt, "state", swap_info.m_state);
986
+ setInt(stmt, "coin_from", swap_info.m_coin_from);
987
+ setInt(stmt, "coin_to", swap_info.m_coin_to);
988
+ setBool(stmt, "initiating", swap_info.m_initiating);
989
+ setBool(stmt, "participating", swap_info.m_participating);
990
+ setInt64(stmt, "amount_from", swap_info.m_amount_from);
991
+ setInt64(stmt, "amount_to", swap_info.m_amount_to);
992
+ setInt64(stmt, "fee_rate_from", swap_info.m_fee_rate_from);
993
+ setInt64(stmt, "fee_rate_to", swap_info.m_fee_rate_to);
994
+ setInt(stmt, "lock_type", swap_info.m_lock_type);
995
+ setInt(stmt, "lock_time_1", swap_info.m_lock_time_1);
996
+ setInt(stmt, "lock_time_2", swap_info.m_lock_time_2);
997
+ setDataVector(stmt, "Kal", swap_info.m_Kal);
998
+ setDataVector(stmt, "Kaf", swap_info.m_Kaf);
999
+ setDataVector(stmt, "Kbl", swap_info.m_Kbl);
1000
+ setDataVector(stmt, "Kbf", swap_info.m_Kbf);
1001
+ setDataVector(stmt, "Kbl_a", swap_info.m_Kbl_a);
1002
+ setDataVector(stmt, "Kbf_a", swap_info.m_Kbf_a);
1003
+ setDataVector(stmt, "lock_tx_a", swap_info.m_lock_tx_a);
1004
+ setDataVector(stmt, "lock_tx_a_script", swap_info.m_lock_tx_a_script);
1005
+ setInt(stmt, "lock_a_txo_n", swap_info.m_lock_a_txo_n);
1006
+ setDataVector(stmt, "lock_spend_tx_a", swap_info.m_lock_spend_tx_a);
1007
+ setDataVector(stmt, "lock_refund_tx_a", swap_info.m_lock_refund_tx_a);
1008
+ setDataVector(stmt, "lock_refund_spend_tx_a", swap_info.m_lock_refund_spend_tx_a);
1009
+ setDataVector(stmt, "change_script", swap_info.m_change_script);
1010
+ setDataVector(stmt, "script_to", swap_info.m_script_to);
1011
+ setDataVector(stmt, "lock_tx_spend_a_vout", swap_info.m_lock_tx_spend_a_vout);
1012
+ setDataVector(stmt, "lock_tx_spend_a_sig_f", swap_info.m_lock_tx_spend_a_sig_f);
1013
+ setDataVector(stmt, "lock_tx_spend_a_otves_l", swap_info.m_lock_tx_spend_a_otves_l);
1014
+ setDataVector(stmt, "lock_tx_refund_a_sig_l", swap_info.m_lock_tx_refund_a_sig_l);
1015
+ setDataVector(stmt, "lock_tx_refund_a_sig_f", swap_info.m_lock_tx_refund_a_sig_f);
1016
+ setDataVector(stmt, "lock_tx_refund_spend_a_otves_f",
1017
+ swap_info.m_lock_tx_refund_spend_a_otves_f);
1018
+ setInt(stmt, "chain_from_height", swap_info.m_chain_from_height);
1019
+ setInt(stmt, "chain_to_height", swap_info.m_chain_to_height);
1020
+ setDataVector(stmt, "fee_outputs", swap_info.m_fee_outputs);
1021
+ setDataVector(stmt, "kal_sig", swap_info.m_kal_sig);
1022
+ setDataVector(stmt, "lock_spend_a_fee_outputs", swap_info.m_lock_spend_a_fee_outputs);
1023
+ setDataVector(stmt, "lock_spend_b_fee_outputs", swap_info.m_lock_spend_b_fee_outputs);
1024
+ setInt64(stmt, "lock_b_scan_height", swap_info.m_lock_b_scan_height);
1025
+ setDataVector(stmt, "bsx_bid_id", swap_info.m_bsx_bid_id);
1026
+ setDataVector(stmt, "kbsl_dleag", swap_info.m_kbsl_dleag);
1027
+ rv = 0;
1028
+ } else if (rc == SQLITE_ERROR) {
1029
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
1030
+ } else if (rc == SQLITE_DONE) {
1031
+ LOGW("No row found.");
1032
+ rv = 2;
1033
+ }
1034
+ sqlite3_finalize(stmt);
1035
+ return rv;
1036
+ }
1037
+
1038
+ int BSXDB::updateSwapInfo(const SwapInfo &swap_info, const std::vector<std::string> *fields)
1039
+ {
1040
+ sqlite3_stmt *stmt;
1041
+
1042
+ std::string sql_query = "UPDATE swapinfo SET ";
1043
+ size_t updated_columns{0};
1044
+ if (fields) {
1045
+ for (const auto &col_name : *fields) {
1046
+ if (updated_columns) {
1047
+ sql_query += ",";
1048
+ }
1049
+ sql_query += col_name + "=@" + col_name;
1050
+ updated_columns++;
1051
+ }
1052
+ } else {
1053
+ const db::Table *swap_info_schema = db::get_table_schema("swapinfo");
1054
+ if (!swap_info_schema) {
1055
+ LOGE("Failed to get table definition: %s", "swapinfo");
1056
+ return 2;
1057
+ }
1058
+ for (const auto &col : swap_info_schema->m_columns) {
1059
+ if (col.m_name == "id" || col.m_name == "swap_id") {
1060
+ continue;
1061
+ }
1062
+ if (updated_columns) {
1063
+ sql_query += ",";
1064
+ }
1065
+ sql_query += col.m_name + "=@" + col.m_name;
1066
+ updated_columns++;
1067
+ }
1068
+ }
1069
+
1070
+ sql_query += " WHERE swap_id = @swap_id";
1071
+
1072
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
1073
+ if (rc != SQLITE_OK) {
1074
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1075
+ return 1;
1076
+ }
1077
+
1078
+ bindSwapInfoData(stmt, swap_info);
1079
+
1080
+ int rv = 0;
1081
+ rc = sqlite3_step(stmt);
1082
+ if (rc != SQLITE_DONE) {
1083
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
1084
+ rv = 1;
1085
+ }
1086
+
1087
+ sqlite3_finalize(stmt);
1088
+ return rv;
1089
+ }
1090
+
1091
+ int BSXDB::updateSwapState(const std::vector<unsigned char> &swap_id, int new_state)
1092
+ {
1093
+ sqlite3_stmt *stmt;
1094
+
1095
+ std::string sql_query = "UPDATE swapinfo SET state = @state WHERE swap_id = @swap_id";
1096
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
1097
+ if (rc != SQLITE_OK) {
1098
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1099
+ return 1;
1100
+ }
1101
+ if (0 != bindInt(stmt, "@state", new_state) || 0 != bindDataVector(stmt, "@swap_id", swap_id)) {
1102
+ LOGE("Failed to bind parameters.");
1103
+ sqlite3_finalize(stmt);
1104
+ return 1;
1105
+ }
1106
+
1107
+ int rv = 0;
1108
+ rc = sqlite3_step(stmt);
1109
+ if (rc != SQLITE_DONE) {
1110
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
1111
+ rv = 1;
1112
+ }
1113
+
1114
+ sqlite3_finalize(stmt);
1115
+ return rv;
1116
+ }
1117
+
1118
+ int BSXDB::getActiveSwapIds(std::vector<std::vector<unsigned char>> &swap_ids)
1119
+ {
1120
+ sqlite3_stmt *stmt;
1121
+ std::string sql_query = "SELECT swap_id FROM swapinfo WHERE swapinfo.state IN ";
1122
+ sql_query += " (SELECT state_ind FROM swapstates WHERE in_progress != 0)";
1123
+
1124
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
1125
+ if (rc != SQLITE_OK) {
1126
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1127
+ return 1;
1128
+ }
1129
+
1130
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1131
+ const void *swap_id_data = sqlite3_column_blob(stmt, 0);
1132
+ size_t swap_id_size = sqlite3_column_bytes(stmt, 0);
1133
+ size_t i = swap_ids.size();
1134
+ swap_ids.resize(swap_ids.size() + 1);
1135
+ swap_ids[i].resize(swap_id_size);
1136
+ if (swap_id_size) {
1137
+ memcpy(swap_ids[i].data(), swap_id_data, swap_id_size);
1138
+ }
1139
+ }
1140
+ if (rc != SQLITE_DONE) {
1141
+ LOGE("Selecting from \"%s\": %s", "swapinfo", sqlite3_errmsg(m_db));
1142
+ }
1143
+
1144
+ sqlite3_finalize(stmt);
1145
+ return rc == SQLITE_DONE ? 0 : 1;
1146
+ }
1147
+
1148
+ int BSXDB::getSwapList(std::vector<SwapInfo> &swaps)
1149
+ {
1150
+ sqlite3_stmt *stmt;
1151
+ std::string sql_query =
1152
+ "SELECT swap_id, created_at, state, coin_from, coin_to, amount_from, amount_to,"
1153
+ " initiating, participating, request_id, offer_id, lock_spend_a_fee_outputs,"
1154
+ " lock_spend_b_fee_outputs"
1155
+ " FROM swapinfo ORDER BY created_at DESC";
1156
+
1157
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
1158
+ if (rc != SQLITE_OK) {
1159
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1160
+ return 1;
1161
+ }
1162
+
1163
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1164
+ SwapInfo swap_info;
1165
+ const void *swap_id_data = sqlite3_column_blob(stmt, 0);
1166
+ size_t swap_id_size = sqlite3_column_bytes(stmt, 0);
1167
+ swap_info.m_swap_id.resize(swap_id_size);
1168
+ if (swap_id_size) {
1169
+ memcpy(swap_info.m_swap_id.data(), swap_id_data, swap_id_size);
1170
+ }
1171
+ swap_info.m_created_at = sqlite3_column_int64(stmt, 1);
1172
+ swap_info.m_state = sqlite3_column_int(stmt, 2);
1173
+ swap_info.m_coin_from = sqlite3_column_int(stmt, 3);
1174
+ swap_info.m_coin_to = sqlite3_column_int(stmt, 4);
1175
+ swap_info.m_amount_from = sqlite3_column_int64(stmt, 5);
1176
+ swap_info.m_amount_to = sqlite3_column_int64(stmt, 6);
1177
+ swap_info.m_initiating = sqlite3_column_int(stmt, 7) != 0;
1178
+ swap_info.m_participating = sqlite3_column_int(stmt, 8) != 0;
1179
+ const void *request_id_data = sqlite3_column_blob(stmt, 9);
1180
+ size_t request_id_size = sqlite3_column_bytes(stmt, 9);
1181
+ swap_info.m_request_id.resize(request_id_size);
1182
+ if (request_id_size) {
1183
+ memcpy(swap_info.m_request_id.data(), request_id_data, request_id_size);
1184
+ }
1185
+ const void *offer_id_data = sqlite3_column_blob(stmt, 10);
1186
+ size_t offer_id_size = sqlite3_column_bytes(stmt, 10);
1187
+ swap_info.m_offer_id.resize(offer_id_size);
1188
+ if (offer_id_size) {
1189
+ memcpy(swap_info.m_offer_id.data(), offer_id_data, offer_id_size);
1190
+ }
1191
+ const void *spend_fees_data = sqlite3_column_blob(stmt, 11);
1192
+ size_t spend_fees_size = sqlite3_column_bytes(stmt, 11);
1193
+ swap_info.m_lock_spend_a_fee_outputs.resize(spend_fees_size);
1194
+ if (spend_fees_size) {
1195
+ memcpy(swap_info.m_lock_spend_a_fee_outputs.data(), spend_fees_data, spend_fees_size);
1196
+ }
1197
+ const void *spend_b_fees_data = sqlite3_column_blob(stmt, 12);
1198
+ size_t spend_b_fees_size = sqlite3_column_bytes(stmt, 12);
1199
+ swap_info.m_lock_spend_b_fee_outputs.resize(spend_b_fees_size);
1200
+ if (spend_b_fees_size) {
1201
+ memcpy(swap_info.m_lock_spend_b_fee_outputs.data(), spend_b_fees_data,
1202
+ spend_b_fees_size);
1203
+ }
1204
+ swaps.push_back(std::move(swap_info));
1205
+ }
1206
+ if (rc != SQLITE_DONE) {
1207
+ LOGE("Selecting from \"%s\": %s", "swapinfo", sqlite3_errmsg(m_db));
1208
+ }
1209
+
1210
+ sqlite3_finalize(stmt);
1211
+ return rc == SQLITE_DONE ? 0 : 1;
1212
+ }
1213
+
1214
+ int BSXDB::getSwapIdsForSignResend(std::vector<std::vector<unsigned char>> &swap_ids, int64_t now,
1215
+ int64_t cutoff_time, int max_sends)
1216
+ {
1217
+ sqlite3_stmt *stmt;
1218
+ // Accepted, unexpired swaps where the last sent SwapSign message event is
1219
+ // older than cutoff_time, and fewer than max_sends have gone out.
1220
+ // Two proofs of delivery end it: the server's ack, and - when participating,
1221
+ // where the counterparty publishes lock tx a and only does so on receiving
1222
+ // the accept - that transaction reaching the network.
1223
+ std::string sql_query = "SELECT swap_id FROM swapinfo"
1224
+ " WHERE state IN (@state, @awaiting) AND expire_at > @now"
1225
+ " AND COALESCE((SELECT MAX(created_at) FROM eventlog"
1226
+ " WHERE eventlog.swap_id = swapinfo.swap_id"
1227
+ " AND event_type = @event_type"
1228
+ " AND event_sub_type = @event_sub_type), 0) <= @cutoff_time"
1229
+ " AND (SELECT COUNT(*) FROM eventlog"
1230
+ " WHERE eventlog.swap_id = swapinfo.swap_id"
1231
+ " AND event_type = @event_type"
1232
+ " AND event_sub_type = @event_sub_type) < @max_sends"
1233
+ " AND NOT EXISTS (SELECT 1 FROM eventlog"
1234
+ " WHERE eventlog.swap_id = swapinfo.swap_id"
1235
+ " AND event_type = @ack_type"
1236
+ " AND event_sub_type = @event_sub_type)"
1237
+ " AND NOT (swapinfo.participating = 1"
1238
+ " AND EXISTS (SELECT 1 FROM swaptxns"
1239
+ " WHERE swaptxns.swap_id = swapinfo.swap_id"
1240
+ " AND tx_type = @a_lock AND state >= @seen_state))";
1241
+
1242
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
1243
+ if (rc != SQLITE_OK) {
1244
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1245
+ return 1;
1246
+ }
1247
+ bindInt(stmt, "@state", SwapStates::ACCEPTED);
1248
+ bindInt(stmt, "@awaiting", SwapStates::AWAITING_LOCK_SIGS);
1249
+ bindInt64(stmt, "@now", now);
1250
+ bindInt(stmt, "@event_type", EventLogTypes::SENT_MESSAGE);
1251
+ bindInt(stmt, "@event_sub_type", npb::MessageTypes::GLUE_SWAP_SIGN);
1252
+ bindInt64(stmt, "@cutoff_time", cutoff_time);
1253
+ bindInt(stmt, "@max_sends", max_sends);
1254
+ bindInt(stmt, "@ack_type", EventLogTypes::ACK_RECEIVED);
1255
+ bindInt(stmt, "@a_lock", TxTypes::A_LOCK);
1256
+ bindInt(stmt, "@seen_state", TxStates::IN_MEMPOOL);
1257
+
1258
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1259
+ const void *swap_id_data = sqlite3_column_blob(stmt, 0);
1260
+ size_t swap_id_size = sqlite3_column_bytes(stmt, 0);
1261
+ size_t i = swap_ids.size();
1262
+ swap_ids.resize(swap_ids.size() + 1);
1263
+ swap_ids[i].resize(swap_id_size);
1264
+ if (swap_id_size) {
1265
+ memcpy(swap_ids[i].data(), swap_id_data, swap_id_size);
1266
+ }
1267
+ }
1268
+ if (rc != SQLITE_DONE) {
1269
+ LOGE("Selecting from \"%s\": %s", "swapinfo", sqlite3_errmsg(m_db));
1270
+ sqlite3_finalize(stmt);
1271
+ return 1;
1272
+ }
1273
+ sqlite3_finalize(stmt);
1274
+ return 0;
1275
+ }
1276
+
1277
+ int BSXDB::getSwapIdsForEndedResend(std::vector<std::vector<unsigned char>> &swap_ids,
1278
+ int64_t cutoff_time, int max_sends)
1279
+ {
1280
+ sqlite3_stmt *stmt;
1281
+ std::string ended_states;
1282
+ for (const int64_t state : SwapStates::endedStates()) {
1283
+ ended_states += (ended_states.empty() ? "" : ",") + std::to_string(state);
1284
+ }
1285
+ // No COALESCE on the last send: a swap never sent one stays unsent
1286
+ std::string sql_query = "SELECT swap_id FROM swapinfo WHERE state IN (" + ended_states + ")";
1287
+ sql_query += " AND EXISTS (SELECT 1 FROM offers"
1288
+ " WHERE offers.offer_id = swapinfo.offer_id AND offers.kind = @bridged)"
1289
+ " AND (SELECT MAX(created_at) FROM eventlog"
1290
+ " WHERE eventlog.swap_id = swapinfo.swap_id"
1291
+ " AND event_type = @event_type"
1292
+ " AND event_sub_type = @event_sub_type) <= @cutoff_time"
1293
+ " AND (SELECT COUNT(*) FROM eventlog"
1294
+ " WHERE eventlog.swap_id = swapinfo.swap_id"
1295
+ " AND event_type = @event_type"
1296
+ " AND event_sub_type = @event_sub_type) < @max_sends"
1297
+ " AND NOT EXISTS (SELECT 1 FROM eventlog"
1298
+ " WHERE eventlog.swap_id = swapinfo.swap_id"
1299
+ " AND event_type = @ack_type"
1300
+ " AND event_sub_type = @event_sub_type)";
1301
+
1302
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
1303
+ if (rc != SQLITE_OK) {
1304
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1305
+ return 1;
1306
+ }
1307
+ bindInt(stmt, "@bridged", OfferKinds::BRIDGED);
1308
+ bindInt(stmt, "@event_type", EventLogTypes::SENT_MESSAGE);
1309
+ bindInt(stmt, "@event_sub_type", npb::MessageTypes::GLUE_SWAP_ENDED);
1310
+ bindInt64(stmt, "@cutoff_time", cutoff_time);
1311
+ bindInt(stmt, "@max_sends", max_sends);
1312
+ bindInt(stmt, "@ack_type", EventLogTypes::ACK_RECEIVED);
1313
+
1314
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1315
+ const void *swap_id_data = sqlite3_column_blob(stmt, 0);
1316
+ size_t swap_id_size = sqlite3_column_bytes(stmt, 0);
1317
+ size_t i = swap_ids.size();
1318
+ swap_ids.resize(swap_ids.size() + 1);
1319
+ swap_ids[i].resize(swap_id_size);
1320
+ if (swap_id_size) {
1321
+ memcpy(swap_ids[i].data(), swap_id_data, swap_id_size);
1322
+ }
1323
+ }
1324
+ if (rc != SQLITE_DONE) {
1325
+ LOGE("Selecting from \"%s\": %s", "swapinfo", sqlite3_errmsg(m_db));
1326
+ sqlite3_finalize(stmt);
1327
+ return 1;
1328
+ }
1329
+ sqlite3_finalize(stmt);
1330
+ return 0;
1331
+ }
1332
+
1333
+ int BSXDB::getOfferIdsForAcceptResend(std::vector<std::vector<unsigned char>> &offer_ids,
1334
+ int64_t now, int64_t cutoff_time, int max_sends)
1335
+ {
1336
+ sqlite3_stmt *stmt;
1337
+ // Chosen, unexpired offers whose last sent accept is older than
1338
+ // cutoff_time and which have had fewer than max_sends goes. Three things
1339
+ // end it: the server's ack, the request no longer waiting on the accept,
1340
+ // and a swap arriving under that request - the accept can only have gone
1341
+ // out for the swap to come back.
1342
+ std::string sql_query =
1343
+ "SELECT offer_id FROM offers"
1344
+ " WHERE chosen = 1 AND expire_at > @now"
1345
+ " AND COALESCE((SELECT MAX(created_at) FROM eventlog"
1346
+ " WHERE eventlog.swap_id = offers.offer_id"
1347
+ " AND event_type = @event_type"
1348
+ " AND event_sub_type IN (@private_accept, @bridge_accept)), 0)"
1349
+ " <= @cutoff_time"
1350
+ " AND (SELECT COUNT(*) FROM eventlog"
1351
+ " WHERE eventlog.swap_id = offers.offer_id"
1352
+ " AND event_type = @event_type"
1353
+ " AND event_sub_type IN (@private_accept, @bridge_accept)) < @max_sends"
1354
+ " AND NOT EXISTS (SELECT 1 FROM eventlog"
1355
+ " WHERE eventlog.swap_id = offers.offer_id"
1356
+ " AND event_type = @ack_type"
1357
+ " AND event_sub_type IN (@private_accept, @bridge_accept))"
1358
+ " AND EXISTS (SELECT 1 FROM swaprequests"
1359
+ " WHERE swaprequests.swap_request_id = offers.request_id"
1360
+ " AND swaprequests.state = @state_accept_sent"
1361
+ " AND swaprequests.expire_at > @now)"
1362
+ " AND NOT EXISTS (SELECT 1 FROM swapinfo"
1363
+ " WHERE swapinfo.request_id = offers.request_id)";
1364
+
1365
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
1366
+ if (rc != SQLITE_OK) {
1367
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1368
+ return 1;
1369
+ }
1370
+ bindInt64(stmt, "@now", now);
1371
+ bindInt(stmt, "@event_type", EventLogTypes::SENT_MESSAGE);
1372
+ bindInt(stmt, "@private_accept", npb::MessageTypes::GLUE_PRIVATE_ACCEPT);
1373
+ bindInt(stmt, "@bridge_accept", npb::MessageTypes::GLUE_BRIDGE_ACCEPT);
1374
+ bindInt64(stmt, "@cutoff_time", cutoff_time);
1375
+ bindInt(stmt, "@max_sends", max_sends);
1376
+ bindInt(stmt, "@ack_type", EventLogTypes::ACK_RECEIVED);
1377
+ bindInt(stmt, "@state_accept_sent", SwapRequestStates::ACCEPT_SENT);
1378
+
1379
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1380
+ const unsigned char *p = (const unsigned char *)sqlite3_column_blob(stmt, 0);
1381
+ int len = sqlite3_column_bytes(stmt, 0);
1382
+ offer_ids.emplace_back(p, p + (p ? len : 0));
1383
+ }
1384
+ if (rc != SQLITE_DONE) {
1385
+ LOGE("Selecting from \"%s\": %s", "offers", sqlite3_errmsg(m_db));
1386
+ sqlite3_finalize(stmt);
1387
+ return 1;
1388
+ }
1389
+ sqlite3_finalize(stmt);
1390
+ return 0;
1391
+ }
1392
+
1393
+ int BSXDB::getSwapIdsForReleaseResend(std::vector<std::vector<unsigned char>> &swap_ids,
1394
+ int64_t cutoff_time)
1395
+ {
1396
+ sqlite3_stmt *stmt;
1397
+ // Initiating swaps waiting on the lock tx a spend where the last sent
1398
+ // SwapRelease message event is missing or older than cutoff_time
1399
+ std::string sql_query = "SELECT swap_id FROM swapinfo"
1400
+ " WHERE state = @state AND initiating != 0"
1401
+ " AND COALESCE((SELECT MAX(created_at) FROM eventlog"
1402
+ " WHERE eventlog.swap_id = swapinfo.swap_id"
1403
+ " AND event_type = @event_type"
1404
+ " AND event_sub_type = @event_sub_type), 0) <= @cutoff_time";
1405
+
1406
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
1407
+ if (rc != SQLITE_OK) {
1408
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1409
+ return 1;
1410
+ }
1411
+ bindInt(stmt, "@state", SwapStates::SCRIPT_COIN_LOCK_RELEASED);
1412
+ bindInt(stmt, "@event_type", EventLogTypes::SENT_MESSAGE);
1413
+ bindInt(stmt, "@event_sub_type", npb::MessageTypes::GLUE_SWAP_RELEASE);
1414
+ bindInt64(stmt, "@cutoff_time", cutoff_time);
1415
+
1416
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1417
+ const void *swap_id_data = sqlite3_column_blob(stmt, 0);
1418
+ size_t swap_id_size = sqlite3_column_bytes(stmt, 0);
1419
+ size_t i = swap_ids.size();
1420
+ swap_ids.resize(swap_ids.size() + 1);
1421
+ swap_ids[i].resize(swap_id_size);
1422
+ if (swap_id_size) {
1423
+ memcpy(swap_ids[i].data(), swap_id_data, swap_id_size);
1424
+ }
1425
+ }
1426
+ if (rc != SQLITE_DONE) {
1427
+ LOGE("Selecting from \"%s\": %s", "swapinfo", sqlite3_errmsg(m_db));
1428
+ }
1429
+
1430
+ sqlite3_finalize(stmt);
1431
+ return rc == SQLITE_DONE ? 0 : 1;
1432
+ }
1433
+
1434
+ int BSXDB::saveSwapTx(int64_t &rowid, const SwapTx &swap_tx)
1435
+ {
1436
+ sqlite3_stmt *stmt;
1437
+
1438
+ std::string insert_columns, insert_values;
1439
+ const db::Table *swap_tx_schema = db::get_table_schema("swaptxns");
1440
+ if (!swap_tx_schema) {
1441
+ LOGE("Failed to get table definition: %s", "swaptxns");
1442
+ return 2;
1443
+ }
1444
+ for (const auto &col : swap_tx_schema->m_columns) {
1445
+ if (col.m_name == "id") {
1446
+ continue;
1447
+ }
1448
+ insert_columns += std::string(insert_columns.size() ? "," : "") + col.m_name;
1449
+ insert_values += std::string(insert_values.size() ? "," : "") + "@" + col.m_name;
1450
+ }
1451
+
1452
+ std::string sql_insert = strprintf("INSERT INTO swaptxns (%s)"
1453
+ " VALUES (%s) ",
1454
+ insert_columns, insert_values);
1455
+ sql_insert += " ON CONFLICT(txid) DO UPDATE SET ";
1456
+ size_t upserted_columns{0};
1457
+ for (const auto &col : swap_tx_schema->m_columns) {
1458
+ if (col.m_name == "id" || col.m_name == "txid") {
1459
+ continue;
1460
+ }
1461
+ if (upserted_columns) {
1462
+ sql_insert += ",";
1463
+ }
1464
+ sql_insert += col.m_name + "=excluded." + col.m_name;
1465
+ upserted_columns++;
1466
+ }
1467
+
1468
+ int rc = sqlite3_prepare_v2(m_db, sql_insert.c_str(), -1, &stmt, 0);
1469
+ if (rc != SQLITE_OK) {
1470
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1471
+ return 1;
1472
+ }
1473
+
1474
+ bindDataVector(stmt, "@swap_id", swap_tx.m_swap_id);
1475
+ bindInt(stmt, "@coin_type", swap_tx.m_coin_type);
1476
+ bindInt(stmt, "@tx_type", swap_tx.m_tx_type);
1477
+ bindInt(stmt, "@state", swap_tx.m_state);
1478
+ bindDataVector(stmt, "@txid", swap_tx.m_txid);
1479
+ bindInt(stmt, "@txout_n", swap_tx.m_txout_n);
1480
+ bindInt64(stmt, "@txout_value", swap_tx.m_txout_value);
1481
+ bindDataVector(stmt, "@txout_script", swap_tx.m_txout_script);
1482
+ bindDataVector(stmt, "@tx_data", swap_tx.m_tx_data);
1483
+ bindDataVector(stmt, "@block_hash", swap_tx.m_block_hash);
1484
+ bindInt(stmt, "@block_height", swap_tx.m_block_height);
1485
+ bindInt64(stmt, "@block_time", swap_tx.m_block_time);
1486
+ bindDataVector(stmt, "@merkle_hash", swap_tx.m_merkle_hash);
1487
+ bindDataVector(stmt, "@spend_txid", swap_tx.m_spend_txid);
1488
+ bindInt(stmt, "@spend_n", swap_tx.m_spend_n);
1489
+
1490
+ int rv = 0;
1491
+ rc = sqlite3_step(stmt);
1492
+ if (rc != SQLITE_DONE) {
1493
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
1494
+ rv = 1;
1495
+ }
1496
+
1497
+ sqlite3_finalize(stmt);
1498
+
1499
+ return rv;
1500
+ }
1501
+
1502
+ int BSXDB::loadSwapTx(SwapTx &swap_tx, const std::vector<FilterData> &filters,
1503
+ const std::vector<std::string> *fields, bool newest)
1504
+ {
1505
+ sqlite3_stmt *stmt;
1506
+ std::string retrieve_columns, filter_columns;
1507
+ if (fields) {
1508
+ for (const auto &col_name : *fields) {
1509
+ retrieve_columns += std::string(retrieve_columns.size() ? "," : "") + col_name;
1510
+ }
1511
+ } else {
1512
+ const db::Table *swap_tx_schema = db::get_table_schema("swaptxns");
1513
+ if (!swap_tx_schema) {
1514
+ LOGE("Failed to get table definition: %s", "swaptxns");
1515
+ return 2;
1516
+ }
1517
+ for (const auto &col : swap_tx_schema->m_columns) {
1518
+ retrieve_columns += std::string(retrieve_columns.size() ? "," : "") + col.m_name;
1519
+ }
1520
+ }
1521
+
1522
+ if (0 != expandFilterColumns(filter_columns, filters)) {
1523
+ LOGE("Failed to expand filter columns");
1524
+ return 1;
1525
+ }
1526
+
1527
+ std::string sql =
1528
+ strprintf("SELECT %s FROM swaptxns"
1529
+ " WHERE 1 = 1 %s%s",
1530
+ retrieve_columns, filter_columns, newest ? " ORDER BY id DESC" : "");
1531
+
1532
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
1533
+ if (rc != SQLITE_OK) {
1534
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1535
+ return 1;
1536
+ }
1537
+ if (0 != bindFilterData(stmt, filters)) {
1538
+ LOGE("Failed to bind filter data");
1539
+ sqlite3_finalize(stmt);
1540
+ return 1;
1541
+ }
1542
+ int rv = 1;
1543
+ rc = sqlite3_step(stmt);
1544
+ if (rc == SQLITE_ROW) {
1545
+ setDataVector(stmt, "swap_id", swap_tx.m_swap_id);
1546
+ setInt(stmt, "coin_type", swap_tx.m_coin_type);
1547
+ setInt(stmt, "tx_type", swap_tx.m_tx_type);
1548
+ setInt(stmt, "state", swap_tx.m_state);
1549
+ setDataVector(stmt, "txid", swap_tx.m_txid);
1550
+ setInt(stmt, "txout_n", swap_tx.m_txout_n);
1551
+ setInt64(stmt, "txout_value", swap_tx.m_txout_value);
1552
+ setDataVector(stmt, "txout_script", swap_tx.m_txout_script);
1553
+ setDataVector(stmt, "tx_data", swap_tx.m_tx_data);
1554
+ setDataVector(stmt, "block_hash", swap_tx.m_block_hash);
1555
+ setInt(stmt, "block_height", swap_tx.m_block_height);
1556
+ setInt64(stmt, "block_time", swap_tx.m_block_time);
1557
+ setDataVector(stmt, "merkle_hash", swap_tx.m_merkle_hash);
1558
+ setDataVector(stmt, "spend_txid", swap_tx.m_spend_txid);
1559
+ setInt(stmt, "spend_n", swap_tx.m_spend_n);
1560
+ rv = 0;
1561
+ } else if (rc == SQLITE_ERROR) {
1562
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
1563
+ } else if (rc == SQLITE_DONE) {
1564
+ LOGW("No row found.");
1565
+ rv = 2;
1566
+ }
1567
+ sqlite3_finalize(stmt);
1568
+ return rv;
1569
+ }
1570
+
1571
+ int BSXDB::listSwapTxs(std::vector<SwapTx> &swap_txs, const std::vector<FilterData> &filters)
1572
+ {
1573
+ sqlite3_stmt *stmt;
1574
+
1575
+ std::string filter_columns;
1576
+ if (0 != expandFilterColumns(filter_columns, filters)) {
1577
+ LOGE("Failed to expand filter columns");
1578
+ return 1;
1579
+ }
1580
+
1581
+ std::string sql = strprintf("SELECT coin_type, tx_type, txid, block_height, state FROM swaptxns"
1582
+ " WHERE 1 = 1 %s ORDER BY tx_type ASC",
1583
+ filter_columns);
1584
+
1585
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
1586
+ if (rc != SQLITE_OK) {
1587
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1588
+ return 1;
1589
+ }
1590
+ if (0 != bindFilterData(stmt, filters)) {
1591
+ LOGE("Failed to bind filter data");
1592
+ sqlite3_finalize(stmt);
1593
+ return 1;
1594
+ }
1595
+
1596
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1597
+ SwapTx swap_tx;
1598
+ swap_tx.m_coin_type = sqlite3_column_int(stmt, 0);
1599
+ swap_tx.m_tx_type = sqlite3_column_int(stmt, 1);
1600
+ setDataVector(stmt, 2, swap_tx.m_txid);
1601
+ swap_tx.m_block_height =
1602
+ sqlite3_column_type(stmt, 3) == SQLITE_NULL ? -1 : sqlite3_column_int(stmt, 3);
1603
+ swap_tx.m_state = sqlite3_column_int(stmt, 4);
1604
+ swap_txs.push_back(std::move(swap_tx));
1605
+ }
1606
+ const int rv = rc == SQLITE_DONE ? 0 : 1;
1607
+ if (rv != 0) {
1608
+ LOGE("Selecting from \"%s\": %s", "swaptxns", sqlite3_errmsg(m_db));
1609
+ }
1610
+ sqlite3_finalize(stmt);
1611
+ return rv;
1612
+ }
1613
+
1614
+ int BSXDB::updateSwapTx(const SwapTx &swap_tx, const std::vector<std::string> *fields)
1615
+ {
1616
+ sqlite3_stmt *stmt;
1617
+
1618
+ if (swap_tx.m_txid.size() < 1) {
1619
+ LOGE("updateSwapTx swap_tx txid must be set");
1620
+ return 1;
1621
+ }
1622
+
1623
+ std::string sql_query = "UPDATE swaptxns SET ";
1624
+ size_t updated_columns{0};
1625
+ if (fields) {
1626
+ for (const auto &col_name : *fields) {
1627
+ if (updated_columns) {
1628
+ sql_query += ",";
1629
+ }
1630
+ sql_query += col_name + "=@" + col_name;
1631
+ updated_columns++;
1632
+ }
1633
+ } else {
1634
+ const db::Table *swap_info_schema = db::get_table_schema("swaptxns");
1635
+ if (!swap_info_schema) {
1636
+ LOGE("Failed to get table definition: %s", "swaptxns");
1637
+ return 2;
1638
+ }
1639
+ for (const auto &col : swap_info_schema->m_columns) {
1640
+ if (col.m_name == "id" || col.m_name == "txid") {
1641
+ continue;
1642
+ }
1643
+ if (updated_columns) {
1644
+ sql_query += ",";
1645
+ }
1646
+ sql_query += col.m_name + "=@" + col.m_name;
1647
+ updated_columns++;
1648
+ }
1649
+ }
1650
+
1651
+ sql_query += " WHERE txid = @txid";
1652
+
1653
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
1654
+ if (rc != SQLITE_OK) {
1655
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1656
+ return 1;
1657
+ }
1658
+
1659
+ bindDataVector(stmt, "@swap_id", swap_tx.m_swap_id);
1660
+ bindInt(stmt, "@coin_type", swap_tx.m_coin_type);
1661
+ bindInt(stmt, "@tx_type", swap_tx.m_tx_type);
1662
+ bindInt(stmt, "@state", swap_tx.m_state);
1663
+ bindDataVector(stmt, "@txid", swap_tx.m_txid);
1664
+ bindInt(stmt, "@txout_n", swap_tx.m_txout_n);
1665
+ bindInt64(stmt, "@txout_value", swap_tx.m_txout_value);
1666
+ bindDataVector(stmt, "@txout_script", swap_tx.m_txout_script);
1667
+ bindDataVector(stmt, "@tx_data", swap_tx.m_tx_data);
1668
+ bindDataVector(stmt, "@block_hash", swap_tx.m_block_hash);
1669
+ bindInt(stmt, "@block_height", swap_tx.m_block_height);
1670
+ bindInt64(stmt, "@block_time", swap_tx.m_block_time);
1671
+ bindDataVector(stmt, "@merkle_hash", swap_tx.m_merkle_hash);
1672
+ bindDataVector(stmt, "@spend_txid", swap_tx.m_spend_txid);
1673
+ bindInt(stmt, "@spend_n", swap_tx.m_spend_n);
1674
+
1675
+ int rv = 0;
1676
+ rc = sqlite3_step(stmt);
1677
+ if (rc != SQLITE_DONE) {
1678
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
1679
+ rv = 1;
1680
+ }
1681
+
1682
+ sqlite3_finalize(stmt);
1683
+ return rv;
1684
+ }
1685
+
1686
+ int BSXDB::getStateInfo(StateData &state_info, int state_ind,
1687
+ const std::vector<std::string> *fields)
1688
+ {
1689
+ sqlite3_stmt *stmt;
1690
+
1691
+ std::string retrieve_columns;
1692
+ if (fields) {
1693
+ for (const auto &col_name : *fields) {
1694
+ retrieve_columns += std::string(retrieve_columns.size() ? "," : "") + col_name;
1695
+ }
1696
+ } else {
1697
+ const db::Table *swap_info_schema = db::get_table_schema("swapstates");
1698
+ if (!swap_info_schema) {
1699
+ LOGE("Failed to get table definition: %s", "swapstates");
1700
+ return 2;
1701
+ }
1702
+ for (const auto &col : swap_info_schema->m_columns) {
1703
+ bool skip = false;
1704
+ for (const auto &ignore_column : {"id", "state_ind"}) {
1705
+ if (strcmp(col.m_name.c_str(), ignore_column) == 0) {
1706
+ skip = true;
1707
+ break;
1708
+ }
1709
+ }
1710
+ if (skip) {
1711
+ continue;
1712
+ }
1713
+ retrieve_columns += std::string(retrieve_columns.size() ? "," : "") + col.m_name;
1714
+ }
1715
+ }
1716
+ std::string sql = strprintf("SELECT %s FROM swapstates"
1717
+ " WHERE state_ind = ?",
1718
+ retrieve_columns);
1719
+
1720
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
1721
+ if (rc != SQLITE_OK) {
1722
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1723
+ return 1;
1724
+ }
1725
+ sqlite3_bind_int(stmt, 1, state_ind);
1726
+
1727
+ int rv = 1;
1728
+ rc = sqlite3_step(stmt);
1729
+ if (rc == SQLITE_ROW) {
1730
+ state_info.m_state_ind = state_ind;
1731
+
1732
+ setString(stmt, "label", state_info.m_label);
1733
+ setString(stmt, "note", state_info.m_note);
1734
+ setBool(stmt, "in_progress", state_info.m_in_progress);
1735
+ setBool(stmt, "in_error", state_info.m_in_error);
1736
+ setBool(stmt, "swap_failed", state_info.m_swap_failed);
1737
+ setBool(stmt, "swap_ended", state_info.m_swap_ended);
1738
+ setBool(stmt, "can_accept", state_info.m_can_accept);
1739
+ setBool(stmt, "can_expire", state_info.m_can_expire);
1740
+ setBool(stmt, "can_timeout", state_info.m_can_timeout);
1741
+
1742
+ rv = 0;
1743
+ } else if (rc == SQLITE_ERROR) {
1744
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
1745
+ } else if (rc == SQLITE_DONE) {
1746
+ LOGW("No row found.");
1747
+ rv = 2;
1748
+ }
1749
+ sqlite3_finalize(stmt);
1750
+ return rv;
1751
+ }
1752
+
1753
+ int BSXDB::saveKey(const std::vector<unsigned char> &key,
1754
+ const std::vector<unsigned char> &key_hash)
1755
+ {
1756
+ sqlite3_stmt *stmt;
1757
+
1758
+ const char *sql = "INSERT INTO debug_keys (key_hash, key)"
1759
+ " VALUES (@key_hash, @key);";
1760
+
1761
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
1762
+ if (rc != SQLITE_OK) {
1763
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1764
+ return 1;
1765
+ }
1766
+ if (0 != bindDataVector(stmt, "@key_hash", key_hash) ||
1767
+ 0 != bindDataVector(stmt, "@key", key)) {
1768
+ LOGE("Failed to bind parameters.");
1769
+ sqlite3_finalize(stmt);
1770
+ return 1;
1771
+ }
1772
+
1773
+ int rv = 0;
1774
+ rc = sqlite3_step(stmt);
1775
+ if (rc != SQLITE_DONE) {
1776
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
1777
+ rv = 1;
1778
+ }
1779
+
1780
+ sqlite3_finalize(stmt);
1781
+ return rv;
1782
+ }
1783
+
1784
+ int BSXDB::loadKey(std::vector<unsigned char> &key, const std::vector<unsigned char> &key_hash)
1785
+ {
1786
+ sqlite3_stmt *stmt;
1787
+
1788
+ const char *sql = "SELECT key FROM debug_keys"
1789
+ " WHERE key_hash = ?";
1790
+
1791
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
1792
+ if (rc != SQLITE_OK) {
1793
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1794
+ return 1;
1795
+ }
1796
+ sqlite3_bind_blob(stmt, 1, key_hash.data(), key_hash.size(), SQLITE_STATIC);
1797
+ int rv = 1;
1798
+ rc = sqlite3_step(stmt);
1799
+ if (rc == SQLITE_ROW) {
1800
+ const void *key_data = sqlite3_column_blob(stmt, 0);
1801
+ size_t key_size = sqlite3_column_bytes(stmt, 0);
1802
+ if (key_size != 32) {
1803
+ LOGE("Invalid key size: %zu", key_size);
1804
+ rv = 1;
1805
+ } else {
1806
+ key.resize(key_size);
1807
+ memcpy(key.data(), key_data, key_size);
1808
+ rv = 0;
1809
+ }
1810
+ } else if (rc == SQLITE_ERROR) {
1811
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
1812
+ } else if (rc == SQLITE_DONE) {
1813
+ LOGW("No row found.");
1814
+ rv = 2;
1815
+ }
1816
+ sqlite3_finalize(stmt);
1817
+ return rv;
1818
+ }
1819
+
1820
+ int BSXDB::loadPinnedCert(std::vector<unsigned char> &fingerprint, bool &can_change,
1821
+ const std::string &host, uint16_t port)
1822
+ {
1823
+ sqlite3_stmt *stmt;
1824
+ const char *sql = "SELECT fingerprint, can_change FROM pinned_certs"
1825
+ " WHERE host = @host AND port = @port";
1826
+
1827
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
1828
+ if (rc != SQLITE_OK) {
1829
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1830
+ return 1;
1831
+ }
1832
+ if (0 != bindText(stmt, "@host", host) || 0 != bindInt(stmt, "@port", port)) {
1833
+ sqlite3_finalize(stmt);
1834
+ return 1;
1835
+ }
1836
+
1837
+ int rv = 1;
1838
+ rc = sqlite3_step(stmt);
1839
+ if (rc == SQLITE_ROW) {
1840
+ setDataVector(stmt, "fingerprint", fingerprint);
1841
+ setBool(stmt, "can_change", can_change);
1842
+ rv = 0;
1843
+ } else if (rc == SQLITE_ERROR) {
1844
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
1845
+ } else if (rc == SQLITE_DONE) {
1846
+ rv = 2;
1847
+ }
1848
+ sqlite3_finalize(stmt);
1849
+ return rv;
1850
+ }
1851
+
1852
+ int BSXDB::savePinnedCert(const std::string &host, uint16_t port,
1853
+ const std::vector<unsigned char> &fingerprint, int64_t first_seen,
1854
+ bool can_change)
1855
+ {
1856
+ sqlite3_stmt *stmt;
1857
+ const char *sql = "INSERT INTO pinned_certs (host, port, fingerprint, first_seen, can_change)"
1858
+ " VALUES (@host, @port, @fingerprint, @first_seen, @can_change)"
1859
+ " ON CONFLICT(host, port) DO UPDATE SET fingerprint = excluded.fingerprint,"
1860
+ " first_seen = excluded.first_seen, can_change = excluded.can_change";
1861
+
1862
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
1863
+ if (rc != SQLITE_OK) {
1864
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1865
+ return 1;
1866
+ }
1867
+ if (0 != bindText(stmt, "@host", host) || 0 != bindInt(stmt, "@port", port) ||
1868
+ 0 != bindDataVector(stmt, "@fingerprint", fingerprint) ||
1869
+ 0 != bindInt64(stmt, "@first_seen", first_seen) ||
1870
+ 0 != bindInt(stmt, "@can_change", can_change ? 1 : 0)) {
1871
+ sqlite3_finalize(stmt);
1872
+ return 1;
1873
+ }
1874
+
1875
+ int rv = 0;
1876
+ if (sqlite3_step(stmt) != SQLITE_DONE) {
1877
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
1878
+ rv = 1;
1879
+ }
1880
+ sqlite3_finalize(stmt);
1881
+ return rv;
1882
+ }
1883
+
1884
+ int BSXDB::saveCaCert(const std::string &label, const std::string &host, uint16_t port,
1885
+ const std::string &pem, int64_t added_at)
1886
+ {
1887
+ sqlite3_stmt *stmt;
1888
+ const char *sql = "INSERT INTO ca_certs (label, host, port, pem, added_at)"
1889
+ " VALUES (@label, @host, @port, @pem, @added_at)";
1890
+
1891
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
1892
+ if (rc != SQLITE_OK) {
1893
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1894
+ return 1;
1895
+ }
1896
+ if (0 != bindText(stmt, "@label", label) || 0 != bindText(stmt, "@host", host) ||
1897
+ 0 != bindInt(stmt, "@port", port) || 0 != bindText(stmt, "@pem", pem) ||
1898
+ 0 != bindInt64(stmt, "@added_at", added_at)) {
1899
+ sqlite3_finalize(stmt);
1900
+ return 1;
1901
+ }
1902
+
1903
+ int rv = 0;
1904
+ if (sqlite3_step(stmt) != SQLITE_DONE) {
1905
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
1906
+ rv = 1;
1907
+ }
1908
+ sqlite3_finalize(stmt);
1909
+ return rv;
1910
+ }
1911
+
1912
+ int BSXDB::loadCaCerts(std::string &pem_out, const std::string &host, uint16_t port)
1913
+ {
1914
+ // Rows naming this server replace the general ones rather than adding to
1915
+ // them: supplying a certificate for a server is meant to narrow what may
1916
+ // vouch for it, and a union would leave every other anchor in place.
1917
+ const char *sql_scoped = "SELECT pem FROM ca_certs WHERE host = @host AND port = @port";
1918
+ const char *sql_global = "SELECT pem FROM ca_certs WHERE host = ''";
1919
+
1920
+ for (const char *sql : {sql_scoped, sql_global}) {
1921
+ sqlite3_stmt *stmt;
1922
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
1923
+ if (rc != SQLITE_OK) {
1924
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1925
+ return 1;
1926
+ }
1927
+ if (sql == sql_scoped &&
1928
+ (0 != bindText(stmt, "@host", host) || 0 != bindInt(stmt, "@port", port))) {
1929
+ sqlite3_finalize(stmt);
1930
+ return 1;
1931
+ }
1932
+
1933
+ std::string found;
1934
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1935
+ std::string pem;
1936
+ setString(stmt, "pem", pem);
1937
+ found += pem;
1938
+ if (!found.empty() && found.back() != '\n') {
1939
+ found += '\n';
1940
+ }
1941
+ }
1942
+ const bool failed = (rc != SQLITE_DONE);
1943
+ if (failed) {
1944
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
1945
+ }
1946
+ sqlite3_finalize(stmt);
1947
+ if (failed) {
1948
+ return 1;
1949
+ }
1950
+ if (!found.empty()) {
1951
+ pem_out = found;
1952
+ return 0;
1953
+ }
1954
+ }
1955
+ return 2;
1956
+ }
1957
+
1958
+ int BSXDB::deleteCaCert(int64_t id)
1959
+ {
1960
+ sqlite3_stmt *stmt;
1961
+ const char *sql = "DELETE FROM ca_certs WHERE id = @id";
1962
+
1963
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
1964
+ if (rc != SQLITE_OK) {
1965
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1966
+ return 1;
1967
+ }
1968
+ if (0 != bindInt64(stmt, "@id", id)) {
1969
+ sqlite3_finalize(stmt);
1970
+ return 1;
1971
+ }
1972
+
1973
+ int rv = 0;
1974
+ if (sqlite3_step(stmt) != SQLITE_DONE) {
1975
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
1976
+ rv = 1;
1977
+ }
1978
+ sqlite3_finalize(stmt);
1979
+ if (0 == rv && 0 == sqlite3_changes(m_db)) {
1980
+ return 2;
1981
+ }
1982
+ return rv;
1983
+ }
1984
+
1985
+ int BSXDB::listCaCerts(std::vector<CaCertInfo> &certs)
1986
+ {
1987
+ sqlite3_stmt *stmt;
1988
+ // Without the pem: a listing is for choosing which to remove, and the
1989
+ // certificates are kilobytes each.
1990
+ const char *sql = "SELECT id, label, host, port FROM ca_certs ORDER BY id";
1991
+
1992
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
1993
+ if (rc != SQLITE_OK) {
1994
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
1995
+ return 1;
1996
+ }
1997
+
1998
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
1999
+ CaCertInfo info;
2000
+ setInt64(stmt, "id", info.m_id);
2001
+ setString(stmt, "label", info.m_label);
2002
+ setString(stmt, "host", info.m_host);
2003
+ setInt(stmt, "port", info.m_port);
2004
+ certs.push_back(info);
2005
+ }
2006
+ const bool failed = (rc != SQLITE_DONE);
2007
+ if (failed) {
2008
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
2009
+ }
2010
+ sqlite3_finalize(stmt);
2011
+ return failed ? 1 : 0;
2012
+ }
2013
+
2014
+ int BSXDB::listPinnedCerts(std::vector<PinnedCertInfo> &certs)
2015
+ {
2016
+ sqlite3_stmt *stmt;
2017
+ const char *sql = "SELECT host, port, fingerprint, first_seen, can_change FROM pinned_certs"
2018
+ " ORDER BY host, port";
2019
+
2020
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2021
+ if (rc != SQLITE_OK) {
2022
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2023
+ return 1;
2024
+ }
2025
+
2026
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
2027
+ PinnedCertInfo info;
2028
+ setString(stmt, "host", info.m_host);
2029
+ setInt(stmt, "port", info.m_port);
2030
+ setDataVector(stmt, "fingerprint", info.m_fingerprint);
2031
+ setInt64(stmt, "first_seen", info.m_first_seen);
2032
+ setBool(stmt, "can_change", info.m_can_change);
2033
+ certs.push_back(info);
2034
+ }
2035
+ const bool failed = (rc != SQLITE_DONE);
2036
+ if (failed) {
2037
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
2038
+ }
2039
+ sqlite3_finalize(stmt);
2040
+ return failed ? 1 : 0;
2041
+ }
2042
+
2043
+ static constexpr int SAVED_ADDRESSES_PER_COIN = 20;
2044
+
2045
+ int BSXDB::loadSavedAddresses(std::vector<SavedAddress> &addresses, int coin_type)
2046
+ {
2047
+ sqlite3_stmt *stmt;
2048
+ const char *sql = "SELECT address, label, source, last_used_at FROM saved_addresses"
2049
+ " WHERE coin_type = @coin_type ORDER BY last_used_at DESC, id DESC";
2050
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2051
+ if (rc != SQLITE_OK) {
2052
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2053
+ return 1;
2054
+ }
2055
+ if (0 != bindInt(stmt, "@coin_type", coin_type)) {
2056
+ sqlite3_finalize(stmt);
2057
+ return 1;
2058
+ }
2059
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
2060
+ SavedAddress saved;
2061
+ setString(stmt, "address", saved.m_address);
2062
+ setString(stmt, "label", saved.m_label);
2063
+ setString(stmt, "source", saved.m_source);
2064
+ setInt64(stmt, "last_used_at", saved.m_last_used_at);
2065
+ addresses.push_back(saved);
2066
+ }
2067
+ const bool failed = (rc != SQLITE_DONE);
2068
+ if (failed) {
2069
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
2070
+ }
2071
+ sqlite3_finalize(stmt);
2072
+ return failed ? 1 : 0;
2073
+ }
2074
+
2075
+ int BSXDB::saveAddress(int coin_type, const std::string &address, const std::string *label)
2076
+ {
2077
+ if (address.empty()) {
2078
+ return 1;
2079
+ }
2080
+ std::vector<SavedAddress> saved;
2081
+ if (0 != loadSavedAddresses(saved, coin_type)) {
2082
+ return 1;
2083
+ }
2084
+ auto previous = std::find_if(saved.begin(), saved.end(), [&](const SavedAddress &entry) {
2085
+ return entry.m_address == address;
2086
+ });
2087
+ const bool relabel = previous != saved.end() && label;
2088
+ const std::string kept_label = label ? *label
2089
+ : previous != saved.end() ? previous->m_label
2090
+ : "";
2091
+ // Deleted and inserted again rather than updated, so a new id orders it first within a second
2092
+ const char *statements[] = {
2093
+ relabel ? "UPDATE saved_addresses SET label = @label"
2094
+ " WHERE coin_type = @coin_type AND address = @address"
2095
+ : "DELETE FROM saved_addresses WHERE coin_type = @coin_type AND address = @address",
2096
+ relabel ? nullptr
2097
+ : "INSERT INTO saved_addresses (coin_type, address, label, source, last_used_at)"
2098
+ " VALUES (@coin_type, @address, @label, 'external', @last_used_at)",
2099
+ relabel ? nullptr
2100
+ : "DELETE FROM saved_addresses WHERE coin_type = @coin_type AND id NOT IN"
2101
+ " (SELECT id FROM saved_addresses WHERE coin_type = @coin_type"
2102
+ " ORDER BY last_used_at DESC, id DESC LIMIT @keep)",
2103
+ };
2104
+ Savepoint step(*this, "save_address");
2105
+ if (!step.isOpen()) {
2106
+ return 1;
2107
+ }
2108
+ for (const char *sql : statements) {
2109
+ if (!sql) {
2110
+ continue;
2111
+ }
2112
+ sqlite3_stmt *stmt;
2113
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
2114
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2115
+ return 1;
2116
+ }
2117
+ const bool bound = 0 == bindInt(stmt, "@coin_type", coin_type) &&
2118
+ (!sqlite3_bind_parameter_index(stmt, "@address") ||
2119
+ 0 == bindText(stmt, "@address", address)) &&
2120
+ (!sqlite3_bind_parameter_index(stmt, "@label") ||
2121
+ 0 == bindText(stmt, "@label", kept_label)) &&
2122
+ (!sqlite3_bind_parameter_index(stmt, "@last_used_at") ||
2123
+ 0 == bindInt64(stmt, "@last_used_at", getTime())) &&
2124
+ (!sqlite3_bind_parameter_index(stmt, "@keep") ||
2125
+ 0 == bindInt(stmt, "@keep", SAVED_ADDRESSES_PER_COIN));
2126
+ const bool done = bound && sqlite3_step(stmt) == SQLITE_DONE;
2127
+ if (!done) {
2128
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
2129
+ }
2130
+ sqlite3_finalize(stmt);
2131
+ if (!done) {
2132
+ return 1;
2133
+ }
2134
+ }
2135
+ return step.release();
2136
+ }
2137
+
2138
+ int BSXDB::seedListedProviders(const std::vector<KnownProvider> &listed)
2139
+ {
2140
+ Savepoint step(*this, "seed_listed_providers");
2141
+ if (!step.isOpen()) {
2142
+ return 1;
2143
+ }
2144
+ if (SQLITE_OK != sqlite3_exec(m_db, "UPDATE known_providers SET listed = 0", 0, 0, 0)) {
2145
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
2146
+ return 1;
2147
+ }
2148
+ const char *sql = "INSERT INTO known_providers (provider_key, label, listed, first_seen_at,"
2149
+ " last_seen_at) VALUES (@key, @label, 1, 0, 0) ON CONFLICT(provider_key)"
2150
+ " DO UPDATE SET label = excluded.label, listed = 1";
2151
+ for (const auto &provider : listed) {
2152
+ sqlite3_stmt *stmt;
2153
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
2154
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2155
+ return 1;
2156
+ }
2157
+ const bool done = 0 == bindDataVector(stmt, "@key", provider.m_key) &&
2158
+ 0 == bindText(stmt, "@label", provider.m_label) &&
2159
+ sqlite3_step(stmt) == SQLITE_DONE;
2160
+ if (!done) {
2161
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
2162
+ }
2163
+ sqlite3_finalize(stmt);
2164
+ if (!done) {
2165
+ return 1;
2166
+ }
2167
+ }
2168
+ return step.release();
2169
+ }
2170
+
2171
+ int BSXDB::noteProvider(const std::vector<unsigned char> &key, const std::string &label,
2172
+ int64_t now)
2173
+ {
2174
+ sqlite3_stmt *stmt;
2175
+ const char *sql =
2176
+ "INSERT INTO known_providers (provider_key, label, listed, first_seen_at, last_seen_at)"
2177
+ " VALUES (@key, @label, 0, @now, @now) ON CONFLICT(provider_key) DO UPDATE SET"
2178
+ " last_seen_at = excluded.last_seen_at,"
2179
+ // An unlisted key's first_seen_at counts from its current label
2180
+ " first_seen_at = CASE WHEN first_seen_at = 0 OR (listed = 0 AND label != excluded.label)"
2181
+ " THEN excluded.first_seen_at ELSE first_seen_at END,"
2182
+ // A listed key keeps the list's label, which the seed puts back at every start
2183
+ " label = CASE WHEN listed = 0 THEN excluded.label ELSE label END";
2184
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
2185
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2186
+ return 1;
2187
+ }
2188
+ const bool done = 0 == bindDataVector(stmt, "@key", key) &&
2189
+ 0 == bindText(stmt, "@label", label) && 0 == bindInt64(stmt, "@now", now) &&
2190
+ sqlite3_step(stmt) == SQLITE_DONE;
2191
+ if (!done) {
2192
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
2193
+ }
2194
+ sqlite3_finalize(stmt);
2195
+ return done ? 0 : 1;
2196
+ }
2197
+
2198
+ int BSXDB::loadNodeScan(NodeScan &scan, int coin_type)
2199
+ {
2200
+ scan = NodeScan();
2201
+ const auto each = [&](const char *sql, const std::function<void(sqlite3_stmt *)> &row) {
2202
+ sqlite3_stmt *stmt;
2203
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
2204
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2205
+ return 1;
2206
+ }
2207
+ if (0 != bindInt(stmt, "@coin_type", coin_type)) {
2208
+ sqlite3_finalize(stmt);
2209
+ return 1;
2210
+ }
2211
+ int rc;
2212
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
2213
+ row(stmt);
2214
+ }
2215
+ sqlite3_finalize(stmt);
2216
+ if (rc != SQLITE_DONE) {
2217
+ LOGE("Selecting the node scan: %s", sqlite3_errmsg(m_db));
2218
+ return 1;
2219
+ }
2220
+ return 0;
2221
+ };
2222
+ const auto hexOf = [&](sqlite3_stmt *stmt, size_t column) {
2223
+ std::vector<unsigned char> bytes;
2224
+ setDataVector(stmt, column, bytes);
2225
+ return hex_str(bytes);
2226
+ };
2227
+ if (0 != each("SELECT block_height, block_hash FROM node_scan_blocks"
2228
+ " WHERE coin_type = @coin_type",
2229
+ [&](sqlite3_stmt *stmt) {
2230
+ scan.m_block_hashes[sqlite3_column_int(stmt, 0)] = hexOf(stmt, 1);
2231
+ }) ||
2232
+ 0 != each("SELECT script, since, from_height FROM node_scan_watches"
2233
+ " WHERE coin_type = @coin_type",
2234
+ [&](sqlite3_stmt *stmt) {
2235
+ std::vector<unsigned char> script;
2236
+ setDataVector(stmt, size_t{0}, script);
2237
+ scan.m_watches[script] = NodeScan::Watch{sqlite3_column_int64(stmt, 1),
2238
+ sqlite3_column_int(stmt, 2)};
2239
+ }) ||
2240
+ 0 != each("SELECT txid, txout_n, script, value, block_height, spent_by, spent_height"
2241
+ " FROM node_scan_outputs WHERE coin_type = @coin_type",
2242
+ [&](sqlite3_stmt *stmt) {
2243
+ NodeScan::Output output;
2244
+ output.m_txid = hexOf(stmt, 0);
2245
+ output.m_n = sqlite3_column_int(stmt, 1);
2246
+ setDataVector(stmt, size_t{2}, output.m_script);
2247
+ output.m_value = sqlite3_column_int64(stmt, 3);
2248
+ output.m_height = sqlite3_column_int(stmt, 4);
2249
+ output.m_spent_by = hexOf(stmt, 5);
2250
+ output.m_spent_height = sqlite3_column_int(stmt, 6);
2251
+ scan.m_outputs.push_back(std::move(output));
2252
+ })) {
2253
+ scan = NodeScan();
2254
+ return 1;
2255
+ }
2256
+ return 0;
2257
+ }
2258
+
2259
+ int BSXDB::loadKeyBirthday(int64_t &time)
2260
+ {
2261
+ time = 0;
2262
+ const int rv = getKVInt(RESTORE_BIRTHDAY_KEY, time);
2263
+ if (rv == 0) {
2264
+ return 0;
2265
+ }
2266
+ if (rv != 2) {
2267
+ return 1;
2268
+ }
2269
+ std::vector<unsigned char> mnemonic;
2270
+ std::string birthday;
2271
+ if (0 == loadMainMnemonic(mnemonic, birthday) && !birthday.empty() &&
2272
+ 0 == iso_date_to_unix(time, birthday)) {
2273
+ return 0;
2274
+ }
2275
+ time = 0;
2276
+ return 0;
2277
+ }
2278
+
2279
+ int BSXDB::saveNodeScan(int coin_type, const NodeScan &scan)
2280
+ {
2281
+ Savepoint step(*this, "node_scan");
2282
+ if (!step.isOpen()) {
2283
+ return 1;
2284
+ }
2285
+ const auto run = [&](const char *sql, const std::function<bool(sqlite3_stmt *)> &bind) {
2286
+ sqlite3_stmt *stmt;
2287
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
2288
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2289
+ return false;
2290
+ }
2291
+ const bool done = 0 == bindInt(stmt, "@coin_type", coin_type) && bind(stmt) &&
2292
+ sqlite3_step(stmt) == SQLITE_DONE;
2293
+ if (!done) {
2294
+ LOGE("Writing the node scan: %s", sqlite3_errmsg(m_db));
2295
+ }
2296
+ sqlite3_finalize(stmt);
2297
+ return done;
2298
+ };
2299
+ const auto bytesOf = [](const std::string &hex) {
2300
+ std::vector<unsigned char> bytes;
2301
+ decode_hex(bytes, hex);
2302
+ return bytes;
2303
+ };
2304
+ const auto none = [](sqlite3_stmt *) { return true; };
2305
+ if (!run("DELETE FROM node_scan_blocks WHERE coin_type = @coin_type", none) ||
2306
+ !run("DELETE FROM node_scan_watches WHERE coin_type = @coin_type", none) ||
2307
+ !run("DELETE FROM node_scan_outputs WHERE coin_type = @coin_type", none)) {
2308
+ return 1;
2309
+ }
2310
+ // Bound without a copy, so each lives until its insert has run
2311
+ for (const auto &[height, hash] : scan.m_block_hashes) {
2312
+ const std::vector<unsigned char> hash_bytes = bytesOf(hash);
2313
+ if (!run("INSERT INTO node_scan_blocks (coin_type, block_height, block_hash)"
2314
+ " VALUES (@coin_type, @height, @hash)",
2315
+ [&](sqlite3_stmt *stmt) {
2316
+ return 0 == bindInt(stmt, "@height", height) &&
2317
+ 0 == bindDataVector(stmt, "@hash", hash_bytes);
2318
+ })) {
2319
+ return 1;
2320
+ }
2321
+ }
2322
+ for (const auto &[script, watch] : scan.m_watches) {
2323
+ if (!run("INSERT INTO node_scan_watches (coin_type, script, since, from_height)"
2324
+ " VALUES (@coin_type, @script, @since, @from)",
2325
+ [&](sqlite3_stmt *stmt) {
2326
+ return 0 == bindDataVector(stmt, "@script", script) &&
2327
+ 0 == bindInt64(stmt, "@since", watch.m_since) &&
2328
+ 0 == bindInt(stmt, "@from", watch.m_from);
2329
+ })) {
2330
+ return 1;
2331
+ }
2332
+ }
2333
+ for (const auto &output : scan.m_outputs) {
2334
+ const std::vector<unsigned char> txid = bytesOf(output.m_txid),
2335
+ spent_by = bytesOf(output.m_spent_by);
2336
+ if (!run("INSERT INTO node_scan_outputs (coin_type, txid, txout_n, script, value,"
2337
+ " block_height, spent_by, spent_height) VALUES (@coin_type, @txid, @n, @script,"
2338
+ " @value, @height, @spent_by, @spent_height)",
2339
+ [&](sqlite3_stmt *stmt) {
2340
+ return 0 == bindDataVector(stmt, "@txid", txid) &&
2341
+ 0 == bindInt(stmt, "@n", output.m_n) &&
2342
+ 0 == bindDataVector(stmt, "@script", output.m_script) &&
2343
+ 0 == bindInt64(stmt, "@value", output.m_value) &&
2344
+ 0 == bindInt(stmt, "@height", output.m_height) &&
2345
+ 0 == bindDataVector(stmt, "@spent_by", spent_by) &&
2346
+ 0 == bindInt(stmt, "@spent_height", output.m_spent_height);
2347
+ })) {
2348
+ return 1;
2349
+ }
2350
+ }
2351
+ return step.release();
2352
+ }
2353
+
2354
+ int BSXDB::loadKnownProvider(KnownProvider &provider, const std::vector<unsigned char> &key)
2355
+ {
2356
+ sqlite3_stmt *stmt;
2357
+ const char *sql = "SELECT label, listed, first_seen_at, last_seen_at FROM known_providers"
2358
+ " WHERE provider_key = @key";
2359
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
2360
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2361
+ return 1;
2362
+ }
2363
+ if (0 != bindDataVector(stmt, "@key", key)) {
2364
+ sqlite3_finalize(stmt);
2365
+ return 1;
2366
+ }
2367
+ int rv = 2;
2368
+ const int rc = sqlite3_step(stmt);
2369
+ if (rc == SQLITE_ROW) {
2370
+ provider.m_key = key;
2371
+ const unsigned char *label = sqlite3_column_text(stmt, 0);
2372
+ provider.m_label = label ? (const char *)label : "";
2373
+ provider.m_listed = sqlite3_column_int(stmt, 1) != 0;
2374
+ provider.m_first_seen_at = sqlite3_column_int64(stmt, 2);
2375
+ provider.m_last_seen_at = sqlite3_column_int64(stmt, 3);
2376
+ rv = 0;
2377
+ } else if (rc != SQLITE_DONE) {
2378
+ LOGE("Selecting from \"%s\": %s", "known_providers", sqlite3_errmsg(m_db));
2379
+ rv = 1;
2380
+ }
2381
+ sqlite3_finalize(stmt);
2382
+ return rv;
2383
+ }
2384
+
2385
+ int BSXDB::labelHeldByAnotherKey(bool &held, const std::string &label,
2386
+ const std::vector<unsigned char> &key, int64_t first_seen_at)
2387
+ {
2388
+ held = false;
2389
+ if (label.empty()) {
2390
+ return 0;
2391
+ }
2392
+ sqlite3_stmt *stmt;
2393
+ const char *sql = "SELECT COUNT(*) FROM known_providers WHERE label = @label"
2394
+ " AND provider_key != @key AND (listed = 1 OR (first_seen_at > 0 AND"
2395
+ " first_seen_at < @first_seen))";
2396
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
2397
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2398
+ return 1;
2399
+ }
2400
+ int rv = 1;
2401
+ if (0 == bindText(stmt, "@label", label) && 0 == bindDataVector(stmt, "@key", key) &&
2402
+ 0 == bindInt64(stmt, "@first_seen", first_seen_at) && sqlite3_step(stmt) == SQLITE_ROW) {
2403
+ held = sqlite3_column_int(stmt, 0) > 0;
2404
+ rv = 0;
2405
+ } else {
2406
+ LOGE("Selecting from \"%s\": %s", "known_providers", sqlite3_errmsg(m_db));
2407
+ }
2408
+ sqlite3_finalize(stmt);
2409
+ return rv;
2410
+ }
2411
+
2412
+ int BSXDB::deleteSavedAddress(int coin_type, const std::string &address)
2413
+ {
2414
+ sqlite3_stmt *stmt;
2415
+ const char *sql =
2416
+ "DELETE FROM saved_addresses WHERE coin_type = @coin_type AND address = @address";
2417
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2418
+ if (rc != SQLITE_OK) {
2419
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2420
+ return 1;
2421
+ }
2422
+ if (0 != bindInt(stmt, "@coin_type", coin_type) || 0 != bindText(stmt, "@address", address)) {
2423
+ sqlite3_finalize(stmt);
2424
+ return 1;
2425
+ }
2426
+ int rv = 0;
2427
+ if (sqlite3_step(stmt) != SQLITE_DONE) {
2428
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
2429
+ rv = 1;
2430
+ }
2431
+ sqlite3_finalize(stmt);
2432
+ return 0 == rv && 0 == sqlite3_changes(m_db) ? 2 : rv;
2433
+ }
2434
+
2435
+ int BSXDB::deletePinnedCert(const std::string &host, uint16_t port)
2436
+ {
2437
+ sqlite3_stmt *stmt;
2438
+ const char *sql = "DELETE FROM pinned_certs WHERE host = @host AND port = @port";
2439
+
2440
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2441
+ if (rc != SQLITE_OK) {
2442
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2443
+ return 1;
2444
+ }
2445
+ if (0 != bindText(stmt, "@host", host) || 0 != bindInt(stmt, "@port", port)) {
2446
+ sqlite3_finalize(stmt);
2447
+ return 1;
2448
+ }
2449
+
2450
+ int rv = 0;
2451
+ if (sqlite3_step(stmt) != SQLITE_DONE) {
2452
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
2453
+ rv = 1;
2454
+ }
2455
+ sqlite3_finalize(stmt);
2456
+ if (0 == rv && 0 == sqlite3_changes(m_db)) {
2457
+ return 2;
2458
+ }
2459
+ return rv;
2460
+ }
2461
+
2462
+ std::vector<unsigned char> serializeKeyPath(const std::vector<uint32_t> &path)
2463
+ {
2464
+ std::vector<unsigned char> data(path.size() * 4);
2465
+ for (size_t i = 0; i < path.size(); ++i) {
2466
+ data[i * 4] = path[i] >> 24;
2467
+ data[i * 4 + 1] = path[i] >> 16;
2468
+ data[i * 4 + 2] = path[i] >> 8;
2469
+ data[i * 4 + 3] = path[i];
2470
+ }
2471
+ return data;
2472
+ }
2473
+
2474
+ int deserializeKeyPath(std::vector<uint32_t> &path, const unsigned char *data, size_t len)
2475
+ {
2476
+ if (len < 4 || len % 4 != 0) {
2477
+ return 1;
2478
+ }
2479
+ path.resize(len / 4);
2480
+ for (size_t i = 0; i < path.size(); ++i) {
2481
+ path[i] = (uint32_t(data[i * 4]) << 24) | (uint32_t(data[i * 4 + 1]) << 16) |
2482
+ (uint32_t(data[i * 4 + 2]) << 8) | uint32_t(data[i * 4 + 3]);
2483
+ }
2484
+ return 0;
2485
+ }
2486
+
2487
+ int BSXDB::saveWalletKey(int coin_type, const std::vector<unsigned char> &key_id,
2488
+ const std::vector<uint32_t> &path,
2489
+ const std::vector<unsigned char> &pubkey, int purpose)
2490
+ {
2491
+ sqlite3_stmt *stmt;
2492
+
2493
+ const char *sql = "INSERT INTO wallet_keys (coin_type, key_id, path, pubkey, purpose)"
2494
+ " VALUES (?, ?, ?, ?, ?);";
2495
+
2496
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2497
+ if (rc != SQLITE_OK) {
2498
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2499
+ return 1;
2500
+ }
2501
+ std::vector<unsigned char> path_data = serializeKeyPath(path);
2502
+ sqlite3_bind_int(stmt, 1, coin_type);
2503
+ sqlite3_bind_blob(stmt, 2, key_id.data(), key_id.size(), SQLITE_STATIC);
2504
+ sqlite3_bind_blob(stmt, 3, path_data.data(), path_data.size(), SQLITE_STATIC);
2505
+ sqlite3_bind_blob(stmt, 4, pubkey.data(), pubkey.size(), SQLITE_STATIC);
2506
+ sqlite3_bind_int(stmt, 5, purpose);
2507
+
2508
+ int rv = 0;
2509
+ rc = sqlite3_step(stmt);
2510
+ if (rc != SQLITE_DONE) {
2511
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
2512
+ rv = 1;
2513
+ }
2514
+
2515
+ sqlite3_finalize(stmt);
2516
+ return rv;
2517
+ }
2518
+
2519
+ int BSXDB::loadWalletKey(std::vector<uint32_t> &path, int &purpose, int coin_type,
2520
+ const std::vector<unsigned char> &key_id)
2521
+ {
2522
+ sqlite3_stmt *stmt;
2523
+
2524
+ const char *sql = "SELECT path, purpose FROM wallet_keys"
2525
+ " WHERE coin_type = ? AND key_id = ?";
2526
+
2527
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2528
+ if (rc != SQLITE_OK) {
2529
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2530
+ return 1;
2531
+ }
2532
+ sqlite3_bind_int(stmt, 1, coin_type);
2533
+ sqlite3_bind_blob(stmt, 2, key_id.data(), key_id.size(), SQLITE_STATIC);
2534
+
2535
+ int rv = 1;
2536
+ rc = sqlite3_step(stmt);
2537
+ if (rc == SQLITE_ROW) {
2538
+ const unsigned char *data = (const unsigned char *)sqlite3_column_blob(stmt, 0);
2539
+ rv = deserializeKeyPath(path, data, sqlite3_column_bytes(stmt, 0));
2540
+ purpose = sqlite3_column_int(stmt, 1);
2541
+ } else if (rc == SQLITE_ERROR) {
2542
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
2543
+ } else if (rc == SQLITE_DONE) {
2544
+ rv = 2;
2545
+ }
2546
+ sqlite3_finalize(stmt);
2547
+ return rv;
2548
+ }
2549
+
2550
+ int BSXDB::getNextWalletKeyIndex(int &key_index, int coin_type, int change, int purpose)
2551
+ {
2552
+ sqlite3_stmt *stmt;
2553
+
2554
+ // An index is never reused, so this counts from the highest issued rather
2555
+ // than from the row count: a deleted row must not hand the key out again.
2556
+ // MAX over the blob is MAX over the index because the paths compared are
2557
+ // fixed-width big-endian and share the change prefix.
2558
+ const char *sql = "SELECT MAX(path) FROM wallet_keys WHERE coin_type = ? AND purpose = ?"
2559
+ " AND length(path) = 8 AND substr(path, 1, 4) = ?";
2560
+
2561
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2562
+ if (rc != SQLITE_OK) {
2563
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2564
+ return 1;
2565
+ }
2566
+ std::vector<unsigned char> prefix = serializeKeyPath({(uint32_t)change});
2567
+ sqlite3_bind_int(stmt, 1, coin_type);
2568
+ sqlite3_bind_int(stmt, 2, purpose);
2569
+ sqlite3_bind_blob(stmt, 3, prefix.data(), prefix.size(), SQLITE_STATIC);
2570
+
2571
+ int rv = 1;
2572
+ rc = sqlite3_step(stmt);
2573
+ if (rc == SQLITE_ROW) {
2574
+ if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
2575
+ key_index = 0;
2576
+ rv = 0;
2577
+ } else {
2578
+ std::vector<uint32_t> path;
2579
+ const unsigned char *data = (const unsigned char *)sqlite3_column_blob(stmt, 0);
2580
+ if (0 == deserializeKeyPath(path, data, sqlite3_column_bytes(stmt, 0)) &&
2581
+ path.size() == 2 && path[1] < 0x7fffffff) {
2582
+ key_index = path[1] + 1;
2583
+ rv = 0;
2584
+ }
2585
+ }
2586
+ } else if (rc == SQLITE_ERROR) {
2587
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
2588
+ }
2589
+ sqlite3_finalize(stmt);
2590
+ return rv;
2591
+ }
2592
+
2593
+ static void bindBytesOrNull(sqlite3_stmt *stmt, int index, const std::vector<unsigned char> &data)
2594
+ {
2595
+ if (data.empty()) {
2596
+ sqlite3_bind_null(stmt, index);
2597
+ } else {
2598
+ sqlite3_bind_blob(stmt, index, data.data(), data.size(), SQLITE_STATIC);
2599
+ }
2600
+ }
2601
+
2602
+ static void bindBytes(sqlite3_stmt *stmt, int index, const std::vector<unsigned char> &data)
2603
+ {
2604
+ sqlite3_bind_blob(stmt, index, data.data(), data.size(), SQLITE_STATIC);
2605
+ }
2606
+
2607
+ int BSXDB::stepWalletOutputs(const char *sql, const std::function<void(sqlite3_stmt *)> &bind,
2608
+ int *changes)
2609
+ {
2610
+ sqlite3_stmt *stmt;
2611
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2612
+ if (rc != SQLITE_OK) {
2613
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2614
+ return 1;
2615
+ }
2616
+ bind(stmt);
2617
+ int rv = 0;
2618
+ rc = sqlite3_step(stmt);
2619
+ if (rc != SQLITE_DONE) {
2620
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
2621
+ rv = 1;
2622
+ } else if (changes) {
2623
+ *changes = sqlite3_changes(m_db);
2624
+ }
2625
+ sqlite3_finalize(stmt);
2626
+ return rv;
2627
+ }
2628
+
2629
+ int BSXDB::queryWalletOutputs(std::vector<WalletOutput> &outputs, const char *filter,
2630
+ const std::function<void(sqlite3_stmt *)> &bind)
2631
+ {
2632
+ outputs.clear();
2633
+ const std::string sql =
2634
+ std::string("SELECT coin_type, txid, txout_n, txout_value, txout_script, key_id, chain,"
2635
+ " block_hash, block_height, spend_txid, state FROM wallet_outputs WHERE ") +
2636
+ filter + " ORDER BY id;";
2637
+ sqlite3_stmt *stmt;
2638
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
2639
+ if (rc != SQLITE_OK) {
2640
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2641
+ return 1;
2642
+ }
2643
+ bind(stmt);
2644
+ int rv = 0;
2645
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
2646
+ WalletOutput &output = outputs.emplace_back();
2647
+ output.m_coin_type = sqlite3_column_int(stmt, 0);
2648
+ setDataVector(stmt, 1, output.m_txid);
2649
+ output.m_txout_n = sqlite3_column_int(stmt, 2);
2650
+ output.m_txout_value = sqlite3_column_int64(stmt, 3);
2651
+ setDataVector(stmt, 4, output.m_txout_script);
2652
+ setDataVector(stmt, 5, output.m_key_id);
2653
+ output.m_chain = sqlite3_column_int(stmt, 6);
2654
+ setDataVector(stmt, 7, output.m_block_hash);
2655
+ output.m_block_height =
2656
+ sqlite3_column_type(stmt, 8) == SQLITE_NULL ? -1 : sqlite3_column_int(stmt, 8);
2657
+ setDataVector(stmt, 9, output.m_spend_txid);
2658
+ output.m_state = sqlite3_column_int(stmt, 10);
2659
+ }
2660
+ if (rc != SQLITE_DONE) {
2661
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
2662
+ rv = 1;
2663
+ }
2664
+ sqlite3_finalize(stmt);
2665
+ return rv;
2666
+ }
2667
+
2668
+ int BSXDB::saveWalletOutput(const WalletOutput &output)
2669
+ {
2670
+ return stepWalletOutputs(
2671
+ "INSERT INTO wallet_outputs (coin_type, txid, txout_n, txout_value, txout_script, key_id,"
2672
+ " chain, block_hash, block_height, spend_txid, state)"
2673
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
2674
+ " ON CONFLICT (coin_type, txid, txout_n) DO UPDATE SET"
2675
+ " block_hash = excluded.block_hash, block_height = excluded.block_height,"
2676
+ " state = excluded.state;",
2677
+ [&](sqlite3_stmt *stmt) {
2678
+ sqlite3_bind_int(stmt, 1, output.m_coin_type);
2679
+ bindBytes(stmt, 2, output.m_txid);
2680
+ sqlite3_bind_int(stmt, 3, output.m_txout_n);
2681
+ sqlite3_bind_int64(stmt, 4, output.m_txout_value);
2682
+ bindBytes(stmt, 5, output.m_txout_script);
2683
+ bindBytes(stmt, 6, output.m_key_id);
2684
+ sqlite3_bind_int(stmt, 7, output.m_chain);
2685
+ bindBytesOrNull(stmt, 8, output.m_block_hash);
2686
+ if (output.m_block_height < 0) {
2687
+ sqlite3_bind_null(stmt, 9);
2688
+ } else {
2689
+ sqlite3_bind_int(stmt, 9, output.m_block_height);
2690
+ }
2691
+ bindBytesOrNull(stmt, 10, output.m_spend_txid);
2692
+ sqlite3_bind_int(stmt, 11, output.m_state);
2693
+ });
2694
+ }
2695
+
2696
+ int BSXDB::saveXmrWalletOutput(const XmrWalletOutput &output)
2697
+ {
2698
+ return stepWalletOutputs(
2699
+ "INSERT INTO xmr_wallet_outputs (coin_type, txid, txout_n, block_height, amount, subaddress_index,"
2700
+ " output_key, commitment, global_index, mask, derivation, tx_pubkey, unlock_time,"
2701
+ " key_image, spend_txid, spend_height)"
2702
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
2703
+ " ON CONFLICT (coin_type, txid, txout_n) DO UPDATE SET"
2704
+ " block_height = excluded.block_height, key_image = excluded.key_image,"
2705
+ " spend_txid = excluded.spend_txid, spend_height = excluded.spend_height;",
2706
+ [&](sqlite3_stmt *stmt) {
2707
+ sqlite3_bind_int(stmt, 1, output.m_coin_type);
2708
+ bindBytes(stmt, 2, output.m_txid);
2709
+ sqlite3_bind_int(stmt, 3, output.m_txout_n);
2710
+ sqlite3_bind_int64(stmt, 4, output.m_height);
2711
+ sqlite3_bind_int64(stmt, 5, output.m_amount);
2712
+ sqlite3_bind_int(stmt, 6, output.m_subaddress_index);
2713
+ bindBytes(stmt, 7, output.m_output_key);
2714
+ bindBytes(stmt, 8, output.m_commitment);
2715
+ sqlite3_bind_int64(stmt, 9, output.m_global_index);
2716
+ bindBytes(stmt, 10, output.m_mask);
2717
+ bindBytes(stmt, 11, output.m_derivation);
2718
+ bindBytes(stmt, 12, output.m_tx_pubkey);
2719
+ sqlite3_bind_int64(stmt, 13, output.m_unlock_time);
2720
+ bindBytesOrNull(stmt, 14, output.m_key_image);
2721
+ bindBytesOrNull(stmt, 15, output.m_spend_txid);
2722
+ sqlite3_bind_int64(stmt, 16, output.m_spend_height);
2723
+ });
2724
+ }
2725
+
2726
+ int BSXDB::loadXmrWalletOutputs(std::vector<XmrWalletOutput> &outputs, int coin_type)
2727
+ {
2728
+ outputs.clear();
2729
+ const char *sql =
2730
+ "SELECT txid, txout_n, block_height, amount, subaddress_index, output_key, commitment,"
2731
+ " global_index, mask, derivation, tx_pubkey, unlock_time, key_image, spend_txid,"
2732
+ " spend_height FROM xmr_wallet_outputs WHERE coin_type = ? ORDER BY block_height, id;";
2733
+ sqlite3_stmt *stmt;
2734
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2735
+ if (rc != SQLITE_OK) {
2736
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2737
+ return 1;
2738
+ }
2739
+ sqlite3_bind_int(stmt, 1, coin_type);
2740
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
2741
+ XmrWalletOutput &output = outputs.emplace_back();
2742
+ output.m_coin_type = coin_type;
2743
+ setDataVector(stmt, size_t{0}, output.m_txid);
2744
+ output.m_txout_n = sqlite3_column_int(stmt, 1);
2745
+ output.m_height = sqlite3_column_int64(stmt, 2);
2746
+ output.m_amount = sqlite3_column_int64(stmt, 3);
2747
+ output.m_subaddress_index = sqlite3_column_int(stmt, 4);
2748
+ setDataVector(stmt, 5, output.m_output_key);
2749
+ setDataVector(stmt, 6, output.m_commitment);
2750
+ output.m_global_index = sqlite3_column_int64(stmt, 7);
2751
+ setDataVector(stmt, 8, output.m_mask);
2752
+ setDataVector(stmt, 9, output.m_derivation);
2753
+ setDataVector(stmt, 10, output.m_tx_pubkey);
2754
+ output.m_unlock_time = sqlite3_column_int64(stmt, 11);
2755
+ setDataVector(stmt, 12, output.m_key_image);
2756
+ setDataVector(stmt, 13, output.m_spend_txid);
2757
+ output.m_spend_height = sqlite3_column_int64(stmt, 14);
2758
+ }
2759
+ sqlite3_finalize(stmt);
2760
+ if (rc != SQLITE_DONE) {
2761
+ LOGE("loadXmrWalletOutputs: %s", sqlite3_errmsg(m_db));
2762
+ return 1;
2763
+ }
2764
+ return 0;
2765
+ }
2766
+
2767
+ int BSXDB::deleteXmrWalletOutput(int coin_type, const std::vector<unsigned char> &txid, int txout_n)
2768
+ {
2769
+ return stepWalletOutputs(
2770
+ "DELETE FROM xmr_wallet_outputs WHERE coin_type = ? AND txid = ? AND txout_n = ?;",
2771
+ [&](sqlite3_stmt *stmt) {
2772
+ sqlite3_bind_int(stmt, 1, coin_type);
2773
+ bindBytes(stmt, 2, txid);
2774
+ sqlite3_bind_int(stmt, 3, txout_n);
2775
+ });
2776
+ }
2777
+
2778
+ int BSXDB::setWalletOutputSpent(int coin_type, const std::vector<unsigned char> &txid, int txout_n,
2779
+ const std::vector<unsigned char> &spend_txid)
2780
+ {
2781
+ int changes{0};
2782
+ if (0 != stepWalletOutputs(
2783
+ "UPDATE wallet_outputs SET spend_txid = ?"
2784
+ " WHERE coin_type = ? AND txid = ? AND txout_n = ?;",
2785
+ [&](sqlite3_stmt *stmt) {
2786
+ bindBytes(stmt, 1, spend_txid);
2787
+ sqlite3_bind_int(stmt, 2, coin_type);
2788
+ bindBytes(stmt, 3, txid);
2789
+ sqlite3_bind_int(stmt, 4, txout_n);
2790
+ },
2791
+ &changes)) {
2792
+ return 1;
2793
+ }
2794
+ return changes == 0 ? 2 : 0;
2795
+ }
2796
+
2797
+ int BSXDB::setWalletTxBlock(int coin_type, const std::vector<unsigned char> &txid,
2798
+ const std::vector<unsigned char> &block_hash, int block_height,
2799
+ int state)
2800
+ {
2801
+ return stepWalletOutputs("UPDATE wallet_outputs SET block_hash = ?, block_height = ?, state = ?"
2802
+ " WHERE coin_type = ? AND txid = ?;",
2803
+ [&](sqlite3_stmt *stmt) {
2804
+ bindBytesOrNull(stmt, 1, block_hash);
2805
+ if (block_height < 0) {
2806
+ sqlite3_bind_null(stmt, 2);
2807
+ } else {
2808
+ sqlite3_bind_int(stmt, 2, block_height);
2809
+ }
2810
+ sqlite3_bind_int(stmt, 3, state);
2811
+ sqlite3_bind_int(stmt, 4, coin_type);
2812
+ bindBytes(stmt, 5, txid);
2813
+ });
2814
+ }
2815
+
2816
+ int BSXDB::dropWalletTx(int coin_type, const std::vector<unsigned char> &txid)
2817
+ {
2818
+ const auto bind = [&](sqlite3_stmt *stmt) {
2819
+ sqlite3_bind_int(stmt, 1, coin_type);
2820
+ bindBytes(stmt, 2, txid);
2821
+ };
2822
+ if (0 !=
2823
+ stepWalletOutputs("DELETE FROM wallet_outputs WHERE coin_type = ? AND txid = ?;", bind)) {
2824
+ return 1;
2825
+ }
2826
+ return stepWalletOutputs("UPDATE wallet_outputs SET spend_txid = NULL"
2827
+ " WHERE coin_type = ? AND spend_txid = ?;",
2828
+ bind);
2829
+ }
2830
+
2831
+ int BSXDB::isWalletTxKnown(bool &known, int coin_type, const std::vector<unsigned char> &txid)
2832
+ {
2833
+ known = false;
2834
+ sqlite3_stmt *stmt;
2835
+ const char *sql = "SELECT EXISTS (SELECT 1 FROM wallet_outputs"
2836
+ " WHERE coin_type = ? AND (txid = ? OR spend_txid = ?));";
2837
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2838
+ if (rc != SQLITE_OK) {
2839
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2840
+ return 1;
2841
+ }
2842
+ sqlite3_bind_int(stmt, 1, coin_type);
2843
+ bindBytes(stmt, 2, txid);
2844
+ bindBytes(stmt, 3, txid);
2845
+ int rv = 1;
2846
+ if (sqlite3_step(stmt) == SQLITE_ROW) {
2847
+ known = sqlite3_column_int(stmt, 0) != 0;
2848
+ rv = 0;
2849
+ } else {
2850
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
2851
+ }
2852
+ sqlite3_finalize(stmt);
2853
+ return rv;
2854
+ }
2855
+
2856
+ // Change from the wallet's own tx: anyone can pay a change address, but only the wallet can
2857
+ // spend its outputs
2858
+ #define OWN_CHANGE \
2859
+ "(chain = 1 AND EXISTS (SELECT 1 FROM wallet_outputs s" \
2860
+ " WHERE s.coin_type = wallet_outputs.coin_type AND s.spend_txid = wallet_outputs.txid))"
2861
+
2862
+ int BSXDB::loadSpendableWalletOutputs(std::vector<WalletOutput> &outputs, int coin_type)
2863
+ {
2864
+ return queryWalletOutputs(outputs,
2865
+ "coin_type = ? AND spend_txid IS NULL AND (state = ? OR " OWN_CHANGE
2866
+ ")"
2867
+ " AND NOT EXISTS (SELECT 1 FROM wallet_holds h"
2868
+ " WHERE h.coin_type = wallet_outputs.coin_type"
2869
+ " AND h.txid = wallet_outputs.txid"
2870
+ " AND h.txout_n = wallet_outputs.txout_n)",
2871
+ [&](sqlite3_stmt *stmt) {
2872
+ sqlite3_bind_int(stmt, 1, coin_type);
2873
+ sqlite3_bind_int(stmt, 2, WalletOutputStates::CONFIRMED);
2874
+ });
2875
+ }
2876
+
2877
+ int BSXDB::saveWalletHolds(const std::vector<WalletHold> &holds)
2878
+ {
2879
+ Savepoint step(*this, "wallet_holds");
2880
+ if (!step.isOpen()) {
2881
+ return 1;
2882
+ }
2883
+ for (const auto &hold : holds) {
2884
+ int changes{0};
2885
+ if (0 != stepWalletOutputs(
2886
+ "INSERT INTO wallet_holds (coin_type, txid, txout_n, held_for,"
2887
+ " held_at, held_for_type) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING;",
2888
+ [&](sqlite3_stmt *stmt) {
2889
+ sqlite3_bind_int(stmt, 1, hold.m_coin_type);
2890
+ bindBytes(stmt, 2, hold.m_txid);
2891
+ sqlite3_bind_int(stmt, 3, hold.m_txout_n);
2892
+ bindBytes(stmt, 4, hold.m_held_for);
2893
+ sqlite3_bind_int64(stmt, 5, hold.m_held_at);
2894
+ sqlite3_bind_int(stmt, 6, hold.m_held_for_type);
2895
+ },
2896
+ &changes)) {
2897
+ return 1;
2898
+ }
2899
+ if (changes == 0) {
2900
+ return 2;
2901
+ }
2902
+ }
2903
+ return step.release();
2904
+ }
2905
+
2906
+ int BSXDB::releaseWalletHolds(int &released, int coin_type,
2907
+ const std::vector<unsigned char> &held_for)
2908
+ {
2909
+ released = 0;
2910
+ return stepWalletOutputs(
2911
+ "DELETE FROM wallet_holds WHERE coin_type = ? AND (? IS NULL OR held_for = ?);",
2912
+ [&](sqlite3_stmt *stmt) {
2913
+ sqlite3_bind_int(stmt, 1, coin_type);
2914
+ bindBytesOrNull(stmt, 2, held_for);
2915
+ bindBytesOrNull(stmt, 3, held_for);
2916
+ },
2917
+ &released);
2918
+ }
2919
+
2920
+ int BSXDB::releaseWalletHold(int coin_type, const std::vector<unsigned char> &txid, int txout_n)
2921
+ {
2922
+ return stepWalletOutputs(
2923
+ "DELETE FROM wallet_holds WHERE coin_type = ? AND txid = ? AND txout_n = ?;",
2924
+ [&](sqlite3_stmt *stmt) {
2925
+ sqlite3_bind_int(stmt, 1, coin_type);
2926
+ bindBytes(stmt, 2, txid);
2927
+ sqlite3_bind_int(stmt, 3, txout_n);
2928
+ });
2929
+ }
2930
+
2931
+ int BSXDB::rekeyWalletHolds(int coin_type, const std::vector<unsigned char> &held_for,
2932
+ const std::vector<unsigned char> &new_held_for)
2933
+ {
2934
+ sqlite3_stmt *stmt;
2935
+ const char *sql = "UPDATE wallet_holds SET held_for = @new_held_for"
2936
+ " WHERE coin_type = @coin_type AND held_for = @held_for";
2937
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
2938
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2939
+ return 1;
2940
+ }
2941
+ if (0 != bindInt(stmt, "@coin_type", coin_type) ||
2942
+ 0 != bindDataVector(stmt, "@held_for", held_for) ||
2943
+ 0 != bindDataVector(stmt, "@new_held_for", new_held_for)) {
2944
+ sqlite3_finalize(stmt);
2945
+ return 1;
2946
+ }
2947
+ int rv = 0;
2948
+ if (sqlite3_step(stmt) != SQLITE_DONE) {
2949
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
2950
+ rv = 1;
2951
+ }
2952
+ sqlite3_finalize(stmt);
2953
+ return rv;
2954
+ }
2955
+
2956
+ int BSXDB::loadWalletHolds(std::vector<WalletHold> &holds, int coin_type)
2957
+ {
2958
+ holds.clear();
2959
+ sqlite3_stmt *stmt;
2960
+ const char *sql = "SELECT txid, txout_n, held_for, held_at, held_for_type FROM wallet_holds"
2961
+ " WHERE coin_type = ? ORDER BY id;";
2962
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
2963
+ if (rc != SQLITE_OK) {
2964
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
2965
+ return 1;
2966
+ }
2967
+ sqlite3_bind_int(stmt, 1, coin_type);
2968
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
2969
+ WalletHold &hold = holds.emplace_back();
2970
+ hold.m_coin_type = coin_type;
2971
+ setDataVector(stmt, size_t{0}, hold.m_txid);
2972
+ hold.m_txout_n = sqlite3_column_int(stmt, 1);
2973
+ setDataVector(stmt, 2, hold.m_held_for);
2974
+ hold.m_held_at = sqlite3_column_int64(stmt, 3);
2975
+ hold.m_held_for_type = sqlite3_column_int(stmt, 4);
2976
+ }
2977
+ const int rv = rc == SQLITE_DONE ? 0 : 1;
2978
+ if (rv != 0) {
2979
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
2980
+ }
2981
+ sqlite3_finalize(stmt);
2982
+ return rv;
2983
+ }
2984
+
2985
+ int BSXDB::saveWalletSend(const WalletSend &send)
2986
+ {
2987
+ return stepWalletOutputs("INSERT INTO wallet_sends (coin_type, txid, address, amount, fee,"
2988
+ " sent_at) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING;",
2989
+ [&](sqlite3_stmt *stmt) {
2990
+ sqlite3_bind_int(stmt, 1, send.m_coin_type);
2991
+ bindBytes(stmt, 2, send.m_txid);
2992
+ sqlite3_bind_text(stmt, 3, send.m_address.c_str(), -1,
2993
+ SQLITE_STATIC);
2994
+ sqlite3_bind_int64(stmt, 4, send.m_amount);
2995
+ sqlite3_bind_int64(stmt, 5, send.m_fee);
2996
+ sqlite3_bind_int64(stmt, 6, send.m_sent_at);
2997
+ });
2998
+ }
2999
+
3000
+ int BSXDB::loadWalletSends(std::vector<WalletSend> &sends, int coin_type)
3001
+ {
3002
+ sends.clear();
3003
+ sqlite3_stmt *stmt;
3004
+ const char *sql = "SELECT txid, address, amount, fee, sent_at FROM wallet_sends"
3005
+ " WHERE coin_type = ? ORDER BY id;";
3006
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3007
+ if (rc != SQLITE_OK) {
3008
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3009
+ return 1;
3010
+ }
3011
+ sqlite3_bind_int(stmt, 1, coin_type);
3012
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
3013
+ WalletSend &send = sends.emplace_back();
3014
+ send.m_coin_type = coin_type;
3015
+ setDataVector(stmt, size_t{0}, send.m_txid);
3016
+ setString(stmt, 1, send.m_address);
3017
+ send.m_amount = sqlite3_column_int64(stmt, 2);
3018
+ send.m_fee = sqlite3_column_int64(stmt, 3);
3019
+ send.m_sent_at = sqlite3_column_int64(stmt, 4);
3020
+ }
3021
+ const int rv = rc == SQLITE_DONE ? 0 : 1;
3022
+ if (rv != 0) {
3023
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
3024
+ }
3025
+ sqlite3_finalize(stmt);
3026
+ return rv;
3027
+ }
3028
+
3029
+ int BSXDB::loadUnspentWalletOutputs(std::vector<WalletOutput> &outputs, int coin_type)
3030
+ {
3031
+ return queryWalletOutputs(outputs, "coin_type = ? AND spend_txid IS NULL",
3032
+ [&](sqlite3_stmt *stmt) { sqlite3_bind_int(stmt, 1, coin_type); });
3033
+ }
3034
+
3035
+ int BSXDB::loadWalletOutputs(std::vector<WalletOutput> &outputs, int coin_type)
3036
+ {
3037
+ return queryWalletOutputs(outputs, "coin_type = ?",
3038
+ [&](sqlite3_stmt *stmt) { sqlite3_bind_int(stmt, 1, coin_type); });
3039
+ }
3040
+
3041
+ int BSXDB::loadWalletOutputsForKey(std::vector<WalletOutput> &outputs, int coin_type,
3042
+ const std::vector<unsigned char> &key_id)
3043
+ {
3044
+ return queryWalletOutputs(outputs, "coin_type = ? AND key_id = ?", [&](sqlite3_stmt *stmt) {
3045
+ sqlite3_bind_int(stmt, 1, coin_type);
3046
+ bindBytes(stmt, 2, key_id);
3047
+ });
3048
+ }
3049
+
3050
+ int BSXDB::getWalletBalance(WalletBalance &balance, int coin_type)
3051
+ {
3052
+ balance = WalletBalance();
3053
+ sqlite3_stmt *stmt;
3054
+
3055
+ const char *sql =
3056
+ "SELECT"
3057
+ " COALESCE(SUM(CASE WHEN state = ? THEN txout_value END), 0),"
3058
+ " COALESCE(SUM(CASE WHEN state != ? AND NOT " OWN_CHANGE " THEN txout_value END), 0),"
3059
+ " COALESCE(SUM(CASE WHEN state != ? AND " OWN_CHANGE " THEN txout_value END), 0)"
3060
+ " FROM wallet_outputs WHERE coin_type = ? AND spend_txid IS NULL;";
3061
+
3062
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3063
+ if (rc != SQLITE_OK) {
3064
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3065
+ return 1;
3066
+ }
3067
+ for (int i = 1; i <= 3; ++i) {
3068
+ sqlite3_bind_int(stmt, i, WalletOutputStates::CONFIRMED);
3069
+ }
3070
+ sqlite3_bind_int(stmt, 4, coin_type);
3071
+
3072
+ int rv = 1;
3073
+ rc = sqlite3_step(stmt);
3074
+ if (rc == SQLITE_ROW) {
3075
+ balance.m_confirmed = sqlite3_column_int64(stmt, 0);
3076
+ balance.m_unconfirmed_incoming = sqlite3_column_int64(stmt, 1);
3077
+ balance.m_unconfirmed_change = sqlite3_column_int64(stmt, 2);
3078
+ rv = 0;
3079
+ } else {
3080
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
3081
+ }
3082
+ sqlite3_finalize(stmt);
3083
+ return rv;
3084
+ }
3085
+
3086
+ int BSXDB::getKVInt(const std::string &key, int64_t &value)
3087
+ {
3088
+ sqlite3_stmt *stmt;
3089
+
3090
+ const char *sql = "SELECT value FROM kvint WHERE key = ?";
3091
+
3092
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3093
+ if (rc != SQLITE_OK) {
3094
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3095
+ return 1;
3096
+ }
3097
+ sqlite3_bind_text(stmt, 1, key.c_str(), key.size(), SQLITE_STATIC);
3098
+
3099
+ int rv = 1;
3100
+ rc = sqlite3_step(stmt);
3101
+ if (rc == SQLITE_ROW) {
3102
+ value = sqlite3_column_int64(stmt, 0);
3103
+ rv = 0;
3104
+ } else if (rc == SQLITE_ERROR) {
3105
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
3106
+ } else if (rc == SQLITE_DONE) {
3107
+ rv = 2;
3108
+ }
3109
+ sqlite3_finalize(stmt);
3110
+ return rv;
3111
+ }
3112
+
3113
+ int BSXDB::getKVString(const std::string &key, std::string &value)
3114
+ {
3115
+ sqlite3_stmt *stmt;
3116
+ const char *sql = "SELECT value FROM kvstring WHERE key = ?";
3117
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3118
+ if (rc != SQLITE_OK) {
3119
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3120
+ return 1;
3121
+ }
3122
+ sqlite3_bind_text(stmt, 1, key.c_str(), key.size(), SQLITE_STATIC);
3123
+ int rv = 1;
3124
+ rc = sqlite3_step(stmt);
3125
+ if (rc == SQLITE_ROW) {
3126
+ const unsigned char *text = sqlite3_column_text(stmt, 0);
3127
+ value = text ? reinterpret_cast<const char *>(text) : "";
3128
+ rv = 0;
3129
+ }
3130
+ sqlite3_finalize(stmt);
3131
+ return rv;
3132
+ }
3133
+
3134
+ int BSXDB::setKVString(const std::string &key, const std::string &value)
3135
+ {
3136
+ sqlite3_stmt *stmt;
3137
+ const char *sql = "INSERT INTO kvstring (key, value) VALUES (?, ?)"
3138
+ " ON CONFLICT(key) DO UPDATE SET value = excluded.value;";
3139
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3140
+ if (rc != SQLITE_OK) {
3141
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3142
+ return 1;
3143
+ }
3144
+ sqlite3_bind_text(stmt, 1, key.c_str(), key.size(), SQLITE_STATIC);
3145
+ sqlite3_bind_text(stmt, 2, value.c_str(), value.size(), SQLITE_STATIC);
3146
+ int rv = 0;
3147
+ rc = sqlite3_step(stmt);
3148
+ if (rc != SQLITE_DONE) {
3149
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3150
+ rv = 1;
3151
+ }
3152
+ sqlite3_finalize(stmt);
3153
+ return rv;
3154
+ }
3155
+
3156
+ static int exec_sql(sqlite3 *db, const std::string &sql)
3157
+ {
3158
+ char *err{nullptr};
3159
+ if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &err)) {
3160
+ LOGE("Execution failed: %s", err ? err : sqlite3_errmsg(db));
3161
+ sqlite3_free(err);
3162
+ return 1;
3163
+ }
3164
+ return 0;
3165
+ }
3166
+
3167
+ int BSXDB::savepoint(const std::string &name)
3168
+ {
3169
+ // Never nested: rolling back an outer savepoint undoes an inner one's release
3170
+ assert(sqlite3_get_autocommit(m_db));
3171
+ return exec_sql(m_db, "SAVEPOINT " + name + ";");
3172
+ }
3173
+
3174
+ int BSXDB::releaseSavepoint(const std::string &name)
3175
+ {
3176
+ return exec_sql(m_db, "RELEASE " + name + ";");
3177
+ }
3178
+
3179
+ int BSXDB::rollbackToSavepoint(const std::string &name)
3180
+ {
3181
+ return exec_sql(m_db, "ROLLBACK TO " + name + "; RELEASE " + name + ";");
3182
+ }
3183
+
3184
+ Savepoint::Savepoint(BSXDB &db, const char *name) : m_db(db), m_name(name)
3185
+ {
3186
+ m_open = 0 == m_db.savepoint(m_name);
3187
+ }
3188
+
3189
+ Savepoint::~Savepoint() { rollback(); }
3190
+
3191
+ int Savepoint::release()
3192
+ {
3193
+ if (!m_open) {
3194
+ return 1;
3195
+ }
3196
+ m_open = false;
3197
+ if (0 != m_db.releaseSavepoint(m_name)) {
3198
+ m_db.rollbackToSavepoint(m_name);
3199
+ return 1;
3200
+ }
3201
+ return 0;
3202
+ }
3203
+
3204
+ void Savepoint::rollback()
3205
+ {
3206
+ // Closing the connection has already rolled it back
3207
+ if (m_open && m_db.isDBOpen()) {
3208
+ m_db.rollbackToSavepoint(m_name);
3209
+ }
3210
+ m_open = false;
3211
+ }
3212
+
3213
+ int BSXDB::setKVInt(const std::string &key, int64_t value)
3214
+ {
3215
+ sqlite3_stmt *stmt;
3216
+
3217
+ const char *sql = "INSERT INTO kvint (key, value) VALUES (?, ?)"
3218
+ " ON CONFLICT(key) DO UPDATE SET value = excluded.value;";
3219
+
3220
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3221
+ if (rc != SQLITE_OK) {
3222
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3223
+ return 1;
3224
+ }
3225
+ sqlite3_bind_text(stmt, 1, key.c_str(), key.size(), SQLITE_STATIC);
3226
+ sqlite3_bind_int64(stmt, 2, value);
3227
+
3228
+ int rv = 0;
3229
+ rc = sqlite3_step(stmt);
3230
+ if (rc != SQLITE_DONE) {
3231
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3232
+ rv = 1;
3233
+ }
3234
+
3235
+ sqlite3_finalize(stmt);
3236
+ return rv;
3237
+ }
3238
+
3239
+ int BSXDB::deleteKVString(const std::string &key)
3240
+ {
3241
+ sqlite3_stmt *stmt;
3242
+ const char *sql = "DELETE FROM kvstring WHERE key = ?";
3243
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3244
+ if (rc != SQLITE_OK) {
3245
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3246
+ return 1;
3247
+ }
3248
+ sqlite3_bind_text(stmt, 1, key.c_str(), key.size(), SQLITE_STATIC);
3249
+ int rv = 0;
3250
+ rc = sqlite3_step(stmt);
3251
+ if (rc != SQLITE_DONE) {
3252
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3253
+ rv = 1;
3254
+ }
3255
+ sqlite3_finalize(stmt);
3256
+ return rv;
3257
+ }
3258
+
3259
+ int BSXDB::deleteKVInt(const std::string &key)
3260
+ {
3261
+ sqlite3_stmt *stmt;
3262
+
3263
+ const char *sql = "DELETE FROM kvint WHERE key = ?";
3264
+
3265
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3266
+ if (rc != SQLITE_OK) {
3267
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3268
+ return 1;
3269
+ }
3270
+ sqlite3_bind_text(stmt, 1, key.c_str(), key.size(), SQLITE_STATIC);
3271
+
3272
+ int rv = 0;
3273
+ rc = sqlite3_step(stmt);
3274
+ if (rc != SQLITE_DONE) {
3275
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3276
+ rv = 1;
3277
+ }
3278
+
3279
+ sqlite3_finalize(stmt);
3280
+ return rv;
3281
+ }
3282
+
3283
+ int BSXDB::loadChainCaches(std::map<int, std::vector<unsigned char>> &caches)
3284
+ {
3285
+ caches.clear();
3286
+ sqlite3_stmt *stmt;
3287
+ const char *sql = "SELECT coin_type, data FROM chaincaches";
3288
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
3289
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3290
+ return 1;
3291
+ }
3292
+ int rc;
3293
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
3294
+ const auto *data = (const unsigned char *)sqlite3_column_blob(stmt, 1);
3295
+ const int size = sqlite3_column_bytes(stmt, 1);
3296
+ auto &cache = caches[sqlite3_column_int(stmt, 0)];
3297
+ if (data && size > 0) {
3298
+ cache.assign(data, data + size);
3299
+ }
3300
+ }
3301
+ sqlite3_finalize(stmt);
3302
+ if (rc != SQLITE_DONE) {
3303
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
3304
+ return 1;
3305
+ }
3306
+ return 0;
3307
+ }
3308
+
3309
+ int BSXDB::saveChainCache(int coin_type, const std::vector<unsigned char> &data)
3310
+ {
3311
+ sqlite3_stmt *stmt;
3312
+ const char *sql = data.empty() ? "DELETE FROM chaincaches WHERE coin_type = ?"
3313
+ : "INSERT INTO chaincaches (coin_type, data) VALUES (?, ?)"
3314
+ " ON CONFLICT(coin_type) DO UPDATE SET data = excluded.data;";
3315
+ if (SQLITE_OK != sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0)) {
3316
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3317
+ return 1;
3318
+ }
3319
+ sqlite3_bind_int(stmt, 1, coin_type);
3320
+ if (!data.empty()) {
3321
+ sqlite3_bind_blob(stmt, 2, data.data(), (int)data.size(), SQLITE_STATIC);
3322
+ }
3323
+ int rv = 0;
3324
+ if (sqlite3_step(stmt) != SQLITE_DONE) {
3325
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3326
+ rv = 1;
3327
+ }
3328
+ sqlite3_finalize(stmt);
3329
+ return rv;
3330
+ }
3331
+
3332
+ int BSXDB::getDbVersion(int &version)
3333
+ {
3334
+ sqlite3_stmt *stmt;
3335
+ int rc = sqlite3_prepare_v2(m_db, "PRAGMA user_version;", -1, &stmt, 0);
3336
+ if (rc != SQLITE_OK) {
3337
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3338
+ return 1;
3339
+ }
3340
+ int rv = 1;
3341
+ if (sqlite3_step(stmt) == SQLITE_ROW) {
3342
+ version = sqlite3_column_int(stmt, 0);
3343
+ rv = 0;
3344
+ }
3345
+ sqlite3_finalize(stmt);
3346
+ return rv;
3347
+ }
3348
+
3349
+ static const char *offer_columns =
3350
+ "offer_id, request_id, kind, bsx_offer_id, offerer_address, offerer_reliability, coin_from,"
3351
+ " coin_to, amount_from, amount_to, lock_type, lock_time_1, lock_time_2, fee_rate_from,"
3352
+ " fee_rate_to, expire_at, received_at, chosen, state, accepted_at, day_key_count, pubkey_a,"
3353
+ " pubkey_b, kbs_dleag, kbv, accept_utxos, change_script, coin_to_dest, fee_outputs,"
3354
+ " core_token_fees, provider_key, provider_label_changed";
3355
+
3356
+ static void readOffer(sqlite3_stmt *stmt, OfferData &offer)
3357
+ {
3358
+ auto read_blob = [stmt](int col, std::vector<unsigned char> &out) {
3359
+ const unsigned char *p = (const unsigned char *)sqlite3_column_blob(stmt, col);
3360
+ int len = sqlite3_column_bytes(stmt, col);
3361
+ out.assign(p, p + (p ? len : 0));
3362
+ };
3363
+ read_blob(0, offer.m_offer_id);
3364
+ read_blob(1, offer.m_request_id);
3365
+ offer.m_kind = sqlite3_column_int(stmt, 2);
3366
+ read_blob(3, offer.m_bsx_offer_id);
3367
+ const unsigned char *offerer_address = sqlite3_column_text(stmt, 4);
3368
+ offer.m_offerer_address = offerer_address ? (const char *)offerer_address : "";
3369
+ offer.m_offerer_reliability = sqlite3_column_int(stmt, 5);
3370
+ offer.m_coin_from = sqlite3_column_int(stmt, 6);
3371
+ offer.m_coin_to = sqlite3_column_int(stmt, 7);
3372
+ offer.m_amount_from = sqlite3_column_int64(stmt, 8);
3373
+ offer.m_amount_to = sqlite3_column_int64(stmt, 9);
3374
+ offer.m_lock_type = sqlite3_column_int(stmt, 10);
3375
+ offer.m_lock_time_1 = sqlite3_column_int(stmt, 11);
3376
+ offer.m_lock_time_2 = sqlite3_column_int(stmt, 12);
3377
+ offer.m_fee_rate_from = sqlite3_column_int64(stmt, 13);
3378
+ offer.m_fee_rate_to = sqlite3_column_int64(stmt, 14);
3379
+ offer.m_expire_at = sqlite3_column_int64(stmt, 15);
3380
+ offer.m_received_at = sqlite3_column_int64(stmt, 16);
3381
+ offer.m_chosen = sqlite3_column_int(stmt, 17) != 0;
3382
+ offer.m_state = sqlite3_column_int(stmt, 18);
3383
+ offer.m_accepted_at = sqlite3_column_int64(stmt, 19);
3384
+ offer.m_day_key_count = sqlite3_column_int(stmt, 20);
3385
+ read_blob(21, offer.m_pubkey_a);
3386
+ read_blob(22, offer.m_pubkey_b);
3387
+ read_blob(23, offer.m_kbs_dleag);
3388
+ read_blob(24, offer.m_kbv);
3389
+ read_blob(25, offer.m_accept_utxos);
3390
+ read_blob(26, offer.m_change_script);
3391
+ read_blob(27, offer.m_coin_to_dest);
3392
+ read_blob(28, offer.m_fee_outputs);
3393
+ read_blob(29, offer.m_core_token_fees);
3394
+ read_blob(30, offer.m_provider_key);
3395
+ offer.m_provider_label_changed = sqlite3_column_int(stmt, 31) != 0;
3396
+ }
3397
+
3398
+ int BSXDB::saveDeposit(const DepositInfo &deposit)
3399
+ {
3400
+ sqlite3_stmt *stmt;
3401
+
3402
+ const char *sql = "INSERT INTO deposits"
3403
+ " (deposit_id, coin_type, script, created_at, expire_at, state, amount_due,"
3404
+ " received, refund_address, coin_to, min_amount_to, address_to,"
3405
+ " created_height, spent_outpoints)"
3406
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
3407
+
3408
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3409
+ if (rc != SQLITE_OK) {
3410
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3411
+ return 1;
3412
+ }
3413
+ sqlite3_bind_blob(stmt, 1, deposit.m_deposit_id.data(), deposit.m_deposit_id.size(),
3414
+ SQLITE_STATIC);
3415
+ sqlite3_bind_int(stmt, 2, deposit.m_coin_type);
3416
+ sqlite3_bind_blob(stmt, 3, deposit.m_script.data(), deposit.m_script.size(), SQLITE_STATIC);
3417
+ sqlite3_bind_int64(stmt, 4, deposit.m_created_at);
3418
+ sqlite3_bind_int64(stmt, 5, deposit.m_expire_at);
3419
+ sqlite3_bind_int(stmt, 6, deposit.m_state);
3420
+ sqlite3_bind_int64(stmt, 7, deposit.m_amount_due);
3421
+ sqlite3_bind_int64(stmt, 8, deposit.m_received);
3422
+ sqlite3_bind_text(stmt, 9, deposit.m_refund_address.c_str(), -1, SQLITE_STATIC);
3423
+ sqlite3_bind_int(stmt, 10, deposit.m_coin_to);
3424
+ sqlite3_bind_int64(stmt, 11, deposit.m_min_amount_to);
3425
+ sqlite3_bind_text(stmt, 12, deposit.m_address_to.c_str(), -1, SQLITE_STATIC);
3426
+ sqlite3_bind_int(stmt, 13, deposit.m_created_height);
3427
+ sqlite3_bind_blob(stmt, 14, deposit.m_spent_outpoints.data(), deposit.m_spent_outpoints.size(),
3428
+ SQLITE_STATIC);
3429
+
3430
+ int rv = 0;
3431
+ rc = sqlite3_step(stmt);
3432
+ if (rc != SQLITE_DONE) {
3433
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3434
+ rv = 1;
3435
+ }
3436
+ sqlite3_finalize(stmt);
3437
+ return rv;
3438
+ }
3439
+
3440
+ int BSXDB::updateDeposit(const DepositInfo &deposit)
3441
+ {
3442
+ sqlite3_stmt *stmt;
3443
+
3444
+ const char *sql = "UPDATE deposits SET expire_at = ?, state = ?, amount_due = ?, received = ?,"
3445
+ " min_amount_to = ?, swap_request_id = ?, sweep_txid = ?, sweep_tx = ?,"
3446
+ " spent_outpoints = ?, refund_address = ?, coin_to = ?, address_to = ?,"
3447
+ " sweep_inputs = ?, sweep_signed_at = ?, replaced_sweeps = ?"
3448
+ " WHERE deposit_id = ?;";
3449
+
3450
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3451
+ if (rc != SQLITE_OK) {
3452
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3453
+ return 1;
3454
+ }
3455
+ sqlite3_bind_int64(stmt, 1, deposit.m_expire_at);
3456
+ sqlite3_bind_int(stmt, 2, deposit.m_state);
3457
+ sqlite3_bind_int64(stmt, 3, deposit.m_amount_due);
3458
+ sqlite3_bind_int64(stmt, 4, deposit.m_received);
3459
+ sqlite3_bind_int64(stmt, 5, deposit.m_min_amount_to);
3460
+ sqlite3_bind_blob(stmt, 6, deposit.m_request_id.data(), deposit.m_request_id.size(),
3461
+ SQLITE_STATIC);
3462
+ sqlite3_bind_blob(stmt, 7, deposit.m_sweep_txid.data(), deposit.m_sweep_txid.size(),
3463
+ SQLITE_STATIC);
3464
+ sqlite3_bind_blob(stmt, 8, deposit.m_sweep_tx.data(), deposit.m_sweep_tx.size(), SQLITE_STATIC);
3465
+ sqlite3_bind_blob(stmt, 9, deposit.m_spent_outpoints.data(), deposit.m_spent_outpoints.size(),
3466
+ SQLITE_STATIC);
3467
+ sqlite3_bind_text(stmt, 10, deposit.m_refund_address.c_str(), -1, SQLITE_STATIC);
3468
+ sqlite3_bind_int(stmt, 11, deposit.m_coin_to);
3469
+ sqlite3_bind_text(stmt, 12, deposit.m_address_to.c_str(), -1, SQLITE_STATIC);
3470
+ sqlite3_bind_blob(stmt, 13, deposit.m_sweep_inputs.data(), deposit.m_sweep_inputs.size(),
3471
+ SQLITE_STATIC);
3472
+ sqlite3_bind_int64(stmt, 14, deposit.m_sweep_signed_at);
3473
+ sqlite3_bind_blob(stmt, 15, deposit.m_replaced_sweeps.data(), deposit.m_replaced_sweeps.size(),
3474
+ SQLITE_STATIC);
3475
+ sqlite3_bind_blob(stmt, 16, deposit.m_deposit_id.data(), deposit.m_deposit_id.size(),
3476
+ SQLITE_STATIC);
3477
+
3478
+ int rv = 0;
3479
+ rc = sqlite3_step(stmt);
3480
+ if (rc != SQLITE_DONE) {
3481
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3482
+ rv = 1;
3483
+ } else if (sqlite3_changes(m_db) != 1) {
3484
+ rv = 2;
3485
+ }
3486
+ sqlite3_finalize(stmt);
3487
+ return rv;
3488
+ }
3489
+
3490
+ int BSXDB::loadDeposits(std::vector<DepositInfo> &deposits, const std::vector<FilterData> &filters)
3491
+ {
3492
+ deposits.clear();
3493
+ sqlite3_stmt *stmt;
3494
+
3495
+ std::string filter_columns;
3496
+ if (0 != expandFilterColumns(filter_columns, filters)) {
3497
+ LOGE("Failed to expand filter columns");
3498
+ return 1;
3499
+ }
3500
+ std::string sql =
3501
+ strprintf("SELECT deposit_id, coin_type, script, created_at, expire_at, state,"
3502
+ " amount_due, received, refund_address, coin_to, min_amount_to, address_to,"
3503
+ " swap_request_id, sweep_txid, sweep_tx, spent_outpoints, created_height,"
3504
+ " scan_height, sweep_inputs, sweep_signed_at, replaced_sweeps,"
3505
+ " EXISTS (SELECT 1 FROM eventlog e WHERE e.linked_type = %d"
3506
+ " AND e.linked_id = deposits.deposit_id AND e.event_type = %d) FROM deposits"
3507
+ " WHERE 1 = 1 %s ORDER BY created_at, id;",
3508
+ Concepts::DEPOSIT, EventLogTypes::DEPOSIT_CANCELLED, filter_columns);
3509
+
3510
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
3511
+ if (rc != SQLITE_OK) {
3512
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3513
+ return 1;
3514
+ }
3515
+ if (0 != bindFilterData(stmt, filters)) {
3516
+ LOGE("Failed to bind filter data");
3517
+ sqlite3_finalize(stmt);
3518
+ return 1;
3519
+ }
3520
+
3521
+ auto column_text = [&](int i) {
3522
+ const char *text = (const char *)sqlite3_column_text(stmt, i);
3523
+ return std::string(text ? text : "");
3524
+ };
3525
+ int rv = 0;
3526
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
3527
+ DepositInfo deposit;
3528
+ setDataVector(stmt, size_t(0), deposit.m_deposit_id);
3529
+ deposit.m_coin_type = sqlite3_column_int(stmt, 1);
3530
+ setDataVector(stmt, 2, deposit.m_script);
3531
+ deposit.m_created_at = sqlite3_column_int64(stmt, 3);
3532
+ deposit.m_expire_at = sqlite3_column_int64(stmt, 4);
3533
+ deposit.m_state = sqlite3_column_int(stmt, 5);
3534
+ deposit.m_amount_due = sqlite3_column_int64(stmt, 6);
3535
+ deposit.m_received = sqlite3_column_int64(stmt, 7);
3536
+ deposit.m_refund_address = column_text(8);
3537
+ deposit.m_coin_to = sqlite3_column_int(stmt, 9);
3538
+ deposit.m_min_amount_to = sqlite3_column_int64(stmt, 10);
3539
+ deposit.m_address_to = column_text(11);
3540
+ setDataVector(stmt, 12, deposit.m_request_id);
3541
+ setDataVector(stmt, 13, deposit.m_sweep_txid);
3542
+ setDataVector(stmt, 14, deposit.m_sweep_tx);
3543
+ setDataVector(stmt, 15, deposit.m_spent_outpoints);
3544
+ deposit.m_created_height = sqlite3_column_int(stmt, 16);
3545
+ deposit.m_scan_height = sqlite3_column_int64(stmt, 17);
3546
+ setDataVector(stmt, 18, deposit.m_sweep_inputs);
3547
+ deposit.m_sweep_signed_at = sqlite3_column_int64(stmt, 19);
3548
+ setDataVector(stmt, 20, deposit.m_replaced_sweeps);
3549
+ deposit.m_cancelled = sqlite3_column_int(stmt, 21) != 0;
3550
+ deposits.push_back(std::move(deposit));
3551
+ }
3552
+ if (rc != SQLITE_DONE) {
3553
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
3554
+ rv = 1;
3555
+ }
3556
+ sqlite3_finalize(stmt);
3557
+ return rv;
3558
+ }
3559
+
3560
+ int BSXDB::saveWalletLock(const WalletLock &lock)
3561
+ {
3562
+ sqlite3_stmt *stmt;
3563
+
3564
+ // A coin locked again belongs to whichever request locked it last, so the
3565
+ // new row replaces the old one rather than joining it.
3566
+ const char *sql = "INSERT INTO walletlocks"
3567
+ " (coin, txid, vout, lock_handle, request_id, value, locked_at, released)"
3568
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
3569
+ " ON CONFLICT (coin, txid, vout) DO UPDATE SET"
3570
+ " lock_handle = excluded.lock_handle, request_id = excluded.request_id,"
3571
+ " value = excluded.value, locked_at = excluded.locked_at,"
3572
+ " released = excluded.released;";
3573
+
3574
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3575
+ if (rc != SQLITE_OK) {
3576
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3577
+ return 1;
3578
+ }
3579
+ sqlite3_bind_int(stmt, 1, lock.m_coin);
3580
+ sqlite3_bind_blob(stmt, 2, lock.m_txid.data(), lock.m_txid.size(), SQLITE_STATIC);
3581
+ sqlite3_bind_int(stmt, 3, lock.m_vout);
3582
+ sqlite3_bind_blob(stmt, 4, lock.m_handle.data(), lock.m_handle.size(), SQLITE_STATIC);
3583
+ sqlite3_bind_blob(stmt, 5, lock.m_request_id.data(), lock.m_request_id.size(), SQLITE_STATIC);
3584
+ sqlite3_bind_int64(stmt, 6, lock.m_value);
3585
+ sqlite3_bind_int64(stmt, 7, lock.m_locked_at);
3586
+ sqlite3_bind_int(stmt, 8, lock.m_released ? 1 : 0);
3587
+
3588
+ int rv = 0;
3589
+ rc = sqlite3_step(stmt);
3590
+ if (rc != SQLITE_DONE) {
3591
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3592
+ rv = 1;
3593
+ }
3594
+ sqlite3_finalize(stmt);
3595
+ return rv;
3596
+ }
3597
+
3598
+ int BSXDB::loadWalletLocks(std::vector<WalletLock> &locks, const RequestId *request_id,
3599
+ bool unreleased_only)
3600
+ {
3601
+ locks.clear();
3602
+ sqlite3_stmt *stmt;
3603
+
3604
+ std::string sql = "SELECT coin, txid, vout, lock_handle, request_id, value, locked_at, released"
3605
+ " FROM walletlocks WHERE (? = 0 OR released = 0)";
3606
+ if (request_id != nullptr) {
3607
+ sql += " AND request_id = ?";
3608
+ }
3609
+ sql += " ORDER BY locked_at, txid, vout;";
3610
+
3611
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
3612
+ if (rc != SQLITE_OK) {
3613
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3614
+ return 1;
3615
+ }
3616
+ sqlite3_bind_int(stmt, 1, unreleased_only ? 1 : 0);
3617
+ if (request_id != nullptr) {
3618
+ sqlite3_bind_blob(stmt, 2, request_id->data(), request_id->size(), SQLITE_STATIC);
3619
+ }
3620
+
3621
+ int rv = 0;
3622
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
3623
+ WalletLock lock;
3624
+ lock.m_coin = sqlite3_column_int(stmt, 0);
3625
+ setDataVector(stmt, 1, lock.m_txid);
3626
+ lock.m_vout = sqlite3_column_int(stmt, 2);
3627
+ setDataVector(stmt, 3, lock.m_handle);
3628
+ setDataVector(stmt, 4, lock.m_request_id);
3629
+ lock.m_value = sqlite3_column_int64(stmt, 5);
3630
+ lock.m_locked_at = sqlite3_column_int64(stmt, 6);
3631
+ lock.m_released = sqlite3_column_int(stmt, 7) != 0;
3632
+ locks.push_back(std::move(lock));
3633
+ }
3634
+ if (rc != SQLITE_DONE) {
3635
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
3636
+ rv = 1;
3637
+ }
3638
+ sqlite3_finalize(stmt);
3639
+ return rv;
3640
+ }
3641
+
3642
+ int BSXDB::setWalletLockReleased(int coin, const std::vector<unsigned char> &txid, int vout,
3643
+ const RequestId &request_id)
3644
+ {
3645
+ sqlite3_stmt *stmt;
3646
+
3647
+ // The outpoint is unique, so a coin relocked by another request is this row.
3648
+ const char *sql = "UPDATE walletlocks SET released = 1"
3649
+ " WHERE coin = ? AND txid = ? AND vout = ? AND request_id = ?;";
3650
+
3651
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3652
+ if (rc != SQLITE_OK) {
3653
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3654
+ return 1;
3655
+ }
3656
+ sqlite3_bind_int(stmt, 1, coin);
3657
+ sqlite3_bind_blob(stmt, 2, txid.data(), txid.size(), SQLITE_STATIC);
3658
+ sqlite3_bind_int(stmt, 3, vout);
3659
+ sqlite3_bind_blob(stmt, 4, request_id.data(), request_id.size(), SQLITE_STATIC);
3660
+
3661
+ int rv = 0;
3662
+ rc = sqlite3_step(stmt);
3663
+ if (rc != SQLITE_DONE) {
3664
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3665
+ rv = 1;
3666
+ }
3667
+ sqlite3_finalize(stmt);
3668
+ // No row matched: another request owns it now, which is not an sqlite error
3669
+ if (rv == 0 && sqlite3_changes(m_db) < 1) {
3670
+ return 2;
3671
+ }
3672
+ return rv;
3673
+ }
3674
+
3675
+ int BSXDB::saveOffer(const OfferData &offer)
3676
+ {
3677
+ sqlite3_stmt *stmt;
3678
+
3679
+ std::string sql = std::string("INSERT INTO offers (") + offer_columns +
3680
+ ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,"
3681
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
3682
+
3683
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
3684
+ if (rc != SQLITE_OK) {
3685
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3686
+ return 1;
3687
+ }
3688
+ auto bind_blob = [stmt](int col, const std::vector<unsigned char> &in) {
3689
+ sqlite3_bind_blob(stmt, col, in.data(), in.size(), SQLITE_STATIC);
3690
+ };
3691
+ bind_blob(1, offer.m_offer_id);
3692
+ bind_blob(2, offer.m_request_id);
3693
+ sqlite3_bind_int(stmt, 3, offer.m_kind);
3694
+ bind_blob(4, offer.m_bsx_offer_id);
3695
+ sqlite3_bind_text(stmt, 5, offer.m_offerer_address.c_str(), (int)offer.m_offerer_address.size(),
3696
+ SQLITE_STATIC);
3697
+ sqlite3_bind_int(stmt, 6, offer.m_offerer_reliability);
3698
+ sqlite3_bind_int(stmt, 7, offer.m_coin_from);
3699
+ sqlite3_bind_int(stmt, 8, offer.m_coin_to);
3700
+ sqlite3_bind_int64(stmt, 9, offer.m_amount_from);
3701
+ sqlite3_bind_int64(stmt, 10, offer.m_amount_to);
3702
+ sqlite3_bind_int(stmt, 11, offer.m_lock_type);
3703
+ sqlite3_bind_int(stmt, 12, offer.m_lock_time_1);
3704
+ sqlite3_bind_int(stmt, 13, offer.m_lock_time_2);
3705
+ sqlite3_bind_int64(stmt, 14, offer.m_fee_rate_from);
3706
+ sqlite3_bind_int64(stmt, 15, offer.m_fee_rate_to);
3707
+ sqlite3_bind_int64(stmt, 16, offer.m_expire_at);
3708
+ sqlite3_bind_int64(stmt, 17, offer.m_received_at);
3709
+ sqlite3_bind_int(stmt, 18, offer.m_chosen ? 1 : 0);
3710
+ sqlite3_bind_int(stmt, 19, offer.m_state);
3711
+ sqlite3_bind_int64(stmt, 20, offer.m_accepted_at);
3712
+ sqlite3_bind_int(stmt, 21, offer.m_day_key_count);
3713
+ bind_blob(22, offer.m_pubkey_a);
3714
+ bind_blob(23, offer.m_pubkey_b);
3715
+ bind_blob(24, offer.m_kbs_dleag);
3716
+ bind_blob(25, offer.m_kbv);
3717
+ bind_blob(26, offer.m_accept_utxos);
3718
+ bind_blob(27, offer.m_change_script);
3719
+ bind_blob(28, offer.m_coin_to_dest);
3720
+ bind_blob(29, offer.m_fee_outputs);
3721
+ bind_blob(30, offer.m_core_token_fees);
3722
+ bind_blob(31, offer.m_provider_key);
3723
+ sqlite3_bind_int(stmt, 32, offer.m_provider_label_changed ? 1 : 0);
3724
+
3725
+ int rv = 0;
3726
+ rc = sqlite3_step(stmt);
3727
+ if (rc != SQLITE_DONE) {
3728
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3729
+ rv = 1;
3730
+ }
3731
+ sqlite3_finalize(stmt);
3732
+ return rv;
3733
+ }
3734
+
3735
+ int BSXDB::loadOffer(OfferData &offer, const std::vector<unsigned char> &offer_id)
3736
+ {
3737
+ sqlite3_stmt *stmt;
3738
+
3739
+ std::string sql = std::string("SELECT ") + offer_columns + " FROM offers WHERE offer_id = ?;";
3740
+
3741
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
3742
+ if (rc != SQLITE_OK) {
3743
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3744
+ return 1;
3745
+ }
3746
+ sqlite3_bind_blob(stmt, 1, offer_id.data(), offer_id.size(), SQLITE_STATIC);
3747
+
3748
+ int rv = 1;
3749
+ rc = sqlite3_step(stmt);
3750
+ if (rc == SQLITE_ROW) {
3751
+ readOffer(stmt, offer);
3752
+ rv = 0;
3753
+ } else if (rc != SQLITE_DONE) {
3754
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
3755
+ }
3756
+ sqlite3_finalize(stmt);
3757
+ return rv;
3758
+ }
3759
+
3760
+ int BSXDB::loadOffers(std::vector<OfferData> &offers, const RequestId &request_id, bool chosen_only)
3761
+ {
3762
+ offers.clear();
3763
+ sqlite3_stmt *stmt;
3764
+
3765
+ std::string sql = std::string("SELECT ") + offer_columns +
3766
+ " FROM offers WHERE request_id = ? AND (? = 0 OR chosen = 1)"
3767
+ " ORDER BY received_at, offer_id;";
3768
+
3769
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
3770
+ if (rc != SQLITE_OK) {
3771
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3772
+ return 1;
3773
+ }
3774
+ sqlite3_bind_blob(stmt, 1, request_id.data(), request_id.size(), SQLITE_STATIC);
3775
+ sqlite3_bind_int(stmt, 2, chosen_only ? 1 : 0);
3776
+
3777
+ int rv = 0;
3778
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
3779
+ OfferData offer;
3780
+ readOffer(stmt, offer);
3781
+ offers.push_back(std::move(offer));
3782
+ }
3783
+ if (rc != SQLITE_DONE) {
3784
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
3785
+ rv = 1;
3786
+ }
3787
+ sqlite3_finalize(stmt);
3788
+ return rv;
3789
+ }
3790
+
3791
+ int BSXDB::setOfferChosen(const std::vector<unsigned char> &offer_id, bool chosen)
3792
+ {
3793
+ sqlite3_stmt *stmt;
3794
+
3795
+ const char *sql = "UPDATE offers SET chosen = ? WHERE offer_id = ?;";
3796
+
3797
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3798
+ if (rc != SQLITE_OK) {
3799
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3800
+ return 1;
3801
+ }
3802
+ sqlite3_bind_int(stmt, 1, chosen ? 1 : 0);
3803
+ sqlite3_bind_blob(stmt, 2, offer_id.data(), offer_id.size(), SQLITE_STATIC);
3804
+
3805
+ int rv = 0;
3806
+ rc = sqlite3_step(stmt);
3807
+ if (rc != SQLITE_DONE) {
3808
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3809
+ rv = 1;
3810
+ }
3811
+ sqlite3_finalize(stmt);
3812
+ if (rv == 0 && sqlite3_changes(m_db) < 1) {
3813
+ return 1;
3814
+ }
3815
+ return rv;
3816
+ }
3817
+
3818
+ int BSXDB::saveOfferAccept(const OfferData &offer)
3819
+ {
3820
+ sqlite3_stmt *stmt;
3821
+
3822
+ const char *sql = "UPDATE offers SET chosen = 1, accepted_at = ?, day_key_count = ?,"
3823
+ " pubkey_a = ?, pubkey_b = ?, kbs_dleag = ?, kbv = ?, accept_utxos = ?,"
3824
+ " change_script = ?, coin_to_dest = ? WHERE offer_id = ?;";
3825
+
3826
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3827
+ if (rc != SQLITE_OK) {
3828
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3829
+ return 1;
3830
+ }
3831
+ auto bind_blob = [stmt](int col, const std::vector<unsigned char> &in) {
3832
+ sqlite3_bind_blob(stmt, col, in.data(), in.size(), SQLITE_STATIC);
3833
+ };
3834
+ sqlite3_bind_int64(stmt, 1, offer.m_accepted_at);
3835
+ sqlite3_bind_int(stmt, 2, offer.m_day_key_count);
3836
+ bind_blob(3, offer.m_pubkey_a);
3837
+ bind_blob(4, offer.m_pubkey_b);
3838
+ bind_blob(5, offer.m_kbs_dleag);
3839
+ bind_blob(6, offer.m_kbv);
3840
+ bind_blob(7, offer.m_accept_utxos);
3841
+ bind_blob(8, offer.m_change_script);
3842
+ bind_blob(9, offer.m_coin_to_dest);
3843
+ bind_blob(10, offer.m_offer_id);
3844
+
3845
+ int rv = 0;
3846
+ rc = sqlite3_step(stmt);
3847
+ if (rc != SQLITE_DONE) {
3848
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3849
+ rv = 1;
3850
+ }
3851
+ sqlite3_finalize(stmt);
3852
+ if (rv == 0 && sqlite3_changes(m_db) < 1) {
3853
+ return 1;
3854
+ }
3855
+ return rv;
3856
+ }
3857
+
3858
+ int BSXDB::setOfferRevoked(const std::vector<unsigned char> &offer_id)
3859
+ {
3860
+ sqlite3_stmt *stmt;
3861
+
3862
+ std::string sql = strprintf("UPDATE offers SET state = %d"
3863
+ " WHERE offer_id = ? AND chosen = 0 AND state = %d;",
3864
+ OfferStates::REVOKED, OfferStates::RECEIVED);
3865
+
3866
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
3867
+ if (rc != SQLITE_OK) {
3868
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3869
+ return 1;
3870
+ }
3871
+ sqlite3_bind_blob(stmt, 1, offer_id.data(), offer_id.size(), SQLITE_STATIC);
3872
+
3873
+ int rv = 0;
3874
+ rc = sqlite3_step(stmt);
3875
+ if (rc != SQLITE_DONE) {
3876
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3877
+ rv = 1;
3878
+ }
3879
+ sqlite3_finalize(stmt);
3880
+ if (rv == 0 && sqlite3_changes(m_db) < 1) {
3881
+ return ReturnTypes::DUPLICATE;
3882
+ }
3883
+ return rv;
3884
+ }
3885
+
3886
+ int BSXDB::setOfferRejected(const std::vector<unsigned char> &offer_id)
3887
+ {
3888
+ sqlite3_stmt *stmt;
3889
+
3890
+ std::string sql = strprintf("UPDATE offers SET state = %d, chosen = 0"
3891
+ " WHERE offer_id = ? AND chosen = 1;",
3892
+ OfferStates::REJECTED);
3893
+
3894
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
3895
+ if (rc != SQLITE_OK) {
3896
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3897
+ return 1;
3898
+ }
3899
+ sqlite3_bind_blob(stmt, 1, offer_id.data(), offer_id.size(), SQLITE_STATIC);
3900
+
3901
+ int rv = 0;
3902
+ rc = sqlite3_step(stmt);
3903
+ if (rc != SQLITE_DONE) {
3904
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3905
+ rv = 1;
3906
+ }
3907
+ sqlite3_finalize(stmt);
3908
+ if (rv == 0 && sqlite3_changes(m_db) < 1) {
3909
+ return 1;
3910
+ }
3911
+ return rv;
3912
+ }
3913
+
3914
+ int BSXDB::saveMainExtkey(const std::vector<unsigned char> &encrypted_extkey,
3915
+ const std::vector<unsigned char> &encrypted_mnemonic,
3916
+ const std::string &birthday)
3917
+ {
3918
+ sqlite3_stmt *stmt;
3919
+
3920
+ const char *sql = "INSERT INTO main_extkey (encrypted_extkey, encrypted_mnemonic, birthday)"
3921
+ " VALUES (?, ?, ?);";
3922
+
3923
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3924
+ if (rc != SQLITE_OK) {
3925
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3926
+ return 1;
3927
+ }
3928
+ sqlite3_bind_blob(stmt, 1, encrypted_extkey.data(), encrypted_extkey.size(), SQLITE_STATIC);
3929
+ if (encrypted_mnemonic.empty()) {
3930
+ sqlite3_bind_null(stmt, 2);
3931
+ } else {
3932
+ sqlite3_bind_blob(stmt, 2, encrypted_mnemonic.data(), encrypted_mnemonic.size(),
3933
+ SQLITE_STATIC);
3934
+ }
3935
+ if (birthday.empty()) {
3936
+ sqlite3_bind_null(stmt, 3);
3937
+ } else {
3938
+ sqlite3_bind_text(stmt, 3, birthday.c_str(), -1, SQLITE_STATIC);
3939
+ }
3940
+
3941
+ int rv = 0;
3942
+ rc = sqlite3_step(stmt);
3943
+ if (rc != SQLITE_DONE) {
3944
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3945
+ rv = 1;
3946
+ }
3947
+
3948
+ sqlite3_finalize(stmt);
3949
+ return rv;
3950
+ }
3951
+
3952
+ int BSXDB::updateMainExtkey(const std::vector<unsigned char> &encrypted_extkey,
3953
+ const std::vector<unsigned char> &encrypted_mnemonic)
3954
+ {
3955
+ sqlite3_stmt *stmt;
3956
+
3957
+ const char *sql = "UPDATE main_extkey SET encrypted_extkey = ?, encrypted_mnemonic = ?;";
3958
+
3959
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3960
+ if (rc != SQLITE_OK) {
3961
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3962
+ return 1;
3963
+ }
3964
+ sqlite3_bind_blob(stmt, 1, encrypted_extkey.data(), encrypted_extkey.size(), SQLITE_STATIC);
3965
+ if (encrypted_mnemonic.empty()) {
3966
+ sqlite3_bind_null(stmt, 2);
3967
+ } else {
3968
+ sqlite3_bind_blob(stmt, 2, encrypted_mnemonic.data(), encrypted_mnemonic.size(),
3969
+ SQLITE_STATIC);
3970
+ }
3971
+
3972
+ int rv = 0;
3973
+ rc = sqlite3_step(stmt);
3974
+ if (rc != SQLITE_DONE) {
3975
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
3976
+ rv = 1;
3977
+ } else if (sqlite3_changes(m_db) != 1) {
3978
+ LOGE("updateMainExtkey: no main extkey row.");
3979
+ rv = 1;
3980
+ }
3981
+
3982
+ sqlite3_finalize(stmt);
3983
+ return rv;
3984
+ }
3985
+
3986
+ int BSXDB::loadMainMnemonic(std::vector<unsigned char> &encrypted_mnemonic, std::string &birthday)
3987
+ {
3988
+ sqlite3_stmt *stmt;
3989
+
3990
+ const char *sql = "SELECT encrypted_mnemonic, birthday FROM main_extkey";
3991
+
3992
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
3993
+ if (rc != SQLITE_OK) {
3994
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
3995
+ return 1;
3996
+ }
3997
+
3998
+ int rv = 1;
3999
+ rc = sqlite3_step(stmt);
4000
+ if (rc == SQLITE_ROW) {
4001
+ const void *data = sqlite3_column_blob(stmt, 0);
4002
+ const size_t data_size = sqlite3_column_bytes(stmt, 0);
4003
+ encrypted_mnemonic.assign((const unsigned char *)data,
4004
+ (const unsigned char *)data + (data ? data_size : 0));
4005
+ const unsigned char *text = sqlite3_column_text(stmt, 1);
4006
+ birthday = text ? (const char *)text : "";
4007
+ rv = encrypted_mnemonic.empty() ? 2 : 0;
4008
+ } else if (rc == SQLITE_ERROR) {
4009
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
4010
+ } else if (rc == SQLITE_DONE) {
4011
+ rv = 2;
4012
+ }
4013
+ sqlite3_finalize(stmt);
4014
+ return rv;
4015
+ }
4016
+
4017
+ int BSXDB::loadMainExtkey(std::vector<unsigned char> &encrypted_extkey)
4018
+ {
4019
+ sqlite3_stmt *stmt;
4020
+
4021
+ const char *sql = "SELECT encrypted_extkey FROM main_extkey";
4022
+
4023
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
4024
+ if (rc != SQLITE_OK) {
4025
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4026
+ return 1;
4027
+ }
4028
+
4029
+ int rv = 1;
4030
+ rc = sqlite3_step(stmt);
4031
+ if (rc == SQLITE_ROW) {
4032
+ const void *data = sqlite3_column_blob(stmt, 0);
4033
+ size_t data_size = sqlite3_column_bytes(stmt, 0);
4034
+ encrypted_extkey.resize(data_size);
4035
+ if (data_size) {
4036
+ memcpy(encrypted_extkey.data(), data, data_size);
4037
+ }
4038
+ rv = 0;
4039
+ } else if (rc == SQLITE_ERROR) {
4040
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
4041
+ } else if (rc == SQLITE_DONE) {
4042
+ rv = 2;
4043
+ }
4044
+ sqlite3_finalize(stmt);
4045
+ return rv;
4046
+ }
4047
+
4048
+ int BSXDB::saveSwapKey(const std::vector<unsigned char> &key,
4049
+ const std::vector<unsigned char> &swap_id, int key_type)
4050
+ {
4051
+ sqlite3_stmt *stmt;
4052
+
4053
+ const char *sql = "INSERT INTO swapkeys (swap_id, key_type, key)"
4054
+ " VALUES (@swap_id, @key_type, @key);";
4055
+
4056
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
4057
+ if (rc != SQLITE_OK) {
4058
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4059
+ return 1;
4060
+ }
4061
+ if (0 != bindDataVector(stmt, "@swap_id", swap_id) ||
4062
+ 0 != bindInt(stmt, "@key_type", key_type) || 0 != bindDataVector(stmt, "@key", key)) {
4063
+ LOGE("Failed to bind parameters.");
4064
+ sqlite3_finalize(stmt);
4065
+ return 1;
4066
+ }
4067
+
4068
+ int rv = 0;
4069
+ rc = sqlite3_step(stmt);
4070
+ if (rc != SQLITE_DONE) {
4071
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
4072
+ rv = 1;
4073
+ }
4074
+
4075
+ sqlite3_finalize(stmt);
4076
+ return rv;
4077
+ }
4078
+
4079
+ int BSXDB::loadSwapKey(std::vector<unsigned char> &key, const std::vector<unsigned char> &swap_id,
4080
+ int key_type)
4081
+ {
4082
+ sqlite3_stmt *stmt;
4083
+
4084
+ const char *sql = "SELECT key FROM swapkeys"
4085
+ " WHERE swap_id = ? AND key_type = ?";
4086
+
4087
+ int rc = sqlite3_prepare_v2(m_db, sql, -1, &stmt, 0);
4088
+ if (rc != SQLITE_OK) {
4089
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4090
+ return 1;
4091
+ }
4092
+ sqlite3_bind_blob(stmt, 1, swap_id.data(), swap_id.size(), SQLITE_STATIC);
4093
+ sqlite3_bind_int(stmt, 2, key_type);
4094
+
4095
+ int rv = 1;
4096
+ rc = sqlite3_step(stmt);
4097
+ if (rc == SQLITE_ROW) {
4098
+ const void *key_data = sqlite3_column_blob(stmt, 0);
4099
+ size_t key_size = sqlite3_column_bytes(stmt, 0);
4100
+ key.resize(key_size);
4101
+ memcpy(key.data(), key_data, key_size);
4102
+ rv = 0;
4103
+ } else if (rc == SQLITE_ERROR) {
4104
+ LOGE("Error from select: %s", sqlite3_errmsg(m_db));
4105
+ } else if (rc == SQLITE_DONE) {
4106
+ LOGW("No row found.");
4107
+ rv = 2;
4108
+ }
4109
+ sqlite3_finalize(stmt);
4110
+ return rv;
4111
+ }
4112
+
4113
+ int BSXDB::saveAction(int64_t &rowid, const Action &action)
4114
+ {
4115
+ sqlite3_stmt *stmt;
4116
+
4117
+ const char *sql_select = "SELECT action_id FROM actions WHERE active_ind = 1"
4118
+ " AND linked_type = @linked_type AND linked_id = @linked_id"
4119
+ " AND action_type = @action_type LIMIT 1;";
4120
+ int rc_select = sqlite3_prepare_v2(m_db, sql_select, -1, &stmt, 0);
4121
+ if (rc_select != SQLITE_OK) {
4122
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4123
+ return 1;
4124
+ }
4125
+ if (0 != bindInt(stmt, "@linked_type", action.m_linked_type) ||
4126
+ 0 != bindDataVector(stmt, "@linked_id", action.m_linked_id) ||
4127
+ 0 != bindInt(stmt, "@action_type", action.m_action_type)) {
4128
+ LOGE("Failed to bind parameters.");
4129
+ sqlite3_finalize(stmt);
4130
+ return 1;
4131
+ }
4132
+ if (sqlite3_step(stmt) == SQLITE_ROW) {
4133
+ rowid = sqlite3_column_int64(stmt, 0);
4134
+ sqlite3_finalize(stmt);
4135
+ LOGD("Action already queued.");
4136
+ return 0;
4137
+ }
4138
+ sqlite3_finalize(stmt);
4139
+
4140
+ const char *sql_insert = "INSERT INTO actions (active_ind, created_at, trigger_at, num_tries, "
4141
+ "linked_type, linked_id, "
4142
+ "action_type, action_data)"
4143
+ " VALUES (1, @created_at, @trigger_at, @num_tries, @linked_type, "
4144
+ "@linked_id, @action_type, @action_data);";
4145
+ int rc = sqlite3_prepare_v2(m_db, sql_insert, -1, &stmt, 0);
4146
+ if (rc != SQLITE_OK) {
4147
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4148
+ return 1;
4149
+ }
4150
+
4151
+ if (0 != bindInt64(stmt, "@created_at", action.m_created_at) ||
4152
+ 0 != bindInt64(stmt, "@trigger_at", action.m_trigger_at) ||
4153
+ 0 != bindInt(stmt, "@num_tries", action.m_num_tries) ||
4154
+ 0 != bindInt(stmt, "@linked_type", action.m_linked_type) ||
4155
+ 0 != bindDataVector(stmt, "@linked_id", action.m_linked_id) ||
4156
+ 0 != bindInt(stmt, "@action_type", action.m_action_type) ||
4157
+ 0 != bindDataVector(stmt, "@action_data", action.m_action_data)) {
4158
+ LOGE("Failed to bind parameters.");
4159
+ sqlite3_finalize(stmt);
4160
+ return 1;
4161
+ }
4162
+
4163
+ int rv = 0;
4164
+ rc = sqlite3_step(stmt);
4165
+ if (rc != SQLITE_DONE) {
4166
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
4167
+ rv = 1;
4168
+ }
4169
+
4170
+ sqlite3_finalize(stmt);
4171
+ return rv;
4172
+ }
4173
+
4174
+ int BSXDB::getActions(std::vector<Action> &actions, const std::vector<FilterData> &filters)
4175
+ {
4176
+ sqlite3_stmt *stmt;
4177
+
4178
+ std::string retrieve_columns, filter_columns;
4179
+ const db::Table *actions_schema = db::get_table_schema("actions");
4180
+ if (!actions_schema) {
4181
+ LOGE("Failed to get table definition: %s", "actions");
4182
+ return 2;
4183
+ }
4184
+ for (const auto &col : actions_schema->m_columns) {
4185
+ retrieve_columns += std::string(retrieve_columns.size() ? "," : "") + col.m_name;
4186
+ }
4187
+
4188
+ if (0 != expandFilterColumns(filter_columns, filters)) {
4189
+ LOGE("Failed to expand filter columns");
4190
+ return 1;
4191
+ }
4192
+ std::string sql = strprintf("SELECT %s FROM actions"
4193
+ " WHERE 1 = 1 %s",
4194
+ retrieve_columns, filter_columns);
4195
+
4196
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
4197
+ if (rc != SQLITE_OK) {
4198
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4199
+ return 1;
4200
+ }
4201
+ if (0 != bindFilterData(stmt, filters)) {
4202
+ LOGE("Failed to bind filter data");
4203
+ sqlite3_finalize(stmt);
4204
+ return 1;
4205
+ }
4206
+
4207
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
4208
+ Action action;
4209
+ setInt64(stmt, "action_id", action.m_action_id);
4210
+ setInt64(stmt, "created_at", action.m_created_at);
4211
+ setInt64(stmt, "trigger_at", action.m_trigger_at);
4212
+ setInt(stmt, "num_tries", action.m_num_tries);
4213
+ setInt(stmt, "linked_type", action.m_linked_type);
4214
+ setDataVector(stmt, "linked_id", action.m_linked_id);
4215
+ setInt(stmt, "action_type", action.m_action_type);
4216
+ setDataVector(stmt, "action_data", action.m_action_data);
4217
+ actions.push_back(action);
4218
+ }
4219
+ if (rc != SQLITE_DONE) {
4220
+ LOGE("Selecting from \"%s\": %s", "actions", sqlite3_errmsg(m_db));
4221
+ }
4222
+
4223
+ sqlite3_finalize(stmt);
4224
+ return rc == SQLITE_DONE ? 0 : 1;
4225
+ }
4226
+
4227
+ int BSXDB::removeRows(const char *table_name, const std::vector<FilterData> &filters)
4228
+ {
4229
+ sqlite3_stmt *stmt;
4230
+ std::string filter_columns;
4231
+ if (0 != expandFilterColumns(filter_columns, filters)) {
4232
+ LOGE("Failed to expand filter columns");
4233
+ return 1;
4234
+ }
4235
+ std::string sql = strprintf("DELETE FROM %s"
4236
+ " WHERE 1 = 1 %s",
4237
+ table_name, filter_columns);
4238
+
4239
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
4240
+ if (rc != SQLITE_OK) {
4241
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4242
+ return 1;
4243
+ }
4244
+
4245
+ if (0 != bindFilterData(stmt, filters)) {
4246
+ LOGE("Failed to bind filter data");
4247
+ sqlite3_finalize(stmt);
4248
+ return 1;
4249
+ }
4250
+
4251
+ int rv = 0;
4252
+ rc = sqlite3_step(stmt);
4253
+ if (rc != SQLITE_DONE) {
4254
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
4255
+ rv = 1;
4256
+ }
4257
+
4258
+ sqlite3_finalize(stmt);
4259
+ return rv;
4260
+ }
4261
+
4262
+ int BSXDB::removeActions(const std::vector<FilterData> &filters)
4263
+ {
4264
+ return removeRows("actions", filters);
4265
+ }
4266
+
4267
+ int BSXDB::updateAction(const Action &action, const std::vector<std::string> *fields)
4268
+ {
4269
+
4270
+ sqlite3_stmt *stmt;
4271
+
4272
+ std::string sql_query = "UPDATE actions SET ";
4273
+ size_t updated_columns{0};
4274
+ if (fields) {
4275
+ for (const auto &col_name : *fields) {
4276
+ if (updated_columns) {
4277
+ sql_query += ",";
4278
+ }
4279
+ sql_query += col_name + "=@" + col_name;
4280
+ updated_columns++;
4281
+ }
4282
+ } else {
4283
+ const db::Table *actions_schema = db::get_table_schema("actions");
4284
+ if (!actions_schema) {
4285
+ LOGE("Failed to get table definition: %s", "actions");
4286
+ return 2;
4287
+ }
4288
+
4289
+ for (const auto &col : actions_schema->m_columns) {
4290
+ if (col.m_name == "id") {
4291
+ continue;
4292
+ }
4293
+ if (updated_columns) {
4294
+ sql_query += ",";
4295
+ }
4296
+ sql_query += col.m_name + "=@" + col.m_name;
4297
+ updated_columns++;
4298
+ }
4299
+ }
4300
+
4301
+ sql_query += " WHERE action_id = @action_id";
4302
+
4303
+ int rc = sqlite3_prepare_v2(m_db, sql_query.c_str(), -1, &stmt, 0);
4304
+ if (rc != SQLITE_OK) {
4305
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4306
+ return 1;
4307
+ }
4308
+ if (0 != bindInt(stmt, "@action_id", action.m_action_id)) {
4309
+ LOGE("Failed to bind parameters.");
4310
+ sqlite3_finalize(stmt);
4311
+ return 1;
4312
+ }
4313
+
4314
+ bindInt(stmt, "@active_ind", action.m_active_ind);
4315
+ bindInt64(stmt, "@created_at", action.m_created_at);
4316
+ bindInt64(stmt, "@trigger_at", action.m_trigger_at);
4317
+ bindInt(stmt, "@num_tries", action.m_num_tries);
4318
+ bindInt(stmt, "@linked_type", action.m_linked_type);
4319
+ bindDataVector(stmt, "@linked_id", action.m_linked_id);
4320
+ bindInt(stmt, "@action_type", action.m_action_type);
4321
+ bindDataVector(stmt, "@action_data", action.m_action_data);
4322
+
4323
+ int rv = 0;
4324
+ rc = sqlite3_step(stmt);
4325
+ if (rc != SQLITE_DONE) {
4326
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
4327
+ rv = 1;
4328
+ }
4329
+
4330
+ sqlite3_finalize(stmt);
4331
+ return rv;
4332
+ }
4333
+
4334
+ int BSXDB::logEvent(const std::vector<unsigned char> &swap_id, Event &new_event)
4335
+ {
4336
+ sqlite3_stmt *stmt{nullptr};
4337
+
4338
+ const char *sql_insert = "INSERT INTO eventlog (active_ind, created_at, swap_id, linked_type, "
4339
+ "linked_id, event_type, event_sub_type, "
4340
+ "event_msg, event_data, visible)"
4341
+ " VALUES (1, @created_at, @swap_id, @linked_type, @linked_id, "
4342
+ "@event_type, @event_sub_type, @event_msg, @event_data, @visible);";
4343
+ int rc = sqlite3_prepare_v2(m_db, sql_insert, -1, &stmt, 0);
4344
+ if (rc != SQLITE_OK) {
4345
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4346
+ return 1;
4347
+ }
4348
+ new_event.m_created_at = getTime();
4349
+ new_event.m_visible = new_event.m_visible && EventLogTypes::visible(new_event.m_event_type);
4350
+
4351
+ if (0 != bindInt64(stmt, "@created_at", new_event.m_created_at) ||
4352
+ 0 != bindDataVector(stmt, "@swap_id", swap_id) ||
4353
+ 0 != bindInt(stmt, "@linked_type", new_event.m_linked_type) ||
4354
+ 0 != bindDataVector(stmt, "@linked_id", new_event.m_linked_id) ||
4355
+ 0 != bindInt(stmt, "@event_type", new_event.m_event_type) ||
4356
+ 0 != bindInt(stmt, "@event_sub_type", new_event.m_event_sub_type) ||
4357
+ 0 != bindText(stmt, "@event_msg", new_event.m_event_msg) ||
4358
+ 0 != bindInt(stmt, "@event_data", new_event.m_event_data_int) ||
4359
+ 0 != bindInt(stmt, "@visible", new_event.m_visible ? 1 : 0)) {
4360
+ LOGE("Failed to bind parameters.");
4361
+ sqlite3_finalize(stmt);
4362
+ return 1;
4363
+ }
4364
+
4365
+ int rv = 0;
4366
+ rc = sqlite3_step(stmt);
4367
+ if (rc != SQLITE_DONE) {
4368
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
4369
+ rv = 1;
4370
+ }
4371
+
4372
+ new_event.m_event_id = sqlite3_last_insert_rowid(m_db);
4373
+ notifyEvent(new_event);
4374
+
4375
+ sqlite3_finalize(stmt);
4376
+ return rv;
4377
+ }
4378
+
4379
+ int BSXDB::logEvent(const std::vector<unsigned char> &swap_id, int concept_type,
4380
+ const std::vector<unsigned char> &linked_id, int event_type, int event_sub_type,
4381
+ int event_data)
4382
+ {
4383
+ Event new_event = {.m_linked_type = concept_type,
4384
+ .m_linked_id = linked_id,
4385
+ .m_event_type = event_type,
4386
+ .m_event_sub_type = event_sub_type,
4387
+ .m_event_data_int = event_data};
4388
+ return logEvent(swap_id, new_event);
4389
+ }
4390
+
4391
+ int BSXDB::logEvent(const std::vector<unsigned char> &swap_id, int event_type,
4392
+ std::string &event_description, int event_sub_type)
4393
+ {
4394
+ Event new_event = {.m_event_type = event_type,
4395
+ .m_event_sub_type = event_sub_type,
4396
+ .m_event_msg = event_description};
4397
+ return logEvent(swap_id, new_event);
4398
+ }
4399
+
4400
+ int BSXDB::logEvent(const std::vector<unsigned char> &swap_id, int event_type, int event_sub_type)
4401
+ {
4402
+ Event new_event = {.m_event_type = event_type, .m_event_sub_type = event_sub_type};
4403
+ return logEvent(swap_id, new_event);
4404
+ }
4405
+
4406
+ int BSXDB::getEvents(std::vector<Event> &events, const std::vector<FilterData> &filters)
4407
+ {
4408
+ sqlite3_stmt *stmt;
4409
+
4410
+ std::string filter_columns;
4411
+ if (0 != expandFilterColumns(filter_columns, filters)) {
4412
+ LOGE("Failed to expand filter columns");
4413
+ return 1;
4414
+ }
4415
+
4416
+ std::string sql = strprintf("SELECT event_id, linked_type, linked_id, created_at, event_type, "
4417
+ "event_sub_type, event_msg, event_data, visible FROM eventlog"
4418
+ " WHERE 1 = 1 %s ORDER BY event_id ASC",
4419
+ filter_columns);
4420
+
4421
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
4422
+ if (rc != SQLITE_OK) {
4423
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4424
+ return 1;
4425
+ }
4426
+ if (0 != bindFilterData(stmt, filters)) {
4427
+ LOGE("Failed to bind filter data");
4428
+ sqlite3_finalize(stmt);
4429
+ return 1;
4430
+ }
4431
+
4432
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
4433
+ Event event;
4434
+ event.m_event_id = sqlite3_column_int(stmt, 0);
4435
+ event.m_linked_type = sqlite3_column_int(stmt, 1);
4436
+ setDataVector(stmt, 2, event.m_linked_id);
4437
+ event.m_created_at = sqlite3_column_int64(stmt, 3);
4438
+ event.m_event_type = sqlite3_column_int(stmt, 4);
4439
+ event.m_event_sub_type = sqlite3_column_int(stmt, 5);
4440
+ event.m_event_msg = (const char *)sqlite3_column_text(stmt, 6);
4441
+ event.m_event_data_int = sqlite3_column_int(stmt, 7);
4442
+ event.m_visible = sqlite3_column_int(stmt, 8) != 0;
4443
+ events.push_back(std::move(event));
4444
+ }
4445
+ if (rc != SQLITE_DONE) {
4446
+ LOGE("Selecting from \"%s\": %s", "eventlog", sqlite3_errmsg(m_db));
4447
+ }
4448
+
4449
+ sqlite3_finalize(stmt);
4450
+ return rc == SQLITE_DONE ? 0 : 1;
4451
+ }
4452
+
4453
+ int BSXDB::countEvents(int64_t &num_events, const std::vector<FilterData> &filters)
4454
+ {
4455
+ sqlite3_stmt *stmt;
4456
+ num_events = 0;
4457
+
4458
+ std::string filter_columns;
4459
+ if (0 != expandFilterColumns(filter_columns, filters)) {
4460
+ LOGE("Failed to expand filter columns");
4461
+ return 1;
4462
+ }
4463
+
4464
+ std::string sql = strprintf("SELECT COUNT(*) FROM eventlog"
4465
+ " WHERE 1 = 1 %s",
4466
+ filter_columns);
4467
+
4468
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, 0);
4469
+ if (rc != SQLITE_OK) {
4470
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4471
+ return 1;
4472
+ }
4473
+ if (0 != bindFilterData(stmt, filters)) {
4474
+ LOGE("Failed to bind filter data");
4475
+ sqlite3_finalize(stmt);
4476
+ return 1;
4477
+ }
4478
+
4479
+ if ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
4480
+ num_events = sqlite3_column_int64(stmt, 0);
4481
+ } else {
4482
+ LOGE("Selecting from \"%s\": %s", "eventlog", sqlite3_errmsg(m_db));
4483
+ sqlite3_finalize(stmt);
4484
+ return 1;
4485
+ }
4486
+
4487
+ sqlite3_finalize(stmt);
4488
+ return 0;
4489
+ }
4490
+
4491
+ int BSXDB::insertBlockIndex(int coin_type, const BlockIndexData &bi_data)
4492
+ {
4493
+ sqlite3_stmt *stmt{nullptr};
4494
+
4495
+ std::string insert_columns, insert_values;
4496
+ const db::Table *table_schema = db::get_table_schema("blockindex");
4497
+ if (!table_schema) {
4498
+ LOGE("Failed to get table definition: %s", "blockindex");
4499
+ return 2;
4500
+ }
4501
+ for (const auto &col : table_schema->m_columns) {
4502
+ if (col.m_name == "id") {
4503
+ continue;
4504
+ }
4505
+ insert_columns += std::string(insert_columns.size() ? "," : "") + col.m_name;
4506
+ insert_values += std::string(insert_values.size() ? "," : "") + "@" + col.m_name;
4507
+ }
4508
+
4509
+ std::string sql_insert = strprintf("INSERT INTO blockindex (%s)"
4510
+ " VALUES (%s) ",
4511
+ insert_columns, insert_values);
4512
+ sql_insert += " ON CONFLICT(coin_type, block_height) DO UPDATE SET ";
4513
+ size_t upserted_columns{0};
4514
+ for (const auto &col : table_schema->m_columns) {
4515
+ if (col.m_name == "id") {
4516
+ continue;
4517
+ }
4518
+ if (upserted_columns) {
4519
+ sql_insert += ",";
4520
+ }
4521
+ sql_insert += col.m_name + "=excluded." + col.m_name;
4522
+ upserted_columns++;
4523
+ }
4524
+ int rc = sqlite3_prepare_v2(m_db, sql_insert.c_str(), -1, &stmt, 0);
4525
+ if (rc != SQLITE_OK) {
4526
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4527
+ return 1;
4528
+ }
4529
+
4530
+ bindInt(stmt, "@coin_type", coin_type);
4531
+ bindInt(stmt, "@block_height", bi_data.m_block_height);
4532
+ bindDataVector(stmt, "@header_data", bi_data.m_header_data);
4533
+ bindDataVector(stmt, "@chain_work", bi_data.m_chain_work);
4534
+ bindInt64(stmt, "@block_time", bi_data.m_block_time);
4535
+
4536
+ int rv = 0;
4537
+ rc = sqlite3_step(stmt);
4538
+ if (rc != SQLITE_DONE) {
4539
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
4540
+ rv = 1;
4541
+ }
4542
+
4543
+ sqlite3_finalize(stmt);
4544
+ return rv;
4545
+ }
4546
+
4547
+ int BSXDB::expireBlockIndices(int &num_removed, int coin_type, int below_height, int64_t expired_at)
4548
+ {
4549
+ sqlite3_stmt *stmt;
4550
+ // A NULL block_time never matches, so rows of unknown age are kept.
4551
+ const char *sql_delete = "DELETE FROM blockindex WHERE coin_type = @coin_type"
4552
+ " AND block_height < @below_height AND block_time <= @expired_at;";
4553
+ int rc = sqlite3_prepare_v2(m_db, sql_delete, -1, &stmt, 0);
4554
+ if (rc != SQLITE_OK) {
4555
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4556
+ return 1;
4557
+ }
4558
+ if (0 != bindInt(stmt, "@coin_type", coin_type) ||
4559
+ 0 != bindInt(stmt, "@below_height", below_height) ||
4560
+ 0 != bindInt64(stmt, "@expired_at", expired_at)) {
4561
+ LOGE("Failed to bind parameters.");
4562
+ sqlite3_finalize(stmt);
4563
+ return 1;
4564
+ }
4565
+ int rv = 0;
4566
+ rc = sqlite3_step(stmt);
4567
+ if (rc != SQLITE_DONE) {
4568
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
4569
+ rv = 1;
4570
+ } else {
4571
+ num_removed = sqlite3_changes(m_db);
4572
+ }
4573
+ sqlite3_finalize(stmt);
4574
+ return rv;
4575
+ }
4576
+
4577
+ int BSXDB::deleteBlockIndicesAbove(int coin_type, int above_height)
4578
+ {
4579
+ sqlite3_stmt *stmt;
4580
+ const char *sql_delete = "DELETE FROM blockindex WHERE coin_type = @coin_type"
4581
+ " AND block_height > @above_height;";
4582
+ int rc = sqlite3_prepare_v2(m_db, sql_delete, -1, &stmt, 0);
4583
+ if (rc != SQLITE_OK) {
4584
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4585
+ return 1;
4586
+ }
4587
+ if (0 != bindInt(stmt, "@coin_type", coin_type) ||
4588
+ 0 != bindInt(stmt, "@above_height", above_height)) {
4589
+ LOGE("Failed to bind parameters.");
4590
+ sqlite3_finalize(stmt);
4591
+ return 1;
4592
+ }
4593
+ int rv = 0;
4594
+ rc = sqlite3_step(stmt);
4595
+ if (rc != SQLITE_DONE) {
4596
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
4597
+ rv = 1;
4598
+ }
4599
+ sqlite3_finalize(stmt);
4600
+ return rv;
4601
+ }
4602
+
4603
+ int BSXDB::getAnchorHeader(BlockIndexData &bi_data, int coin_type, int max_height)
4604
+ {
4605
+ DBCursor cursor;
4606
+ QueryOptions query_opts;
4607
+ // Only an anchor carries chain_work, and only an anchor can start a chain,
4608
+ // so the highest row below max_height is the wrong one unless it is one.
4609
+ query_opts.m_filters = {FilterData("block_height", ComparisonTypes::LTE, max_height),
4610
+ FilterData("chain_work", ComparisonTypes::LENGTH_EQUAL, (int64_t)32)};
4611
+ query_opts.m_sorts = {SortData("block_height", SortTypes::DESC)};
4612
+ query_opts.m_limit = 1;
4613
+ int rv = prepareBlockIndexQuery(cursor, coin_type, query_opts);
4614
+ if (rv != 0) {
4615
+ closeCursor(cursor);
4616
+ return rv;
4617
+ }
4618
+ rv = stepBlockIndexQuery(cursor, bi_data);
4619
+ closeCursor(cursor);
4620
+ return rv;
4621
+ }
4622
+
4623
+ int BSXDB::prepareBlockIndexQuery(DBCursor &cursor, int coin_type, const QueryOptions &query_opts)
4624
+ {
4625
+ std::string filter_columns, sort_columns, offset_str, limit_str;
4626
+ if (0 != expandFilterColumns(filter_columns, query_opts.m_filters)) {
4627
+ LOGE("Failed to expand filter columns");
4628
+ return 1;
4629
+ }
4630
+ if (0 != expandSortColumns(sort_columns, query_opts.m_sorts)) {
4631
+ LOGE("Failed to expand sort columns");
4632
+ return 1;
4633
+ }
4634
+
4635
+ if (query_opts.m_offset) {
4636
+ offset_str = " OFFSET :query_offset_val";
4637
+ }
4638
+ if (query_opts.m_limit) {
4639
+ limit_str = " LIMIT :query_limit_val";
4640
+ }
4641
+
4642
+ std::string sql = strprintf("SELECT block_height, header_data, chain_work, block_time"
4643
+ " FROM blockindex"
4644
+ " WHERE coin_type = :coin_type %s %s %s %s",
4645
+ filter_columns, sort_columns, offset_str, limit_str);
4646
+
4647
+ int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &cursor.m_stmt, 0);
4648
+ if (rc != SQLITE_OK) {
4649
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4650
+ return 1;
4651
+ }
4652
+ if (0 != bindInt(cursor.m_stmt, ":coin_type", coin_type) ||
4653
+ 0 != bindFilterData(cursor.m_stmt, query_opts.m_filters)) {
4654
+ LOGE("Failed to bind filter data");
4655
+ sqlite3_finalize(cursor.m_stmt);
4656
+ return 1;
4657
+ }
4658
+ if (query_opts.m_offset) {
4659
+ bindInt64(cursor.m_stmt, ":query_offset_val", query_opts.m_offset);
4660
+ }
4661
+ if (query_opts.m_limit) {
4662
+ bindInt64(cursor.m_stmt, ":query_limit_val", query_opts.m_limit);
4663
+ }
4664
+
4665
+ return 0;
4666
+ }
4667
+
4668
+ int BSXDB::stepBlockIndexQuery(DBCursor &cursor, BlockIndexData &bi_data)
4669
+ {
4670
+ int rc = sqlite3_step(cursor.m_stmt);
4671
+ if (rc == SQLITE_ROW) {
4672
+ bi_data.m_block_height = sqlite3_column_int(cursor.m_stmt, 0);
4673
+ setDataVector(cursor.m_stmt, 1, bi_data.m_header_data);
4674
+ setDataVector(cursor.m_stmt, 2, bi_data.m_chain_work);
4675
+ bi_data.m_block_time = sqlite3_column_int64(cursor.m_stmt, 3);
4676
+ } else if (rc == SQLITE_DONE) {
4677
+ return 2;
4678
+ } else {
4679
+ LOGE("Selecting from \"%s\": %s", "blockindex", sqlite3_errmsg(m_db));
4680
+ return 1;
4681
+ }
4682
+ return 0;
4683
+ }
4684
+
4685
+ int BSXDB::closeCursor(DBCursor &cursor)
4686
+ {
4687
+ if (!cursor.m_stmt) {
4688
+ LOGE("Cursor not open");
4689
+ return 1;
4690
+ }
4691
+ sqlite3_finalize(cursor.m_stmt);
4692
+ cursor.m_stmt = nullptr;
4693
+ return 0;
4694
+ }
4695
+
4696
+ int BSXDB::setDebugFlag(int linked_ind, const std::vector<unsigned char> &linked_id, int flag)
4697
+ {
4698
+ sqlite3_stmt *stmt;
4699
+ const char *sql_insert =
4700
+ "INSERT INTO links (active_ind, created_at, link_type, from_type, from_id, "
4701
+ "to_type)"
4702
+ " VALUES (1, @created_at, @link_type, @from_type, @from_id, "
4703
+ "@to_type);";
4704
+ int rc = sqlite3_prepare_v2(m_db, sql_insert, -1, &stmt, 0);
4705
+ if (rc != SQLITE_OK) {
4706
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4707
+ return 1;
4708
+ }
4709
+ time_t now = getTime();
4710
+ sqlite3_bind_int64(stmt, 1, now);
4711
+ sqlite3_bind_int(stmt, 2, LinkTypes::DEBUG_FLAG);
4712
+ sqlite3_bind_int(stmt, 3, linked_ind);
4713
+ sqlite3_bind_blob(stmt, 4, linked_id.data(), linked_id.size(), SQLITE_STATIC);
4714
+ sqlite3_bind_int(stmt, 5, flag);
4715
+
4716
+ int rv = 0;
4717
+ rc = sqlite3_step(stmt);
4718
+ if (rc != SQLITE_DONE) {
4719
+ LOGE("Execution failed: %s", sqlite3_errmsg(m_db));
4720
+ rv = 1;
4721
+ }
4722
+
4723
+ sqlite3_finalize(stmt);
4724
+ return rv;
4725
+ }
4726
+
4727
+ bool BSXDB::haveDebugFlag(const std::vector<unsigned char> &swap_id, int flag)
4728
+ {
4729
+
4730
+ sqlite3_stmt *stmt;
4731
+ const char *sql_str =
4732
+ "SELECT EXISTS(SELECT 1 FROM swapinfo AS s WHERE s.swap_id = @swap_id AND EXISTS (SELECT 1 FROM links AS l WHERE l.active_ind = 1 "
4733
+ " AND l.link_type = @link_type AND l.from_type = @from_type AND l.from_id = s.request_id AND l.to_type = @to_type))";
4734
+ int rc = sqlite3_prepare_v2(m_db, sql_str, -1, &stmt, 0);
4735
+ if (rc != SQLITE_OK) {
4736
+ LOGE("Failed to prepare statement: %s", sqlite3_errmsg(m_db));
4737
+ return true; // Fail enabling debug
4738
+ }
4739
+
4740
+ sqlite3_bind_blob(stmt, 1, swap_id.data(), swap_id.size(), SQLITE_STATIC);
4741
+ sqlite3_bind_int(stmt, 2, LinkTypes::DEBUG_FLAG);
4742
+ sqlite3_bind_int(stmt, 3, Concepts::SWAP_REQUEST);
4743
+ sqlite3_bind_int(stmt, 4, flag);
4744
+
4745
+ bool exists = true; // Fail enabling debug
4746
+ rc = sqlite3_step(stmt);
4747
+ if (rc != SQLITE_ROW) {
4748
+ LOGE("Query failed: %s", sqlite3_errmsg(m_db));
4749
+ } else {
4750
+ exists = sqlite3_column_int(stmt, 0);
4751
+ }
4752
+
4753
+ sqlite3_finalize(stmt);
4754
+ return exists;
4755
+ }
4756
+
4757
+ static std::string filter_param(size_t n) { return ":filter_" + std::to_string(n); }
4758
+
4759
+ int BSXDB::expandFilterColumns(std::string &filter_columns, const std::vector<FilterData> &filters)
4760
+ {
4761
+ for (size_t n = 0; n < filters.size(); ++n) {
4762
+ const auto &filter = filters[n];
4763
+ const std::string param = filter_param(n);
4764
+ std::string comparison_type;
4765
+ std::string compare_with = filter.m_column_name;
4766
+ switch (filter.m_compare_type) {
4767
+ case ComparisonTypes::EQUAL:
4768
+ comparison_type = "=";
4769
+ break;
4770
+ case ComparisonTypes::GT:
4771
+ comparison_type = ">";
4772
+ break;
4773
+ case ComparisonTypes::LT:
4774
+ comparison_type = "<";
4775
+ break;
4776
+ case ComparisonTypes::GTE:
4777
+ comparison_type = ">=";
4778
+ break;
4779
+ case ComparisonTypes::LTE:
4780
+ comparison_type = "<=";
4781
+ break;
4782
+ case ComparisonTypes::LENGTH_EQUAL:
4783
+ comparison_type = "=";
4784
+ compare_with = "length(" + filter.m_column_name + ")";
4785
+ break;
4786
+ case ComparisonTypes::NOT_EQUAL:
4787
+ comparison_type = "!=";
4788
+ break;
4789
+ case ComparisonTypes::IN:
4790
+ case ComparisonTypes::NOT_IN: {
4791
+ const bool is_in = filter.m_compare_type == ComparisonTypes::IN;
4792
+ if (filter.m_int_list.empty()) {
4793
+ // Nothing is in an empty set, and nothing is excluded by one.
4794
+ if (is_in) {
4795
+ filter_columns += " AND 0";
4796
+ }
4797
+ continue;
4798
+ }
4799
+ std::string placeholders;
4800
+ for (size_t i = 0; i < filter.m_int_list.size(); ++i) {
4801
+ if (i > 0) {
4802
+ placeholders += ", ";
4803
+ }
4804
+ placeholders += param + "_" + std::to_string(i);
4805
+ }
4806
+ filter_columns += " AND " + filter.m_column_name + (is_in ? " IN (" : " NOT IN (") +
4807
+ placeholders + ")";
4808
+ continue;
4809
+ }
4810
+ default:
4811
+ LOGE("Failed to convert comparison type %d", (int)filter.m_compare_type);
4812
+ return 1;
4813
+ }
4814
+ filter_columns += " AND " + compare_with + " " + comparison_type + " " + param;
4815
+ }
4816
+
4817
+ return 0;
4818
+ }
4819
+
4820
+ int BSXDB::expandSortColumns(std::string &sort_columns, const std::vector<SortData> &sorts)
4821
+ {
4822
+ if (sorts.size() < 1) {
4823
+ return 0;
4824
+ }
4825
+ sort_columns = " ORDER BY ";
4826
+ int sort_count{0};
4827
+ for (const auto &sort : sorts) {
4828
+ sort_columns += (sort_count ? " ," : "") + sort.m_column_name +
4829
+ (sort.m_sort_dir == SortTypes::ASC ? " ASC" : " DESC");
4830
+ sort_count++;
4831
+ }
4832
+
4833
+ return 0;
4834
+ }
4835
+
4836
+ int BSXDB::bindFilterData(sqlite3_stmt *stmt, const std::vector<FilterData> &filters)
4837
+ {
4838
+ for (size_t n = 0; n < filters.size(); ++n) {
4839
+ const auto &filter = filters[n];
4840
+ const std::string param = filter_param(n);
4841
+ if (filter.m_compare_type == ComparisonTypes::IN ||
4842
+ filter.m_compare_type == ComparisonTypes::NOT_IN) {
4843
+ for (size_t i = 0; i < filter.m_int_list.size(); ++i) {
4844
+ std::string list_index = param + "_" + std::to_string(i);
4845
+ int list_param = sqlite3_bind_parameter_index(stmt, list_index.c_str());
4846
+ if (list_param < 1) {
4847
+ LOGE("Could not find parameter index: %s", list_index.c_str());
4848
+ return 2;
4849
+ }
4850
+ sqlite3_bind_int64(stmt, list_param, filter.m_int_list[i]);
4851
+ }
4852
+ continue;
4853
+ }
4854
+ int index = sqlite3_bind_parameter_index(stmt, param.c_str());
4855
+ if (index < 1) {
4856
+ LOGE("Could not find parameter index: %s", param.c_str());
4857
+ return 2;
4858
+ }
4859
+ if (filter.m_data_type == db::CT_BYTES) {
4860
+ sqlite3_bind_blob(stmt, index, filter.m_bytes_data.data(), filter.m_bytes_data.size(),
4861
+ SQLITE_STATIC);
4862
+ } else if (filter.m_data_type == db::CT_INT64) {
4863
+ sqlite3_bind_int64(stmt, index, filter.m_int_data);
4864
+ } else if (filter.m_data_type == db::CT_INT) {
4865
+ sqlite3_bind_int64(stmt, index, filter.m_int_data);
4866
+ } else {
4867
+ LOGE("Unknown parameter type for: %s", filter.m_column_name.c_str());
4868
+ return 2;
4869
+ }
4870
+ }
4871
+ return 0;
4872
+ }
4873
+
4874
+ int BSXDB::bindRowData(sqlite3_stmt *stmt, const std::vector<FieldData> &columns)
4875
+ {
4876
+ for (const auto &column : columns) {
4877
+ std::string str_index = ":" + column.m_column_name;
4878
+ int index = sqlite3_bind_parameter_index(stmt, str_index.c_str());
4879
+ if (index < 1) {
4880
+ LOGE("Could not find parameter index: %s", str_index.c_str());
4881
+ return 2;
4882
+ }
4883
+ if (column.m_data_type == db::CT_BYTES) {
4884
+ sqlite3_bind_blob(stmt, index, column.m_bytes_data.data(), column.m_bytes_data.size(),
4885
+ SQLITE_STATIC);
4886
+ } else if (column.m_data_type == db::CT_INT64) {
4887
+ sqlite3_bind_int64(stmt, index, column.m_int_data);
4888
+ } else if (column.m_data_type == db::CT_INT) {
4889
+ sqlite3_bind_int64(stmt, index, column.m_int_data);
4890
+ } else {
4891
+ LOGE("Unknown parameter type for: %s", str_index.c_str());
4892
+ return 2;
4893
+ }
4894
+ }
4895
+ return 0;
4896
+ }
4897
+
4898
+ int BSXDB::bindSwapInfoData(sqlite3_stmt *stmt, const SwapInfo &swap_info)
4899
+ {
4900
+ bindInt(stmt, "@protocol_version", swap_info.m_protocol_version);
4901
+ bindInt64(stmt, "@created_at", swap_info.m_created_at);
4902
+ bindInt(stmt, "@day_key_count", swap_info.m_day_key_count);
4903
+ bindInt64(stmt, "@expire_at", swap_info.m_expire_at);
4904
+ bindDataVector(stmt, "@swap_id", swap_info.m_swap_id);
4905
+ bindDataVector(stmt, "@request_id", swap_info.m_request_id);
4906
+ bindDataVector(stmt, "@offer_id", swap_info.m_offer_id);
4907
+ bindInt(stmt, "@state", swap_info.m_state);
4908
+ bindInt(stmt, "@coin_from", swap_info.m_coin_from);
4909
+ bindInt(stmt, "@coin_to", swap_info.m_coin_to);
4910
+ bindInt(stmt, "@initiating", swap_info.m_initiating);
4911
+ bindInt(stmt, "@participating", swap_info.m_participating);
4912
+ bindInt64(stmt, "@amount_from", swap_info.m_amount_from);
4913
+ bindInt64(stmt, "@amount_to", swap_info.m_amount_to);
4914
+ bindInt64(stmt, "@fee_rate_from", swap_info.m_fee_rate_from);
4915
+ bindInt64(stmt, "@fee_rate_to", swap_info.m_fee_rate_to);
4916
+ bindInt(stmt, "@lock_type", swap_info.m_lock_type);
4917
+ bindInt(stmt, "@lock_time_1", swap_info.m_lock_time_1);
4918
+ bindInt(stmt, "@lock_time_2", swap_info.m_lock_time_2);
4919
+ bindDataVector(stmt, "@Kal", swap_info.m_Kal);
4920
+ bindDataVector(stmt, "@Kaf", swap_info.m_Kaf);
4921
+ bindDataVector(stmt, "@Kbl", swap_info.m_Kbl);
4922
+ bindDataVector(stmt, "@Kbf", swap_info.m_Kbf);
4923
+ bindDataVector(stmt, "@Kbl_a", swap_info.m_Kbl_a);
4924
+ bindDataVector(stmt, "@Kbf_a", swap_info.m_Kbf_a);
4925
+ bindDataVector(stmt, "@lock_tx_a", swap_info.m_lock_tx_a);
4926
+ bindDataVector(stmt, "@lock_tx_a_script", swap_info.m_lock_tx_a_script);
4927
+ bindInt(stmt, "@lock_a_txo_n", swap_info.m_lock_a_txo_n);
4928
+ bindDataVector(stmt, "@lock_spend_tx_a", swap_info.m_lock_spend_tx_a);
4929
+ bindDataVector(stmt, "@lock_refund_tx_a", swap_info.m_lock_refund_tx_a);
4930
+ bindDataVector(stmt, "@lock_refund_spend_tx_a", swap_info.m_lock_refund_spend_tx_a);
4931
+ bindDataVector(stmt, "@change_script", swap_info.m_change_script);
4932
+ bindDataVector(stmt, "@script_to", swap_info.m_script_to);
4933
+ bindDataVector(stmt, "@lock_tx_spend_a_vout", swap_info.m_lock_tx_spend_a_vout);
4934
+ bindDataVector(stmt, "@lock_tx_spend_a_otves_l", swap_info.m_lock_tx_spend_a_otves_l);
4935
+ bindDataVector(stmt, "@lock_tx_spend_a_sig_f", swap_info.m_lock_tx_spend_a_sig_f);
4936
+ bindDataVector(stmt, "@lock_tx_refund_a_sig_l", swap_info.m_lock_tx_refund_a_sig_l);
4937
+ bindDataVector(stmt, "@lock_tx_refund_a_sig_f", swap_info.m_lock_tx_refund_a_sig_f);
4938
+ bindDataVector(stmt, "@lock_tx_refund_spend_a_otves_f",
4939
+ swap_info.m_lock_tx_refund_spend_a_otves_f);
4940
+ bindInt(stmt, "@chain_from_height", swap_info.m_chain_from_height);
4941
+ bindInt(stmt, "@chain_to_height", swap_info.m_chain_to_height);
4942
+ bindDataVector(stmt, "@fee_outputs", swap_info.m_fee_outputs);
4943
+ bindDataVector(stmt, "@kal_sig", swap_info.m_kal_sig);
4944
+ bindDataVector(stmt, "@lock_spend_a_fee_outputs", swap_info.m_lock_spend_a_fee_outputs);
4945
+ bindDataVector(stmt, "@lock_spend_b_fee_outputs", swap_info.m_lock_spend_b_fee_outputs);
4946
+ bindInt64(stmt, "@lock_b_scan_height", swap_info.m_lock_b_scan_height);
4947
+ bindDataVector(stmt, "@bsx_bid_id", swap_info.m_bsx_bid_id);
4948
+ bindDataVector(stmt, "@kbsl_dleag", swap_info.m_kbsl_dleag);
4949
+ return 0;
4950
+ }
4951
+ } // namespace bsx