Skip to content

Commit

Permalink
More debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed May 6, 2024
1 parent 9fc6bbf commit 3960fe4
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 36 deletions.
Expand Up @@ -12,6 +12,7 @@ use tari_crypto::{
RistrettoPublicKey,
RistrettoSecretKey,
},
tari_utilities::ByteArray,
};

use crate::{
Expand Down Expand Up @@ -88,9 +89,6 @@ pub fn handler_get_metadata_signature(comm: &mut Comm) -> Result<(), AppSW> {

comm.append(&[RESPONSE_VERSION]); // version
comm.append(&metadata_signature.to_vec());
comm.append(&[255, 255, 255, 0, 0, 0, 255]);
comm.append(&challenge.to_vec());
comm.append(&[255, 255, 255, 0, 0, 0, 255]);
comm.reply_ok();

Ok(())
Expand Down
@@ -1,10 +1,15 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use ledger_device_sdk::io::Comm;
use ledger_device_sdk::{io::Comm, ui::gadgets::SingleMessage};
use tari_crypto::{keys::PublicKey, ristretto::RistrettoPublicKey, tari_utilities::ByteArray};

use crate::{utils::derive_from_bip32_key, AppSW, KeyType, RESPONSE_VERSION};
use crate::{
utils::{derive_from_bip32_key, u64_to_string},
AppSW,
KeyType,
RESPONSE_VERSION,
};

pub fn handler_get_public_key(comm: &mut Comm) -> Result<(), AppSW> {
let data = comm.get_data().map_err(|_| AppSW::WrongApduLength)?;
Expand All @@ -17,7 +22,16 @@ pub fn handler_get_public_key(comm: &mut Comm) -> Result<(), AppSW> {
index_bytes.clone_from_slice(&data[8..16]);
let index = u64::from_le_bytes(index_bytes);

let pk = match derive_from_bip32_key(account, index, KeyType::Nonce) {
let mut key_bytes = [0u8; 8];
key_bytes.clone_from_slice(&data[8..16]);
let key_int = u64::from_le_bytes(key_bytes);
let key = KeyType::from_branch_key(key_int);
let second_key = KeyType::from_branch_key(key_int);

let what_key = u64_to_string(second_key.as_byte() as u64);
SingleMessage::new(&what_key).show_and_wait();

let pk = match derive_from_bip32_key(account, index, key) {
Ok(k) => RistrettoPublicKey::from_secret_key(&k),
Err(e) => return Err(e),
};
Expand Down
2 changes: 1 addition & 1 deletion applications/minotari_ledger_wallet/wallet/src/hashing.rs
Expand Up @@ -16,7 +16,7 @@ impl<M: DomainSeparation, D: Digest> DomainSeparatedConsensusHasher<M, D>
where D: Default
{
pub fn new(label: &'static str, network: u64) -> Self {
let hasher = DomainSeparatedBorshHasher::<M, D>::new_with_label(&format!("{}.n{}", label, network));
let hasher = DomainSeparatedBorshHasher::<M, D>::new_with_label(&format!("{}.n{}", label, network as u8));
Self { hasher }
}

Expand Down
24 changes: 14 additions & 10 deletions applications/minotari_ledger_wallet/wallet/src/main.rs
Expand Up @@ -119,20 +119,24 @@ const P2_MORE: u8 = 0x01;
const STATIC_ALPHA_INDEX: u64 = 42;
const MAX_PAYLOADS: u8 = 250;

#[repr(u8)]
pub enum KeyType {
Alpha,
Nonce,
Recovery,
SenderOffset,
Alpha = 0x01,
Nonce = 0x02,
Recovery = 0x03,
SenderOffset = 0x04,
}

impl KeyType {
fn to_byte(&self) -> u8 {
match self {
Self::Alpha => 1,
Self::Nonce => 2,
Self::Recovery => 3,
Self::SenderOffset => 4,
pub fn as_byte(self) -> u8 {
self as u8
}

fn from_branch_key(n: u64) -> Self {
match n {
1 => Self::Alpha,
6 => Self::SenderOffset,
5 | 2 | _ => Self::Nonce,
}
}
}
Expand Down
29 changes: 19 additions & 10 deletions applications/minotari_ledger_wallet/wallet/src/utils.rs
Expand Up @@ -8,7 +8,7 @@ use digest::consts::U64;
use ledger_device_sdk::{
ecc::{bip32_derive, make_bip32_path, CurvesId, CxError},
io::SyscallError,
ui::gadgets::SingleMessage,
ui::gadgets::{MessageScroller, SingleMessage},
};
use tari_crypto::{
hash_domain,
Expand Down Expand Up @@ -242,7 +242,7 @@ pub fn derive_from_bip32_key(
) -> Result<RistrettoSecretKey, AppSW> {
let account = u64_to_string(u64_account);
let index = u64_to_string(u64_index);
let key_type = u64_to_string(u64_key_type.to_byte() as u64);
let key_type = u64_to_string(u64_key_type.as_byte() as u64);

let mut bip32_path = "m/44'/".to_string();
bip32_path.push_str(&BIP32_COIN_TYPE.to_string());
Expand Down Expand Up @@ -272,12 +272,21 @@ pub fn finalize_metadata_signature_challenge(
commitment: &PedersenCommitment,
message: &[u8; 32],
) -> [u8; 64] {
DomainSeparatedConsensusHasher::<TransactionHashDomain, Blake2b<U64>>::new("metadata_signature", network)
.chain(&ephemeral_pubkey)
.chain(&ephemeral_commitment)
.chain(&sender_offset_public_key)
.chain(&commitment)
.chain(&message)
.finalize()
.into()
let network_str = u64_to_string(network);
MessageScroller::new(&network_str).event_loop();
MessageScroller::new(&sender_offset_public_key.to_string()).event_loop();
MessageScroller::new(&ephemeral_commitment.as_public_key().to_string()).event_loop();
MessageScroller::new(&ephemeral_pubkey.to_string()).event_loop();
MessageScroller::new(&commitment.as_public_key().to_string()).event_loop();

let challenge =
DomainSeparatedConsensusHasher::<TransactionHashDomain, Blake2b<U64>>::new("metadata_signature", network)
.chain(ephemeral_pubkey)
.chain(ephemeral_commitment)
.chain(sender_offset_public_key)
.chain(commitment)
.chain(&message)
.finalize();

challenge.into()
}
5 changes: 4 additions & 1 deletion base_layer/core/src/transactions/key_manager/inner.rs
Expand Up @@ -200,7 +200,10 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static
) {
(true, WalletType::Ledger(ledger)) => {
let transport = get_transport().map_err(|e| KeyManagerServiceError::LedgerError(e.to_string()))?;
let command = ledger.build_command(Instruction::GetPublicKey, index.to_le_bytes().to_vec());
let mut data = index.to_le_bytes().to_vec();
let branch_u8 = TransactionKeyManagerBranch::from_key(branch).as_byte();
data.extend_from_slice(&(branch_u8 as u64).to_le_bytes());
let command = ledger.build_command(Instruction::GetPublicKey, data);

match command.execute_with_transport(&transport) {
Ok(result) => {
Expand Down
31 changes: 24 additions & 7 deletions base_layer/core/src/transactions/key_manager/interface.rs
Expand Up @@ -52,15 +52,16 @@ pub enum TxoStage {
Output,
}

#[repr(u8)]
#[derive(Clone, Copy, EnumIter)]
pub enum TransactionKeyManagerBranch {
DataEncryption,
Coinbase,
MetadataEphemiralNonce,
CommitmentMask,
Nonce,
KernelNonce,
SenderOffset,
DataEncryption = 0x00,
Coinbase = 0x01,
MetadataEphemiralNonce = 0x02,
CommitmentMask = 0x03,
Nonce = 0x04,
KernelNonce = 0x05,
SenderOffset = 0x06,
}

impl TransactionKeyManagerBranch {
Expand All @@ -77,6 +78,22 @@ impl TransactionKeyManagerBranch {
TransactionKeyManagerBranch::SenderOffset => "sender offset".to_string(),
}
}

pub fn from_key(key: &str) -> Self {
match key {
"data encryption" => TransactionKeyManagerBranch::DataEncryption,
"coinbase" => TransactionKeyManagerBranch::Coinbase,
"commitment mask" => TransactionKeyManagerBranch::CommitmentMask,
"metadata ephemiral nonce" => TransactionKeyManagerBranch::MetadataEphemiralNonce,
"kernel nonce" => TransactionKeyManagerBranch::KernelNonce,
"sender offset" => TransactionKeyManagerBranch::SenderOffset,
"nonce" | _ => TransactionKeyManagerBranch::Nonce,
}
}

pub fn as_byte(self) -> u8 {
self as u8
}
}

#[derive(Clone, Copy, EnumIter)]
Expand Down
Expand Up @@ -34,6 +34,7 @@ use digest::consts::{U32, U64};
use log::debug;
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use tari_common::configuration::Network;
use tari_common_types::types::{
ComAndPubSignature,
Commitment,
Expand All @@ -54,6 +55,7 @@ use tari_crypto::{
};
use tari_hashing::TransactionHashDomain;
use tari_script::TariScript;
use tari_utilities::ByteArray;

use super::TransactionOutputVersion;
use crate::{
Expand Down Expand Up @@ -320,7 +322,7 @@ impl TransactionOutput {
self.minimum_value_promise,
);

debug!(target: "c::brian::test", "BRIAN HERE: {:?}", challenge);
debug!(target: "c::brian::test", "challenge: {:?}", &challenge);

if !self.metadata_signature.verify_challenge(
&self.commitment,
Expand Down Expand Up @@ -408,6 +410,20 @@ impl TransactionOutput {
encrypted_data,
&minimum_value_promise,
);

debug!(target: "c::brian::test", "version: {:?}", &version);
debug!(target: "c::brian::test", "network: {:?}", &Network::LocalNet.as_byte());
debug!(target: "c::brian::test", "sender_offset_public_key: {:?}", &sender_offset_public_key);
debug!(target: "c::brian::test", "sender_offset_public_key: {:?}", &sender_offset_public_key.to_vec());
debug!(target: "c::brian::test", "ephemeral_commitment: {:?}", &ephemeral_commitment.as_public_key());
debug!(target: "c::brian::test", "ephemeral_commitment: {:?}", &ephemeral_commitment.to_vec());
debug!(target: "c::brian::test", "ephemeral_pubkey: {:?}", &ephemeral_pubkey);
debug!(target: "c::brian::test", "ephemeral_pubkey: {:?}", &ephemeral_pubkey.to_vec());
debug!(target: "c::brian::test", "commitment: {:?}", &commitment.as_public_key());
debug!(target: "c::brian::test", "commitment: {:?}", &commitment.to_vec());
debug!(target: "c::brian::test", "message: {:?}", &message.to_hex());
debug!(target: "c::brian::test", "message: {:?}", &message.to_vec());

TransactionOutput::finalize_metadata_signature_challenge(
version,
sender_offset_public_key,
Expand Down

0 comments on commit 3960fe4

Please sign in to comment.