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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// OpenEthereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

//! Utilities and helpers for transaction dispatch.

mod full;
mod prospective_signer;

#[cfg(any(test, feature = "accounts"))]
mod signing;
#[cfg(not(any(test, feature = "accounts")))]
mod signing {
    use super::*;
    use v1::helpers::errors;

    /// Dummy signer implementation
    #[derive(Debug, Clone)]
    pub struct Signer;

    impl Signer {
        /// Create new instance of dummy signer (accept any AccountProvider)
        pub fn new<T>(_ap: T) -> Self {
            Signer
        }
    }

    impl super::Accounts for Signer {
        fn sign_transaction(
            &self,
            _filled: FilledTransactionRequest,
            _chain_id: Option<u64>,
            _nonce: U256,
            _password: SignWith,
        ) -> Result<WithToken<SignedTransaction>> {
            Err(errors::account("Signing unsupported", "See #9997"))
        }

        fn sign_message(
            &self,
            _address: Address,
            _password: SignWith,
            _hash: SignMessage,
        ) -> Result<WithToken<Signature>> {
            Err(errors::account("Signing unsupported", "See #9997"))
        }

        fn decrypt(
            &self,
            _address: Address,
            _password: SignWith,
            _data: Bytes,
        ) -> Result<WithToken<Bytes>> {
            Err(errors::account("Signing unsupported", "See #9997"))
        }

        fn supports_prospective_signing(&self, _address: &Address, _password: &SignWith) -> bool {
            false
        }

        fn default_account(&self) -> Address {
            Default::default()
        }

        fn is_unlocked(&self, _address: &Address) -> bool {
            false
        }
    }
}

pub use self::{full::FullDispatcher, signing::Signer};
pub use v1::helpers::nonce::Reservations;

use std::{fmt::Debug, ops::Deref, sync::Arc};

use bytes::Bytes;
use crypto::publickey::Signature;
use ethcore::{client::BlockChainClient, miner::MinerService};
use ethereum_types::{Address, H256, H520, U256};
use ethkey::Password;
use hash::keccak;
use types::{
    transaction::{PendingTransaction, SignedTransaction},
    BlockNumber,
};

use jsonrpc_core::{
    futures::{future, Future, IntoFuture},
    BoxFuture, Error, Result,
};
use v1::{
    helpers::{ConfirmationPayload, FilledTransactionRequest, TransactionRequest},
    types::{
        Bytes as RpcBytes, ConfirmationPayload as RpcConfirmationPayload, ConfirmationResponse,
        DecryptRequest as RpcDecryptRequest, EIP191SignRequest as RpcSignRequest,
        EthSignRequest as RpcEthSignRequest, RichRawTransaction as RpcRichRawTransaction,
    },
};

/// Has the capability to dispatch, sign, and decrypt.
///
/// Requires a clone implementation, with the implication that it be cheap;
/// usually just bumping a reference count or two.
pub trait Dispatcher: Send + Sync + Clone {
    // TODO: when ATC exist, use zero-cost
    // type Out<T>: IntoFuture<T, Error>

    /// Fill optional fields of a transaction request, fetching gas price but not nonce.
    fn fill_optional_fields(
        &self,
        request: TransactionRequest,
        default_sender: Address,
        force_nonce: bool,
    ) -> BoxFuture<FilledTransactionRequest>;

    /// Sign the given transaction request without dispatching, fetching appropriate nonce.
    fn sign<P>(
        &self,
        filled: FilledTransactionRequest,
        signer: &Arc<dyn Accounts>,
        password: SignWith,
        post_sign: P,
    ) -> BoxFuture<P::Item>
    where
        P: PostSign + 'static,
        <P::Out as futures::future::IntoFuture>::Future: Send;

    /// Converts a `SignedTransaction` into `RichRawTransaction`
    fn enrich(&self, _: SignedTransaction) -> RpcRichRawTransaction;

    /// "Dispatch" a local transaction.
    fn dispatch_transaction(&self, signed_transaction: PendingTransaction) -> Result<H256>;
}

/// Payload to sign
pub enum SignMessage {
    /// Eth-sign kind data (requires prefixing)
    Data(Bytes),
    /// Prefixed data hash
    Hash(H256),
}

/// Abstract transaction signer.
///
/// NOTE This signer is semi-correct, it's a temporary measure to avoid moving too much code.
/// If accounts are ultimately removed all password-dealing endpoints will be wiped out.
pub trait Accounts: Send + Sync {
    /// Sign given filled transaction request for the specified chain_id.
    fn sign_transaction(
        &self,
        filled: FilledTransactionRequest,
        chain_id: Option<u64>,
        nonce: U256,
        password: SignWith,
    ) -> Result<WithToken<SignedTransaction>>;

    /// Sign given message.
    fn sign_message(
        &self,
        address: Address,
        password: SignWith,
        hash: SignMessage,
    ) -> Result<WithToken<Signature>>;

    /// Decrypt given message.
    fn decrypt(
        &self,
        address: Address,
        password: SignWith,
        data: Bytes,
    ) -> Result<WithToken<Bytes>>;

    /// Returns `true` if the accounts can sign multiple times.
    fn supports_prospective_signing(&self, address: &Address, password: &SignWith) -> bool;

    /// Returns default account.
    fn default_account(&self) -> Address;

    /// Returns true if account is unlocked (i.e. can sign without a password)
    fn is_unlocked(&self, address: &Address) -> bool;
}

/// action to execute after signing
/// e.g importing a transaction into the chain
pub trait PostSign: Send {
    /// item that this PostSign returns
    type Item: Send;
    /// incase you need to perform async PostSign actions
    type Out: IntoFuture<Item = Self::Item, Error = Error> + Send;
    /// perform an action with the signed transaction
    fn execute(self, signer: WithToken<SignedTransaction>) -> Self::Out;
}

impl PostSign for () {
    type Item = WithToken<SignedTransaction>;
    type Out = Result<Self::Item>;
    fn execute(self, signed: WithToken<SignedTransaction>) -> Self::Out {
        Ok(signed)
    }
}

impl<F: Send, T: Send> PostSign for F
where
    F: FnOnce(WithToken<SignedTransaction>) -> Result<T>,
{
    type Item = T;
    type Out = Result<Self::Item>;
    fn execute(self, signed: WithToken<SignedTransaction>) -> Self::Out {
        (self)(signed)
    }
}

/// Single-use account token.
pub type AccountToken = Password;

/// Values used to unlock accounts for signing.
#[derive(Clone, PartialEq)]
pub enum SignWith {
    /// Nothing -- implies the account is already unlocked.
    Nothing,
    /// Unlock with password.
    Password(Password),
    /// Unlock with single-use token.
    Token(AccountToken),
}

impl SignWith {
    #[cfg(any(test, feature = "accounts"))]
    fn is_password(&self) -> bool {
        if let SignWith::Password(_) = *self {
            true
        } else {
            false
        }
    }
}

/// A value, potentially accompanied by a signing token.
pub enum WithToken<T> {
    /// No token.
    No(T),
    /// With token.
    Yes(T, AccountToken),
}

impl<T: Debug> Deref for WithToken<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        match *self {
            WithToken::No(ref v) => v,
            WithToken::Yes(ref v, _) => v,
        }
    }
}

impl<T: Debug> WithToken<T> {
    /// Map the value with the given closure, preserving the token.
    pub fn map<S, F>(self, f: F) -> WithToken<S>
    where
        S: Debug,
        F: FnOnce(T) -> S,
    {
        match self {
            WithToken::No(v) => WithToken::No(f(v)),
            WithToken::Yes(v, token) => WithToken::Yes(f(v), token),
        }
    }

    /// Convert into inner value, ignoring possible token.
    pub fn into_value(self) -> T {
        match self {
            WithToken::No(v) => v,
            WithToken::Yes(v, _) => v,
        }
    }

    /// Convert the `WithToken` into a tuple.
    pub fn into_tuple(self) -> (T, Option<AccountToken>) {
        match self {
            WithToken::No(v) => (v, None),
            WithToken::Yes(v, token) => (v, Some(token)),
        }
    }
}

impl<T: Debug> From<(T, AccountToken)> for WithToken<T> {
    fn from(tuple: (T, AccountToken)) -> Self {
        WithToken::Yes(tuple.0, tuple.1)
    }
}

impl<T: Debug> From<(T, Option<AccountToken>)> for WithToken<T> {
    fn from(tuple: (T, Option<AccountToken>)) -> Self {
        match tuple.1 {
            Some(token) => WithToken::Yes(tuple.0, token),
            None => WithToken::No(tuple.0),
        }
    }
}

/// Execute a confirmation payload.
pub fn execute<D: Dispatcher + 'static>(
    dispatcher: D,
    signer: &Arc<dyn Accounts>,
    payload: ConfirmationPayload,
    pass: SignWith,
) -> BoxFuture<WithToken<ConfirmationResponse>> {
    match payload {
        ConfirmationPayload::SendTransaction(request) => {
            let condition = request.condition.clone().map(Into::into);
            let cloned_dispatcher = dispatcher.clone();
            let post_sign = move |with_token_signed: WithToken<SignedTransaction>| {
                let (signed, token) = with_token_signed.into_tuple();
                let signed_transaction = PendingTransaction::new(signed, condition);
                cloned_dispatcher
                    .dispatch_transaction(signed_transaction)
                    .map(|hash| (hash, token))
            };

            Box::new(
                dispatcher
                    .sign(request, &signer, pass, post_sign)
                    .map(|(hash, token)| {
                        WithToken::from((ConfirmationResponse::SendTransaction(hash), token))
                    }),
            )
        }
        ConfirmationPayload::SignTransaction(request) => Box::new(
            dispatcher
                .sign(request, &signer, pass, ())
                .map(move |result| {
                    result
                        .map(move |tx| dispatcher.enrich(tx))
                        .map(ConfirmationResponse::SignTransaction)
                }),
        ),
        ConfirmationPayload::EthSignMessage(address, data) => {
            let res = signer
                .sign_message(address, pass, SignMessage::Data(data))
                .map(|result| {
                    result
                        .map(|s| H520(s.into_electrum()))
                        .map(ConfirmationResponse::Signature)
                });

            Box::new(future::done(res))
        }
        ConfirmationPayload::SignMessage(address, data) => {
            let res = signer
                .sign_message(address, pass, SignMessage::Hash(data))
                .map(|result| {
                    result
                        .map(|rsv| H520(rsv.into_electrum()))
                        .map(ConfirmationResponse::Signature)
                });

            Box::new(future::done(res))
        }
        ConfirmationPayload::Decrypt(address, data) => {
            let res = signer
                .decrypt(address, pass, data)
                .map(|result| result.map(RpcBytes).map(ConfirmationResponse::Decrypt));
            Box::new(future::done(res))
        }
    }
}

/// Returns a eth_sign-compatible hash of data to sign.
/// The data is prepended with special message to prevent
/// malicious DApps from using the function to sign forged transactions.
pub fn eth_data_hash(mut data: Bytes) -> H256 {
    let mut message_data = format!("\x19Ethereum Signed Message:\n{}", data.len()).into_bytes();
    message_data.append(&mut data);
    keccak(message_data)
}

/// Extract the default gas price from a client and miner.
pub fn default_gas_price<C, M>(client: &C, miner: &M, percentile: usize) -> U256
where
    C: BlockChainClient,
    M: MinerService,
{
    client
        .gas_price_corpus(100)
        .percentile(percentile)
        .cloned()
        .unwrap_or_else(|| miner.sensible_gas_price())
}

/// Extract the default priority gas price from a client and miner.
pub fn default_max_priority_fee_per_gas<C, M>(
    client: &C,
    miner: &M,
    percentile: usize,
    eip1559_transition: BlockNumber,
) -> U256
where
    C: BlockChainClient,
    M: MinerService,
{
    client
        .priority_gas_price_corpus(100, eip1559_transition)
        .percentile(percentile)
        .cloned()
        .unwrap_or_else(|| miner.sensible_max_priority_fee())
}

/// Convert RPC confirmation payload to signer confirmation payload.
/// May need to resolve in the future to fetch things like gas price.
pub fn from_rpc<D>(
    payload: RpcConfirmationPayload,
    default_account: Address,
    dispatcher: &D,
) -> BoxFuture<ConfirmationPayload>
where
    D: Dispatcher,
{
    match payload {
        RpcConfirmationPayload::SendTransaction(request) => Box::new(
            dispatcher
                .fill_optional_fields(request.into(), default_account, false)
                .map(ConfirmationPayload::SendTransaction),
        ),
        RpcConfirmationPayload::SignTransaction(request) => Box::new(
            dispatcher
                .fill_optional_fields(request.into(), default_account, false)
                .map(ConfirmationPayload::SignTransaction),
        ),
        RpcConfirmationPayload::Decrypt(RpcDecryptRequest { address, msg }) => Box::new(
            future::ok(ConfirmationPayload::Decrypt(address, msg.into())),
        ),
        RpcConfirmationPayload::EthSignMessage(RpcEthSignRequest { address, data }) => Box::new(
            future::ok(ConfirmationPayload::EthSignMessage(address, data.into())),
        ),
        RpcConfirmationPayload::EIP191SignMessage(RpcSignRequest { address, data }) => {
            Box::new(future::ok(ConfirmationPayload::SignMessage(address, data)))
        }
    }
}