Skip to content

Commit

Permalink
refactor: remove bincode and serde (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito authored Oct 1, 2023
1 parent 6c14588 commit 72bba16
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 126 deletions.
72 changes: 1 addition & 71 deletions Cargo.lock

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

4 changes: 0 additions & 4 deletions crates/identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ path = "../utils"

[dependencies.secp256k1]
version = "~0.27.0"

[dependencies.serde]
version = "~1.0.188"
features = ["derive"]
3 changes: 1 addition & 2 deletions crates/identity/src/account/account.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use secp256k1::{PublicKey, Secp256k1, SecretKey};
use serde::{Deserialize, Serialize};

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

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Account<T> {
pub address: String,
pub public_key: Vec<u8>,
Expand Down
4 changes: 0 additions & 4 deletions crates/keychain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@ path = "../utils"
[dependencies.vault]
package = "walleth-vault"
path = "../vault"

[dependencies.serde]
version = "~1.0.188"
features = ["derive"]
4 changes: 0 additions & 4 deletions crates/keychain/hdkey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@ features = ["std"]

[dependencies.secp256k1]
version = "~0.27.0"

[dependencies.serde]
version = "~1.0.188"
features = ["derive"]
3 changes: 1 addition & 2 deletions crates/keychain/hdkey/src/hdkey.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bip32::XPrv;
use secp256k1::{PublicKey, Secp256k1, SecretKey};
use serde::{Deserialize, Serialize};

use crate::{
utils::{generate_seed_bytes, get_derivation_path, parse_mnemonic},
Expand All @@ -11,7 +10,7 @@ use identity::{
Account, AccountDeriver, GenericIdentity, IdentityError, Initializable, MultiKeyPair,
};

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug)]
pub struct HDKey {
seed: Vec<u8>,
}
Expand Down
6 changes: 2 additions & 4 deletions crates/keychain/src/keychain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use hdkey::HDKey;
use serde::{Deserialize, Serialize};

use super::KeychainError;
use hdkey::HDKey;
use identity::{Account, IdentityError, Initializable, MultiKeyPair};
use utils::{Controller, Observable};
use vault::{Vault, VaultError};
Expand All @@ -14,7 +12,7 @@ where
MultiKeyPair(Vault<M>),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug)]
pub struct KeychainState {
/// The accounts in the keychain
/// This is a list of public accounts
Expand Down
5 changes: 2 additions & 3 deletions crates/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ version = "~0.4.3"
[dependencies.sha3]
version = "~0.10.8"

[dependencies.serde]
version = "~1.0.188"
features = ["derive"]
[dependencies.secp256k1]
version = "~0.27.0"
4 changes: 0 additions & 4 deletions crates/vault/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,3 @@ package = "walleth-vault-safe"

[dependencies.secp256k1]
version = "~0.27.0"

[dependencies.serde]
version = "~1.0.188"
features = ["derive"]
7 changes: 0 additions & 7 deletions crates/vault/safe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ keywords = [
"signing",
]

[dependencies.bincode]
version = "~1.3.3"

[dependencies.chacha20poly1305]
version = "~0.9.0"

Expand All @@ -28,9 +25,5 @@ version = "~0.12.2"
[dependencies.rand_core]
version = "~0.6.4"

[dependencies.serde]
version = "~1.0.188"
features = ["derive"]

[dependencies.sha3]
version = "~0.10.8"
8 changes: 4 additions & 4 deletions crates/vault/safe/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::fmt::{Display, Formatter, Result};

pub enum SafeError {
Serialization,
Deserialization,
Serialization(String),
Deserialization(String),
}

impl Display for SafeError {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
SafeError::Serialization => write!(f, "Unable to serialize safe"),
SafeError::Deserialization => write!(f, "Unable to deserialize safe"),
SafeError::Serialization(message) => write!(f, "Unable to serialize safe > {}", message),
SafeError::Deserialization(message) => write!(f, "Unable to deserialize safe > {}", message),
}
}
}
39 changes: 29 additions & 10 deletions crates/vault/safe/src/safe.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{ChaCha20Poly1305Cipher, CipherKey, SafeError};

/// A safe is a container for encrypted data.
Expand All @@ -10,7 +8,7 @@ use crate::{ChaCha20Poly1305Cipher, CipherKey, SafeError};
///
/// The encrypted bytes are encrypted and can be used
/// to store sensitive information.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone)]
pub struct Safe<T> {
pub metadata: T,
encrypted_bytes: Box<[u8]>,
Expand Down Expand Up @@ -40,31 +38,52 @@ impl<T> Safe<T> {
}
}

impl<T> Safe<T>
impl<T> From<Safe<T>> for Vec<u8>
where
T: Serialize,
T: TryFrom<Vec<u8>> + Into<Vec<u8>>,
{
/// Serialize `Safe` to bytes
pub fn to_bytes(&self) -> Result<Vec<u8>, SafeError> {
bincode::serialize(&self).or(Err(SafeError::Serialization))
fn from(safe: Safe<T>) -> Vec<u8> {
let mut bytes: Vec<u8> = vec![];
let metadata_bytes = safe.metadata.into();

bytes.append(&mut vec![u8::try_from(metadata_bytes.len()).unwrap()]);
bytes.append(&mut metadata_bytes.into());
bytes.append(&mut safe.encrypted_bytes.into());
bytes.append(&mut safe.nonce.to_vec());

bytes
}
}

impl<T> TryFrom<Vec<u8>> for Safe<T>
where
T: DeserializeOwned,
T: TryFrom<Vec<u8>> + Into<Vec<u8>>,
{
type Error = SafeError;

/// Deserialize `Safe` from bytes
fn try_from(bytes: Vec<u8>) -> Result<Self, SafeError> {
bincode::deserialize(&bytes).or(Err(SafeError::Deserialization))
let metadata_len = bytes[0];
let metadata = T::try_from(bytes[1..metadata_len as usize + 1].to_vec()).or(Err(
SafeError::Deserialization("error deserializing metadata".to_string()),
))?;
let encrypted_bytes = bytes[metadata_len as usize + 1..bytes.len() - 24].to_vec();
let nonce = bytes[bytes.len() - 24..bytes.len()].to_vec();

Ok(Safe {
metadata,
encrypted_bytes: encrypted_bytes.into_boxed_slice(),
nonce: nonce.try_into().or(Err(SafeError::Deserialization(
"unexpected bytes length".to_string(),
)))?,
})
}
}

impl<T> PartialEq for Safe<T>
where
T: PartialEq,
T: PartialEq + TryFrom<Vec<u8>> + Into<Vec<u8>>,
{
fn eq(&self, other: &Self) -> bool {
self.metadata == other.metadata
Expand Down
12 changes: 6 additions & 6 deletions crates/vault/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub enum VaultError {
VaultRestoreFromBytes(String),
SafeCreation,
SafeDecrypt,
SafeExport,
SafeRestore,
SafeExport(String),
SafeRestore(String),
}

impl Display for VaultError {
Expand All @@ -37,8 +37,8 @@ impl Display for VaultError {
}
Self::SafeCreation => write!(f, "Safe creation error"),
Self::SafeDecrypt => write!(f, "Safe decryption error"),
Self::SafeExport => write!(f, "Safe export error"),
Self::SafeRestore => write!(f, "Safe restore error"),
Self::SafeExport(message) => write!(f, "Safe export error > {}", message),
Self::SafeRestore(message) => write!(f, "Safe restore error > {}", message),
Self::IdentityError(error) => write!(f, "{}", error),
}
}
Expand All @@ -59,8 +59,8 @@ impl From<SignerError> for VaultError {
impl From<SafeError> for VaultError {
fn from(error: SafeError) -> Self {
match error {
SafeError::Serialization => Self::SafeExport,
SafeError::Deserialization => Self::SafeRestore,
SafeError::Serialization(message) => Self::SafeExport(message),
SafeError::Deserialization(message) => Self::SafeRestore(message),
}
}
}
Expand Down
Loading

0 comments on commit 72bba16

Please sign in to comment.