Clean up input and output methods on Transaction type
This commit is contained in:
parent
8e51756a3a
commit
e86909ab3d
@ -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,
|
||||
@ -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> {}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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>>
|
||||
)
|
||||
|
||||
/**
|
||||
|
@ -275,9 +275,9 @@ interface Transaction {
|
||||
|
||||
u32 lock_time();
|
||||
|
||||
sequence<TxIn> inputs();
|
||||
sequence<TxIn> input();
|
||||
|
||||
sequence<TxOut> outputs();
|
||||
sequence<TxOut> output();
|
||||
};
|
||||
|
||||
interface PartiallySignedTransaction {
|
||||
|
@ -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,
|
||||
@ -255,49 +266,33 @@ 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,
|
||||
})
|
||||
Ok(Transaction { internal: tx })
|
||||
}
|
||||
|
||||
fn txid(&self) -> String {
|
||||
@ -333,19 +328,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()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user