Skip to content

Commit

Permalink
feat: use script type from external library
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Jun 18, 2024
1 parent 6642c58 commit eaab4fe
Show file tree
Hide file tree
Showing 24 changed files with 6,742 additions and 40 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ xcuserdata
.idea/
.editorconfig
bdk.kt
bitcoin.kt

# Swift related
/.build
Expand All @@ -33,6 +34,11 @@ bdk.swift
.build
*.xcframework/
Info.plist
BitcoinFFI.h
Bitcoin.swift
BitcoinFFi.modulemap
bdkffi.xcframework

# Python related
__pycache__
__pycache__
bitcoin.py
10 changes: 10 additions & 0 deletions bdk-ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ bdk_bitcoind_rpc = { version = "0.10.0" }

uniffi = { version = "=0.27.1" }
bitcoin-internals = { version = "0.2.0", features = ["alloc"] }
bitcoin-ffi = { git = "https://github.com/thunderbiscuit/bitcoin-ffi.git", rev = "59f880aa45478511f067953fa7e903e60ebb10d3" }
# bitcoin-ffi = { version = "0.1.0", path = "../../bitcoin-ffi/" }
thiserror = "1.0.58"

[build-dependencies]
Expand Down
13 changes: 7 additions & 6 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -587,12 +587,6 @@ dictionary SentAndReceivedValues {
// bdk crate - bitcoin re-exports
// ------------------------------------------------------------------------

interface Script {
constructor(sequence<u8> raw_output_script);

sequence<u8> to_bytes();
};

[NonExhaustive]
enum Network {
"Bitcoin",
Expand Down Expand Up @@ -710,3 +704,10 @@ dictionary TxIn {
u32 sequence;
sequence<sequence<u8>> witness;
};

// ------------------------------------------------------------------------
// types defined in external crate rust-bitcoin-ffi
// ------------------------------------------------------------------------

[ExternalInterface="bitcoin_ffi"]
typedef extern Script;
40 changes: 20 additions & 20 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::fmt::Display;
use bdk_bitcoind_rpc::bitcoincore_rpc::jsonrpc::serde_json;
use bdk_wallet::bitcoin::address::{NetworkChecked, NetworkUnchecked};
use bdk_wallet::bitcoin::amount::ParseAmountError;
use bdk_wallet::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;
use bdk_wallet::bitcoin::blockdata::transaction::TxOut as BdkTxOut;
use bdk_wallet::bitcoin::consensus::encode::serialize;
use bdk_wallet::bitcoin::consensus::Decodable;
Expand All @@ -18,6 +17,7 @@ use bdk_wallet::bitcoin::Psbt as BdkPsbt;
use bdk_wallet::bitcoin::Transaction as BdkTransaction;
use bdk_wallet::bitcoin::TxIn as BdkTxIn;
use bdk_wallet::bitcoin::Txid;
use bitcoin_ffi::Script;

use std::io::Cursor;
use std::ops::Deref;
Expand Down Expand Up @@ -58,25 +58,25 @@ impl From<BdkAmount> for Amount {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Script(pub(crate) BdkScriptBuf);

impl Script {
pub fn new(raw_output_script: Vec<u8>) -> Self {
let script: BdkScriptBuf = raw_output_script.into();
Script(script)
}

pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}
}

impl From<BdkScriptBuf> for Script {
fn from(script: BdkScriptBuf) -> Self {
Script(script)
}
}
// #[derive(Clone, Debug, PartialEq, Eq)]
// pub struct Script(pub(crate) BdkScriptBuf);
//
// impl Script {
// pub fn new(raw_output_script: Vec<u8>) -> Self {
// let script: BdkScriptBuf = raw_output_script.into();
// Script(script)
// }
//
// pub fn to_bytes(&self) -> Vec<u8> {
// self.0.to_bytes()
// }
// }
//
// impl From<BdkScriptBuf> for Script {
// fn from(script: BdkScriptBuf) -> Self {
// Script(script)
// }
// }

#[derive(Debug, PartialEq, Eq)]
pub struct Address(BdkAddress<NetworkChecked>);
Expand Down
3 changes: 2 additions & 1 deletion bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::bitcoin::Amount;
use crate::bitcoin::FeeRate;
use crate::bitcoin::OutPoint;
use crate::bitcoin::Psbt;
use crate::bitcoin::Script;
use crate::bitcoin::Transaction;
use crate::bitcoin::TxIn;
use crate::bitcoin::TxOut;
Expand Down Expand Up @@ -61,6 +60,8 @@ use crate::wallet::TxBuilder;
use crate::wallet::Update;
use crate::wallet::Wallet;

use bitcoin_ffi::Script;

use bdk_wallet::bitcoin::Network;
use bdk_wallet::keys::bip39::WordCount;
use bdk_wallet::wallet::tx_builder::ChangeSpendPolicy;
Expand Down
4 changes: 2 additions & 2 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::bitcoin::Amount;
use crate::bitcoin::{Address, OutPoint, Script, Transaction, TxOut};
use crate::bitcoin::{Address, Amount, OutPoint, Transaction, TxOut};
use crate::InspectError;

use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf;
Expand All @@ -12,6 +11,7 @@ use bdk_wallet::wallet::AddressInfo as BdkAddressInfo;
use bdk_wallet::wallet::Balance as BdkBalance;
use bdk_wallet::KeychainKind;
use bdk_wallet::LocalOutput as BdkLocalOutput;
use bitcoin_ffi::Script;

use std::sync::{Arc, Mutex};

Expand Down
3 changes: 2 additions & 1 deletion bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::bitcoin::Amount;
use crate::bitcoin::{FeeRate, OutPoint, Psbt, Script, Transaction};
use crate::bitcoin::{FeeRate, OutPoint, Psbt, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
CalculateFeeError, CannotConnectError, CreateTxError, DescriptorError, PersistenceError,
Expand All @@ -20,6 +20,7 @@ use bdk_wallet::wallet::tx_builder::ChangeSpendPolicy;
use bdk_wallet::wallet::Update as BdkUpdate;
use bdk_wallet::Wallet as BdkWallet;
use bdk_wallet::{KeychainKind, SignOptions};
use bitcoin_ffi::Script;

use std::collections::HashSet;
use std::str::FromStr;
Expand Down
10 changes: 4 additions & 6 deletions bdk-ffi/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ Before running integration tests you must install the following development tool

1. [Java](https://openjdk.org/) and [Kotlin](https://kotlinlang.org/),
[sdkman](https://sdkman.io/) can help:
```shell
sdk install java 11.0.16.1-zulu
sdk install kotlin 1.7.20`
```

```shell
sdk install java 11.0.16.1-zulu
sdk install kotlin 1.7.20`
```
2. [Swift](https://www.swift.org/)

3. [Python](https://www.python.org/)
6 changes: 5 additions & 1 deletion bdk-ffi/tests/test_generated_bindings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
uniffi::build_foreign_language_testcases!(
"tests/bindings/test.kts",
"tests/bindings/test.swift",
"tests/bindings/test.py",
// Using types defined in an external library seems to break this test and we don't know how to
// fix it yet, but the actual Python tests and the generated package work fine.
// from .bitcoin import Script
// ImportError: attempted relative import with no known parent package
// "tests/bindings/test.py",
);
4 changes: 4 additions & 0 deletions bdk-ffi/uniffi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ cdylib_name = "bdkffi"
[bindings.swift]
module_name = "BitcoinDevKit"
cdylib_name = "bdkffi"

[bindings.kotlin.external_packages]
# Map the crate names from [External={name}] into Kotlin package names
bitcoin_ffi = "org.rustbitcoin.bitcoin"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bitcoindevkit

import org.rustbitcoin.bitcoin.Script
import kotlin.test.Test

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
Expand Down
3 changes: 3 additions & 0 deletions bdk-python/tests/test_live_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def test_broadcast_transaction(self):
address="tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989",
network=bdk.Network.SIGNET
)

script: bdk.Script = recipient.script_pubkey
print(f"The script for address {recipient} is {script}")

psbt: bdk.Psbt = bdk.TxBuilder().add_recipient(script=recipient.script_pubkey(), amount=bdk.Amount.from_sat(4200)).fee_rate(fee_rate=bdk.FeeRate.from_sat_per_vb(2)).finish(wallet)
self.assertTrue(psbt.serialize().startswith("cHNi"), "The PSBT should start with cHNi")
Expand Down
Loading

0 comments on commit eaab4fe

Please sign in to comment.