sui_cluster_test/
wallet_client.rs

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