Skip to content

Commit

Permalink
refactor: split modules into crates (#13)
Browse files Browse the repository at this point in the history
* refactor: add projects crates

* fix: keychain backup and restore

* fix: restore deserialization issue

* refactor: remove broken examples

* refactor: run clippy

* chore: rm ds_store

* chore: fix gitignore

* refactor: run rustfmt
  • Loading branch information
mikesposito authored Oct 1, 2023
1 parent 5d1eec0 commit 6c14588
Show file tree
Hide file tree
Showing 67 changed files with 1,302 additions and 3,525 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v3

- name: Build
run: cargo build --verbose
run: cargo build --workspace --verbose

lint:
runs-on: ubuntu-latest
Expand All @@ -36,4 +36,4 @@ jobs:
- uses: actions/checkout@v3

- name: Text
run: cargo test
run: cargo test --workspace
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
target
.DS_Store
.vscode
.idea
98 changes: 62 additions & 36 deletions Cargo.lock

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

54 changes: 34 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,42 @@ name = "walleth"
version = "0.1.0"
authors = ["mikesposito"]
license-file = "LICENSE"
readme="README.md"
readme = "README.md"
description = "A (WIP) Rust library for easily create, manage, use and protect Ethereum accounts."
edition = "2021"
repository = "https://github.com/mikesposito/walleth/"
keywords = [
"ethereum",
"wallet",
"library",
"crypto",
"signing",
keywords = ["ethereum", "wallet", "library", "crypto", "signing"]

[workspace]
members = [
"crates/identity",
"crates/keychain",
"crates/keychain/hdkey",
"crates/utils",
"crates/vault",
"crates/vault/safe",
]

[dependencies]
bincode = "1.3.3"
bip32 = "~0.5.1"
chacha20poly1305 = "~0.9.0"
ecdsa = { version = "0.16.8", features = ["arithmetic", "signing"] }
hex = "~0.4.3"
hmac = "~0.12.1"
k256 = "0.13.1"
pbkdf2 = "~0.12.2"
rand_core = { version = "~0.6.0", features = ["std"] }
secp256k1 = "~0.27.0"
serde = { version = "1.0.188", features = ["derive"] }
sha3 = "~0.10.8"
[dependencies.identity]
path = "crates/identity"
package = "walleth-identity"

[dependencies.vault]
path = "crates/vault"
package = "walleth-vault"

[dependencies.safe]
path = "crates/vault/safe"
package = "walleth-vault-safe"

[dependencies.utils]
path = "crates/utils"
package = "walleth-utils"

[dependencies.keychain]
path = "crates/keychain"
package = "walleth-keychain"

[dependencies.hdkey]
path = "crates/keychain/hdkey"
package = "walleth-keychain-hdkey"
25 changes: 25 additions & 0 deletions crates/identity/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "walleth-identity"
version = "0.0.0"
authors = ["mikesposito"]
readme="README.md"
edition = "2021"
repository = "https://github.com/mikesposito/walleth/crates/walleth-identity"
keywords = [
"ethereum",
"wallet",
"library",
"crypto",
"signing",
]

[dependencies.utils]
package = "walleth-utils"
path = "../utils"

[dependencies.secp256k1]
version = "~0.27.0"

[dependencies.serde]
version = "~1.0.188"
features = ["derive"]
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use secp256k1::{PublicKey, Secp256k1, SecretKey};
use serde::{Deserialize, Serialize};

use crate::{
use super::AccountError;
use utils::{
crypto::sha3::keccak256,
hex::{add0x, assert_is_valid_hex_address, encode},
utils::crypto::sha3::keccak256,
AccountError,
};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Account {
pub struct Account<T> {
pub address: String,
pub public_key: Vec<u8>,
pub path: T,
}

impl Account {
impl<T> Account<T> {
/// Create a new `Account` from an extended public key
pub fn from_public_key(public_key: &PublicKey) -> Result<Self, AccountError> {
pub fn from_public_key(public_key: &PublicKey, path: T) -> Result<Self, AccountError> {
let extended_address = encode(&keccak256(&public_key.serialize()));
let address = extended_address[extended_address.len() - 40..].to_string();

Expand All @@ -24,14 +25,17 @@ impl Account {
Ok(Account {
address: add0x(&address).to_owned(),
public_key: public_key.serialize().to_vec(),
path,
})
}

/// Create a new `Account` from a private key
pub fn from_private_key(private_key: SecretKey) -> Result<Self, AccountError> {
pub fn from_private_key(private_key: [u8; 32], path: T) -> Result<Self, AccountError> {
let secp = Secp256k1::new();
let public_key = PublicKey::from_secret_key(&secp, &private_key);
let public_key = SecretKey::from_slice(&private_key)
.or(Err(AccountError::InvalidPrivateKey))?
.public_key(&secp);

Self::from_public_key(&public_key)
Self::from_public_key(&public_key, path)
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::hex::HexError;
use utils::hex::HexError;

#[derive(Debug)]
pub enum AccountError {
InvalidHexAddress,
InvalidKeyLength,
InvalidPrivateKey,
}

impl std::fmt::Display for AccountError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidHexAddress => write!(f, "Invalid hex address"),
Self::InvalidKeyLength => write!(f, "Invalid key length"),
Self::InvalidPrivateKey => write!(f, "Invalid private key"),
}
}
}
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions crates/identity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod account;
pub mod signer;
pub mod traits;

pub use account::{Account, AccountError};
pub use signer::{Signer, SignerError};
pub use traits::*;
28 changes: 28 additions & 0 deletions crates/identity/src/signer/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[derive(Debug)]
pub enum SignerError {
GenericError,
InvalidPrivateKey,
InvalidSignature,
}

impl std::fmt::Display for SignerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidPrivateKey => write!(f, "Invalid private key"),
Self::InvalidSignature => write!(f, "Invalid signature"),
Self::GenericError => write!(f, "Secp256k1 error"),
}
}
}

impl std::error::Error for SignerError {}

impl From<secp256k1::Error> for SignerError {
fn from(error: secp256k1::Error) -> Self {
match error {
secp256k1::Error::InvalidSecretKey => Self::InvalidPrivateKey,
secp256k1::Error::InvalidSignature => Self::InvalidSignature,
_ => Self::GenericError,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub mod signable;
pub use signable::*;

pub mod errors;
pub use errors::SignerError;
pub use errors::*;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use secp256k1::Message;

use crate::utils::crypto::sha3::keccak256;
use utils::crypto::sha3::keccak256;

#[derive(Debug, Clone)]
pub struct Signable {
Expand Down
Loading

0 comments on commit 6c14588

Please sign in to comment.