iota_cluster_test/
wallet_client.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use iota_keys::keystore::AccountKeystore;
6use iota_sdk::{IotaClient, IotaClientBuilder, wallet_context::WalletContext};
7use iota_types::{
8    base_types::IotaAddress,
9    crypto::{KeypairTraits, Signature},
10    transaction::TransactionData,
11};
12use shared_crypto::intent::Intent;
13use tracing::{Instrument, info, info_span};
14
15use super::Cluster;
16use crate::cluster::new_wallet_context_from_cluster;
17
18pub struct WalletClient {
19    wallet_context: WalletContext,
20    address: IotaAddress,
21    fullnode_client: IotaClient,
22}
23
24#[allow(clippy::borrowed_box)]
25impl WalletClient {
26    pub async fn new_from_cluster(cluster: &(dyn Cluster + Sync + Send)) -> Self {
27        let key = cluster.user_key();
28        let address: IotaAddress = key.public().into();
29        let wallet_context = new_wallet_context_from_cluster(cluster, key)
30            .instrument(info_span!("init_wallet_context_for_test_user"));
31
32        let rpc_url = String::from(cluster.fullnode_url());
33        info!("Use fullnode rpc: {}", &rpc_url);
34        let fullnode_client = IotaClientBuilder::default().build(rpc_url).await.unwrap();
35
36        Self {
37            wallet_context: wallet_context.into_inner(),
38            address,
39            fullnode_client,
40        }
41    }
42
43    pub fn get_wallet(&self) -> &WalletContext {
44        &self.wallet_context
45    }
46
47    pub fn get_wallet_mut(&mut self) -> &mut WalletContext {
48        &mut self.wallet_context
49    }
50
51    pub fn get_wallet_address(&self) -> IotaAddress {
52        self.address
53    }
54
55    pub fn get_fullnode_client(&self) -> &IotaClient {
56        &self.fullnode_client
57    }
58
59    pub fn sign(&self, txn_data: &TransactionData, desc: &str) -> Signature {
60        self.get_wallet()
61            .config()
62            .keystore()
63            .sign_secure(&self.address, txn_data, Intent::iota_transaction())
64            .unwrap_or_else(|e| panic!("Failed to sign transaction for {}. {}", desc, e))
65    }
66}