Skip to content

Commit

Permalink
chore: use U64 to serialize chain id as quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg committed Oct 17, 2024
1 parent d922f5e commit 297f56a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions crates/wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<ChainId, Capabilities>);
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(&chain_id)
self.0.get(&U64::from(chain_id))
}
}

Expand Down Expand Up @@ -184,7 +185,7 @@ impl<Provider, Eth> OdysseyWallet<Provider, Eth> {
eth_api,
chain_id,
capabilities: WalletCapabilities(HashMap::from_iter([(
chain_id,
U64::from(chain_id),
Capabilities { delegation: DelegationCapability { addresses: valid_designations } },
)])),
permit: Default::default(),
Expand Down Expand Up @@ -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"]
}
Expand All @@ -423,7 +424,7 @@ mod tests {
assert_eq!(
caps,
WalletCapabilities(HashMap::from_iter([(
0x69420,
U64::from(0x69420),
Capabilities {
delegation: DelegationCapability {
addresses: vec![address!("90f79bf6eb2c4f870365e785982e1f101e93b906")],
Expand Down

0 comments on commit 297f56a

Please sign in to comment.