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
use std::sync::Arc;
use accounts::AccountProvider;
use bytes::Bytes;
use crypto::{publickey::Signature, DEFAULT_MAC};
use ethereum_types::{Address, H256, U256};
use jsonrpc_core::{Error, ErrorCode};
use types::transaction::{
AccessListTx, Action, EIP1559TransactionTx, SignedTransaction, Transaction, TypedTransaction,
TypedTxId,
};
use jsonrpc_core::Result;
use v1::helpers::{errors, FilledTransactionRequest};
use super::{eth_data_hash, SignMessage, SignWith, WithToken};
pub struct Signer {
accounts: Arc<AccountProvider>,
}
impl Signer {
pub fn new(accounts: Arc<AccountProvider>) -> Self {
Signer { accounts }
}
}
impl super::Accounts for Signer {
fn sign_transaction(
&self,
filled: FilledTransactionRequest,
chain_id: Option<u64>,
nonce: U256,
password: SignWith,
) -> Result<WithToken<SignedTransaction>> {
let mut legacy_tx = Transaction {
nonce,
action: filled.to.map_or(Action::Create, Action::Call),
gas: filled.gas,
gas_price: filled.gas_price.unwrap_or_default(),
value: filled.value,
data: filled.data,
};
let t = match TypedTxId::from_U64_option_id(filled.transaction_type) {
Some(TypedTxId::Legacy) => TypedTransaction::Legacy(legacy_tx),
Some(TypedTxId::AccessList) => {
if filled.access_list.is_none() {
return Err(Error::new(ErrorCode::InvalidParams));
}
TypedTransaction::AccessList(AccessListTx::new(
legacy_tx,
filled
.access_list
.unwrap_or_default()
.into_iter()
.map(Into::into)
.collect(),
))
}
Some(TypedTxId::EIP1559Transaction) => {
if let Some(max_fee_per_gas) = filled.max_fee_per_gas {
legacy_tx.gas_price = max_fee_per_gas;
} else {
return Err(Error::new(ErrorCode::InvalidParams));
}
if let Some(max_priority_fee_per_gas) = filled.max_priority_fee_per_gas {
let transaction = AccessListTx::new(
legacy_tx,
filled
.access_list
.unwrap_or_default()
.into_iter()
.map(Into::into)
.collect(),
);
TypedTransaction::EIP1559Transaction(EIP1559TransactionTx {
transaction,
max_priority_fee_per_gas,
})
} else {
return Err(Error::new(ErrorCode::InvalidParams));
}
}
None => return Err(Error::new(ErrorCode::InvalidParams)),
};
let hash = t.signature_hash(chain_id);
let signature = signature(&*self.accounts, filled.from, hash, password)?;
Ok(signature.map(|sig| {
SignedTransaction::new(t.with_signature(sig, chain_id))
.expect("Transaction was signed by AccountsProvider; it never produces invalid signatures; qed")
}))
}
fn sign_message(
&self,
address: Address,
password: SignWith,
hash: SignMessage,
) -> Result<WithToken<Signature>> {
match hash {
SignMessage::Data(data) => {
let hash = eth_data_hash(data);
signature(&self.accounts, address, hash, password)
}
SignMessage::Hash(hash) => signature(&self.accounts, address, hash, password),
}
}
fn decrypt(
&self,
address: Address,
password: SignWith,
data: Bytes,
) -> Result<WithToken<Bytes>> {
match password.clone() {
SignWith::Nothing => self
.accounts
.decrypt(address, None, &DEFAULT_MAC, &data)
.map(WithToken::No),
SignWith::Password(pass) => self
.accounts
.decrypt(address, Some(pass), &DEFAULT_MAC, &data)
.map(WithToken::No),
SignWith::Token(token) => self
.accounts
.decrypt_with_token(address, token, &DEFAULT_MAC, &data)
.map(Into::into),
}
.map_err(|e| match password {
SignWith::Nothing => errors::signing(e),
_ => errors::password(e),
})
}
fn supports_prospective_signing(&self, address: &Address, password: &SignWith) -> bool {
let is_unlocked_permanently = self.accounts.is_unlocked_permanently(address);
let has_password = password.is_password();
is_unlocked_permanently || has_password
}
fn default_account(&self) -> Address {
self.accounts.default_account().ok().unwrap_or_default()
}
fn is_unlocked(&self, address: &Address) -> bool {
self.accounts.is_unlocked(address)
}
}
fn signature(
accounts: &AccountProvider,
address: Address,
hash: H256,
password: SignWith,
) -> Result<WithToken<Signature>> {
match password.clone() {
SignWith::Nothing => accounts.sign(address, None, hash).map(WithToken::No),
SignWith::Password(pass) => accounts.sign(address, Some(pass), hash).map(WithToken::No),
SignWith::Token(token) => accounts
.sign_with_token(address, token, hash)
.map(Into::into),
}
.map_err(|e| match password {
SignWith::Nothing => errors::signing(e),
_ => errors::password(e),
})
}