Skip to content

Commit

Permalink
Merge pull request #400 from icon-project/fix/rlp_eth_changes
Browse files Browse the repository at this point in the history
fix: make changes for eth abi sui
  • Loading branch information
gcranju authored Oct 23, 2024
2 parents cb69b17 + 3596518 commit 4e08258
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 47 deletions.
6 changes: 6 additions & 0 deletions contracts/sui/libs/sui_rlp/sources/decoder.move
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,11 @@ module sui_rlp::decoder {
bcs::peel_address(&mut bcs)
}

public fun decode_bool(vec:&vector<u8>):bool{
let val= *vector::borrow(vec,0);
val==1

}


}
37 changes: 17 additions & 20 deletions contracts/sui/libs/sui_rlp/sources/encoder.move
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,19 @@ module sui_rlp::encoder {
let total_length = result.length();
let len=vector::length(&result);

if( total_length<= 55){
encoded_list=encode_length(len,0xc0);
vector::append(&mut encoded_list,result);
if( total_length<= 55){
encoded_list=encode_length(len,0xc0);
vector::append(&mut encoded_list,result);

} else {
let length_bytes = utils::to_bytes_u64(len);
let prefix = (0xf7 + vector::length(&length_bytes)) as u8;
//std::debug::print(&b"PREFIX".to_string());
//std::debug::print(&prefix);
vector::push_back(&mut encoded_list, prefix);
//std::debug::print(&encoded_list);
vector::append(&mut encoded_list, length_bytes);
//std::debug::print(&encoded_list);
} else {
let length_bytes = utils::to_bytes_u64(len);
let prefix = (0xf7 + vector::length(&length_bytes)) as u8;
vector::push_back(&mut encoded_list, prefix);
vector::append(&mut encoded_list, length_bytes);
vector::append(&mut encoded_list, result);

vector::append(&mut encoded_list, result);
//std::debug::print(&encoded_list);


}
}

}else{
vector::push_back(&mut encoded_list,0xc0);
Expand Down Expand Up @@ -132,8 +126,11 @@ module sui_rlp::encoder {
let vec= bcs::to_bytes(addr);
encode(&vec)
}
}




public fun encode_bool(val:bool):vector<u8>{
if(val==true){
return vector<u8>[1]
};
vector<u8>[0]
}
}
58 changes: 33 additions & 25 deletions contracts/sui/libs/sui_rlp/sources/utils.move
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module sui_rlp::utils {
use std::vector::{Self};
use std::string::{Self,String};
use std::bcs;
public fun to_bytes_u32(number: u32): vector<u8> {
let mut bytes: vector<u8> = vector::empty();
let mut i:u8=0;
Expand Down Expand Up @@ -33,20 +34,6 @@ module sui_rlp::utils {
result
}

public fun to_bytes_u64(number: u64): vector<u8> {
let mut bytes: vector<u8> = vector::empty();
let mut i:u8=0;
while(i < 8){
let val =( (number>>(i * 8) & 0xFF) as u8) ;
vector::push_back(&mut bytes,val);
i=i+1;
};
bytes.reverse();
let mut prefix = vector<u8>[0];
prefix.append(truncate_zeros(&bytes));
prefix
}

fun truncate_zeros(bytes: &vector<u8>): vector<u8> {
let mut i = 0;
let mut started = false;
Expand Down Expand Up @@ -85,21 +72,42 @@ module sui_rlp::utils {
result
}



// Convert u128 to bytes
public fun to_bytes_u128(number: u128): vector<u8> {
let mut bytes: vector<u8> = vector::empty();
let mut i:u8=0;
public fun to_bytes_u128(number:u128):vector<u8>{
let mut right:u128= 128;
let mut i=1;
while(i < 16){
let val = ((number>>(i * 8) & 0xFF) as u8) ;
vector::push_back(&mut bytes,val);
right=right << 8;
i=i+1;

};
let mut bytes=bcs::to_bytes(&number);
bytes.reverse();
if (number < right){
truncate_zeros(&bytes)
}else {
let mut prefix = vector<u8>[0];
prefix.append(truncate_zeros(&bytes));
prefix
}
}

public fun to_bytes_u64(number:u64):vector<u8>{
let mut right:u64= 128;
let mut i=1;
while(i < 8){
right=right << 8;
i=i+1;

};
let mut bytes=bcs::to_bytes(&number);
bytes.reverse();
let mut prefix = vector<u8>[0];
prefix.append(truncate_zeros(&bytes));
prefix
if (number < right){
truncate_zeros(&bytes)
}else {
let mut prefix = vector<u8>[0];
prefix.append(truncate_zeros(&bytes));
prefix
}
}

// Convert bytes to u128
Expand Down
4 changes: 2 additions & 2 deletions contracts/sui/xcall/sources/types/message_result.move
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module xcall::message_result_tests {
let msg= message_result::create(1,message_result::success(),vector::empty());
let encoded= message_result::encode(&msg);
std::debug::print(&encoded);
assert!(encoded==x"c58200010180",0x01);
assert!(encoded==x"c3010180",0x01);
let decoded=message_result::decode(&encoded);
assert!(decoded==msg,0x01);

Expand All @@ -99,7 +99,7 @@ module xcall::message_result_tests {
let msg= message_result::create(2,message_result::failure(),vector::empty());
let encoded= message_result::encode(&msg);
std::debug::print(&encoded);
assert!(encoded==x"c58200020080",0x01);
assert!(encoded==x"c3020080",0x01);
let decoded=message_result::decode(&encoded);
assert!(decoded==msg,0x01);

Expand Down

0 comments on commit 4e08258

Please sign in to comment.