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

use anyhow::bail;
use diem_rest_client::{
    diem_api_types::{HexEncodedBytes, ScriptPayload, TransactionPayload},
    Transaction,
};
use diem_sdk::{
    client::SignedTransaction,
    crypto::{ed25519::Ed25519PrivateKey, PrivateKey, SigningKey, Uniform},
    transaction_builder::Currency,
    types::{account_config::XUS_NAME, transaction::authenticator::AuthenticationKey},
};
use forge::{PublicUsageContext, PublicUsageTest, Result, Test};
use tokio::runtime::Runtime;

pub struct ExternalTransactionSigner;

impl Test for ExternalTransactionSigner {
    fn name(&self) -> &'static str {
        "smoke-test::external-transaction-signer"
    }
}

impl PublicUsageTest for ExternalTransactionSigner {
    fn run<'t>(&self, ctx: &mut PublicUsageContext<'t>) -> Result<()> {
        let runtime = Runtime::new().unwrap();
        runtime.block_on(self.async_run(ctx))
    }
}

impl ExternalTransactionSigner {
    async fn async_run(&self, ctx: &mut PublicUsageContext<'_>) -> Result<()> {
        let client = ctx.rest_client();

        // generate key pair
        let private_key = Ed25519PrivateKey::generate(ctx.rng());
        let public_key = private_key.public_key();

        // create transfer parameters
        let sender_auth_key = AuthenticationKey::ed25519(&public_key);
        let sender_address = sender_auth_key.derived_address();
        ctx.create_parent_vasp_account(sender_auth_key).await?;
        ctx.fund(sender_address, 10_000_000).await?;

        let receiver = ctx.random_account();
        ctx.create_parent_vasp_account(receiver.authentication_key())
            .await?;
        ctx.fund(receiver.address(), 1_000_000).await?;

        let amount = 1_000_000;
        let test_gas_unit_price = 1;
        let test_max_gas_amount = 1_000_000;

        // prepare transfer transaction
        let test_sequence_number = client
            .get_account(sender_address)
            .await?
            .into_inner()
            .sequence_number;

        let currency_code = XUS_NAME;

        let unsigned_txn = ctx
            .transaction_factory()
            .with_diem_version(0) // Force Script not ScriptFunctions
            .peer_to_peer(Currency::XUS, receiver.address(), amount)
            .sender(sender_address)
            .sequence_number(test_sequence_number)
            .max_gas_amount(test_max_gas_amount)
            .gas_unit_price(test_gas_unit_price)
            .build();

        assert_eq!(unsigned_txn.sender(), sender_address);

        // sign the transaction with the private key
        let signature = private_key.sign(&unsigned_txn);

        // submit the transaction
        let txn = SignedTransaction::new(unsigned_txn.clone(), public_key, signature);
        client.submit_and_wait(&txn).await?;

        // query the transaction and check it contains the same values as requested
        let txn = client
            .get_account_transactions(sender_address, Some(test_sequence_number), Some(1))
            .await?
            .into_inner()
            .into_iter()
            .next()
            .unwrap();

        match txn {
            Transaction::UserTransaction(user_txn) => {
                assert_eq!(*user_txn.request.sender.inner(), sender_address);
                assert_eq!(user_txn.request.sequence_number.0, test_sequence_number);
                assert_eq!(user_txn.request.gas_unit_price.0, test_gas_unit_price);
                assert_eq!(
                    user_txn.request.gas_currency_code,
                    currency_code.to_string()
                );
                assert_eq!(user_txn.request.max_gas_amount.0, test_max_gas_amount);

                if let TransactionPayload::ScriptPayload(ScriptPayload {
                    code,
                    type_arguments,
                    arguments,
                }) = user_txn.request.payload
                {
                    let expected_code = match unsigned_txn.clone().into_payload() {
                        diem_types::transaction::TransactionPayload::Script(script) => {
                            HexEncodedBytes::from(script.code().to_vec())
                        }
                        _ => bail!("unexpected transaction payload: {:?}", &unsigned_txn),
                    };
                    assert_eq!(code.bytecode, expected_code);
                    assert_eq!(
                        type_arguments
                            .into_iter()
                            .map(|t| t.to_string())
                            .collect::<Vec<String>>(),
                        vec!["0x1::XUS::XUS"]
                    );
                    assert_eq!(
                        arguments
                            .into_iter()
                            .map(|arg| arg.as_str().unwrap().to_owned())
                            .collect::<Vec<String>>(),
                        vec![
                            receiver.address().to_hex_literal(),
                            amount.to_string(),
                            "0x".to_string(),
                            "0x".to_string()
                        ]
                    );
                } else {
                    bail!("unexpected transaction playload")
                }
            }
            _ => bail!("Query should get user transaction"),
        }
        Ok(())
    }
}