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
mod hash;
mod mnemonic;
pub use mnemonic::{MnemonicBuilder, MnemonicBuilderError};
mod private_key;
pub use private_key::WalletError;
#[cfg(feature = "yubihsm")]
mod yubi;
use crate::{to_eip155_v, Signer};
use ethers_core::{
k256::{
ecdsa::{recoverable::Signature as RecoverableSignature, signature::DigestSigner},
elliptic_curve::FieldBytes,
Secp256k1,
},
types::{
transaction::{eip2718::TypedTransaction, eip712::Eip712},
Address, Signature, H256, U256,
},
utils::hash_message,
};
use hash::Sha256Proxy;
use async_trait::async_trait;
use std::fmt;
#[derive(Clone)]
pub struct Wallet<D: DigestSigner<Sha256Proxy, RecoverableSignature>> {
pub(crate) signer: D,
pub(crate) address: Address,
pub(crate) chain_id: u64,
}
impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> Wallet<D> {
pub fn new_with_signer(signer: D, address: Address, chain_id: u64) -> Self {
Wallet { signer, address, chain_id }
}
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<D: Sync + Send + DigestSigner<Sha256Proxy, RecoverableSignature>> Signer for Wallet<D> {
type Error = WalletError;
async fn sign_message<S: Send + Sync + AsRef<[u8]>>(
&self,
message: S,
) -> Result<Signature, Self::Error> {
let message = message.as_ref();
let message_hash = hash_message(message);
Ok(self.sign_hash(message_hash))
}
async fn sign_transaction(&self, tx: &TypedTransaction) -> Result<Signature, Self::Error> {
let mut tx_with_chain = tx.clone();
if tx_with_chain.chain_id().is_none() {
tx_with_chain.set_chain_id(self.chain_id);
}
Ok(self.sign_transaction_sync(&tx_with_chain))
}
async fn sign_typed_data<T: Eip712 + Send + Sync>(
&self,
payload: &T,
) -> Result<Signature, Self::Error> {
let encoded =
payload.encode_eip712().map_err(|e| Self::Error::Eip712Error(e.to_string()))?;
Ok(self.sign_hash(H256::from(encoded)))
}
fn address(&self) -> Address {
self.address
}
fn chain_id(&self) -> u64 {
self.chain_id
}
fn with_chain_id<T: Into<u64>>(mut self, chain_id: T) -> Self {
self.chain_id = chain_id.into();
self
}
}
impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> Wallet<D> {
pub fn sign_transaction_sync(&self, tx: &TypedTransaction) -> Signature {
let chain_id = tx.chain_id().map(|id| id.as_u64()).unwrap_or(self.chain_id);
let mut tx = tx.clone();
tx.set_chain_id(chain_id);
let sighash = tx.sighash();
let mut sig = self.sign_hash(sighash);
sig.v = to_eip155_v(sig.v as u8 - 27, chain_id);
sig
}
pub fn sign_hash(&self, hash: H256) -> Signature {
let recoverable_sig: RecoverableSignature =
self.signer.sign_digest(Sha256Proxy::from(hash));
let v = u8::from(recoverable_sig.recovery_id()) as u64 + 27;
let r_bytes: FieldBytes<Secp256k1> = recoverable_sig.r().into();
let s_bytes: FieldBytes<Secp256k1> = recoverable_sig.s().into();
let r = U256::from_big_endian(r_bytes.as_slice());
let s = U256::from_big_endian(s_bytes.as_slice());
Signature { r, s, v }
}
pub fn signer(&self) -> &D {
&self.signer
}
}
impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> fmt::Debug for Wallet<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Wallet")
.field("address", &self.address)
.field("chain_Id", &self.chain_id)
.finish()
}
}