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
use crate::{
account_address::AccountAddress,
chain_id::ChainId,
transaction::{RawTransaction, SignedTransaction, TransactionPayload},
};
use anyhow::Result;
use chrono::Utc;
use diem_crypto::{ed25519::*, test_utils::KeyPair, traits::SigningKey};
pub fn create_unsigned_txn(
payload: TransactionPayload,
sender_address: AccountAddress,
sender_sequence_number: u64,
max_gas_amount: u64,
gas_unit_price: u64,
gas_currency_code: String,
txn_expiration_duration_secs: i64, chain_id: ChainId,
) -> RawTransaction {
RawTransaction::new(
sender_address,
sender_sequence_number,
payload,
max_gas_amount,
gas_unit_price,
gas_currency_code,
(Utc::now().timestamp() + txn_expiration_duration_secs) as u64,
chain_id,
)
}
pub trait TransactionSigner {
fn sign_txn(&self, raw_txn: RawTransaction) -> Result<SignedTransaction>;
}
pub fn create_user_txn<T: TransactionSigner + ?Sized>(
signer: &T,
payload: TransactionPayload,
sender_address: AccountAddress,
sender_sequence_number: u64,
max_gas_amount: u64,
gas_unit_price: u64,
gas_currency_code: String,
txn_expiration_duration_secs: i64, chain_id: ChainId,
) -> Result<SignedTransaction> {
let raw_txn = create_unsigned_txn(
payload,
sender_address,
sender_sequence_number,
max_gas_amount,
gas_unit_price,
gas_currency_code,
txn_expiration_duration_secs,
chain_id,
);
signer.sign_txn(raw_txn)
}
impl TransactionSigner for KeyPair<Ed25519PrivateKey, Ed25519PublicKey> {
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,
))
}
}