From 297f56a79301514f865ee0c638f96255b0a1b6a1 Mon Sep 17 00:00:00 2001 From: Oliver Nordbjerg Date: Thu, 17 Oct 2024 18:21:15 +0200 Subject: [PATCH] chore: use `U64` to serialize chain id as quantity --- crates/wallet/src/lib.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/wallet/src/lib.rs b/crates/wallet/src/lib.rs index bb761f0..3086561 100644 --- a/crates/wallet/src/lib.rs +++ b/crates/wallet/src/lib.rs @@ -20,7 +20,7 @@ use alloy_network::{ eip2718::Encodable2718, Ethereum, EthereumWallet, NetworkWallet, TransactionBuilder, }; -use alloy_primitives::{map::HashMap, Address, ChainId, TxHash, TxKind, U256}; +use alloy_primitives::{map::HashMap, Address, ChainId, TxHash, TxKind, U256, U64}; use alloy_rpc_types::TransactionRequest; use jsonrpsee::{ core::{async_trait, RpcResult}, @@ -58,13 +58,14 @@ pub struct Capabilities { } /// 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); +pub struct WalletCapabilities(pub HashMap); 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(&chain_id) + self.0.get(&U64::from(chain_id)) } } @@ -184,7 +185,7 @@ impl OdysseyWallet { eth_api, chain_id, capabilities: WalletCapabilities(HashMap::from_iter([( - chain_id, + U64::from(chain_id), Capabilities { delegation: DelegationCapability { addresses: valid_designations } }, )])), permit: Default::default(), @@ -391,27 +392,27 @@ mod tests { validate_tx_request, Capabilities, DelegationCapability, OdysseyWalletError, WalletCapabilities, }; - use alloy_primitives::{address, map::HashMap, Address, U256}; + use alloy_primitives::{address, map::HashMap, Address, U256, U64}; use alloy_rpc_types::TransactionRequest; #[test] fn ser() { let caps = WalletCapabilities(HashMap::from_iter([( - 0x69420, + U64::from(0x69420), Capabilities { delegation: DelegationCapability { addresses: vec![address!("90f79bf6eb2c4f870365e785982e1f101e93b906")], }, }, )])); - assert_eq!(serde_json::to_string(&caps).unwrap(), "{\"431136\":{\"delegation\":{\"addresses\":[\"0x90f79bf6eb2c4f870365e785982e1f101e93b906\"]}}}"); + assert_eq!(serde_json::to_string(&caps).unwrap(), "{\"0x69420\":{\"delegation\":{\"addresses\":[\"0x90f79bf6eb2c4f870365e785982e1f101e93b906\"]}}}"); } #[test] fn de() { let caps: WalletCapabilities = serde_json::from_str( r#"{ - "431136": { + "0x69420": { "delegation": { "addresses": ["0x90f79bf6eb2c4f870365e785982e1f101e93b906"] } @@ -423,7 +424,7 @@ mod tests { assert_eq!( caps, WalletCapabilities(HashMap::from_iter([( - 0x69420, + U64::from(0x69420), Capabilities { delegation: DelegationCapability { addresses: vec![address!("90f79bf6eb2c4f870365e785982e1f101e93b906")],