Skip to content

Commit

Permalink
rust: get_unspent_outputs: do not serialize liquid fields
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoComandini committed Apr 14, 2022
1 parent 95151ec commit cd547fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
22 changes: 11 additions & 11 deletions subprojects/gdk_rust/gdk_common/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,12 @@ pub struct Txo {
}

impl Txo {
pub fn confidential(&self) -> Option<bool> {
self.txoutsecrets.as_ref().map(is_confidential_txoutsecrets)
}

pub fn is_confidential(&self) -> bool {
match &self.txoutsecrets {
None => false,
Some(s) => is_confidential_txoutsecrets(&s),
}
self.confidential().unwrap_or(false)
}

pub fn asset_id(&self) -> Option<elements::issuance::AssetId> {
Expand All @@ -781,7 +782,6 @@ pub struct UnspentOutput {
pub txhash: String,
/// `true` iff belongs to internal chain, i.e. is change
pub is_internal: bool,
pub confidential: bool,
pub user_path: Vec<ChildNumber>,
#[serde(skip)]
pub scriptpubkey: BEScript,
Expand All @@ -793,19 +793,19 @@ pub struct UnspentOutput {
pub public_key: String,

// liquid fields
pub asset_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub confidential: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub asset_id: Option<String>,
}

impl TryFrom<Txo> for UnspentOutput {
type Error = Error;

fn try_from(txo: Txo) -> Result<Self, Error> {
let (is_internal, pointer) = parse_path(&txo.user_path.clone().into())?;
let asset_id = match &txo.txoutsecrets {
None => "".into(),
Some(s) => s.asset.to_hex(),
};
let confidential = txo.is_confidential();
let asset_id = txo.txoutsecrets.as_ref().map(|s| s.asset.to_hex());
let confidential = txo.confidential();
Ok(Self {
txhash: txo.outpoint.txid().to_hex(),
pt_idx: txo.outpoint.vout(),
Expand Down
18 changes: 14 additions & 4 deletions subprojects/gdk_rust/gdk_rust/tests/test_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,19 @@ impl TestSession {
utxos_opt.confidential_utxos_only = Some(true);
let utxos = self.session.get_unspent_outputs(&utxos_opt).unwrap();
assert_eq!(init_num_utxos, utxos.0.get(policy_asset).unwrap().len());
assert!(utxos.0.get(policy_asset).unwrap().iter().all(|u| u.confidential));
assert!(utxos.0.get(policy_asset).unwrap().iter().all(|u| u.confidential.unwrap_or(false)));
// confidential and unconfidential balance (default)
assert_eq!(init_sat + unconf_sat, self.balance_account(0, None, Some(false)));
utxos_opt.confidential_utxos_only = Some(false);
let utxos = self.session.get_unspent_outputs(&utxos_opt).unwrap();
assert_eq!(init_num_utxos + 1, utxos.0.get(policy_asset).unwrap().len());
assert!(utxos.0.get(policy_asset).unwrap().iter().any(|u| u.confidential));
assert!(utxos.0.get(policy_asset).unwrap().iter().any(|u| !u.confidential));
assert!(utxos.0.get(policy_asset).unwrap().iter().any(|u| u.confidential.unwrap_or(false)));
assert!(utxos
.0
.get(policy_asset)
.unwrap()
.iter()
.any(|u| !u.confidential.unwrap_or(false)));

// Spend only confidential utxos
let node_address = self.node_getnewaddress(None);
Expand Down Expand Up @@ -669,7 +674,12 @@ impl TestSession {
let mut utxos = self.session.get_unspent_outputs(&utxos_opt).unwrap();
utxos.0.get_mut(policy_asset).unwrap().retain(|e| e.txhash == unconf_txid);
assert_eq!(utxos.0.get(policy_asset).unwrap().len(), 1);
assert!(utxos.0.get(policy_asset).unwrap().iter().all(|u| !u.confidential));
assert!(utxos
.0
.get(policy_asset)
.unwrap()
.iter()
.all(|u| !u.confidential.unwrap_or(false)));
let sat = unconf_sat / 2;
let txid = self.send_tx(&node_address, sat, None, None, Some(utxos), None, None);
self.list_tx_contains(&txid, &[node_address], true);
Expand Down

0 comments on commit cd547fe

Please sign in to comment.