Skip to content

Commit a1e4fc0

Browse files
committed
feat(target_chains/starknet): add utils for decoding signed integers, move array_felt252_to_bytes31 to utils
1 parent 67132c0 commit a1e4fc0

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

target_chains/starknet/contracts/src/util.cairo

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,67 @@ pub fn u64_byte_reverse(value: u64) -> u64 {
6262
pub trait UnwrapWithFelt252<T, E> {
6363
fn unwrap_with_felt252(self: Result<T, E>) -> T;
6464
}
65+
66+
/// Reinterpret `u64` as `i64` as if it was a two's complement binary representation.
67+
pub fn u64_as_i64(value: u64) -> i64 {
68+
if value < 0x8000000000000000 {
69+
value.try_into().unwrap()
70+
} else {
71+
let value: i128 = value.into();
72+
(value - 0x10000000000000000).try_into().unwrap()
73+
}
74+
}
75+
76+
/// Reinterpret `u32` as `i32` as if it was a two's complement binary representation.
77+
pub fn u32_as_i32(value: u32) -> i32 {
78+
if value < 0x80000000 {
79+
value.try_into().unwrap()
80+
} else {
81+
let value: i64 = value.into();
82+
(value - 0x100000000).try_into().unwrap()
83+
}
84+
}
85+
86+
pub fn array_felt252_to_bytes31(mut input: Array<felt252>) -> Array<bytes31> {
87+
let mut output = array![];
88+
loop {
89+
match input.pop_front() {
90+
Option::Some(v) => { output.append(v.try_into().unwrap()); },
91+
Option::None => { break; },
92+
}
93+
};
94+
output
95+
}
96+
97+
#[cfg(test)]
98+
mod tests {
99+
use super::{u64_as_i64, u32_as_i32};
100+
101+
#[test]
102+
fn test_u64_as_i64() {
103+
assert!(u64_as_i64(0) == 0);
104+
assert!(u64_as_i64(1) == 1);
105+
assert!(u64_as_i64(2) == 2);
106+
assert!(u64_as_i64(3) == 3);
107+
assert!(u64_as_i64(9223372036854775806) == 9223372036854775806);
108+
assert!(u64_as_i64(9223372036854775807) == 9223372036854775807);
109+
assert!(u64_as_i64(9223372036854775808) == -9223372036854775808);
110+
assert!(u64_as_i64(9223372036854775809) == -9223372036854775807);
111+
assert!(u64_as_i64(18446744073709551614) == -2);
112+
assert!(u64_as_i64(18446744073709551615) == -1);
113+
}
114+
115+
#[test]
116+
fn test_u32_as_i32() {
117+
assert!(u32_as_i32(0) == 0);
118+
assert!(u32_as_i32(1) == 1);
119+
assert!(u32_as_i32(2) == 2);
120+
assert!(u32_as_i32(3) == 3);
121+
assert!(u32_as_i32(2147483646) == 2147483646);
122+
assert!(u32_as_i32(2147483647) == 2147483647);
123+
assert!(u32_as_i32(2147483648) == -2147483648);
124+
assert!(u32_as_i32(2147483649) == -2147483647);
125+
assert!(u32_as_i32(4294967294) == -2);
126+
assert!(u32_as_i32(4294967295) == -1);
127+
}
128+
}

target_chains/starknet/contracts/tests/wormhole.cairo

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use snforge_std::{declare, ContractClassTrait, start_prank, stop_prank, CheatTarget};
22
use pyth::wormhole::{IWormholeDispatcher, IWormholeDispatcherTrait, ParseAndVerifyVmError};
33
use pyth::reader::{ByteArray, ByteArrayImpl, ReaderImpl};
4-
use pyth::util::UnwrapWithFelt252;
4+
use pyth::util::{UnwrapWithFelt252, array_felt252_to_bytes31};
55
use core::starknet::ContractAddress;
66
use core::panic_with_felt252;
77

@@ -94,17 +94,6 @@ fn test_submit_guardian_set_rejects_empty() {
9494
dispatcher.submit_new_guardian_set(1, array![]).unwrap_with_felt252();
9595
}
9696

97-
fn array_left252_to_bytes31(mut input: Array<felt252>) -> Array<bytes31> {
98-
let mut output = array![];
99-
loop {
100-
match input.pop_front() {
101-
Option::Some(v) => { output.append(v.try_into().unwrap()); },
102-
Option::None => { break; },
103-
}
104-
};
105-
output
106-
}
107-
10897
fn deploy(owner: ContractAddress, guardians: Array<felt252>) -> IWormholeDispatcher {
10998
let mut args = array![];
11099
(owner, guardians).serialize(ref args);
@@ -300,5 +289,5 @@ fn good_vm1() -> ByteArray {
300289
52685537088250779930155363779405986390839624071318818148325576008719597568,
301290
14615204155786886573933667335033405822686404253588533,
302291
];
303-
ByteArrayImpl::new(array_left252_to_bytes31(bytes), 22)
292+
ByteArrayImpl::new(array_felt252_to_bytes31(bytes), 22)
304293
}

0 commit comments

Comments
 (0)