From 500c560a8bede01f01c6aa37d7cf2586206cc49c Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Fri, 10 Mar 2023 15:36:58 -0500 Subject: [PATCH] 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 --- Cargo.lock | 8 +++--- .../src/main/kotlin/org/bitcoindevkit/bdk.kt | 26 ++++++++++++++++--- bdk-ffi/src/bdk.udl | 10 ++++++- bdk-ffi/src/lib.rs | 20 ++++++++++++-- 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e603bef..0310efaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1197,18 +1197,18 @@ checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" [[package]] name = "thiserror" -version = "1.0.38" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" +checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.38" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" +checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" dependencies = [ "proc-macro2", "quote", diff --git a/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt b/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt index baf89764..3cd6e2f6 100644 --- a/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt +++ b/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt @@ -288,13 +288,15 @@ class Blockchain( * @param transactionBytes The transaction bytes, bitcoin consensus encoded. */ class Transaction(transactionBytes: List) { - /** Return the transaction bytes, bitcoin consensus encoded. */ - fun serialize(): List {} + /** Computes the txid. */ + fun txid(): String {} /** * 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 {} @@ -304,9 +306,25 @@ class Transaction(transactionBytes: List) { /** * 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 {} + + /** Return the transaction bytes, bitcoin consensus encoded. */ + fun serialize(): List {} + + /** 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 {} } /** diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index dea06045..287b5413 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -247,13 +247,21 @@ interface Transaction { [Throws=BdkError] constructor(sequence transaction_bytes); - sequence serialize(); + string txid(); u64 weight(); u64 size(); u64 vsize(); + + sequence serialize(); + + boolean is_coin_base(); + + boolean is_explicitly_rbf(); + + boolean is_lock_time_enabled(); }; interface PartiallySignedTransaction { diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index de67dea3..6cef73a8 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -251,8 +251,8 @@ impl Transaction { Ok(Transaction { internal: tx }) } - fn serialize(&self) -> Vec { - self.internal.serialize() + fn txid(&self) -> String { + self.internal.txid().to_string() } fn weight(&self) -> u64 { @@ -266,6 +266,22 @@ impl Transaction { fn vsize(&self) -> u64 { self.internal.vsize() as u64 } + + fn serialize(&self) -> Vec { + 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.