Skip to content

Commit

Permalink
rlp issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sagars committed Sep 19, 2024
1 parent 4b08999 commit 6b5818a
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions libs/soroban-rlp/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use soroban_sdk::{
bytes,
xdr::{FromXdr, ToXdr},
Bytes, Env, String,
};

pub fn u32_to_bytes(env: &Env, number: u32) -> Bytes {
let mut bytes = Bytes::new(&env);
let mut bytes = bytes!(&env, 0x00);
let mut i = 3;
let mut leading_zero = true;
while i >= 0 {
let val = (number >> (i * 8) & 0xff) as u8;
if val > 0 {
if val > 0 || !leading_zero {
leading_zero = false;
bytes.push_back(val);
}

Check warning on line 16 in libs/soroban-rlp/src/utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/soroban-rlp/src/utils.rs#L7-L16

Added lines #L7 - L16 were not covered by tests

Expand All @@ -26,11 +29,13 @@ pub fn bytes_to_u32(bytes: Bytes) -> u32 {
}

Check warning on line 29 in libs/soroban-rlp/src/utils.rs

View check run for this annotation

Codecov / codecov/patch

libs/soroban-rlp/src/utils.rs#L23-L29

Added lines #L23 - L29 were not covered by tests

pub fn u64_to_bytes(env: &Env, number: u64) -> Bytes {
let mut bytes = Bytes::new(&env);
let mut bytes = bytes!(&env, 0x00);
let mut i = 7;
let mut leading_zero = true;
while i >= 0 {
let val = (number >> (i * 8) & 0xff) as u8;
if val > 0 {
if val > 0 || !leading_zero {
leading_zero = false;
bytes.push_back(val);
}

Expand All @@ -48,11 +53,13 @@ pub fn bytes_to_u64(bytes: Bytes) -> u64 {
}

pub fn u128_to_bytes(env: &Env, number: u128) -> Bytes {
let mut bytes = Bytes::new(&env);
let mut bytes = bytes!(&env, 0x00);
let mut i = 15;
let mut leading_zero = true;
while i >= 0 {
let val = (number >> (i * 8) & 0xff) as u8;
if val > 0 {
if val > 0 || !leading_zero {
leading_zero = false;
bytes.push_back(val);
}

Expand Down Expand Up @@ -101,4 +108,4 @@ pub fn bytes_to_string(env: &Env, bytes: Bytes) -> String {
bytes_xdr.set(3, 14);

String::from_xdr(&env, &bytes_xdr).unwrap()
}
}

0 comments on commit 6b5818a

Please sign in to comment.