diff --git a/walk2earn/w3bstream/Cargo.lock b/walk2earn/w3bstream/Cargo.lock index b1a6fca..2faa8c7 100644 --- a/walk2earn/w3bstream/Cargo.lock +++ b/walk2earn/w3bstream/Cargo.lock @@ -11,11 +11,20 @@ dependencies = [ "memchr", ] +[[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 = "anyhow" -version = "1.0.66" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" +checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" [[package]] name = "arrayvec" @@ -23,12 +32,30 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "base16ct" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce" +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] +name = "base64" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" + [[package]] name = "base64ct" version = "1.5.3" @@ -56,6 +83,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bumpalo" +version = "3.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" + [[package]] name = "byte-slice-cast" version = "1.2.1" @@ -68,18 +101,53 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f" +dependencies = [ + "iana-time-zone", + "num-integer", + "num-traits", + "serde", + "winapi", +] + +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + [[package]] name = "const-oid" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "722e23542a15cea1f65d4a1419c4cfd7a26706c70871a13a04238ca3f40f1661" +[[package]] +name = "core-foundation-sys" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" + [[package]] name = "cpufeatures" version = "0.2.5" @@ -117,6 +185,85 @@ dependencies = [ "typenum", ] +[[package]] +name = "cxx" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86d3488e7665a7a483b57e25bdd90d0aeb2bc7608c8d0346acf2ad3f1caf1d62" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48fcaf066a053a41a81dfb14d57d99738b767febb8b735c3016e469fac5da690" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2ef98b8b717a829ca5603af80e1f9e2e48013ab227b68ef37872ef84ee479bf" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "086c685979a698443656e5cf7856c95c642295a38599f12fb1ff76fb28d19892" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "darling" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0808e1bd8671fb44a113a14e13497557533369847788fa2ae912b6ebfce9fa8" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "001d80444f28e193f30c2f293455da62dcf9a6b29918a4253152ae2b1de592cb" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b36230598a2d5de7ec1c6f51f72d8a99a9208daff41de2084d06e3fd3ea56685" +dependencies = [ + "darling_core", + "quote", + "syn", +] + [[package]] name = "der" version = "0.6.0" @@ -236,6 +383,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "funty" version = "2.0.0" @@ -274,6 +427,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hex" version = "0.4.3" @@ -289,6 +448,36 @@ dependencies = [ "digest", ] +[[package]] +name = "iana-time-zone" +version = "0.1.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "impl-codec" version = "0.6.0" @@ -318,12 +507,32 @@ dependencies = [ "syn", ] +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", + "serde", +] + [[package]] name = "itoa" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" +[[package]] +name = "js-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "keccak" version = "0.1.2" @@ -342,12 +551,49 @@ version = "0.2.135" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c" +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + [[package]] name = "memchr" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.15.0" @@ -525,6 +771,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" +[[package]] +name = "scratch" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" + [[package]] name = "sec1" version = "0.3.0" @@ -542,18 +794,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.145" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.145" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" dependencies = [ "proc-macro2", "quote", @@ -562,15 +814,43 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.86" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41feea4228a6f1cd09ec7a3593a682276702cd67b5273544757dae23c096f074" +checksum = "cad406b69c91885b5107daf2c29572f6c8cdb3c66826821e286c533490c0bc76" dependencies = [ "itoa", "ryu", "serde", ] +[[package]] +name = "serde_with" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d904179146de381af4c93d3af6ca4984b3152db687dacb9c3c35e86f39809c" +dependencies = [ + "base64 0.13.1", + "chrono", + "hex", + "indexmap", + "serde", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1966009f3c05f095697c537312f5415d1e3ed31ce0a56942bac4c771c5c335e" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "serdect" version = "0.1.0" @@ -628,6 +908,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "subtle" version = "2.4.1" @@ -636,9 +922,9 @@ checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] name = "syn" -version = "1.0.102" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fcd952facd492f9be3ef0d0b7032a6e442ee9b361d4acc2b1d0c4aaa5f613a1" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" dependencies = [ "proc-macro2", "quote", @@ -651,6 +937,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.37" @@ -671,6 +966,33 @@ dependencies = [ "syn", ] +[[package]] +name = "time" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53250a3b3fed8ff8fd988587d8925d26a83ac3845d9e03b220b37f34c2b8d6c2" +dependencies = [ + "itoa", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" + +[[package]] +name = "time-macros" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a460aeb8de6dcb0f381e1ee05f1cd56fcf5a5f6eb8187ff3d8f0b11078d38b7c" +dependencies = [ + "time-core", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -713,6 +1035,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "version_check" version = "0.9.4" @@ -733,6 +1061,7 @@ dependencies = [ "serde_json", "sha2", "signature", + "ws-sdk", ] [[package]] @@ -741,6 +1070,104 @@ 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.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "ws-sdk" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64d9451bdc2a4670a3460a2dddbf0c145961e0c5f74b4badd6b75c8d5a09be8a" +dependencies = [ + "anyhow", + "base64 0.21.0", + "serde", + "serde_json", + "serde_with", +] + [[package]] name = "wyz" version = "0.5.0" diff --git a/walk2earn/w3bstream/Cargo.toml b/walk2earn/w3bstream/Cargo.toml index 8b24f15..8aa85c1 100644 --- a/walk2earn/w3bstream/Cargo.toml +++ b/walk2earn/w3bstream/Cargo.toml @@ -18,4 +18,5 @@ p256 = { version = "0.11.1", features = ["serde", "ecdsa-core"]} rand_core = "0.6.4" signature = "1.6.4" lazy_static = "1.4.0" -anyhow = "1.0" \ No newline at end of file +anyhow = "1.0" +ws-sdk = "0.1.12" diff --git a/walk2earn/w3bstream/src/lib.rs b/walk2earn/w3bstream/src/lib.rs index 40513e1..ba9d3f6 100644 --- a/walk2earn/w3bstream/src/lib.rs +++ b/walk2earn/w3bstream/src/lib.rs @@ -13,10 +13,10 @@ use p256::{ use serde_json::Value; use sha2::{Digest, Sha256}; use signature::hazmat::PrehashVerifier; +use ws_sdk::{blockchain, database, log, stream}; use types::Record; mod abi; -mod sdk; mod types; lazy_static! { @@ -37,65 +37,71 @@ pub extern "C" fn alloc(size: i32) -> *mut u8 { #[no_mangle] pub extern "C" fn data_handler(event_id: i32) -> i32 { - sdk::log_info( - &format!("data_handler function called with event_id: {}", event_id)); - sdk::log_info( - &format!("DeviceRegistry Contract: {}", CONTRACT_ADDR_DEVICES_REGISTRY)); - sdk::log_info( - &format!("WalkToEarn Contract: {}", CONTRACT_ADDR_WALK_TO_EARN)); - - let data_str = sdk::get_data_as_str(event_id).unwrap(); - sdk::log_info(&format!("event data as string: {}",data_str)); - - let data_u8 = match sdk::get_data(event_id) { - Some(data) => data, + log::log_info(&format!( + "data_handler function called with event_id: {}", + event_id + )); + log::log_info(&format!( + "DeviceRegistry Contract: {}", + CONTRACT_ADDR_DEVICES_REGISTRY + )); + log::log_info(&format!( + "WalkToEarn Contract: {}", + CONTRACT_ADDR_WALK_TO_EARN + )); + + let data_u8 = match stream::get_data(event_id as _) { + Ok(data) => data, _ => { - sdk::log_error("failed to get data"); + log::log_error("failed to get data"); return -1; } }; - + log::log_info(&format!( + "event data as string: {}", + String::from_utf8(data_u8.to_owned()).unwrap() + )); + let input: Value = match serde_json::from_slice(data_u8.as_slice()) { Ok(val) => val, _ => { - sdk::log_error("failed to decode the event"); + log::log_error("failed to decode the event"); return -1; } }; - // sdk::log_info(&format!("input: {:?}", input)); + // log::log_info(&format!("input: {:?}", input)); - sdk::log_info(&format!("Verifying the signature")); + log::log_info(&format!("Verifying the signature")); if !verify_signature(&input) { { - sdk::log_error("failed to verify signature"); + log::log_error("failed to verify signature"); return -1; } } - sdk::log_info(&format!("Signature is correct")); - + log::log_info(&format!("Signature is correct")); let device_id = match input["device_id"].as_str() { Some(id) => id, _ => { - sdk::log_error("failed to get deviceId"); + log::log_error("failed to get deviceId"); return -1; } }; - sdk::log_info(&format!("authorizing device {:?}", device_id)); + log::log_info(&format!("authorizing device {:?}", device_id)); if !auth_device(device_id) { { - sdk::log_error(&format!("failed to auth device(id:{})", device_id)); + log::log_error(&format!("failed to auth device(id:{})", device_id)); return -1; } }; - sdk::log_info(&format!("Device is authorized: Storing data in DB.")); + log::log_info(&format!("Device is authorized: Storing data in DB.")); match sink_data(&input) { Ok(_) => 0, Err(_) => { - sdk::log_error("failed to sink data"); + log::log_error("failed to sink data"); return -1; } } @@ -103,19 +109,22 @@ pub extern "C" fn data_handler(event_id: i32) -> i32 { #[no_mangle] pub extern "C" fn claim_handler(resource_id: i32) -> i32 { - sdk::log_info(&format!("Claim function called with resource_id: {}", resource_id)); + log::log_info(&format!( + "Claim function called with resource_id: {}", + resource_id + )); - let data_u8 = match sdk::get_data(resource_id) { - Some(data) => data, + let data_u8 = match stream::get_data(resource_id as _) { + Ok(data) => data, _ => { - sdk::log_error("failed to get data from event"); + log::log_error("failed to get data from event"); return -1; } }; let input: Value = match serde_json::from_slice(data_u8.as_slice()) { Ok(val) => val, _ => { - sdk::log_error("failed to decode the event"); + log::log_error("failed to decode the event"); return -1; } }; @@ -123,22 +132,25 @@ pub extern "C" fn claim_handler(resource_id: i32) -> i32 { let (req_id, device_id, from, to) = match decode_event_data(&input) { Some(val) => val, _ => { - sdk::log_error("failed to decode event data"); + log::log_error("failed to decode event data"); return -1; } }; - sdk::log_info(&format!("calculating proof of walk for device id:{}), from timestamp: {}, to timestamp: {}", device_id, from, to)); + log::log_info(&format!( + "calculating proof of walk for device id:{}), from timestamp: {}, to timestamp: {}", + device_id, from, to + )); let steps = match calc_steps(&device_id, from, to) { Some(steps) => steps, _ => { - sdk::log_info("failed to calculate steps"); + log::log_info("failed to calculate steps"); return -1; } }; - sdk::log_info(&format!("Steps walked in the time range: {}", steps)); + log::log_info(&format!("Steps walked in the time range: {}", steps)); let data = match match steps { 0 => encode_func_claim_activity_reply_data( @@ -151,16 +163,21 @@ pub extern "C" fn claim_handler(resource_id: i32) -> i32 { } { Ok(val) => val, _ => { - sdk::log_error("failed to encode claim data"); + log::log_error("failed to encode claim data"); return -1; } }; - sdk::log_info(&format!("Sending proof of walk tx {:?}", data)); - match sdk::send_tx(&String::from(CONTRACT_ADDR_WALK_TO_EARN), &String::from("0"), &data) { + log::log_info(&format!("Sending proof of walk tx {:?}", data)); + match blockchain::send_tx( + 4690, + &String::from(CONTRACT_ADDR_WALK_TO_EARN), + &String::from("0"), + &data, + ) { Ok(_) => 0, Err(_) => { - sdk::log_error("send tx failed"); + log::log_error("send tx failed"); return -1; } } @@ -201,8 +218,8 @@ fn auth_device(device_id: &str) -> bool { Ok(data) => data, _ => return false, }; - let ret = match sdk::call_contract(&to, &data) { - Some(val) => val, + let ret = match blockchain::call_contract(4690, &to, &data) { + Ok(val) => val, _ => return false, }; match decode_func_is_authorized_device_data(ret) { @@ -236,9 +253,9 @@ fn decode_func_is_authorized_device_data(data: Vec) -> Result Result<()> { let id = data["device_id"].as_str().unwrap().to_string(); - let mut value: Vec = match sdk::get_db(&id) { - Some(ret) => serde_json::from_slice(ret.as_slice())?, - None => vec![], + let mut value: Vec = match database::kv::get(&id) { + Ok(ret) => serde_json::from_slice(ret.as_slice())?, + _ => vec![], }; value.push(Record { @@ -248,7 +265,7 @@ fn sink_data(data: &Value) -> Result<()> { totalsteps: data["message"]["steps"].as_u64().unwrap(), }); - sdk::set_db(&id, serde_json::to_string(&value)?.into_bytes()) + database::kv::set(&id, serde_json::to_string(&value)?.into_bytes()) } fn decode_event_data(data: &Value) -> Option<(u64, String, u64, u64)> { @@ -298,7 +315,7 @@ fn get_value_in_log_by_name(log: &Log, name: &str) -> Option { } fn calc_steps(device_id: &String, from: u64, to: u64) -> Option { - let ret = sdk::get_db(&device_id)?; + let ret = database::kv::get(&device_id).ok()?; let records: Vec = serde_json::from_slice(ret.as_slice()).ok()?; if records.len() < 2 { return Some(0); @@ -324,7 +341,7 @@ fn encode_func_claim_activity_reply_data( is_success: bool, error: String, ) -> Result { - sdk::log_info(&format!("Encoding claim activity reply with req_id: {req_id}, steps: {steps}, is_success: {is_success}, error: {error}", req_id=req_id, steps=steps, is_success=is_success, error=error)); + log::log_info(&format!("Encoding claim activity reply with req_id: {req_id}, steps: {steps}, is_success: {is_success}, error: {error}", req_id=req_id, steps=steps, is_success=is_success, error=error)); let encoded = WALK_TO_EARN .function("claimActivityReply")? @@ -335,6 +352,6 @@ fn encode_func_claim_activity_reply_data( ethabi::Token::String(error), ])?; let ret = hex::encode(encoded); - // sdk::log_info(&format!("Encoded data: {}", ret)); + // log::log_info(&format!("Encoded data: {}", ret)); Ok(ret) } diff --git a/walk2earn/w3bstream/src/sdk.rs b/walk2earn/w3bstream/src/sdk.rs deleted file mode 100644 index fb1b96e..0000000 --- a/walk2earn/w3bstream/src/sdk.rs +++ /dev/null @@ -1,113 +0,0 @@ -use anyhow::{bail, Result}; - -#[link(wasm_import_module = "env")] -extern "C" { - fn ws_get_data(event_id: i32, return_ptr: *const *mut u8, return_size: *const i32) -> i32; - // fn set_data(event_id: i32, ptr: *const u8, size: i32); - fn ws_log(log_level: i32, ptr: *const u8, size: i32) -> i32; - fn ws_get_db( - key_ptr: *const u8, - key_size: i32, - return_ptr: *const *mut u8, - return_size: *const i32, - ) -> i32; - fn ws_set_db(key_ptr: *const u8, key_size: i32, value_ptr: *const u8, value_size: i32) -> i32; - fn ws_send_tx(ptr: *const u8, size: i32) -> i32; - fn ws_call_contract( - ptr: *const u8, - size: i32, - return_ptr: *const *mut u8, - return_size: *const i32, - ) -> i32; -} - -enum LogLevel { - Trace = 1, - Debug, - Info, - Warn, - Error, -} - -pub fn get_data(resource_id: i32) -> Option> { - let data_ptr = &mut (0 as i32) as *const _ as *const *mut u8; - let data_size = &(0 as i32); - match unsafe { ws_get_data(resource_id, data_ptr, data_size) } { - 0 => Some(unsafe { Vec::from_raw_parts(*data_ptr, *data_size as _, *data_size as _) }), - _ => None, - } -} - -pub fn get_data_as_str(resource_id: i32) -> Option { - let data_ptr = &mut (0 as i32) as *const _ as *const *mut u8; - let data_size = &(0 as i32); - match unsafe { ws_get_data(resource_id, data_ptr, data_size) } { - 0 => Some(unsafe { String::from_raw_parts(*data_ptr, *data_size as _, *data_size as _) }), - _ => None, - } -} - -pub fn call_contract(to: &String, data: &String) -> Option> { - let data_ptr = &mut (0 as i32) as *const _ as *const *mut u8; - let data_size = &(0 as i32); - - let tx = crate::types::Call { - to: to.clone(), - data: data.clone(), - }; - let str = serde_json::to_string(&tx).ok()?; - match unsafe { ws_call_contract(str.as_ptr(), str.len() as _, data_ptr, data_size) } { - 0 => Some(unsafe { Vec::from_raw_parts(*data_ptr, *data_size as _, *data_size as _) }), - _ => None, - } -} - -pub fn log_info(str: &str) { - unsafe { ws_log(LogLevel::Info as _, str.as_bytes().as_ptr(), str.len() as _) }; -} - -pub fn log_error(str: &str) { - unsafe { - ws_log( - LogLevel::Error as _, - str.as_bytes().as_ptr(), - str.len() as _, - ) - }; -} - -pub fn set_db(key: &String, value: Vec) -> Result<()> { - match unsafe { - ws_set_db( - key.as_ptr(), - key.len() as _, - value.as_ptr(), - value.len() as _, - ) - } { - 0 => Ok(()), - _ => bail!("fail to set db"), - } -} - -pub fn get_db(key: &String) -> Option> { - let data_ptr = &mut (0 as i32) as *const _ as *const *mut u8; - let data_size = &(0 as i32); - match unsafe { ws_get_db(key.as_ptr(), key.len() as _, data_ptr, data_size) } { - 0 => Some(unsafe { Vec::from_raw_parts(*data_ptr, *data_size as _, *data_size as _) }), - _ => None, - } -} - -pub fn send_tx(to: &String, value: &String, data: &String) -> Result<()> { - let tx = crate::types::Tx { - to: to.clone(), - value: value.clone(), - data: data.clone(), - }; - let str = serde_json::to_string(&tx)?; - match unsafe { ws_send_tx(str.as_ptr(), str.len() as _) } { - 0 => Ok(()), - _ => bail!("fail to send tx"), - } -} diff --git a/walk2earn/w3bstream/src/types.rs b/walk2earn/w3bstream/src/types.rs index ad44350..6dbf440 100644 --- a/walk2earn/w3bstream/src/types.rs +++ b/walk2earn/w3bstream/src/types.rs @@ -1,18 +1,5 @@ use serde::{Deserialize, Serialize}; -#[derive(Serialize)] -pub struct Tx { - pub to: String, - pub data: String, - pub value: String, -} - -#[derive(Serialize)] -pub struct Call { - pub to: String, - pub data: String, -} - #[derive(Serialize, Deserialize)] pub struct Record { pub device_id: String,