diff --git a/.gitignore b/.gitignore index 5f17eeb353f..f001bd0c218 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,7 @@ include/TrustWalletCore/TWEthereumChainID.h # Generated include/TrustWalletCore/TWTONAddressConverter.h +include/TrustWalletCore/TWFFITest.h # Wasm emsdk/ diff --git a/codegen-v2/src/codegen/cpp/code_gen.rs b/codegen-v2/src/codegen/cpp/code_gen.rs index 9206739182d..aaf428fd2d7 100644 --- a/codegen-v2/src/codegen/cpp/code_gen.rs +++ b/codegen-v2/src/codegen/cpp/code_gen.rs @@ -55,7 +55,17 @@ fn convert_rust_type_to_cpp(ty: &str) -> String { { format!("{} *_Nullable", &captures[1]) } else { - ty.to_string() + match ty { + "u8" => "uint8_t".to_string(), + "u16" => "uint16_t".to_string(), + "u32" => "uint32_t".to_string(), + "u64" => "uint64_t".to_string(), + "i8" => "int8_t".to_string(), + "i16" => "int16_t".to_string(), + "i32" => "int32_t".to_string(), + "i64" => "int64_t".to_string(), + _ => ty.to_string(), + } } } @@ -199,7 +209,7 @@ fn generate_return_type(func: &TWStaticFunction, converted_args: &Vec) - .map_err(|e| BadFormat(e.to_string()))?; } _ => { - writeln!(&mut return_string, " return Rust::{}", func.rust_name) + write!(&mut return_string, " return Rust::{}", func.rust_name) .map_err(|e| BadFormat(e.to_string()))?; return_string += generate_function_call(&converted_args)?.as_str(); } diff --git a/rust/chains/tw_sui/src/transaction/raw_types.rs b/rust/chains/tw_sui/src/transaction/raw_types.rs index b5191de7b6c..051dfbb1908 100644 --- a/rust/chains/tw_sui/src/transaction/raw_types.rs +++ b/rust/chains/tw_sui/src/transaction/raw_types.rs @@ -64,6 +64,7 @@ pub enum InputObjectArg { #[serde(rename_all = "camelCase")] Receiving { digest: String, + #[serde(with = "as_string")] version: u64, object_id: String, }, diff --git a/rust/chains/tw_sui/tests/aftermath_json_support.rs b/rust/chains/tw_sui/tests/aftermath_json_support.rs index 3315c9879be..795acdf0409 100644 --- a/rust/chains/tw_sui/tests/aftermath_json_support.rs +++ b/rust/chains/tw_sui/tests/aftermath_json_support.rs @@ -163,3 +163,69 @@ fn test_raw_json_with_all_transactions() { let result = TransactionBuilder::raw_json(raw_json, 0, 0); assert!(result.is_ok()); } + +#[test] +fn test_raw_json_with_rall_inputs() { + let raw_json = r#" + { + "version": 1, + "sender": "0x1", + "expiration": null, + "gasConfig": { + "budget": "30216120", + "price": "750", + "payment": [] + }, + "inputs": [ + { + "kind": "Input", + "index": 1, + "value": { + "Pure": [ + 89, + 93, + 60, + 0, + 0, + 0, + 0, + 0 + ] + }, + "type": "pure" + }, + { + "kind": "Input", + "index": 2, + "value": { + "Object": { + "Shared": { + "mutable": false, + "initialSharedVersion": "228660837", + "objectId": "0x72fbc93a45192357c87557fe73ea62fe5968efb5482834e9243f850377251534" + } + } + }, + "type": "object" + }, + { + "kind": "Input", + "index": 3, + "value": { + "Object": { + "Receiving": { + "digest": "3ruaZRSLjur2FPsUmcueZF91umNEckEitmxTNrgHrJFc", + "version": "488916618", + "objectId": "0x72fbc93a45192357c87557fe73ea62fe5968efb5482834e9243f850377251534" + } + } + }, + "type": "object" + } + ], + "transactions": [] + } + "#; + let result = TransactionBuilder::raw_json(raw_json, 0, 0); + assert!(result.is_ok()); +} diff --git a/rust/wallet_core_rs/src/ffi/ffi_test.rs b/rust/wallet_core_rs/src/ffi/ffi_test.rs new file mode 100644 index 00000000000..c8e7739b7ae --- /dev/null +++ b/rust/wallet_core_rs/src/ffi/ffi_test.rs @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright © 2017 Trust Wallet. + +#![allow(clippy::missing_safety_doc)] + +use tw_macros::tw_ffi; +use tw_memory::ffi::{tw_string::TWString, Nonnull, NullableMut, RawPtrTrait}; +use tw_misc::try_or_else; + +/// Sum two unsigned integers of 8 bits +#[tw_ffi(ty = static_function, class = TWFFITest, name = UnsignedSumU8)] +#[no_mangle] +pub unsafe extern "C" fn tw_ffi_test_unsigned_sum_u8(a: u8, b: u8) -> u8 { + a.checked_add(b).unwrap_or(u8::MAX) +} + +/// Sum two unsigned integers of 16 bits +#[tw_ffi(ty = static_function, class = TWFFITest, name = UnsignedSumU16)] +#[no_mangle] +pub unsafe extern "C" fn tw_ffi_test_unsigned_sum_u16(a: u16, b: u16) -> u16 { + a.checked_add(b).unwrap_or(u16::MAX) +} + +/// Sum two unsigned integers of 32 bits +#[tw_ffi(ty = static_function, class = TWFFITest, name = UnsignedSumU32)] +#[no_mangle] +pub unsafe extern "C" fn tw_ffi_test_unsigned_sum_u32(a: u32, b: u32) -> u32 { + a.checked_add(b).unwrap_or(u32::MAX) +} + +/// Sum two unsigned integers of 64 bits +#[tw_ffi(ty = static_function, class = TWFFITest, name = UnsignedSumU64)] +#[no_mangle] +pub unsafe extern "C" fn tw_ffi_test_unsigned_sum_u64(a: u64, b: u64) -> u64 { + a.checked_add(b).unwrap_or(u64::MAX) +} + +/// Sum two signed integers of 8 bits +#[tw_ffi(ty = static_function, class = TWFFITest, name = SignedSumI8)] +#[no_mangle] +pub unsafe extern "C" fn tw_ffi_test_signed_sum_i8(a: i8, b: i8) -> i8 { + a.checked_add(b).unwrap_or(i8::MAX) +} + +/// Sum two signed integers of 16 bits +#[tw_ffi(ty = static_function, class = TWFFITest, name = SignedSumI16)] +#[no_mangle] +pub unsafe extern "C" fn tw_ffi_test_signed_sum_i16(a: i16, b: i16) -> i16 { + a.checked_add(b).unwrap_or(i16::MAX) +} + +/// Sum two signed integers of 32 bits +#[tw_ffi(ty = static_function, class = TWFFITest, name = SignedSumI32)] +#[no_mangle] +pub unsafe extern "C" fn tw_ffi_test_signed_sum_i32(a: i32, b: i32) -> i32 { + a.checked_add(b).unwrap_or(i32::MAX) +} + +/// Sum two signed integers of 64 bits +#[tw_ffi(ty = static_function, class = TWFFITest, name = SignedSumI64)] +#[no_mangle] +pub unsafe extern "C" fn tw_ffi_test_signed_sum_i64(a: i64, b: i64) -> i64 { + a.checked_add(b).unwrap_or(i64::MAX) +} + +/// Concatenate a string with a character +#[tw_ffi(ty = static_function, class = TWFFITest, name = StringWithU8)] +#[no_mangle] +pub unsafe extern "C" fn tw_ffi_test_string_with_u8( + a: Nonnull, + b: u8, +) -> NullableMut { + let a = try_or_else!(TWString::from_ptr_as_ref(a), std::ptr::null_mut); + let a_str = try_or_else!(a.as_str(), std::ptr::null_mut); + let mut result = String::new(); + result.push_str(a_str); + result.push(b as char); + TWString::from(result).into_ptr() +} diff --git a/rust/wallet_core_rs/src/ffi/mod.rs b/rust/wallet_core_rs/src/ffi/mod.rs index 7b77f1ed28a..abb69c520e6 100644 --- a/rust/wallet_core_rs/src/ffi/mod.rs +++ b/rust/wallet_core_rs/src/ffi/mod.rs @@ -6,6 +6,8 @@ pub mod bitcoin; #[cfg(feature = "ethereum")] pub mod ethereum; +// Test file for FFI +// pub mod ffi_test; #[cfg(feature = "solana")] pub mod solana; #[cfg(feature = "ton")]