Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions rust/tw_keypair/src/ffi/asn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ pub unsafe extern "C" fn ecdsa_signature_from_asn_der(
encoded_len: usize,
) -> CByteArrayResult {
let encoded_ref = CByteArrayRef::new(encoded, encoded_len);
let Some(encoded) = encoded_ref.to_vec() else {
return CByteArrayResult::error(CKeyPairError::InvalidSignature);
};
let encoded = encoded_ref.to_vec();

der::Signature::from_bytes(encoded.as_slice())
.map(|sign| CByteArray::from(sign.to_vec()))
Expand Down
9 changes: 3 additions & 6 deletions rust/tw_keypair/src/ffi/privkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub unsafe extern "C" fn tw_private_key_create_with_data(
input_len: usize,
) -> *mut TWPrivateKey {
let bytes_ref = CByteArrayRef::new(input, input_len);
let bytes = try_or_else!(bytes_ref.to_vec(), std::ptr::null_mut);
let bytes = bytes_ref.to_vec();

PrivateKey::new(bytes)
.map(|private| TWPrivateKey(private).into_ptr())
Expand Down Expand Up @@ -85,7 +85,7 @@ pub unsafe extern "C" fn tw_private_key_is_valid(
curve: u32,
) -> bool {
let curve = try_or_false!(Curve::from_raw(curve));
let priv_key_slice = try_or_false!(CByteArrayRef::new(key, key_len).as_slice());
let priv_key_slice = CByteArrayRef::new(key, key_len).as_slice();
PrivateKey::is_valid(priv_key_slice, curve)
}

Expand All @@ -105,10 +105,7 @@ pub unsafe extern "C" fn tw_private_key_sign(
) -> CByteArray {
let curve = try_or_else!(Curve::from_raw(curve), CByteArray::default);
let private = try_or_else!(TWPrivateKey::from_ptr_as_ref(key), CByteArray::default);
let message_to_sign = try_or_else!(
CByteArrayRef::new(message, message_len).as_slice(),
CByteArray::default
);
let message_to_sign = CByteArrayRef::new(message, message_len).as_slice();

// Return an empty signature if an error occurs.
let sig = private.0.sign(message_to_sign, curve).unwrap_or_default();
Expand Down
6 changes: 3 additions & 3 deletions rust/tw_keypair/src/ffi/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub unsafe extern "C" fn tw_public_key_create_with_data(
ty: u32,
) -> *mut TWPublicKey {
let bytes_ref = CByteArrayRef::new(input, input_len);
let bytes = try_or_else!(bytes_ref.to_vec(), std::ptr::null_mut);
let bytes = bytes_ref.to_vec();
let ty = try_or_else!(PublicKeyType::from_raw(ty), std::ptr::null_mut);
PublicKey::new(bytes, ty)
.map(|public| TWPublicKey(public).into_ptr())
Expand Down Expand Up @@ -67,8 +67,8 @@ pub unsafe extern "C" fn tw_public_key_verify(
msg_len: usize,
) -> bool {
let public = try_or_false!(TWPublicKey::from_ptr_as_ref(key));
let sig = try_or_false!(CByteArrayRef::new(sig, sig_len).as_slice());
let msg = try_or_false!(CByteArrayRef::new(msg, msg_len).as_slice());
let sig = CByteArrayRef::new(sig, sig_len).as_slice();
let msg = CByteArrayRef::new(msg, msg_len).as_slice();
public.0.verify(sig, msg)
}

Expand Down
10 changes: 5 additions & 5 deletions rust/tw_memory/src/ffi/c_byte_array_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ impl CByteArrayRef {
/// # Safety
///
/// The inner data must be valid.
pub unsafe fn to_vec(&self) -> Option<Vec<u8>> {
self.as_slice().map(|data| data.to_vec())
pub unsafe fn to_vec(&self) -> Vec<u8> {
self.as_slice().to_vec()
}

/// Returns a slice.
Expand All @@ -32,10 +32,10 @@ impl CByteArrayRef {
/// # Safety
///
/// The inner data must be valid.
pub unsafe fn as_slice(&self) -> Option<&'static [u8]> {
pub unsafe fn as_slice(&self) -> &'static [u8] {
if self.data.is_null() {
return None;
return &[];
}
Some(std::slice::from_raw_parts(self.data, self.size))
std::slice::from_raw_parts(self.data, self.size)
}
}
8 changes: 3 additions & 5 deletions rust/tw_memory/src/ffi/tw_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl TWData {
}

/// Creates a `TWData` from a raw byte array.
pub unsafe fn from_raw_data(bytes: *const u8, size: usize) -> Option<TWData> {
CByteArrayRef::new(bytes, size).to_vec().map(TWData)
pub unsafe fn from_raw_data(bytes: *const u8, size: usize) -> TWData {
TWData(CByteArrayRef::new(bytes, size).to_vec())
}

/// Converts `TWData` into `Data` without additional allocation.
Expand Down Expand Up @@ -65,9 +65,7 @@ impl RawPtrTrait for TWData {}
/// \return Non-null filled block of data.
#[no_mangle]
pub unsafe extern "C" fn tw_data_create_with_bytes(bytes: *const u8, size: usize) -> *mut TWData {
TWData::from_raw_data(bytes, size)
.map(|data| data.into_ptr())
.unwrap_or_else(std::ptr::null_mut)
TWData::from_raw_data(bytes, size).into_ptr()
}

/// Deletes a block of data created with a `TWDataCreate*` method.
Expand Down
4 changes: 1 addition & 3 deletions rust/tw_memory/src/ffi/tw_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ pub struct TWString(CString);

impl TWString {
pub unsafe fn is_utf8_string(bytes: *const u8, size: usize) -> bool {
let Some(bytes) = CByteArrayRef::new(bytes, size).to_vec() else {
return false;
};
let bytes = CByteArrayRef::new(bytes, size).to_vec();
String::from_utf8(bytes).is_ok()
}

Expand Down
18 changes: 18 additions & 0 deletions tests/chains/Ethereum/BarzTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,24 @@ TEST(Barz, SignAuthorization) {
}
}

TEST(Barz, SignAuthorizationZeroNonce) {
{
const auto chainId = store(uint256_t(1));
const auto contractAddress = "0xB91aaa96B138A1B1D94c9df4628187132c5F2bf1";
const Data nonce;
const auto privateKey = "0x947dd69af402e7f48da1b845dfc1df6be593d01a0d8274bd03ec56712e7164e8";

const auto signedAuthorization = WRAPS(TWBarzSignAuthorization(WRAPD(TWDataCreateWithBytes(chainId.data(), chainId.size())).get(), STRING(contractAddress).get(), WRAPD(TWDataCreateWithBytes(nonce.data(), nonce.size())).get(), STRING(privateKey).get()));
auto json = nlohmann::json::parse(std::string(TWStringUTF8Bytes(signedAuthorization.get())));
ASSERT_EQ(json["chainId"], hexEncoded(chainId));
ASSERT_EQ(json["address"], contractAddress);
ASSERT_EQ(json["nonce"], "0x00");
ASSERT_EQ(json["yParity"], hexEncoded(store(uint256_t(0))));
ASSERT_EQ(json["r"], "0x2269a34ea41b8898fb28196c3548836e2df0efe5968574be1cefc0355af11c24");
ASSERT_EQ(json["s"], "0x601b4443deafb48303d4f4a580505485e3fa7f516472675227494a88e9d0a5b5");
}
}

TEST(Barz, GetEncodedHash) {
{
const auto chainId = store(uint256_t(31337), 32);
Expand Down
10 changes: 10 additions & 0 deletions tests/common/DataTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,13 @@ TEST(DataTests, hasPrefix) {
const Data prefix23 = parse_hex("bb");
EXPECT_FALSE(has_prefix(data, prefix23));
}

TEST(DataTests, rustEmptyArray) {
Data empty;
EXPECT_EQ(empty.size(), 0ul);
EXPECT_EQ(empty.data(), nullptr);
auto* rustPtr = Rust::tw_data_create_with_bytes(empty.data(), empty.size());
EXPECT_NE(rustPtr, nullptr);
EXPECT_EQ(Rust::tw_data_size(rustPtr), 0);
Rust::tw_data_delete(rustPtr);
}
Loading