Skip to content

Commit

Permalink
fix wallet display
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed May 30, 2024
1 parent 2e581ff commit b02d185
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl SendTab {
},
},
SendInputMode::Message => match c {
'\n' => self.send_input_mode = SendInputMode::None,
'\n' => self.send_input_mode = SendInputMode::PaymentId,
c => {
self.message_field.push(c);
return KeyHandled::Handled;
Expand Down Expand Up @@ -453,7 +453,7 @@ impl<B: Backend> Component<B> for SendTab {
.constraints(
[
Constraint::Length(3),
Constraint::Length(14),
Constraint::Length(17),
Constraint::Min(42),
Constraint::Length(1),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: BSD-3-Clause

use std::collections::HashMap;
use tari_utilities::hex::Hex;

use chrono::{DateTime, Local};
use log::*;
Expand Down Expand Up @@ -433,10 +432,9 @@ impl TransactionsTab {
};
let maturity = Span::styled(maturity, Style::default().fg(Color::White));


let payment_id = match tx.payment_id {
Some(v) => format!("#{}", v.as_bytes().to_hex()),
None => "None".to_string()
Some(v) => format!("#{}", v),
None => "None".to_string(),
};
let payment_id = Span::styled(payment_id, Style::default().fg(Color::White));

Expand Down
14 changes: 8 additions & 6 deletions applications/minotari_console_wallet/src/ui/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,10 @@ impl AppState {
.map_err(|_| UiError::PublicKeyParseError)?,
};
let output_features = OutputFeatures { ..Default::default() };
let payment_id_bytes: Vec<u8> = from_hex(&payment_id_hex)
let payment_id_u64: u64 = payment_id_hex
.parse::<u64>()
.map_err(|_| UiError::HexError("Could not convert payment_id to bytes".to_string()))?;
let payment_id = PaymentId::from_bytes(&payment_id_bytes).map_err(|_| UiError::PaymentIdParseError)?;
let payment_id = PaymentId::U64(payment_id_u64);

let fee_per_gram = fee_per_gram * uT;
let tx_service_handle = inner.wallet.transaction_service.clone();
Expand Down Expand Up @@ -384,9 +385,10 @@ impl AppState {
Err(_) => TariAddress::from_bytes(&from_hex(&address).map_err(|_| UiError::PublicKeyParseError)?)
.map_err(|_| UiError::PublicKeyParseError)?,
};
let payment_id_bytes: Vec<u8> = from_hex(&payment_id_hex)
let payment_id_u64: u64 = payment_id_hex
.parse::<u64>()
.map_err(|_| UiError::HexError("Could not convert payment_id to bytes".to_string()))?;
let payment_id = PaymentId::from_bytes(&payment_id_bytes).map_err(|_| UiError::PaymentIdParseError)?;
let payment_id = PaymentId::U64(payment_id_u64);

let output_features = OutputFeatures { ..Default::default() };

Expand Down Expand Up @@ -1213,7 +1215,7 @@ pub struct CompletedTransactionInfo {
pub weight: u64,
pub inputs_count: usize,
pub outputs_count: usize,
pub payment_id: Option<PaymentId>
pub payment_id: Option<PaymentId>,
}

impl CompletedTransactionInfo {
Expand Down Expand Up @@ -1254,7 +1256,7 @@ impl CompletedTransactionInfo {
weight,
inputs_count,
outputs_count,
payment_id: tx.payment_id
payment_id: tx.payment_id,
})
}
}
Expand Down
2 changes: 0 additions & 2 deletions applications/minotari_console_wallet/src/ui/ui_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ pub enum UiError {
WalletStorageError(#[from] WalletStorageError),
#[error("Could not convert string into Public Key")]
PublicKeyParseError,
#[error("Could not convert string into Payment Id")]
PaymentIdParseError,
#[error("Could not convert string into Net Address")]
AddressParseError,
#[error("Peer did not include an address")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@

//! Encrypted data using the extended-nonce variant XChaCha20-Poly1305 encryption with secure random nonce.
use std::{convert::TryInto, mem::size_of};
use std::{
convert::TryInto,
fmt,
fmt::{Display, Formatter},
mem::size_of,
};

use blake2::Blake2b;
use borsh::{BorshDeserialize, BorshSerialize};
Expand Down Expand Up @@ -59,7 +64,7 @@ const SIZE_NONCE: usize = size_of::<XNonce>();
const SIZE_VALUE: usize = size_of::<u64>();
const SIZE_MASK: usize = PrivateKey::KEY_LEN;
const SIZE_TAG: usize = size_of::<Tag>();
const STATIC_SIZE_TOTAL: usize = SIZE_NONCE + SIZE_VALUE + SIZE_MASK + SIZE_TAG;
pub const STATIC_ENCRYPTED_DATA_SIZE_TOTAL: usize = SIZE_NONCE + SIZE_VALUE + SIZE_MASK + SIZE_TAG;

// Number of hex characters of encrypted data to display on each side of ellipsis when truncating
const DISPLAY_CUTOFF: usize = 16;
Expand Down Expand Up @@ -125,6 +130,17 @@ impl PaymentId {
}
}

impl Display for PaymentId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
PaymentId::Zero => write!(f, "N/A"),
PaymentId::U32(v) => write!(f, "{}", v),
PaymentId::U64(v) => write!(f, "{}", v),
PaymentId::U256(v) => write!(f, "{}", v),
}
}
}

/// AEAD associated data
const ENCRYPTED_DATA_AAD: &[u8] = b"TARI_AAD_VALUE_AND_MASK_EXTEND_NONCE_VARIANT";

Expand Down Expand Up @@ -158,7 +174,7 @@ impl EncryptedData {
let tag = cipher.encrypt_in_place_detached(&nonce, ENCRYPTED_DATA_AAD, bytes.as_mut_slice())?;

// Put everything together: nonce, ciphertext, tag
let mut data = vec![0; STATIC_SIZE_TOTAL + payment_id.get_size()];
let mut data = vec![0; STATIC_ENCRYPTED_DATA_SIZE_TOTAL + payment_id.get_size()];
data[..SIZE_TAG].clone_from_slice(&tag);
data[SIZE_TAG..SIZE_TAG + SIZE_NONCE].clone_from_slice(&nonce);
data[SIZE_TAG + SIZE_NONCE..SIZE_TAG + SIZE_NONCE + SIZE_VALUE + SIZE_MASK + payment_id.get_size()]
Expand Down Expand Up @@ -207,17 +223,17 @@ impl EncryptedData {

/// Parse encrypted data from a byte slice
pub fn from_bytes(bytes: &[u8]) -> Result<Self, EncryptedDataError> {
if !(bytes.len() == STATIC_SIZE_TOTAL ||
bytes.len() == STATIC_SIZE_TOTAL + size_of::<u32>() ||
bytes.len() == STATIC_SIZE_TOTAL + size_of::<u64>() ||
bytes.len() == STATIC_SIZE_TOTAL + size_of::<U256>())
if !(bytes.len() == STATIC_ENCRYPTED_DATA_SIZE_TOTAL ||
bytes.len() == STATIC_ENCRYPTED_DATA_SIZE_TOTAL + size_of::<u32>() ||
bytes.len() == STATIC_ENCRYPTED_DATA_SIZE_TOTAL + size_of::<u64>() ||
bytes.len() == STATIC_ENCRYPTED_DATA_SIZE_TOTAL + size_of::<U256>())
{
return Err(EncryptedDataError::IncorrectLength(format!(
"Expected {}, {}, {} or {} bytes, got {}",
STATIC_SIZE_TOTAL,
STATIC_SIZE_TOTAL + size_of::<u32>(),
STATIC_SIZE_TOTAL + size_of::<u64>(),
STATIC_SIZE_TOTAL + size_of::<U256>(),
STATIC_ENCRYPTED_DATA_SIZE_TOTAL,
STATIC_ENCRYPTED_DATA_SIZE_TOTAL + size_of::<u32>(),
STATIC_ENCRYPTED_DATA_SIZE_TOTAL + size_of::<u64>(),
STATIC_ENCRYPTED_DATA_SIZE_TOTAL + size_of::<U256>(),
bytes.len()
)));
}
Expand Down Expand Up @@ -262,7 +278,7 @@ impl EncryptedData {
/// Returns the size of the payment id
pub fn get_payment_id_size(&self) -> usize {
// the length should always at least be the static total size, the extra len is the payment id
self.data.len().saturating_sub(STATIC_SIZE_TOTAL)
self.data.len().saturating_sub(STATIC_ENCRYPTED_DATA_SIZE_TOTAL)
}
}

Expand All @@ -280,7 +296,7 @@ impl Hex for EncryptedData {
impl Default for EncryptedData {
fn default() -> Self {
Self {
data: Vec::with_capacity(STATIC_SIZE_TOTAL),
data: Vec::with_capacity(STATIC_ENCRYPTED_DATA_SIZE_TOTAL),
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions base_layer/core/src/validation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ use crate::{
PowAlgorithm,
PowError,
},
transactions::transaction_components::{EncryptedData, TransactionInput, TransactionKernel, TransactionOutput},
transactions::transaction_components::{
encrypted_data::STATIC_ENCRYPTED_DATA_SIZE_TOTAL,
EncryptedData,
TransactionInput,
TransactionKernel,
TransactionOutput,
},
validation::ValidationError,
};

Expand Down Expand Up @@ -229,7 +235,7 @@ pub fn check_tari_encrypted_data_byte_size(
max_encrypted_data_size: usize,
) -> Result<(), ValidationError> {
let encrypted_data_size = encrypted_data.as_bytes().len();
if encrypted_data_size > max_encrypted_data_size {
if encrypted_data_size > max_encrypted_data_size + STATIC_ENCRYPTED_DATA_SIZE_TOTAL {
return Err(ValidationError::EncryptedDataExceedsMaxSize {
max_encrypted_data_size,
actual_encrypted_data_size: encrypted_data_size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,7 @@ impl CompletedTransaction {
target: LOG_TARGET,
"Could not create payment id from stored bytes"
);
CompletedTransactionConversionError::BincodeDeserialize (
CompletedTransactionConversionError::BincodeDeserialize(
"payment id could not be converted from bytes".to_string(),
)
})?,
Expand Down

0 comments on commit b02d185

Please sign in to comment.