Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 133 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["crates/tx3-cardano", "crates/tx3-lang", "crates/tx3-test"]
members = [ "crates/tx3-bitcoin","crates/tx3-cardano", "crates/tx3-lang", "crates/tx3-test"]

[workspace.package]
publish = true
Expand Down
22 changes: 22 additions & 0 deletions crates/tx3-bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "tx3-bitcoin"
description = "Bitcoin compiler for tx3-lang protocols"
publish.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true
keywords.workspace = true
documentation.workspace = true
homepage.workspace = true
readme.workspace = true

[dependencies]
bitcoin = "0.32.6"
hex = "0.4.3"
serde = "1.0.219"
thiserror = "2.0.12"
tokio = "1.45.1"
trait-variant = "0.1.2"
tx3-lang = { version = "0.5.0", path = "../tx3-lang" }
69 changes: 69 additions & 0 deletions crates/tx3-bitcoin/src/compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use bitcoin::{
absolute::LockTime, hashes::Hash, transaction::Version, OutPoint, ScriptBuf, Sequence,
Transaction, TxIn, TxOut,
};

use tx3_lang::ir;

use crate::Error;

pub fn expr_into_amount(expr: &ir::Expression) -> Result<bitcoin::Amount, Error> {
match expr {
ir::Expression::Number(x) => Ok(bitcoin::Amount::from_sat(*x as u64)),
ir::Expression::Assets(x) if x.len() == 1 => expr_into_amount(&x[0].amount),
_ => Err(Error::CoerceError(
format!("{:?}", expr),
"Number".to_string(),
)),
}
}

fn compile_single_output(output: &ir::Output) -> Result<TxOut, Error> {
let value = output
.amount
.as_ref()
.map_or(Err(Error::MissingAmount), expr_into_amount)?;

let script_pubkey = output
.address
.as_ref()
.map_or(Err(Error::MissingAddress), expr_into_script_pubkey)?;

Ok(TxOut {
value,
script_pubkey,
})
}

fn compile_outputs(tx: &ir::Tx) -> Result<Vec<TxOut>, Error> {
tx.outputs.iter().map(compile_single_output).collect()
}

fn compile_inputs(tx: &ir::Tx) -> Result<Vec<TxIn>, Error> {
tx.inputs
.iter()
.flat_map(|input| input.refs.iter())
.map(|ref_| {
let txid = Hash::hash(ref_.txid.as_slice());
let vout = ref_.index as u32;

TxIn {
previous_output: OutPoint::new(txid, vout),
script_sig: ScriptBuf::new(),
sequence: Sequence::ZERO,
witness: Default::default(),
}
})
.collect()
}

pub fn compile_tx(tx: &ir::Tx, pparams: &PParams) -> Result<Transaction, Error> {
let tx = Transaction {
version: Version::TWO,
lock_time: LockTime::ZERO,
input: compile_inputs(tx)?,
output: compile_outputs(tx)?,
};

Ok(tx)
}
11 changes: 11 additions & 0 deletions crates/tx3-bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod compile;
mod resolve;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("error coercing {0} into {1}")]
CoerceError(String, String),

#[error("missing amount")]
MissingAmount,
}
Loading