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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

// Copyright 2021 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use crate::{
    account_address::AccountAddress,
    chain_id::ChainId,
    transaction::{RawTransaction, SignedTransaction, TransactionPayload},
    validator_config::{ConsensusPrivateKey, ConsensusPublicKey},
};
use anyhow::Result;
use chrono::Utc;
use diem_crypto::{test_utils::KeyPair, traits::SigningKey};

pub fn create_unsigned_txn(
    payload: TransactionPayload, sender_address: AccountAddress,
    txn_expiration_duration_secs: i64, /* for compatibility with UTC's
                                        * timestamp. */
    chain_id: ChainId,
) -> RawTransaction {
    RawTransaction::new(
        sender_address,
        payload,
        (Utc::now().timestamp() + txn_expiration_duration_secs) as u64,
        chain_id,
    )
}

pub trait TransactionSigner {
    fn sign_txn(&self, raw_txn: RawTransaction) -> Result<SignedTransaction>;
}

/// Craft a transaction request.
pub fn create_user_txn<T: TransactionSigner + ?Sized>(
    signer: &T, payload: TransactionPayload, sender_address: AccountAddress,
    txn_expiration_duration_secs: i64, /* for compatibility with UTC's
                                        * timestamp. */
    chain_id: ChainId,
) -> Result<SignedTransaction> {
    let raw_txn = create_unsigned_txn(
        payload,
        sender_address,
        txn_expiration_duration_secs,
        chain_id,
    );
    signer.sign_txn(raw_txn)
}

impl TransactionSigner for KeyPair<ConsensusPrivateKey, ConsensusPublicKey> {
    fn sign_txn(&self, raw_txn: RawTransaction) -> Result<SignedTransaction> {
        let signature = self.private_key.sign(&raw_txn);
        Ok(SignedTransaction::new(
            raw_txn,
            self.public_key.clone(),
            signature,
        ))
    }
}