Skip to content
Closed
Show file tree
Hide file tree
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
15 changes: 3 additions & 12 deletions spdk-core/src/client/spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ use anyhow::{Error, Result};

use crate::constants::{DATA_CARRIER_SIZE, NUMS};

use super::{
OutputSpendStatus, OwnedOutput, Recipient, RecipientAddress, SilentPaymentUnsignedTransaction,
SpClient,
};
use super::{OwnedOutput, Recipient, RecipientAddress, SilentPaymentUnsignedTransaction, SpClient};

impl SpClient {
// For now it's only suitable for wallet that spends only silent payments outputs that it owns
Expand All @@ -38,10 +35,7 @@ impl SpClient {
network: Network,
) -> Result<SilentPaymentUnsignedTransaction> {
// check that all available outputs are unspent
if available_utxos
.iter()
.any(|(_, o)| o.spend_status != OutputSpendStatus::Unspent)
{
if available_utxos.iter().any(|(_, o)| !o.is_unspent()) {
return Err(Error::msg("All outputs must be unspent".to_string()));
}

Expand Down Expand Up @@ -173,10 +167,7 @@ impl SpClient {
network: Network,
) -> Result<SilentPaymentUnsignedTransaction> {
// check that all available outputs are unspent
if available_utxos
.iter()
.any(|(_, o)| o.spend_status != OutputSpendStatus::Unspent)
{
if available_utxos.iter().any(|(_, o)| !o.is_unspent()) {
return Err(Error::msg("All outputs must be unspent".to_string()));
}

Expand Down
49 changes: 40 additions & 9 deletions spdk-core/src/client/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,36 @@ use bitcoin::{
hex::{DisplayHex, FromHex},
key::Secp256k1,
secp256k1::{PublicKey, SecretKey},
Address, Amount, Network, OutPoint, ScriptBuf, Transaction,
Address, Amount, BlockHash, Network, OutPoint, ScriptBuf, Transaction, Txid,
};
use serde::{Deserialize, Serialize};
use silentpayments::{receiving::Label, SilentPaymentAddress};

type SpendingTxId = [u8; 32];
type MinedInBlock = [u8; 32];
#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SpendInfo {
pub spending_txid: Option<Txid>,
pub mined_in_block: Option<BlockHash>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum OutputSpendStatus {
Unspent,
Spent(SpendingTxId),
Mined(MinedInBlock),
impl SpendInfo {
pub fn new_empty() -> Self {
Self {
spending_txid: None,
mined_in_block: None,
}
}

pub fn is_unspent(&self) -> bool {
self.spending_txid.is_none() && self.mined_in_block.is_none()
}

pub fn is_spent_unconfirmed(&self) -> bool {
self.spending_txid.is_some() && self.mined_in_block.is_none()
}

pub fn is_mined(&self) -> bool {
self.mined_in_block.is_some()
}
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
Expand All @@ -29,7 +46,21 @@ pub struct OwnedOutput {
pub amount: Amount,
pub script: ScriptBuf,
pub label: Option<Label>,
pub spend_status: OutputSpendStatus,
pub spend_info: SpendInfo,
}

impl OwnedOutput {
pub fn is_unspent(&self) -> bool {
self.spend_info.is_unspent()
}

pub fn is_mined(&self) -> bool {
self.spend_info.is_mined()
}

pub fn is_spent_unconfirmed(&self) -> bool {
self.spend_info.is_spent_unconfirmed()
}
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
Expand Down
5 changes: 3 additions & 2 deletions spdk-core/src/scanner/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use silentpayments::receiving::Label;

use crate::{
backend::{BlockData, ChainBackend, FilterData, UtxoData},
client::{OutputSpendStatus, OwnedOutput, SpClient},
client::{OwnedOutput, SpClient},
updater::Updater,
SpendInfo,
};

pub struct SpScanner<'a> {
Expand Down Expand Up @@ -198,7 +199,7 @@ impl<'a> SpScanner<'a> {
amount: utxo.value,
script: utxo.scriptpubkey,
label,
spend_status: OutputSpendStatus::Unspent,
spend_info: SpendInfo::new_empty(),
};

res.insert(outpoint, out);
Expand Down
Loading