Skip to content

Commit

Permalink
Clean up input and output methods on Transaction type
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Mar 20, 2023
1 parent 8e51756 commit b143b2b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 41 deletions.
40 changes: 38 additions & 2 deletions api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ sealed class BlockchainConfig {
* @property confirmationTime If the transaction is confirmed, [BlockTime] contains height and timestamp of the block containing the transaction. This property is null for unconfirmed transactions.
*/
data class TransactionDetails (
var transaction?: Transaction,
var fee: ULong?,
var received: ULong,
var sent: ULong,
Expand Down Expand Up @@ -325,6 +326,23 @@ class Transaction(transactionBytes: List<UByte>) {

/** Returns true if this transactions nLockTime is enabled (BIP-65). */
fun isLockTimeEnabled(): Boolean {}

/** The protocol version, is currently expected to be 1 or 2 (BIP 68). */
fun version(): Int {}

/**
* Block height or timestamp. Transaction cannot be included in a block until this height/time.
* Relevant BIPs
* BIP-65 OP_CHECKLOCKTIMEVERIFY
* BIP-113 Median time-past as endpoint for lock-time calculations
*/
fun lockTime(): UInt {}

/** List of transaction inputs. */
fun input(): List<TxIn> {}

/** List of transaction outputs. */
fun output(): List<TxOut> {}
}

/**
Expand Down Expand Up @@ -366,11 +384,29 @@ data class OutPoint (
* A transaction output, which defines new coins to be created from old ones.
*
* @property value The value of the output, in satoshis.
* @property address The address of the output.
* @property scriptPubkey The script which must be satisfied for the output to be spent.
*/
data class TxOut (
var value: ULong,
var address: String
var scriptPubkey: Script
)

/**
* Bitcoin transaction input.
*
* It contains the location of the previous transaction’s output, that it spends and set of scripts that satisfy its spending conditions.
*
* @property previousOutput The reference to the previous output that is being used an an input.
* @property scriptSig The script which pushes values on the stack which will cause the referenced output’s script to be accepted.
* @property sequence The sequence number, which suggests to miners which of two conflicting transactions should be preferred, or 0xFFFFFFFF to ignore this feature. This is generally never used since the miner behaviour cannot be enforced.
* @property witness Witness data: an array of byte-arrays. Note that this field is not (de)serialized with the rest of the TxIn in Encodable/Decodable, as it is (de)serialized at the end of the full Transaction. It is (de)serialized with the rest of the TxIn in other (de)serialization routines.
*
*/
data class TxIn (
var previousOutput: OutPoint,
var scriptSig: Script,
var sequence: UInt,
var witness: List<List<UByte>>
)

/**
Expand Down
4 changes: 2 additions & 2 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ interface Transaction {

u32 lock_time();

sequence<TxIn> inputs();
sequence<TxIn> input();

sequence<TxOut> outputs();
sequence<TxOut> output();
};

interface PartiallySignedTransaction {
Expand Down
71 changes: 34 additions & 37 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ pub struct TxOut {
script_pubkey: Arc<Script>,
}

impl From<&bdk::bitcoin::blockdata::transaction::TxOut> for TxOut {
fn from(x: &bdk::bitcoin::blockdata::transaction::TxOut) -> TxOut {
TxOut {
value: x.value,
script_pubkey: Arc::new(Script {
script: x.script_pubkey.clone(),
}),
}
}
}

pub struct LocalUtxo {
outpoint: OutPoint,
txout: TxOut,
Expand Down Expand Up @@ -255,48 +266,34 @@ pub struct TxIn {
pub witness: Vec<Vec<u8>>,
}

impl From<&bdk::bitcoin::blockdata::transaction::TxIn> for TxIn {
fn from(x: &bdk::bitcoin::blockdata::transaction::TxIn) -> TxIn {
TxIn {
previous_output: OutPoint {
txid: x.previous_output.txid.to_string(),
vout: x.previous_output.vout,
},
script_sig: Arc::new(Script {
script: x.script_sig.clone(),
}),
sequence: x.sequence.0,
witness: x.witness.to_vec(),
}
}
}

/// A Bitcoin transaction.
#[derive(Debug, Clone)]
pub struct Transaction {
internal: BdkTransaction,
pub version: i32,
pub lock_time: u32,
pub inputs: Vec<TxIn>,
pub outputs: Vec<TxOut>,
}

impl Transaction {
fn new(transaction_bytes: Vec<u8>) -> Result<Self, BdkError> {
let mut decoder = Cursor::new(transaction_bytes);
let tx: BdkTransaction = BdkTransaction::consensus_decode(&mut decoder)?;
let inputs: Vec<TxIn> = tx
.input
.iter()
.map(|input| TxIn {
previous_output: OutPoint {
txid: input.previous_output.txid.to_string(),
vout: input.previous_output.vout,
},
script_sig: Arc::new(Script::from(input.script_sig.clone())),
sequence: input.sequence.0,
witness: input.witness.to_vec(),
})
.collect();
let outputs: Vec<TxOut> = tx
.output
.iter()
.map(|output| TxOut {
value: output.value,
script_pubkey: Arc::new(Script::from(output.script_pubkey.clone())),
})
.collect();

Ok(Transaction {
internal: tx.clone(),
version: tx.version,
lock_time: tx.lock_time.0,
inputs,
outputs,
internal: tx,
})
}

Expand Down Expand Up @@ -333,19 +330,19 @@ impl Transaction {
}

fn version(&self) -> i32 {
self.version
self.internal.version
}

fn lock_time(&self) -> u32 {
self.lock_time
self.internal.lock_time.0
}

fn inputs(&self) -> Vec<TxIn> {
self.inputs.clone()
fn input(&self) -> Vec<TxIn> {
self.internal.input.iter().map(|x| x.into()).collect()
}

fn outputs(&self) -> Vec<TxOut> {
self.outputs.clone()
fn output(&self) -> Vec<TxOut> {
self.internal.output.iter().map(|x| x.into()).collect()
}
}

Expand Down

0 comments on commit b143b2b

Please sign in to comment.