Skip to content

Commit

Permalink
Merge pull request #617 from KeystoneHQ/feat/update_bin_check
Browse files Browse the repository at this point in the history
Feat/update bin check
  • Loading branch information
soralit authored May 15, 2024
2 parents a222f77 + 73d5c92 commit 74901fe
Show file tree
Hide file tree
Showing 33 changed files with 1,539 additions and 1,053 deletions.
118 changes: 34 additions & 84 deletions rust/rust_c/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/rust_c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ sui_rust_c = { path = "./src/sui", optional = true }
tron_rust_c = { path = "./src/tron", optional = true }
xrp_rust_c = { path = "./src/xrp", optional = true }
arweave_rust_c = { path = "./src/arweave", optional = true }
signature_rust_c = { path = "./src/signature", optional = true }

[lib]
crate-type = ["staticlib"]
Expand Down Expand Up @@ -89,6 +90,7 @@ multi-coins = [
"tron_rust_c",
"xrp_rust_c",
"arweave_rust_c",
"signature_rust_c"
]
btc-only = [
"common_rust_c/btc-only",
Expand Down
3 changes: 3 additions & 0 deletions rust/rust_c/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ include = [
"tron_rust_c",
"xrp_rust_c",
"arweave_rust_c",
"signature_rust_c",
"wallet_rust_c",
"btc_only_wallet_rust_c",
"multi_coins_wallet_rust_c",
Expand All @@ -42,6 +43,7 @@ extra_bindings = [
"tron_rust_c",
"xrp_rust_c",
"arweave_rust_c",
"signature_rust_c",
"wallet_rust_c",
"btc_only_wallet_rust_c",
"multi_coins_wallet_rust_c",
Expand All @@ -65,6 +67,7 @@ crates = [
"tron_rust_c",
"xrp_rust_c",
"arweave_rust_c",
"signature_rust_c",
"wallet_rust_c",
"btc_only_wallet_rust_c",
"multi_coins_wallet_rust_c",
Expand Down
2 changes: 2 additions & 0 deletions rust/rust_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use ethereum_rust_c;
#[cfg(feature = "multi-coins")]
use near_rust_c;
#[cfg(feature = "multi-coins")]
use signature_rust_c;
#[cfg(feature = "multi-coins")]
use solana_rust_c;
#[cfg(feature = "multi-coins")]
use sui_rust_c;
Expand Down
14 changes: 14 additions & 0 deletions rust/rust_c/src/signature/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "signature_rust_c"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
secp256k1={ version = "0.27.0", default_features = false, features = ["alloc", "lowmemory"]}
thiserror = { version = "1.0", package = "thiserror-core", default-features = false }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
cty = "0.2.2"
cstr_core = "0.2.6"
common_rust_c = {path = "../common"}
10 changes: 10 additions & 0 deletions rust/rust_c/src/signature/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use alloc::string::String;
use thiserror::Error;

pub type Result<T> = core::result::Result<T, RustCError>;

#[derive(Error, Debug, PartialEq)]
pub enum RustCError {
#[error("FormatTypeError, type is {0}")]
FormatTypeError(String),
}
26 changes: 26 additions & 0 deletions rust/rust_c/src/signature/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![no_std]
#![feature(error_in_core)]
#![allow(unused_unsafe)]
extern crate alloc;
mod errors;
mod signature;

use common_rust_c::utils::recover_c_char;
use core::slice;
use cty::c_char;
use signature::verify_signature;

#[no_mangle]
pub extern "C" fn verify_frimware_signature(
signature_ptr: *mut c_char,
message_hash_ptr: *mut u8,
pubkey_ptr: *mut u8,
) -> bool {
let signature = recover_c_char(signature_ptr);
let message_hash = unsafe { slice::from_raw_parts(message_hash_ptr, 32) };
let publick_key = unsafe { slice::from_raw_parts(pubkey_ptr, 65) };
match hex::decode(signature) {
Ok(data) => verify_signature(&data, message_hash, publick_key).unwrap_or(false),
Err(_) => false,
}
}
49 changes: 49 additions & 0 deletions rust/rust_c/src/signature/src/signature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::errors::{Result, RustCError};
use alloc::string::ToString;
use secp256k1::{ecdsa, Message, PublicKey, Secp256k1, SecretKey};

pub fn verify_signature(signature: &[u8], message_hash: &[u8], pubkey: &[u8]) -> Result<bool> {
let secp = Secp256k1::verification_only();
let public_key =
PublicKey::from_slice(pubkey).map_err(|e| RustCError::FormatTypeError(e.to_string()))?;
let message = Message::from_slice(message_hash)
.map_err(|e| RustCError::FormatTypeError(e.to_string()))?;
let mut sig = ecdsa::Signature::from_compact(signature)
.map_err(|e| RustCError::FormatTypeError(e.to_string()))?;
sig.normalize_s();
let result = secp.verify_ecdsa(&message, &sig, &public_key).is_ok();
Ok(result)
}

pub fn sign_message_by_key(message_hash: &[u8], private_key: &[u8]) -> Result<[u8; 64]> {
let secp = Secp256k1::signing_only();
let message = Message::from_slice(message_hash)
.map_err(|e| RustCError::FormatTypeError(e.to_string()))?;

let private_key = SecretKey::from_slice(private_key)
.map_err(|e| RustCError::FormatTypeError(e.to_string()))?;

let sig = secp.sign_ecdsa(&message, &private_key);
Ok(sig.serialize_compact())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_should_sign_and_verify_the_signature() {
let test_key = "f254b030d04cdd902e9219d8390e1deb5a585f3c25bacf5c74bd07803a8dd873";
let private_key = hex::decode(test_key).unwrap();
let message_hash =
hex::decode("0D94D045A7E0D4547E161AC360C73581A95383435A48D8869AB08FF34A8DB5E7")
.unwrap();

let sig = hex::decode("168c267d21968b1447a676276d7ee7055810d58ac5524457361a09647bf19d2b108dd831a9d590019c93151d700e1c20eaf95fef24c60e645c04178227880e94").unwrap();
let pubkey_bytes = hex::decode(PUBKEY_STRING).unwrap();
// test pubkey
const PUBKEY_STRING: &str = "04e3003fa1467452743ed7b97cc8c0786f3b9c255d31ccca9e6dc59915b17fa8ed5933cf74ce8ec3614a503422f0e77b495a07567e29256858d6282f63c6dbfebd";
let result = verify_signature(&sig, &message_hash, &pubkey_bytes).unwrap();
assert_eq!(result, true);
}
}
2 changes: 1 addition & 1 deletion src/config/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define SOFTWARE_VERSION_MAX_LEN (32)
#define SOFTWARE_VERSION_MAJOR 1
#define SOFTWARE_VERSION_MINOR 4
#define SOFTWARE_VERSION_BUILD 2
#define SOFTWARE_VERSION_BUILD 3
#define SOFTWARE_VERSION_BETA 1
#define SOFTWARE_VERSION (SOFTWARE_VERSION_MAJOR * 10000 + SOFTWARE_VERSION_MINOR * 100 + SOFTWARE_VERSION_BUILD)

Expand Down
Loading

0 comments on commit 74901fe

Please sign in to comment.