Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm buildable packages #7

Closed
wants to merge 13 commits into from
6 changes: 6 additions & 0 deletions .github/workflows/client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ jobs:
with:
command: build
args: --release --all-features
- name: Wasm pack installation
uses: jetli/[email protected]
with:
version: 'latest'
- name: Wasm pack build
run: wasm-pack build --target web
lints:
name: Lints (fmt + clippy)
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/js-helpers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ jobs:
with:
command: build
args: --release --all-features
- name: Wasm pack installation
uses: jetli/[email protected]
with:
version: 'latest'
- name: Wasm pack build
run: wasm-pack build --target web
lints:
name: Lints (fmt + clippy)
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/wallet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ jobs:
with:
command: build
args: --release --all-features
- name: Wasm build
uses: jetli/[email protected]
with:
version: 'latest'
- name: Wasm pack build
run: wasm-pack build --target web
lints:
name: Lints (fmt + clippy)
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ debug = false
debug-assertions = false

[patch.crates-io]
cosmos-sdk-proto = { git = "https://github.com/forbole/cosmos-rust", branch = "main"}
cosmos-sdk-proto = { git = "https://github.com/forbole/cosmos-rust", branch = "grpc-web-clients-generation"}
2 changes: 1 addition & 1 deletion modules/js-helpers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crw-js-helpers"
version = "0.1.0"
version = "0.1.1"
authors = ["bragaz <[email protected]"]
edition = "2018"
description = "Javascript helpers to interact with cosmos-rust-wallet"
Expand Down
25 changes: 25 additions & 0 deletions modules/js-helpers/README.md
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 11 additions & 6 deletions modules/js-helpers/src/signer.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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
Expand Down Expand Up @@ -68,13 +69,16 @@ pub fn import_wallet(mnemonic: &str, derivation_path: &str, hrp: &str) -> Result
.into();

Ok(JsValue::from_serde(&wallet).unwrap())


}

#[cfg(test)]
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() {
Expand All @@ -87,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"
)
}
Expand Down
5 changes: 2 additions & 3 deletions packages/crw-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crw-client"
version = "0.1.0"
version = "0.1.1"
authors = ["bragaz <[email protected]"]
edition = "2018"
description = "Client package of cosmos-rust-wallet to broadcast tx and query data from a cosmos based blockchain"
Expand All @@ -14,10 +14,9 @@ crate-type = ["cdylib", "lib"]
[dependencies]
cosmos-sdk-proto = { version = "0.3.0"}
prost = { version = "0.7.0"}
reqwest = { version = "0.11.0", features = ["blocking", "json"]}
reqwest = { version = "0.11.0", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0.62" }
tonic = { version = "0.4.1"}
crw-types = { path = "../../packages/crw-types", version = "0.1.0" }
wasm-bindgen = { version = "0.2.70", features = ["serde-serialize"]}
wasm-bindgen-futures = { version = "0.4.21"}
Expand Down
30 changes: 7 additions & 23 deletions packages/crw-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! This file contains all the methods to fetch useful data from a cosmos-sdk-based chain

use cosmos_sdk_proto::cosmos::{
auth::v1beta1::{query_client::QueryClient, BaseAccount, QueryAccountRequest},
auth::v1beta1::{query_client::Query, BaseAccount, QueryAccountRequest},
base::abci::v1beta1::TxResponse,
tx::v1beta1::{service_client::ServiceClient, BroadcastMode, BroadcastTxRequest},
tx::v1beta1::{service_client::Service, BroadcastMode, BroadcastTxRequest},
};
use crw_types::error::Error;
use reqwest::{get, StatusCode};
use serde::{Deserialize, Serialize};
use tonic::{codegen::http::Uri, transport::Channel, Request};

#[derive(Clone, Serialize, Deserialize)]
/// Response of /node_info query
Expand Down Expand Up @@ -42,25 +41,17 @@ impl ChainClient {

/// Returns the account data associated with the given address
pub async fn get_account_data(&self, address: String) -> Result<BaseAccount, Error> {
// 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::<Uri>().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 =
Expand All @@ -76,14 +67,8 @@ impl ChainClient {
tx_bytes: Vec<u8>,
broadcast_mode: BroadcastMode,
) -> Result<TxResponse, Error> {
// Create channel connection to the gRPC server
let channel = Channel::builder(self.grpc_addr.parse::<Uri>().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,
Expand All @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion packages/crw-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crw-types"
version = "0.1.0"
version = "0.1.1"
authors = ["bragaz <[email protected]"]
edition = "2018"
description = "Collection of types used by cosmos-rust-wallet packages and modules"
Expand All @@ -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"] }
Expand Down
24 changes: 24 additions & 0 deletions packages/crw-types/src/any_wrapper.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
}

impl From<AnyWrapper> for Any {
fn from(any_wrap: AnyWrapper) -> Any {
Any {
type_url: any_wrap.type_url,
value: any_wrap.value,
}
}
}

impl From<AnyWrapper> for Fee {
fn from(any_wrap: AnyWrapper) -> Fee {
prost::Message::decode(any_wrap.value.as_slice()).unwrap()
}
}
1 change: 1 addition & 0 deletions packages/crw-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod any_wrapper;
pub mod error;
pub mod msg;
16 changes: 0 additions & 16 deletions packages/crw-types/src/msg.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
}

/// Transaction message wrapper
pub struct Msg(pub Any);
Expand All @@ -31,12 +24,3 @@ impl From<Msg> for Any {
msg.0
}
}

impl From<AnyWrapper> for Any {
fn from(any_wrap: AnyWrapper) -> Any {
Any {
type_url: any_wrap.type_url,
value: any_wrap.value,
}
}
}
2 changes: 1 addition & 1 deletion packages/crw-wallet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crw-wallet"
version = "0.1.0"
version = "0.1.1"
authors = ["bragaz <[email protected]"]
edition = "2018"
description = "Wallet package of cosmos-rust-wallet to create a wallet and sign txs of a cosmos-sdk based chain"
Expand Down
3 changes: 1 addition & 2 deletions packages/crw-wallet/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ use prost_types::Any;
use ripemd160::Ripemd160;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::convert::TryFrom;
use std::str::FromStr;
use std::{convert::TryFrom, str::FromStr};

/// Keychain contains a pair of Secp256k1 keys.
pub struct Keychain {
Expand Down