Skip to content

Commit 7f6004b

Browse files
fix(barz): Replace u32 parameters with i32 (trustwallet#4504)
* Disallow to use unsigned integer parameters within class methods
1 parent c2fd67d commit 7f6004b

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

rust/tw_evm/src/ffi/barz.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ pub unsafe extern "C" fn tw_barz_get_counterfactual_address(
4141

4242
/// Returns the init code parameter of ERC-4337 User Operation
4343
///
44-
/// \param factory The address of the factory contract.
44+
/// \param factory The address of the factory contract
4545
/// \param public_key Public key for the verification facet
46-
/// \param verification_facet The address of the verification facet.
47-
/// \param salt The salt of the init code.
46+
/// \param verification_facet The address of the verification facet
47+
/// \param salt The salt of the init code; Must be non-negative
4848
/// \return The init code.
4949
#[tw_ffi(ty = static_function, class = TWBarz, name = GetInitCode)]
5050
#[no_mangle]
5151
pub unsafe extern "C" fn tw_barz_get_init_code(
5252
factory: Nonnull<TWString>,
5353
public_key: NonnullMut<TWPublicKey>,
5454
verification_facet: Nonnull<TWString>,
55-
salt: u32,
55+
salt: i32,
5656
) -> NullableMut<TWData> {
5757
let factory_address = try_or_else!(TWString::from_ptr_as_ref(factory), std::ptr::null_mut);
5858
let factory_address = try_or_else!(factory_address.as_str(), std::ptr::null_mut);
@@ -63,6 +63,8 @@ pub unsafe extern "C" fn tw_barz_get_init_code(
6363
std::ptr::null_mut
6464
);
6565
let verification_facet = try_or_else!(verification_facet.as_str(), std::ptr::null_mut);
66+
let salt = try_or_else!(salt.try_into(), std::ptr::null_mut);
67+
6668
let init_code = try_or_else!(
6769
get_init_code(
6870
factory_address,
@@ -117,18 +119,20 @@ pub unsafe extern "C" fn tw_barz_get_formatted_signature(
117119
///
118120
/// \param msg_hash Original msgHash
119121
/// \param barzAddress The address of Barz wallet signing the message
120-
/// \param chainId The chainId of the network the verification will happen
122+
/// \param chainId The chainId of the network the verification will happen; Must be non-negative
121123
/// \return The final hash to be signed.
122124
#[tw_ffi(ty = static_function, class = TWBarz, name = GetPrefixedMsgHash)]
123125
#[no_mangle]
124126
pub unsafe extern "C" fn tw_barz_get_prefixed_msg_hash(
125127
msg_hash: Nonnull<TWData>,
126128
barz_address: Nonnull<TWString>,
127-
chain_id: u32,
129+
chain_id: i32,
128130
) -> NullableMut<TWData> {
129131
let msg_hash = try_or_else!(TWData::from_ptr_as_ref(msg_hash), std::ptr::null_mut);
130132
let barz_address = try_or_else!(TWString::from_ptr_as_ref(barz_address), std::ptr::null_mut);
131133
let barz_address = try_or_else!(barz_address.as_str(), std::ptr::null_mut);
134+
let chain_id = try_or_else!(chain_id.try_into(), std::ptr::null_mut);
135+
132136
let prefixed_msg_hash = try_or_else!(
133137
get_prefixed_msg_hash(msg_hash.as_slice(), barz_address, chain_id),
134138
std::ptr::null_mut

rust/tw_macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use proc_macro::TokenStream;
22

33
mod code_gen;
44
mod tw_ffi;
5+
mod utils;
56

67
#[proc_macro_attribute]
78
pub fn tw_ffi(attr: TokenStream, item: TokenStream) -> TokenStream {

rust/tw_macros/src/tw_ffi.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::fs;
1212
use std::path::Path;
1313

1414
use crate::code_gen::{TWArg, TWConfig, TWFunction};
15+
use crate::utils::is_uint;
1516

1617
pub mod keywords {
1718
use syn::custom_keyword;
@@ -134,6 +135,13 @@ pub fn tw_ffi(attr: TokenStream2, item: TokenStream2) -> Result<TokenStream2> {
134135
};
135136

136137
let class = args.class.unwrap().to_string();
138+
// TODO add support for structs.
139+
if func_args.iter().any(|arg| is_uint(&arg.ty)) {
140+
return Err(syn::Error::new(
141+
proc_macro2::Span::call_site(),
142+
"Unsigned integers are not supported within class methods. Consider using 'struct' or signed integers. See https://kotlinlang.org/docs/inline-classes.html#mangling"
143+
));
144+
}
137145
let docs = func
138146
.attrs
139147
.iter()

rust/tw_macros/src/utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright © 2017 Trust Wallet.
4+
5+
pub fn is_uint(ty: &str) -> bool {
6+
matches!(ty, "u8" | "u16" | "u32" | "u64" | "u128" | "usize")
7+
}

0 commit comments

Comments
 (0)