Skip to content
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
9 changes: 2 additions & 7 deletions packages/utilities/src/renderer/metadata.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use game_components_embeddable_game_standard::registry::interface::GameMetadata;
use game_components_embeddable_game_standard::token::structs::TokenMetadata;
use graffiti::json::JsonImpl;
use starknet::{ContractAddress, get_block_timestamp};
use crate::utils::encoding::{U256BytesUsedTraitImpl, bytes_base64_encode, felt252_to_byte_array};
use crate::utils::encoding::{bytes_base64_encode, felt252_to_byte_array};

fn create_trait(name: ByteArray, value: ByteArray) -> ByteArray {
JsonImpl::new().add("trait", name).add("value", value).build()
Expand Down Expand Up @@ -104,12 +104,7 @@ pub fn create_custom_metadata(

// Optional player name trait
if !player_name.is_zero() {
let mut _player_name = Default::default();
_player_name
.append_word(
player_name, U256BytesUsedTraitImpl::bytes_used(player_name.into()).into(),
);
attributes.append(create_trait("Player Name", _player_name));
attributes.append(create_trait("Player Name", felt252_to_byte_array(player_name)));
}

// Add dynamic game details as traits
Expand Down
8 changes: 2 additions & 6 deletions packages/utilities/src/renderer/svg.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use game_components_embeddable_game_standard::minigame::extensions::settings::st
use game_components_embeddable_game_standard::registry::interface::GameMetadata;
use game_components_embeddable_game_standard::token::structs::TokenMetadata;
use starknet::get_block_timestamp;
use crate::utils::encoding::{U256BytesUsedTraitImpl, felt252_to_byte_array};
use crate::utils::encoding::felt252_to_byte_array;

fn create_text(
text: ByteArray,
Expand Down Expand Up @@ -250,11 +250,7 @@ pub fn create_default_svg(

let mut _player_name: ByteArray = Default::default();
if player_name.is_non_zero() {
_player_name
.append_word(
player_name, U256BytesUsedTraitImpl::bytes_used(player_name.into()).into(),
);
_player_name = uri_encode(_player_name);
_player_name = uri_encode(felt252_to_byte_array(player_name));
} else {
_player_name = "---";
}
Expand Down
11 changes: 10 additions & 1 deletion packages/utilities/src/utils/encoding.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,16 @@ pub impl U256BytesUsedTraitImpl of BytesUsedTrait<u256> {
pub fn felt252_to_byte_array(value: felt252) -> ByteArray {
let mut result: ByteArray = Default::default();
if value.is_non_zero() {
result.append_word(value, U256BytesUsedTraitImpl::bytes_used(value.into()).into());
let len: u8 = U256BytesUsedTraitImpl::bytes_used(value.into());
// ByteArray.append_word requires len <= 31. A felt252 encodes at most
// 31 bytes of short-string data, but bytes_used(u256) can return 32 for
// large values (high limb non-zero). Cap at 31 to prevent 'bad append len'.
let len: usize = if len > 31 {
31
} else {
len.into()
};
result.append_word(value, len);
Comment on lines +193 to +198

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For conciseness and to avoid variable shadowing, you can combine the length capping and the function call into a single expression. This makes the code slightly more direct and idiomatic.

        result.append_word(value, if len > 31 { 31 } else { len.into() });

}
result
}
Expand Down
52 changes: 51 additions & 1 deletion packages/utilities/src/utils/tests/test_encoding.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// All functions are pure - no contract deployment needed.

use core::num::traits::Bounded;
use crate::utils::encoding::{U256BytesUsedTraitImpl, bytes_base64_encode, u128_to_ascii_felt};
use crate::utils::encoding::{
U256BytesUsedTraitImpl, bytes_base64_encode, felt252_to_byte_array, u128_to_ascii_felt,
};

// ==============================================================================
// BASE64 ENCODING TESTS
Expand Down Expand Up @@ -647,3 +649,51 @@ fn test_u128_to_ascii_felt_panics_over_31_digits() {
// u128 max = 340282366920938463463374607431768211455 (39 digits)
u128_to_ascii_felt(340282366920938463463374607431768211455);
}

// ==============================================================================
// FELT252 TO BYTE ARRAY TESTS
// ==============================================================================

#[test]
fn test_felt252_to_byte_array_zero() {
let result = felt252_to_byte_array(0);
assert!(result.len() == 0, "Zero should produce empty ByteArray");
}

#[test]
fn test_felt252_to_byte_array_short_string() {
let result = felt252_to_byte_array('hello');
assert!(result == "hello", "Should convert short string correctly");
}

#[test]
fn test_felt252_to_byte_array_max_short_string() {
// 31-byte short string (max that fits in a single felt252 word)
let result = felt252_to_byte_array('1234567890123456789012345678901');
assert!(result.len() == 31, "31-byte string should produce 31-byte ByteArray");
}

#[test]
fn test_felt252_to_byte_array_large_value_no_panic() {
// A felt252 value large enough that U256BytesUsedTraitImpl::bytes_used
// returns 32, which previously caused 'bad append len' panic.
// The Starknet prime P ≈ 2^251, so any felt252 where the u256
// representation has a non-zero high limb AND bytes_used(high) >= 16
// would return 32 bytes. We use P-1 (max valid felt252).
let large: felt252 = (-1); // P - 1, the largest valid felt252
let result = felt252_to_byte_array(large);
// Should not panic — length is capped at 31
assert!(result.len() == 31, "Large felt252 should produce 31-byte ByteArray");
}

#[test]
#[fuzzer(runs: 256)]
fn test_fuzz_felt252_to_byte_array_no_panic(value: felt252) {
// Should never panic regardless of input
let result = felt252_to_byte_array(value);
if value == 0 {
assert!(result.len() == 0, "Zero should produce empty ByteArray");
} else {
assert!(result.len() > 0 && result.len() <= 31, "Non-zero should produce 1-31 bytes");
}
}