Skip to content
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

feat: removed wallet_getCapabilities #104

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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: 0 additions & 1 deletion bin/odyssey/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ fn main() {
wallet,
ctx.registry.eth_api().clone(),
ctx.config().chain.chain().id(),
valid_delegations,
)
.into_rpc(),
)?;
Expand Down
30 changes: 12 additions & 18 deletions crates/e2e-tests/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{collections::BTreeMap, sync::LazyLock};
use std::{str::FromStr, sync::LazyLock};

use alloy::{
eips::eip7702::Authorization,
primitives::{b256, Address, B256, U256},
primitives::{b256, Address, B256},
providers::{PendingTransactionBuilder, Provider, ProviderBuilder},
signers::SignerSync,
};
Expand Down Expand Up @@ -54,14 +54,11 @@ async fn test_wallet_api() -> Result<(), Box<dyn std::error::Error>> {
"59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
))?;

let capabilities: BTreeMap<U256, BTreeMap<String, BTreeMap<String, Vec<Address>>>> =
provider.client().request_noparams("wallet_getCapabilities").await?;

let chain_id = U256::from(provider.get_chain_id().await?);

let delegation_address =
capabilities.get(&chain_id).unwrap().get("delegation").unwrap().get("addresses").unwrap()
[0];
let delegation_address = Address::from_str(
&std::env::var("DELEGATION_ADDRESS")
.unwrap_or_else(|_| "0x90f79bf6eb2c4f870365e785982e1f101e93b906".to_string()),
)
.unwrap();

let auth = Authorization {
chain_id: provider.get_chain_id().await?,
Expand Down Expand Up @@ -98,14 +95,11 @@ async fn test_new_wallet_api() -> Result<(), Box<dyn std::error::Error>> {
"59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
))?;

let capabilities: BTreeMap<U256, BTreeMap<String, BTreeMap<String, Vec<Address>>>> =
provider.client().request_noparams("wallet_getCapabilities").await?;

let chain_id = U256::from(provider.get_chain_id().await?);

let delegation_address =
capabilities.get(&chain_id).unwrap().get("delegation").unwrap().get("addresses").unwrap()
[0];
let delegation_address = Address::from_str(
&std::env::var("DELEGATION_ADDRESS")
.unwrap_or_else(|_| "0x90f79bf6eb2c4f870365e785982e1f101e93b906".to_string()),
)
.unwrap();

let auth = Authorization {
chain_id: provider.get_chain_id().await?,
Expand Down
89 changes: 3 additions & 86 deletions crates/wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use alloy_eips::BlockId;
use alloy_network::{
eip2718::Encodable2718, Ethereum, EthereumWallet, NetworkWallet, TransactionBuilder,
};
use alloy_primitives::{map::HashMap, Address, ChainId, TxHash, TxKind, U256, U64};
use alloy_primitives::{Address, ChainId, TxHash, TxKind, U256};
use alloy_rpc_types::TransactionRequest;
use jsonrpsee::{
core::{async_trait, RpcResult},
Expand Down Expand Up @@ -53,39 +53,10 @@ pub struct DelegationCapability {
pub addresses: Vec<Address>,
}

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

/// A map of wallet capabilities per chain ID.
// NOTE(onbjerg): We use `U64` to serialize the chain ID as a quantity. This can be changed back to `ChainId` with https://github.com/alloy-rs/alloy/issues/1502
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub struct WalletCapabilities(pub HashMap<U64, Capabilities>);

impl WalletCapabilities {
/// Get the capabilities of the wallet API for the specified chain ID.
pub fn get(&self, chain_id: ChainId) -> Option<&Capabilities> {
self.0.get(&U64::from(chain_id))
}
}

/// Odyssey `wallet_` RPC namespace.
#[cfg_attr(not(test), rpc(server, namespace = "wallet"))]
#[cfg_attr(test, rpc(server, client, namespace = "wallet"))]
pub trait OdysseyWalletApi {
/// 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:
Expand Down Expand Up @@ -172,17 +143,12 @@ impl<Provider, Eth> OdysseyWallet<Provider, Eth> {
wallet: EthereumWallet,
eth_api: Eth,
chain_id: ChainId,
valid_designations: Vec<Address>,
) -> Self {
let inner = OdysseyWalletInner {
provider,
wallet,
eth_api,
chain_id,
capabilities: WalletCapabilities(HashMap::from_iter([(
U64::from(chain_id),
Capabilities { delegation: DelegationCapability { addresses: valid_designations } },
)])),
permit: Default::default(),
metrics: WalletMetrics::default(),
};
Expand All @@ -200,11 +166,6 @@ where
Provider: StateProviderFactory + Send + Sync + 'static,
Eth: FullEthApi + Send + Sync + 'static,
{
fn get_capabilities(&self) -> RpcResult<WalletCapabilities> {
trace!(target: "rpc::wallet", "Serving wallet_getCapabilities");
Ok(self.inner.capabilities.clone())
}

async fn send_transaction(&self, mut request: TransactionRequest) -> RpcResult<TxHash> {
trace!(target: "rpc::wallet", ?request, "Serving odyssey_sendTransaction");

Expand Down Expand Up @@ -328,7 +289,6 @@ struct OdysseyWalletInner<Provider, Eth> {
eth_api: Eth,
wallet: EthereumWallet,
chain_id: ChainId,
capabilities: WalletCapabilities,
/// Used to guard tx signing
permit: Mutex<()>,
/// Metrics for the `wallet_` RPC namespace.
Expand Down Expand Up @@ -366,52 +326,9 @@ struct WalletMetrics {

#[cfg(test)]
mod tests {
use crate::{
validate_tx_request, Capabilities, DelegationCapability, OdysseyWalletError,
WalletCapabilities,
};
use alloy_primitives::{address, map::HashMap, Address, U256, U64};
use crate::{validate_tx_request, OdysseyWalletError};
use alloy_primitives::{Address, U256};
use alloy_rpc_types::TransactionRequest;

#[test]
fn ser() {
let caps = WalletCapabilities(HashMap::from_iter([(
U64::from(0x69420),
Capabilities {
delegation: DelegationCapability {
addresses: vec![address!("90f79bf6eb2c4f870365e785982e1f101e93b906")],
},
},
)]));
assert_eq!(serde_json::to_string(&caps).unwrap(), "{\"0x69420\":{\"delegation\":{\"addresses\":[\"0x90f79bf6eb2c4f870365e785982e1f101e93b906\"]}}}");
}

#[test]
fn de() {
let caps: WalletCapabilities = serde_json::from_str(
r#"{
"0x69420": {
"delegation": {
"addresses": ["0x90f79bf6eb2c4f870365e785982e1f101e93b906"]
}
}
}"#,
)
.expect("could not deser");

assert_eq!(
caps,
WalletCapabilities(HashMap::from_iter([(
U64::from(0x69420),
Capabilities {
delegation: DelegationCapability {
addresses: vec![address!("90f79bf6eb2c4f870365e785982e1f101e93b906")],
},
},
)]))
);
}

#[test]
fn no_value_allowed() {
assert_eq!(
Expand Down
Loading