Skip to content

Commit

Permalink
style(rust): run fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
soralit committed Oct 13, 2023
1 parent 4097d31 commit fb8c833
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 52 deletions.
7 changes: 6 additions & 1 deletion rust/apps/cardano/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ pub fn check_tx(tx: Vec<u8>, context: ParseContext) -> R<()> {
ParsedCardanoTx::verify(cardano_tx, context)
}

pub fn sign_tx(tx: Vec<u8>, context: ParseContext, entropy: &[u8], passphrase: &[u8]) -> R<Vec<u8>> {
pub fn sign_tx(
tx: Vec<u8>,
context: ParseContext,
entropy: &[u8],
passphrase: &[u8],
) -> R<Vec<u8>> {
let cardano_tx = cardano_serialization_lib::Transaction::from_bytes(tx)?;
let hash = blake2b_256(cardano_tx.body().to_bytes().as_ref());
let mut witness_set = cardano_serialization_lib::TransactionWitnessSet::new();
Expand Down
64 changes: 46 additions & 18 deletions rust/keystore/src/algorithms/ed25519/bip32_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ use third_party::ed25519_bip32_core::{DerivationScheme, XPrv, XPub};

use crate::errors::{KeystoreError, Result};

pub fn get_extended_public_key_by_entropy(entropy: &[u8], passphrase: &[u8], path: &String) -> Result<XPub> {
pub fn get_extended_public_key_by_entropy(
entropy: &[u8],
passphrase: &[u8],
path: &String,
) -> Result<XPub> {
let xprv = get_extended_private_key_by_entropy(entropy, passphrase, path)?;
Ok(xprv.public())
}

pub fn get_extended_private_key_by_entropy(entropy: &[u8], passphrase: &[u8], path: &String) -> Result<XPrv> {
pub fn get_extended_private_key_by_entropy(
entropy: &[u8],
passphrase: &[u8],
path: &String,
) -> Result<XPrv> {
let icarus_master_key = get_icarus_master_key_by_entropy(entropy, passphrase)?;
let path = normalize_path(path);
let derivation_path = DerivationPath::from_str(path.as_str())
Expand All @@ -31,7 +39,12 @@ pub fn get_extended_private_key_by_entropy(entropy: &[u8], passphrase: &[u8], pa
Ok(key)
}

pub fn sign_message_by_entropy(entropy: &[u8], passphrase: &[u8], message: &[u8], path: &String) -> Result<[u8; 64]> {
pub fn sign_message_by_entropy(
entropy: &[u8],
passphrase: &[u8],
message: &[u8],
path: &String,
) -> Result<[u8; 64]> {
let xprv = get_extended_private_key_by_entropy(entropy, passphrase, path)?;
let sig = xprv.sign::<Vec<u8>>(message);
Ok(*sig.to_bytes())
Expand All @@ -51,19 +64,30 @@ pub fn get_icarus_master_key_by_entropy(entropy: &[u8], passphrase: &[u8]) -> Re
Ok(XPrv::normalize_bytes_force3rd(hash))
}

pub fn sign_message_by_icarus_master_key(master_key: &[u8], message: &[u8], path: &String) -> Result<[u8; 64]> {
pub fn sign_message_by_icarus_master_key(
master_key: &[u8],
message: &[u8],
path: &String,
) -> Result<[u8; 64]> {
let xprv = derive_extended_privkey_by_icarus_master_key(master_key, path)?;
let sig = xprv.sign::<Vec<u8>>(message);
Ok(*sig.to_bytes())
}

pub fn derive_extended_pubkey_by_icarus_master_key(master_key: &[u8], path: &String) -> Result<XPub> {
pub fn derive_extended_pubkey_by_icarus_master_key(
master_key: &[u8],
path: &String,
) -> Result<XPub> {
let privkey = derive_extended_privkey_by_icarus_master_key(master_key, path)?;
Ok(privkey.public())
}

pub fn derive_extended_privkey_by_icarus_master_key(master_key: &[u8], path: &String) -> Result<XPrv> {
let xprv = XPrv::from_slice_verified(master_key).map_err(|e| KeystoreError::DerivationError(e.to_string()))?;
pub fn derive_extended_privkey_by_icarus_master_key(
master_key: &[u8],
path: &String,
) -> Result<XPrv> {
let xprv = XPrv::from_slice_verified(master_key)
.map_err(|e| KeystoreError::DerivationError(e.to_string()))?;
derive_bip32_ed25519_privkey(xprv, path)
}

Expand All @@ -72,12 +96,10 @@ fn derive_bip32_ed25519_privkey(root: XPrv, path: &String) -> Result<XPrv> {
let derivation_path = DerivationPath::from_str(path.as_str())
.map_err(|e| KeystoreError::InvalidDerivationPath(format!("{}", e)))?;
let childrens: Vec<ChildNumber> = derivation_path.into();
let key = childrens
.iter()
.fold(root, |acc, cur| match cur {
ChildNumber::Hardened { index } => acc.derive(DerivationScheme::V2, index + 0x80000000),
ChildNumber::Normal { index } => acc.derive(DerivationScheme::V2, *index),
});
let key = childrens.iter().fold(root, |acc, cur| match cur {
ChildNumber::Hardened { index } => acc.derive(DerivationScheme::V2, index + 0x80000000),
ChildNumber::Normal { index } => acc.derive(DerivationScheme::V2, *index),
});
Ok(key)
}

Expand All @@ -101,7 +123,8 @@ mod tests {
#[test]
fn test_icarus_master_key_with_passphrase() {
let entropy = hex::decode("46e62370a138a182a498b8e2885bc032379ddf38").unwrap();
let mut icarus_master_key = get_icarus_master_key_by_entropy(entropy.as_slice(), b"").unwrap();
let mut icarus_master_key =
get_icarus_master_key_by_entropy(entropy.as_slice(), b"").unwrap();
assert_eq!("c065afd2832cd8b087c4d9ab7011f481ee1e0721e78ea5dd609f3ab3f156d245d176bd8fd4ec60b4731c3918a2a72a0226c0cd119ec35b47e4d55884667f552a23f7fdcd4a10c6cd2c7393ac61d877873e248f417634aa3d812af327ffe9d620",
icarus_master_key.to_string());

Expand All @@ -114,15 +137,20 @@ mod tests {
fn test_get_extended_private_key() {
{
let entropy = hex::decode("00000000000000000000000000000000").unwrap();
let key = get_extended_private_key_by_entropy(entropy.as_slice(), b"", &"m/0'".to_string())
.unwrap();
let key =
get_extended_private_key_by_entropy(entropy.as_slice(), b"", &"m/0'".to_string())
.unwrap();
assert_eq!("8872ff61b06281da05205ffb765c256175cc2aaab52cd7176b5d80286c0e6f539e936c7b5f018c935544e3dff339dfe739c47ae8b364330a3162f028c658257b35a473378343fcc479fd326bc2c2a23f183ce682d514bd5a5b1d9a14ff8297cf",
key.to_string())
}
{
let entropy = hex::decode("00000000000000000000000000000000").unwrap();
let key = get_extended_public_key_by_entropy(entropy.as_slice(), b"", &"m/1852'/1815'/0'".to_string())
.unwrap();
let key = get_extended_public_key_by_entropy(
entropy.as_slice(),
b"",
&"m/1852'/1815'/0'".to_string(),
)
.unwrap();
assert_eq!("beb7e770b3d0f1932b0a2f3a63285bf9ef7d3e461d55446d6a3911d8f0ee55c0b0e2df16538508046649d0e6d5b32969555a23f2f1ebf2db2819359b0d88bd16",
key.to_string())
}
Expand Down
48 changes: 32 additions & 16 deletions rust/rust_c/src/interfaces/cardano/address.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
use app_cardano;
use app_cardano::address::AddressType;
use cty::c_char;
use crate::interfaces::structs::SimpleResponse;
use crate::interfaces::types::PtrString;
use crate::interfaces::utils::{convert_c_char, recover_c_char};
use app_cardano;
use app_cardano::address::AddressType;
use cty::c_char;

#[no_mangle]
pub extern "C" fn ada_get_base_address(xpub: PtrString, index: u32, network_id: u8) -> *mut SimpleResponse<c_char> {
pub extern "C" fn ada_get_base_address(
xpub: PtrString,
index: u32,
network_id: u8,
) -> *mut SimpleResponse<c_char> {
let xpub = recover_c_char(xpub);
match app_cardano::address::derive_address(xpub, 0, index, 0, AddressType::Base, network_id)
{
match app_cardano::address::derive_address(xpub, 0, index, 0, AddressType::Base, network_id) {
Ok(result) => SimpleResponse::success(convert_c_char(result)).simple_c_ptr(),
Err(e) => SimpleResponse::from(e).simple_c_ptr()
Err(e) => SimpleResponse::from(e).simple_c_ptr(),
}
}

#[no_mangle]
pub extern "C" fn ada_get_enterprise_address(xpub: PtrString, index: u32, network_id: u8) -> *mut SimpleResponse<c_char> {
pub extern "C" fn ada_get_enterprise_address(
xpub: PtrString,
index: u32,
network_id: u8,
) -> *mut SimpleResponse<c_char> {
let xpub = recover_c_char(xpub);
match app_cardano::address::derive_address(xpub, 0, index, 0, AddressType::Enterprise, network_id)
{
match app_cardano::address::derive_address(
xpub,
0,
index,
0,
AddressType::Enterprise,
network_id,
) {
Ok(result) => SimpleResponse::success(convert_c_char(result)).simple_c_ptr(),
Err(e) => SimpleResponse::from(e).simple_c_ptr()
Err(e) => SimpleResponse::from(e).simple_c_ptr(),
}
}

#[no_mangle]
pub extern "C" fn ada_get_stake_address(xpub: PtrString, index: u32, network_id: u8) -> *mut SimpleResponse<c_char> {
pub extern "C" fn ada_get_stake_address(
xpub: PtrString,
index: u32,
network_id: u8,
) -> *mut SimpleResponse<c_char> {
let xpub = recover_c_char(xpub);
match app_cardano::address::derive_address(xpub, 0, 0, index, AddressType::Stake, network_id)
{
match app_cardano::address::derive_address(xpub, 0, 0, index, AddressType::Stake, network_id) {
Ok(result) => SimpleResponse::success(convert_c_char(result)).simple_c_ptr(),
Err(e) => SimpleResponse::from(e).simple_c_ptr()
Err(e) => SimpleResponse::from(e).simple_c_ptr(),
}
}
}
13 changes: 8 additions & 5 deletions rust/rust_c/src/interfaces/cardano/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use third_party::ur_registry::crypto_key_path::CryptoKeyPath;

use third_party::ur_registry::registry_types::CARDANO_SIGNATURE;

pub mod structs;
pub mod address;
pub mod structs;

#[no_mangle]
pub extern "C" fn cardano_check_tx(
Expand Down Expand Up @@ -76,10 +76,13 @@ pub extern "C" fn cardano_sign_tx(
let passphrase = recover_c_char(passphrase);
match parse_context {
Ok(parse_context) => {
let sign_result = app_cardano::transaction::sign_tx(tx_hex, parse_context, entropy, passphrase.as_bytes())
.map(|v| {
CardanoSignature::new(cardano_sign_reqeust.get_request_id(), v).try_into()
});
let sign_result = app_cardano::transaction::sign_tx(
tx_hex,
parse_context,
entropy,
passphrase.as_bytes(),
)
.map(|v| CardanoSignature::new(cardano_sign_reqeust.get_request_id(), v).try_into());
match sign_result {
Ok(d) => match d {
Ok(data) => UREncodeResult::encode(
Expand Down
28 changes: 16 additions & 12 deletions rust/rust_c/src/interfaces/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::interfaces::errors::RustCError;
use crate::interfaces::structs::SimpleResponse;
use alloc::boxed::Box;
use alloc::string::ToString;
use core::slice;
use cty::c_char;
use third_party::hex;
use third_party::hex::ToHex;
use crate::interfaces::errors::RustCError;

use crate::interfaces::types::{PtrBytes, PtrString};
use crate::interfaces::utils::{convert_c_char, recover_c_char};
Expand Down Expand Up @@ -86,13 +86,16 @@ pub extern "C" fn get_bip32_ed25519_extended_pubkey(
entropy_len: u32,
passphrase: PtrString,
path: PtrString,
) -> *mut SimpleResponse<c_char>
{
) -> *mut SimpleResponse<c_char> {
let entropy = unsafe { slice::from_raw_parts(entropy, entropy_len as usize) };
let path = recover_c_char(path);
let passphrase = recover_c_char(passphrase);
let extended_key =
keystore::algorithms::ed25519::bip32_ed25519::get_extended_public_key_by_entropy(&entropy, passphrase.as_bytes(), &path);
keystore::algorithms::ed25519::bip32_ed25519::get_extended_public_key_by_entropy(
&entropy,
passphrase.as_bytes(),
&path,
);
match extended_key {
Ok(result) => SimpleResponse::success(convert_c_char(result.encode_hex())).simple_c_ptr(),
Err(e) => SimpleResponse::from(e).simple_c_ptr(),
Expand All @@ -104,12 +107,13 @@ pub extern "C" fn get_icarus_master_key(
entropy: PtrBytes,
entropy_len: u32,
passphrase: PtrString,
) -> *mut SimpleResponse<c_char>
{
) -> *mut SimpleResponse<c_char> {
let entropy = unsafe { slice::from_raw_parts(entropy, entropy_len as usize) };
let passphrase = recover_c_char(passphrase);
let master_key =
keystore::algorithms::ed25519::bip32_ed25519::get_icarus_master_key_by_entropy(&entropy, passphrase.as_bytes());
let master_key = keystore::algorithms::ed25519::bip32_ed25519::get_icarus_master_key_by_entropy(
&entropy,
passphrase.as_bytes(),
);
match master_key {
Ok(result) => SimpleResponse::success(convert_c_char(result.encode_hex())).simple_c_ptr(),
Err(e) => SimpleResponse::from(e).simple_c_ptr(),
Expand All @@ -128,13 +132,13 @@ pub extern "C" fn derive_bip32_ed25519_extended_pubkey(
let master_key =
keystore::algorithms::ed25519::bip32_ed25519::derive_extended_pubkey_by_icarus_master_key(&root, &path);
match master_key {
Ok(result) => SimpleResponse::success(convert_c_char(result.encode_hex())).simple_c_ptr(),
Ok(result) => {
SimpleResponse::success(convert_c_char(result.encode_hex())).simple_c_ptr()
}
Err(e) => SimpleResponse::from(e).simple_c_ptr(),
}
}
Err(e) => {
SimpleResponse::from(e).simple_c_ptr()
}
Err(e) => SimpleResponse::from(e).simple_c_ptr(),
}
}

Expand Down

0 comments on commit fb8c833

Please sign in to comment.