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

Starkfelt decimal #58

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::io::Error;

use serde::{Deserialize, Serialize};
use starknet_crypto::{pedersen_hash as starknet_crypto_pedersen_hash, FieldElement};
use web3::types::U256;

use crate::serde_utils::{
bytes_from_hex_str, hex_str_from_bytes, BytesAsHex, NonPrefixedBytesAsHex, PrefixedBytesAsHex,
Expand Down Expand Up @@ -49,6 +50,24 @@ pub fn pedersen_hash_array(felts: &[StarkFelt]) -> StarkHash {
#[serde(try_from = "PrefixedBytesAsHex<32_usize>", into = "PrefixedBytesAsHex<32_usize>")]
pub struct StarkFelt([u8; 32]);

#[derive(Clone, Copy, Eq, PartialEq, Default)]
pub struct StarkFeltAsDecimal(U256);

impl Serialize for StarkFeltAsDecimal {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.0.to_string())
}
}

impl Display for StarkFeltAsDecimal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0.to_string())
}
}

impl StarkFelt {
/// Returns a new [`StarkFelt`].
pub fn new(bytes: [u8; 32]) -> Result<StarkFelt, StarknetApiError> {
Expand Down Expand Up @@ -201,6 +220,18 @@ impl TryFrom<StarkFelt> for usize {
}
}

impl From<StarkFelt> for U256 {
fn from(felt: StarkFelt) -> Self {
web3::types::U256::from_big_endian(&felt.0)
}
}

impl From<StarkFelt> for StarkFeltAsDecimal {
fn from(felt: StarkFelt) -> Self {
StarkFeltAsDecimal(U256::from(felt))
}
}

impl Debug for StarkFelt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.str_format(f)
Expand Down
15 changes: 14 additions & 1 deletion src/hash_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::hash::{pedersen_hash, pedersen_hash_array, StarkFelt};
use crate::hash::{pedersen_hash, pedersen_hash_array, StarkFelt, StarkFeltAsDecimal};
use crate::stark_felt;

#[test]
Expand Down Expand Up @@ -67,3 +67,16 @@ fn hash_serde() {
assert_eq!(bytes, d.0);
}
}

#[test]
fn stark_felt_from_hex_to_decimal() {
let felt = stark_felt!("0x000000000000d");
assert_eq!(StarkFeltAsDecimal::from(felt).to_string(), "13");

let felt = stark_felt!("0x264d6571d5f186bab2a9d5d8d30aa38bf5502bc4354870edbfe194c6f655c9b");
let felt_decimal = StarkFeltAsDecimal::from(felt);
assert_eq!(
felt_decimal.to_string(),
"1082789725971120866445625682125121757273101071727151005357998861560691645595"
);
}