Sitelet https://github.com/alloy-rs/examples/pull/204/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ This repository contains the following examples:
- [x] [Ledger signer](./examples/wallets/examples/ledger_signer.rs)
- [x] [Private key signer](./examples/wallets/examples/private_key_signer.rs)
- [x] [Mnemonic signer](./examples/wallets/examples/mnemonic_signer.rs)
- [x] [Ethereum Wallet](./examples/wallets/examples/ethereum_wallet.rs)
- [x] [Sign message](./examples/wallets/examples/sign_message.rs)
- [x] [Verify message](./examples/wallets/examples/verify_message.rs)
- [x] [Sign permit hash](./examples/wallets/examples/sign_permit_hash.rs)
Expand Down
5 changes: 2 additions & 3 deletions examples/advanced/examples/any_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! of Arbitrum's transaction receipts.

use alloy::{
network::{AnyNetwork, EthereumWallet},
network::AnyNetwork,
primitives::{address, Address, U128, U256, U64},
providers::ProviderBuilder,
signers::local::PrivateKeySigner,
Expand Down Expand Up @@ -45,11 +45,10 @@ async fn main() -> Result<()> {
// [RISK WARNING! Writing a private key in the code file is insecure behavior.]
// The following code is for testing only. Set up signer from private key, be aware of danger.
let signer: PrivateKeySigner = "<PRIVATE_KEY>".parse().expect("should parse private key");
let wallet = EthereumWallet::from(signer);

// Create a provider with the Arbitrum Sepolia network and the wallet.
let rpc_url = "https://sepolia-rollup.arbitrum.io/rpc".parse()?;
let provider = ProviderBuilder::new().network::<AnyNetwork>().wallet(wallet).on_http(rpc_url);
let provider = ProviderBuilder::new().network::<AnyNetwork>().wallet(signer).on_http(rpc_url);

// Create a contract instance.
let contract = Counter::new(COUNTER_CONTRACT_ADDRESS, &provider);
Expand Down
5 changes: 2 additions & 3 deletions examples/fillers/examples/wallet_filler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Example of using the `WalletFiller` in the provider.

use alloy::{
network::{EthereumWallet, TransactionBuilder},
network::TransactionBuilder,
node_bindings::Anvil,
primitives::{address, b256, U256},
providers::{Provider, ProviderBuilder},
Expand All @@ -18,13 +18,12 @@ async fn main() -> Result<()> {

// Set up signer from the first default Anvil account (Alice).
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = anvil.endpoint_url();
let provider = ProviderBuilder::new()
// Add the `WalletFiller` to the provider
.wallet(wallet)
.wallet(signer)
.on_http(rpc_url);

// Build an EIP-1559 type transaction to send 100 wei to Vitalik.
Expand Down
5 changes: 2 additions & 3 deletions examples/providers/examples/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Example of using the `ProviderBuilder` to create a provider with a signer and network.

use alloy::{
network::{EthereumWallet, TransactionBuilder},
network::TransactionBuilder,
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
Expand All @@ -18,15 +18,14 @@ async fn main() -> Result<()> {

// Set up signer from the first default Anvil account (Alice).
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(signer.clone());

// Create two users, Alice and Bob.
let alice = signer.address();
let bob = anvil.addresses()[1];

// Set up the HTTP provider with the `reqwest` crate.
let rpc_url = anvil.endpoint_url();
let provider = ProviderBuilder::new().wallet(wallet).on_http(rpc_url);
let provider = ProviderBuilder::new().wallet(signer).on_http(rpc_url);

// Create a transaction.
let tx = TransactionRequest::default().with_to(bob).with_value(U256::from(100));
Expand Down
4 changes: 1 addition & 3 deletions examples/subscriptions/examples/event_multiplexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::str::FromStr;

use alloy::{
network::EthereumWallet,
node_bindings::Anvil,
primitives::I256,
providers::{ProviderBuilder, WsConnect},
Expand Down Expand Up @@ -50,11 +49,10 @@ async fn main() -> Result<()> {
let anvil = Anvil::new().block_time(1).try_spawn()?;

let pk: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(pk);

// Create a provider.
let ws = WsConnect::new(anvil.ws_endpoint());
let provider = ProviderBuilder::new().wallet(wallet).on_ws(ws).await?;
let provider = ProviderBuilder::new().wallet(pk).on_ws(ws).await?;

// Deploy the `EventExample` contract.
let contract = EventMultiplexer::deploy(provider).await?;
Expand Down
4 changes: 1 addition & 3 deletions examples/subscriptions/examples/poll_logs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Example of watching and polling for contract events by `WebSocket` subscription.

use alloy::{
network::EthereumWallet,
node_bindings::Anvil,
providers::{ProviderBuilder, WsConnect},
signers::local::PrivateKeySigner,
Expand Down Expand Up @@ -39,11 +38,10 @@ async fn main() -> Result<()> {
// Ensure `anvil` is available in $PATH.
let anvil = Anvil::new().block_time(1).try_spawn()?;
let pk: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::new(pk);

// Create a WebSocket provider.
let ws = WsConnect::new(anvil.ws_endpoint());
let provider = ProviderBuilder::new().wallet(wallet).on_ws(ws).await?;
let provider = ProviderBuilder::new().wallet(pk).on_ws(ws).await?;

// Deploy the `Counter` contract.
let contract = Counter::deploy(provider.clone()).await?;
Expand Down
5 changes: 2 additions & 3 deletions examples/transactions/examples/send_eip7702_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use alloy::{
eips::eip7702::Authorization,
network::{EthereumWallet, TransactionBuilder, TransactionBuilder7702},
network::{TransactionBuilder, TransactionBuilder7702},
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
Expand Down Expand Up @@ -45,8 +45,7 @@ async fn main() -> Result<()> {

// Create a provider with the wallet for only Bob (not Alice).
let rpc_url = anvil.endpoint_url();
let wallet = EthereumWallet::from(bob.clone());
let provider = ProviderBuilder::new().wallet(wallet).on_http(rpc_url);
let provider = ProviderBuilder::new().wallet(bob.clone()).on_http(rpc_url);

// Deploy the contract Alice will authorize.
let contract = Log::deploy(&provider).await?;
Expand Down
74 changes: 74 additions & 0 deletions examples/wallets/examples/ethereum_wallet.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very helpful

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(())
}
5 changes: 2 additions & 3 deletions examples/wallets/examples/keystore_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::path::PathBuf;

use alloy::{
network::{EthereumWallet, TransactionBuilder},
network::TransactionBuilder,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
Expand All @@ -21,11 +21,10 @@ async fn main() -> Result<()> {
let keystore_file_path =
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")?).join("examples/keystore/alice.json");
let signer = LocalSigner::decrypt_keystore(keystore_file_path, password)?;
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let provider =
ProviderBuilder::new().wallet(wallet).on_anvil_with_config(|anvil| anvil.block_time(1));
ProviderBuilder::new().wallet(signer).on_anvil_with_config(|anvil| anvil.block_time(1));

// Build a transaction to send 100 wei from Alice to Vitalik.
// The `from` field is automatically filled to the first signer's address (Alice).
Expand Down
5 changes: 2 additions & 3 deletions examples/wallets/examples/ledger_signer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Example of signing and sending a transaction using a Ledger device.

use alloy::{
network::{EthereumWallet, TransactionBuilder},
network::TransactionBuilder,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
Expand All @@ -13,11 +13,10 @@ use eyre::Result;
async fn main() -> Result<()> {
// Instantiate the application by acquiring a lock on the Ledger device.
let signer = LedgerSigner::new(HDPath::LedgerLive(0), Some(1)).await?;
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = "https://eth.merkle.io".parse()?;
let provider = ProviderBuilder::new().wallet(wallet).on_http(rpc_url);
let provider = ProviderBuilder::new().wallet(signer).on_http(rpc_url);

// Build a transaction to send 100 wei from Alice to Vitalik.
// The `from` field is automatically filled to the first signer's address (Alice).
Expand Down
5 changes: 2 additions & 3 deletions examples/wallets/examples/private_key_signer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Example of using a local wallet to sign and send a transaction.

use alloy::{
network::{EthereumWallet, TransactionBuilder},
network::TransactionBuilder,
node_bindings::Anvil,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
Expand All @@ -21,11 +21,10 @@ async fn main() -> Result<()> {
// The following code is for testing only. Set up signer from private key, be aware of danger.
// let signer: PrivateKeySigner = "<PRIVATE_KEY>".parse().expect("should parse private key");
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = anvil.endpoint_url();
let provider = ProviderBuilder::new().wallet(wallet).on_http(rpc_url);
let provider = ProviderBuilder::new().wallet(signer).on_http(rpc_url);

// Build a transaction to send 100 wei from Alice to Vitalik.
// The `from` field is automatically filled to the first signer's address (Alice).
Expand Down
5 changes: 2 additions & 3 deletions examples/wallets/examples/trezor_signer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Example of signing and sending a transaction using a Trezor device.

use alloy::{
network::{EthereumWallet, TransactionBuilder},
network::TransactionBuilder,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
Expand All @@ -13,11 +13,10 @@ use eyre::Result;
async fn main() -> Result<()> {
// Instantiate the application by acquiring a lock on the Trezor device.
let signer = TrezorSigner::new(HDPath::TrezorLive(0), Some(1)).await?;
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = "https://eth.merkle.io".parse()?;
let provider = ProviderBuilder::new().wallet(wallet).on_http(rpc_url);
let provider = ProviderBuilder::new().wallet(signer).on_http(rpc_url);

// Build a transaction to send 100 wei from Alice to Vitalik.
// The `from` field is automatically filled to the first signer's address (Alice).
Expand Down
5 changes: 2 additions & 3 deletions examples/wallets/examples/yubi_signer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Example of signing and sending a transaction using a Yubi device.

use alloy::{
network::{EthereumWallet, TransactionBuilder},
network::TransactionBuilder,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
Expand All @@ -22,11 +22,10 @@ async fn main() -> Result<()> {
// `from_key` method to upload a key you already have, or the `new` method
// to generate a new keypair.
let signer = YubiSigner::connect(connector, Credentials::default(), 0);
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = "https://eth.merkle.io".parse()?;
let provider = ProviderBuilder::new().wallet(wallet).on_http(rpc_url);
let provider = ProviderBuilder::new().wallet(signer).on_http(rpc_url);

// Build a transaction to send 100 wei from Alice to Vitalik.
// The `from` field is automatically filled to the first signer's address (Alice).
Expand Down
1 change: 1 addition & 0 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function main () {
-e 'foundry_fork_db' \
-e 'reth_db_layer' \
-e 'reth_db_provider' \
-e 'ethereum_wallet' \
| xargs -n1 echo
)"

Expand Down