Skip to content

Commit

Permalink
fix for conflicting alloy signers
Browse files Browse the repository at this point in the history
  • Loading branch information
0xForerunner committed Nov 4, 2024
1 parent 166caf5 commit 8c78966
Showing 1 changed file with 38 additions and 50 deletions.
88 changes: 38 additions & 50 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod relay;
pub mod tx_sitter;
pub mod utils;

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

Expand All @@ -31,6 +32,7 @@ use tracing_subscriber::util::SubscriberInitExt;
use self::abi::IWorldIDIdentityManager::TreeChanged;
use self::block_scanner::BlockScanner;
use self::config::Config;
use self::relay::signer::AlloySignerProvider;

/// This service syncs the state of the World Tree and spawns a server that can deliver inclusion proofs for a given identity.
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -181,45 +183,48 @@ pub async fn run(config: Config) -> Result<()> {
/// Additionally initializes the signers from the global wallet configuration if present,
/// otherwise from the bridged network configuration.
fn init_relays(cfg: Config) -> Result<Vec<Relayer>> {
// Optinally use a global wallet configuration for all networks without a specific wallet configuration.
let global_signer = if let Some(wallet) = cfg.canonical_network.wallet {
match wallet {
WalletConfig::Mnemonic { mnemonic } => {
let signer = MnemonicBuilder::<English>::default()
.phrase(mnemonic)
.index(0)?
.build()?;
let wallet = EthereumWallet::new(signer);
// A global signer is required when using an [`AlloySigner`]
// in order to keep the transaction nonce in sync.
let mut alloy_signer_providers =
HashMap::<String, Arc<AlloySignerProvider>>::new();

let provider =
cfg.canonical_network.provider.signer(wallet.clone());

Some(Arc::new(provider))
}
_ => None,
}
} else {
None
};
cfg.bridged_networks
.iter()
.map(|bridged| {
let wallet_config = bridged.wallet.as_ref();
let wallet_config = bridged
.wallet
.clone()
.or(cfg.canonical_network.wallet.clone())
.ok_or_else(|| eyre!("No wallet configuration found"))?;

match bridged.ty {
NetworkType::Evm => match wallet_config {
Some(WalletConfig::Mnemonic { mnemonic }) => {
let signer = MnemonicBuilder::<English>::default()
.phrase(mnemonic)
.index(0)?
.build()?;
let wallet = EthereumWallet::new(signer);
let provider = cfg
.canonical_network
.provider
.signer(wallet.clone());
WalletConfig::Mnemonic { mnemonic } => {
let provider = match alloy_signer_providers
.get(&mnemonic)
{
Some(provider) => provider.clone(),
None => {
let signer =
MnemonicBuilder::<English>::default()
.phrase(&mnemonic)
.index(0)?
.build()?;
let wallet = EthereumWallet::new(signer);
let provider = Arc::new(
cfg.canonical_network
.provider
.signer(wallet.clone()),
);
alloy_signer_providers
.insert(mnemonic.clone(), provider.clone());
provider
}
};

let alloy_signer = AlloySigner::new(
bridged.state_bridge_addr,
Arc::new(provider),
provider,
);

Ok(Relayer::EVMRelay(EVMRelay::new(
Expand All @@ -228,11 +233,11 @@ fn init_relays(cfg: Config) -> Result<Vec<Relayer>> {
bridged.provider.rpc_endpoint.clone(),
)))
}
Some(WalletConfig::TxSitter { url, gas_limit }) => {
WalletConfig::TxSitter { url, gas_limit } => {
let signer = TxSitterSigner::new(
url.as_str(),
bridged.state_bridge_addr,
*gas_limit,
gas_limit,
);

Ok(Relayer::EVMRelay(EVMRelay::new(
Expand All @@ -241,23 +246,6 @@ fn init_relays(cfg: Config) -> Result<Vec<Relayer>> {
bridged.provider.rpc_endpoint.clone(),
)))
}
None => {
if let Some(global_signer) = &global_signer {
info!(network = %bridged.name, "Using global wallet configuration for bridged network");
let alloy_signer = AlloySigner::new(
bridged.state_bridge_addr,
global_signer.clone(),
);

Ok(Relayer::EVMRelay(EVMRelay::new(
Signer::AlloySigner(alloy_signer),
bridged.world_id_addr,
bridged.provider.rpc_endpoint.clone(),
)))
} else {
Err(eyre!("No wallet configuration found"))
}
}
},
NetworkType::Svm => unimplemented!(),
NetworkType::Scroll => unimplemented!(),
Expand Down

0 comments on commit 8c78966

Please sign in to comment.