Skip to content

Commit cc2881a

Browse files
committed
rust: TransactionMeta: add transaction_outputs
No support for Liquid yet.
1 parent ac3d79c commit cc2881a

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

subprojects/gdk_rust/gdk_common/src/model.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ pub struct TransactionMeta {
321321
pub version: u32,
322322
#[serde(rename = "transaction_locktime")]
323323
pub lock_time: u32,
324+
pub transaction_outputs: Vec<TransactionOutput>,
324325
}
325326

326327
impl From<BETransaction> for TransactionMeta {
@@ -355,6 +356,7 @@ impl From<BETransaction> for TransactionMeta {
355356
used_utxos: vec![],
356357
version: transaction.version(),
357358
lock_time: transaction.lock_time(),
359+
transaction_outputs: vec![],
358360
}
359361
}
360362
}
@@ -396,6 +398,25 @@ impl TransactionMeta {
396398
}
397399
}
398400

401+
#[derive(Debug, Clone, Serialize, Deserialize)]
402+
pub struct TransactionOutput {
403+
pub address: String, // Only used by Trezor
404+
pub address_type: String,
405+
406+
/// True if the corresponding scriptpubkey belongs to the account (not the wallet)
407+
pub is_relevant: bool,
408+
pub is_change: bool, // Same as is_relevant
409+
410+
pub subaccount: u32,
411+
pub is_internal: bool,
412+
pub pointer: u32, // child_number in bip32 terminology
413+
pub user_path: Vec<ChildNumber>,
414+
415+
pub pt_idx: u32, // vout
416+
pub script: String, // script code
417+
pub satoshi: u64,
418+
}
419+
399420
#[derive(Debug, Clone, Serialize, Deserialize)]
400421
pub struct AddressIO {
401422
pub address: String,

subprojects/gdk_rust/gdk_electrum/src/account.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use gdk_common::be::{
2222
use gdk_common::error::fn_err;
2323
use gdk_common::model::{
2424
parse_path, AccountInfo, AddressAmount, AddressPointer, Balances, CreateTransaction,
25-
GetTransactionsOpt, SPVVerifyTxResult, TransactionMeta, UnspentOutput, UpdateAccountOpt,
26-
UtxoStrategy,
25+
GetTransactionsOpt, SPVVerifyTxResult, TransactionMeta, TransactionOutput, UnspentOutput,
26+
UpdateAccountOpt, UtxoStrategy,
2727
};
2828
use gdk_common::scripts::{p2pkh_script, p2shwpkh_script_sig, ScriptType};
2929
use gdk_common::util::is_confidential_txoutsecrets;
@@ -308,6 +308,49 @@ impl Account {
308308
p2pkh_script(public_key).into()
309309
}
310310

311+
pub fn tx_outputs(&self, tx: &BETransaction) -> Result<Vec<TransactionOutput>, Error> {
312+
let store_read = self.store.read()?;
313+
let acc_store = store_read.account_cache(self.account_num)?;
314+
let mut tx_outputs = vec![];
315+
for vout in 0..tx.output_len() as u32 {
316+
let address = tx.output_address(vout, self.network.id()).unwrap_or_default();
317+
let satoshi = tx.output_value(vout, &acc_store.unblinded).unwrap_or_default();
318+
let script_pubkey = tx.output_script(vout);
319+
tx_outputs.push(match acc_store.paths.get(&script_pubkey) {
320+
None => TransactionOutput {
321+
address,
322+
satoshi,
323+
address_type: "".into(),
324+
is_relevant: false,
325+
is_change: false,
326+
subaccount: self.account_num,
327+
is_internal: false,
328+
pointer: 0,
329+
pt_idx: vout,
330+
script: "".into(),
331+
user_path: vec![],
332+
},
333+
Some(account_path) => {
334+
let (is_internal, pointer) = parse_path(&account_path)?;
335+
TransactionOutput {
336+
address,
337+
satoshi,
338+
address_type: self.script_type.to_string(),
339+
is_relevant: true,
340+
is_change: true, // same as is_relevant
341+
subaccount: self.account_num,
342+
is_internal,
343+
pointer,
344+
pt_idx: vout,
345+
script: self.script_code(&account_path).to_hex(),
346+
user_path: self.get_full_path(&account_path).into(),
347+
}
348+
}
349+
});
350+
}
351+
Ok(tx_outputs)
352+
}
353+
311354
fn txo(&self, outpoint: &BEOutPoint) -> Result<UnspentOutput, Error> {
312355
let vout = outpoint.vout();
313356
let txid = outpoint.txid();
@@ -1243,6 +1286,7 @@ pub fn create_tx(
12431286
}
12441287

12451288
let used_utxos = account.used_utxos(&tx)?;
1289+
let tx_outputs = account.tx_outputs(&tx)?;
12461290
let mut created_tx = TransactionMeta::new(
12471291
tx,
12481292
None,
@@ -1256,6 +1300,7 @@ pub fn create_tx(
12561300
SPVVerifyTxResult::InProgress,
12571301
);
12581302
created_tx.used_utxos = used_utxos;
1303+
created_tx.transaction_outputs = tx_outputs;
12591304
created_tx.changes_used = Some(changes.len() as u32);
12601305
created_tx.addressees_read_only = request.previous_transaction.is_some();
12611306
info!("returning: {:?}", created_tx);

0 commit comments

Comments
 (0)