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
use std::sync::Arc;
use ethereum_types::{Address, H160, H256, H520, U256};
use jsonrpc_core::{
futures::{future, Future},
BoxFuture, Result,
};
use v1::{
helpers::{
deprecated::{self, DeprecationNotice},
dispatch::{self, Dispatcher},
errors,
},
metadata::Metadata,
traits::{EthSigning, ParitySigning},
types::{
Bytes as RpcBytes, ConfirmationPayload as RpcConfirmationPayload,
ConfirmationResponse as RpcConfirmationResponse, Either as RpcEither,
RichRawTransaction as RpcRichRawTransaction, TransactionRequest as RpcTransactionRequest,
},
};
pub struct SigningUnsafeClient<D> {
accounts: Arc<dyn dispatch::Accounts>,
dispatcher: D,
deprecation_notice: DeprecationNotice,
}
impl<D: Dispatcher + 'static> SigningUnsafeClient<D> {
pub fn new(accounts: &Arc<dyn dispatch::Accounts>, dispatcher: D) -> Self {
SigningUnsafeClient {
accounts: accounts.clone(),
dispatcher,
deprecation_notice: Default::default(),
}
}
fn handle(
&self,
payload: RpcConfirmationPayload,
account: Address,
) -> BoxFuture<RpcConfirmationResponse> {
let accounts = self.accounts.clone();
let dis = self.dispatcher.clone();
Box::new(
dispatch::from_rpc(payload, account, &dis)
.and_then(move |payload| {
dispatch::execute(dis, &accounts, payload, dispatch::SignWith::Nothing)
})
.map(dispatch::WithToken::into_value),
)
}
}
impl<D: Dispatcher + 'static> EthSigning for SigningUnsafeClient<D> {
type Metadata = Metadata;
fn sign(&self, _: Metadata, address: H160, data: RpcBytes) -> BoxFuture<H520> {
self.deprecation_notice
.print("eth_sign", deprecated::msgs::ACCOUNTS);
Box::new(
self.handle(
RpcConfirmationPayload::EthSignMessage((address, data).into()),
address,
)
.then(|res| match res {
Ok(RpcConfirmationResponse::Signature(signature)) => Ok(signature),
Err(e) => Err(e),
e => Err(errors::internal("Unexpected result", e)),
}),
)
}
fn send_transaction(&self, _meta: Metadata, request: RpcTransactionRequest) -> BoxFuture<H256> {
self.deprecation_notice
.print("eth_sendTransaction", deprecated::msgs::ACCOUNTS);
Box::new(
self.handle(
RpcConfirmationPayload::SendTransaction(request),
self.accounts.default_account(),
)
.then(|res| match res {
Ok(RpcConfirmationResponse::SendTransaction(hash)) => Ok(hash),
Err(e) => Err(e),
e => Err(errors::internal("Unexpected result", e)),
}),
)
}
fn sign_transaction(
&self,
_meta: Metadata,
request: RpcTransactionRequest,
) -> BoxFuture<RpcRichRawTransaction> {
self.deprecation_notice
.print("eth_signTransaction", deprecated::msgs::ACCOUNTS);
Box::new(
self.handle(
RpcConfirmationPayload::SignTransaction(request),
self.accounts.default_account(),
)
.then(|res| match res {
Ok(RpcConfirmationResponse::SignTransaction(tx)) => Ok(tx),
Err(e) => Err(e),
e => Err(errors::internal("Unexpected result", e)),
}),
)
}
}
impl<D: Dispatcher + 'static> ParitySigning for SigningUnsafeClient<D> {
type Metadata = Metadata;
fn compose_transaction(
&self,
_meta: Metadata,
transaction: RpcTransactionRequest,
) -> BoxFuture<RpcTransactionRequest> {
let accounts = self.accounts.clone();
let default_account = accounts.default_account();
Box::new(
self.dispatcher
.fill_optional_fields(transaction.into(), default_account, true)
.map(Into::into),
)
}
fn decrypt_message(&self, _: Metadata, address: H160, data: RpcBytes) -> BoxFuture<RpcBytes> {
self.deprecation_notice
.print("parity_decryptMessage", deprecated::msgs::ACCOUNTS);
Box::new(
self.handle(
RpcConfirmationPayload::Decrypt((address, data).into()),
address,
)
.then(|res| match res {
Ok(RpcConfirmationResponse::Decrypt(data)) => Ok(data),
Err(e) => Err(e),
e => Err(errors::internal("Unexpected result", e)),
}),
)
}
fn post_sign(
&self,
_: Metadata,
_: H160,
_: RpcBytes,
) -> BoxFuture<RpcEither<U256, RpcConfirmationResponse>> {
Box::new(future::err(errors::signer_disabled()))
}
fn post_transaction(
&self,
_: Metadata,
_: RpcTransactionRequest,
) -> BoxFuture<RpcEither<U256, RpcConfirmationResponse>> {
Box::new(future::err(errors::signer_disabled()))
}
fn check_request(&self, _: U256) -> Result<Option<RpcConfirmationResponse>> {
Err(errors::signer_disabled())
}
}