Skip to content

Commit

Permalink
Fix u256 primitive serde (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody authored Oct 14, 2023
1 parent b29c98e commit 7d2c8c9
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions crates/dojo-types/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl Primitive {
let mut buffer = [0u8; 32];
let value0_bytes = value[0].to_bytes_be();
let value1_bytes = value[1].to_bytes_be();
buffer[..16].copy_from_slice(&value0_bytes);
buffer[16..].copy_from_slice(&value1_bytes);
buffer[16..].copy_from_slice(&value0_bytes[16..]);
buffer[..16].copy_from_slice(&value1_bytes[16..]);
Ok(format!("'0x{}'", hex::encode(buffer)))
}
}
Expand Down Expand Up @@ -131,8 +131,8 @@ impl Primitive {
let value0_bytes = value0.to_bytes_be();
let value1_bytes = value1.to_bytes_be();
let mut bytes = [0u8; 32];
bytes[..16].copy_from_slice(&value0_bytes);
bytes[16..].copy_from_slice(&value1_bytes);
bytes[16..].copy_from_slice(&value0_bytes[16..]);
bytes[..16].copy_from_slice(&value1_bytes[16..]);
*value = Some(U256::from_be_bytes(bytes));
Ok(())
}
Expand Down Expand Up @@ -177,8 +177,8 @@ impl Primitive {
Primitive::U256(value) => value
.map(|v| {
let bytes: [u8; 32] = v.to_be_bytes();
let value0_slice = &bytes[..16];
let value1_slice = &bytes[16..];
let value0_slice = &bytes[16..];
let value1_slice = &bytes[..16];
let mut value0_array = [0u8; 32];
let mut value1_array = [0u8; 32];
value0_array[16..].copy_from_slice(value0_slice);
Expand All @@ -200,3 +200,38 @@ impl Primitive {
}
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use crypto_bigint::U256;
use starknet::core::types::FieldElement;

use super::Primitive;

#[test]
fn test_u256() {
let primitive = Primitive::U256(Some(U256::from_be_hex(
"aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccdddddddddddddddd",
)));
let sql_value = primitive.to_sql_value().unwrap();
let serialized = primitive.serialize().unwrap();

let mut deserialized = primitive;
deserialized.deserialize(&mut serialized.clone()).unwrap();

assert_eq!(
sql_value,
"'0xaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccdddddddddddddddd'"
);
assert_eq!(
serialized,
vec![
FieldElement::from_str("0xccccccccccccccccdddddddddddddddd").unwrap(),
FieldElement::from_str("0xaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb").unwrap()
]
);
assert_eq!(deserialized, primitive)
}
}

0 comments on commit 7d2c8c9

Please sign in to comment.