Compare commits

...

82 Commits

Author SHA1 Message Date
thunderbiscuit
138200db07 Bump version to 0.8.0 2022-07-29 13:59:59 -04:00
thunderbiscuit
f1eb8c678d Update changelog to reflect 0.8.0 additions 2022-07-29 13:37:47 -04:00
Steve Myers
7d95433c28 Merge bitcoindevkit/bdk-ffi#175: Set project MSRV to 1.57.0
6d53cbeb25 Fix fmt error (Steve Myers)
1a12f37a2f Set MSRV to 1.57.0 (Steve Myers)

Pull request description:

  ### Description

  The `master` branch is currently not passing the new github checks.

  A required dependency of `bdk-ffi-bindgen` is `uniffi_bindgen` and it depends on `os_str_bytes` v6.2.0 which has a MSRV of 1.57.0. I therefore had to make the MSRV of `bdk-ffi` also 1.57.0 and update the github actions. This PR also fixes a small `cargo fmt` warning.

  ### Notes to the reviewers

  ```shell
  % cargo +1.56.1 build
  error: package `os_str_bytes v6.2.0` cannot be built because it requires rustc 1.57.0 or newer, while the currently active rustc version is 1.56.1
  ```

  ```shell
  % cargo tree -i os_str_bytes
  os_str_bytes v6.2.0
  └── clap_lex v0.2.4
      └── clap v3.1.18
          └── uniffi_bindgen v0.19.3
              └── bdk-ffi-bindgen v0.2.0 (/Users/steve/git/notmandatory/bdk-ffi/bdk-ffi-bindgen)
  ```

  ### Checklists

  #### All Submissions:

  * [x] I've signed all my commits
  * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md)
  * [x] I ran `cargo fmt` and `cargo clippy` before committing

Top commit has no ACKs.

Tree-SHA512: 87ad4a02d91184b421871354b1f5c31d906bf7460fe992db15666da32a65f13d8fbaf4ae53928f410a7206b195c8dff76af07210c144a4d02854ceb6af265f7e
2022-07-29 09:43:16 -07:00
Steve Myers
6d53cbeb25 Fix fmt error 2022-07-26 12:36:07 -07:00
Steve Myers
1a12f37a2f Set MSRV to 1.57.0 2022-07-26 12:35:41 -07:00
Steve Myers
04022787c4 Merge bitcoindevkit/bdk-ffi#174: Add github workflows for audit, test and fmt
46850ed471 Add github issue templates and PR template (Steve Myers)
49da6fcbae Fix clippy warnings (Steve Myers)
075510d6e4 Add github workflows for audit, test and fmt (Steve Myers)

Pull request description:

  1. Copied and updated github workflows from BDK project.
  2. Fixed clippy warnings.
  3. Added github issue templates and PR templates from BDK project.

  Fixes #88

Top commit has no ACKs.

Tree-SHA512: 7aaca6be0f05d12f4561e165ebc45b52e40200b231b55326732756d45116f371f2da4c5d7cc590cd0af9450c344530f815a357fa979a34d0181efc2149ffccb3
2022-07-26 12:25:25 -07:00
Steve Myers
46850ed471 Add github issue templates and PR template 2022-07-25 13:17:52 -07:00
Steve Myers
49da6fcbae Fix clippy warnings 2022-07-25 13:17:52 -07:00
Steve Myers
075510d6e4 Add github workflows for audit, test and fmt 2022-07-25 13:17:38 -07:00
thunderbiscuit
fa5c67cd05 Bump uniffi-rs to 0.19.3 (#173) 2022-07-25 15:15:23 -04:00
thunderbiscuit
1a20d0a6c2 Merge pull request #164 from zoedberg/coin_selection
Add coin selection methods
2022-07-21 15:39:55 -04:00
Zoe Faltibà
a3ae4635eb Post review fixes 2022-07-21 21:04:57 +02:00
Zoe Faltibà
5f12900c6d Add TxBuilder coin selection methods 2022-07-21 16:59:16 +02:00
thunderbiscuit
1a5a628a5d Merge pull request #170 from thunderbiscuit/bump/bdk-0.20.0
Update BDK to version 0.20.0
2022-07-20 11:49:21 -04:00
thunderbiscuit
d8c8261e44 Update BDK to version 0.20.0 2022-07-15 12:03:19 -04:00
thunderbiscuit
692904df1e Merge pull request #158 from zoedberg/list_unspent
Add Wallet list_unspent method
2022-07-12 12:07:52 -04:00
thunderbiscuit
2ac42b3ae0 Add list_unspent and add_data methods to changelog 2022-07-12 12:05:31 -04:00
Pedro
6be22daf84 Add add_data method for txbuilder op_return (#163) 2022-06-23 07:20:02 -04:00
thunderbiscuit
fad5e3cefd Fix comment for NetworkLocalUtxo trait 2022-06-22 14:48:56 -03:00
Zoe Faltibà
28a9b302d5 Post review changes 2022-06-18 15:38:39 +02:00
Zoe Faltibà
8f5ca7f0fc Add Wallet list_unspent method 2022-06-18 15:37:40 +02:00
Steve Myers
80ed21e4c9 Bump version to 0.7.0 2022-06-14 10:46:44 -07:00
Steve Myers
f2efcb6196 Update CHANGELOG for v0.7.0 2022-06-14 10:45:35 -07:00
Steve Myers
cc8a17ef86 Merge bitcoindevkit/bdk-ffi#148: Add Wallet and TxBuilder tests using test funded wallet
e469dcd32c Add test with funded wallet (Steve Myers)

Pull request description:

  This PR adds an example test using the `bdk` test funded wallet.  The example test makes sure the `bdk-ffi` `TxBuilder` is able to drain a single wallet UTXO to a single address.  More tests can be added as we need them in future PRs.

  Required to complete #141

Top commit has no ACKs.

Tree-SHA512: 780e8cf5b3d3091f3322113f017c5b5524b30a3ac9e18910539c51042740d2809535a947b8d56012076ac5e9ad1abcf707ceaf17651457ea327a0b522fcc1002
2022-06-14 10:27:43 -07:00
Steve Myers
5ffe9ff331 Merge bitcoindevkit/bdk-ffi#161: Match bdk API and return a boolean when signing a PSBT
9a3d609826 Match bdk API and return a boolean when signing a PSBT (thunderbiscuit)

Pull request description:

  This is a fix for #160.

  I was looking at the `get_transactions()` method just below and I'm not sure which syntax is best (let me know if you have opinions on this) between

  What I have:
  ```rust
  fn sign(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<bool, Error> {
      let mut psbt = psbt.internal.lock().unwrap();
      self.get_wallet().sign(&mut psbt, SignOptions::default())
  }
  ```

  If I mirrored `get_transactions()`:
  ```rust
  fn sign(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<bool, Error> {
      let mut psbt = psbt.internal.lock().unwrap();
      let finalized = self.get_wallet().sign(&mut psbt, SignOptions::default())?;
      Ok(finalized)
  }
  ```

ACKs for top commit:
  notmandatory:
    reACK 9a3d609826

Tree-SHA512: c220929ea9bf7f670c850aebef1c2ebefcbf354f3887e692be36dced30e0e180816426bd58c5a58f61a9759e2f9f451b56e9448f42c23e26f96cf857fd6aa37c
2022-06-13 12:09:03 -07:00
thunderbiscuit
9a3d609826 Match bdk API and return a boolean when signing a PSBT 2022-06-13 15:58:44 -03:00
Steve Myers
bf8fef807d Merge bitcoindevkit/bdk-ffi#162: Update BDK to version 0.19.0
0a3347b85a Update BDK to version 0.19.0 (Steve Myers)

Pull request description:

  Update BDK to version 0.19.0
    - fixes #155, sqlite-db issue causing wrong balance
    - adds experimental taproot descriptor and PSBT support

ACKs for top commit:
  thunderbiscuit:
    Tested ACK [0a3347b](0a3347b85a).

Tree-SHA512: c94ee13f948996350f1079a5fb9be73225e40a930b982dcd7d6826bc19ae3d5ed76d4d3b78ebebf1ce78092570a26a53c93bcc6edf3eeca731142921cf297f83
2022-06-13 11:07:21 -07:00
Steve Myers
e469dcd32c Add test with funded wallet 2022-06-10 15:59:33 -07:00
Steve Myers
0a3347b85a Update BDK to version 0.19.0 2022-06-10 11:25:37 -07:00
Steve Myers
f40ab551b6 Merge bitcoindevkit/bdk-ffi#147: Clean up UDL file
efc475e33f Clean up UDL file (thunderbiscuit)

Pull request description:

  This PR groups related constructs together, fixes indentation inconsistencies (2 spaces is the standard), and adds space between methods in interfaces.

ACKs for top commit:
  notmandatory:
    ACK efc475e33f

Tree-SHA512: 8b37afd1d05f23cb51e04664459b88f3cf415f8616ee0a080294bc27c71c16ed8049ea605b4b41091e4c8276b107da21aff0c1712f2ebfb0dd059f68a4240745
2022-05-17 10:08:55 -07:00
thunderbiscuit
efc475e33f Clean up UDL file 2022-05-17 08:16:17 -04:00
thunderbiscuit
cdea6dc0bf Merge pull request #137 from thunderbiscuit/feat/address-index
Add address index to return type of get_address
2022-05-17 07:31:03 -04:00
thunderbiscuit
6beb98ca4c Incorporate PR review fixes 2022-05-16 15:06:01 -04:00
thunderbiscuit
04d538ad45 Add get_address method on the wallet 2022-05-16 14:50:12 -04:00
thunderbiscuit
c074a92e0c Add address index to return type of get_last_unused_address 2022-05-16 14:45:21 -04:00
Steve Myers
ff260edb3c Merge bitcoindevkit/bdk-ffi#153: Simplify using struct update syntax and clone
e5cd7cb3a2 Simplify using struct update syntax and clone (Sudarsan Balaji)

Pull request description:

ACKs for top commit:
  notmandatory:
    ACK e5cd7cb3a2

Tree-SHA512: 643013f6b0e131a56d9819598c99e7ac4ed008e121bd22d0e3d7dcff61bcb0826c8f1295bf2317580865e2b014f12b56bf4b538f0461a120ad3222b35b7cb933
2022-05-13 10:14:13 -07:00
Steve Myers
15a0795626 Merge bitcoindevkit/bdk-ffi#152: Release version 0.6.0
30e54ac067 Bump version to 0.6.0 (Steve Myers)

Pull request description:

ACKs for top commit:
  thunderbiscuit:
    ACK 30e54ac067.

Tree-SHA512: 235e1f894ba5bfac2fa60330d6e38c6179aaa27b4c6cb8cede17974207c9e04674d4cbbbaaa1705578e0552f09cc78db3ee1015e14cad91281a9d06764495cdd
2022-05-12 19:32:12 -07:00
Sudarsan Balaji
e5cd7cb3a2 Simplify using struct update syntax and clone 2022-05-12 22:50:03 +01:00
Steve Myers
30e54ac067 Bump version to 0.6.0 2022-05-10 22:41:48 -07:00
Steve Myers
71583eca7f Merge bitcoindevkit/bdk-ffi#150: Add new BumpFeeTxBuilder interface
0787d9c446 Fix order of BumpFeeTxBuilder parameters (Steve Myers)
390d12703e Change TxBuilder and BumpFeeTxBuilder build() to finish() (Steve Myers)
9f903932dc Add BumpFeeTxBuilder (Steve Myers)

Pull request description:

  Add BumpFeeTxBuilder to bump the fee on an unconfirmed tx created by the Wallet. The structure of the new interface is:

  ```udl
  interface BumpFeeTxBuilder {
    constructor(string txid, float new_fee_rate);
    BumpFeeTxBuilder allow_shrinking(string address);
    BumpFeeTxBuilder enable_rbf();
    BumpFeeTxBuilder enable_rbf_with_sequence(u32 nsequence);
    [Throws=BdkError]
    PartiallySignedBitcoinTransaction build([ByRef] Wallet wallet);
  };
  ```
  Fixes #150

Top commit has no ACKs.

Tree-SHA512: a24ed41f7b897a0e091ef55d4c1347b3973cbe628b7d1bd69c95e663bc6f049de8f7e3b019115d763cff3fa6d5cb4d31ed474022c3087e8404a2af37f380d56d
2022-05-10 22:35:32 -07:00
Steve Myers
0787d9c446 Fix order of BumpFeeTxBuilder parameters 2022-05-06 09:59:07 -07:00
Steve Myers
390d12703e Change TxBuilder and BumpFeeTxBuilder build() to finish() 2022-05-06 09:52:20 -07:00
Steve Myers
9f903932dc Add BumpFeeTxBuilder 2022-05-05 20:22:55 -07:00
Steve Myers
3b243efefd Merge bitcoindevkit/bdk-ffi#149: Update to bdk 0.18.0
f38f4c6197 Update CHANGELOG (Steve Myers)
11ba16ec1b Move txid getter to PartiallySignedBitcoinTransaction (Steve Myers)
4665c551dd Update EsploraConfig, Blockchain broadcast, Wallet sync (Steve Myers)
907540d214 Update bdk to 0.18.0 with sqlite-bundled feature (Steve Myers)

Pull request description:

  Changes that were needed to match updated bdk APIs:
  * new bdk-ffi `Blockchain`  interface with `constructo(BlockchainConfig)` and `broadcast()` functions
  * `Blockchain.broadcast()` function is now void (doesn't return txid)
  * added `PartiallySignedBitcoinTransaction.txid()` getter function
  * `Wallet.sync()` function now takes a `Blockchain` and optional `Progress` callback interface

  Tests performed:
  * from `bdk-kotlin/bdk-ffi` added local remote to this branch, ran build.sh script
  * fixed broken `bdk-kotlin` jvm and android tests and confirmed tests all pass

Top commit has no ACKs.

Tree-SHA512: d50633bbc8fd8a0d141597b30122c72957d2a0d64fc1537b649eeb8a5df1b1fb9a78ee1f03b9c606f47dee3952c9a91ae09eb47eb7a66d5f0fcb5545c86d906b
2022-05-05 09:29:37 -07:00
Steve Myers
f38f4c6197 Update CHANGELOG 2022-05-02 16:17:27 -07:00
Steve Myers
11ba16ec1b Move txid getter to PartiallySignedBitcoinTransaction 2022-04-25 21:31:40 -07:00
Steve Myers
4665c551dd Update EsploraConfig, Blockchain broadcast, Wallet sync 2022-04-25 21:31:38 -07:00
Steve Myers
907540d214 Update bdk to 0.18.0 with sqlite-bundled feature 2022-04-25 21:31:36 -07:00
Steve Myers
e6a6be5b60 Merge bitcoindevkit/bdk-ffi#145: Fix fee parameter typo in TransactionDetails
c722223b49 Fix fee parameter typo in TransactionDetails (dhruvbaliyan)

Pull request description:

  Solves issue #136
  Generated Kotlin file now have "fee" in TransactionDetails as parameter instead of "fees"
  ```
  data class TransactionDetails (
      var fee: ULong?,
      var received: ULong,
      var sent: ULong,
      var txid: String
  ) {
      // ...
  }
  ```

ACKs for top commit:
  thunderbiscuit:
    Tested ACK c722223. Works as expected in my apps. Thanks for the quick fix!

Tree-SHA512: c55a6e77ca5a0cd19758fc628fc48ed997b3c86247a1eadf5be77771818e3aa5f4db10025e7aa30d05be573e94d7439b15c7fc1f3d6dad752487f7f1ad455367
2022-04-25 21:30:17 -07:00
dhruvbaliyan
c722223b49 Fix fee parameter typo in TransactionDetails 2022-04-20 06:13:49 +05:30
Steve Myers
236360e8c4 Merge bitcoindevkit/bdk-ffi#140: Add RBF methods to TxBuilder
220835cffd Add RBF to TxBuilder (Sudarsan Balaji)
b3c93b0435 Expose functions (Sudarsan Balaji)

Pull request description:

  Fix #133

  We need to create another `RbfValue` enum because the actual type is only visible within the crate in `bdk`.

ACKs for top commit:
  notmandatory:
    tACK 220835cffd

Tree-SHA512: 648ea26a9742c8a395876f38c7299ff0dabb4ccad64e2f6a47d29ceecf44d9e54d845410fa68665e4d4d03d8eda1e51f680d0b89df307b003de49cf7b98e8701
2022-04-16 22:36:34 -07:00
Sudarsan Balaji
220835cffd Add RBF to TxBuilder 2022-04-15 21:04:21 +01:00
Sudarsan Balaji
b3c93b0435 Expose functions 2022-04-15 21:04:04 +01:00
Sudarsan Balaji
a12e5ed396 Use Path instead of PathBuf 2022-04-04 11:07:51 +01:00
Sudarsan Balaji
fc00d0d38c Use unwrap_or_else panic instead of expect 2022-04-04 11:01:50 +01:00
Sudarsan Balaji
7ea5e75bc4 Use write_all when not writing partially 2022-04-04 10:59:19 +01:00
Sudarsan Balaji
a5bd16db4d Enforce rust naming conventions 2022-04-04 10:58:01 +01:00
Steve Myers
d72905168b Merge bitcoindevkit/bdk-ffi#128: Release/0.5
8a556d0ba0 Bump bdk-ffi version to 0.5.0 (Steve Myers)
d7c5f24fe8 Bump bdk-ffi-bindgen version to 0.2.0 (Steve Myers)
f1431c3073 Update CHANGELOG.md (Steve Myers)

Pull request description:

Top commit has no ACKs.

Tree-SHA512: 2d612936740b93148c90acf512005c82e1fe38b4708710952abeac03d361e7dca6c6bdea4e82981a87dbba1cb6d37c0bd48b4ab252467798aa60aca463af5e5e
2022-04-01 19:24:48 -07:00
Steve Myers
8a556d0ba0 Bump bdk-ffi version to 0.5.0 2022-04-01 19:16:53 -07:00
Steve Myers
d7c5f24fe8 Bump bdk-ffi-bindgen version to 0.2.0 2022-04-01 19:16:29 -07:00
Steve Myers
f1431c3073 Update CHANGELOG.md 2022-04-01 19:15:58 -07:00
Steve Myers
e797efea57 Merge bitcoindevkit/bdk-ffi#126: Add workspace and bdk-ffi-bindgen binary cli tool
b207464fe6 Update README.md with bdk-ffi-bindgen info (Steve Myers)
fca5d1602b Add workspace and move bin to bdk-ffi-bindgen package (Steve Myers)
f4e097c4ac Only print python fix up lib path if used (Steve Myers)
c66dfdd52a Use structopt to capture generate options (Steve Myers)
ce848725b4 Add binary to remove the need for uniffi-bindgen cli tool (thunderbiscuit)

Pull request description:

  This PR is based on the mozilla/application-services [embedded-uniffi-bindgen](https://github.com/mozilla/application-services/tree/main/tools/embedded-uniffi-bindgen) tool. The purpose is to keep the bdk-ffi and bdk-ffi-bindgen tool in sync with the same version of uniffi-rs.

  Fixes #124, this PR replaces #122.

  The `bdkffi` library code remains unchanged but the `bin/generate` and `bin/generate-bindings` bins are combined and put in a new workspace binary package called `bdk-ffi-bindgen`.  The `bdk-ffi-bindgen` binary uses the following options, defaults, and environment variables:

  ```shell
  % cargo run -p bdk-ffi-bindgen -- --help

  bdk-ffi-bindgen 0.1.0
  A tool to generate bdk-ffi language bindings

  USAGE:
      bdk-ffi-bindgen [OPTIONS] --language <language> --out-dir <out-dir>

  FLAGS:
      -h, --help       Prints help information
      -V, --version    Prints version information

  OPTIONS:
      -l, --language <language>
              Language to generate bindings for [env: BDKFFI_BINDGEN_LANGUAGE=]  [possible values: kotlin, swift, python]

      -o, --out-dir <out-dir>
              Output directory to put generated language bindings [env: BDKFFI_BINDGEN_OUTPUT_DIR=]

      -p, --python-fixup-path <python-fixup-path>    Python fix up lib path [env: BDKFFI_BINDGEN_PYTHON_FIXUP_PATH=]
      -u, --udl-file <udl-file>                      UDL file [env: BDKFFI_BINDGEN_UDL=]  [default: src/bdk.udl]

  ```

Top commit has no ACKs.

Tree-SHA512: fa1a1c097fe5d0e704d76078c10f82c466dad5d045c8c93d579c2d13c448c52fb6a4f99dfd3dbc46be30471477ae2d1f9264201e14bae7948b408c8e0b3c9b81
2022-04-01 18:58:18 -07:00
Sudarsan Balaji
a41d628b14 Fix typo 2022-04-01 13:56:43 +05:30
Steve Myers
b207464fe6 Update README.md with bdk-ffi-bindgen info 2022-03-31 20:18:31 -07:00
Steve Myers
fca5d1602b Add workspace and move bin to bdk-ffi-bindgen package 2022-03-31 19:29:05 -07:00
Steve Myers
f4e097c4ac Only print python fix up lib path if used 2022-03-31 19:29:03 -07:00
Steve Myers
c66dfdd52a Use structopt to capture generate options 2022-03-31 19:29:01 -07:00
thunderbiscuit
ce848725b4 Add binary to remove the need for uniffi-bindgen cli tool 2022-03-31 19:28:59 -07:00
Sudarsan Balaji
5512b31969 Simplify 2022-03-31 17:26:32 +01:00
Sudarsan Balaji
a48f9b4387 Simplify 2022-03-31 17:22:52 +01:00
Sudarsan Balaji
87a0a15ea7 Fix formatting
cargo fmt
2022-03-31 17:18:03 +01:00
Sudarsan Balaji
ee91ad5b31 Add TxBuilder::drain_to 2022-03-31 17:17:43 +01:00
Sudarsan Balaji
ba68103be1 Add TxBuilder::drain_wallet 2022-03-31 17:17:24 +01:00
Steve Myers
bc43d2eb1a Map TxBuilder address error to BdkError::Generic 2022-03-28 17:30:40 -07:00
Sudarsan Balaji
adc3f68e31 Remove unnecessary map_err 2022-03-28 20:04:22 +01:00
Sudarsan Balaji
dd5622f724 Remove PSBT constructor 2022-03-25 17:39:25 +00:00
Sudarsan Balaji
e5aa51c3f8 Use TxBuilder in PSBT constructor 2022-03-25 17:37:25 +00:00
Sudarsan Balaji
a39fc787d5 Add initial version of TxBuilder 2022-03-25 17:24:21 +00:00
Sudarsan Balaji
51603e06d9 Remove to_owned() 2022-03-25 15:18:25 +00:00
Steve Myers
a1b89adf84 Remove unneeded WalletHolder and WalletOperations traits 2022-03-20 19:59:20 -05:00
Steve Myers
b1d483463f Merge bitcoindevkit/bdk-ffi#120: Fix Wallet.broadcast function, now returns a tx id as a hex string
851f61296a Fix Wallet.broadcast function, now returns a tx id as a hex string (Steve Myers)

Pull request description:

ACKs for top commit:
  thunderbiscuit:
    Tested ACK 851f612.

Tree-SHA512: 86e1d39029924e4fa3a0c21e9f45c1ba0694f4db9d1cfd8dee25a5675d5a8b7851a7c712ce57fb74382a7428fcecbe8ecee4b7b87b9245672bbd6ccea63dfc13
2022-03-16 16:53:20 -05:00
Steve Myers
851f61296a Fix Wallet.broadcast function, now returns a tx id as a hex string 2022-03-15 20:50:12 -05:00
14 changed files with 1155 additions and 308 deletions

26
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View File

@@ -0,0 +1,26 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: 'bug'
assignees: ''
---
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->
**To Reproduce**
<!-- Steps or code to reproduce the behavior. -->
**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->
**Build environment**
- BDK tag/commit: <!-- e.g. v0.13.0, 3a07614 -->
- OS+version: <!-- e.g. ubuntu 20.04.01, macOS 12.0.1, windows -->
- Rust/Cargo version: <!-- e.g. 1.56.0 -->
- Rust/Cargo target: <!-- e.g. x86_64-apple-darwin, x86_64-unknown-linux-gnu, etc. -->
**Additional context**
<!-- Add any other context about the problem here. -->

View File

@@ -0,0 +1,77 @@
---
name: Summer of Bitcoin Project
about: Template to suggest a new https://www.summerofbitcoin.org/ project.
title: ''
labels: 'summer-of-bitcoin'
assignees: ''
---
<!--
## Overview
Project ideas are scoped for a university-level student with a basic background in CS and bitcoin
fundamentals - achievable over 12-weeks. Below are just a few types of ideas:
- Low-hanging fruit: Relatively short projects with clear goals; requires basic technical knowledge
and minimal familiarity with the codebase.
- Core development: These projects derive from the ongoing work from the core of your development
team. The list of features and bugs is never-ending, and help is always welcome.
- Risky/Exploratory: These projects push the scope boundaries of your development effort. They
might require expertise in an area not covered by your current development team. They might take
advantage of a new technology. There is a reasonable chance that the project might be less
successful, but the potential rewards make it worth the attempt.
- Infrastructure/Automation: These projects are the code that your organization uses to get its
development work done; for example, projects that improve the automation of releases, regression
tests and automated builds. This is a category where a Summer of Bitcoin student can be really
helpful, doing work that the development team has been putting off while they focus on core
development.
- Quality Assurance/Testing: Projects that work on and test your project's software development
process. Additionally, projects that involve a thorough test and review of individual PRs.
- Fun/Peripheral: These projects might not be related to the current core development focus, but
create new innovations and new perspectives for your project.
-->
**Description**
<!-- Description: 3-7 sentences describing the project background and tasks to be done. -->
**Expected Outcomes**
<!-- Short bullet list describing what is to be accomplished -->
**Resources**
<!-- 2-3 reading materials for candidate to learn about the repo, project, scope etc -->
<!-- Recommended reading such as a developer/contributor guide -->
<!-- [Another example a paper citation](https://arxiv.org/pdf/1802.08091.pdf) -->
<!-- [Another example an existing issue](https://github.com/opencv/opencv/issues/11013) -->
<!-- [An existing related module](https://github.com/opencv/opencv_contrib/tree/master/modules/optflow) -->
**Skills Required**
<!-- 3-4 technical skills that the candidate should know -->
<!-- hands on experience with git -->
<!-- mastery plus experience coding in C++ -->
<!-- basic knowledge in matrix and tensor computations, college course work in cryptography -->
<!-- strong mathematical background -->
<!-- Bonus - has experience with React Native. Best if you have also worked with OSSFuzz -->
**Mentor(s)**
<!-- names of mentor(s) for this project go here -->
**Difficulty**
<!-- Easy, Medium, Hard -->
**Competency Test (optional)**
<!-- 2-3 technical tasks related to the project idea or repository youd like a candidate to
perform in order to demonstrate competency, good first bugs, warm-up exercises -->
<!-- ex. Read the instructions here to get Bitcoin core running on your machine -->
<!-- ex. pick an issue labeled as “newcomer” in the repository, and send a merge request to the
repository. You can also suggest some other improvement that we did not think of yet, or
something that you find interesting or useful -->
<!-- ex. fixes for coding style are usually easy to do, and are good issues for first time
contributions for those learning how to interact with the project. After you are done with the
coding style issue, try making a different contribution. -->
<!-- ex. setup a full Debian packaging development environment and learn the basics of Debian
packaging. Then identify and package the missing dependencies to package Specter Desktop -->
<!-- ex. write a pull parser for CSV files. You'll be judged by the decisions to store the parser
state and how flexible it is to wrap this parser in other scenarios. -->
<!-- ex. Stretch Goal: Implement some basic metaprogram/app to prove you're very familiar with BDK.
Be prepared to make adjustments as we judge your solution. -->

30
.github/pull_request_template.md vendored Normal file
View File

@@ -0,0 +1,30 @@
<!-- You can erase any parts of this template not applicable to your Pull Request. -->
### Description
<!-- Describe the purpose of this PR, what's being adding and/or fixed -->
### Notes to the reviewers
<!-- In this section you can include notes directed to the reviewers, like explaining why some parts
of the PR were done in a specific way -->
### Checklists
#### All Submissions:
* [ ] I've signed all my commits
* [ ] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md)
* [ ] 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
* [ ] I've updated `CHANGELOG.md`
#### Bugfixes:
* [ ] This pull request breaks the existing API
* [ ] I've added tests to reproduce the issue which are now passing
* [ ] I'm linking the issue being fixed by this PR

19
.github/workflows/audit.yml vendored Normal file
View File

@@ -0,0 +1,19 @@
name: Audit
on:
push:
paths:
- '**/Cargo.toml'
- '**/Cargo.lock'
schedule:
- cron: '0 0 * * 0' # Once per week
jobs:
security_audit:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions-rs/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}

61
.github/workflows/cont_integration.yml vendored Normal file
View File

@@ -0,0 +1,61 @@
on: [push, pull_request]
name: CI
jobs:
build-test:
name: Build and test
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- version: 1.60.0 # STABLE
clippy: true
- version: 1.57.0 # MSRV
steps:
- name: checkout
uses: actions/checkout@v2
- name: Generate cache key
run: echo "${{ matrix.rust.version }} ${{ matrix.features }}" | tee .cache_key
- name: cache
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('.cache_key') }}-${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}
- name: Set default toolchain
run: rustup default ${{ matrix.rust.version }}
- name: Set profile
run: rustup set profile minimal
- name: Add clippy
if: ${{ matrix.rust.clippy }}
run: rustup component add clippy
- name: Update toolchain
run: rustup update
- name: Build
run: cargo build
- name: Clippy
if: ${{ matrix.rust.clippy }}
run: cargo clippy --all-targets -- -D warnings
- name: Test
run: cargo test
fmt:
name: Rust fmt
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set default toolchain
run: rustup default nightly
- name: Set profile
run: rustup set profile minimal
- name: Add rustfmt
run: rustup component add rustfmt
- name: Update toolchain
run: rustup update
- name: Check fmt
run: cargo fmt --all -- --config format_code_in_doc_comments=true --check

1
.gitignore vendored
View File

@@ -14,3 +14,4 @@ testdb
xcuserdata
.lsp
.clj-kondo
.idea/

View File

@@ -6,18 +6,55 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [v0.4.0]
## [v0.8.0]
- Update BDK to version 0.20.0 [#169]
- APIs Added
- `TxBuilder.add_data(data: Vec<u8>)` [#163]
- `Wallet.list_unspent()` returns `Vec<LocalUtxo>` [#158]
- Add coin control methods on TxBuilder [#164]
[#163]: https://github.com/bitcoindevkit/bdk-ffi/pull/163
[#158]: https://github.com/bitcoindevkit/bdk-ffi/pull/158
[#164]: https://github.com/bitcoindevkit/bdk-ffi/pull/164
[#169]: https://github.com/bitcoindevkit/bdk-ffi/pull/169
## [v0.7.0]
- Update BDK to version 0.19.0
- fixes sqlite-db issue causing wrong balance
- adds experimental taproot descriptor and PSBT support
- APIs Removed
- `Wallet.get_new_address()`, returned String, [#137]
- `Wallet.get_last_unused_address()`, returned String [#137]
- APIs Added
- `Wallet.get_address(AddressIndex)`, returns `AddressInfo` [#137]
- APIs Changed
- `Wallet.sign(PartiallySignedBitcoinTransaction)` now returns a bool, true if finalized [#161]
[#137]: https://github.com/bitcoindevkit/bdk-ffi/pull/137
[#161]: https://github.com/bitcoindevkit/bdk-ffi/pull/161
## [v0.6.0]
- Update BDK to version 0.18.0
- Add BumpFeeTxBuilder to bump the fee on an unconfirmed tx created by the Wallet
- Change TxBuilder.build() to TxBuilder.finish() to align with bdk function name
## [v0.5.0]
- Fix Wallet.broadcast function, now returns a tx id as a hex string
- Remove creating a new spending Transaction via the PartiallySignedBitcoinTransaction constructor
- Add TxBuilder for creating new spending PartiallySignedBitcoinTransaction
- Add TxBuilder .add_recipient, .fee_rate, and .build functions
- Add TxBuilder .drain_wallet and .drain_to functions
- Update generate cli tool to generate all binding languages and rename to bdk-ffi-bindgen
## [v0.4.0]
- Add dual license MIT and Apache 2.0
- Add sqlite database support
- Fix memory database configuration enum, remove junk field
## [v0.3.1]
- Remove hard coded sync progress value (was always returning 21.0)
## [v0.3.0]
- Move bdk-kotlin bindings and ios example to separate repos
- Add bin to generate Python bindings
- Add `PartiallySignedBitcoinTransaction::deserialize` function as named constructor to decode from a string per [BIP 0174]
@@ -28,7 +65,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [v0.2.0]
[unreleased]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.4.0...HEAD
[unreleased]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.8.0...HEAD
[v0.8.0]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.7.0...v0.8.0
[v0.7.0]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.6.0...v0.7.0
[v0.6.0]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.5.0...v0.6.0
[v0.5.0]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.4.0...v0.5.0
[v0.4.0]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.3.1...v0.4.0
[v0.3.1]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.3.0...v0.3.1
[v0.3.0]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.2.0...v0.3.0

View File

@@ -1,32 +1,23 @@
[package]
name = "bdk-ffi"
version = "0.4.1"
version = "0.8.0"
authors = ["Steve Myers <steve@notmandatory.org>", "Sudarsan Balaji <sudarsan.balaji@artfuldev.com>"]
edition = "2018"
[workspace]
members = [".","bdk-ffi-bindgen"]
default-members = [".", "bdk-ffi-bindgen"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["staticlib", "cdylib"]
name = "bdkffi"
[dependencies]
bdk = { version = "0.14", features = ["all-keys", "use-esplora-ureq", "sqlite"] }
bdk = { version = "0.20", features = ["all-keys", "use-esplora-ureq", "sqlite-bundled"] }
# TODO remove when bdk "sqlite-bundled" feature added
rusqlite = { version = "0.25.3", features = ["bundled"] }
uniffi_macros = { version = "0.16.0", features = ["builtin-bindgen"] }
uniffi = { version = "0.16.0", features = ["builtin-bindgen"] }
thiserror = "1.0"
anyhow = "=1.0.45" # remove after upgrading to next version of uniffi
uniffi_bindgen = { version = "0.16.0", optional = true }
uniffi_macros = { version = "0.19.3", features = ["builtin-bindgen"] }
uniffi = { version = "0.19.3", features = ["builtin-bindgen"] }
[build-dependencies]
uniffi_build = { version = "0.16.0", features = ["builtin-bindgen"] }
[features]
generate-python = ["uniffi_bindgen"]
[[bin]]
name = "generate"
uniffi_build = { version = "0.19.3", features = ["builtin-bindgen"] }

View File

@@ -1,9 +1,10 @@
# Native language bindings for BDK
This repository contains source code for generating native language bindings for the rust based
[bdk] library which is the central artifact of the [Bitcoin Dev Kit] project.
The workspace in this repository creates the `libbdkffi` multi-language library for the rust based
[bdk] library from the [Bitcoin Dev Kit] project. The `bdk-ffi-bindgen` package builds a tool for
generating the actual language binding code used to access the `libbdkffi` library.
Each supported language has it's own repository that includes this project as a [git submodule].
Each supported language has its own repository that includes this project as a [git submodule].
The rust code in this project is a wrapper around the [bdk] library to expose it's APIs in a
uniform way using the [mozilla/uniffi-rs] bindings generator for each supported target language.
@@ -19,6 +20,14 @@ language binding for [bdk] supported by this project.
| Swift | iOS, macOS | [bdk-swift] |
| Python | linux, macOS | [bdk-python] |
## Language bindings generator tool
Use the `bdk-ffi-bindgen` tool to generate language binding code for the above supported languages.
To run `bdk-ffi-bindgen` and see the available options use the command:
```shell
cargo run -p bdk-ffi-bindgen -- --help
```
[bdk]: https://github.com/bitcoindevkit/bdk
[Bitcoin Dev Kit]: https://github.com/bitcoindevkit
[git submodule]: https://git-scm.com/book/en/v2/Git-Tools-Submodules
@@ -30,14 +39,6 @@ language binding for [bdk] supported by this project.
## Contributing
### Install uniffi-bindgen cli tool
Install the uniffi-bindgen binary on your system using:
`cargo install uniffi_bindgen`
The version must be the same as the `uniffi` dependency in `Cargo.toml`.
### Adding new structs and functions
See the [UniFFI User Guide](https://mozilla.github.io/uniffi-rs/)

View File

@@ -0,0 +1,10 @@
[package]
name = "bdk-ffi-bindgen"
version = "0.2.0"
edition = "2021"
[dependencies]
anyhow = "=1.0.45" # remove after upgrading to next version of uniffi
structopt = "0.3"
uniffi_bindgen = "0.19.3"
camino = "1.0.9"

137
bdk-ffi-bindgen/src/main.rs Normal file
View File

@@ -0,0 +1,137 @@
use camino::Utf8Path;
use std::fmt;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use structopt::StructOpt;
#[derive(Debug, PartialEq)]
pub enum Language {
Kotlin,
Python,
Swift,
}
impl fmt::Display for Language {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Language::Kotlin => write!(f, "kotlin"),
Language::Swift => write!(f, "swift"),
Language::Python => write!(f, "python"),
}
}
}
#[derive(Debug)]
pub enum Error {
UnsupportedLanguage,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl FromStr for Language {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"kotlin" => Ok(Language::Kotlin),
"python" => Ok(Language::Python),
"swift" => Ok(Language::Swift),
_ => Err(Error::UnsupportedLanguage),
}
}
}
fn generate_bindings(opt: &Opt) -> anyhow::Result<(), anyhow::Error> {
let path: &Utf8Path = Utf8Path::from_path(&opt.udl_file).unwrap();
let out_dir: &Utf8Path = Utf8Path::from_path(&opt.out_dir).unwrap();
uniffi_bindgen::generate_bindings(
path,
None,
vec![opt.language.to_string().as_str()],
Some(out_dir),
false,
)?;
Ok(())
}
fn fixup_python_lib_path(
out_dir: &Path,
lib_name: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
use std::fs;
use std::io::Write;
const LOAD_INDIRECT_DEF: &str = "def loadIndirect():";
let bindings_file = out_dir.join("bdk.py");
let mut data = fs::read_to_string(&bindings_file)?;
let pos = data
.find(LOAD_INDIRECT_DEF)
.unwrap_or_else(|| panic!("loadIndirect not found in `{}`", bindings_file.display()));
let range = pos..pos + LOAD_INDIRECT_DEF.len();
let replacement = format!(
r#"
def loadIndirect():
import glob
return getattr(ctypes.cdll, glob.glob(os.path.join(os.path.dirname(os.path.abspath(__file__)), '{}.*'))[0])
def _loadIndirectOld():"#,
&lib_name.to_str().expect("lib name")
);
data.replace_range(range, &replacement);
let mut file = fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(&bindings_file)?;
file.write_all(data.as_bytes())?;
Ok(())
}
#[derive(Debug, StructOpt)]
#[structopt(
name = "bdk-ffi-bindgen",
about = "A tool to generate bdk-ffi language bindings"
)]
struct Opt {
/// UDL file
#[structopt(env = "BDKFFI_BINDGEN_UDL", short, long, default_value("src/bdk.udl"), parse(try_from_str = PathBuf::from_str))]
udl_file: PathBuf,
/// Language to generate bindings for
#[structopt(env = "BDKFFI_BINDGEN_LANGUAGE", short, long, possible_values(&["kotlin","swift","python"]), parse(try_from_str = Language::from_str))]
language: Language,
/// Output directory to put generated language bindings
#[structopt(env = "BDKFFI_BINDGEN_OUTPUT_DIR", short, long, parse(try_from_str = PathBuf::from_str))]
out_dir: PathBuf,
/// Python fix up lib path
#[structopt(env = "BDKFFI_BINDGEN_PYTHON_FIXUP_PATH", short, long, parse(try_from_str = PathBuf::from_str))]
python_fixup_path: Option<PathBuf>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let opt = Opt::from_args();
println!("Input UDL file is {:?}", opt.udl_file);
println!("Chosen language is {}", opt.language);
println!("Output directory is {:?}", opt.out_dir);
generate_bindings(&opt)?;
if opt.language == Language::Python {
if let Some(path) = opt.python_fixup_path {
println!("Fixing up python lib path, {:?}", &path);
fixup_python_lib_path(&opt.out_dir, &path)?;
}
}
Ok(())
}

View File

@@ -1,6 +1,7 @@
namespace bdk {
[Throws=BdkError]
ExtendedKeyInfo generate_extended_key(Network network, WordCount word_count, string? password);
[Throws=BdkError]
ExtendedKeyInfo restore_extended_key(Network network, string mnemonic, string? password);
};
@@ -49,6 +50,16 @@ enum BdkError {
"Rusqlite",
};
dictionary AddressInfo {
u32 index;
string address;
};
enum AddressIndex {
"New",
"LastUnused",
};
enum Network {
"Bitcoin",
"Testnet",
@@ -73,15 +84,15 @@ interface DatabaseConfig {
};
dictionary TransactionDetails {
u64? fees;
u64 received;
u64 sent;
string txid;
u64? fee;
u64 received;
u64 sent;
string txid;
};
dictionary BlockTime {
u32 height;
u64 timestamp;
u32 height;
u64 timestamp;
};
[Enum]
@@ -90,58 +101,6 @@ interface Transaction {
Confirmed(TransactionDetails details, BlockTime confirmation);
};
dictionary ElectrumConfig {
string url;
string? socks5;
u8 retry;
u8? timeout;
u64 stop_gap;
};
dictionary EsploraConfig {
string base_url;
string? proxy;
u64 timeout_read;
u64 timeout_write;
u64 stop_gap;
};
[Enum]
interface BlockchainConfig {
Electrum(ElectrumConfig config);
Esplora(EsploraConfig config);
};
callback interface BdkProgress {
void update(f32 progress, string? message);
};
interface Wallet {
[Throws=BdkError]
constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config, BlockchainConfig blockchain_config);
string get_new_address();
string get_last_unused_address();
[Throws=BdkError]
u64 get_balance();
[Throws=BdkError]
void sign([ByRef] PartiallySignedBitcoinTransaction psbt);
[Throws=BdkError]
sequence<Transaction> get_transactions();
Network get_network();
[Throws=BdkError]
void sync(BdkProgress progress_update, u32? max_address_param);
[Throws=BdkError]
Transaction broadcast([ByRef] PartiallySignedBitcoinTransaction psbt);
};
interface PartiallySignedBitcoinTransaction {
[Throws=BdkError]
constructor([ByRef] Wallet wallet, string recipient, u64 amount, float? fee_rate);
[Name=deserialize,Throws=BdkError]
constructor(string psbt_base64);
string serialize();
};
dictionary ExtendedKeyInfo {
string mnemonic;
string xprv;
@@ -155,3 +114,143 @@ enum WordCount {
"Words21",
"Words24",
};
dictionary ElectrumConfig {
string url;
string? socks5;
u8 retry;
u8? timeout;
u64 stop_gap;
};
dictionary EsploraConfig {
string base_url;
string? proxy;
u8? concurrency;
u64 stop_gap;
u64? timeout;
};
[Enum]
interface BlockchainConfig {
Electrum(ElectrumConfig config);
Esplora(EsploraConfig config);
};
interface Blockchain {
[Throws=BdkError]
constructor(BlockchainConfig config);
[Throws=BdkError]
void broadcast([ByRef] PartiallySignedBitcoinTransaction psbt);
};
callback interface Progress {
void update(f32 progress, string? message);
};
dictionary OutPoint {
string txid;
u32 vout;
};
dictionary TxOut {
u64 value;
string address;
};
enum KeychainKind {
"External",
"Internal",
};
dictionary LocalUtxo {
OutPoint outpoint;
TxOut txout;
KeychainKind keychain;
boolean is_spent;
};
interface Wallet {
[Throws=BdkError]
constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config);
[Throws=BdkError]
AddressInfo get_address(AddressIndex address_index);
[Throws=BdkError]
u64 get_balance();
[Throws=BdkError]
boolean sign([ByRef] PartiallySignedBitcoinTransaction psbt);
[Throws=BdkError]
sequence<Transaction> get_transactions();
Network get_network();
[Throws=BdkError]
void sync([ByRef] Blockchain blockchain, Progress? progress);
[Throws=BdkError]
sequence<LocalUtxo> list_unspent();
};
interface PartiallySignedBitcoinTransaction {
[Throws=BdkError]
constructor(string psbt_base64);
string serialize();
string txid();
};
interface TxBuilder {
constructor();
TxBuilder add_recipient(string address, u64 amount);
TxBuilder add_unspendable(OutPoint unspendable);
TxBuilder add_utxo(OutPoint outpoint);
TxBuilder add_utxos(sequence<OutPoint> outpoints);
TxBuilder do_not_spend_change();
TxBuilder manually_selected_only();
TxBuilder only_spend_change();
TxBuilder unspendable(sequence<OutPoint> unspendable);
TxBuilder fee_rate(float sat_per_vbyte);
TxBuilder fee_absolute(u64 fee_amount);
TxBuilder drain_wallet();
TxBuilder drain_to(string address);
TxBuilder enable_rbf();
TxBuilder enable_rbf_with_sequence(u32 nsequence);
TxBuilder add_data(sequence<u8> data);
[Throws=BdkError]
PartiallySignedBitcoinTransaction finish([ByRef] Wallet wallet);
};
interface BumpFeeTxBuilder {
constructor(string txid, float new_fee_rate);
BumpFeeTxBuilder allow_shrinking(string address);
BumpFeeTxBuilder enable_rbf();
BumpFeeTxBuilder enable_rbf_with_sequence(u32 nsequence);
[Throws=BdkError]
PartiallySignedBitcoinTransaction finish([ByRef] Wallet wallet);
};

View File

@@ -1,67 +0,0 @@
pub const BDK_UDL: &str = "src/bdk.udl";
#[cfg(feature = "generate-python")]
fn fixup_python_lib_path<O: AsRef<std::path::Path>>(
out_dir: O,
lib_name: &str,
) -> Result<(), Box<dyn std::error::Error>> {
use std::fs;
use std::io::Write;
const LOAD_INDIRECT_DEF: &str = "def loadIndirect():";
let bindings_file = out_dir.as_ref().join("bdk.py");
let mut data = fs::read_to_string(&bindings_file)?;
let pos = data.find(LOAD_INDIRECT_DEF).expect(&format!(
"loadIndirect not found in `{}`",
bindings_file.display()
));
let range = pos..pos + LOAD_INDIRECT_DEF.len();
let replacement = format!(
r#"
def loadIndirect():
import glob
return getattr(ctypes.cdll, glob.glob(os.path.join(os.path.dirname(os.path.abspath(__file__)), '{}.*'))[0])
def _loadIndirectOld():"#,
lib_name
);
data.replace_range(range, &replacement);
let mut file = fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(&bindings_file)?;
file.write(data.as_bytes())?;
Ok(())
}
#[cfg(feature = "generate-python")]
fn generate_python() -> Result<(), Box<dyn std::error::Error>> {
use std::env;
let out_path = env::var("GENERATE_PYTHON_BINDINGS_OUT")
.map_err(|_| String::from("`GENERATE_PYTHON_BINDINGS_OUT` env variable missing"))?;
uniffi_bindgen::generate_bindings(
&format!("{}/{}", env!("CARGO_MANIFEST_DIR"), BDK_UDL),
None,
vec!["python"],
Some(&out_path),
false,
)?;
if let Some(name) = env::var("GENERATE_PYTHON_BINDINGS_FIXUP_LIB_PATH").ok() {
fixup_python_lib_path(&out_path, &name)?;
}
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "generate-python")]
generate_python()?;
Ok(())
}

View File

@@ -1,26 +1,63 @@
use bdk::bitcoin::hashes::hex::ToHex;
use bdk::bitcoin::secp256k1::Secp256k1;
use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
use bdk::bitcoin::{Address, Network};
use bdk::bitcoin::{Address, Network, OutPoint as BdkOutPoint, Script, Txid};
use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig};
use bdk::blockchain::Progress;
use bdk::blockchain::{
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
};
use bdk::blockchain::{Blockchain as BdkBlockchain, Progress as BdkProgress};
use bdk::database::any::{AnyDatabase, SledDbConfiguration, SqliteDbConfiguration};
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
use bdk::keys::bip39::{Language, Mnemonic, WordCount};
use bdk::keys::{DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey};
use bdk::miniscript::BareCtx;
use bdk::wallet::AddressIndex;
use bdk::{BlockTime, Error, FeeRate, SignOptions, Wallet as BdkWallet};
use std::convert::TryFrom;
use bdk::wallet::tx_builder::ChangeSpendPolicy;
use bdk::wallet::AddressIndex as BdkAddressIndex;
use bdk::wallet::AddressInfo as BdkAddressInfo;
use bdk::{
BlockTime, Error, FeeRate, KeychainKind, SignOptions, SyncOptions as BdkSyncOptions,
Wallet as BdkWallet,
};
use std::collections::HashSet;
use std::convert::{From, TryFrom};
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;
use std::sync::{Mutex, MutexGuard};
use std::sync::{Arc, Mutex, MutexGuard};
uniffi_macros::include_scaffolding!("bdk");
type BdkError = Error;
pub struct AddressInfo {
pub index: u32,
pub address: String,
}
impl From<BdkAddressInfo> for AddressInfo {
fn from(x: bdk::wallet::AddressInfo) -> AddressInfo {
AddressInfo {
index: x.index,
address: x.address.to_string(),
}
}
}
pub enum AddressIndex {
New,
LastUnused,
}
impl From<AddressIndex> for BdkAddressIndex {
fn from(x: AddressIndex) -> BdkAddressIndex {
match x {
AddressIndex::New => BdkAddressIndex::New,
AddressIndex::LastUnused => BdkAddressIndex::LastUnused,
}
}
}
pub enum DatabaseConfig {
Memory,
Sled { config: SledDbConfiguration },
@@ -38,9 +75,9 @@ pub struct ElectrumConfig {
pub struct EsploraConfig {
pub base_url: String,
pub proxy: Option<String>,
pub timeout_read: u64,
pub timeout_write: u64,
pub concurrency: Option<u8>,
pub stop_gap: u64,
pub timeout: Option<u64>,
}
pub enum BlockchainConfig {
@@ -48,13 +85,9 @@ pub enum BlockchainConfig {
Esplora { config: EsploraConfig },
}
trait WalletHolder<B> {
fn get_wallet(&self) -> MutexGuard<BdkWallet<B, AnyDatabase>>;
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TransactionDetails {
pub fees: Option<u64>,
pub fee: Option<u64>,
pub received: u64,
pub sent: u64,
pub txid: String,
@@ -74,7 +107,7 @@ pub enum Transaction {
impl From<&bdk::TransactionDetails> for TransactionDetails {
fn from(x: &bdk::TransactionDetails) -> TransactionDetails {
TransactionDetails {
fees: x.fee,
fee: x.fee,
txid: x.txid.to_string(),
received: x.received,
sent: x.sent,
@@ -96,130 +129,12 @@ impl From<&bdk::TransactionDetails> for Transaction {
}
}
trait WalletOperations<B>: WalletHolder<B> {
fn get_new_address(&self) -> String {
self.get_wallet()
.get_address(AddressIndex::New)
.unwrap()
.address
.to_string()
}
fn get_last_unused_address(&self) -> String {
self.get_wallet()
.get_address(AddressIndex::LastUnused)
.unwrap()
.address
.to_string()
}
fn get_balance(&self) -> Result<u64, Error> {
self.get_wallet().get_balance()
}
fn sign(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<(), Error> {
let mut psbt = psbt.internal.lock().unwrap();
let finalized = self.get_wallet().sign(&mut psbt, SignOptions::default())?;
match finalized {
true => Ok(()),
false => Err(BdkError::Generic(format!(
"transaction signing not finalized {:?}",
psbt
))),
}
}
fn get_transactions(&self) -> Result<Vec<Transaction>, Error> {
let transactions = self.get_wallet().list_transactions(true)?;
Ok(transactions.iter().map(Transaction::from).collect())
}
struct Blockchain {
blockchain_mutex: Mutex<AnyBlockchain>,
}
struct Wallet {
wallet_mutex: Mutex<BdkWallet<AnyBlockchain, AnyDatabase>>,
}
pub trait BdkProgress: Send + Sync {
fn update(&self, progress: f32, message: Option<String>);
}
struct BdkProgressHolder {
progress_update: Box<dyn BdkProgress>,
}
impl Progress for BdkProgressHolder {
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error> {
self.progress_update.update(progress, message);
Ok(())
}
}
struct PartiallySignedBitcoinTransaction {
internal: Mutex<PartiallySignedTransaction>,
}
impl PartiallySignedBitcoinTransaction {
fn new(
wallet: &Wallet,
recipient: String,
amount: u64,
fee_rate: Option<f32>, // satoshis per vbyte
) -> Result<Self, Error> {
let wallet = wallet.get_wallet();
match Address::from_str(&recipient) {
Ok(address) => {
let (psbt, _details) = {
let mut builder = wallet.build_tx();
builder.add_recipient(address.script_pubkey(), amount);
if let Some(sat_per_vb) = fee_rate {
builder.fee_rate(FeeRate::from_sat_per_vb(sat_per_vb));
}
builder.finish()?
};
Ok(PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt),
})
}
Err(..) => Err(BdkError::Generic(
"failed to read wallet address".to_string(),
)),
}
}
pub fn deserialize(psbt_base64: String) -> Result<Self, Error> {
let psbt: PartiallySignedTransaction = PartiallySignedTransaction::from_str(&psbt_base64)?;
Ok(PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt),
})
}
pub fn serialize(&self) -> String {
let psbt = self.internal.lock().unwrap().clone();
psbt.to_string()
}
}
impl WalletHolder<AnyBlockchain> for Wallet {
fn get_wallet(&self) -> MutexGuard<BdkWallet<AnyBlockchain, AnyDatabase>> {
self.wallet_mutex.lock().unwrap()
}
}
impl WalletOperations<AnyBlockchain> for Wallet {}
impl Wallet {
fn new(
descriptor: String,
change_descriptor: Option<String>,
network: Network,
database_config: DatabaseConfig,
blockchain_config: BlockchainConfig,
) -> Result<Self, BdkError> {
let any_database_config = match database_config {
DatabaseConfig::Memory => AnyDatabaseConfig::Memory(()),
DatabaseConfig::Sled { config } => AnyDatabaseConfig::Sled(config),
DatabaseConfig::Sqlite { config } => AnyDatabaseConfig::Sqlite(config),
};
impl Blockchain {
fn new(blockchain_config: BlockchainConfig) -> Result<Self, BdkError> {
let any_blockchain_config = match blockchain_config {
BlockchainConfig::Electrum { config } => {
AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
@@ -234,42 +149,205 @@ impl Wallet {
AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
base_url: config.base_url,
proxy: config.proxy,
timeout_read: config.timeout_read,
timeout_write: config.timeout_write,
concurrency: config.concurrency,
stop_gap: usize::try_from(config.stop_gap).unwrap(),
timeout: config.timeout,
})
}
};
let database = AnyDatabase::from_config(&any_database_config)?;
let blockchain = AnyBlockchain::from_config(&any_blockchain_config)?;
Ok(Self {
blockchain_mutex: Mutex::new(blockchain),
})
}
fn get_blockchain(&self) -> MutexGuard<AnyBlockchain> {
self.blockchain_mutex.lock().expect("blockchain")
}
fn broadcast(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<(), Error> {
let tx = psbt.internal.lock().unwrap().clone().extract_tx();
self.get_blockchain().broadcast(&tx)
}
}
struct Wallet {
wallet_mutex: Mutex<BdkWallet<AnyDatabase>>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct OutPoint {
txid: String,
vout: u32,
}
impl From<&OutPoint> for BdkOutPoint {
fn from(x: &OutPoint) -> BdkOutPoint {
BdkOutPoint {
txid: Txid::from_str(&x.txid).unwrap(),
vout: x.vout,
}
}
}
pub struct TxOut {
value: u64,
address: String,
}
pub struct LocalUtxo {
outpoint: OutPoint,
txout: TxOut,
keychain: KeychainKind,
is_spent: bool,
}
// This trait is used to convert the bdk TxOut type with field `script_pubkey: Script`
// into the bdk-ffi TxOut type which has a field `address: String` instead
trait NetworkLocalUtxo {
fn from_utxo(x: &bdk::LocalUtxo, network: Network) -> LocalUtxo;
}
impl NetworkLocalUtxo for LocalUtxo {
fn from_utxo(x: &bdk::LocalUtxo, network: Network) -> LocalUtxo {
LocalUtxo {
outpoint: OutPoint {
txid: x.outpoint.txid.to_string(),
vout: x.outpoint.vout,
},
txout: TxOut {
value: x.txout.value,
address: bdk::bitcoin::util::address::Address::from_script(
&x.txout.script_pubkey,
network,
)
.unwrap()
.to_string(),
},
keychain: x.keychain,
is_spent: x.is_spent,
}
}
}
pub trait Progress: Send + Sync + 'static {
fn update(&self, progress: f32, message: Option<String>);
}
struct ProgressHolder {
progress: Box<dyn Progress>,
}
impl BdkProgress for ProgressHolder {
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error> {
self.progress.update(progress, message);
Ok(())
}
}
impl fmt::Debug for ProgressHolder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ProgressHolder").finish_non_exhaustive()
}
}
#[derive(Debug)]
struct PartiallySignedBitcoinTransaction {
internal: Mutex<PartiallySignedTransaction>,
}
impl PartiallySignedBitcoinTransaction {
fn new(psbt_base64: String) -> Result<Self, Error> {
let psbt: PartiallySignedTransaction = PartiallySignedTransaction::from_str(&psbt_base64)?;
Ok(PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt),
})
}
fn serialize(&self) -> String {
let psbt = self.internal.lock().unwrap().clone();
psbt.to_string()
}
fn txid(&self) -> String {
let tx = self.internal.lock().unwrap().clone().extract_tx();
let txid = tx.txid();
txid.to_hex()
}
}
impl Wallet {
fn new(
descriptor: String,
change_descriptor: Option<String>,
network: Network,
database_config: DatabaseConfig,
) -> Result<Self, BdkError> {
let any_database_config = match database_config {
DatabaseConfig::Memory => AnyDatabaseConfig::Memory(()),
DatabaseConfig::Sled { config } => AnyDatabaseConfig::Sled(config),
DatabaseConfig::Sqlite { config } => AnyDatabaseConfig::Sqlite(config),
};
let database = AnyDatabase::from_config(&any_database_config)?;
let wallet_mutex = Mutex::new(BdkWallet::new(
&descriptor,
change_descriptor.to_owned().as_ref(),
change_descriptor.as_ref(),
network,
database,
blockchain,
)?);
Ok(Wallet { wallet_mutex })
}
fn get_wallet(&self) -> MutexGuard<BdkWallet<AnyDatabase>> {
self.wallet_mutex.lock().expect("wallet")
}
fn get_network(&self) -> Network {
self.get_wallet().network()
}
fn sync(
&self,
progress_update: Box<dyn BdkProgress>,
max_address_param: Option<u32>,
blockchain: &Blockchain,
progress: Option<Box<dyn Progress>>,
) -> Result<(), BdkError> {
self.get_wallet()
.sync(BdkProgressHolder { progress_update }, max_address_param)
let bdk_sync_opts = BdkSyncOptions {
progress: progress.map(|p| {
Box::new(ProgressHolder { progress: p })
as Box<(dyn bdk::blockchain::Progress + 'static)>
}),
};
let blockchain = blockchain.get_blockchain();
self.get_wallet().sync(blockchain.deref(), bdk_sync_opts)
}
fn broadcast(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<Transaction, Error> {
let tx = psbt.internal.lock().unwrap().clone().extract_tx();
self.get_wallet().broadcast(&tx)?;
let tx_details = self.get_wallet().get_tx(&tx.txid(), true)?;
Ok(Transaction::from(&tx_details.unwrap()))
fn get_address(&self, address_index: AddressIndex) -> Result<AddressInfo, BdkError> {
self.get_wallet()
.get_address(address_index.into())
.map(AddressInfo::from)
}
fn get_balance(&self) -> Result<u64, Error> {
self.get_wallet().get_balance()
}
fn sign(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<bool, Error> {
let mut psbt = psbt.internal.lock().unwrap();
self.get_wallet().sign(&mut psbt, SignOptions::default())
}
fn get_transactions(&self) -> Result<Vec<Transaction>, Error> {
let transactions = self.get_wallet().list_transactions(true)?;
Ok(transactions.iter().map(Transaction::from).collect())
}
fn list_unspent(&self) -> Result<Vec<LocalUtxo>, Error> {
let unspents = self.get_wallet().list_unspent()?;
Ok(unspents
.iter()
.map(|u| LocalUtxo::from_utxo(u, self.get_network()))
.collect())
}
}
@@ -313,4 +391,347 @@ fn restore_extended_key(
})
}
fn to_script_pubkey(address: &str) -> Result<Script, BdkError> {
Address::from_str(address)
.map(|x| x.script_pubkey())
.map_err(|e| BdkError::Generic(e.to_string()))
}
#[derive(Clone, Debug)]
enum RbfValue {
Default,
Value(u32),
}
#[derive(Clone, Debug)]
struct TxBuilder {
recipients: Vec<(String, u64)>,
utxos: Vec<OutPoint>,
unspendable: HashSet<OutPoint>,
change_policy: ChangeSpendPolicy,
manually_selected_only: bool,
fee_rate: Option<f32>,
fee_absolute: Option<u64>,
drain_wallet: bool,
drain_to: Option<String>,
rbf: Option<RbfValue>,
data: Vec<u8>,
}
impl TxBuilder {
fn new() -> Self {
TxBuilder {
recipients: Vec::new(),
utxos: Vec::new(),
unspendable: HashSet::new(),
change_policy: ChangeSpendPolicy::ChangeAllowed,
manually_selected_only: false,
fee_rate: None,
fee_absolute: None,
drain_wallet: false,
drain_to: None,
rbf: None,
data: Vec::new(),
}
}
fn add_recipient(&self, recipient: String, amount: u64) -> Arc<Self> {
let mut recipients = self.recipients.to_vec();
recipients.append(&mut vec![(recipient, amount)]);
Arc::new(TxBuilder {
recipients,
..self.clone()
})
}
fn add_unspendable(&self, unspendable: OutPoint) -> Arc<Self> {
let mut unspendable_hash_set = self.unspendable.clone();
unspendable_hash_set.insert(unspendable);
Arc::new(TxBuilder {
unspendable: unspendable_hash_set,
..self.clone()
})
}
fn add_utxo(&self, outpoint: OutPoint) -> Arc<Self> {
self.add_utxos(vec![outpoint])
}
fn add_utxos(&self, mut outpoints: Vec<OutPoint>) -> Arc<Self> {
let mut utxos = self.utxos.to_vec();
utxos.append(&mut outpoints);
Arc::new(TxBuilder {
utxos,
..self.clone()
})
}
fn do_not_spend_change(&self) -> Arc<Self> {
Arc::new(TxBuilder {
change_policy: ChangeSpendPolicy::ChangeForbidden,
..self.clone()
})
}
fn manually_selected_only(&self) -> Arc<Self> {
Arc::new(TxBuilder {
manually_selected_only: true,
..self.clone()
})
}
fn only_spend_change(&self) -> Arc<Self> {
Arc::new(TxBuilder {
change_policy: ChangeSpendPolicy::OnlyChange,
..self.clone()
})
}
fn unspendable(&self, unspendable: Vec<OutPoint>) -> Arc<Self> {
Arc::new(TxBuilder {
unspendable: unspendable.into_iter().collect(),
..self.clone()
})
}
fn fee_rate(&self, sat_per_vb: f32) -> Arc<Self> {
Arc::new(TxBuilder {
fee_rate: Some(sat_per_vb),
..self.clone()
})
}
fn fee_absolute(&self, fee_amount: u64) -> Arc<Self> {
Arc::new(TxBuilder {
fee_absolute: Some(fee_amount),
..self.clone()
})
}
fn drain_wallet(&self) -> Arc<Self> {
Arc::new(TxBuilder {
drain_wallet: true,
..self.clone()
})
}
fn drain_to(&self, address: String) -> Arc<Self> {
Arc::new(TxBuilder {
drain_to: Some(address),
..self.clone()
})
}
fn enable_rbf(&self) -> Arc<Self> {
Arc::new(TxBuilder {
rbf: Some(RbfValue::Default),
..self.clone()
})
}
fn enable_rbf_with_sequence(&self, nsequence: u32) -> Arc<Self> {
Arc::new(TxBuilder {
rbf: Some(RbfValue::Value(nsequence)),
..self.clone()
})
}
fn add_data(&self, data: Vec<u8>) -> Arc<Self> {
Arc::new(TxBuilder {
data,
..self.clone()
})
}
fn finish(&self, wallet: &Wallet) -> Result<Arc<PartiallySignedBitcoinTransaction>, Error> {
let wallet = wallet.get_wallet();
let mut tx_builder = wallet.build_tx();
for (address, amount) in &self.recipients {
tx_builder.add_recipient(to_script_pubkey(address)?, *amount);
}
tx_builder.change_policy(self.change_policy);
if !self.utxos.is_empty() {
let bdk_utxos: Vec<BdkOutPoint> = self.utxos.iter().map(BdkOutPoint::from).collect();
let utxos: &[BdkOutPoint] = &bdk_utxos;
tx_builder.add_utxos(utxos)?;
}
if !self.unspendable.is_empty() {
let bdk_unspendable: Vec<BdkOutPoint> =
self.unspendable.iter().map(BdkOutPoint::from).collect();
tx_builder.unspendable(bdk_unspendable);
}
if self.manually_selected_only {
tx_builder.manually_selected_only();
}
if let Some(sat_per_vb) = self.fee_rate {
tx_builder.fee_rate(FeeRate::from_sat_per_vb(sat_per_vb));
}
if let Some(fee_amount) = self.fee_absolute {
tx_builder.fee_absolute(fee_amount);
}
if self.drain_wallet {
tx_builder.drain_wallet();
}
if let Some(address) = &self.drain_to {
tx_builder.drain_to(to_script_pubkey(address)?);
}
if let Some(rbf) = &self.rbf {
match *rbf {
RbfValue::Default => {
tx_builder.enable_rbf();
}
RbfValue::Value(nsequence) => {
tx_builder.enable_rbf_with_sequence(nsequence);
}
}
}
if !&self.data.is_empty() {
tx_builder.add_data(self.data.as_slice());
}
tx_builder
.finish()
.map(|(psbt, _)| PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt),
})
.map(Arc::new)
}
}
#[derive(Clone)]
struct BumpFeeTxBuilder {
txid: String,
fee_rate: f32,
allow_shrinking: Option<String>,
rbf: Option<RbfValue>,
}
impl BumpFeeTxBuilder {
fn new(txid: String, fee_rate: f32) -> Self {
Self {
txid,
fee_rate,
allow_shrinking: None,
rbf: None,
}
}
fn allow_shrinking(&self, address: String) -> Arc<Self> {
Arc::new(Self {
allow_shrinking: Some(address),
..self.clone()
})
}
fn enable_rbf(&self) -> Arc<Self> {
Arc::new(Self {
rbf: Some(RbfValue::Default),
..self.clone()
})
}
fn enable_rbf_with_sequence(&self, nsequence: u32) -> Arc<Self> {
Arc::new(Self {
rbf: Some(RbfValue::Value(nsequence)),
..self.clone()
})
}
fn finish(&self, wallet: &Wallet) -> Result<Arc<PartiallySignedBitcoinTransaction>, Error> {
let wallet = wallet.get_wallet();
let txid = Txid::from_str(self.txid.as_str())?;
let mut tx_builder = wallet.build_fee_bump(txid)?;
tx_builder.fee_rate(FeeRate::from_sat_per_vb(self.fee_rate));
if let Some(allow_shrinking) = &self.allow_shrinking {
let address =
Address::from_str(allow_shrinking).map_err(|e| Error::Generic(e.to_string()))?;
let script = address.script_pubkey();
tx_builder.allow_shrinking(script)?;
}
if let Some(rbf) = &self.rbf {
match *rbf {
RbfValue::Default => {
tx_builder.enable_rbf();
}
RbfValue::Value(nsequence) => {
tx_builder.enable_rbf_with_sequence(nsequence);
}
}
}
tx_builder
.finish()
.map(|(psbt, _)| PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt),
})
.map(Arc::new)
}
}
uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);
// The goal of these tests to to ensure `bdk-ffi` intermediate code correctly calls `bdk` APIs.
// These tests should not be used to verify `bdk` behavior that is already tested in the `bdk`
// crate.
#[cfg(test)]
mod test {
use crate::{TxBuilder, Wallet};
use bdk::bitcoin::Address;
use bdk::bitcoin::Network::Testnet;
use bdk::wallet::get_funded_wallet;
use std::str::FromStr;
use std::sync::Mutex;
#[test]
fn test_drain_wallet() {
let test_wpkh = "wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)";
let (funded_wallet, _, _) = get_funded_wallet(test_wpkh);
let test_wallet = Wallet {
wallet_mutex: Mutex::new(funded_wallet),
};
let drain_to_address = "tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt".to_string();
let tx_builder = TxBuilder::new()
.drain_wallet()
.drain_to(drain_to_address.clone());
//dbg!(&tx_builder);
assert!(tx_builder.drain_wallet);
assert_eq!(tx_builder.drain_to, Some(drain_to_address));
let psbt = tx_builder.finish(&test_wallet).unwrap();
let psbt = psbt.internal.lock().unwrap().clone();
// confirm one input with 50,000 sats
assert_eq!(psbt.inputs.len(), 1);
let input_value = psbt
.inputs
.get(0)
.cloned()
.unwrap()
.non_witness_utxo
.unwrap()
.output
.get(0)
.unwrap()
.value;
assert_eq!(input_value, 50_000_u64);
// confirm one output to correct address with all sats - fee
assert_eq!(psbt.outputs.len(), 1);
let output_address = Address::from_script(
&psbt
.unsigned_tx
.output
.get(0)
.cloned()
.unwrap()
.script_pubkey,
Testnet,
)
.unwrap();
assert_eq!(
output_address,
Address::from_str("tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt").unwrap()
);
let output_value = psbt.unsigned_tx.output.get(0).cloned().unwrap().value;
assert_eq!(output_value, 49_890_u64); // input - fee
}
}