Skip to content

Commit

Permalink
[Rust]: Fix BlockchainType deserialization
Browse files Browse the repository at this point in the history
* Remove `CoinType` arguments from FFI functions
  • Loading branch information
satoshiotomakan committed Nov 21, 2023
1 parent f90e975 commit 14589d1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
3 changes: 2 additions & 1 deletion rust/tw_coin_registry/src/blockchain_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ pub enum BlockchainType {
NativeEvmos,
NativeInjective,
Ronin,
Unsupported,
Thorchain,
// end_of_blockchain_type - USED TO GENERATE CODE
#[serde(other)]
Unsupported,
}

impl BlockchainType {
Expand Down
15 changes: 10 additions & 5 deletions rust/wallet_core_rs/src/ffi/ethereum/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use tw_misc::try_or_else;
/// \return serialized `EthereumAbi::Proto::ContractCallDecodingOutput`.
#[no_mangle]
pub unsafe extern "C" fn tw_ethereum_abi_decode_contract_call(
coin: CoinType,
coin: u32,
input: *const TWData,
) -> *mut TWData {
let coin = try_or_else!(CoinType::try_from(coin), std::ptr::null_mut);
let input_data = try_or_else!(TWData::from_ptr_as_ref(input), std::ptr::null_mut);
let evm_dispatcher = try_or_else!(evm_dispatcher(coin), std::ptr::null_mut);

Expand All @@ -39,9 +40,10 @@ pub unsafe extern "C" fn tw_ethereum_abi_decode_contract_call(
/// \return The serialized data of a `TW.EthereumAbi.Proto.ParamsDecodingOutput` proto object.
#[no_mangle]
pub unsafe extern "C" fn tw_ethereum_abi_decode_params(
coin: CoinType,
coin: u32,
input: *const TWData,
) -> *mut TWData {
let coin = try_or_else!(CoinType::try_from(coin), std::ptr::null_mut);
let input_data = try_or_else!(TWData::from_ptr_as_ref(input), std::ptr::null_mut);
let evm_dispatcher = try_or_else!(evm_dispatcher(coin), std::ptr::null_mut);

Expand All @@ -58,9 +60,10 @@ pub unsafe extern "C" fn tw_ethereum_abi_decode_params(
/// \return function type signature as a Non-null string.
#[no_mangle]
pub unsafe extern "C" fn tw_ethereum_abi_function_get_signature(
coin: CoinType,
coin: u32,
input: *const TWData,
) -> *mut TWString {
let coin = try_or_else!(CoinType::try_from(coin), std::ptr::null_mut);
let input_data = try_or_else!(TWData::from_ptr_as_ref(input), || TWString::new()
.into_ptr());
let evm_dispatcher = try_or_else!(evm_dispatcher(coin), || TWString::new().into_ptr());
Expand All @@ -78,9 +81,10 @@ pub unsafe extern "C" fn tw_ethereum_abi_function_get_signature(
/// \return The serialized data of a `TW.EthereumAbi.Proto.FunctionEncodingOutput` proto object.
#[no_mangle]
pub unsafe extern "C" fn tw_ethereum_abi_encode_function(
coin: CoinType,
coin: u32,
input: *const TWData,
) -> *mut TWData {
let coin = try_or_else!(CoinType::try_from(coin), std::ptr::null_mut);
let input_data = try_or_else!(TWData::from_ptr_as_ref(input), std::ptr::null_mut);
let evm_dispatcher = try_or_else!(evm_dispatcher(coin), std::ptr::null_mut);

Expand All @@ -97,9 +101,10 @@ pub unsafe extern "C" fn tw_ethereum_abi_encode_function(
/// \return The serialized data of a `TW.EthereumAbi.Proto.ValueDecodingOutput` proto object.
#[no_mangle]
pub unsafe extern "C" fn tw_ethereum_abi_decode_value(
coin: CoinType,
coin: u32,
input: *const TWData,
) -> *mut TWData {
let coin = try_or_else!(CoinType::try_from(coin), std::ptr::null_mut);
let input_data = try_or_else!(TWData::from_ptr_as_ref(input), std::ptr::null_mut);
let evm_dispatcher = try_or_else!(evm_dispatcher(coin), std::ptr::null_mut);

Expand Down
6 changes: 2 additions & 4 deletions rust/wallet_core_rs/src/ffi/ethereum/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ use tw_misc::try_or_else;
/// \param input Non-null serialized `EthereumRlp::Proto::EncodingInput`.
/// \return serialized `EthereumRlp::Proto::EncodingOutput`.
#[no_mangle]
pub unsafe extern "C" fn tw_ethereum_rlp_encode(
coin: CoinType,
input: *const TWData,
) -> *mut TWData {
pub unsafe extern "C" fn tw_ethereum_rlp_encode(coin: u32, input: *const TWData) -> *mut TWData {
let coin = try_or_else!(CoinType::try_from(coin), std::ptr::null_mut);
let input_data = try_or_else!(TWData::from_ptr_as_ref(input), std::ptr::null_mut);
let evm_dispatcher = try_or_else!(evm_dispatcher(coin), std::ptr::null_mut);
evm_dispatcher
Expand Down
10 changes: 5 additions & 5 deletions rust/wallet_core_rs/tests/ethereum_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn test_ethereum_abi_decode_contract_call() {
let input_data = TWDataHelper::create(serialize(&input).unwrap());

let output_data = TWDataHelper::wrap(unsafe {
tw_ethereum_abi_decode_contract_call(CoinType::Ethereum, input_data.ptr())
tw_ethereum_abi_decode_contract_call(CoinType::Ethereum as u32, input_data.ptr())
})
.to_vec()
.expect("!tw_ethereum_abi_decode_contract_call returned nullptr");
Expand Down Expand Up @@ -97,7 +97,7 @@ fn test_ethereum_abi_decode_params() {
let input_data = TWDataHelper::create(serialize(&input).unwrap());

let output_data = TWDataHelper::wrap(unsafe {
tw_ethereum_abi_decode_params(CoinType::Ethereum, input_data.ptr())
tw_ethereum_abi_decode_params(CoinType::Ethereum as u32, input_data.ptr())
})
.to_vec()
.expect("!tw_ethereum_abi_decode_params returned nullptr");
Expand Down Expand Up @@ -134,7 +134,7 @@ fn test_ethereum_abi_function_get_signature() {
let input_data = TWDataHelper::create(serialize(&input).unwrap());

let actual = TWStringHelper::wrap(unsafe {
tw_ethereum_abi_function_get_signature(CoinType::Ethereum, input_data.ptr())
tw_ethereum_abi_function_get_signature(CoinType::Ethereum as u32, input_data.ptr())
})
.to_string()
.expect("!tw_ethereum_abi_function_get_signature returned nullptr");
Expand All @@ -155,7 +155,7 @@ fn test_ethereum_abi_encode_function() {
let input_data = TWDataHelper::create(serialize(&input).unwrap());

let output_data = TWDataHelper::wrap(unsafe {
tw_ethereum_abi_encode_function(CoinType::Ethereum, input_data.ptr())
tw_ethereum_abi_encode_function(CoinType::Ethereum as u32, input_data.ptr())
})
.to_vec()
.expect("!tw_ethereum_abi_encode_function returned nullptr");
Expand All @@ -181,7 +181,7 @@ fn test_ethereum_abi_decode_value() {
let input_data = TWDataHelper::create(serialize(&input).unwrap());

let output_data = TWDataHelper::wrap(unsafe {
tw_ethereum_abi_decode_value(CoinType::Ethereum, input_data.ptr())
tw_ethereum_abi_decode_value(CoinType::Ethereum as u32, input_data.ptr())
})
.to_vec()
.expect("!tw_ethereum_abi_decode_value returned nullptr");
Expand Down
9 changes: 5 additions & 4 deletions rust/wallet_core_rs/tests/ethereum_rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ fn test_ethereum_rlp() {
let input = RlpProto::EncodingInput { item: Some(item) };
let input_data = TWDataHelper::create(serialize(&input).unwrap());

let output_data =
TWDataHelper::wrap(unsafe { tw_ethereum_rlp_encode(CoinType::Ethereum, input_data.ptr()) })
.to_vec()
.expect("!tw_ethereum_rlp_encode returned nullptr");
let output_data = TWDataHelper::wrap(unsafe {
tw_ethereum_rlp_encode(CoinType::Ethereum as u32, input_data.ptr())
})
.to_vec()
.expect("!tw_ethereum_rlp_encode returned nullptr");
let output: RlpProto::EncodingOutput =
deserialize(&output_data).expect("!tw_ethereum_rlp_encode returned an invalid output");

Expand Down

0 comments on commit 14589d1

Please sign in to comment.