1
1
use alloy_network:: EthereumWallet ;
2
2
use alloy_primitives:: Address ;
3
3
use alloy_signer_local:: PrivateKeySigner ;
4
- use secure_types:: { SecureString , SecureVec , Zeroize } ;
4
+ use secure_types:: { SecureArray , SecureString , Zeroize } ;
5
5
use serde:: { Deserialize , Serialize } ;
6
6
use std:: fmt:: Write ;
7
7
8
8
#[ derive( Clone , Serialize , Deserialize ) ]
9
9
pub struct SecureSigner {
10
10
address : Address ,
11
- vec : SecureVec < u8 > ,
11
+ #[ serde( alias = "vec" ) ]
12
+ data : SecureArray < u8 , 32 > ,
12
13
}
13
14
14
15
impl SecureSigner {
@@ -33,11 +34,13 @@ impl SecureSigner {
33
34
34
35
/// Securely erase the signer's key from memory
35
36
pub fn erase ( & mut self ) {
36
- self . vec . erase ( ) ;
37
+ self . data . erase ( ) ;
37
38
}
38
39
39
40
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 ) )
41
44
}
42
45
43
46
pub fn address ( & self ) -> Address {
@@ -46,8 +49,8 @@ impl SecureSigner {
46
49
47
50
pub fn to_signer ( & self ) -> PrivateKeySigner {
48
51
self
49
- . vec
50
- . slice_scope ( |bytes| PrivateKeySigner :: from_slice ( bytes) . unwrap ( ) )
52
+ . data
53
+ . unlocked_scope ( |bytes| PrivateKeySigner :: from_slice ( bytes) . unwrap ( ) )
51
54
}
52
55
53
56
pub fn to_wallet ( & self ) -> EthereumWallet {
@@ -57,13 +60,13 @@ impl SecureSigner {
57
60
58
61
impl From < PrivateKeySigner > for SecureSigner {
59
62
fn from ( value : PrivateKeySigner ) -> Self {
60
- let bytes = value. to_bytes ( ) . to_vec ( ) ;
61
- let secure_vec = SecureVec :: from_vec ( bytes) . unwrap ( ) ;
62
63
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 }
67
70
}
68
71
}
69
72
@@ -118,14 +121,30 @@ mod tests {
118
121
let _address = secure_signer. to_signer ( ) . address ( ) ;
119
122
}
120
123
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
+
121
133
#[ test]
122
134
fn test_serde ( ) {
123
135
let signer = PrivateKeySigner :: random ( ) ;
124
136
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 ( ) ;
127
137
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
+
129
147
assert_eq ! ( signer. address( ) , signer2. address( ) ) ;
148
+ assert_eq ! ( signer. address( ) , signer3. address( ) ) ;
130
149
}
131
150
}
0 commit comments