Skip to content

Commit

Permalink
feat: wallet crate
Browse files Browse the repository at this point in the history
Creates a new crate, `alphanet-wallet`, that defines a new
RPC namespace, `wallet_`, which specifies the interface for
performing sequencer-sponsored transactions using EIP-7702.
  • Loading branch information
onbjerg committed Oct 3, 2024
1 parent 7da1aa8 commit 1f294b1
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"crates/node",
"crates/precompile",
"crates/testing",
"crates/wallet",
]
default-members = ["bin/alphanet/"]
resolver = "2"
Expand Down Expand Up @@ -56,6 +57,7 @@ strip = false
# alphanet
alphanet-node = { path = "crates/node" }
alphanet-precompile = { path = "crates/precompile" }
alphanet-wallet = { path = "crates/wallet" }

alloy = { version = "0.4", features = [
"contract",
Expand All @@ -65,6 +67,7 @@ alloy = { version = "0.4", features = [
] }
alloy-network = { version = "0.4" }
alloy-primitives = { version = "0.8.5" }
alloy-rpc-types = { version = "0.4" }
alloy-signer-local = { version = "0.4", features = ["mnemonic"] }

# tokio
Expand Down Expand Up @@ -106,10 +109,14 @@ reth-revm = { git = "https://github.com/paradigmxyz/reth.git", rev = "a8a380f",
reth-tracing = { git = "https://github.com/paradigmxyz/reth.git", rev = "a8a380f" }
reth-transaction-pool = { git = "https://github.com/paradigmxyz/reth.git", rev = "a8a380f" }

# rpc
jsonrpsee = "0.24"

# misc
clap = "4"
eyre = "0.6.12"
tracing = "0.1.0"
serde = "1"
serde_json = "1"
once_cell = "1.19"

Expand Down
19 changes: 19 additions & 0 deletions crates/wallet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "alphanet-wallet"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true

[dependencies]
alloy-primitives.workspace = true
alloy-rpc-types.workspace = true
jsonrpsee = { workspace = true, features = ["server", "macros"] }
serde = { workspace = true, features = ["derive"] }

[lints]
workspace = true
79 changes: 79 additions & 0 deletions crates/wallet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! # AlphaNet wallet.
//!
//! Implementations of a custom `wallet_` namespace for AlphaNet experiment 1.
//!
//! - `wallet_getCapabilities` based on [EIP-5792][eip-5792], with the only capability being
//! `delegation`.
//! - `wallet_sendTransaction` that can perform sequencer-sponsored [EIP-7702][eip-7702] delegations
//! and send other sequencer-sponsored transactions on behalf of EOAs with delegated code.
//!
//! # Restrictions
//!
//! `wallet_sendTransaction` has additional verifications in place to prevent some rudimentary abuse
//! of the sequencer's funds. For example, transactions cannot contain any `value`.
//!
//! [eip-5792]: https://eips.ethereum.org/EIPS/eip-5792
//! [eip-7702]: https://eips.ethereum.org/EIPS/eip-7702

#![cfg_attr(not(test), warn(unused_crate_dependencies))]

use alloy_primitives::{map::HashMap, Address, ChainId, TxHash};
use alloy_rpc_types::TransactionRequest;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use serde::{Deserialize, Serialize};

/// The capability to perform [EIP-7702][eip-7702] delegations, sponsored by the sequencer.
///
/// The sequencer will only perform delegations, and act on behalf of delegated accounts, if the
/// account delegates to one of the addresses specified within this capability.
///
/// [eip-7702]: https://eips.ethereum.org/EIPS/eip-7702
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DelegationCapability {
/// A list of valid delegation contracts.
pub addresses: Vec<Address>,
}

/// Wallet capabilities for a specific chain.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Capabilities {
/// The capability to delegate.
pub delegation: DelegationCapability,
}

/// A map of wallet capabilities per chain ID.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct WalletCapabilities(pub HashMap<ChainId, Capabilities>);

/// AlphaNet `wallet_` RPC namespace.
#[cfg_attr(not(test), rpc(server, namespace = "wallet"))]
#[cfg_attr(test, rpc(server, client, namespace = "wallet"))]
pub trait AlphaNetWalletApi {
/// Get the capabilities of the wallet.
///
/// Currently the only capability is [`DelegationCapability`].
///
/// See also [EIP-5792][eip-5792].
///
/// [eip-5792]: https://eips.ethereum.org/EIPS/eip-5792
#[method(name = "getCapabilities")]
fn get_capabilities(&self) -> RpcResult<WalletCapabilities>;

/// Send a sequencer-sponsored transaction.
///
/// The transaction will only be processed if:
///
/// - The transaction is an [EIP-7702][eip-7702] transaction that delegates to one of the
/// addresses listed in [`DelegationCapability`] (see [`Self::get_capabilities`])
/// - The transaction is an [EIP-1559][eip-1559] transaction to an EOA that is currently
/// delegated to one of the addresses above
/// - The value in the transaction is exactly 0.
///
/// The sequencer will sign the transaction and inject it into the transaction pool, provided it
/// is valid. The nonce is managed by the sequencer.
///
/// [eip-7702]: https://eips.ethereum.org/EIPS/eip-7702
/// [eip-1559]: https://eips.ethereum.org/EIPS/eip-1559
#[method(name = "sendTransaction")]
fn send_transaction(&self, request: TransactionRequest) -> RpcResult<TxHash>;
}

0 comments on commit 1f294b1

Please sign in to comment.