|
| 1 | +//! Demonstrates how `EthereumWallet` can use multiple different types of signers. |
| 2 | +
|
| 3 | +use alloy::{ |
| 4 | + network::{EthereumWallet, TransactionBuilder, TxSigner}, |
| 5 | + node_bindings::Anvil, |
| 6 | + primitives::{address, U256}, |
| 7 | + providers::{Provider, ProviderBuilder}, |
| 8 | + rpc::types::TransactionRequest, |
| 9 | + signers::{ |
| 10 | + aws::AwsSigner, |
| 11 | + ledger::{HDPath, LedgerSigner}, |
| 12 | + local::PrivateKeySigner, |
| 13 | + }, |
| 14 | +}; |
| 15 | +use aws_config::BehaviorVersion; |
| 16 | + |
| 17 | +#[tokio::main] |
| 18 | +async fn main() -> eyre::Result<()> { |
| 19 | + // Spin up a local Anvil node. |
| 20 | + // Ensure `anvil` is available in $PATH. |
| 21 | + let anvil = Anvil::new().try_spawn()?; |
| 22 | + |
| 23 | + // Initialize your signers. |
| 24 | + |
| 25 | + // Set up a local signer. |
| 26 | + let pk_signer: PrivateKeySigner = |
| 27 | + "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".parse()?; |
| 28 | + let pk_addr = pk_signer.address(); |
| 29 | + |
| 30 | + // Set up a hardware wallet signer |
| 31 | + let ledger = LedgerSigner::new(HDPath::LedgerLive(0), Some(1)).await?; |
| 32 | + let ledger_addr = ledger.address(); |
| 33 | + |
| 34 | + // AWS KMS signer |
| 35 | + let key_id = std::env::var("AWS_KEY_ID").expect("AWS_KEY_ID not set in .env file"); |
| 36 | + let config = aws_config::load_defaults(BehaviorVersion::latest()).await; |
| 37 | + let client = aws_sdk_kms::Client::new(&config); |
| 38 | + let aws = AwsSigner::new(client, key_id, Some(1)).await?; |
| 39 | + let _aws_addr = aws.address(); |
| 40 | + |
| 41 | + // Initialize `EthereumWallet`. |
| 42 | + // `pk_signer` is set as the default signer. |
| 43 | + // This signer is used to sign `TransactionRequest` and `TypedTransaction` objects that do |
| 44 | + // not specify a signer address in the `from` field. |
| 45 | + let mut wallet = EthereumWallet::new(pk_signer); |
| 46 | + // Add aws and ledger signers to the wallet |
| 47 | + wallet.register_signer(aws); |
| 48 | + wallet.register_signer(ledger); |
| 49 | + |
| 50 | + // Create a provider with the `WalletFiller`. |
| 51 | + let provider = ProviderBuilder::new().wallet(wallet).on_http(anvil.endpoint_url()); |
| 52 | + |
| 53 | + // Note that the `from` field hasn't been specified. |
| 54 | + // The wallet filler in the provider will set to it the default signer's address, which is |
| 55 | + // `pk_signer`. |
| 56 | + let tx = TransactionRequest::default() |
| 57 | + .with_to(address!("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")) |
| 58 | + .with_value(U256::from(100)); |
| 59 | + |
| 60 | + let receipt = provider.send_transaction(tx).await?.get_receipt().await?; |
| 61 | + assert_eq!(pk_addr, receipt.from); |
| 62 | + |
| 63 | + // One can hint the wallet filler about which signer to use by specifying the `from` field. |
| 64 | + // In this case, the `ledger` signer will be used to sign the transaction. |
| 65 | + let tx = TransactionRequest::default() |
| 66 | + .with_from(ledger_addr) |
| 67 | + .with_to(address!("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")) |
| 68 | + .with_value(U256::from(100)); |
| 69 | + |
| 70 | + let receipt = provider.send_transaction(tx).await?.get_receipt().await?; |
| 71 | + assert_eq!(ledger_addr, receipt.from); |
| 72 | + |
| 73 | + Ok(()) |
| 74 | +} |
0 commit comments