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

Ability to detect and spend unblinded addresses #42

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions rust/src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ impl From<AssetIdBTreeMapInt> for Balances {
.into_iter()
.map(|(key, value)| Balance {
asset_id: key.to_string(),
value,
value: value,
blinded: true,
})
.collect()
}
Expand All @@ -68,14 +69,13 @@ impl From<AssetIdHashMapInt> for Balances {
.into_iter()
.map(|(key, value)| Balance {
asset_id: key.to_string(),
value,
value: value,
blinded: true,
})
.collect()
}
}

use std::convert::TryFrom;

use super::error::LwkError;

impl From<AssetIdBTreeMapUInt> for Balances {
Expand All @@ -87,6 +87,7 @@ impl From<AssetIdBTreeMapUInt> for Balances {
Ok(converted_value) => Some(Balance {
asset_id: key.to_string(),
value: converted_value,
blinded: true,
}),
Err(_) => {
eprintln!("Warning: Overflow encountered converting {} to i64", value);
Expand Down Expand Up @@ -121,6 +122,7 @@ impl From<WalletTxOut> for TxOut {
pub struct Balance {
pub asset_id: String,
pub value: i64,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason balance value is i64 is because in the list of transactions a send tx shows a negative Balance

pub blinded: bool,
}

#[derive(Clone, Debug, PartialEq)]
Expand Down
70 changes: 67 additions & 3 deletions rust/src/api/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::descriptor::Descriptor;
use super::error::LwkError;
use super::types::Address;
use super::types::AssetIdBTreeMapUInt;
use super::types::Balance;
use super::types::Balances;
use super::types::Network;
use super::types::PsetAmounts;
Expand Down Expand Up @@ -81,9 +82,33 @@ impl Wallet {
}

pub fn balances(&self) -> anyhow::Result<Balances, LwkError> {
let balance_map: AssetIdBTreeMapUInt = (self.get_wallet()?.balance()?).into();
let balance = Balances::from(balance_map);
Ok(balance)
let wallet = self.get_wallet()?;
let mut balance_map = wallet.balance()?;
let explicit_utxos = wallet.explicit_utxos()?;

let ublinded_balances: Balances = explicit_utxos.iter()
.filter_map(|utxo| match i64::try_from(utxo.unblinded.value) {
Ok(converted_value) => Some(Balance {
asset_id: utxo.unblinded.asset.to_string(),
value: converted_value,
blinded: true,
}),
Err(_) => {
eprintln!("Warning: Overflow encountered converting {} to i64", utxo.unblinded.value);
None
}
})
.collect();

for utxo in explicit_utxos {
balance_map.insert(utxo.unblinded.asset, utxo.unblinded.value);
}

let balance_map: AssetIdBTreeMapUInt = balance_map.into();

let mut balances = Balances::from(balance_map);
balances.extend(ublinded_balances);
Ok(balances)
}

pub fn txs(&self) -> anyhow::Result<Vec<Tx>, LwkError> {
Expand Down Expand Up @@ -143,6 +168,45 @@ impl Wallet {
Ok(pset.to_string())
}

pub fn build_unblinded_tx(
&self,
sats: u64,
out_address: String,
fee_rate: f32,
asset: String,
) -> anyhow::Result<String, LwkError> {
let wallet = self.get_wallet()?;
let tx_builder = wallet.tx_builder();
let address = LwkAddress::from_str(&out_address)?;
let asset = match LwkAssetId::from_str(&asset) {
Ok(result) => result,
Err(_) => {
return Err(LwkError {
msg: "Invalid asset".to_string(),
})
}
};
let external_utxos = wallet.explicit_utxos()?;
let matching_utxo = external_utxos
.into_iter()
.filter(|e_utxo| e_utxo.unblinded.asset == asset)
.next();

match matching_utxo {
Some(external_utxo) => {
let pset = tx_builder
.add_recipient(&address, sats, asset)?
.fee_rate(Some(fee_rate))
.add_external_utxos(vec![external_utxo])?
.finish()?;
Ok(pset.to_string())
}
None => Err(LwkError {
msg: "Asset Id not found".to_owned(),
}),
}
}

pub fn decode_tx(&self, pset: String) -> anyhow::Result<PsetAmounts, LwkError> {
let mut pset = PartiallySignedTransaction::from_str(&pset)?;
let pset_details = self.get_wallet()?.get_details(&mut pset)?;
Expand Down