-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsigner.rs
More file actions
379 lines (335 loc) · 13 KB
/
Copy pathsigner.rs
File metadata and controls
379 lines (335 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use std::sync::Arc;
use alloy::{
dyn_abi::TypedData,
primitives::{Address, B256, hex, keccak256},
sol,
sol_types::{SolCall, SolValue, eip712_domain},
};
use engine_core::{
chain::Chain,
credentials::SigningCredential,
error::{ContractErrorToEngineError, EngineError},
signer::{AccountSigner, EoaSigner, EoaSigningOptions, Erc4337SigningOptions},
};
use serde::Serialize;
use vault_types::enclave::encrypted::eoa::MessageFormat;
use crate::{
account_factory::{AccountFactory, get_account_factory},
smart_account::{DeterminedSmartAccount, SmartAccount, SmartAccountFromSalt},
};
sol! {
#[sol(rpc)]
contract ERC1271Contract {
function isValidSignature(bytes32 hash, bytes signature) external view returns (bytes4 magicValue);
}
}
sol! {
#[sol(rpc)]
contract AccountImplementationContract {
function getMessageHash(bytes32 _hash) external view returns (bytes32);
}
}
sol! {
#[derive(Serialize)]
struct AccountMessage {
bytes message;
}
}
/// ERC-6492 magic suffix
const ERC6492_MAGIC_SUFFIX: [u8; 32] =
hex!("6492649264926492649264926492649264926492649264926492649264926492");
/// Builder for creating SmartAccountSigner with computed address and factory pattern detection
pub struct SmartAccountSignerBuilder<C: Chain> {
eoa_signer: Arc<EoaSigner>,
credentials: SigningCredential,
options: Erc4337SigningOptions,
chain: C,
}
impl<C: Chain + Clone> SmartAccountSignerBuilder<C> {
pub fn new(
eoa_signer: Arc<EoaSigner>,
credentials: SigningCredential,
options: Erc4337SigningOptions,
chain: C,
) -> Self {
Self {
eoa_signer,
credentials,
options,
chain,
}
}
/// Build the signer with computed address and factory pattern detection
pub async fn build(self) -> Result<SmartAccountSigner<C>, EngineError> {
// 1. Parse Account Salt using the helper method
let salt_data = self.options.get_salt_data()?;
// 2. Determine Smart Account
let smart_account = match self.options.smart_account_address {
Some(address) => DeterminedSmartAccount { address },
None => SmartAccountFromSalt {
admin_address: self.options.signer_address,
chain: &self.chain,
factory_address: self.options.entrypoint_details.factory_address,
salt_data: &salt_data,
}
.to_determined_smart_account()
.await
.map_err(|e| EngineError::ValidationError {
message: format!("Failed to determine smart account: {}", e),
})?,
};
let factory = get_account_factory(
&self.chain,
self.options.entrypoint_details.factory_address,
None,
);
let init_calldata = factory.init_calldata(self.options.signer_address, salt_data);
let impl_address = factory.implementation_address().await?;
// Check if factory supports 712 pattern
let supports_712_factory = self
.check_712_factory_support(impl_address)
.await
.unwrap_or(false);
Ok(SmartAccountSigner {
options: self.options,
chain: self.chain,
eoa_signer: self.eoa_signer,
credentials: self.credentials,
smart_account,
supports_712_factory,
init_calldata,
})
}
async fn check_712_factory_support(&self, impl_address: Address) -> Result<bool, EngineError> {
let impl_contract =
AccountImplementationContract::new(impl_address, self.chain.provider().clone());
// Test with a dummy hash
let dummy_hash = B256::ZERO;
match impl_contract.getMessageHash(dummy_hash).call().await {
Ok(response) => Ok(response != B256::ZERO),
Err(_) => Ok(false),
}
}
}
/// Smart Account signer with pre-computed address and factory pattern support
#[derive(Clone)]
pub struct SmartAccountSigner<C: Chain> {
options: Erc4337SigningOptions,
credentials: SigningCredential,
chain: C,
eoa_signer: Arc<EoaSigner>,
smart_account: DeterminedSmartAccount,
init_calldata: Vec<u8>,
supports_712_factory: bool,
}
impl<C: Chain + Clone> SmartAccountSigner<C> {
/// Sign message with 712 factory wrapping if supported
async fn sign_message_with_factory_pattern(
&self,
message: &str,
format: MessageFormat,
) -> Result<String, EngineError> {
if self.supports_712_factory {
// Wrap message in EIP-712 domain for 712 factory pattern
let message_hash = self.hash_message(message, format);
self.sign_712_wrapped_hash(message_hash).await
} else {
// Direct EOA signing
self.eoa_signer
.sign_message(
EoaSigningOptions {
chain_id: Some(self.chain.chain_id()),
from: self.options.signer_address,
},
message,
format,
&self.credentials,
)
.await
}
}
/// Sign typed data with 712 factory wrapping if supported
async fn sign_typed_data_with_factory_pattern(
&self,
typed_data: &TypedData,
) -> Result<String, EngineError> {
// Check if self-verifying contract (e.g., session key operations)
let is_self_verifying = typed_data
.domain
.verifying_contract
.map(|addr| addr == self.smart_account.address)
.unwrap_or(false);
if is_self_verifying {
// Direct EOA signing for self-verifying contracts
return self
.eoa_signer
.sign_typed_data(
EoaSigningOptions {
chain_id: Some(self.chain.chain_id()),
from: self.options.signer_address,
},
typed_data,
&self.credentials,
)
.await;
}
if self.supports_712_factory {
// Wrap typed data hash in EIP-712 domain for 712 factory pattern
let typed_data_hash =
typed_data
.eip712_signing_hash()
.map_err(|_e| EngineError::ValidationError {
message: "Failed to compute typed data hash".to_string(),
})?;
self.sign_712_wrapped_hash(typed_data_hash).await
} else {
// Direct EOA signing
self.eoa_signer
.sign_typed_data(
EoaSigningOptions {
chain_id: Some(self.chain.chain_id()),
from: self.options.signer_address,
},
typed_data,
&self.credentials,
)
.await
}
}
/// Sign hash wrapped in AccountMessage EIP-712 structure for 712 factory pattern
async fn sign_712_wrapped_hash(&self, hash: B256) -> Result<String, EngineError> {
let domain = eip712_domain! {
name: "Account",
version: "1",
chain_id: self.options.chain_id,
verifying_contract: self.smart_account.address,
};
let account_message = AccountMessage {
message: hash.abi_encode().into(),
};
// Get the EIP712 signing hash using alloy's native functionality
let typed_data = TypedData::from_struct(&account_message, Some(domain));
// Sign the hash directly with EOA
self.eoa_signer
.sign_typed_data(
EoaSigningOptions {
chain_id: Some(self.chain.chain_id()),
from: self.options.signer_address,
},
&typed_data,
&self.credentials,
)
.await
}
/// Verify ERC-1271 signature
pub async fn verify_erc1271(&self, hash: B256, signature: &str) -> Result<bool, EngineError> {
let signature_bytes = hex::decode(signature.strip_prefix("0x").unwrap_or(signature))
.map_err(|_| EngineError::ValidationError {
message: "Invalid signature hex".to_string(),
})?;
let contract =
ERC1271Contract::new(self.smart_account.address, self.chain.provider().clone());
match contract
.isValidSignature(hash, signature_bytes.into())
.call()
.await
{
Ok(response) => {
let expected_magic = ERC1271Contract::isValidSignatureCall::SELECTOR;
Ok(response.as_slice() == expected_magic)
}
Err(e) => {
Err(e.to_engine_error(self.chain.chain_id(), Some(self.smart_account.address)))
}
}
}
/// Create ERC-6492 signature for undeployed accounts
async fn create_erc6492_signature(&self, signature: &str) -> Result<String, EngineError> {
let signature_bytes = hex::decode(signature.strip_prefix("0x").unwrap_or(signature))
.map_err(|_| EngineError::ValidationError {
message: "Invalid signature hex".to_string(),
})?;
let mut output_buffer = Vec::new();
// Factory address (20 bytes)
output_buffer.extend_from_slice(self.options.entrypoint_details.factory_address.as_slice());
// Factory calldata length (32 bytes) + calldata
let init_code_len = alloy::primitives::U256::from(self.init_calldata.len());
output_buffer.extend_from_slice(&init_code_len.to_be_bytes::<32>());
output_buffer.extend_from_slice(&self.init_calldata);
// Signature length (32 bytes) + signature
let sig_len = alloy::primitives::U256::from(signature_bytes.len());
output_buffer.extend_from_slice(&sig_len.to_be_bytes::<32>());
output_buffer.extend_from_slice(&signature_bytes);
// Magic suffix
output_buffer.extend_from_slice(&ERC6492_MAGIC_SUFFIX);
Ok(format!("0x{}", hex::encode(output_buffer)))
}
/// Hash message according to format
fn hash_message(&self, message: &str, format: MessageFormat) -> B256 {
match format {
MessageFormat::Text => {
let prefixed =
format!("\x19Ethereum Signed Message:\n{}{}", message.len(), message);
keccak256(prefixed.as_bytes())
}
MessageFormat::Hex => {
let bytes = hex::decode(message.strip_prefix("0x").unwrap_or(message))
.unwrap_or_else(|_| message.as_bytes().to_vec());
keccak256(bytes)
}
}
}
pub async fn sign_message(
&self,
message: &str,
format: MessageFormat,
) -> Result<String, EngineError> {
let is_deployed = self.smart_account.is_deployed(&self.chain).await?;
// Get signature with appropriate factory pattern handling
let signature = self
.sign_message_with_factory_pattern(message, format)
.await?;
if is_deployed {
// Verify ERC-1271 signature for deployed accounts
// let message_hash = self.hash_message(message, format);
// let is_valid = self.verify_erc1271(message_hash, &signature).await?;
// if is_valid {
Ok(signature)
// } else {
// Err(EngineError::ValidationError {
// message: "ERC-1271 signature validation failed".to_string(),
// })
// }
} else {
// Create ERC-6492 signature for undeployed accounts
self.create_erc6492_signature(&signature).await
}
}
pub async fn sign_typed_data(&self, typed_data: &TypedData) -> Result<String, EngineError> {
let is_deployed = self.smart_account.is_deployed(&self.chain).await?;
// Get signature with appropriate factory pattern handling
let signature = self
.sign_typed_data_with_factory_pattern(typed_data)
.await?;
if is_deployed {
// Verify ERC-1271 signature for deployed accounts
// let typed_data_hash =
// typed_data
// .eip712_signing_hash()
// .map_err(|_e| EngineError::ValidationError {
// message: "Failed to compute typed data hash".to_string(),
// })?;
// let is_valid = self.verify_erc1271(typed_data_hash, &signature).await?;
// if is_valid {
Ok(signature)
// } else {
// Err(EngineError::ValidationError {
// message: "ERC-1271 signature validation failed".to_string(),
// })
// }
} else {
// Create ERC-6492 signature for undeployed accounts
self.create_erc6492_signature(&signature).await
}
}
}