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
8 changes: 4 additions & 4 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ crate-type = ["cdylib", "staticlib"]

[dependencies]
flutter_rust_bridge = "=2.11.1"
spdk-wallet = { git = "https://github.com/cygnet3/spdk", branch = "master" }
spdk-wallet = { git = "https://github.com/cygnet3/spdk", branch = "rm-owned-output" }
lazy_static = "1.4"
anyhow = "1.0"
serde = { version = "1.0.188", features = ["derive"] }
Expand Down
33 changes: 30 additions & 3 deletions rust/src/api/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,42 @@ use std::{
use flutter_rust_bridge::frb;
use serde::{Deserialize, Serialize};
use spdk_wallet::bitcoin::{self, hashes::Hash, hex::DisplayHex};
use spdk_wallet::bitcoin::{Amount, BlockHash, OutPoint, Txid};
use spdk_wallet::client::{OutputSpendStatus, OwnedOutput};
use spdk_wallet::bitcoin::{absolute::Height, Amount, BlockHash, OutPoint, ScriptBuf, Txid};

use anyhow::{Error, Result};

use crate::api::structs::amount::ApiAmount;
use crate::api::structs::owned_output::ApiOwnedOutput;
use crate::stream::StateUpdate;

// Local definition of OwnedOutput and OutputSpendStatus.
// These types were removed from spdk-wallet; kept here temporarily
// so that the existing OwnedOutputs serialization/storage code still works.
// TODO: remove when OwnedOutputs is replaced by SQLite storage.

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) enum OutputSpendStatus {
Unspent,
Spent([u8; 32]),
Mined([u8; 32]),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct OwnedOutput {
pub(crate) blockheight: Height,
pub(crate) tweak: [u8; 32],
pub(crate) amount: Amount,
pub(crate) script: ScriptBuf,
pub(crate) label: Option<String>,
pub(crate) spend_status: OutputSpendStatus,
}

impl OwnedOutput {
pub fn is_unspent(&self) -> bool {
self.spend_status == OutputSpendStatus::Unspent
}
}

#[frb(opaque)]
pub struct OwnedOutPoints(HashSet<OutPoint>);

Expand Down Expand Up @@ -84,7 +111,7 @@ impl OwnedOutputs {
tweak: output.tweak.to_be_bytes(),
amount: output.value,
script: output.script_pubkey.clone(),
label: output.label.clone(),
label: output.label.as_ref().map(|l| l.as_string()),
},
)
}))
Expand Down
3 changes: 2 additions & 1 deletion rust/src/api/structs/output_spend_status.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use spdk_wallet::bitcoin::hex::{self, DisplayHex};
use spdk_wallet::client::OutputSpendStatus;

use crate::api::outputs::OutputSpendStatus;

type SpendingTxId = String;
type MinedInBlock = String;
Expand Down
40 changes: 34 additions & 6 deletions rust/src/api/structs/owned_output.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::{Deserialize, Serialize};
use spdk_wallet::bitcoin::{absolute::Height, ScriptBuf};
use spdk_wallet::client::OwnedOutput;
use spdk_wallet::bitcoin::{absolute::Height, secp256k1::Scalar, ScriptBuf};
use spdk_wallet::updater::DiscoveredOutput;

use crate::api::structs::amount::ApiAmount;
use crate::api::structs::output_spend_status::ApiOutputSpendStatus;
use crate::api::outputs::OwnedOutput;
use crate::api::structs::{amount::ApiAmount, output_spend_status::ApiOutputSpendStatus};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ApiOwnedOutput {
Expand All @@ -15,14 +15,16 @@ pub struct ApiOwnedOutput {
pub spend_status: ApiOutputSpendStatus,
}

// Conversions to/from local OwnedOutput (for OwnedOutputs serialization)

impl From<OwnedOutput> for ApiOwnedOutput {
fn from(value: OwnedOutput) -> Self {
ApiOwnedOutput {
blockheight: value.blockheight.to_consensus_u32(),
tweak: value.tweak,
amount: value.amount.into(),
script: value.script.to_hex_string(),
label: value.label.map(|l| l.as_string()),
label: value.label,
spend_status: value.spend_status.into(),
}
}
Expand All @@ -35,8 +37,34 @@ impl From<ApiOwnedOutput> for OwnedOutput {
tweak: value.tweak,
amount: value.amount.into(),
script: ScriptBuf::from_hex(&value.script).unwrap(),
label: value.label.map(|l| l.try_into().unwrap()),
label: value.label,
spend_status: value.spend_status.into(),
}
}
}

// Conversions to/from DiscoveredOutput (for SpClient transaction API)

impl From<DiscoveredOutput> for ApiOwnedOutput {
fn from(value: DiscoveredOutput) -> Self {
ApiOwnedOutput {
blockheight: 0, // not available in DiscoveredOutput
tweak: value.tweak.to_be_bytes(),
amount: value.value.into(),
script: value.script_pubkey.to_hex_string(),
label: value.label.map(|l| l.as_string()),
spend_status: ApiOutputSpendStatus::Unspent,
}
}
}

impl From<ApiOwnedOutput> for DiscoveredOutput {
fn from(value: ApiOwnedOutput) -> Self {
DiscoveredOutput {
tweak: Scalar::from_be_bytes(value.tweak).unwrap(),
value: value.amount.into(),
script_pubkey: ScriptBuf::from_hex(&value.script).unwrap(),
label: value.label.map(|l| l.try_into().unwrap()),
}
}
}
7 changes: 4 additions & 3 deletions rust/src/api/wallet/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use anyhow::Result;
use bip39::rand::{thread_rng, RngCore};
use spdk_wallet::backend_blindbit_v1::BlindbitClient;
use spdk_wallet::bitcoin::{consensus::serialize, hex::DisplayHex, OutPoint};
use spdk_wallet::client::{FeeRate, OwnedOutput, Recipient, RecipientAddress, SpClient};
use spdk_wallet::client::{FeeRate, Recipient, RecipientAddress, SpClient};
use spdk_wallet::updater::DiscoveredOutput;

use super::SpWallet;

Expand All @@ -23,7 +24,7 @@ impl SpWallet {
network: ApiNetwork,
) -> Result<ApiSilentPaymentUnsignedTransaction> {
let client = &self.client;
let available_utxos: Result<Vec<(OutPoint, OwnedOutput)>> = api_outputs
let available_utxos: Result<Vec<(OutPoint, DiscoveredOutput)>> = api_outputs
.into_iter()
.map(|(string, output)| {
let outpoint = OutPoint::from_str(&string)?;
Expand Down Expand Up @@ -53,7 +54,7 @@ impl SpWallet {
network: ApiNetwork,
) -> Result<ApiSilentPaymentUnsignedTransaction> {
let client = &self.client;
let available_utxos: Result<Vec<(OutPoint, OwnedOutput)>> = api_outputs
let available_utxos: Result<Vec<(OutPoint, DiscoveredOutput)>> = api_outputs
.into_iter()
.map(|(string, output)| {
let outpoint = OutPoint::from_str(&string)?;
Expand Down
6 changes: 3 additions & 3 deletions rust/src/state/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use spdk_wallet::updater::Updater;
use spdk_wallet::{
bitcoin::{absolute::Height, BlockHash, OutPoint},
updater::SimplifiedOutput,
updater::DiscoveredOutput,
};

use crate::stream::{send_scan_progress, send_state_update, ScanProgress, StateUpdate};
Expand All @@ -17,7 +17,7 @@ pub struct StateUpdater {
update: bool,
blkhash: Option<BlockHash>,
blkheight: Option<Height>,
found_outputs: HashMap<OutPoint, SimplifiedOutput>,
found_outputs: HashMap<OutPoint, DiscoveredOutput>,
found_inputs: HashSet<OutPoint>,
}

Expand Down Expand Up @@ -78,7 +78,7 @@ impl Updater for StateUpdater {
&mut self,
height: Height,
blkhash: BlockHash,
found_outputs: HashMap<OutPoint, SimplifiedOutput>,
found_outputs: HashMap<OutPoint, DiscoveredOutput>,
) -> Result<()> {
// may have already been written by record_block_inputs
self.update = true;
Expand Down
4 changes: 2 additions & 2 deletions rust/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::frb_generated::StreamSink;
use lazy_static::lazy_static;
use spdk_wallet::{
bitcoin::{absolute::Height, BlockHash, OutPoint},
updater::SimplifiedOutput,
updater::DiscoveredOutput,
};

lazy_static! {
Expand All @@ -24,7 +24,7 @@ pub enum StateUpdate {
Update {
blkheight: Height,
blkhash: BlockHash,
found_outputs: HashMap<OutPoint, SimplifiedOutput>,
found_outputs: HashMap<OutPoint, DiscoveredOutput>,
found_inputs: HashSet<OutPoint>,
},
}
Expand Down