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: use FixedBytes for sol_data::FixedBytes #276

Merged
merged 1 commit into from
Sep 19, 2023
Merged
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
2 changes: 1 addition & 1 deletion crates/dyn-abi/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl DynSolType {
}
(Self::Bytes, DynToken::PackedSeq(buf)) => Ok(DynSolValue::Bytes(buf.to_vec())),
(Self::FixedBytes(size), DynToken::Word(word)) => Ok(DynSolValue::FixedBytes(
sol_data::FixedBytes::<32>::detokenize(word.into()).into(),
sol_data::FixedBytes::<32>::detokenize(word.into()),
*size,
)),
// cheating here, but it's ok
Expand Down
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
2 changes: 1 addition & 1 deletion crates/sol-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
//! // `crate::SolStruct`. This means you get eip-712 signing for freeeeee
//! let my_struct = MyStruct {
//! a: U256::from(1),
//! b: [0; 32],
//! b: [0; 32].into(),
//! c: vec![Default::default()],
//! };
//!
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/src/types/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub trait Encodable<T: ?Sized + SolType> {
///
/// // This is the native rust representation of a Solidity type!
/// // How cool is that!
/// const MY_STRUCT: MyStruct = MyStruct { a: true, b: [0x01, 0x02] };
/// const MY_STRUCT: MyStruct = MyStruct { a: true, b: alloy_primitives::FixedBytes([0x01, 0x02]) };
/// ```
pub trait SolType {
/// The corresponding Rust type.
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-types/tests/doctests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ 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(),
c: keccak256("Hello World"),
d: Vec::new(),
};
// topics are `(SELECTOR, a, keccak256(c))`
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()));
}
Loading