-
Notifications
You must be signed in to change notification settings - Fork 63
feat: use signers in provider and demonstrate EthereumWallet #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac43ca2
chore: use signers instead of `EthereumWallet`
yash-atreya d923b8b
Merge branch 'main' into yash/signer-instead-of-wallet
yash-atreya 7ed5032
nits
yash-atreya 29fb1a4
feat: EthereumWallet example
yash-atreya 5d251f3
nit
yash-atreya a215586
clippy
yash-atreya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //! Demonstrates how `EthereumWallet` can use multiple different types of signers. | ||
|
|
||
| use alloy::{ | ||
| network::{EthereumWallet, TransactionBuilder, TxSigner}, | ||
| node_bindings::Anvil, | ||
| primitives::{address, U256}, | ||
| providers::{Provider, ProviderBuilder}, | ||
| rpc::types::TransactionRequest, | ||
| signers::{ | ||
| aws::AwsSigner, | ||
| ledger::{HDPath, LedgerSigner}, | ||
| local::PrivateKeySigner, | ||
| }, | ||
| }; | ||
| use aws_config::BehaviorVersion; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> eyre::Result<()> { | ||
| // Spin up a local Anvil node. | ||
| // Ensure `anvil` is available in $PATH. | ||
| let anvil = Anvil::new().try_spawn()?; | ||
|
|
||
| // Initialize your signers. | ||
|
|
||
| // Set up a local signer. | ||
| let pk_signer: PrivateKeySigner = | ||
| "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".parse()?; | ||
| let pk_addr = pk_signer.address(); | ||
|
|
||
| // Set up a hardware wallet signer | ||
| let ledger = LedgerSigner::new(HDPath::LedgerLive(0), Some(1)).await?; | ||
| let ledger_addr = ledger.address(); | ||
|
|
||
| // AWS KMS signer | ||
| let key_id = std::env::var("AWS_KEY_ID").expect("AWS_KEY_ID not set in .env file"); | ||
| let config = aws_config::load_defaults(BehaviorVersion::latest()).await; | ||
| let client = aws_sdk_kms::Client::new(&config); | ||
| let aws = AwsSigner::new(client, key_id, Some(1)).await?; | ||
| let _aws_addr = aws.address(); | ||
|
|
||
| // Initialize `EthereumWallet`. | ||
| // `pk_signer` is set as the default signer. | ||
| // This signer is used to sign `TransactionRequest` and `TypedTransaction` objects that do | ||
| // not specify a signer address in the `from` field. | ||
| let mut wallet = EthereumWallet::new(pk_signer); | ||
| // Add aws and ledger signers to the wallet | ||
| wallet.register_signer(aws); | ||
| wallet.register_signer(ledger); | ||
|
|
||
| // Create a provider with the `WalletFiller`. | ||
| let provider = ProviderBuilder::new().wallet(wallet).on_http(anvil.endpoint_url()); | ||
|
|
||
| // Note that the `from` field hasn't been specified. | ||
| // The wallet filler in the provider will set to it the default signer's address, which is | ||
| // `pk_signer`. | ||
| let tx = TransactionRequest::default() | ||
| .with_to(address!("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")) | ||
| .with_value(U256::from(100)); | ||
|
|
||
| let receipt = provider.send_transaction(tx).await?.get_receipt().await?; | ||
| assert_eq!(pk_addr, receipt.from); | ||
|
|
||
| // One can hint the wallet filler about which signer to use by specifying the `from` field. | ||
| // In this case, the `ledger` signer will be used to sign the transaction. | ||
| let tx = TransactionRequest::default() | ||
| .with_from(ledger_addr) | ||
| .with_to(address!("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")) | ||
| .with_value(U256::from(100)); | ||
|
|
||
| let receipt = provider.send_transaction(tx).await?.get_receipt().await?; | ||
| assert_eq!(ledger_addr, receipt.from); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is very helpful