SpWallet in Device

This commit is contained in:
NicolasCantu 2025-04-08 16:01:16 +02:00
parent d69d179439
commit 985e6dcdc9

View File

@ -13,13 +13,12 @@ use sp_client::{
OutputSpendStatus, OwnedOutput, SpClient
};
use crate::pcd::Member;
use crate::{pcd::Member, silentpayments::SpWallet};
#[derive(Debug, Serialize, Deserialize, Clone, Default, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Device {
sp_client: SpClient,
outputs: HashMap<OutPoint, OwnedOutput>,
sp_wallet: SpWallet,
pairing_process_commitment: Option<OutPoint>,
paired_member: Member,
}
@ -29,31 +28,26 @@ impl Device {
let local_address = sp_client.get_receiving_address();
let member = Member::new(vec![SilentPaymentAddress::try_from(local_address).unwrap()]);
Self {
sp_client,
outputs: HashMap::new(),
sp_wallet: SpWallet::new(sp_client),
pairing_process_commitment: None,
paired_member: member,
}
}
pub fn get_sp_client(&self) -> &SpClient {
&self.sp_client
}
pub fn get_mut_sp_client(&mut self) -> &mut SpClient {
&mut self.sp_client
self.sp_wallet.get_sp_client()
}
pub fn get_outputs(&self) -> &HashMap<OutPoint, OwnedOutput> {
&self.outputs
self.sp_wallet.get_outputs()
}
pub fn get_mut_outputs(&mut self) -> &mut HashMap<OutPoint, OwnedOutput> {
&mut self.outputs
self.sp_wallet.get_mut_outputs()
}
pub fn get_balance(&self) -> Amount {
self.outputs.values()
self.sp_wallet.get_outputs().values()
.filter(|output| output.spend_status == OutputSpendStatus::Unspent)
.fold(Amount::ZERO, |acc, x| acc + x.amount)
}
@ -64,7 +58,8 @@ impl Device {
for i in 0..tx.output.len() {
if self
.outputs
.sp_wallet
.get_outputs()
.contains_key(&OutPoint {
txid,
vout: i as u32,
@ -75,7 +70,7 @@ impl Device {
}
for input in tx.input.iter() {
if let Some(output) = self.outputs.get(&input.previous_output) {
if let Some(output) = self.sp_wallet.get_outputs().get(&input.previous_output) {
match &output.spend_status {
OutputSpendStatus::Spent(tx) => {
if *tx == txid.to_string() {
@ -92,7 +87,7 @@ impl Device {
let shared_secret = calculate_ecdh_shared_secret(
&partial_tweak,
&self.sp_client.get_scan_key(),
&self.sp_wallet.get_sp_client().get_scan_key(),
);
let mut pubkeys_to_check: HashMap<XOnlyPublicKey, u32> = HashMap::new();
for (vout, output) in (0u32..).zip(tx.output.iter()) {
@ -102,7 +97,8 @@ impl Device {
}
}
let ours = self
.sp_client
.sp_wallet
.get_sp_client()
.sp_receiver
.scan_transaction(&shared_secret, pubkeys_to_check.keys().cloned().collect())?;
let mut new_outputs: HashMap<OutPoint, OwnedOutput> = HashMap::new();
@ -131,12 +127,12 @@ impl Device {
}
}
let mut res = new_outputs.clone();
self.outputs.extend(new_outputs);
self.sp_wallet.get_mut_outputs().extend(new_outputs);
let txid = tx.txid().to_string();
// update outputs that we own and that are spent
for input in tx.input.iter() {
if let Some(prevout) = self.outputs.get_mut(&input.previous_output) {
if let Some(prevout) = self.sp_wallet.get_mut_outputs().get_mut(&input.previous_output) {
// This is spent by this tx
prevout.spend_status = OutputSpendStatus::Spent(txid.clone());
res.insert(input.previous_output, prevout.clone());