Skip to content

Commit

Permalink
feat: use FixedBytes for sol_data::FixedBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 19, 2023
1 parent 4bdc7de commit 6ba6ee3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 81 deletions.
8 changes: 4 additions & 4 deletions crates/sol-types/src/coder/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub fn decode_params<'de, T: TokenSeq<'de>>(data: &'de [u8], validate: bool) ->
mod tests {
use crate::{sol_data, utils::pad_u32, SolType};
use alloc::string::ToString;
use alloy_primitives::{Address, B256, U256};
use alloy_primitives::{address, Address, B256, U256};
use hex_literal::hex;

#[test]
Expand Down Expand Up @@ -559,9 +559,9 @@ mod tests {
assert_eq!(
MyTy::decode_params(&encoded, false).unwrap(),
(
hex!("8497afefdc5ac170a664a231f6efb25526ef813f").into(),
B256::repeat_byte(0x01).into(),
[0x02; 4],
address!("8497afefdc5ac170a664a231f6efb25526ef813f"),
B256::repeat_byte(0x01),
[0x02; 4].into(),
"0x0000001F".into(),
)
);
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-types/src/coder/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,12 @@ mod tests {

#[test]
fn encode_fixed_bytes() {
let encoded = sol_data::FixedBytes::<2>::encode_single(&[0x12, 0x34]);
let encoded = sol_data::FixedBytes::<2>::encode_single(&[0x12, 0x34].into());
let expected = hex!("1234000000000000000000000000000000000000000000000000000000000000");
assert_eq!(encoded, expected);
assert_eq!(
encoded.len(),
sol_data::FixedBytes::<2>::encoded_size(&[0x12, 0x34])
sol_data::FixedBytes::<2>::encoded_size(&[0x12, 0x34].into())
);
}

Expand Down
121 changes: 56 additions & 65 deletions crates/sol-types/src/eip712.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,18 @@ impl Eip712Domain {
self.salt,
) {
(None, None, None, None, None) => vec![],
(None, None, None, None, Some(salt)) => {
<(sol_data::FixedBytes<32>,)>::encode(&(salt.0,))
}
(None, None, None, None, Some(salt)) => <(sol_data::FixedBytes<32>,)>::encode(&(salt,)),
(None, None, None, Some(verifying_contract), None) => {
<(sol_data::Address,)>::encode(&(verifying_contract,))
}
(None, None, None, Some(verifying_contract), Some(salt)) => {
<(sol_data::Address, sol_data::FixedBytes<32>)>::encode(&(
verifying_contract,
salt.0,
))
<(sol_data::Address, sol_data::FixedBytes<32>)>::encode(&(verifying_contract, salt))
}
(None, None, Some(chain_id), None, None) => {
<(sol_data::Uint<256>,)>::encode(&(chain_id,))
}
(None, None, Some(chain_id), None, Some(salt)) => {
<(sol_data::Uint<256>, sol_data::FixedBytes<32>)>::encode(&(chain_id, salt.0))
<(sol_data::Uint<256>, sol_data::FixedBytes<32>)>::encode(&(chain_id, salt))
}
(None, None, Some(chain_id), Some(verifying_contract), None) => {
<(sol_data::Uint<256>, sol_data::Address)>::encode(&(chain_id, verifying_contract))
Expand All @@ -158,20 +153,20 @@ impl Eip712Domain {
sol_data::Uint<256>,
sol_data::Address,
sol_data::FixedBytes<32>,
)>::encode(&(chain_id, verifying_contract, salt.0))
)>::encode(&(chain_id, verifying_contract, salt))
}
(None, Some(version), None, None, None) => {
<(sol_data::FixedBytes<32>,)>::encode(&(keccak256(version.as_bytes()).0,))
<(sol_data::FixedBytes<32>,)>::encode(&(keccak256(version.as_bytes()),))
}
(None, Some(version), None, None, Some(salt)) => {
<(sol_data::FixedBytes<32>, sol_data::FixedBytes<32>)>::encode(&(
keccak256(version.as_bytes()).0,
salt.0,
keccak256(version.as_bytes()),
salt,
))
}
(None, Some(version), None, Some(verifying_contract), None) => {
<(sol_data::FixedBytes<32>, sol_data::Address)>::encode(&(
keccak256(version.as_bytes()).0,
keccak256(version.as_bytes()),
verifying_contract,
))
}
Expand All @@ -180,11 +175,11 @@ impl Eip712Domain {
sol_data::Address,
sol_data::FixedBytes<32>,
)>::encode(
&(keccak256(version.as_bytes()).0, verifying_contract, salt.0),
&(keccak256(version.as_bytes()), verifying_contract, salt),
),
(None, Some(version), Some(chain_id), None, None) => {
<(sol_data::FixedBytes<32>, sol_data::Uint<256>)>::encode(&(
keccak256(version.as_bytes()).0,
keccak256(version.as_bytes()),
chain_id,
))
}
Expand All @@ -193,57 +188,53 @@ impl Eip712Domain {
sol_data::FixedBytes<32>,
sol_data::Uint<256>,
sol_data::FixedBytes<32>,
)>::encode(&(keccak256(version.as_bytes()).0, chain_id, salt.0))
}
(None, Some(version), Some(chain_id), Some(verifying_contract), None) => {
<(
sol_data::FixedBytes<32>,
sol_data::Uint<256>,
sol_data::Address,
)>::encode(&(
keccak256(version.as_bytes()).0,
chain_id,
verifying_contract,
))
)>::encode(&(keccak256(version.as_bytes()), chain_id, salt))
}
(None, Some(version), Some(chain_id), Some(verifying_contract), None) => <(
sol_data::FixedBytes<32>,
sol_data::Uint<256>,
sol_data::Address,
)>::encode(
&(keccak256(version.as_bytes()), chain_id, verifying_contract),
),
(None, Some(version), Some(chain_id), Some(verifying_contract), Some(salt)) => {
<(
sol_data::FixedBytes<32>,
sol_data::Uint<256>,
sol_data::Address,
sol_data::FixedBytes<32>,
)>::encode(&(
keccak256(version.as_bytes()).0,
keccak256(version.as_bytes()),
chain_id,
verifying_contract,
salt.0,
salt,
))
}
(Some(name), None, None, None, None) => {
<(sol_data::FixedBytes<32>,)>::encode(&(keccak256(name.as_bytes()).0,))
<(sol_data::FixedBytes<32>,)>::encode(&(keccak256(name.as_bytes()),))
}
(Some(name), None, None, None, Some(salt)) => {
<(sol_data::FixedBytes<32>, sol_data::FixedBytes<32>)>::encode(&(
keccak256(name.as_bytes()).0,
salt.0,
keccak256(name.as_bytes()),
salt,
))
}
(Some(name), None, None, Some(verifying_contract), None) => {
<(sol_data::FixedBytes<32>, sol_data::Address)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(name.as_bytes()),
verifying_contract,
))
}
(Some(name), None, None, Some(verifying_contract), Some(salt)) => <(
sol_data::FixedBytes<32>,
sol_data::Address,
sol_data::FixedBytes<32>,
)>::encode(
&(keccak256(name.as_bytes()).0, verifying_contract, salt.0),
),
(Some(name), None, None, Some(verifying_contract), Some(salt)) => {
<(
sol_data::FixedBytes<32>,
sol_data::Address,
sol_data::FixedBytes<32>,
)>::encode(&(keccak256(name.as_bytes()), verifying_contract, salt))
}
(Some(name), None, Some(chain_id), None, None) => {
<(sol_data::FixedBytes<32>, sol_data::Uint<256>)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(name.as_bytes()),
chain_id,
))
}
Expand All @@ -252,14 +243,14 @@ impl Eip712Domain {
sol_data::FixedBytes<32>,
sol_data::Uint<256>,
sol_data::FixedBytes<32>,
)>::encode(&(keccak256(name.as_bytes()).0, chain_id, salt.0))
)>::encode(&(keccak256(name.as_bytes()), chain_id, salt))
}
(Some(name), None, Some(chain_id), Some(verifying_contract), None) => <(
sol_data::FixedBytes<32>,
sol_data::Uint<256>,
sol_data::Address,
)>::encode(
&(keccak256(name.as_bytes()).0, chain_id, verifying_contract),
&(keccak256(name.as_bytes()), chain_id, verifying_contract),
),
(Some(name), None, Some(chain_id), Some(verifying_contract), Some(salt)) => {
<(
Expand All @@ -268,35 +259,35 @@ impl Eip712Domain {
sol_data::Address,
sol_data::FixedBytes<32>,
)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(name.as_bytes()),
chain_id,
verifying_contract,
salt.0,
salt,
))
}
(Some(name), Some(version), None, None, None) => {
<(sol_data::FixedBytes<32>, sol_data::FixedBytes<32>)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(version.as_bytes()).0,
keccak256(name.as_bytes()),
keccak256(version.as_bytes()),
))
}
(Some(name), Some(version), None, None, Some(salt)) => <(
sol_data::FixedBytes<32>,
sol_data::FixedBytes<32>,
sol_data::FixedBytes<32>,
)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(version.as_bytes()).0,
salt.0,
keccak256(name.as_bytes()),
keccak256(version.as_bytes()),
salt,
)),
(Some(name), Some(version), None, Some(verifying_contract), None) => {
<(
sol_data::FixedBytes<32>,
sol_data::FixedBytes<32>,
sol_data::Address,
)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(version.as_bytes()).0,
keccak256(name.as_bytes()),
keccak256(version.as_bytes()),
verifying_contract,
))
}
Expand All @@ -307,19 +298,19 @@ impl Eip712Domain {
sol_data::Address,
sol_data::FixedBytes<32>,
)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(version.as_bytes()).0,
keccak256(name.as_bytes()),
keccak256(version.as_bytes()),
verifying_contract,
salt.0,
salt,
))
}
(Some(name), Some(version), Some(chain_id), None, None) => <(
sol_data::FixedBytes<32>,
sol_data::FixedBytes<32>,
sol_data::Uint<256>,
)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(version.as_bytes()).0,
keccak256(name.as_bytes()),
keccak256(version.as_bytes()),
chain_id,
)),
(Some(name), Some(version), Some(chain_id), None, Some(salt)) => {
Expand All @@ -329,10 +320,10 @@ impl Eip712Domain {
sol_data::Uint<256>,
sol_data::FixedBytes<32>,
)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(version.as_bytes()).0,
keccak256(name.as_bytes()),
keccak256(version.as_bytes()),
chain_id,
salt.0,
salt,
))
}
(Some(name), Some(version), Some(chain_id), Some(verifying_contract), None) => {
Expand All @@ -342,8 +333,8 @@ impl Eip712Domain {
sol_data::Uint<256>,
sol_data::Address,
)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(version.as_bytes()).0,
keccak256(name.as_bytes()),
keccak256(version.as_bytes()),
chain_id,
verifying_contract,
))
Expand All @@ -356,11 +347,11 @@ impl Eip712Domain {
sol_data::Address,
sol_data::FixedBytes<32>,
)>::encode(&(
keccak256(name.as_bytes()).0,
keccak256(version.as_bytes()).0,
keccak256(name.as_bytes()),
keccak256(version.as_bytes()),
chain_id,
verifying_contract,
salt.0,
salt,
))
}
}
Expand Down
9 changes: 6 additions & 3 deletions crates/sol-types/src/types/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

use crate::{token::*, utils, Encodable, Result, SolType, Word};
use alloc::{borrow::Cow, string::String as RustString, vec::Vec};
use alloy_primitives::{keccak256, Address as RustAddress, Function as RustFunction, I256, U256};
use alloy_primitives::{
keccak256, Address as RustAddress, FixedBytes as RustFixedBytes, Function as RustFunction,
I256, U256,
};
use core::{borrow::Borrow, fmt::*, hash::Hash, marker::PhantomData, ops::*};

/// Bool - `bool`
Expand Down Expand Up @@ -450,7 +453,7 @@ impl<const N: usize> SolType for FixedBytes<N>
where
ByteCount<N>: SupportedFixedBytes,
{
type RustType = [u8; N];
type RustType = RustFixedBytes<N>;
type TokenType<'a> = WordToken;

#[inline]
Expand Down Expand Up @@ -480,7 +483,7 @@ where
#[inline]
fn encode_packed_to(rust: &Self::RustType, out: &mut Vec<u8>) {
// write only the first n bytes
out.extend_from_slice(rust);
out.extend_from_slice(rust.as_slice());
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/sol-types/tests/doctests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn event() {
assert_event_signature::<MyEvent>("MyEvent(bytes32,uint256,string,bytes)");
assert!(!MyEvent::ANONYMOUS);
let event = MyEvent {
a: [0x11; 32],
a: [0x11; 32].into(),
b: U256::from(1u64),
c: keccak256("Hello World").into(),
d: Vec::new(),
Expand Down
2 changes: 1 addition & 1 deletion crates/sol-types/tests/doctests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type SolTuple = sol! { tuple(address, bytes, string) };
#[test]
fn types() {
let _ = <sol!(bool)>::encode_single(&true);
let _ = B32::encode_single(&[0; 32]);
let _ = B32::encode_single(&[0; 32].into());
let _ = SolArrayOf::<sol!(bool)>::encode_single(&vec![true, false]);
let _ = SolTuple::encode_single(&(Address::ZERO, vec![0; 32], "hello".to_string()));
}
10 changes: 5 additions & 5 deletions crates/sol-types/tests/sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ fn e2e() {

let a = MyStruct {
a: U256::from(1),
b: [0; 32],
b: [0; 32].into(),
c: Vec::new(),
};

MyTuple::hex_encode(&(a.clone(), [0; 32]));
MyTuple::hex_encode(&(a.clone(), [0; 32].into()));
MyStruct::hex_encode(&a);

LateBinding::<MyStruct>::hex_encode(&(vec![a.clone(), a.clone()], Address::default()));

MyStruct2::hex_encode(&MyStruct2 {
a,
b: [0; 32],
b: [0; 32].into(),
c: vec![],
});

Expand Down Expand Up @@ -202,14 +202,14 @@ fn getters() {
assert_eq!(data1Call::SIGNATURE, "data1(uint256,bool,uint256)");
let _ = data1Return {
_0: U256::ZERO,
_1: [0, 0, 0],
_1: [0, 0, 0].into(),
_2: vec![],
};

assert_eq!(data2Call::SIGNATURE, "data2(uint256,bool)");
let _ = data2Return {
_0: U256::ZERO,
_1: [0, 0, 0],
_1: [0, 0, 0].into(),
_2: vec![],
};

Expand Down

0 comments on commit 6ba6ee3

Please sign in to comment.