diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fd928138..67a01331e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Added missing pattern for only including context and vm config to `testing_env!` to remove friction - Added `_array` suffix versions of `sha256`, `keccak256`, and `keccak512` hash functions in `env` [PR 646](https://github.com/near/near-sdk-rs/pull/646) - These return a fixed length array instead of heap allocating with `Vec` +- Added `ripemd160_array` hash function that returns a fixed length byte array [PR 648](https://github.com/near/near-sdk-rs/pull/648) ## `4.0.0-pre.4` [10-15-2021] - Unpin `syn` dependency in macros from `=1.0.57` to be more composable with other crates. [PR 605](https://github.com/near/near-sdk-rs/pull/605) diff --git a/examples/callback-results/res/callback_results.wasm b/examples/callback-results/res/callback_results.wasm index 2f125b83c..9e4de02a3 100755 Binary files a/examples/callback-results/res/callback_results.wasm and b/examples/callback-results/res/callback_results.wasm differ diff --git a/examples/cross-contract-high-level/res/cross_contract_high_level.wasm b/examples/cross-contract-high-level/res/cross_contract_high_level.wasm index 268093bdc..8c9e91d9f 100755 Binary files a/examples/cross-contract-high-level/res/cross_contract_high_level.wasm and b/examples/cross-contract-high-level/res/cross_contract_high_level.wasm differ diff --git a/examples/cross-contract-low-level/res/cross_contract_low_level.wasm b/examples/cross-contract-low-level/res/cross_contract_low_level.wasm index 8ae65afd2..1376256e5 100755 Binary files a/examples/cross-contract-low-level/res/cross_contract_low_level.wasm and b/examples/cross-contract-low-level/res/cross_contract_low_level.wasm differ diff --git a/examples/fungible-token/res/defi.wasm b/examples/fungible-token/res/defi.wasm index e439e8bff..8f421cd29 100755 Binary files a/examples/fungible-token/res/defi.wasm and b/examples/fungible-token/res/defi.wasm differ diff --git a/examples/fungible-token/res/fungible_token.wasm b/examples/fungible-token/res/fungible_token.wasm index 59b1b8211..93c2b2853 100755 Binary files a/examples/fungible-token/res/fungible_token.wasm and b/examples/fungible-token/res/fungible_token.wasm differ diff --git a/examples/lockable-fungible-token/res/lockable_fungible_token.wasm b/examples/lockable-fungible-token/res/lockable_fungible_token.wasm index cd9da2a93..66fd5dfc1 100755 Binary files a/examples/lockable-fungible-token/res/lockable_fungible_token.wasm and b/examples/lockable-fungible-token/res/lockable_fungible_token.wasm differ diff --git a/examples/mission-control/res/mission_control.wasm b/examples/mission-control/res/mission_control.wasm index 60d9b60a1..f556a2ba1 100755 Binary files a/examples/mission-control/res/mission_control.wasm and b/examples/mission-control/res/mission_control.wasm differ diff --git a/examples/non-fungible-token/res/approval_receiver.wasm b/examples/non-fungible-token/res/approval_receiver.wasm index ab17f8805..1108c6ce2 100755 Binary files a/examples/non-fungible-token/res/approval_receiver.wasm and b/examples/non-fungible-token/res/approval_receiver.wasm differ diff --git a/examples/non-fungible-token/res/non_fungible_token.wasm b/examples/non-fungible-token/res/non_fungible_token.wasm index 68e07cb2e..a3f773eb4 100755 Binary files a/examples/non-fungible-token/res/non_fungible_token.wasm and b/examples/non-fungible-token/res/non_fungible_token.wasm differ diff --git a/examples/non-fungible-token/res/token_receiver.wasm b/examples/non-fungible-token/res/token_receiver.wasm index b9ca201bc..d3ed2ac70 100755 Binary files a/examples/non-fungible-token/res/token_receiver.wasm and b/examples/non-fungible-token/res/token_receiver.wasm differ diff --git a/examples/status-message-collections/res/status_message_collections.wasm b/examples/status-message-collections/res/status_message_collections.wasm index 942f4ecb7..92214c32e 100755 Binary files a/examples/status-message-collections/res/status_message_collections.wasm and b/examples/status-message-collections/res/status_message_collections.wasm differ diff --git a/examples/status-message/res/status_message.wasm b/examples/status-message/res/status_message.wasm index ce1603897..c18435000 100755 Binary files a/examples/status-message/res/status_message.wasm and b/examples/status-message/res/status_message.wasm differ diff --git a/examples/test-contract/res/test_contract.wasm b/examples/test-contract/res/test_contract.wasm index b01310d61..ccfe2c959 100755 Binary files a/examples/test-contract/res/test_contract.wasm and b/examples/test-contract/res/test_contract.wasm differ diff --git a/near-sdk/src/environment/env.rs b/near-sdk/src/environment/env.rs index 64cb75829..758c8370e 100644 --- a/near-sdk/src/environment/env.rs +++ b/near-sdk/src/environment/env.rs @@ -54,6 +54,12 @@ macro_rules! method_into_register { //* Note: need specific length functions because const generics don't work with mem::transmute //* https://github.com/rust-lang/rust/issues/61956 +pub(crate) unsafe fn read_register_fixed_20(register_id: u64) -> [u8; 20] { + let mut hash = [MaybeUninit::::uninit(); 20]; + sys::read_register(register_id, hash.as_mut_ptr() as _); + std::mem::transmute(hash) +} + pub(crate) unsafe fn read_register_fixed_32(register_id: u64) -> [u8; 32] { let mut hash = [MaybeUninit::::uninit(); 32]; sys::read_register(register_id, hash.as_mut_ptr() as _); @@ -279,6 +285,17 @@ pub fn keccak512_array(value: &[u8]) -> [u8; 64] { } } +/// Hashes the bytes using the RIPEMD-160 hash function. This returns a 20 byte hash. +pub fn ripemd160_array(value: &[u8]) -> [u8; 20] { + //* SAFETY: ripemd160 syscall will always generate 20 bytes inside of the atomic op register + //* so the read will have a sufficient buffer of 20, and can transmute from uninit + //* because all bytes are filled. This assumes a valid ripemd160 implementation. + unsafe { + sys::ripemd160(value.len() as _, value.as_ptr() as _, ATOMIC_OP_REGISTER); + read_register_fixed_20(ATOMIC_OP_REGISTER) + } +} + // ################ // # Promises API # // ################ @@ -789,5 +806,10 @@ mod tests { .unwrap() .as_slice() ); + + assert_eq!( + &super::ripemd160_array(b"some value"), + base64::decode("CfAl/tcE4eysj4iyvaPlaHbaA6w=").unwrap().as_slice() + ); } } diff --git a/near-sdk/src/environment/mock/mocked_blockchain.rs b/near-sdk/src/environment/mock/mocked_blockchain.rs index b70a437a7..fadc62504 100644 --- a/near-sdk/src/environment/mock/mocked_blockchain.rs +++ b/near-sdk/src/environment/mock/mocked_blockchain.rs @@ -191,6 +191,10 @@ mod mock_chain { with_mock_interface(|b| b.keccak512(value_len, value_ptr, register_id)) } #[no_mangle] + extern "C" fn ripemd160(value_len: u64, value_ptr: u64, register_id: u64) { + with_mock_interface(|b| b.ripemd160(value_len, value_ptr, register_id)) + } + #[no_mangle] extern "C" fn value_return(value_len: u64, value_ptr: u64) { with_mock_interface(|b| b.value_return(value_len, value_ptr)) }