From 12d4bf0e20bbfb03202453292326b031ed77c125 Mon Sep 17 00:00:00 2001 From: bragaz Date: Tue, 6 Apr 2021 12:11:39 +0200 Subject: [PATCH 01/12] polished imports removed unused code update version of crates --- Cargo.toml | 4 +--- modules/js-helpers/Cargo.toml | 2 +- packages/crw-client/Cargo.toml | 5 ++--- packages/crw-client/src/client.rs | 36 +++++++++---------------------- packages/crw-types/Cargo.toml | 2 +- packages/crw-wallet/Cargo.toml | 2 +- packages/crw-wallet/src/crypto.rs | 6 ++++-- 7 files changed, 20 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c94282c..6c06b6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,4 @@ debug = false debug-assertions = false [patch.crates-io] -cosmos-sdk-proto = { git = "https://github.com/forbole/cosmos-rust", branch = "main"} -# tonic = { git = "https://github.com/bragaz/tonic", rev = "d9e43d4" } -# h2 = { git = "https://github.com/boxdot/h2", branch = "tonic-ws-transport" } +cosmos-sdk-proto = { git = "https://github.com/forbole/cosmos-rust", branch = "grpc-web-clients-generation"} diff --git a/modules/js-helpers/Cargo.toml b/modules/js-helpers/Cargo.toml index 84308f8..56f4605 100644 --- a/modules/js-helpers/Cargo.toml +++ b/modules/js-helpers/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "crw-js-helpers" -version = "0.1.0" +version = "0.1.1" authors = ["bragaz Result { - // TODO move this externally to create it one time only - // Create channel connection to the gRPC server - let channel = Channel::builder(self.grpc_addr.parse::().unwrap()) - .connect() - .await - .map_err(|err| Error::Grpc(err.to_string()))?; - // Create gRPC query auth client from channel - let mut client = QueryClient::new(channel); + let client = Query::new(self.grpc_addr.clone()); // Build a new request - let request = Request::new(QueryAccountRequest { address }); + let request = QueryAccountRequest { address }; // Send request and wait for response let response = client .account(request) .await - .map_err(|err| Error::Grpc(err.to_string()))? - .into_inner(); + .map_err(|err| Error::Grpc(err.to_string()))?; // Decode response body into BaseAccount let base_account: BaseAccount = @@ -76,14 +67,8 @@ impl ChainClient { tx_bytes: Vec, broadcast_mode: BroadcastMode, ) -> Result { - // Create channel connection to the gRPC server - let channel = Channel::builder(self.grpc_addr.parse::().unwrap()) - .connect() - .await - .map_err(|err| Error::Grpc(err.to_string()))?; - // Create gRPC tx client from channel - let mut tx_client = ServiceClient::new(channel); + let tx_client = Service::new(self.grpc_addr.clone()); let mode = match broadcast_mode { BroadcastMode::Unspecified => 0, @@ -92,13 +77,12 @@ impl ChainClient { BroadcastMode::Async => 3, }; - let request = Request::new(BroadcastTxRequest { tx_bytes, mode }); + let request = BroadcastTxRequest { tx_bytes, mode }; let tx_response = tx_client .broadcast_tx(request) .await .map_err(|err| Error::Grpc(err.to_string()))? - .into_inner() .tx_response .unwrap(); diff --git a/packages/crw-types/Cargo.toml b/packages/crw-types/Cargo.toml index 7a07183..2eab7b1 100644 --- a/packages/crw-types/Cargo.toml +++ b/packages/crw-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "crw-types" -version = "0.1.0" +version = "0.1.1" authors = ["bragaz Date: Tue, 6 Apr 2021 12:23:00 +0200 Subject: [PATCH 02/12] added wasm build action --- .github/workflows/client.yml | 4 ++++ .github/workflows/types.yml | 4 ++++ .github/workflows/wallet.yml | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/.github/workflows/client.yml b/.github/workflows/client.yml index 91c2a1c..7e12f4f 100644 --- a/.github/workflows/client.yml +++ b/.github/workflows/client.yml @@ -29,6 +29,10 @@ jobs: with: command: build args: --release --all-features + - name: Wasm build + uses: jetli/wasm-pack-action@v0.3.0 + with: + version: 'latest' lints: name: Lints (fmt + clippy) runs-on: ubuntu-latest diff --git a/.github/workflows/types.yml b/.github/workflows/types.yml index c33cf1e..8eda938 100644 --- a/.github/workflows/types.yml +++ b/.github/workflows/types.yml @@ -29,6 +29,10 @@ jobs: with: command: build args: --release --all-features + - name: Wasm build + uses: jetli/wasm-pack-action@v0.3.0 + with: + version: 'latest' lints: name: Lints (fmt + clippy) runs-on: ubuntu-latest diff --git a/.github/workflows/wallet.yml b/.github/workflows/wallet.yml index b359361..a49c4e0 100644 --- a/.github/workflows/wallet.yml +++ b/.github/workflows/wallet.yml @@ -29,6 +29,10 @@ jobs: with: command: build args: --release --all-features + - name: Wasm build + uses: jetli/wasm-pack-action@v0.3.0 + with: + version: 'latest' lints: name: Lints (fmt + clippy) runs-on: ubuntu-latest From 993e7b53af26718eaaf0905668e11d4b3c6be20a Mon Sep 17 00:00:00 2001 From: bragaz Date: Tue, 6 Apr 2021 12:30:21 +0200 Subject: [PATCH 03/12] formatted files --- packages/crw-client/src/client.rs | 10 +++++----- packages/crw-wallet/src/crypto.rs | 5 +---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/crw-client/src/client.rs b/packages/crw-client/src/client.rs index 7a4f4ef..33575c5 100644 --- a/packages/crw-client/src/client.rs +++ b/packages/crw-client/src/client.rs @@ -1,13 +1,13 @@ //! This file contains all the methods to fetch useful data from a cosmos-sdk-based chain -use crw_types::error::Error; -use reqwest::{get, StatusCode}; -use serde::{Deserialize, Serialize}; use cosmos_sdk_proto::cosmos::{ - auth::v1beta1::{BaseAccount, QueryAccountRequest, query_client::Query}, - tx::v1beta1::{BroadcastMode, BroadcastTxRequest, service_client::Service}, + auth::v1beta1::{query_client::Query, BaseAccount, QueryAccountRequest}, base::abci::v1beta1::TxResponse, + tx::v1beta1::{service_client::Service, BroadcastMode, BroadcastTxRequest}, }; +use crw_types::error::Error; +use reqwest::{get, StatusCode}; +use serde::{Deserialize, Serialize}; #[derive(Clone, Serialize, Deserialize)] /// Response of /node_info query diff --git a/packages/crw-wallet/src/crypto.rs b/packages/crw-wallet/src/crypto.rs index 3416bfc..4ce1061 100644 --- a/packages/crw-wallet/src/crypto.rs +++ b/packages/crw-wallet/src/crypto.rs @@ -26,10 +26,7 @@ use prost_types::Any; use ripemd160::Ripemd160; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; -use std::{ - convert::TryFrom, - str::FromStr -}; +use std::{convert::TryFrom, str::FromStr}; /// Keychain contains a pair of Secp256k1 keys. pub struct Keychain { From bfce696c05d3b9d29d1641f557127aea597e52a0 Mon Sep 17 00:00:00 2001 From: bragaz Date: Tue, 6 Apr 2021 14:51:34 +0200 Subject: [PATCH 04/12] fixed building errors implement from trait for anyWrapper with Fee --- modules/js-helpers/src/signer.rs | 9 +++++---- packages/crw-types/Cargo.toml | 1 + packages/crw-types/src/any_wrapper.rs | 24 ++++++++++++++++++++++++ packages/crw-types/src/lib.rs | 1 + packages/crw-types/src/msg.rs | 16 ---------------- 5 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 packages/crw-types/src/any_wrapper.rs diff --git a/modules/js-helpers/src/signer.rs b/modules/js-helpers/src/signer.rs index 2266da9..298a76e 100644 --- a/modules/js-helpers/src/signer.rs +++ b/modules/js-helpers/src/signer.rs @@ -1,6 +1,6 @@ use cosmos_sdk_proto::cosmos::tx::v1beta1::{BroadcastMode, Fee}; use crw_client::{client::get_node_info, client::ChainClient}; -use crw_types::msg::{AnyWrapper, Msg}; +use crw_types::{any_wrapper::AnyWrapper, msg::Msg}; use crw_wallet::{crypto::Wallet, crypto::WalletJS}; use prost_types::Any; use wasm_bindgen::prelude::*; @@ -19,9 +19,10 @@ pub async fn sign_and_send_msg( // convert all the js values to actual rust types let wallet_js: WalletJS = js_wallet.into_serde().unwrap(); let wallet = Wallet::from(wallet_js); - let msg_wrapped: AnyWrapper = js_msg.into_serde().unwrap(); - let msg: Msg = Msg(Any::from(msg_wrapped)); - let fees: Fee = js_fees.into_serde().unwrap(); + let wrapped_msg: AnyWrapper = js_msg.into_serde().unwrap(); + let msg: Msg = Msg(Any::from(wrapped_msg)); + let wrapped_fee: AnyWrapper = js_fees.into_serde().unwrap(); + let fees: Fee = Fee::from(wrapped_fee); let response = get_node_info(lcd_addr.to_string()) .await diff --git a/packages/crw-types/Cargo.toml b/packages/crw-types/Cargo.toml index 2eab7b1..f5608e6 100644 --- a/packages/crw-types/Cargo.toml +++ b/packages/crw-types/Cargo.toml @@ -9,6 +9,7 @@ repository = "https://github.com/forbole/cosmos-rust-wallet" keywords = ["blockchain", "cosmos", "cosmos-rust-wallet"] [dependencies] +cosmos-sdk-proto = { version = "0.3.0"} prost = { version = "0.7.0"} prost-types = {version = "0.7"} serde = { version = "1.0", features = ["derive"] } diff --git a/packages/crw-types/src/any_wrapper.rs b/packages/crw-types/src/any_wrapper.rs new file mode 100644 index 0000000..9561e6d --- /dev/null +++ b/packages/crw-types/src/any_wrapper.rs @@ -0,0 +1,24 @@ +use cosmos_sdk_proto::cosmos::tx::v1beta1::Fee; +use prost_types::Any; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct AnyWrapper { + pub type_url: String, + pub value: Vec, +} + +impl From for Any { + fn from(any_wrap: AnyWrapper) -> Any { + Any { + type_url: any_wrap.type_url, + value: any_wrap.value, + } + } +} + +impl From for Fee { + fn from(any_wrap: AnyWrapper) -> Fee { + prost::Message::decode(any_wrap.value.as_slice()).unwrap() + } +} diff --git a/packages/crw-types/src/lib.rs b/packages/crw-types/src/lib.rs index a968ed1..5abbe89 100644 --- a/packages/crw-types/src/lib.rs +++ b/packages/crw-types/src/lib.rs @@ -1,2 +1,3 @@ +pub mod any_wrapper; pub mod error; pub mod msg; diff --git a/packages/crw-types/src/msg.rs b/packages/crw-types/src/msg.rs index ed3e3f7..ba9542b 100644 --- a/packages/crw-types/src/msg.rs +++ b/packages/crw-types/src/msg.rs @@ -1,12 +1,5 @@ //! Transaction Message representation use prost_types::Any; -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize)] -pub struct AnyWrapper { - pub type_url: String, - pub value: Vec, -} /// Transaction message wrapper pub struct Msg(pub Any); @@ -31,12 +24,3 @@ impl From for Any { msg.0 } } - -impl From for Any { - fn from(any_wrap: AnyWrapper) -> Any { - Any { - type_url: any_wrap.type_url, - value: any_wrap.value, - } - } -} From a03d32d5c11de9cf43c28fec2dbd848cbaeae982 Mon Sep 17 00:00:00 2001 From: bragaz Date: Tue, 6 Apr 2021 15:59:44 +0200 Subject: [PATCH 05/12] fixing clippy errors --- modules/js-helpers/src/signer.rs | 10 +++++----- packages/crw-wallet/src/crypto.rs | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/js-helpers/src/signer.rs b/modules/js-helpers/src/signer.rs index 298a76e..d551779 100644 --- a/modules/js-helpers/src/signer.rs +++ b/modules/js-helpers/src/signer.rs @@ -1,7 +1,7 @@ use cosmos_sdk_proto::cosmos::tx::v1beta1::{BroadcastMode, Fee}; use crw_client::{client::get_node_info, client::ChainClient}; use crw_types::{any_wrapper::AnyWrapper, msg::Msg}; -use crw_wallet::{crypto::Wallet, crypto::WalletJS}; +use crw_wallet::{crypto::Wallet, crypto::WalletJs}; use prost_types::Any; use wasm_bindgen::prelude::*; @@ -17,7 +17,7 @@ pub async fn sign_and_send_msg( grpc_addr: String, ) -> Result { // convert all the js values to actual rust types - let wallet_js: WalletJS = js_wallet.into_serde().unwrap(); + let wallet_js: WalletJs = js_wallet.into_serde().unwrap(); let wallet = Wallet::from(wallet_js); let wrapped_msg: AnyWrapper = js_msg.into_serde().unwrap(); let msg: Msg = Msg(Any::from(wrapped_msg)); @@ -63,7 +63,7 @@ pub async fn sign_and_send_msg( #[wasm_bindgen] #[allow(dead_code)] pub fn import_wallet(mnemonic: &str, derivation_path: &str, hrp: &str) -> Result { - let wallet: WalletJS = + let wallet: WalletJs = Wallet::from_mnemonic(mnemonic, derivation_path.to_string(), hrp.to_string()) .map_err(|error| JsValue::from_serde(&error).unwrap())? .into(); @@ -74,7 +74,7 @@ pub fn import_wallet(mnemonic: &str, derivation_path: &str, hrp: &str) -> Result #[cfg(test)] mod test { use crate::signer::import_wallet; - use crw_wallet::crypto::WalletJS; + use crw_wallet::crypto::WalletJs; use wasm_bindgen_test::*; #[wasm_bindgen_test] @@ -85,7 +85,7 @@ mod test { "desmos" ).unwrap(); - let wallet_js: WalletJS = js_wallet.into_serde().unwrap(); + let wallet_js: WalletJs = js_wallet.into_serde().unwrap(); assert_eq!( wallet_js.bech32_address, diff --git a/packages/crw-wallet/src/crypto.rs b/packages/crw-wallet/src/crypto.rs index 4ce1061..62b67ee 100644 --- a/packages/crw-wallet/src/crypto.rs +++ b/packages/crw-wallet/src/crypto.rs @@ -43,7 +43,7 @@ pub struct Wallet { #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] -pub struct WalletJS { +pub struct WalletJs { pub public_key: String, pub private_key: String, pub bech32_address: String, @@ -177,8 +177,8 @@ impl Wallet { /// From trait implementation for Wallet <-> WalletJS /// This trait perform a smooth conversion between these two types -impl From for Wallet { - fn from(wallet_js: WalletJS) -> Self { +impl From for Wallet { + fn from(wallet_js: WalletJs) -> Self { let private_key = ExtendedPrivKey::from_str(wallet_js.private_key.as_str()).unwrap(); let public_key = ExtendedPubKey::from_str(wallet_js.public_key.as_str()).unwrap(); Wallet { @@ -191,9 +191,9 @@ impl From for Wallet { } } -impl From for WalletJS { +impl From for WalletJs { fn from(wallet: Wallet) -> Self { - WalletJS { + WalletJs { public_key: wallet.keychain.ext_public_key.to_string(), private_key: wallet.keychain.ext_private_key.to_string(), bech32_address: wallet.bech32_address, From 75fe0fea624c750ddf81dcf16ff2324f7320dbd9 Mon Sep 17 00:00:00 2001 From: bragaz Date: Tue, 6 Apr 2021 16:36:54 +0200 Subject: [PATCH 06/12] added wasm-pack build to action --- .github/workflows/client.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/client.yml b/.github/workflows/client.yml index 7e12f4f..d214097 100644 --- a/.github/workflows/client.yml +++ b/.github/workflows/client.yml @@ -29,10 +29,12 @@ jobs: with: command: build args: --release --all-features - - name: Wasm build + - name: Wasm pack installation uses: jetli/wasm-pack-action@v0.3.0 with: version: 'latest' + command: wasm-pack build + args: --target web lints: name: Lints (fmt + clippy) runs-on: ubuntu-latest From 5569dfc0317e2429531f37b1d25e07a9e2f480d3 Mon Sep 17 00:00:00 2001 From: bragaz Date: Tue, 6 Apr 2021 16:53:23 +0200 Subject: [PATCH 07/12] added wasm-pack build for crw-client --- .github/workflows/client.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/client.yml b/.github/workflows/client.yml index d214097..c625f54 100644 --- a/.github/workflows/client.yml +++ b/.github/workflows/client.yml @@ -33,8 +33,8 @@ jobs: uses: jetli/wasm-pack-action@v0.3.0 with: version: 'latest' - command: wasm-pack build - args: --target web + - name: Wasm pack build + run: wasm-pack build --target web lints: name: Lints (fmt + clippy) runs-on: ubuntu-latest From 4968f6859fb551c7bca035ec084742e962a26285 Mon Sep 17 00:00:00 2001 From: bragaz Date: Tue, 6 Apr 2021 16:59:09 +0200 Subject: [PATCH 08/12] fixed syntax error on yaml --- .github/workflows/client.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/client.yml b/.github/workflows/client.yml index c625f54..a2c9fe2 100644 --- a/.github/workflows/client.yml +++ b/.github/workflows/client.yml @@ -33,7 +33,7 @@ jobs: uses: jetli/wasm-pack-action@v0.3.0 with: version: 'latest' - - name: Wasm pack build + - name: Wasm pack build run: wasm-pack build --target web lints: name: Lints (fmt + clippy) From 93cac708eb45a2c38ede6430dd8913d5ffe71a09 Mon Sep 17 00:00:00 2001 From: bragaz Date: Tue, 6 Apr 2021 17:42:28 +0200 Subject: [PATCH 09/12] added wasm build to all the remaining packages and module --- .github/workflows/js-helpers.yml | 6 ++++++ .github/workflows/types.yml | 2 ++ .github/workflows/wallet.yml | 2 ++ 3 files changed, 10 insertions(+) diff --git a/.github/workflows/js-helpers.yml b/.github/workflows/js-helpers.yml index dbb382e..0026bbf 100644 --- a/.github/workflows/js-helpers.yml +++ b/.github/workflows/js-helpers.yml @@ -29,6 +29,12 @@ jobs: with: command: build args: --release --all-features + - name: Wasm pack installation + uses: jetli/wasm-pack-action@v0.3.0 + with: + version: 'latest' + - name: Wasm pack build + run: wasm-pack build --target web lints: name: Lints (fmt + clippy) runs-on: ubuntu-latest diff --git a/.github/workflows/types.yml b/.github/workflows/types.yml index 8eda938..47b5cdf 100644 --- a/.github/workflows/types.yml +++ b/.github/workflows/types.yml @@ -33,6 +33,8 @@ jobs: uses: jetli/wasm-pack-action@v0.3.0 with: version: 'latest' + - name: Wasm pack build + run: wasm-pack build --target web lints: name: Lints (fmt + clippy) runs-on: ubuntu-latest diff --git a/.github/workflows/wallet.yml b/.github/workflows/wallet.yml index a49c4e0..38f12ad 100644 --- a/.github/workflows/wallet.yml +++ b/.github/workflows/wallet.yml @@ -33,6 +33,8 @@ jobs: uses: jetli/wasm-pack-action@v0.3.0 with: version: 'latest' + - name: Wasm pack build + run: wasm-pack build --target web lints: name: Lints (fmt + clippy) runs-on: ubuntu-latest From d76b59ed851152147f85e4815755390dbe01ee63 Mon Sep 17 00:00:00 2001 From: bragaz Date: Wed, 7 Apr 2021 09:16:08 +0200 Subject: [PATCH 10/12] remove useless wasm build from crw-types --- .github/workflows/types.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/types.yml b/.github/workflows/types.yml index 47b5cdf..c33cf1e 100644 --- a/.github/workflows/types.yml +++ b/.github/workflows/types.yml @@ -29,12 +29,6 @@ jobs: with: command: build args: --release --all-features - - name: Wasm build - uses: jetli/wasm-pack-action@v0.3.0 - with: - version: 'latest' - - name: Wasm pack build - run: wasm-pack build --target web lints: name: Lints (fmt + clippy) runs-on: ubuntu-latest From 2d7f76827f8d4655be282fbebad15932762b822b Mon Sep 17 00:00:00 2001 From: bragaz Date: Thu, 8 Apr 2021 17:22:38 +0200 Subject: [PATCH 11/12] added readme for wasm tests and wasm tests broswer config --- modules/js-helpers/README.md | 25 +++++++++++++++++++++++++ modules/js-helpers/src/signer.rs | 1 + 2 files changed, 26 insertions(+) create mode 100644 modules/js-helpers/README.md diff --git a/modules/js-helpers/README.md b/modules/js-helpers/README.md new file mode 100644 index 0000000..bff0c44 --- /dev/null +++ b/modules/js-helpers/README.md @@ -0,0 +1,25 @@ +## Testing + +This repository implements a set of small test crates. +Running these tests requieres to [install](https://rustwasm.github.io/wasm-pack/installer/) +`wasm-pack`. + +To run the tests run: + +```bash +wasm-pack test --firefox --headless client-test +``` + +To test in chrome, run: + +```bash +wasm-pack test --chrome --headless client-test +``` + +To test in safari, run: + +```bash +wasm-pack test --safari --headless client-test +``` + +You can omit the `--headless` flag to let the test open a web browser page showing the tests' result. \ No newline at end of file diff --git a/modules/js-helpers/src/signer.rs b/modules/js-helpers/src/signer.rs index d551779..e4de12f 100644 --- a/modules/js-helpers/src/signer.rs +++ b/modules/js-helpers/src/signer.rs @@ -76,6 +76,7 @@ mod test { use crate::signer::import_wallet; use crw_wallet::crypto::WalletJs; use wasm_bindgen_test::*; + wasm_bindgen_test_configure!(run_in_browser); #[wasm_bindgen_test] async fn import_wallet_works() { From 8dd2fe7768a71135c19710e1e3876797bf02ad48 Mon Sep 17 00:00:00 2001 From: bragaz Date: Thu, 8 Apr 2021 17:55:45 +0200 Subject: [PATCH 12/12] trying fixing wasm test --- modules/js-helpers/src/signer.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/js-helpers/src/signer.rs b/modules/js-helpers/src/signer.rs index e4de12f..0b063da 100644 --- a/modules/js-helpers/src/signer.rs +++ b/modules/js-helpers/src/signer.rs @@ -69,6 +69,8 @@ pub fn import_wallet(mnemonic: &str, derivation_path: &str, hrp: &str) -> Result .into(); Ok(JsValue::from_serde(&wallet).unwrap()) + + } #[cfg(test)] @@ -89,11 +91,12 @@ mod test { let wallet_js: WalletJs = js_wallet.into_serde().unwrap(); assert_eq!( - wallet_js.bech32_address, + wallet_js.bech32_address.as_str(), "desmos1k8u92hx3k33a5vgppkyzq6m4frxx7ewnlkyjrh" ); + assert_eq!( - wallet_js.public_key, + wallet_js.public_key.as_str(), "02f5bf794ef934cb419bb9113f3a94c723ec6c2881a8d99eef851fd05b61ad698d" ) }