Skip to content

Commit

Permalink
feat(iota-sdk): add sign_and_execute_transaction() fn to example utils (
Browse files Browse the repository at this point in the history
#2101)

* feat(iota-sdk): add sign_and_execute_transaction() fn to example utils

* clippy

* Add missing )

* Update crates/iota-sdk/examples/programmable_transactions_api.rs

Co-authored-by: Thibault Martinez <[email protected]>

* Accept IotaAddress as ref

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez authored Aug 29, 2024
1 parent 8b1c4a0 commit 70d738d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
35 changes: 8 additions & 27 deletions crates/iota-sdk/examples/programmable_transactions_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
//! addresses from the local wallet, and then
//! 1) finds a coin from the active address that has Iota,
//! 2) splits the coin into one coin of 1000 NANOS and the rest,
//! 3 transfers the split coin to second Iota address,
//! 4) signs the transaction,
//! 5) executes it.
//! 3) transfers the split coin to second Iota address,
//! 4) signs and executes the transaction.
//! For some of these actions it prints some output.
//! Finally, at the end of the program it prints the number of coins for the
//! Iota address that received the coin.
Expand All @@ -19,18 +18,11 @@
//! cargo run --example programmable_transactions_api

mod utils;
use iota_config::{iota_config_dir, IOTA_KEYSTORE_FILENAME};
use iota_keys::keystore::{AccountKeystore, FileBasedKeystore};
use iota_sdk::{
rpc_types::IotaTransactionBlockResponseOptions,
types::{
programmable_transaction_builder::ProgrammableTransactionBuilder,
quorum_driver_types::ExecuteTransactionRequestType,
transaction::{Argument, Command, Transaction, TransactionData},
},
use iota_sdk::types::{
programmable_transaction_builder::ProgrammableTransactionBuilder,
transaction::{Argument, Command, TransactionData},
};
use shared_crypto::intent::Intent;
use utils::setup_for_write;
use utils::{setup_for_write, sign_and_execute_transaction};

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
Expand Down Expand Up @@ -78,20 +70,9 @@ async fn main() -> Result<(), anyhow::Error> {
gas_price,
);

// 4) Sign transaction
let keystore = FileBasedKeystore::new(&iota_config_dir()?.join(IOTA_KEYSTORE_FILENAME))?;
let signature = keystore.sign_secure(&sender, &tx_data, Intent::iota_transaction())?;

// 5) Execute the transaction
// 5) Sign and execute transaction
print!("Executing the transaction...");
let transaction_response = client
.quorum_driver_api()
.execute_transaction_block(
Transaction::from_data(tx_data, vec![signature]),
IotaTransactionBlockResponseOptions::full_content(),
Some(ExecuteTransactionRequestType::WaitForLocalExecution),
)
.await?;
let transaction_response = sign_and_execute_transaction(&client, &sender, tx_data).await?;
print!("done\n Transaction information: ");
println!("{:?}", transaction_response);

Expand Down
34 changes: 22 additions & 12 deletions crates/iota-sdk/examples/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use futures::{future, stream::StreamExt};
use iota_config::{
iota_config_dir, Config, PersistedConfig, IOTA_CLIENT_CONFIG, IOTA_KEYSTORE_FILENAME,
};
use iota_json_rpc_types::{Coin, IotaObjectDataOptions};
use iota_json_rpc_types::{Coin, IotaObjectDataOptions, IotaTransactionBlockResponse};
use iota_keys::keystore::{AccountKeystore, FileBasedKeystore};
use iota_sdk::{
iota_client_config::{IotaClientConfig, IotaEnv},
Expand Down Expand Up @@ -246,18 +246,8 @@ pub async fn split_coin_digest(
gas_price,
);

// sign & execute the transaction
let keystore = FileBasedKeystore::new(&iota_config_dir()?.join(IOTA_KEYSTORE_FILENAME))?;
let signature = keystore.sign_secure(sender, &tx_data, Intent::iota_transaction())?;
let transaction_response = sign_and_execute_transaction(client, sender, tx_data).await?;

let transaction_response = client
.quorum_driver_api()
.execute_transaction_block(
Transaction::from_data(tx_data, vec![signature]),
IotaTransactionBlockResponseOptions::new(),
Some(ExecuteTransactionRequestType::WaitForLocalExecution),
)
.await?;
Ok(transaction_response.digest)
}

Expand Down Expand Up @@ -310,6 +300,26 @@ pub fn retrieve_wallet() -> Result<WalletContext, anyhow::Error> {
Ok(wallet)
}

pub async fn sign_and_execute_transaction(
client: &IotaClient,
sender: &IotaAddress,
tx_data: TransactionData,
) -> Result<IotaTransactionBlockResponse, anyhow::Error> {
let keystore = FileBasedKeystore::new(&iota_config_dir()?.join(IOTA_KEYSTORE_FILENAME))?;
let signature = keystore.sign_secure(sender, &tx_data, Intent::iota_transaction())?;

let transaction_block_response = client
.quorum_driver_api()
.execute_transaction_block(
Transaction::from_data(tx_data, vec![signature]),
IotaTransactionBlockResponseOptions::full_content(),
Some(ExecuteTransactionRequestType::WaitForLocalExecution),
)
.await?;

Ok(transaction_block_response)
}

// this function should not be used. It is only used to make clippy happy,
// and to reduce the number of allow(dead_code) annotations to just this one
#[allow(dead_code)]
Expand Down

0 comments on commit 70d738d

Please sign in to comment.