sign_txs/
sign_txs.rs

1use multiversx_sdk::{data::transaction::Transaction, wallet::Wallet};
2use multiversx_sdk_http::{DEVNET_GATEWAY, GatewayHttpProxy};
3
4#[tokio::main]
5async fn main() {
6    let wl = Wallet::from_private_key(
7        "1648ad209d6b157a289884933e3bb30f161ec7113221ec16f87c3578b05830b0",
8    )
9    .unwrap();
10    let addr = wl.to_address();
11    let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
12    let network_config = blockchain.get_network_config().await.unwrap();
13
14    let arg = blockchain
15        .get_default_transaction_arguments(&addr, &network_config)
16        .await
17        .unwrap();
18
19    let mut unsign_tx = Transaction {
20        nonce: arg.nonce,
21        value: "1000000000000000000".to_string(),
22        receiver: addr.to_bech32(&network_config.address_hrp),
23        sender: addr.to_bech32(&network_config.address_hrp),
24        gas_price: arg.gas_price,
25        gas_limit: arg.gas_limit,
26        data: arg.data,
27        signature: None,
28        chain_id: arg.chain_id,
29        version: arg.version,
30        options: arg.options,
31    };
32
33    let mut txs: Vec<Transaction> = vec![];
34
35    let signature = wl.sign_tx(&unsign_tx);
36    unsign_tx.signature = Some(hex::encode(signature));
37    txs.push(unsign_tx.clone());
38
39    unsign_tx.version = 2;
40    unsign_tx.options = 1;
41    unsign_tx.nonce += 1;
42
43    let signature = wl.sign_tx(&unsign_tx);
44    unsign_tx.signature = Some(hex::encode(signature));
45    txs.push(unsign_tx.clone());
46
47    let tx_hash = blockchain.send_transactions(&txs).await.unwrap();
48
49    assert!(!tx_hash.is_empty());
50    println!("tx_hashes {tx_hash:?}");
51}