Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose more of the Transaction type #307

Merged

Conversation

thunderbiscuit
Copy link
Member

@thunderbiscuit thunderbiscuit commented Feb 1, 2023

Description

We've recently exposed the Transaction type, and I think a few methods on it would be useful. This is a draft PR with the first 3: weight(), size(), and vsize() Edit: it's now much more. I think there might be other methods we might want to expose as well. Take a look at the docs to see them all.

Other candidates have now been added:

  1. is_explicitly_rbf
  2. is_lock_time_enabled
  3. is_absolute_timelock_satisfied
  4. is_coin_base
  5. txid

This PR is growing in size but I decided to add all 4 fields on the Transaction type. This is useful because it means we can now add the transaction field on the TransactionDetails type (also added in this PR).

I still have a few questions regarding all the translation between the ffi and bdk/rust types, some of the traits I had to remove (Eq and PartialEq on the TransactionDetails type) as well as the usage of Option<Arc<T>> vs Arc<Option<T>>. Will outline those tomorrow.

Closes #303
Closes #187

Changelog notice

APIs Added
- `Transaction` type now exposes the `.weight()`, `.size()`, `.vsize()`, `is_explicitly_rbf()`, `is_lock_time_enabled()`, `is_coin_base(), `txid()`, `input()`, `output()`, `version()`, and `lock_time()` methods [#307]
- New `TxIn` type exposed [#307]
APIs Changed
- `TransactionDetails` now exposes the optional `transaction` field [#307]
Breaking Changes
- `TxOut` type replaces the `address` field by the `script_pubkey` field [#307]
- The `Wallet.list_transaction()` method now takes the `include_raw: bool` argument [#307]

[#307]: https://github.com/bitcoindevkit/bdk-ffi/pull/307

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

@thunderbiscuit
Copy link
Member Author

thunderbiscuit commented Mar 10, 2023

I added the new methods.

You can test the PR by building bdk-jvm with a custom name and publishing to local maven, and then running the following script:

@file:Repository("file:///~/.m2/repository/")
@file:DependsOn("org.bitcoindevkit:bdk-jvm:0.28.0-PR307")

import org.bitcoindevkit.*

val memoryDatabaseConfig = DatabaseConfig.Memory
val blockchainConfig = BlockchainConfig.Electrum(
    ElectrumConfig(
        "ssl://electrum.blockstream.info:60002",
        null,
        5u,
        null,
        100u,
        true,
    )
)

val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
val wallet = Wallet(descriptor, null, Network.TESTNET, memoryDatabaseConfig)
val blockchain = Blockchain(blockchainConfig)
    
wallet.sync(blockchain, null)
val balance = wallet.getBalance()
check(balance.total > 0uL) { 
     "Wallet balance is 0, please add funds!"
}

@OptIn(ExperimentalUnsignedTypes::class)
val faucetAddress = "tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt"
val (psbt, txDetails) = TxBuilder()
        .addRecipient(Address(faucetAddress).scriptPubkey(), 10000u)
        .feeRate(2.0f)
        .enableRbf()
        .finish(wallet)

val tx = psbt.extractTx()
println("Transaction info is:")
println("Coinbase: ${tx.isCoinBase()}")
println("Explicitly signals RBF: ${tx.isExplicitlyRbf()}")
println("Locktime is enabled: ${tx.isLockTimeEnabled()}")
println("Txid: ${tx.txid()}")
println("Weight: ${tx.weight()}")
println("Size: ${tx.size()}")
println("Virtual size: ${tx.vsize()}")
println("Tx version: ${tx.version()}")
println("Tx locktime: ${tx.lockTime()}")
println("Tx inputs: ${tx.inputs()}")
println("Tx outputs: ${tx.outputs()}")

Cargo.toml Show resolved Hide resolved
bdk-ffi/src/lib.rs Outdated Show resolved Hide resolved
bdk-ffi/src/lib.rs Outdated Show resolved Hide resolved
bdk-ffi/src/lib.rs Show resolved Hide resolved
@thunderbiscuit
Copy link
Member Author

@notmandatory this is now ready for review.

@thunderbiscuit thunderbiscuit force-pushed the feat/transaction-methods branch 4 times, most recently from 65e875e to 82ad557 Compare March 21, 2023 17:21
@thunderbiscuit thunderbiscuit changed the title Add weight, size, and vsize methods on the Transaction type Expose more of the Transaction type Mar 23, 2023
bdk-ffi/src/lib.rs Outdated Show resolved Hide resolved
bdk-ffi/src/lib.rs Outdated Show resolved Hide resolved
bdk-ffi/src/lib.rs Outdated Show resolved Hide resolved
bdk-ffi/src/psbt.rs Outdated Show resolved Hide resolved
Copy link
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK cba69e6

@notmandatory notmandatory merged commit d3a6453 into bitcoindevkit:master Mar 23, 2023
@thunderbiscuit thunderbiscuit added this to the Release 0.28.0 milestone Mar 24, 2023
@thunderbiscuit thunderbiscuit deleted the feat/transaction-methods branch November 14, 2023 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Expose utility methods on the Transaction type RUSTSEC-2021-0139: ansi_term is Unmaintained
2 participants