Skip to content

Commit 0782ab5

Browse files
update SecureSigner to use a SecureArray instead
1 parent d635498 commit 0782ab5

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ alloy-dyn-abi = {version = "1", features = ["eip712"]}
4040
# sha3 = "0.10.8"
4141

4242
# Make sure zeroize is enabled
43-
generic-array = {version = "=1.2.0", features = ["zeroize"]}
43+
generic-array = {version = "1", features = ["zeroize"]}
4444

4545
# Encryption
4646
ncrypt-me = "0.1"

crates/zeus-eth/crates/utils/src/secure_signer.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use alloy_network::EthereumWallet;
22
use alloy_primitives::Address;
33
use alloy_signer_local::PrivateKeySigner;
4-
use secure_types::{SecureString, SecureVec, Zeroize};
4+
use secure_types::{SecureArray, SecureString, Zeroize};
55
use serde::{Deserialize, Serialize};
66
use std::fmt::Write;
77

88
#[derive(Clone, Serialize, Deserialize)]
99
pub struct SecureSigner {
1010
address: Address,
11-
vec: SecureVec<u8>,
11+
#[serde(alias = "vec")]
12+
data: SecureArray<u8, 32>,
1213
}
1314

1415
impl SecureSigner {
@@ -33,11 +34,13 @@ impl SecureSigner {
3334

3435
/// Securely erase the signer's key from memory
3536
pub fn erase(&mut self) {
36-
self.vec.erase();
37+
self.data.erase();
3738
}
3839

3940
pub fn is_erased(&self) -> bool {
40-
self.vec.len() == 0
41+
self
42+
.data
43+
.unlocked_scope(|slice| slice.iter().all(|byte| *byte == 0))
4144
}
4245

4346
pub fn address(&self) -> Address {
@@ -46,8 +49,8 @@ impl SecureSigner {
4649

4750
pub fn to_signer(&self) -> PrivateKeySigner {
4851
self
49-
.vec
50-
.slice_scope(|bytes| PrivateKeySigner::from_slice(bytes).unwrap())
52+
.data
53+
.unlocked_scope(|bytes| PrivateKeySigner::from_slice(bytes).unwrap())
5154
}
5255

5356
pub fn to_wallet(&self) -> EthereumWallet {
@@ -57,13 +60,13 @@ impl SecureSigner {
5760

5861
impl From<PrivateKeySigner> for SecureSigner {
5962
fn from(value: PrivateKeySigner) -> Self {
60-
let bytes = value.to_bytes().to_vec();
61-
let secure_vec = SecureVec::from_vec(bytes).unwrap();
6263
let address = value.address();
63-
SecureSigner {
64-
address,
65-
vec: secure_vec,
66-
}
64+
let mut key_bytes = value.to_bytes();
65+
let data = SecureArray::new(key_bytes.into()).unwrap();
66+
key_bytes.zeroize();
67+
erase_signer(value);
68+
69+
SecureSigner { address, data }
6770
}
6871
}
6972

@@ -118,14 +121,30 @@ mod tests {
118121
let _address = secure_signer.to_signer().address();
119122
}
120123

124+
#[test]
125+
fn test_is_erased() {
126+
let signer = PrivateKeySigner::random();
127+
let mut secure_signer = SecureSigner::from(signer.clone());
128+
assert!(!secure_signer.is_erased());
129+
secure_signer.erase();
130+
assert!(secure_signer.is_erased());
131+
}
132+
121133
#[test]
122134
fn test_serde() {
123135
let signer = PrivateKeySigner::random();
124136
let secure_signer = SecureSigner::from(signer.clone());
125-
let json = serde_json::to_string(&secure_signer).unwrap();
126-
let deserialized: SecureSigner = serde_json::from_str(&json).unwrap();
127137

128-
let signer2 = deserialized.to_signer();
138+
let json_string = serde_json::to_string(&secure_signer).unwrap();
139+
let json_bytes = serde_json::to_vec(&secure_signer).unwrap();
140+
141+
let deserialized_string: SecureSigner = serde_json::from_str(&json_string).unwrap();
142+
let deserialized_bytes: SecureSigner = serde_json::from_slice(&json_bytes).unwrap();
143+
144+
let signer2 = deserialized_string.to_signer();
145+
let signer3 = deserialized_bytes.to_signer();
146+
129147
assert_eq!(signer.address(), signer2.address());
148+
assert_eq!(signer.address(), signer3.address());
130149
}
131150
}

0 commit comments

Comments
 (0)