-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
mod.rs
50 lines (44 loc) · 1.45 KB
/
mod.rs
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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
pub mod transfer;
pub mod proto {
include!(concat!(env!("OUT_DIR"), "/proto/mod.rs"));
}
use tw_keypair::ecdsa::secp256k1::PrivateKey;
use tw_proto::InternetComputer::Proto::mod_Transaction::OneOftransaction_oneof as Tx;
use crate::protocol::{identity, principal::Principal, rosetta};
#[derive(Debug)]
pub enum SignTransactionError {
InvalidAmount,
InvalidArguments,
Identity(identity::SigningError),
EncodingArgsFailed,
InvalidToAccountIdentifier,
InvalidEnvelopePair,
}
pub fn sign_transaction(
private_key: PrivateKey,
canister_id: Principal,
transaction: &Tx,
) -> Result<rosetta::SignedTransaction, SignTransactionError> {
match transaction {
Tx::transfer(transfer_args) => transfer::transfer(
private_key,
canister_id,
transfer::TransferArgs {
memo: transfer_args.memo,
amount: transfer_args.amount,
max_fee: None,
to: transfer_args.to_account_identifier.to_string(),
current_timestamp_nanos: transfer_args.current_timestamp_nanos,
permitted_drift: if transfer_args.permitted_drift > 0 {
Some(transfer_args.permitted_drift)
} else {
None
},
},
),
Tx::None => Err(SignTransactionError::InvalidArguments),
}
}