Skip to content

Commit

Permalink
wazm
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Nov 27, 2023
1 parent 3878e88 commit d0d8c40
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,19 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
target: wasm32-unknown-unknown
- uses: taiki-e/install-action@cargo-hack
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- name: cargo hack
run: cargo hack check --target wasm32-unknown-unknown
run: |
cargo hack check --workspace --target wasm32-unknown-unknown \
--exclude alloy-signer \
--exclude alloy-signer-aws \
--exclude alloy-signer-ledger \
--exclude alloy-signer-trezor
feature-checks:
runs-on: ubuntu-latest
Expand Down
7 changes: 5 additions & 2 deletions crates/signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ alloy-primitives.workspace = true

elliptic-curve.workspace = true
k256.workspace = true
rand.workspace = true
thiserror.workspace = true
async-trait.workspace = true

Expand All @@ -32,6 +31,9 @@ eth-keystore = { version = "0.5.0", default-features = false, optional = true }
coins-bip32 = { version = "0.8.7", default-features = false, optional = true }
coins-bip39 = { version = "0.8.7", default-features = false, optional = true }

# rand
rand = { workspace = true, optional = true }

# yubi
yubihsm = { version = "0.42", features = ["secp256k1", "http", "usb"], optional = true }

Expand All @@ -49,6 +51,7 @@ coins-bip39 = { version = "0.8.7", default-features = false, features = ["englis
[features]
eip712 = ["dep:alloy-sol-types"]

keystore = ["dep:eth-keystore"]
keystore = ["dep:eth-keystore", "rand"]
mnemonic = ["dep:coins-bip32", "dep:coins-bip39"]
rand = ["dep:rand"]
yubihsm = ["dep:yubihsm"]
19 changes: 10 additions & 9 deletions crates/signer/src/wallet/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use crate::{utils::secret_key_to_address, Wallet, WalletError};
use coins_bip32::path::DerivationPath;
use coins_bip39::{Mnemonic, Wordlist};
use k256::ecdsa::SigningKey;
use rand::Rng;
use std::{fs::File, io::Write, marker::PhantomData, path::PathBuf, str::FromStr};
use std::{marker::PhantomData, path::PathBuf};
use thiserror::Error;

#[cfg(feature = "rand")]
use rand::Rng;

const DEFAULT_DERIVATION_PATH_PREFIX: &str = "m/44'/60'/0'/0/";
const DEFAULT_DERIVATION_PATH: &str = "m/44'/60'/0'/0/0";

Expand Down Expand Up @@ -87,12 +89,11 @@ impl<W: Wordlist> MnemonicBuilder<W> {
///
/// # Examples
///
/// ```
/// ```no_run
/// use alloy_signer::{coins_bip39::English, MnemonicBuilder};
/// # async fn foo() -> Result<(), Box<dyn std::error::Error>> {
///
/// let mut rng = rand::thread_rng();
/// let wallet = MnemonicBuilder::<English>::default().word_count(24).build_random(&mut rng)?;
/// # async fn foo() -> Result<(), Box<dyn std::error::Error>> {
/// let wallet = MnemonicBuilder::<English>::default().word_count(24).build()?;
///
/// # Ok(())
/// # }
Expand All @@ -110,7 +111,7 @@ impl<W: Wordlist> MnemonicBuilder<W> {

/// Sets the derivation path of the child key to be derived.
pub fn derivation_path<T: AsRef<str>>(mut self, path: T) -> Result<Self, WalletError> {
self.derivation_path = DerivationPath::from_str(path.as_ref())?;
self.derivation_path = path.as_ref().parse()?;
Ok(self)
}

Expand Down Expand Up @@ -139,6 +140,7 @@ impl<W: Wordlist> MnemonicBuilder<W> {

/// Builds a `LocalWallet` using the parameters set in the mnemonic builder and constructing
/// the phrase using the provided random number generator.
#[cfg(feature = "rand")]
pub fn build_random<R: Rng>(&self, rng: &mut R) -> Result<Wallet<SigningKey>, WalletError> {
let mnemonic = match &self.phrase {
None => Mnemonic::<W>::new_with_count(rng, self.word_count)?,
Expand All @@ -148,8 +150,7 @@ impl<W: Wordlist> MnemonicBuilder<W> {

// Write the mnemonic phrase to storage if a directory has been provided.
if let Some(dir) = &self.write_to {
let mut file = File::create(dir.as_path().join(wallet.address.to_string()))?;
file.write_all(mnemonic.to_phrase().as_bytes())?;
std::fs::write(dir.join(wallet.address.to_string()), mnemonic.to_phrase().as_bytes())?;
}

Ok(wallet)
Expand Down
10 changes: 5 additions & 5 deletions crates/signer/src/wallet/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use k256::{
ecdsa::{self, SigningKey},
FieldBytes, SecretKey as K256SecretKey,
};
use rand::{CryptoRng, Rng};
use std::str::FromStr;
use thiserror::Error;

#[cfg(feature = "rand")]
use rand::{CryptoRng, Rng};

#[cfg(feature = "keystore")]
use {elliptic_curve::rand_core, eth_keystore::KeystoreError, std::path::Path};

Expand Down Expand Up @@ -61,12 +63,14 @@ impl Wallet<SigningKey> {

/// Creates a new random keypair seeded with [`rand::thread_rng()`].
#[inline]
#[cfg(feature = "rand")]
pub fn random() -> Self {
Self::random_with(&mut rand::thread_rng())
}

/// Creates a new random keypair seeded with the provided RNG.
#[inline]
#[cfg(feature = "rand")]
pub fn random_with<R: Rng + CryptoRng>(rng: &mut R) -> Self {
Self::new_pk(SigningKey::random(rng))
}
Expand All @@ -84,7 +88,6 @@ impl Wallet<SigningKey> {
/// provided directory. Returns a tuple (Wallet, String) of the wallet instance for the
/// keystore with its random UUID. Accepts an optional name for the keystore file. If `None`,
/// the keystore is stored as the stringified UUID.
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub fn new_keystore<P, R, S>(
dir: P,
Expand All @@ -102,7 +105,6 @@ impl Wallet<SigningKey> {
}

/// Decrypts an encrypted JSON from the provided path to construct a Wallet instance
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub fn decrypt_keystore<P, S>(keypath: P, password: S) -> Result<Self, WalletError>
where
Expand All @@ -117,7 +119,6 @@ impl Wallet<SigningKey> {
/// provided directory. Returns a tuple (Wallet, String) of the wallet instance for the
/// keystore with its random UUID. Accepts an optional name for the keystore file. If `None`,
/// the keystore is stored as the stringified UUID.
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub fn encrypt_keystore<P, R, B, S>(
keypath: P,
Expand Down Expand Up @@ -160,7 +161,6 @@ impl FromStr for Wallet<SigningKey> {
}

#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))]
mod tests {
use super::*;
use crate::{LocalWallet, Signer};
Expand Down

0 comments on commit d0d8c40

Please sign in to comment.