diff --git a/Cargo.lock b/Cargo.lock index f8835ee..82ef196 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.15" @@ -75,6 +90,18 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + [[package]] name = "backtrace" version = "0.3.71" @@ -95,10 +122,16 @@ name = "bal-server" version = "0.1.0" dependencies = [ "bitcoin", + "bitcoincore-rpc", + "bitcoincore-rpc-json", + "bs58", + "byteorder", "bytes", + "chrono", "confy", "env_logger", - "hex-conservative", + "hex", + "hex-conservative 0.1.1", "http-body-util", "hyper", "hyper-util", @@ -106,51 +139,150 @@ dependencies = [ "regex", "serde", "serde_json", + "sha2", "sqlite", "tokio", + "zmq", ] [[package]] -name = "bech32" -version = "0.10.0-beta" +name = "base58ck" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98f7eed2b2781a6f0b5c903471d48e15f56fb4e1165df8a9a2337fd1a59d45ea" +checksum = "2c8d66485a3a2ea485c1913c4572ce0256067a5377ac8c75c4960e1cda98605f" +dependencies = [ + "bitcoin-internals", + "bitcoin_hashes", +] + +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] +name = "bech32" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d965446196e3b7decd44aa7ee49e31d630118f90ef12f97900f262eb915c951d" [[package]] name = "bitcoin" -version = "0.31.2" +version = "0.32.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c85783c2fe40083ea54a33aa2f0ba58831d90fcd190f5bdc47e74e84d2a96ae" +checksum = "ce6bc65742dea50536e35ad42492b234c27904a27f0abdcbce605015cb4ea026" dependencies = [ + "base58ck", "bech32", "bitcoin-internals", + "bitcoin-io", + "bitcoin-units", "bitcoin_hashes", - "hex-conservative", + "hex-conservative 0.2.1", "hex_lit", "secp256k1", + "serde", ] [[package]] name = "bitcoin-internals" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9425c3bf7089c983facbae04de54513cce73b41c7f9ff8c845b54e7bc64ebbfb" +checksum = "30bdbe14aa07b06e6cfeffc529a1f099e5fbe249524f8125358604df99a4bed2" +dependencies = [ + "serde", +] + +[[package]] +name = "bitcoin-io" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b47c4ab7a93edb0c7198c5535ed9b52b63095f4e9b45279c6736cec4b856baf" + +[[package]] +name = "bitcoin-units" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5285c8bcaa25876d07f37e3d30c303f2609179716e11d688f51e8f1fe70063e2" +dependencies = [ + "bitcoin-internals", + "serde", +] [[package]] name = "bitcoin_hashes" -version = "0.13.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" +checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16" dependencies = [ - "bitcoin-internals", - "hex-conservative", + "bitcoin-io", + "hex-conservative 0.2.1", + "serde", +] + +[[package]] +name = "bitcoincore-rpc" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedd23ae0fd321affb4bbbc36126c6f49a32818dc6b979395d24da8c9d4e80ee" +dependencies = [ + "bitcoincore-rpc-json", + "jsonrpc", + "log", + "serde", + "serde_json", +] + +[[package]] +name = "bitcoincore-rpc-json" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8909583c5fab98508e80ef73e5592a651c954993dc6b7739963257d19f0e71a" +dependencies = [ + "bitcoin", + "serde", + "serde_json", ] [[package]] name = "bitflags" -version = "2.5.0" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bs58" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" + +[[package]] +name = "bumpalo" +version = "3.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" @@ -163,6 +295,21 @@ name = "cc" version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" +dependencies = [ + "jobserver", + "libc", + "once_cell", +] + +[[package]] +name = "cfg-expr" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d067ad48b8650848b989a59a86c6c36a995d02d2bf778d45c3c5d57bc2718f02" +dependencies = [ + "smallvec", + "target-lexicon", +] [[package]] name = "cfg-if" @@ -170,6 +317,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + [[package]] name = "colorchoice" version = "1.0.2" @@ -188,6 +349,108 @@ dependencies = [ "toml", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crossbeam" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-queue" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "dircpy" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a88521b0517f5f9d51d11925d8ab4523497dcf947073fa3231a311b63941131c" +dependencies = [ + "jwalk", + "log", + "walkdir", +] + [[package]] name = "directories" version = "5.0.1" @@ -209,6 +472,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + [[package]] name = "env_filter" version = "0.1.2" @@ -277,6 +546,16 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.14" @@ -300,18 +579,39 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "hex-conservative" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30ed443af458ccb6d81c1e7e661545f94d3176752fb1df2f543b902a1e0f51e2" +[[package]] +name = "hex-conservative" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5313b072ce3c597065a808dbf612c4c8e8590bdbf8b579508bf7a762c5eae6cd" +dependencies = [ + "arrayvec", +] + [[package]] name = "hex_lit" version = "0.1.1" @@ -405,6 +705,30 @@ dependencies = [ "tokio", ] +[[package]] +name = "iana-time-zone" +version = "0.1.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "indexmap" version = "2.2.6" @@ -428,10 +752,51 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] -name = "libc" -version = "0.2.153" +name = "jobserver" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" +dependencies = [ + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "jsonrpc" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3662a38d341d77efecb73caf01420cfa5aa63c0253fd7bc05289ef9f6616e1bf" +dependencies = [ + "base64", + "minreq", + "serde", + "serde_json", +] + +[[package]] +name = "jwalk" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2735847566356cd2179a2a38264839308f7079fa96e6bd5a42d740460e003c56" +dependencies = [ + "crossbeam", + "rayon", +] + +[[package]] +name = "libc" +version = "0.2.172" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" [[package]] name = "libredox" @@ -439,7 +804,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags", + "bitflags 2.9.0", "libc", ] @@ -464,6 +829,17 @@ dependencies = [ "adler", ] +[[package]] +name = "minreq" +version = "2.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d2aaba477837b46ec1289588180fabfccf0c3b1d1a0c6b1866240cd6cd5ce9" +dependencies = [ + "log", + "serde", + "serde_json", +] + [[package]] name = "mio" version = "0.8.11" @@ -475,6 +851,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -494,6 +879,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + [[package]] name = "option-ext" version = "0.2.0" @@ -518,6 +909,15 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-macro2" version = "1.0.81" @@ -536,6 +936,56 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redox_users" version = "0.4.5" @@ -582,6 +1032,12 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustversion" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" + [[package]] name = "ryu" version = "1.0.18" @@ -589,20 +1045,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] -name = "secp256k1" -version = "0.28.2" +name = "same-file" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d24b59d129cdadea20aea4fb2352fa053712e5d713eee47d700cd4b2bc002f10" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "secp256k1" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113" dependencies = [ "bitcoin_hashes", + "rand", "secp256k1-sys", + "serde", ] [[package]] name = "secp256k1-sys" -version = "0.9.2" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5d1746aae42c19d583c3c1a8c646bfad910498e2051c551a7f2e3c0c9fbb7eb" +checksum = "d4387882333d3aa8cb20530a17c69a3752e97837832f34f6dccc760e715001d9" dependencies = [ "cc", ] @@ -648,6 +1115,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "smallvec" version = "1.15.0" @@ -705,6 +1183,25 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "system-deps" +version = "6.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e535eb8dded36d55ec13eddacd30dec501792ff23a0b1682c38601b8cf2349" +dependencies = [ + "cfg-expr", + "heck", + "pkg-config", + "toml", + "version-compare", +] + +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + [[package]] name = "thiserror" version = "1.0.59" @@ -786,6 +1283,12 @@ dependencies = [ "winnow", ] +[[package]] +name = "typenum" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -798,12 +1301,160 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "version-compare" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "windows-core" +version = "0.61.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38" + +[[package]] +name = "windows-result" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c64fd11a4fd95df68efcfee5f44a294fe71b8bc6a91993e2791938abcc712252" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -951,3 +1602,55 @@ checksum = "f0c976aaaa0e1f90dbb21e9587cdaf1d9679a1cde8875c0d6bd83ab96a208352" dependencies = [ "memchr", ] + +[[package]] +name = "zerocopy" +version = "0.8.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zeromq-src" +version = "0.2.6+4.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc120b771270365d5ed0dfb4baf1005f2243ae1ae83703265cb3504070f4160b" +dependencies = [ + "cc", + "dircpy", +] + +[[package]] +name = "zmq" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd3091dd571fb84a9b3e5e5c6a807d186c411c812c8618786c3c30e5349234e7" +dependencies = [ + "bitflags 1.3.2", + "libc", + "zmq-sys", +] + +[[package]] +name = "zmq-sys" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e8351dc72494b4d7f5652a681c33634063bbad58046c1689e75270908fdc864" +dependencies = [ + "libc", + "system-deps", + "zeromq-src", +] diff --git a/Cargo.toml b/Cargo.toml index 99ddead..b11f91f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,50 +5,26 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +bs58 = "0.4.0" +bytes = "1.2" +bitcoin = { version = "0.32.5" } +bitcoincore-rpc = "0.19.0" +bitcoincore-rpc-json = "0.19.0" +byteorder = "1.5.0" +confy = "0.6.1" +chrono = "0.4.40" +env_logger = "0.11.5" +hex = "0.4.3" +hex-conservative = "0.1.1" hyper = { version = "1.3.1", features = ["http1","server"] } hyper-util = { version = "0.1.3", features = ["tokio"] } - -tokio = { version = "1", features = ["rt", "net","macros","rt-multi-thread"] } # Keep only necessary runtime components http-body-util = "0.1" -bytes = "1.2" +log = "0.4.21" +sha2 = "0.10.8" serde = { version = "1.0.152", features = ["derive"] } serde_json = "1.0.116" -confy = "0.6.1" -bitcoin = "0.31.0" sqlite = "0.34.0" -hex-conservative = "0.1.1" regex = "1.10.4" -log = "0.4.21" -env_logger = "0.11.5" - - -# hyper = { version = "1.2", features = ["server"] } # Disattiva tutte le funzionalità tranne "server" -# tokio = { version = "1", features = ["full"] } # Tieni "full" perché è utilizzato in tokio-util -# http-body-util = { version = "0.1", default-features = false } # Disattiva le funzionalità di default -# hyper-util = { version = "0.1", features = [] } # Disattiva tutte le funzionalità -# bytes = "1.2" -# serde = { version = "1.0.152", features = ["derive"] } -# serde_json = "1.0.116" -# confy = "0.6.1" -# bitcoin = "0.31.0" -# sqlite = "0.34.0" -# hex-conservative = "0.1.1" -# regex = "1.10.4" -# log = "0.4.21" -# env_logger = "0.11.5" - -# hyper = { version = "1.2", features = ["full"] } -# tokio = { version = "1", features = ["full"] } -# http-body-util = "0.1" -# hyper-util = { version = "0.1", features = ["full"] } -# bytes = "1.2" -# serde = { version = "1.0.152", features = ["derive"] } # <- Only one serde version needed (serde or serde_derive) -# serde_json = "1.0.116" -# confy = "0.6.1" -# bitcoin = "0.31.0" -# sqlite = "0.34.0" -# hex-conservative = "0.1.1" -# regex = "1.10.4" -# log = "0.4.21" -# env_logger = "0.11.5" +tokio = { version = "1", features = ["rt", "net","macros","rt-multi-thread"] } # Keep only necessary runtime components +zmq = "0.10.0" diff --git a/src/bin/bal-pusher.rs b/src/bin/bal-pusher.rs new file mode 100644 index 0000000..1ea4771 --- /dev/null +++ b/src/bin/bal-pusher.rs @@ -0,0 +1,421 @@ +extern crate bitcoincore_rpc; +extern crate zmq; +use bitcoin::Network; + +use bitcoincore_rpc::{bitcoin, Auth, Client, Error, RpcApi}; +use bitcoincore_rpc_json::GetBlockchainInfoResult; + +use sqlite::{Value}; +use serde::Serialize; +use serde::Deserialize; +use std::env; +use std::fs::OpenOptions; +use std::io::{ Write}; +use log::{info,debug,warn,error}; +use zmq::{Context, Socket}; +use std::str; +use std::{thread, time::Duration}; +use std::collections::HashMap; +//use byteorder::{LittleEndian, ReadBytesExt}; +//use std::io::Cursor; +use hex; +use std::error::Error as StdError; + +const LOCKTIME_THRESHOLD:i64 = 5000000; + +#[derive(Debug, Clone,Serialize, Deserialize)] +struct MyConfig { + zmq_listener: String, + requests_file: String, + db_file: String, + bitcoin_dir: String, + regtest: NetworkParams, + testnet: NetworkParams, + signet: NetworkParams, + mainnet: NetworkParams, + + +} + +impl Default for MyConfig { + fn default() -> Self { + MyConfig { + zmq_listener: "tcp://127.0.0.1:28332".to_string(), + requests_file: "rawrequests.log".to_string(), + db_file: "../bal.db".to_string(), + bitcoin_dir: "".to_string(), + regtest: get_network_params_default(Network::Regtest), + testnet: get_network_params_default(Network::Testnet), + signet: get_network_params_default(Network::Signet), + mainnet: get_network_params_default(Network::Bitcoin), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct NetworkParams { + host: String, + port: u16, + dir_path: String, + db_field: String, + cookie_file: String, + rpc_user: String, + rpc_pass: String, +} +fn get_network_params(cfg: &MyConfig,network:Network)-> &NetworkParams{ + match network{ + Network::Testnet => &cfg.testnet, + Network::Signet => &cfg.signet, + Network::Regtest => &cfg.regtest, + _ => &cfg.mainnet + } +} +fn get_network_params_default(network:Network) -> NetworkParams{ + match network { + Network::Testnet => NetworkParams{ + host: "http://localhost".to_string(), + port: 18332, + dir_path: "testnet3/".to_string(), + db_field: "testnet".to_string(), + cookie_file: "".to_string(), + rpc_user: "".to_string(), + rpc_pass: "".to_string(), + }, + Network::Signet => NetworkParams{ + host: "http://localhost".to_string(), + port: 18332, + dir_path: "signet/".to_string(), + db_field: "signet".to_string(), + cookie_file: "".to_string(), + rpc_user: "".to_string(), + rpc_pass: "".to_string(), + }, + Network::Regtest => NetworkParams{ + host: "http://localhost".to_string(), + port: 18443, + dir_path: "regtest/".to_string(), + db_field: "regtest".to_string(), + cookie_file: "".to_string(), + rpc_user: "".to_string(), + rpc_pass: "".to_string(), + }, + _ => NetworkParams{ + host: "http://localhost".to_string(), + port: 8332, + dir_path: "".to_string(), + db_field: "bitcoin".to_string(), + cookie_file: "".to_string(), + rpc_user: "".to_string(), + rpc_pass: "".to_string(), + }, + } +} + +fn get_cookie_filename(network: &NetworkParams) ->Result>{ + if network.cookie_file !=""{ + Ok(network.cookie_file.clone()) + }else{ + match env::var_os("HOME") { + Some(home) => { + info!("some home {}",home.to_str().unwrap()); + match home.to_str(){ + Some(home_str) => { + let cookie_file_path = format!("{}/.bitcoin/{}.cookie",home_str, network.dir_path); + + Ok(cookie_file_path) + }, + None => Err("wrong HOME value".into()) + } + }, + None => Err("Please Set HOME environment variable".into()) + } + } +} +fn get_client_from_username(url: &String, network: &NetworkParams) -> Result<(Client,GetBlockchainInfoResult),Box>{ + if network.rpc_user != "" { + match Client::new(&url[..],Auth::UserPass(network.rpc_user.to_string(),network.rpc_pass.to_string())){ + Ok(client) => match client.get_blockchain_info(){ + Ok(bcinfo) => Ok((client,bcinfo)), + Err(err) => Err(err.into()) + } + Err(err)=>Err(err.into()) + } + }else{ + Err("Failed".into()) + } +} +fn get_client_from_cookie(url: &String,network: &NetworkParams)->Result<(Client,GetBlockchainInfoResult),Box>{ + match get_cookie_filename(network){ + Ok(cookie) => { + match Client::new(&url[..], Auth::CookieFile(cookie.into())) { + Ok(client) => match client.get_blockchain_info(){ + Ok(bcinfo) => Ok((client,bcinfo)), + Err(err) => Err(err.into()) + }, + Err(err)=>Err(err.into()) + + } + }, + Err(err)=>Err(err.into()) + } +} +fn get_client(network: &NetworkParams) -> Result<(Client,GetBlockchainInfoResult),Box>{ + let url = format!("{}:{}",network.host,&network.port); + match get_client_from_username(&url,network){ + Ok(client) =>{Ok(client)}, + Err(_) =>{ + match get_client_from_cookie(&url,&network){ + Ok(client)=>{ + Ok(client) + }, + Err(err)=> Err(err.into()) + } + } + } +} +fn main_result(cfg: &MyConfig, network_params: &NetworkParams) -> Result<(), Error> { + + + /*let url = args.next().expect("Usage: "); + let user = args.next().expect("no user given"); + let pass = args.next().expect("no pass given"); + */ + //let network = Network::Regtest + match get_client(network_params){ + Ok((rpc,bcinfo)) => { + info!("connected"); + //let best_block_hash = rpc.get_best_block_hash()?; + //info!("best block hash: {}", best_block_hash); + //let bestblockcount = rpc.get_block_count()?; + //info!("best block height: {}", bestblockcount); + //let best_block_hash_by_height = rpc.get_block_hash(bestblockcount)?; + //info!("best block hash by height: {}", best_block_hash_by_height); + //assert_eq!(best_block_hash_by_height, best_block_hash); + //let from_block= std::cmp::max(0, bestblockcount - 11); + //let mut time_sum:u64=0; + //for i in from_block..bestblockcount{ + // let hash = rpc.get_block_hash(i).unwrap(); + // let block: bitcoin::Block = rpc.get_by_id(&hash).unwrap(); + // time_sum += >::into(block.header.time); + //} + //let average_time = time_sum/11; + info!("median time: {}",bcinfo.median_time); + let average_time = bcinfo.median_time; + let db = sqlite::open(&cfg.db_file).unwrap(); + + let query_tx = db.prepare("SELECT * FROM tbl_tx WHERE network = :network AND status = :status AND ( locktime < :bestblock_height OR locktime > :locktime_threshold AND locktime < :bestblock_time);").unwrap().into_iter(); + //let query_tx = db.prepare("SELECT * FROM tbl_tx where status = :status").unwrap().into_iter(); + let mut pushed_txs:Vec = Vec::new(); + let mut invalid_txs: std::collections::HashMap = HashMap::new(); + for row in query_tx.bind::<&[(_, Value)]>(&[ + (":locktime_threshold", (LOCKTIME_THRESHOLD as i64).into()), + (":bestblock_time", (average_time as i64).into()), + (":bestblock_height", (bcinfo.blocks as i64).into()), + (":network", network_params.db_field.clone().into()), + (":status", 0.into()), + ][..]) + .unwrap() + .map(|row| row.unwrap()) + { + let tx = row.read::<&str, _>("tx"); + let txid = row.read::<&str, _>("txid"); + let locktime = row.read::("locktime"); + info!("to be pushed: {}: {}",txid, locktime); + match rpc.send_raw_transaction(tx){ + Ok(o) => { + let mut file = OpenOptions::new() + .append(true) // Set the append option + .create(true) // Create the file if it doesn't exist + .open("valid_txs")?; + let data = format!("{}\t:\t{}\t:\t{}\n",txid,average_time,locktime); + file.write_all(data.as_bytes())?; + drop(file); + + info!("tx: {} pusshata PUSHED\n{}",txid,o); + pushed_txs.push(txid.to_string()); + }, + Err(err) => { + let mut file = OpenOptions::new() + .append(true) // Set the append option + .create(true) // Create the file if it doesn't exist + .open("invalid_txs")?; + let data = format!("{}:\t{}\t:\t{}\t:\t{}\n",txid,err,average_time,locktime); + file.write_all(data.as_bytes())?; + drop(file); + warn!("Error: {}\n{}",err,txid); + //store err in invalid_txs + invalid_txs.insert(txid.to_string(), err.to_string()); + + }, + }; + } + + if pushed_txs.len() > 0 { + let _ = db.execute(format!("UPDATE tbl_tx SET status = 1 WHERE txid in ('{}');",pushed_txs.join("','"))); + } + if invalid_txs.len() > 0 { + for (txid,txerr) in &invalid_txs{ + //let _ = db.execute(format!("UPDATE tbl_tx SET status = 2 WHERE txid in ('{}'Yp);",invalid_txs.join("','"))); + let _ = db.execute(format!("UPDATE tbl_tx SET status = 2, push_err='{txerr}' WHERE txid = '{txid}'")); + } + } + } + Err(_)=>{ + panic!("impossible to get client") + } + } + Ok(()) +} + +fn parse_env(cfg: &mut MyConfig){ + match env::var("BAL_PUSHER_ZMQ_LISTENER") { + Ok(value) => { + cfg.zmq_listener = value;}, + Err(_) => {}, + } + match env::var("BAL_PUSHER_REQUEST_FILE") { + Ok(value) => { + cfg.requests_file = value;}, + Err(_) => {}, + } + match env::var("BAL_PUSHER_DB_FILE") { + Ok(value) => { + cfg.db_file = value;}, + Err(_) => {}, + } + match env::var("BAL_PUSHER_BITCOIN_DIR") { + Ok(value) => { + cfg.bitcoin_dir = value;}, + Err(_) => {}, + } + cfg.regtest = parse_env_netconfig(cfg,"regtest"); + cfg.signet = parse_env_netconfig(cfg,"signet"); + cfg.testnet = parse_env_netconfig(cfg,"testnet"); + drop(parse_env_netconfig(cfg,"bitcoin")); + +} +fn parse_env_netconfig(cfg_lock: &mut MyConfig, chain: &str) -> NetworkParams{ +//fn parse_env_netconfig(cfg_lock: &MutexGuard, chain: &str) -> &NetworkParams{ + let cfg = match chain{ + "regtest" => &mut cfg_lock.regtest, + "signet" => &mut cfg_lock.signet, + "testnet" => &mut cfg_lock.testnet, + &_ => &mut cfg_lock.mainnet, + }; + match env::var(format!("BAL_PUSHER_{}_HOST",chain.to_uppercase())) { + Ok(value) => { cfg.host= value; }, + Err(_) => {}, + } + match env::var(format!("BAL_PUSHER_{}_PORT",chain.to_uppercase())) { + Ok(value) => { + match value.parse::(){ + Ok(value) =>{ cfg.port = value.try_into().unwrap(); }, + Err(_) => {}, + } + } + Err(_) => {}, + } + match env::var(format!("BAL_PUSHER_{}_DIR_PATH",chain.to_uppercase())) { + Ok(value) => { cfg.dir_path = value; }, + Err(_) => {}, + } + match env::var(format!("BAL_PUSHER_{}_DB_FIELD",chain.to_uppercase())) { + Ok(value) => { cfg.db_field = value; }, + Err(_) => {}, + } + match env::var(format!("BAL_PUSHER_{}_COOKIE_FILE",chain.to_uppercase())) { + Ok(value) => { + cfg.cookie_file = value; }, + Err(_) => {}, + } + match env::var(format!("BAL_PUSHER_{}_RPC_USER",chain.to_uppercase())) { + Ok(value) => { cfg.rpc_user = value; }, + Err(_) => {}, + } + match env::var(format!("BAL_PUSHER_{}_RPC_PASSWORD",chain.to_uppercase())) { + Ok(value) => { cfg.rpc_pass = value; }, + Err(_) => {}, + } + cfg.clone() +} + +fn get_default_config()-> MyConfig { + let file = confy::get_configuration_file_path("bal-pusher",None).expect("Error while getting path"); + info!("Default configuration file path is: {:#?}", file); + confy::load("bal-pusher",None).expect("cant_load") +} + +fn main(){ + env_logger::init(); + let mut cfg: MyConfig = match env::var("BAL_PUSHER_CONFIG_FILE") { + Ok(value) => { + match confy::load_path(value.to_string()){ + Ok(val) => { + info!("The configuration file path is: {:#?}", value); + val + }, + Err(err) => { + error!("{}",err); + get_default_config() + } + } + }, + Err(_) => { + get_default_config() + }, + }; + + parse_env(&mut cfg); + let mut args = std::env::args(); + let _exe_name = args.next().unwrap(); + let arg_network = match args.next(){ + Some(nargs) => nargs, + None => "bitcoin".to_string() + }; + let network = match arg_network.as_str(){ + + "testnet" => Network::Testnet, + "signet" => Network::Signet, + "regtest" => Network::Regtest, + _ => Network::Bitcoin, + }; + + + debug!("Network: {}",arg_network); + let network_params = get_network_params(&cfg,network); + + + let context = Context::new(); + let socket: Socket = context.socket(zmq::SUB).unwrap(); + + let zmq_address = cfg.zmq_listener.clone(); + socket.connect(&zmq_address).unwrap(); + + socket.set_subscribe(b"").unwrap(); + + let _ = main_result(&cfg,&network_params); + info!("waiting new blocks.."); + let mut last_seq:Vec=[0;4].to_vec(); + loop { + let message = socket.recv_multipart(0).unwrap(); + let topic = message[0].clone(); + let body = message[1].clone(); + let seq = message[2].clone(); + if last_seq >= seq { + continue + } + last_seq = seq; + //let mut sequence_str = "Unknown".to_string(); + /*if seq.len()==4{ + let mut rdr = Cursor::new(seq); + let sequence = rdr.read_u32::().expect("Failed to read integer"); + sequence_str = sequence.to_string(); + }*/ + if topic == b"hashblock" { + info!("NEW BLOCK{}", hex::encode(body)); + //let cfg = cfg.clone(); + let _ = main_result(&cfg,&network_params); + } + thread::sleep(Duration::from_millis(100)); // Sleep for 100ms + } +} diff --git a/src/bin/bal-server.rs b/src/bin/bal-server.rs new file mode 100644 index 0000000..06d1701 --- /dev/null +++ b/src/bin/bal-server.rs @@ -0,0 +1,551 @@ +use bytes::Bytes; +use http_body_util::{ combinators::BoxBody, BodyExt, Empty, Full }; +use hyper::server::conn::http1; +use hyper::service::service_fn; +use hyper::{ Method, Request, Response, StatusCode }; +use tokio::net::TcpListener; +use hyper_util::rt::TokioIo; + + +use std::net::IpAddr; +use std::env; + +//use std::time::{SystemTime,UNIX_EPOCH}; +use std::sync::{ Arc, Mutex, MutexGuard }; +//use std::net::SocketAddr; +use std::collections::HashMap; +use sqlite::{ State, Value, Connection }; + +use bitcoin::{ consensus, Transaction, Network }; + +use hex_conservative::FromHex; +use regex::Regex; +use serde::{ Serialize, Deserialize}; +use log::{ info, error, trace, debug}; +use serde_json; +use chrono::Utc; + +#[path = "../db.rs"] +mod db; +use crate::db::{ create_database, get_next_address_index, insert_xpub, save_new_address, get_last_used_address_by_ip, execute_insert }; + + +#[path = "../xpub.rs"] +mod xpub; +use crate::xpub::new_address_from_xpub; +const VERSION:&str="0.2.0"; +const NETWORKS : [&str; 4]= ["bitcoin","testnet","signet","regtest"]; +#[derive(Debug, Clone,Serialize, Deserialize)] +struct NetConfig { + address: String, + fixed_fee: u64, + xpub: bool, + network: Network, + name: String, + enabled: bool, +} + +impl NetConfig { + fn default_network(name:String, network: Network) -> Self { + NetConfig { + address: "".to_string(), + fixed_fee: 50000, + xpub: false, + name:name, + network:network, + enabled: false, + } + } +} + +#[derive(Debug, Serialize, Deserialize,Clone)] +struct MyConfig { + regtest: NetConfig, + signet: NetConfig, + testnet: NetConfig, + mainnet: NetConfig, + bind_address: String, + bind_port: u16, // Changed to u16 for port numbers + db_file: String, +} + +impl Default for MyConfig { + fn default() -> Self { + MyConfig { + regtest: NetConfig::default_network("regtest".to_string(), Network::Regtest), + signet: NetConfig::default_network("signet".to_string(), Network::Signet), + testnet: NetConfig::default_network("testnet".to_string(), Network::Testnet), + mainnet: NetConfig::default_network("bitcoin".to_string(), Network::Bitcoin), + bind_address: "127.0.0.1".to_string(), + bind_port: 9137, + db_file: "bal.db".to_string(), + } + } +} +impl MyConfig { + fn get_net_config(&self, param: &str) -> &NetConfig{ + match param { + "regtest" => &self.regtest, + "testnet" => &self.testnet, + "signet" => &self.signet, + _ => &self.mainnet, + } + } +} + +async fn echo_version( +) -> Result>, hyper::Error> { + Ok(Response::new(full(VERSION))) +} +async fn echo_info( + param: &str, + cfg: &MyConfig, + remote_addr: String, +) -> Result>, hyper::Error> { + info!("echo info!!!{}",param); + let netconfig=MyConfig::get_net_config(cfg,param); + if !netconfig.enabled { + trace!("network disabled"); + return Ok(Response::new(full("network disabled"))); + } + let address = match netconfig.xpub{ + false => { + let address = netconfig.address.to_string(); + info!("is address: {}",&address); + address + }, + true => { + let db = sqlite::open(&cfg.db_file).unwrap(); + match get_last_used_address_by_ip(&db,&netconfig.name,&netconfig.address,&remote_addr){ + Some(address)=>address, + None => { + let next = get_next_address_index(&db,&netconfig.name,&netconfig.address); + let address = new_address_from_xpub(&netconfig.address,next.1,netconfig.network).unwrap(); + save_new_address(&db,next.0,&address.0,&address.1,&remote_addr); + debug!("address {} {}",address.0,address.1); + debug!("next {} {}",next.0,next.1); + address.0 + } + } + } + }; + trace!("address: {}:{}",address,netconfig.fixed_fee); + return Ok(Response::new(full("{\"address\":\"".to_owned()+&address+"\",\"base_fee\":\""+&netconfig.fixed_fee.to_string()+"\"}"))); +} +async fn echo_search(whole_body: &Bytes, + cfg: &MyConfig, +) -> Result>, hyper::Error> { + info!("echo search!!!"); + let strbody = std::str::from_utf8(&whole_body).unwrap(); + info!("{}",strbody); + + let mut response = Response::new(full("Bad data received".to_owned())); + *response.status_mut() = StatusCode::BAD_REQUEST; + if strbody.len() >0 && strbody.len()<=70 { + let db = sqlite::open(&cfg.db_file).unwrap(); + let mut statement = db.prepare("SELECT * FROM tbl_tx WHERE txid = ?").unwrap(); + statement.bind((1, strbody)).unwrap(); + + while let Ok(State::Row) = statement.next() { + let mut response_data = HashMap::new(); + match statement.read::("status") { + Ok(value) => response_data.insert("status", value), + Err(e) => { + error!("Error reading status: {}", e); + break; + //response_data.insert("status", "Error".to_string()) + } + }; + + // Read the transaction (tx) + match statement.read::("tx") { + Ok(value) => response_data.insert("tx", value), + Err(e) => { + error!("Error reading tx: {}", e); + break; + //response_data.insert("tx", "Error".to_string()) + } + }; + + match statement.read::("our_address") { + Ok(value) => response_data.insert("our_address", value), + Err(e) => { + error!("Error reading address: {}", e); + break; + //response_data.insert("tx", "Error".to_string()) + } + }; + + match statement.read::("our_fees") { + Ok(value) => response_data.insert("our_fees", value), + Err(e) => { + error!("Error reading fees: {}", e); + break; + //response_data.insert("tx", "Error".to_string()) + } + }; + + // Read the request id (reqid) + match statement.read::("reqid") { + Ok(value) => response_data.insert("time", value), + Err(e) => { + error!("Error reading reqid: {}", e); + break; + //response_data.insert("time", "Error".to_string()) + } + }; + response = match serde_json::to_string(&response_data){ + Ok(json_data) => Response::new(full(json_data)), + Err(_) => {break;} + }; + + return Ok(response); + } + } + Ok(response) + + +} +async fn echo_push(whole_body: &Bytes, + cfg: &MyConfig, + param: &str, +) -> Result>, hyper::Error> { + //let whole_body = req.collect().await?.to_bytes(); + let strbody = std::str::from_utf8(&whole_body).unwrap(); + info!("network:{}\n{}",¶m, &strbody); + + let mut response = Response::new(full("Bad data received".to_owned())); + let mut response_not_enable = Response::new(full("Network not enabled".to_owned())); + *response.status_mut() = StatusCode::BAD_REQUEST; + *response_not_enable.status_mut()=StatusCode::BAD_REQUEST; + let netconfig = MyConfig::get_net_config(&cfg,param); + if !netconfig.enabled{ + return Ok(response_not_enable); + } + let req_time = Utc::now().timestamp_nanos_opt().unwrap(); // Returns i64 + + + let lines = strbody.split("\n"); + let sqltxshead = "INSERT INTO tbl_tx (txid, wtxid, ntxid, tx, locktime, reqid, network, our_address, our_fees)".to_string(); + let mut sqltxs = "".to_string(); + let sqlinpshead = "INSERT INTO tbl_inp (txid, in_txid, in_vout )".to_string(); + let mut sqlinps = "".to_string(); + let sqloutshead = "INSERT INTO tbl_out (txid, vout, script_pubkey, amount )".to_string(); + let mut sqlouts = "".to_string(); + let mut union_tx = true; + let mut union_inps = true; + let mut union_outs = true; + let mut already_present = false; + let db = sqlite::open(&cfg.db_file).unwrap(); + let netconfig = MyConfig::get_net_config(cfg,param); + let mut ptx:Vec<(usize, Value)> = vec![]; + let mut pinps:Vec<(usize, Value)> = vec![]; + let mut pouts:Vec<(usize, Value)> = vec![]; + let mut linenum = 1; + let mut lineinp = 1; + let mut lineout = 1; + for line in lines { + if line.len() == 0{ + trace!("line len is: {}",line.len()); + continue + } + let linea = format!("{req_time}:{line}"); + info!("New Tx: {}", linea); + let raw_tx = match Vec::::from_hex(line) { + Ok(raw_tx) => raw_tx, + Err(err) => { + error!("rawtx error: {}",err); + continue + } + }; + if raw_tx.len() > 0 { + trace!("len: {}",raw_tx.len()); + let tx: Transaction = match consensus::deserialize(&raw_tx){ + Ok(tx) => tx, + Err(err) => {error!("error: unable to parse tx: {}\n{}",line,err);continue} + }; + let txid = tx.compute_txid().to_string(); + trace!("txid: {}",txid); + let mut statement = db.prepare("SELECT * FROM tbl_tx WHERE txid = ?").unwrap(); + statement.bind((1,&txid[..])).unwrap(); + if let Ok(State::Row) = statement.next() { + trace!("already present"); + already_present=true; + continue; + } + let ntxid = tx.compute_ntxid(); + let wtxid = tx.compute_wtxid(); + let mut found = false; + let locktime = tx.lock_time; + let mut our_address:String = "".to_string(); + let mut our_fees:u64 = 0; + for input in tx.input{ + if union_inps == false { + sqlinps = format!("{sqlinps} UNION ALL"); + }else{ + union_inps = false; + } + sqlinps = format!("{sqlinps} SELECT ?, ?, ?"); + pinps.push((lineinp+0,Value::String(txid.to_string()))); + pinps.push((lineinp+1,Value::String(input.previous_output.txid.to_string()))); + pinps.push((lineinp+2,Value::String(input.previous_output.vout.to_string()))); + lineinp += 3; + + } + if netconfig.fixed_fee ==0 { + found = true; + } + let mut idx = 0; + for output in tx.output{ + let script_pubkey = output.script_pubkey; + let address = match bitcoin::Address::from_script(script_pubkey.as_script(), netconfig.network){ + Ok(address) => address.to_string(), + Err(_) => String::new(), + }; + let amount = output.value; + our_fees = netconfig.fixed_fee;//search wllexecutor output + if netconfig.xpub{ + let sql="select * from tbl_address where address=?"; + let mut stmt = db.prepare(&sql).expect("failed to fetch addresses"); + stmt.bind((1,Value::String(address.to_string()))).unwrap(); + if let Ok(State::Row) = stmt.next() { + our_address = address.to_string(); + } + } else { + our_address = netconfig.address.to_string(); + } + if address == our_address && amount.to_sat() >= netconfig.fixed_fee { + our_fees = amount.to_sat(); + our_address = netconfig.address.to_string(); + found = true; + trace!("address and fees are correct {}: {}",our_address,our_fees); + } + if union_outs == false { + sqlouts = format!("{sqlouts} UNION ALL"); + }else{ + union_outs = false; + } + sqlouts = format!("{sqlouts} SELECT ?, ?, ?, ?"); + pouts.push((lineout+0,Value::String(txid.to_string()))); + pouts.push((lineout+1,Value::Integer(idx))); + pouts.push((lineout+2,Value::String(script_pubkey.to_string()))); + pouts.push((lineout+3,Value::Integer(amount.to_sat().try_into().unwrap()))); + idx += 1; + lineout += 4; + } + if found == false{ + error!("willexecutor output not found "); + return Ok(response) + } else { + if union_tx == false { + sqltxs = format!("{sqltxs} UNION ALL"); + }else{ + union_tx = false; + } + sqltxs = format!("{sqltxs} SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?"); + ptx.push((linenum+0,Value::String(txid))); + ptx.push((linenum+1,Value::String(wtxid.to_string()))); + ptx.push((linenum+2,Value::String(ntxid.to_string()))); + ptx.push((linenum+3,Value::String(line.to_string()))); + ptx.push((linenum+4,Value::String(locktime.to_string()))); + ptx.push((linenum+5,Value::String(req_time.to_string()))); + ptx.push((linenum+6,Value::String(param.to_string()))); + ptx.push((linenum+7,Value::String(our_address.to_string()))); + ptx.push((linenum+8,Value::String(our_fees.to_string()))); + linenum += 9; + } + }else{ + trace!("rawTx len is: {}",raw_tx.len()); + debug!("{}",&sqltxs); + } + } + if sqltxs.len()== 0{ + if already_present == true{ + return Ok(Response::new(full("already present"))) + } + } + let sqltxs = format!("{}{};", sqltxshead, sqltxs); + let sqlinps = format!("{}{};", sqlinpshead, sqlinps); + let sqlouts = format!("{}{};", sqloutshead, sqlouts); + if let Err(err) = execute_insert(&db, sqltxs, ptx, sqlinps, pinps, sqlouts, pouts){ + debug!("{}",err); + return Ok(response); + } + Ok(Response::new(full("thx"))) +} + +fn match_uri<'a>(path: &str, uri: &'a str) -> Option<&'a str> { + let re = Regex::new(path).unwrap(); + if let Some(captures) = re.captures(uri) { + if let Some(param) = captures.name("param") { + return Some(param.as_str()); + } + } + None +} + +/// This is our service handler. It receives a Request, routes on its +/// path, and returns a Future of a Response. + +async fn echo( + req: Request, + cfg: &MyConfig, + ip: &String +) -> Result>, hyper::Error> { + + let mut not_found = Response::new(empty()); + *not_found.status_mut() = StatusCode::NOT_FOUND; + let mut ret: Result>, hyper::Error> = Ok(not_found); + + let uri = req.uri().path().to_string(); + + let remote_addr = req.headers().get("X-Real-IP").and_then(|value| value.to_str().ok()).and_then(|xff| xff.split(',').next()).map(|ip| ip.trim().to_string()).unwrap_or_else(|| ip.to_string()); + trace!("{}: {}",remote_addr,uri); + match req.method() { + // Serve some instructions at / + &Method::POST => { + let whole_body = req.collect().await?.to_bytes(); + if let Some(param) = match_uri(r"^?/?(?P[^/]?+)?/pushtxs$",uri.as_str()) { + //let whole_body = collect_body(req,512_000).await?; + ret = echo_push(&whole_body,cfg,param).await; + } + if uri=="/searchtx"{ + //let whole_body = collect_body(req,64).await?; + ret = echo_search(&whole_body,cfg).await; + } + ret + } + &Method::GET => { + if let Some(param) = match_uri(r"^?/?(?P[^/]?+)?/info$",uri.as_str()) { + ret = echo_info(param,cfg,remote_addr).await; + } + if uri=="/version"{ + ret= echo_version().await; + } + ret + } + + // Return the 404 Not Found for other routes. + _ => ret + } +} + +fn empty() -> BoxBody { + Empty::::new() + .map_err(|never| match never {}) + .boxed() +} + +fn full>(chunk: T) -> BoxBody { + Full::new(chunk.into()) + .map_err(|never| match never {}) + .boxed() +} +fn parse_env(cfg: &Arc>){ + let mut cfg_lock = cfg.lock().unwrap(); + match env::var("BAL_SERVER_DB_FILE") { + Ok(value) => { + cfg_lock.db_file = value;}, + Err(_) => {}, + } + match env::var("BAL_SERVER_BIND_ADDRESS") { + Ok(value) => { + cfg_lock.bind_address= value;}, + Err(_) => {}, + } + match env::var("BAL_SERVER_BIND_PORT") { + Ok(value) => { + match value.parse::(){ + Ok(value) =>{ cfg_lock.bind_port = value; }, + Err(_) => {}, + } + } + Err(_) => {}, + } + cfg_lock = parse_env_netconfig(cfg_lock,"regtest"); + cfg_lock = parse_env_netconfig(cfg_lock,"signet"); + cfg_lock = parse_env_netconfig(cfg_lock,"testnet"); + drop(parse_env_netconfig(cfg_lock,"bitcoin")); + +} +fn parse_env_netconfig<'a>(mut cfg_lock: MutexGuard<'a, MyConfig>, chain: &'a str) -> MutexGuard<'a, MyConfig>{ + let cfg = match chain{ + "regtest" => &mut cfg_lock.regtest, + "signet" => &mut cfg_lock.signet, + "testnet" => &mut cfg_lock.testnet, + &_ => &mut cfg_lock.mainnet, + }; + match env::var(format!("BAL_SERVER_{}_ADDRESS",chain.to_uppercase())) { + Ok(value) => { + cfg.address = value; + if cfg.address.len() > 5 { + if cfg.address[1..4] == *"pub" { + cfg.xpub=true; + trace!("is_xpub"); + } + cfg.enabled=true; + } + }, + + + Err(_) => {}, + } + match env::var(format!("BAL_SERVER_{}_FIXE_FEE",chain.to_uppercase())) { + Ok(value) => { + match value.parse::(){ + Ok(value) =>{ cfg.fixed_fee = value; }, + Err(_) => {}, + } + } + Err(_) => {}, + } + cfg_lock +} + +fn init_network(db: &Connection, cfg: &MyConfig){ + for network in NETWORKS{ + let netconfig = MyConfig::get_net_config(cfg,network); + insert_xpub(&db,&netconfig.name,&netconfig.address); + } +} +#[tokio::main] +async fn main() -> Result<(), Box> { + env_logger::init(); + let cfg: Arc> =Arc::>::default(); + parse_env(&cfg); + + + let cfg_lock = cfg.lock().unwrap(); + + let db = sqlite::open(&cfg_lock.db_file).unwrap(); + create_database(&db); + init_network(&db,&cfg_lock); + + let addr = cfg_lock.bind_address.to_string(); + let addr: IpAddr = addr.parse()?; + + let listener = TcpListener::bind((addr,cfg_lock.bind_port)).await?; + info!("Listening on http://{}:{}", addr,cfg_lock.bind_port); + + + loop { + let (stream, _) = listener.accept().await?; + let ip = stream.peer_addr()?.to_string().split(":").next().unwrap().to_string(); + let io = TokioIo::new(stream); + + tokio::task::spawn({ + let cfg = cfg_lock.clone(); + async move { + if let Err(err) = http1::Builder::new() + .serve_connection(io, service_fn(|req: Request| async { + echo(req,&cfg,&ip).await + })) + .await + { + error!("Error serving connection: {:?}", err); + } + + } + }); + } +} diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..17b5253 --- /dev/null +++ b/src/db.rs @@ -0,0 +1,132 @@ +use sqlite::{ Connection, Value, State, Error }; +use log::{info, trace, error}; + +pub fn create_database(db: &Connection){ + info!("database sanity check"); + let _ = db.execute("CREATE TABLE IF NOT EXISTS tbl_tx (txid PRIMARY KEY, date_creation TIMESTAMP DEFAULT CURRENT_TIMESTAMP, date_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP, wtxid, ntxid, tx, locktime integer, network, network_fees, reqid, our_fees, our_address, status integer DEFAULT 0);"); + let _ = db.execute("ALTER TABLE tbl_tx ADD COLUMN push_err TEXT"); + + let _ = db.execute("CREATE TABLE IF NOT EXISTS tbl_inp(id, txid, in_txid, in_vout);"); + let _ = db.execute("CREATE UNIQUE INDEX ON tbl_inp(txid,in_txid,in_vout);"); + + let _ = db.execute("CREATE TABLE IF NOT EXISTS tbl_out(id, txid, script_pubkey, amount, vout);"); + let _ = db.execute("CREATE UNIQUE INDEX ON tbl_out(txid, script_pubkey, amount, vout);"); + + + let _ = db.execute("CREATE TABLE IF NOT EXISTS tbl_xpub (id INTEGER PRIMARY KEY , network TEXT, xpub TEXT, date_create TIMESTAMP DEFAULT CURRENT_TIMESTAMP,path_idx INTEGER DEFAULT -1);"); + let _ = db.execute("CREATE UNIQUE INDEX idx_xpub ON tbl_xpub (network, xpub)"); + let _ = db.execute("CREATE TABLE IF NOT EXISTS tbl_address (address TEXT PRIMARY_KEY, path TEXT NOT NULL, date_create TIMESTAMP DEFAULT CURRENT_TIMESTAMP, xpub INTEGER,remote_address TEXT);"); + + +} +/* + pub fn get_xpub_id(db: &Connection, network: &String, xpub: &String) -> Option{ + let mut stmt = db.prepare("SELECT * FROM tbl_xpub where network = ? and xpub = ?;").unwrap(); + let _ = stmt.bind((1,Value::String(network.to_string()))).unwrap(); + let _ = stmt.bind((2,Value::String(xpub.to_string()))).unwrap(); + if let Ok(State::Row) = stmt.next(){ + return Some(stmt.read::("id").unwrap()); + } else { + return None; + } +} +*/ +pub fn insert_xpub(db: &Connection, network: &String, xpub: &String){ + if xpub != "" { + trace!("going to insert: {} xpub:{}", network, xpub); + let mut stmt = db.prepare ("INSERT INTO tbl_xpub(network,xpub) VALUES(?, ?);").unwrap(); + let _ = stmt.bind((1,Value::String(network.to_string()))).unwrap(); + let _ = stmt.bind((2,Value::String(xpub.to_string()))).unwrap(); + let _ = stmt.next(); + } +} + +pub fn get_last_used_address_by_ip(db: &Connection, network: &String, xpub: &String, address: &String) -> Option{ + let mut stmt = db.prepare("SELECT tbl_address.address FROM tbl_xpub join tbl_address on(tbl_xpub.id = tbl_address.xpub) where tbl_xpub.network = ? and tbl_address.remote_address = ? and tbl_xpub.xpub = ?ORDER BY tbl_address.date_create DESC LIMIT 1;").unwrap(); + let _ = stmt.bind((1,Value::String(network.to_string()))); + let _ = stmt.bind((2,Value::String(address.to_string()))); + let _ = stmt.bind((3,Value::String(xpub.to_string()))); + if let Ok(State::Row) = stmt.next(){ + let address = stmt.read::("address").unwrap(); + return Some(address); + }else{ + return None; + } + +} +pub fn get_next_address_index(db: &Connection, network: &String, xpub: &String) -> (i64,i64){ + let mut stmt = db.prepare("UPDATE tbl_xpub SET path_idx = path_idx + 1 WHERE network = ? and xpub= ? RETURNING path_idx,id;").unwrap(); + let _ = stmt.bind((1,Value::String(network.to_string()))).unwrap(); + let _ = stmt.bind((2,Value::String(xpub.to_string()))).unwrap(); + match stmt.next(){ + Ok(State::Row) =>{ + let next = stmt.read::("path_idx").unwrap(); + let id = stmt.read::("id").unwrap(); + return (id,next); + },Err(_)=> { + return (0,0); + },Ok(State::Done) =>{ + return (0,0); + } + }; +} +pub fn save_new_address(db: &Connection,xpub: i64,address: &String, path: &String,remote_addr: &String){ + let mut stmt = db.prepare("INSERT INTO tbl_address(address,path,xpub,remote_address) VALUES(?,?,?,?);").unwrap(); + + let _ = stmt.bind((1,Value::String(address.to_string()))).unwrap(); + let _ = stmt.bind((2,Value::String(path.to_string()))).unwrap(); + let _ = stmt.bind((3,Value::Integer(xpub))).unwrap(); + let _ = stmt.bind((4,Value::String(remote_addr.to_string()))).unwrap(); + + let _ = stmt.next(); +} +pub fn execute_insert(db: &Connection, + sqltxs: String, + ptx: Vec<(usize, Value)>, + sqlinp: String, + pinp: Vec<(usize, Value)>, + sqlout: String, + pout: Vec<(usize, Value)>) -> Result<(),Error>{ + let _ = db.execute("BEGIN TRANSACTION"); + let mut stmt = db.prepare(sqltxs.as_str()).expect("failed to prepare sqltxs"); + if let Err(err) = stmt.bind::<&[(_,Value)]>(&ptx[..]) { + error!("error binding transaction parameters: {}", err); + let _ = db.execute("ROLLBACK"); + return Err(err); + + } + if let Err(err) = stmt.next() { + error!("error inserting transactions {}",err); + let _ = db.execute("ROLLBACK"); + }else{ + let mut stmt = db.prepare(sqlinp.as_str()).expect("failed to prepare sqlinp"); + if let Err(err) = stmt.bind::<&[(_,Value)]>(&pinp[..]) { + error!("error binding inputs parameters {}", err); + let _ = db.execute("ROLLBACK"); + return Err(err); + } + if let Err(err) = stmt.next() { + error!("error inserting inputs {}", err); + let _ = db.execute("ROLLBACK"); + return Err(err); + + }else{ + let mut stmt = db.prepare(sqlout.as_str()).expect("failed to prepare sqlout"); + if let Err(err) = stmt.bind::<&[(_,Value)]>(&pout[..]) { + error!("error binding outs parameters {}", err); + let _ = db.execute("ROLLBACK"); + return Err(err); + } + if let Err(err) = stmt.next() { + error!("error inserting outs {}", err); + let _ = db.execute("ROLLBACK"); + return Err(err); + + } + } + } + let _ = db.execute("COMMIT"); + Ok(()) + +} + diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 0fe0321..0000000 --- a/src/main.rs +++ /dev/null @@ -1,506 +0,0 @@ - -use bytes::Bytes; -use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full}; -use hyper::server::conn::http1; -use hyper::service::service_fn; -use hyper::{Method, Request, Response, StatusCode}; -use tokio::net::TcpListener; -use hyper_util::rt::TokioIo; - - -use std::net::IpAddr; -use std::env; - -use std::time::{SystemTime,UNIX_EPOCH}; -use std::sync::{Arc, Mutex, MutexGuard}; -use std::collections::HashMap; -use sqlite::{State,Connection}; - -use bitcoin::{consensus, Transaction, Network}; - -use hex_conservative::FromHex; -use regex::Regex; -use serde::{Serialize, Deserialize}; -use log::{info,error,trace,debug}; -use serde_json; - -#[derive(Debug, Clone,Serialize, Deserialize)] -struct NetConfig { - address: String, - fixed_fee: u64, -} - -impl Default for NetConfig { - fn default() -> Self { - NetConfig { - address: "".to_string(), - fixed_fee: 50000, - } - } -} - -impl NetConfig { - fn default_regtest() -> Self { - NetConfig { - address: "".to_string(), - fixed_fee: 50000, - } - } -} - -#[derive(Debug, Serialize, Deserialize,Clone)] -struct MyConfig { - regtest: NetConfig, - signet: NetConfig, - testnet4: NetConfig, - testnet3: NetConfig, - mainnet: NetConfig, - bind_address: String, - bind_port: u16, // Changed to u16 for port numbers - db_file: String, -} - -impl Default for MyConfig { - fn default() -> Self { - MyConfig { - regtest: NetConfig::default_regtest(), - signet: NetConfig::default(), // Use default for other networks - testnet4: NetConfig::default(), - testnet3: NetConfig::default(), - mainnet: NetConfig::default(), - bind_address: "127.0.0.1".to_string(), - bind_port: 9137, // Ensure this is a u16 - db_file: "../bal.db".to_string(), - } - } -} -impl MyConfig { - fn get_net_config(&self, param: &str) -> &NetConfig{ - match param { - "regtest" => &self.regtest, - "testnet" => &self.testnet3, - "signet" => &self.signet, - _ => &self.mainnet, - } - } -} - -async fn echo_info( - param: &str, - cfg: &MyConfig, -) -> Result>, hyper::Error> { - info!("echo info!!!{}",param); - let netconfig=MyConfig::get_net_config(cfg,param); - return Ok(Response::new(full("{\"address\":\"".to_owned()+&netconfig.address+"\",\"base_fee\":\""+&netconfig.fixed_fee.to_string()+"\"}"))); -} -async fn echo_search(whole_body: &Bytes, - cfg: &MyConfig, -) -> Result>, hyper::Error> { - info!("echo search!!!"); - let strbody = std::str::from_utf8(&whole_body).unwrap(); - info!("{}",strbody); - trace!("{}",strbody.len()); - - let mut response = Response::new(full("Bad data received".to_owned())); - *response.status_mut() = StatusCode::BAD_REQUEST; - if strbody.len() >0 && strbody.len()<=70 { - let db = sqlite::open(&cfg.db_file).unwrap(); - trace!("qua ci arrivo"); - let mut statement = db.prepare("SELECT * FROM tbl_tx WHERE txid = ?").unwrap(); - statement.bind((1, strbody)).unwrap(); - - while let Ok(State::Row) = statement.next() { - trace!("qua tutto ok"); - let mut response_data = HashMap::new(); - match statement.read::("status") { - Ok(value) => response_data.insert("status", value), - Err(e) => { - error!("Error reading status: {}", e); - break; - //response_data.insert("status", "Error".to_string()) - } - }; - - // Read the transaction (tx) - match statement.read::("tx") { - Ok(value) => response_data.insert("tx", value), - Err(e) => { - error!("Error reading tx: {}", e); - break; - //response_data.insert("tx", "Error".to_string()) - } - }; - - match statement.read::("our_address") { - Ok(value) => response_data.insert("our_address", value), - Err(e) => { - error!("Error reading address: {}", e); - break; - //response_data.insert("tx", "Error".to_string()) - } - }; - - match statement.read::("our_fees") { - Ok(value) => response_data.insert("our_fees", value), - Err(e) => { - error!("Error reading fees: {}", e); - break; - //response_data.insert("tx", "Error".to_string()) - } - }; - - // Read the request id (reqid) - match statement.read::("reqid") { - Ok(value) => response_data.insert("time", value), - Err(e) => { - error!("Error reading reqid: {}", e); - break; - //response_data.insert("time", "Error".to_string()) - } - }; - response = match serde_json::to_string(&response_data){ - Ok(json_data) => Response::new(full(json_data)), - Err(_) => {break;} - }; - - return Ok(response); - } - } - Ok(response) - - -} -async fn echo_push(whole_body: &Bytes, - cfg: &MyConfig, - param: &str, -) -> Result>, hyper::Error> { - //let whole_body = req.collect().await?.to_bytes(); - let strbody = std::str::from_utf8(&whole_body).unwrap(); - let lines = strbody.split("\n"); - - let mut response = Response::new(full("Bad data received".to_owned())); - *response.status_mut() = StatusCode::BAD_REQUEST; - debug!("network: {}", param); - let network = match param{ - "testnet" => Network::Testnet, - "signet" => Network::Signet, - "regtest" => Network::Regtest, - &_ => Network::Bitcoin, - }; - - let req_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards").as_nanos(); - - - let sqltxshead = "INSERT INTO tbl_tx (txid, wtxid, ntxid, tx, locktime, reqid, network, our_address, our_fees)".to_string(); - let mut sqltxs = "".to_string(); - //let mut sqlinps = "INSERT INTO tbl_input (txid, in_txid,in_vout)".to_string(); - //let mut sqlouts = "INSERT INTO tbl_output (txid, script_pubkey, address, amount)\n".to_string(); - let mut union_tx = true; - let mut already_present = false; - //let mut union_inps = true; - //let mut union_outs = true; - let db = sqlite::open(&cfg.db_file).unwrap(); - let netconfig = MyConfig::get_net_config(cfg,param); - for line in lines { - if line.len() == 0{ - trace!("line len is: {}",line.len()); - continue - } - let linea = format!("{req_time}:{line}"); - info!("New Tx: {}", linea); - let raw_tx = match Vec::::from_hex(line) { - Ok(raw_tx) => raw_tx, - Err(err) => { - error!("rawtx error: {}",err); - continue - } - }; - if raw_tx.len() > 0 { - trace!("len: {}",raw_tx.len()); - let tx: Transaction = match consensus::deserialize(&raw_tx){ - Ok(tx) => tx, - Err(err) => {error!("error: unable to parse tx: {}\n{}",line,err);continue} - }; - let txid = tx.txid().to_string(); - trace!("txid: {}",txid); - let mut statement = db.prepare("SELECT * FROM tbl_tx WHERE txid = ?").unwrap(); - statement.bind((1,&txid[..])).unwrap(); - //statement.bind((1,"Bob")).unwrap(); - if let Ok(State::Row) = statement.next() { - trace!("already present"); - already_present=true; - continue; - } - let ntxid = tx.ntxid(); - let wtxid = tx.wtxid(); - let mut found = false; - let locktime = tx.lock_time; - let mut our_fees = 0; - let mut our_address:String = "".to_string(); - //dbg!(netconfig.fixed_fee); - if netconfig.fixed_fee >0 { - for output in tx.output{ - let script_pubkey = output.script_pubkey; - let address = match bitcoin::Address::from_script(script_pubkey.as_script(), network){ - Ok(address) => address.to_string(), - Err(_) => String::new(), - }; - //dbg!(&address); - let amount = output.value; - //dbg!(&amount); - //search wllexecutor output - if address == netconfig.address.to_string() && amount.to_sat() >= netconfig.fixed_fee{ - our_fees = amount.to_sat(); - our_address = netconfig.address.to_string(); - found = true; - trace!("address and fees are correct {}: {}",our_address,our_fees); - break; - }else { - trace!("address and fees not found {}: {}",address,amount.to_sat()); - trace!("address and fees not found {}: {}",netconfig.address.to_string(),netconfig.fixed_fee); - } - } - } else { found = true; } - if found == false{ - error!("willexecutor output not found "); - //return Ok(response) - } else { - if union_tx == false { - sqltxs = format!("{sqltxs} UNION ALL"); - }else{ - union_tx = false; - } - sqltxs = format!("{sqltxs} SELECT '{txid}', '{wtxid}', '{ntxid}', '{line}', '{locktime}', '{req_time}', '{network}','{our_address}',{our_fees}"); - } - - } - else{ - trace!("rawTx len is: {}",raw_tx.len()); - debug!("{}",&sqltxs); - } - } - debug!("SQL: {}",sqltxs); - if sqltxs.len()== 0{ - if already_present == true{ - //dbg!(already_present); - return Ok(Response::new(full("already present"))) - } - } - let _ = db.execute("BEGIN TRANSACTION"); - let sql = format!("{}{}",sqltxshead,sqltxs); - if let Err(err) = db.execute(&sql){ - error!("error executing sql:{} - {}",&sql,err); - let _ = db.execute("ROLLBACK"); - //dbg!(&already_present); - if already_present == true{ - trace!("already_present = True"); - return Ok(Response::new(full("already present"))) - } - - return Ok(response) - } - //if !error { - // if let Err(_) = db.execute(sqlinps){ - // let _ = db.execute("ROLLBACK"); - // error = true - // } - //} - //if !error { - // if let Err(_) = db.execute(sqlouts){ - // let _ = db.execute("ROLLBACK"); - // error = true; - // } - //} - let _ = db.execute("COMMIT"); - Ok(Response::new(full("thx"))) -} -fn create_database(db: Connection){ - info!("database sanity check"); - let _ = db.execute("CREATE TABLE IF NOT EXISTS tbl_tx (txid PRIMARY KEY, date_creation TIMESTAMP DEFAULT CURRENT_TIMESTAMP, date_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP, wtxid, ntxid, tx, locktime integer, network, network_fees, reqid, our_fees, our_address, status integer DEFAULT 0);"); - let _ = db.execute("ALTER TABLE tbl_tx ADD COLUMN push_err TEXT"); - let _ = db.execute("CREATE TABLE IF NOT EXISTS tbl_input (txid, in_txid,in_vout, spend_txidi);"); - let _ = db.execute("CREATE TABLE IF NOT EXISTS tbl_output (txid, script_pubkey, address, amount);"); - - let _ = db.execute("CREATE UNIQUE INDEX idx_tbl_input ON(txid, txid,in_txid,in_vout)"); - -} - - - -fn match_uri<'a>(path: &str, uri: &'a str) -> Option<&'a str> { - let re = Regex::new(path).unwrap(); - if let Some(captures) = re.captures(uri) { - if let Some(param) = captures.name("param") { - return Some(param.as_str()); - } - } - None -} - -/// This is our service handler. It receives a Request, routes on its -/// path, and returns a Future of a Response. - -async fn echo( - req: Request, - cfg: &MyConfig, -) -> Result>, hyper::Error> { - - let mut not_found = Response::new(empty()); - *not_found.status_mut() = StatusCode::NOT_FOUND; - let mut ret: Result>, hyper::Error> = Ok(not_found); - - let uri = req.uri().path().to_string(); - //dbg!(&req); - //dbg!(&uri); - match req.method() { - // Serve some instructions at / - &Method::POST => { - let whole_body = req.collect().await?.to_bytes(); - if let Some(param) = match_uri(r"^?/?(?P[^/]?+)?/pushtxs$",uri.as_str()) { - //let whole_body = collect_body(req,512_000).await?; - ret = echo_push(&whole_body,cfg,param).await; - } - if uri=="/searchtx"{ - //let whole_body = collect_body(req,64).await?; - ret = echo_search(&whole_body,cfg).await; - } - ret - } - &Method::GET => { - if let Some(param) = match_uri(r"^?/?(?P[^/]?+)?/info$",uri.as_str()) { - ret = echo_info(param,cfg).await; - } - ret - } - - // Return the 404 Not Found for other routes. - _ => ret - } -} - -fn empty() -> BoxBody { - Empty::::new() - .map_err(|never| match never {}) - .boxed() -} - -fn full>(chunk: T) -> BoxBody { - Full::new(chunk.into()) - .map_err(|never| match never {}) - .boxed() -} -fn parse_env(cfg: &Arc>){ - let mut cfg_lock = cfg.lock().unwrap(); - match env::var("BAL_SERVER_DB_FILE") { - Ok(value) => { - cfg_lock.db_file = value;}, - Err(_) => {}, - } - match env::var("BAL_SERVER_BIND_ADDRESS") { - Ok(value) => { - cfg_lock.bind_address= value;}, - Err(_) => {}, - } - match env::var("BAL_SERVER_BIND_PORT") { - Ok(value) => { - match value.parse::(){ - Ok(value) =>{ cfg_lock.bind_port = value; }, - Err(_) => {}, - } - } - Err(_) => {}, - } - cfg_lock = parse_env_netconfig(cfg_lock,"regtest"); - cfg_lock = parse_env_netconfig(cfg_lock,"signet"); - cfg_lock = parse_env_netconfig(cfg_lock,"testnet3"); - cfg_lock = parse_env_netconfig(cfg_lock,"testnet"); - drop(parse_env_netconfig(cfg_lock,"bitcoin")); - -} -fn parse_env_netconfig<'a>(mut cfg_lock: MutexGuard<'a, MyConfig>, chain: &'a str) -> MutexGuard<'a, MyConfig>{ - let cfg = match chain{ - "regtest" => &mut cfg_lock.regtest, - "signet" => &mut cfg_lock.signet, - "testnet3" => &mut cfg_lock.testnet3, - "testnet" => &mut cfg_lock.testnet4, - &_ => &mut cfg_lock.mainnet, - }; - match env::var(format!("BAL_SERVER_{}_ADDRESS",chain.to_uppercase())) { - Ok(value) => { cfg.address = value; }, - Err(_) => {}, - } - match env::var(format!("BAL_SERVER_{}_FIXE_FEE",chain.to_uppercase())) { - Ok(value) => { - match value.parse::(){ - Ok(value) =>{ cfg.fixed_fee = value; }, - Err(_) => {}, - } - } - Err(_) => {}, - } - cfg_lock -} -fn get_default_config()-> MyConfig { - - let file = confy::get_configuration_file_path("bal-server",None).expect("Error while getting path"); - info!("Default configuration file path is: {:#?}", file); - confy::load("bal-server",None).expect("cant_load") -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - env_logger::init(); - let cfg: Arc> = match env::var("BAL_SERVER_CONFIG_FILE") { - Ok(value) => { - Arc::new(Mutex::new( - match confy::load_path(value.to_string()){ - Ok(val) => { - info!("The configuration file path is: {:#?}", value); - val - }, - Err(err) => { - error!("{}",err); - get_default_config() - } - } - )) - }, - Err(_) => { - Arc::new(Mutex::new(get_default_config())) - }, - }; - - parse_env(&cfg); - let cfg_lock = cfg.lock().unwrap(); - - let db = sqlite::open(&cfg_lock.db_file).unwrap(); - create_database(db); - - let addr = cfg_lock.bind_address.to_string(); - info!("bind address:{}",addr); - let addr: IpAddr = addr.parse()?; - - let listener = TcpListener::bind((addr,cfg_lock.bind_port)).await?; - info!("Listening on http://{}:{}", addr,cfg_lock.bind_port); - - - loop { - let (stream, _) = listener.accept().await?; - let io = TokioIo::new(stream); - - tokio::task::spawn({ - let cfg = cfg_lock.clone(); - async move { - if let Err(err) = http1::Builder::new() - .serve_connection(io, service_fn(|req: Request| async { - echo(req,&cfg).await - })) - .await - { - error!("Error serving connection: {:?}", err); - } - - } - }); - } -} diff --git a/src/xpub.rs b/src/xpub.rs new file mode 100644 index 0000000..8106428 --- /dev/null +++ b/src/xpub.rs @@ -0,0 +1,113 @@ +use bs58; +use sha2::{Digest, Sha256}; +use bitcoin::bip32::Xpub; +use std::str::FromStr; +use bitcoin::bip32::DerivationPath; +use bitcoin::key::Secp256k1; +use bitcoin::Address; +use bitcoin::ScriptBuf; +use bitcoin::WPubkeyHash; +use bitcoin::Network; +use bitcoin::hashes::Hash; + + +// Mainnet (BIP44/BIP49/BIP84) +enum BS58Prefix{ + Xpub, + //Ypub, + //Zpub, + //Tpub, + //Vpub, + //Upub +} +const XPUB_PREFIX:[u8; 4] = [0x04, 0x88, 0xB2, 0x1E]; // xpub (Legacy P2PKH) +//const YPUB_PREFIX:[u8; 4] = [0x04, 0x9D, 0x7C, 0xB2]; // ypub (Nested SegWit P2SH-P2WPKH) +//const ZPUB_PREFIX:[u8; 4] = [0x04, 0xB2, 0x47, 0x46]; // zpub (Native SegWit P2WPKH) +//const TPUB_PREFIX:[u8; 4] = [0x04, 0x35, 0x87, 0xCF]; // tpub (Testnet Legacy P2PKH) +//const VPUB_PREFIX:[u8; 4] = [0x04, 0x5F, 0x1C, 0xF6]; // vpub (Testnet Nested SegWit) +//const UPUB_PREFIX:[u8; 4] = [0x04, 0x4A, 0x52, 0x62]; // upub (RegTest Nested SegWit) + + + +fn base58check_decode(s: &str) -> Result, String> { + let data = bs58::decode(s).into_vec().map_err(|e| e.to_string())?; + if data.len() < 4 { + return Err("Data troppo corta".to_string()); + } + let (payload, checksum) = data.split_at(data.len() - 4); + let hash = Sha256::digest(&Sha256::digest(payload)); + if hash[0..4] != checksum[..] { + return Err("Checksum invalido".to_string()); + } + Ok(payload.to_vec()) +} + +fn base58check_encode(data: &[u8]) -> String { + let checksum = &Sha256::digest(&Sha256::digest(data))[0..4]; + let full = [data, checksum].concat(); + bs58::encode(full).into_string() +} + +fn convert_to(zpub: &str,prefix: BS58Prefix) -> Result { + + let mut data = base58check_decode(zpub)?; + + if data.len() < 4 { + return Err("Non è una zpub valida.".to_string()); + } + data.splice(0..4, match prefix { + BS58Prefix::Xpub => XPUB_PREFIX, + //BS58Prefix::Ypub => YPUB_PREFIX, + //BS58Prefix::Zpub => ZPUB_PREFIX, + //BS58Prefix::Vpub => VPUB_PREFIX, + //BS58Prefix::Tpub => TPUB_PREFIX, + //BS58Prefix::Upub => UPUB_PREFIX, + }); + + Ok(base58check_encode(&data)) +} +pub fn new_address_from_xpub(zpub: &str, index: i64,network: Network)-> Result<(String,String), Box>{ + let xpub = Xpub::from_str(&convert_to(zpub,BS58Prefix::Xpub)?)?; + let path = format!("m/0/{}",index); + let derivation_path = DerivationPath::from_str(&path.as_str())?; + let secp = Secp256k1::new(); + let derived_xpub = xpub.derive_pub(&secp, &derivation_path)?; + let public_key = derived_xpub.public_key; + let pubkey_bytes = public_key.serialize(); + let witness_program = WPubkeyHash::hash(&pubkey_bytes); + let redeem_script = ScriptBuf::new_p2wpkh(&witness_program); + //let script_pubkey = ScriptBuf::new_p2sh(&redeem_script.script_hash()); + let address = Address::from_script(&redeem_script, network)?; + //let address = Address::from_script(&script_pubkey, network)?; + Ok((address.to_string(),path.to_string())) + +} +/* +fn main() -> Result<(), Box>{ + //let zpub = "xpub6C29v8gxCXREHUzoGNfqqFqZWxTVEmYtmZshuzfSwBKNmfYQxoizRziCkkUUA4WwJZkJs2i7nttRiC6MQG7mxZpouXeYkTZe3U52RyPAeo2"; + //let zpub = "vpub5Ut36m34VebUUjdhYaxJCjSPqk3ZR8bA2MXLmbHRQCycAxy5Q1GFPJspLkJywJjBgQnvU3rmwPKTPp1ELLWeXrve3zBufpZR4MRCCTNHzsn"; + let zpub = "zpub6qdfveGrxBQN3z8paZ88EHpCn5MGXpUoHwQmHhPbj4rPQtUjbWyCHrJFYZGVY7MsmVbDaeu4JYqRqcdLzMx78wZFEWbLrF9FG3gr2MPQC5H"; + match convert_to(zpub,BS58Prefix::Xpub) { + Ok(xpub) => println!("XPUB: {}", xpub), + Err(e) => eprintln!("Errore: {}", e), + } + let xpub = Xpub::from_str(&convert_to(zpub,BS58Prefix::Xpub)?)?; + + let derivation_path = DerivationPath::from_str("m/0/0")?; + let secp = Secp256k1::new(); + let derived_xpub = xpub.derive_pub(&secp, &derivation_path)?; + + let public_key = derived_xpub.public_key; + let pubkey_bytes = public_key.serialize(); + let witness_program = WPubkeyHash::hash(&pubkey_bytes); + let redeem_script = ScriptBuf::new_p2wpkh(&witness_program); + let script_pubkey = ScriptBuf::new_p2sh(&redeem_script.script_hash()); + + // Generate the Bitcoin SegWit (BIP49) address + let network = Network::Bitcoin; + let address = Address::from_script(&redeem_script, network)?; + let address = Address::from_script(&script_pubkey, network)?; + + Ok(()) +} +*/