bitcoindevkit / bdk-ffi

Please consider this project *experimental*.
Other
87 stars 39 forks source link

Enable building transaction from any given ScriptPubKey #159

Closed thunderbiscuit closed 1 year ago

thunderbiscuit commented 2 years ago

This feature is required to make the language bindings interop with LDK.

The LDK workflow is the following:

  1. LDK gives you an output script in raw bytes which you must use to build a ready-to-be-broadcast transaction (the funding transaction)
  2. You must give this tx in its raw form back to LDK, which will then broadcast it (potentially using BDK as well, see #157)

I have recreated what is required in Rust in this small cli tool. Of this workflow, I believe only a few lines would be "new" APIs for the bindings, namely:

// 1. transform the LDK output script from raw bytes into a Script type using either one of:
// let script: Script = Script::from_hex(&output_script_hex).unwrap();
let script: Script = Script::from(output_script_raw);

// 2. extract the tx from the psbt
let funding_tx: Transaction = psbt.extract_tx();

// 3. serialize the tx to give it back to LDK
let funding_tx_encoded = funding_tx.serialize();
thunderbiscuit commented 2 years ago

One of the first issues that need to be fixed for this to work is that our add_recipient() method on the TxBuilder only accepts addresses:

for (address, amount) in &self.recipients {
    tx_builder.add_recipient(to_script_pubkey(address)?, *amount);
}

We'll need to enable it to take arbitrary scripts.

The second part of this is to expose the ability to extract and serialize the transaction once it has been signed.