Skip to content

Commit

Permalink
feat!: add source address to encrypted data (#6472)
Browse files Browse the repository at this point in the history
Description
---
This adds the source address to encrypted data

Motivation and Context
---
This allows a one-sided payment to send over its address to the
receiving wallet
It also allows a wallet recovering funds to see where the funds came
from.

How Has This Been Tested?
---
unit test and manual
  • Loading branch information
SWvheerden authored Aug 16, 2024
1 parent 3828796 commit e97afc5
Show file tree
Hide file tree
Showing 26 changed files with 311 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use chrono::{DateTime, Local};
use log::*;
use minotari_wallet::transaction_service::storage::models::TxCancellationReason;
use tari_common_types::transaction::{TransactionDirection, TransactionStatus};
use tari_core::transactions::transaction_components::encrypted_data::PaymentId;
use tokio::runtime::Handle;
use tui::{
backend::Backend,
Expand Down Expand Up @@ -442,12 +441,13 @@ impl TransactionsTab {

let payment_id = match tx.payment_id.clone() {
Some(v) => {
if let PaymentId::Open(bytes) = v {
let bytes = v.get_data();
if bytes.is_empty() {
format!("#{}", v)
} else {
String::from_utf8(bytes)
.unwrap_or_else(|_| "Invalid string".to_string())
.to_string()
} else {
format!("#{}", v)
}
},
None => "None".to_string(),
Expand Down
18 changes: 9 additions & 9 deletions base_layer/common_types/src/tari_address/dual_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
TariAddressFeatures,
INTERNAL_DUAL_BASE58_MAX_SIZE,
INTERNAL_DUAL_BASE58_MIN_SIZE,
INTERNAL_DUAL_SIZE,
TARI_ADDRESS_INTERNAL_DUAL_SIZE,
},
types::PublicKey,
};
Expand Down Expand Up @@ -77,12 +77,12 @@ impl DualAddress {
/// helper function to convert emojis to u8
pub fn emoji_to_bytes(emoji: &str) -> Result<Vec<u8>, TariAddressError> {
// The string must be the correct size, including the checksum
if emoji.chars().count() != INTERNAL_DUAL_SIZE {
if emoji.chars().count() != TARI_ADDRESS_INTERNAL_DUAL_SIZE {
return Err(TariAddressError::InvalidSize);
}

// Convert the emoji string to a byte array
let mut bytes = Vec::<u8>::with_capacity(INTERNAL_DUAL_SIZE);
let mut bytes = Vec::<u8>::with_capacity(TARI_ADDRESS_INTERNAL_DUAL_SIZE);
for c in emoji.chars() {
if let Some(i) = REVERSE_EMOJI.get(&c) {
bytes.push(*i);
Expand Down Expand Up @@ -130,7 +130,7 @@ impl DualAddress {
/// Construct Tari Address from bytes
pub fn from_bytes(bytes: &[u8]) -> Result<Self, TariAddressError>
where Self: Sized {
if bytes.len() != INTERNAL_DUAL_SIZE {
if bytes.len() != TARI_ADDRESS_INTERNAL_DUAL_SIZE {
return Err(TariAddressError::InvalidSize);
}
if validate_checksum(bytes).is_err() {
Expand All @@ -151,8 +151,8 @@ impl DualAddress {
}

/// Convert Tari Address to bytes
pub fn to_bytes(&self) -> [u8; INTERNAL_DUAL_SIZE] {
let mut buf = [0u8; INTERNAL_DUAL_SIZE];
pub fn to_bytes(&self) -> [u8; TARI_ADDRESS_INTERNAL_DUAL_SIZE] {
let mut buf = [0u8; TARI_ADDRESS_INTERNAL_DUAL_SIZE];
buf[0] = self.network.as_byte();
buf[1] = self.features.0;
buf[2..34].copy_from_slice(self.public_view_key.as_bytes());
Expand Down Expand Up @@ -231,7 +231,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_DUAL_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_DUAL_SIZE);

let features = emoji_id_from_public_key.features();
assert_eq!(features, TariAddressFeatures::create_interactive_and_one_sided());
Expand Down Expand Up @@ -259,7 +259,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_DUAL_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_DUAL_SIZE);

let features = emoji_id_from_public_key.features();
assert_eq!(features, TariAddressFeatures::create_interactive_only());
Expand Down Expand Up @@ -288,7 +288,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_DUAL_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_DUAL_SIZE);

let features = emoji_id_from_public_key.features();
assert_eq!(features, TariAddressFeatures::create_one_sided_only());
Expand Down
34 changes: 22 additions & 12 deletions base_layer/common_types/src/tari_address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ use crate::{
types::PublicKey,
};

const INTERNAL_DUAL_SIZE: usize = 67; // number of bytes used for the internal representation
const INTERNAL_SINGLE_SIZE: usize = 35; // number of bytes used for the internal representation
pub const TARI_ADDRESS_INTERNAL_DUAL_SIZE: usize = 67; // number of bytes used for the internal representation
pub const TARI_ADDRESS_INTERNAL_SINGLE_SIZE: usize = 35; // number of bytes used for the internal representation
const INTERNAL_DUAL_BASE58_MIN_SIZE: usize = 89; // number of bytes used for the internal representation
const INTERNAL_DUAL_BASE58_MAX_SIZE: usize = 91; // number of bytes used for the internal representation
const INTERNAL_SINGLE_MIN_BASE58_SIZE: usize = 45; // number of bytes used for the internal representation
Expand Down Expand Up @@ -152,13 +152,23 @@ impl TariAddress {
TariAddress::Single(SingleAddress::new_with_interactive_only(spend_key, network))
}

/// Gets the bytes size of the Tari Address
pub fn get_size(&self) -> usize {
match self {
TariAddress::Dual(_) => TARI_ADDRESS_INTERNAL_DUAL_SIZE,
TariAddress::Single(_) => TARI_ADDRESS_INTERNAL_SINGLE_SIZE,
}
}

/// helper function to convert emojis to u8
fn emoji_to_bytes(emoji: &str) -> Result<Vec<u8>, TariAddressError> {
// The string must be the correct size, including the checksum
if !(emoji.chars().count() == INTERNAL_SINGLE_SIZE || emoji.chars().count() == INTERNAL_DUAL_SIZE) {
if !(emoji.chars().count() == TARI_ADDRESS_INTERNAL_SINGLE_SIZE ||
emoji.chars().count() == TARI_ADDRESS_INTERNAL_DUAL_SIZE)
{
return Err(TariAddressError::InvalidSize);
}
if emoji.chars().count() == INTERNAL_SINGLE_SIZE {
if emoji.chars().count() == TARI_ADDRESS_INTERNAL_SINGLE_SIZE {
SingleAddress::emoji_to_bytes(emoji)
} else {
DualAddress::emoji_to_bytes(emoji)
Expand Down Expand Up @@ -229,10 +239,10 @@ impl TariAddress {
/// Construct Tari Address from bytes
pub fn from_bytes(bytes: &[u8]) -> Result<TariAddress, TariAddressError>
where Self: Sized {
if !(bytes.len() == INTERNAL_SINGLE_SIZE || bytes.len() == INTERNAL_DUAL_SIZE) {
if !(bytes.len() == TARI_ADDRESS_INTERNAL_SINGLE_SIZE || bytes.len() == TARI_ADDRESS_INTERNAL_DUAL_SIZE) {
return Err(TariAddressError::InvalidSize);
}
if bytes.len() == INTERNAL_SINGLE_SIZE {
if bytes.len() == TARI_ADDRESS_INTERNAL_SINGLE_SIZE {
Ok(TariAddress::Single(SingleAddress::from_bytes(bytes)?))
} else {
Ok(TariAddress::Dual(DualAddress::from_bytes(bytes)?))
Expand Down Expand Up @@ -345,7 +355,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_SINGLE_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_SINGLE_SIZE);

// Generate an emoji ID from the emoji string and ensure we recover it
let emoji_id_from_emoji_string = TariAddress::from_emoji_string(&emoji_string).unwrap();
Expand All @@ -370,7 +380,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_SINGLE_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_SINGLE_SIZE);

// Generate an emoji ID from the emoji string and ensure we recover it
let emoji_id_from_emoji_string = TariAddress::from_emoji_string(&emoji_string).unwrap();
Expand All @@ -395,7 +405,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_SINGLE_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_SINGLE_SIZE);

// Generate an emoji ID from the emoji string and ensure we recover it
let emoji_id_from_emoji_string = TariAddress::from_emoji_string(&emoji_string).unwrap();
Expand Down Expand Up @@ -424,7 +434,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_DUAL_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_DUAL_SIZE);

let features = emoji_id_from_public_key.features();
assert_eq!(features, TariAddressFeatures::create_interactive_and_one_sided());
Expand Down Expand Up @@ -453,7 +463,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_DUAL_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_DUAL_SIZE);

let features = emoji_id_from_public_key.features();
assert_eq!(features, TariAddressFeatures::create_interactive_only());
Expand Down Expand Up @@ -482,7 +492,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_DUAL_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_DUAL_SIZE);

let features = emoji_id_from_public_key.features();
assert_eq!(features, TariAddressFeatures::create_one_sided_only());
Expand Down
18 changes: 9 additions & 9 deletions base_layer/common_types/src/tari_address/single_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
TariAddressFeatures,
INTERNAL_SINGLE_MAX_BASE58_SIZE,
INTERNAL_SINGLE_MIN_BASE58_SIZE,
INTERNAL_SINGLE_SIZE,
TARI_ADDRESS_INTERNAL_SINGLE_SIZE,
},
types::PublicKey,
};
Expand Down Expand Up @@ -69,12 +69,12 @@ impl SingleAddress {
/// helper function to convert emojis to u8
pub fn emoji_to_bytes(emoji: &str) -> Result<Vec<u8>, TariAddressError> {
// The string must be the correct size, including the checksum
if emoji.chars().count() != INTERNAL_SINGLE_SIZE {
if emoji.chars().count() != TARI_ADDRESS_INTERNAL_SINGLE_SIZE {
return Err(TariAddressError::InvalidSize);
}

// Convert the emoji string to a byte array
let mut bytes = Vec::<u8>::with_capacity(INTERNAL_SINGLE_SIZE);
let mut bytes = Vec::<u8>::with_capacity(TARI_ADDRESS_INTERNAL_SINGLE_SIZE);
for c in emoji.chars() {
if let Some(i) = REVERSE_EMOJI.get(&c) {
bytes.push(*i);
Expand Down Expand Up @@ -117,7 +117,7 @@ impl SingleAddress {
/// Construct Tari Address from bytes
pub fn from_bytes(bytes: &[u8]) -> Result<Self, TariAddressError>
where Self: Sized {
if bytes.len() != INTERNAL_SINGLE_SIZE {
if bytes.len() != TARI_ADDRESS_INTERNAL_SINGLE_SIZE {
return Err(TariAddressError::InvalidSize);
}
if validate_checksum(bytes).is_err() {
Expand All @@ -135,8 +135,8 @@ impl SingleAddress {
}

/// Convert Tari Address to bytes
pub fn to_bytes(&self) -> [u8; INTERNAL_SINGLE_SIZE] {
let mut buf = [0u8; INTERNAL_SINGLE_SIZE];
pub fn to_bytes(&self) -> [u8; TARI_ADDRESS_INTERNAL_SINGLE_SIZE] {
let mut buf = [0u8; TARI_ADDRESS_INTERNAL_SINGLE_SIZE];
buf[0] = self.network.as_byte();
buf[1] = self.features.0;
buf[2..34].copy_from_slice(self.public_spend_key.as_bytes());
Expand Down Expand Up @@ -214,7 +214,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_SINGLE_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_SINGLE_SIZE);

// Generate an emoji ID from the emoji string and ensure we recover it
let emoji_id_from_emoji_string = SingleAddress::from_emoji_string(&emoji_string).unwrap();
Expand All @@ -239,7 +239,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_SINGLE_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_SINGLE_SIZE);
// Generate an emoji ID from the emoji string and ensure we recover it
let emoji_id_from_emoji_string = SingleAddress::from_emoji_string(&emoji_string).unwrap();
assert_eq!(emoji_id_from_emoji_string.to_emoji_string(), emoji_string);
Expand All @@ -263,7 +263,7 @@ mod test {

// Check the size of the corresponding emoji string
let emoji_string = emoji_id_from_public_key.to_emoji_string();
assert_eq!(emoji_string.chars().count(), INTERNAL_SINGLE_SIZE);
assert_eq!(emoji_string.chars().count(), TARI_ADDRESS_INTERNAL_SINGLE_SIZE);

// Generate an emoji ID from the emoji string and ensure we recover it
let emoji_id_from_emoji_string = SingleAddress::from_emoji_string(&emoji_string).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ impl UnconfirmedPool {
#[cfg(test)]
mod test {
use tari_common::configuration::Network;
use tari_common_types::tari_address::TariAddress;
use tari_script::{ExecutionStack, TariScript};

use super::*;
Expand Down Expand Up @@ -993,6 +994,7 @@ mod test {
change.script_key_id.clone(),
change.commitment_mask_key_id.clone(),
Covenant::default(),
TariAddress::default(),
);

let test_params = TestParams::new(&key_manager).await;
Expand Down
3 changes: 3 additions & 0 deletions base_layer/core/src/transactions/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use tari_common::configuration::Network;
use tari_common_sqlite::{error::SqliteStorageError, sqlite_connection_pool::PooledDbConnection};
use tari_common_types::{
key_branches::TransactionKeyManagerBranch,
tari_address::TariAddress,
types::{Commitment, PrivateKey, PublicKey, Signature},
};
use tari_crypto::keys::{PublicKey as PK, SecretKey};
Expand Down Expand Up @@ -650,6 +651,7 @@ pub async fn create_transaction_with(
change.script_key_id,
change.commitment_mask_key_id,
Covenant::default(),
TariAddress::default(),
);
for input in inputs {
stx_builder.with_input(input).await.unwrap();
Expand Down Expand Up @@ -720,6 +722,7 @@ pub async fn create_stx_protocol_internal(
change.script_key_id,
change.commitment_mask_key_id,
Covenant::default(),
TariAddress::default(),
);

for tx_input in &schema.from {
Expand Down
Loading

0 comments on commit e97afc5

Please sign in to comment.