Skip to content

Commit

Permalink
Properly save and load wallet files
Browse files Browse the repository at this point in the history
  • Loading branch information
soareschen committed Mar 4, 2025
1 parent 4b3eafe commit 0780e4f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
19 changes: 6 additions & 13 deletions relayer/crates/starknet-cli/src/contexts/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,25 +259,18 @@ impl ConfigUpdater<StarknetChainDriver, StarknetRelayerConfig> for UpdateStarkne
.next(),
};

let relayer_wallet_file = {
// FIXME: This is a temporary solution to write the wallet to a file
// We should use a solution like how hermes-sdk does it for Cosmos-SDK
// https://github.com/informalsystems/hermes-sdk/blob/27168d1/crates/cosmos/cosmos-integration-tests/src/impls/bootstrap/relayer_chain_config.rs#L83-L105
let file_path = "./relayer_wallet.toml";

let relayer_wallet_str = to_string_pretty(&relayer_wallet)?;

std::fs::write(file_path, relayer_wallet_str)?;

file_path
};
let relayer_wallet_path = chain_driver
.chain_store_dir
.join("wallets/relayer.toml")
.display()
.to_string();

let chain_config = StarknetChainConfig {
json_rpc_url: format!(
"http://{}:{}/",
chain_driver.node_config.rpc_addr, chain_driver.node_config.rpc_port
),
relayer_wallet: relayer_wallet_file.to_string(),
relayer_wallet: relayer_wallet_path,
poll_interval: chain_driver.chain.poll_interval,
block_time: chain_driver.chain.block_time,
contract_addresses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use hermes_cosmos_test_components::bootstrap::traits::types::genesis_config::Cha
use hermes_error::impls::ProvideHermesError;
use hermes_error::types::HermesError;
use hermes_runtime::types::runtime::HermesRuntime;
use hermes_runtime_components::traits::fs::create_dir::CanCreateDir;
use hermes_runtime_components::traits::fs::write_file::CanWriteStringToFile;
use hermes_runtime_components::traits::runtime::{
HasRuntime, RuntimeGetterComponent, RuntimeTypeProviderComponent,
};
Expand Down Expand Up @@ -116,6 +118,19 @@ impl ChainDriverBuilder<StarknetBootstrap> for StarknetBootstrapComponents {
) -> Result<StarknetChainDriver, HermesError> {
let runtime = bootstrap.runtime.clone();

let chain_store_dir = bootstrap.chain_store_dir.clone();

runtime.create_dir(&chain_store_dir.join("wallets")).await?;

for (name, wallet) in wallets.iter() {
let wallet_str = toml::to_string_pretty(wallet)?;
let wallet_path = chain_store_dir.join(format!("wallets/{name}.toml"));

runtime
.write_string_to_file(&wallet_path, &wallet_str)
.await?;
}

let relayer_wallet = wallets
.get("relayer")
.ok_or_else(|| StarknetBootstrap::raise_error("expect relayer wallet to be present"))?
Expand Down Expand Up @@ -181,6 +196,7 @@ impl ChainDriverBuilder<StarknetBootstrap> for StarknetBootstrapComponents {
let chain_driver = StarknetChainDriver {
runtime,
chain,
chain_store_dir,
genesis_config,
node_config,
wallets,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::path::PathBuf;

use cgp::core::component::UseDelegate;
use cgp::core::error::{ErrorRaiserComponent, ErrorTypeProviderComponent};
Expand Down Expand Up @@ -28,6 +29,7 @@ use tokio::process::Child;
pub struct StarknetChainDriver {
pub runtime: HermesRuntime,
pub chain: StarknetChain,
pub chain_store_dir: PathBuf,
pub genesis_config: StarknetGenesisConfig,
pub node_config: StarknetNodeConfig,
pub wallets: BTreeMap<String, StarknetWallet>,
Expand Down
12 changes: 8 additions & 4 deletions relayer/crates/starknet-relayer/src/contexts/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::sync::Arc;
use core::marker::PhantomData;
use core::ops::Deref;
use std::path::PathBuf;

use cgp::core::component::UseDelegate;
use cgp::core::error::{ErrorRaiserComponent, ErrorTypeProviderComponent};
Expand All @@ -25,6 +26,7 @@ use hermes_relayer_components::multi::traits::birelay_at::BiRelayTypeAtComponent
use hermes_relayer_components::multi::traits::chain_at::ChainTypeAtComponent;
use hermes_relayer_components::multi::traits::relay_at::RelayTypeAtComponent;
use hermes_runtime::types::runtime::HermesRuntime;
use hermes_runtime_components::traits::fs::read_file::CanReadFileAsString;
use hermes_runtime_components::traits::runtime::{
RuntimeGetterComponent, RuntimeTypeProviderComponent,
};
Expand Down Expand Up @@ -227,10 +229,12 @@ impl StarknetBuilder {
return Err(eyre!("Starknet chain has a different ID as configured. Expected: {expected_chain_id}, got: {chain_id}").into());
}

let relayer_wallet = toml::from_str::<StarknetWallet>(&std::fs::read_to_string(
&self.starknet_chain_config.relayer_wallet,
)?)
.map_err(|e| eyre!("Failed to parse relayer wallet: {e}"))?;
let wallet_path = PathBuf::from(self.starknet_chain_config.relayer_wallet.clone());

let wallet_str = self.runtime.read_file_as_string(&wallet_path).await?;

let relayer_wallet: StarknetWallet = toml::from_str(&wallet_str)
.map_err(|e| eyre!("Failed to parse relayer wallet: {e}"))?;

let account = SingleOwnerAccount::new(
rpc_client.clone(),
Expand Down

0 comments on commit 0780e4f

Please sign in to comment.