|
2 | 2 | mod tests { |
3 | 3 | use aptos_crypto::compat::Sha3_256; |
4 | 4 | use aptos_crypto::ed25519::{Ed25519PrivateKey, Ed25519PublicKey}; |
| 5 | + use aptos_crypto::Uniform; |
5 | 6 | use aptos_rust_sdk::client::builder::AptosClientBuilder; |
6 | 7 | use aptos_rust_sdk::client::config::AptosNetwork; |
7 | 8 | use aptos_rust_sdk_types::api_types::address::AccountAddress; |
8 | 9 | use aptos_rust_sdk_types::api_types::chain_id::ChainId; |
9 | 10 | use aptos_rust_sdk_types::api_types::module_id::ModuleId; |
10 | 11 | use aptos_rust_sdk_types::api_types::transaction::{ |
11 | | - EntryFunction, RawTransaction, SignedTransaction, TransactionPayload |
| 12 | + EntryFunction, RawTransaction, SignedTransaction, TransactionPayload, |
12 | 13 | }; |
13 | 14 | use aptos_rust_sdk_types::api_types::transaction_authenticator::{ |
14 | 15 | AccountAuthenticator, AuthenticationKey, TransactionAuthenticator, |
15 | 16 | }; |
16 | 17 | use ed25519_dalek::Digest; |
| 18 | + use std::str::FromStr; |
17 | 19 |
|
18 | 20 | #[tokio::test] |
19 | 21 | async fn submit_transaction() { |
@@ -81,25 +83,122 @@ mod tests { |
81 | 83 | let signature = key.sign_message(&message); |
82 | 84 |
|
83 | 85 | let simulate_transaction = client |
84 | | - .simulate_transaction( |
85 | | - SignedTransaction::new( |
86 | | - raw_txn.clone(), |
| 86 | + .simulate_transaction(SignedTransaction::new( |
| 87 | + raw_txn.clone(), |
87 | 88 | TransactionAuthenticator::single_sender(AccountAuthenticator::no_authenticator()), |
88 | | - ) |
89 | | - ) |
| 89 | + )) |
90 | 90 | .await; |
91 | 91 |
|
92 | 92 | println!("Simulate Transaction: {:?}", simulate_transaction); |
93 | 93 |
|
94 | 94 | let transaction = client |
95 | | - .submit_transaction( |
96 | | - SignedTransaction::new( |
97 | | - raw_txn.clone(), |
98 | | - TransactionAuthenticator::ed25519(Ed25519PublicKey::from(&key), signature), |
99 | | - ) |
100 | | - ) |
| 95 | + .submit_transaction(SignedTransaction::new( |
| 96 | + raw_txn.clone(), |
| 97 | + TransactionAuthenticator::ed25519(Ed25519PublicKey::from(&key), signature), |
| 98 | + )) |
101 | 99 | .await; |
102 | 100 |
|
103 | 101 | println!("Transaction: {:?}", transaction); |
104 | 102 | } |
| 103 | + |
| 104 | + #[tokio::test] |
| 105 | + async fn submit_feepayer_transaction() { |
| 106 | + let builder = AptosClientBuilder::new(AptosNetwork::testnet()); |
| 107 | + let client = builder.build(); |
| 108 | + |
| 109 | + let state = client.get_state().await.unwrap(); |
| 110 | + |
| 111 | + let mut seed = [0u8; 32]; |
| 112 | + let seed_bytes = |
| 113 | + hex::decode("4aeeeb3f286caa91984d4a16d424786c7aa26947050b00e84ab7033f2aab0c2d") |
| 114 | + .unwrap(); // Remove the 0x prefix |
| 115 | + seed[..seed_bytes.len()].copy_from_slice(&seed_bytes); |
| 116 | + |
| 117 | + let fee_payer_key = Ed25519PrivateKey::try_from(seed_bytes.as_slice()).unwrap(); |
| 118 | + let fee_payer_address = |
| 119 | + AuthenticationKey::ed25519(&Ed25519PublicKey::from(&fee_payer_key)).account_address(); |
| 120 | + println!("Feepayer Address: {:?}", fee_payer_address.to_string()); |
| 121 | + |
| 122 | + let txn_sender_key = Ed25519PrivateKey::generate(&mut rand::thread_rng()); |
| 123 | + let txn_sender_address = |
| 124 | + AuthenticationKey::ed25519(&Ed25519PublicKey::from(&txn_sender_key)).account_address(); |
| 125 | + |
| 126 | + let payload = TransactionPayload::EntryFunction(EntryFunction::new( |
| 127 | + ModuleId::new( |
| 128 | + AccountAddress::from_str( |
| 129 | + "0x94bd6fa34dba07f935ea2288ba36d74aa5dda6ae541137844cc2f0af8b6b73f3", |
| 130 | + ) |
| 131 | + .unwrap(), |
| 132 | + "create_object".to_string(), |
| 133 | + ), |
| 134 | + "create".to_string(), |
| 135 | + vec![], |
| 136 | + vec![], |
| 137 | + )); |
| 138 | + |
| 139 | + let max_gas_amount = 1500; |
| 140 | + let gas_unit_price = 100; |
| 141 | + let expiration_timestamp_secs = state.timestamp_usecs / 1000 / 1000 + 60 * 10; |
| 142 | + let chain_id = ChainId::Testnet; |
| 143 | + |
| 144 | + let raw_txn = RawTransaction::new( |
| 145 | + txn_sender_address, |
| 146 | + 0, |
| 147 | + payload, |
| 148 | + max_gas_amount, |
| 149 | + gas_unit_price, |
| 150 | + expiration_timestamp_secs, |
| 151 | + chain_id, |
| 152 | + ); |
| 153 | + |
| 154 | + let mut sha3 = Sha3_256::new(); |
| 155 | + sha3.update("APTOS::RawTransactionWithData".as_bytes()); |
| 156 | + let hash = sha3.finalize().to_vec(); |
| 157 | + let mut bytes = vec![]; |
| 158 | + bcs::serialize_into(&mut bytes, &raw_txn).unwrap(); |
| 159 | + let mut message = vec![]; |
| 160 | + message.extend(hash); |
| 161 | + // FeePayerRawTransaction 1 |
| 162 | + message.extend(bcs::to_bytes(&1u8).unwrap()); |
| 163 | + message.extend(bytes); |
| 164 | + message.extend(bcs::to_bytes::<Vec<AccountAddress>>(&vec![]).unwrap()); |
| 165 | + message.extend(bcs::to_bytes::<AccountAddress>(&fee_payer_address).unwrap()); |
| 166 | + |
| 167 | + let txn_sender_signature = txn_sender_key.sign_message(&message); |
| 168 | + |
| 169 | + let fee_payer_signature = fee_payer_key.sign_message(&message); |
| 170 | + |
| 171 | + let simulate_transaction = client |
| 172 | + .simulate_transaction(SignedTransaction::new( |
| 173 | + raw_txn.clone(), |
| 174 | + TransactionAuthenticator::fee_payer( |
| 175 | + AccountAuthenticator::no_authenticator(), |
| 176 | + vec![], |
| 177 | + vec![], |
| 178 | + fee_payer_address, |
| 179 | + AccountAuthenticator::no_authenticator(), |
| 180 | + ), |
| 181 | + )) |
| 182 | + .await; |
| 183 | + println!("Simulate Transaction: {:?}", simulate_transaction); |
| 184 | + let transaction = client |
| 185 | + .submit_transaction(SignedTransaction::new( |
| 186 | + raw_txn.clone(), |
| 187 | + TransactionAuthenticator::fee_payer( |
| 188 | + AccountAuthenticator::ed25519( |
| 189 | + Ed25519PublicKey::from(&txn_sender_key), |
| 190 | + txn_sender_signature, |
| 191 | + ), |
| 192 | + vec![], |
| 193 | + vec![], |
| 194 | + fee_payer_address, |
| 195 | + AccountAuthenticator::ed25519( |
| 196 | + Ed25519PublicKey::from(&fee_payer_key), |
| 197 | + fee_payer_signature, |
| 198 | + ), |
| 199 | + ), |
| 200 | + )) |
| 201 | + .await; |
| 202 | + println!("Transaction: {:?}", transaction); |
| 203 | + } |
105 | 204 | } |
0 commit comments