Compare commits
1 Commits
release/0.
...
release/0.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f5ac99fee |
17
CHANGELOG.md
17
CHANGELOG.md
@@ -6,20 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
## [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
|
- 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]
|
## [v0.4.0]
|
||||||
|
|
||||||
@@ -43,9 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [v0.2.0]
|
## [v0.2.0]
|
||||||
|
|
||||||
[unreleased]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.6.0...HEAD
|
[unreleased]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.4.0...HEAD
|
||||||
[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.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.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
|
[v0.3.0]: https://github.com/bitcoindevkit/bdk-ffi/compare/v0.2.0...v0.3.0
|
||||||
|
|||||||
21
Cargo.toml
21
Cargo.toml
@@ -1,23 +1,32 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bdk-ffi"
|
name = "bdk-ffi"
|
||||||
version = "0.6.0"
|
version = "0.4.1"
|
||||||
authors = ["Steve Myers <steve@notmandatory.org>", "Sudarsan Balaji <sudarsan.balaji@artfuldev.com>"]
|
authors = ["Steve Myers <steve@notmandatory.org>", "Sudarsan Balaji <sudarsan.balaji@artfuldev.com>"]
|
||||||
edition = "2018"
|
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
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["staticlib", "cdylib"]
|
crate-type = ["staticlib", "cdylib"]
|
||||||
name = "bdkffi"
|
name = "bdkffi"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bdk = { version = "0.18", features = ["all-keys", "use-esplora-ureq", "sqlite-bundled"] }
|
bdk = { version = "0.14", features = ["all-keys", "use-esplora-ureq", "sqlite"] }
|
||||||
|
|
||||||
|
# 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_macros = { version = "0.16.0", features = ["builtin-bindgen"] }
|
||||||
uniffi = { 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 }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
uniffi_build = { version = "0.16.0", features = ["builtin-bindgen"] }
|
uniffi_build = { version = "0.16.0", features = ["builtin-bindgen"] }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
generate-python = ["uniffi_bindgen"]
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "generate"
|
||||||
|
|||||||
23
README.md
23
README.md
@@ -1,10 +1,9 @@
|
|||||||
# Native language bindings for BDK
|
# Native language bindings for BDK
|
||||||
|
|
||||||
The workspace in this repository creates the `libbdkffi` multi-language library for the rust based
|
This repository contains source code for generating native language bindings for the rust based
|
||||||
[bdk] library from the [Bitcoin Dev Kit] project. The `bdk-ffi-bindgen` package builds a tool for
|
[bdk] library which is the central artifact of the [Bitcoin Dev Kit] project.
|
||||||
generating the actual language binding code used to access the `libbdkffi` library.
|
|
||||||
|
|
||||||
Each supported language has its own repository that includes this project as a [git submodule].
|
Each supported language has it's 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
|
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.
|
uniform way using the [mozilla/uniffi-rs] bindings generator for each supported target language.
|
||||||
|
|
||||||
@@ -20,14 +19,6 @@ language binding for [bdk] supported by this project.
|
|||||||
| Swift | iOS, macOS | [bdk-swift] |
|
| Swift | iOS, macOS | [bdk-swift] |
|
||||||
| Python | linux, macOS | [bdk-python] |
|
| 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
|
[bdk]: https://github.com/bitcoindevkit/bdk
|
||||||
[Bitcoin Dev Kit]: https://github.com/bitcoindevkit
|
[Bitcoin Dev Kit]: https://github.com/bitcoindevkit
|
||||||
[git submodule]: https://git-scm.com/book/en/v2/Git-Tools-Submodules
|
[git submodule]: https://git-scm.com/book/en/v2/Git-Tools-Submodules
|
||||||
@@ -39,6 +30,14 @@ cargo run -p bdk-ffi-bindgen -- --help
|
|||||||
|
|
||||||
## Contributing
|
## 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
|
### Adding new structs and functions
|
||||||
|
|
||||||
See the [UniFFI User Guide](https://mozilla.github.io/uniffi-rs/)
|
See the [UniFFI User Guide](https://mozilla.github.io/uniffi-rs/)
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "bdk-ffi-bindgen"
|
|
||||||
version = "0.2.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
anyhow = "=1.0.45" # remove after upgrading to next version of uniffi
|
|
||||||
structopt = "0.3"
|
|
||||||
uniffi_bindgen = "0.16.0"
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
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> {
|
|
||||||
uniffi_bindgen::generate_bindings(
|
|
||||||
&opt.udl_file,
|
|
||||||
None,
|
|
||||||
vec![opt.language.to_string().as_str()],
|
|
||||||
Some(&opt.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(())
|
|
||||||
}
|
|
||||||
45
src/bdk.udl
45
src/bdk.udl
@@ -73,7 +73,7 @@ interface DatabaseConfig {
|
|||||||
};
|
};
|
||||||
|
|
||||||
dictionary TransactionDetails {
|
dictionary TransactionDetails {
|
||||||
u64? fee;
|
u64? fees;
|
||||||
u64 received;
|
u64 received;
|
||||||
u64 sent;
|
u64 sent;
|
||||||
string txid;
|
string txid;
|
||||||
@@ -101,9 +101,9 @@ dictionary ElectrumConfig {
|
|||||||
dictionary EsploraConfig {
|
dictionary EsploraConfig {
|
||||||
string base_url;
|
string base_url;
|
||||||
string? proxy;
|
string? proxy;
|
||||||
u8? concurrency;
|
u64 timeout_read;
|
||||||
|
u64 timeout_write;
|
||||||
u64 stop_gap;
|
u64 stop_gap;
|
||||||
u64? timeout;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
[Enum]
|
[Enum]
|
||||||
@@ -112,20 +112,13 @@ interface BlockchainConfig {
|
|||||||
Esplora(EsploraConfig config);
|
Esplora(EsploraConfig config);
|
||||||
};
|
};
|
||||||
|
|
||||||
interface Blockchain {
|
callback interface BdkProgress {
|
||||||
[Throws=BdkError]
|
|
||||||
constructor(BlockchainConfig config);
|
|
||||||
[Throws=BdkError]
|
|
||||||
void broadcast([ByRef] PartiallySignedBitcoinTransaction psbt);
|
|
||||||
};
|
|
||||||
|
|
||||||
callback interface Progress {
|
|
||||||
void update(f32 progress, string? message);
|
void update(f32 progress, string? message);
|
||||||
};
|
};
|
||||||
|
|
||||||
interface Wallet {
|
interface Wallet {
|
||||||
[Throws=BdkError]
|
[Throws=BdkError]
|
||||||
constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config);
|
constructor(string descriptor, string? change_descriptor, Network network, DatabaseConfig database_config, BlockchainConfig blockchain_config);
|
||||||
string get_new_address();
|
string get_new_address();
|
||||||
string get_last_unused_address();
|
string get_last_unused_address();
|
||||||
[Throws=BdkError]
|
[Throws=BdkError]
|
||||||
@@ -136,35 +129,17 @@ interface Wallet {
|
|||||||
sequence<Transaction> get_transactions();
|
sequence<Transaction> get_transactions();
|
||||||
Network get_network();
|
Network get_network();
|
||||||
[Throws=BdkError]
|
[Throws=BdkError]
|
||||||
void sync([ByRef] Blockchain blockchain, Progress? progress);
|
void sync(BdkProgress progress_update, u32? max_address_param);
|
||||||
|
[Throws=BdkError]
|
||||||
|
string broadcast([ByRef] PartiallySignedBitcoinTransaction psbt);
|
||||||
};
|
};
|
||||||
|
|
||||||
interface PartiallySignedBitcoinTransaction {
|
interface PartiallySignedBitcoinTransaction {
|
||||||
[Throws=BdkError]
|
[Throws=BdkError]
|
||||||
|
constructor([ByRef] Wallet wallet, string recipient, u64 amount, float? fee_rate);
|
||||||
|
[Name=deserialize,Throws=BdkError]
|
||||||
constructor(string psbt_base64);
|
constructor(string psbt_base64);
|
||||||
string serialize();
|
string serialize();
|
||||||
string txid();
|
|
||||||
};
|
|
||||||
|
|
||||||
interface TxBuilder {
|
|
||||||
constructor();
|
|
||||||
TxBuilder add_recipient(string address, u64 amount);
|
|
||||||
TxBuilder fee_rate(float sat_per_vbyte);
|
|
||||||
TxBuilder drain_wallet();
|
|
||||||
TxBuilder drain_to(string address);
|
|
||||||
TxBuilder enable_rbf();
|
|
||||||
TxBuilder enable_rbf_with_sequence(u32 nsequence);
|
|
||||||
[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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
dictionary ExtendedKeyInfo {
|
dictionary ExtendedKeyInfo {
|
||||||
|
|||||||
67
src/bin/generate.rs
Normal file
67
src/bin/generate.rs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
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(())
|
||||||
|
}
|
||||||
501
src/lib.rs
501
src/lib.rs
@@ -1,26 +1,22 @@
|
|||||||
use bdk::bitcoin::hashes::hex::ToHex;
|
use bdk::bitcoin::hashes::hex::ToHex;
|
||||||
use bdk::bitcoin::secp256k1::Secp256k1;
|
use bdk::bitcoin::secp256k1::Secp256k1;
|
||||||
use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
|
use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
|
||||||
use bdk::bitcoin::{Address, Network, Script, Txid};
|
use bdk::bitcoin::{Address, Network};
|
||||||
use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig};
|
use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig};
|
||||||
|
use bdk::blockchain::Progress;
|
||||||
use bdk::blockchain::{
|
use bdk::blockchain::{
|
||||||
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
|
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
|
||||||
};
|
};
|
||||||
use bdk::blockchain::{Blockchain as BdkBlockchain, Progress as BdkProgress};
|
|
||||||
use bdk::database::any::{AnyDatabase, SledDbConfiguration, SqliteDbConfiguration};
|
use bdk::database::any::{AnyDatabase, SledDbConfiguration, SqliteDbConfiguration};
|
||||||
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
|
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
|
||||||
use bdk::keys::bip39::{Language, Mnemonic, WordCount};
|
use bdk::keys::bip39::{Language, Mnemonic, WordCount};
|
||||||
use bdk::keys::{DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey};
|
use bdk::keys::{DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey};
|
||||||
use bdk::miniscript::BareCtx;
|
use bdk::miniscript::BareCtx;
|
||||||
use bdk::wallet::AddressIndex;
|
use bdk::wallet::AddressIndex;
|
||||||
use bdk::{
|
use bdk::{BlockTime, Error, FeeRate, SignOptions, Wallet as BdkWallet};
|
||||||
BlockTime, Error, FeeRate, SignOptions, SyncOptions as BdkSyncOptions, Wallet as BdkWallet,
|
|
||||||
};
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
|
||||||
use std::ops::Deref;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::{Arc, Mutex, MutexGuard};
|
use std::sync::{Mutex, MutexGuard};
|
||||||
|
|
||||||
uniffi_macros::include_scaffolding!("bdk");
|
uniffi_macros::include_scaffolding!("bdk");
|
||||||
|
|
||||||
@@ -43,9 +39,9 @@ pub struct ElectrumConfig {
|
|||||||
pub struct EsploraConfig {
|
pub struct EsploraConfig {
|
||||||
pub base_url: String,
|
pub base_url: String,
|
||||||
pub proxy: Option<String>,
|
pub proxy: Option<String>,
|
||||||
pub concurrency: Option<u8>,
|
pub timeout_read: u64,
|
||||||
|
pub timeout_write: u64,
|
||||||
pub stop_gap: u64,
|
pub stop_gap: u64,
|
||||||
pub timeout: Option<u64>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum BlockchainConfig {
|
pub enum BlockchainConfig {
|
||||||
@@ -53,9 +49,13 @@ pub enum BlockchainConfig {
|
|||||||
Esplora { config: EsploraConfig },
|
Esplora { config: EsploraConfig },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait WalletHolder<B> {
|
||||||
|
fn get_wallet(&self) -> MutexGuard<BdkWallet<B, AnyDatabase>>;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||||
pub struct TransactionDetails {
|
pub struct TransactionDetails {
|
||||||
pub fee: Option<u64>,
|
pub fees: Option<u64>,
|
||||||
pub received: u64,
|
pub received: u64,
|
||||||
pub sent: u64,
|
pub sent: u64,
|
||||||
pub txid: String,
|
pub txid: String,
|
||||||
@@ -75,7 +75,7 @@ pub enum Transaction {
|
|||||||
impl From<&bdk::TransactionDetails> for TransactionDetails {
|
impl From<&bdk::TransactionDetails> for TransactionDetails {
|
||||||
fn from(x: &bdk::TransactionDetails) -> TransactionDetails {
|
fn from(x: &bdk::TransactionDetails) -> TransactionDetails {
|
||||||
TransactionDetails {
|
TransactionDetails {
|
||||||
fee: x.fee,
|
fees: x.fee,
|
||||||
txid: x.txid.to_string(),
|
txid: x.txid.to_string(),
|
||||||
received: x.received,
|
received: x.received,
|
||||||
sent: x.sent,
|
sent: x.sent,
|
||||||
@@ -97,144 +97,7 @@ impl From<&bdk::TransactionDetails> for Transaction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Blockchain {
|
trait WalletOperations<B>: WalletHolder<B> {
|
||||||
blockchain_mutex: Mutex<AnyBlockchain>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Blockchain {
|
|
||||||
fn new(blockchain_config: BlockchainConfig) -> Result<Self, BdkError> {
|
|
||||||
let any_blockchain_config = match blockchain_config {
|
|
||||||
BlockchainConfig::Electrum { config } => {
|
|
||||||
AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
|
|
||||||
retry: config.retry,
|
|
||||||
socks5: config.socks5,
|
|
||||||
timeout: config.timeout,
|
|
||||||
url: config.url,
|
|
||||||
stop_gap: usize::try_from(config.stop_gap).unwrap(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
BlockchainConfig::Esplora { config } => {
|
|
||||||
AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
|
|
||||||
base_url: config.base_url,
|
|
||||||
proxy: config.proxy,
|
|
||||||
concurrency: config.concurrency,
|
|
||||||
stop_gap: usize::try_from(config.stop_gap).unwrap(),
|
|
||||||
timeout: config.timeout,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
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>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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.as_ref(),
|
|
||||||
network,
|
|
||||||
database,
|
|
||||||
)?);
|
|
||||||
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,
|
|
||||||
blockchain: &Blockchain,
|
|
||||||
progress: Option<Box<dyn Progress>>,
|
|
||||||
) -> Result<(), BdkError> {
|
|
||||||
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 get_new_address(&self) -> String {
|
fn get_new_address(&self) -> String {
|
||||||
self.get_wallet()
|
self.get_wallet()
|
||||||
.get_address(AddressIndex::New)
|
.get_address(AddressIndex::New)
|
||||||
@@ -273,6 +136,143 @@ impl Wallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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().expect("wallet")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
};
|
||||||
|
let any_blockchain_config = match blockchain_config {
|
||||||
|
BlockchainConfig::Electrum { config } => {
|
||||||
|
AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
|
||||||
|
retry: config.retry,
|
||||||
|
socks5: config.socks5,
|
||||||
|
timeout: config.timeout,
|
||||||
|
url: config.url,
|
||||||
|
stop_gap: usize::try_from(config.stop_gap).unwrap(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
BlockchainConfig::Esplora { config } => {
|
||||||
|
AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
|
||||||
|
base_url: config.base_url,
|
||||||
|
proxy: config.proxy,
|
||||||
|
timeout_read: config.timeout_read,
|
||||||
|
timeout_write: config.timeout_write,
|
||||||
|
stop_gap: usize::try_from(config.stop_gap).unwrap(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let database = AnyDatabase::from_config(&any_database_config)?;
|
||||||
|
let blockchain = AnyBlockchain::from_config(&any_blockchain_config)?;
|
||||||
|
let wallet_mutex = Mutex::new(BdkWallet::new(
|
||||||
|
&descriptor,
|
||||||
|
change_descriptor.to_owned().as_ref(),
|
||||||
|
network,
|
||||||
|
database,
|
||||||
|
blockchain,
|
||||||
|
)?);
|
||||||
|
Ok(Wallet { wallet_mutex })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_network(&self) -> Network {
|
||||||
|
self.get_wallet().network()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sync(
|
||||||
|
&self,
|
||||||
|
progress_update: Box<dyn BdkProgress>,
|
||||||
|
max_address_param: Option<u32>,
|
||||||
|
) -> Result<(), BdkError> {
|
||||||
|
self.get_wallet()
|
||||||
|
.sync(BdkProgressHolder { progress_update }, max_address_param)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn broadcast(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<String, Error> {
|
||||||
|
let tx = psbt.internal.lock().unwrap().clone().extract_tx();
|
||||||
|
let txid = self.get_wallet().broadcast(&tx)?;
|
||||||
|
Ok(txid.to_hex())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ExtendedKeyInfo {
|
pub struct ExtendedKeyInfo {
|
||||||
mnemonic: String,
|
mnemonic: String,
|
||||||
xprv: String,
|
xprv: String,
|
||||||
@@ -313,205 +313,4 @@ 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)]
|
|
||||||
enum RbfValue {
|
|
||||||
Default,
|
|
||||||
Value(u32),
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TxBuilder {
|
|
||||||
recipients: Vec<(String, u64)>,
|
|
||||||
fee_rate: Option<f32>,
|
|
||||||
drain_wallet: bool,
|
|
||||||
drain_to: Option<String>,
|
|
||||||
rbf: Option<RbfValue>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TxBuilder {
|
|
||||||
fn new() -> Self {
|
|
||||||
TxBuilder {
|
|
||||||
recipients: Vec::new(),
|
|
||||||
fee_rate: None,
|
|
||||||
drain_wallet: false,
|
|
||||||
drain_to: None,
|
|
||||||
rbf: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
fee_rate: self.fee_rate,
|
|
||||||
drain_wallet: self.drain_wallet,
|
|
||||||
drain_to: self.drain_to.clone(),
|
|
||||||
rbf: self.rbf.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fee_rate(&self, sat_per_vb: f32) -> Arc<Self> {
|
|
||||||
Arc::new(TxBuilder {
|
|
||||||
recipients: self.recipients.to_vec(),
|
|
||||||
fee_rate: Some(sat_per_vb),
|
|
||||||
drain_wallet: self.drain_wallet,
|
|
||||||
drain_to: self.drain_to.clone(),
|
|
||||||
rbf: self.rbf.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn drain_wallet(&self) -> Arc<Self> {
|
|
||||||
Arc::new(TxBuilder {
|
|
||||||
recipients: self.recipients.to_vec(),
|
|
||||||
fee_rate: self.fee_rate,
|
|
||||||
drain_wallet: true,
|
|
||||||
drain_to: self.drain_to.clone(),
|
|
||||||
rbf: self.rbf.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn drain_to(&self, address: String) -> Arc<Self> {
|
|
||||||
Arc::new(TxBuilder {
|
|
||||||
recipients: self.recipients.to_vec(),
|
|
||||||
fee_rate: self.fee_rate,
|
|
||||||
drain_wallet: self.drain_wallet,
|
|
||||||
drain_to: Some(address),
|
|
||||||
rbf: self.rbf.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn enable_rbf(&self) -> Arc<Self> {
|
|
||||||
Arc::new(TxBuilder {
|
|
||||||
recipients: self.recipients.to_vec(),
|
|
||||||
fee_rate: self.fee_rate,
|
|
||||||
drain_wallet: self.drain_wallet,
|
|
||||||
drain_to: self.drain_to.clone(),
|
|
||||||
rbf: Some(RbfValue::Default),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn enable_rbf_with_sequence(&self, nsequence: u32) -> Arc<Self> {
|
|
||||||
Arc::new(TxBuilder {
|
|
||||||
recipients: self.recipients.to_vec(),
|
|
||||||
fee_rate: self.fee_rate,
|
|
||||||
drain_wallet: self.drain_wallet,
|
|
||||||
drain_to: self.drain_to.clone(),
|
|
||||||
rbf: Some(RbfValue::Value(nsequence)),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
if let Some(sat_per_vb) = self.fee_rate {
|
|
||||||
tx_builder.fee_rate(FeeRate::from_sat_per_vb(sat_per_vb));
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tx_builder
|
|
||||||
.finish()
|
|
||||||
.map(|(psbt, _)| PartiallySignedBitcoinTransaction {
|
|
||||||
internal: Mutex::new(psbt),
|
|
||||||
})
|
|
||||||
.map(Arc::new)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
txid: self.txid.clone(),
|
|
||||||
fee_rate: self.fee_rate,
|
|
||||||
allow_shrinking: Some(address),
|
|
||||||
rbf: self.rbf.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn enable_rbf(&self) -> Arc<Self> {
|
|
||||||
Arc::new(Self {
|
|
||||||
txid: self.txid.clone(),
|
|
||||||
fee_rate: self.fee_rate,
|
|
||||||
allow_shrinking: self.allow_shrinking.clone(),
|
|
||||||
rbf: Some(RbfValue::Default),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn enable_rbf_with_sequence(&self, nsequence: u32) -> Arc<Self> {
|
|
||||||
Arc::new(Self {
|
|
||||||
txid: self.txid.clone(),
|
|
||||||
fee_rate: self.fee_rate,
|
|
||||||
allow_shrinking: self.allow_shrinking.clone(),
|
|
||||||
rbf: Some(RbfValue::Value(nsequence)),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);
|
||||||
|
|||||||
Reference in New Issue
Block a user