Skip to content

Commit

Permalink
Ase new SDK API for chaincode and hash functions + add traces
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Feb 13, 2024
1 parent 45b68d5 commit f39d96b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
18 changes: 11 additions & 7 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["yhql", "agrojean-ledger"]
edition = "2021"

[dependencies]
ledger_device_sdk = "1.5.0"
ledger_device_sdk = "1.5.1"
ledger_secure_sdk_sys = "1.2.0"
include_gif = "1.0.1"
serde = {version="1.0.192", default_features = false, features = ["derive"]}
Expand Down Expand Up @@ -35,3 +35,7 @@ icon = "crab_14x14.gif"

[package.metadata.ledger.nanosplus]
icon = "crab_14x14.gif"

[patch.crates-io]
ledger_device_sdk = {path = "../ledger-device-rust-sdk/ledger_device_sdk"}
ledger_secure_sdk_sys = {path = "../ledger-device-rust-sdk/ledger_secure_sdk_sys"}
39 changes: 32 additions & 7 deletions src/handlers/get_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::app_ui::address::ui_display_pk;
use crate::utils::Bip32Path;
use crate::AppSW;
use ledger_device_sdk::ecc::{Secp256k1, SeedDerive};
use ledger_device_sdk::hash::{HashId, Hasher};
use ledger_device_sdk::io::Comm;
use ledger_secure_sdk_sys::{
cx_hash_no_throw, cx_hash_t, cx_keccak_init_no_throw, cx_sha3_t, CX_LAST, CX_OK,
Expand All @@ -28,12 +29,34 @@ pub fn handler_get_public_key(comm: &mut Comm, display: bool) -> Result<(), AppS
let data = comm.get_data().map_err(|_| AppSW::WrongApduLength)?;
let path: Bip32Path = data.try_into()?;

let pk = Secp256k1::derive_from_path(path.as_ref())
.public_key()
.map_err(|_| AppSW::KeyDeriveFail)?;
{
let test_hash: &[u8; 29] = b"Not your keys, not your coins";

let mut keccak = Hasher::new(HashId::KECCAK_256, 32).unwrap();

let mut output: [u8; 32] = [0u8; 32];

let size = keccak.get_size().unwrap();
if size == 32 {
ledger_device_sdk::testing::debug_print("Size match \n");
} else {
ledger_device_sdk::testing::debug_print("Size mismatch \n");
}

ledger_device_sdk::testing::debug_print("Calling hash\n");

let _res = keccak.hash(test_hash, &mut output);

//let _ = keccak.hash_update(test_hash);
//let _ = keccak.hash_final(&mut output);
}

let (k, _cc) = Secp256k1::derive_from_path(path.as_ref());
let pk = k.public_key().map_err(|_| AppSW::KeyDeriveFail)?;

// Display address on device if requested
if display {
//let mut keccak256 = Hasher::new(HashId::KECCAK_256, 32).unwrap();
let mut keccak256: cx_sha3_t = Default::default();
let mut address: [u8; 32] = [0u8; 32];

Expand All @@ -42,13 +65,12 @@ pub fn handler_get_public_key(comm: &mut Comm, display: bool) -> Result<(), AppS
return Err(AppSW::AddrDisplayFail);
}

let mut pk_mut = pk.pubkey;
let pk_ptr = pk_mut.as_mut_ptr().offset(1);
let pk_ptr = pk.as_ref();
if cx_hash_no_throw(
&mut keccak256.header as *mut cx_hash_t,
CX_LAST,
pk_ptr,
64_usize,
pk_ptr[1..].as_ptr(),
pk_ptr[1..].len(),
address.as_mut_ptr(),
address.len(),
) != CX_OK
Expand All @@ -57,6 +79,9 @@ pub fn handler_get_public_key(comm: &mut Comm, display: bool) -> Result<(), AppS
}
}

//let pk_ptr = pk.as_ref();
//let _ = keccak256.hash(&(pk_ptr[1..]), &mut address);

if !ui_display_pk(&address)? {
return Err(AppSW::Deny);
}
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/sign_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ fn compute_signature_and_append(comm: &mut Comm, ctx: &mut TxContext) -> Result<
}
}

let (sig, siglen, parity) = Secp256k1::derive_from_path(ctx.path.as_ref())
let (k, _cc) = Secp256k1::derive_from_path(ctx.path.as_ref());
let (sig, siglen, parity) = k
.deterministic_sign(&message_hash)
.map_err(|_| AppSW::TxSignFail)?;
comm.append(&[siglen as u8]);
Expand Down

0 comments on commit f39d96b

Please sign in to comment.