Skip to content

Commit

Permalink
cosmrs: add getrandom feature (#434)
Browse files Browse the repository at this point in the history
Enables support for using CosmRS in environments unsupported by
`getrandom`, like non-JS WASM environments
  • Loading branch information
tony-iqlusion committed Oct 3, 2023
1 parent 25e6b59 commit 817aa47
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cosmrs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
- run: cargo build --target ${{ matrix.target }} --release
- run: cargo build --target ${{ matrix.target }} --no-default-features --release

test:
runs-on: ubuntu-latest
Expand Down
15 changes: 0 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions cosmrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,29 @@ rust-version = "1.72"

[dependencies]
cosmos-sdk-proto = { version = "=0.20.0-pre", default-features = false, path = "../cosmos-sdk-proto" }
ecdsa = { version = "0.16", features = ["std"] }
ecdsa = "0.16"
eyre = "0.6"
k256 = { version = "0.13", features = ["ecdsa", "sha256"] }
rand_core = { version = "0.6", features = ["std"] }
k256 = { version = "0.13", default-features = false, features = ["ecdsa", "sha256"] }
rand_core = { version = "0.6", default-features = false }
serde = { version = "1", features = ["serde_derive"] }
serde_json = "1"
subtle-encoding = { version = "0.5", features = ["bech32-preview"] }
tendermint = { version = "0.34", features = ["secp256k1"] }
thiserror = "1"

# optional dependencies
bip32 = { version = "0.5", optional = true }
bip32 = { version = "0.5", optional = true, default-features = false, features = ["alloc", "secp256k1"] }
tendermint-rpc = { version = "0.34", optional = true, features = ["http-client"] }
tokio = { version = "1", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }

[dev-dependencies]
hex-literal = "0.4"

[features]
default = ["bip32"]
default = ["bip32", "getrandom"]
cosmwasm = ["cosmos-sdk-proto/cosmwasm"]
dev = ["rpc", "tokio"]
getrandom = ["rand_core/getrandom"]
grpc = ["cosmos-sdk-proto/grpc-transport", "grpc-core"]
grpc-core = ["cosmos-sdk-proto/grpc"]
rpc = ["tendermint-rpc"]
Expand Down
11 changes: 9 additions & 2 deletions cosmrs/src/crypto/secp256k1/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use crate::{
ErrorReport, Result,
};
use ecdsa::signature::{Keypair, Signer};
use eyre::eyre;
use k256::ecdsa::VerifyingKey;

#[cfg(feature = "getrandom")]
use rand_core::OsRng;

/// ECDSA/secp256k1 signing key (i.e. private key)
Expand Down Expand Up @@ -35,11 +38,13 @@ impl SigningKey {

/// Initialize from a raw scalar value (big endian).
pub fn from_slice(bytes: &[u8]) -> Result<Self> {
let signing_key = k256::ecdsa::SigningKey::from_slice(bytes)?;
let signing_key =
k256::ecdsa::SigningKey::from_slice(bytes).map_err(|_| eyre!("invalid signing key"))?;
Ok(Self::new(Box::new(signing_key)))
}

/// Generate a random signing key.
#[cfg(feature = "getrandom")]
pub fn random() -> Self {
Self::new(Box::new(k256::ecdsa::SigningKey::random(&mut OsRng)))
}
Expand All @@ -58,7 +63,9 @@ impl SigningKey {

/// Sign the given message, returning a signature.
pub fn sign(&self, msg: &[u8]) -> Result<Signature> {
Ok(self.inner.try_sign(msg)?)
self.inner
.try_sign(msg)
.map_err(|_| eyre!("signing failure"))
}

/// Get the [`PublicKey`] for this [`SigningKey`].
Expand Down
3 changes: 2 additions & 1 deletion cosmrs/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
//! The following example illustrates how to build, sign, and parse
//! a Cosmos SDK transaction:
//!
//! ```
#![cfg_attr(feature = "getrandom", doc = " ```ignore")]
#![cfg_attr(not(feature = "getrandom"), doc = " ```ignore")]
//! # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! use cosmrs::{
//! bank::MsgSend,
Expand Down

0 comments on commit 817aa47

Please sign in to comment.