Add new utility methods on Transaction type

This PR adds the txid(), is_coin_base(), is_explicitly_rbf(),
and is_lock_time_enabled() methods.
Fixes #303
This commit is contained in:
thunderbiscuit 2023-03-10 15:36:58 -05:00
parent d3e183a498
commit e0506deffa
No known key found for this signature in database
GPG Key ID: 88253696EB836462
4 changed files with 53 additions and 11 deletions

8
Cargo.lock generated
View File

@ -1197,18 +1197,18 @@ checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.38" version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.38" version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -288,13 +288,15 @@ class Blockchain(
* @param transactionBytes The transaction bytes, bitcoin consensus encoded. * @param transactionBytes The transaction bytes, bitcoin consensus encoded.
*/ */
class Transaction(transactionBytes: List<UByte>) { class Transaction(transactionBytes: List<UByte>) {
/** Return the transaction bytes, bitcoin consensus encoded. */ /** Computes the txid. */
fun serialize(): List<UByte> {} fun txid(): String {}
/** /**
* Returns the "weight" of this transaction, as defined by BIP141. * Returns the "weight" of this transaction, as defined by BIP141.
* *
* For transactions with an empty witness, this is simply the consensus-serialized size times four. For transactions with a witness, this is the non-witness consensus-serialized size multiplied by three plus the with-witness consensus-serialized size. * For transactions with an empty witness, this is simply the consensus-serialized size times four.
* For transactions with a witness, this is the non-witness consensus-serialized size multiplied by three
* plus the with-witness consensus-serialized size.
*/ */
fun weight(): ULong {} fun weight(): ULong {}
@ -304,9 +306,25 @@ class Transaction(transactionBytes: List<UByte>) {
/** /**
* Returns the "virtual size" (vsize) of this transaction. * Returns the "virtual size" (vsize) of this transaction.
* *
* Will be ceil(weight / 4.0). Note this implements the virtual size as per BIP141, which is different to what is implemented in Bitcoin Core. The computation should be the same for any remotely sane transaction. * Will be ceil(weight / 4.0). Note this implements the virtual size as per BIP141, which is different to
* what is implemented in Bitcoin Core. The computation should be the same for any remotely sane transaction.
*/ */
fun vsize(): ULong {} fun vsize(): ULong {}
/** Return the transaction bytes, bitcoin consensus encoded. */
fun serialize(): List<UByte> {}
/** Is this a coin base transaction? */
fun isCoinBase(): Boolean {}
/**
* Returns true if the transaction itself opted in to be BIP-125-replaceable (RBF).
* This does not cover the case where a transaction becomes replaceable due to ancestors being RBF.
*/
fun isExplicitlyRbf(): Boolean {}
/** Returns true if this transactions nLockTime is enabled (BIP-65). */
fun isLockTimeEnabled(): Boolean {}
} }
/** /**

View File

@ -247,13 +247,21 @@ interface Transaction {
[Throws=BdkError] [Throws=BdkError]
constructor(sequence<u8> transaction_bytes); constructor(sequence<u8> transaction_bytes);
sequence<u8> serialize(); string txid();
u64 weight(); u64 weight();
u64 size(); u64 size();
u64 vsize(); u64 vsize();
sequence<u8> serialize();
boolean is_coin_base();
boolean is_explicitly_rbf();
boolean is_lock_time_enabled();
}; };
interface PartiallySignedTransaction { interface PartiallySignedTransaction {

View File

@ -251,8 +251,8 @@ impl Transaction {
Ok(Transaction { internal: tx }) Ok(Transaction { internal: tx })
} }
fn serialize(&self) -> Vec<u8> { fn txid(&self) -> String {
self.internal.serialize() self.internal.txid().to_string()
} }
fn weight(&self) -> u64 { fn weight(&self) -> u64 {
@ -266,6 +266,22 @@ impl Transaction {
fn vsize(&self) -> u64 { fn vsize(&self) -> u64 {
self.internal.vsize() as u64 self.internal.vsize() as u64
} }
fn serialize(&self) -> Vec<u8> {
self.internal.serialize()
}
fn is_coin_base(&self) -> bool {
self.internal.is_coin_base()
}
fn is_explicitly_rbf(&self) -> bool {
self.internal.is_explicitly_rbf()
}
fn is_lock_time_enabled(&self) -> bool {
self.internal.is_lock_time_enabled()
}
} }
/// A Bitcoin address. /// A Bitcoin address.