Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check address features on send #6433

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions base_layer/wallet/src/transaction_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,13 +1092,13 @@ where
reply_channel: oneshot::Sender<Result<TransactionServiceResponse, TransactionServiceError>>,
) -> Result<(), TransactionServiceError> {
let tx_id = TxId::new_random();
if destination.network() != self.resources.interactive_tari_address.network() {
if let Err(e) = self.verify_send(&destination, TariAddressFeatures::create_interactive_only()) {
let _result = reply_channel
.send(Err(TransactionServiceError::InvalidNetwork))
.inspect_err(|_| {
warn!(target: LOG_TARGET, "Failed to send service reply");
});
return Err(TransactionServiceError::InvalidNetwork);
return Err(e);
}
// If we're paying ourselves, let's complete and submit the transaction immediately
if &self
Expand Down Expand Up @@ -1595,6 +1595,7 @@ where
>,
) -> Result<Box<(TxId, PublicKey, TransactionOutput)>, TransactionServiceError> {
let tx_id = TxId::new_random();
self.verify_send(&destination, TariAddressFeatures::create_one_sided_only())?;
// this can be anything, so lets generate a random private key
let pre_image = PublicKey::from_secret_key(&PrivateKey::random(&mut OsRng));
let hash: [u8; 32] = Sha256::digest(pre_image.as_bytes()).into();
Expand All @@ -1608,7 +1609,7 @@ where
HashSha256 PushHash(Box::new(hash)) Equal IfThen
PushPubKey(Box::new(destination.public_spend_key().clone()))
Else
CheckHeightVerify(height) PushPubKey(Box::new(self.resources.node_identity.public_key().clone()))
CheckHeightVerify(height) PushPubKey(Box::new(self.resources.one_sided_tari_address.public_spend_key().clone()))
EndIf
);

Expand Down Expand Up @@ -1830,6 +1831,7 @@ where
payment_id: PaymentId,
) -> Result<TxId, TransactionServiceError> {
let tx_id = TxId::new_random();
self.verify_send(&dest_address, TariAddressFeatures::create_one_sided_only())?;

// For a stealth transaction, the script is not provided because the public key that should be included
// is not known at this stage. This will only be known later. For now,
Expand Down Expand Up @@ -2063,9 +2065,6 @@ where
JoinHandle<Result<TxId, TransactionServiceProtocolError<TxId>>>,
>,
) -> Result<TxId, TransactionServiceError> {
if destination.network() != self.resources.one_sided_tari_address.network() {
return Err(TransactionServiceError::InvalidNetwork);
}
let dest_pubkey = destination.public_spend_key().clone();
self.send_one_sided_or_stealth(
destination,
Expand Down Expand Up @@ -2394,10 +2393,6 @@ where
JoinHandle<Result<TxId, TransactionServiceProtocolError<TxId>>>,
>,
) -> Result<TxId, TransactionServiceError> {
if destination.network() != self.resources.one_sided_tari_address.network() {
return Err(TransactionServiceError::InvalidNetwork);
}

self.send_one_sided_or_stealth(
destination,
amount,
Expand Down Expand Up @@ -3534,6 +3529,23 @@ where
fn connectivity(&self) -> &TWalletConnectivity {
&self.resources.connectivity
}

fn verify_send(
&self,
address: &TariAddress,
sending_method: TariAddressFeatures,
) -> Result<(), TransactionServiceError> {
if address.network() != self.resources.interactive_tari_address.network() {
return Err(TransactionServiceError::InvalidNetwork);
}
if !address.features().contains(sending_method) {
return Err(TransactionServiceError::InvalidAddress(format!(
"Address does not support feature {} ",
sending_method
)));
}
Ok(())
}
}

/// This struct is a collection of the common resources that a protocol in the service requires.
Expand Down
Loading