From b3d60f223b1074ccfd1164700ad22f9cdf8e89b2 Mon Sep 17 00:00:00 2001 From: Anorak Date: Fri, 2 Feb 2024 18:32:02 +0530 Subject: [PATCH 1/3] Added linux build with Dockerfile --- rust/Cargo.toml | 6 ++++-- rust/Dockerfile | 15 ++++++++++----- rust/build.sh | 3 +-- rust/docker-entrypoint.sh | 2 +- rust/linux.sh | 7 ++++++- rust/makefile | 4 ++-- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 3fa53ca..3065be0 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -9,8 +9,10 @@ name = "boltzclient" doctest = false crate-type = ["staticlib", "cdylib", "lib"] -[target.x86_64-unknown-linux-gnu] -linker = "x86_64-unknown-linux-gnu-gcc" +#Needed to build linux bin without docker +#[target.x86_64-unknown-linux-gnu] +#linker = "x86_64-unknown-linux-gnu-gcc" + [dependencies] boltz-client = { version = "0.1.2"} diff --git a/rust/Dockerfile b/rust/Dockerfile index 5daadbd..70901df 100644 --- a/rust/Dockerfile +++ b/rust/Dockerfile @@ -18,19 +18,24 @@ RUN apt-get update --allow-releaseinfo-change && \ openssl libssl-dev git expect jq lsb-release tree \ pkg-config autoconf libtool neovim -// CHANGE THIS TO ONLY USE LINUX TARGETS -RUN rustup target add x86_64-unknown-linux-gnu RUN mkdir /.cargo -COPY config /.cargo/config RUN chown -R debian /.cargo - +RUN cargo +COPY Cargo.toml /.cargo/config ENV CARGO_HOME=/.cargo ENV NDK_VERSION=23.0.7599858 - VOLUME ["/boltz-dart"] +#WORKDIR /boltz-dart + +#Used for cargo build to find Cargo.toml + +RUN rustup target add x86_64-unknown-linux-gnu +#RUN TARGET_CC=x86_64-unknown-linux-gnu cargo build --target-dir ./boltz-dart -r --target=x86_64-unknown-linux-gnu + + COPY docker-entrypoint.sh /usr/bin USER debian diff --git a/rust/build.sh b/rust/build.sh index 5af8f6a..9583f33 100755 --- a/rust/build.sh +++ b/rust/build.sh @@ -1,4 +1,3 @@ #!/bin/bash -e -./android.sh -./darwin.sh +./linux.sh exit \ No newline at end of file diff --git a/rust/docker-entrypoint.sh b/rust/docker-entrypoint.sh index 099a763..d16b902 100755 --- a/rust/docker-entrypoint.sh +++ b/rust/docker-entrypoint.sh @@ -1,3 +1,3 @@ #!/bin/bash -/boltz-dart/rust/build.sh +/boltz-dart/linux.sh exec "$@" \ No newline at end of file diff --git a/rust/linux.sh b/rust/linux.sh index 83065c2..6b9add3 100755 --- a/rust/linux.sh +++ b/rust/linux.sh @@ -7,9 +7,14 @@ LINUX_DIR=$ROOT/$NAME.$VERSION/linux # final binaries stored here # arm-unknown-linux-gnueabi \ # armv7-unknown-linux-gnueabi \ # i686-unknown-linux-gnu \ + +cd boltz-dart + +#cd ./src || exit + for TARGET in \ x86_64-unknown-linux-gnu do rustup target add $TARGET - TARGET_CC=x86_64-unknown-linux-gnu cargo build --target-dir $LINUX_DIR -r --target=x86_64-unknown-linux-gnu + cargo build --target-dir $LINUX_DIR -r --target=x86_64-unknown-linux-gnu done diff --git a/rust/makefile b/rust/makefile index 39457b9..6aa0ee2 100644 --- a/rust/makefile +++ b/rust/makefile @@ -21,10 +21,10 @@ init: ## all: Compile iOS, Android and bindings targets all: init jnilib xcframework linux - + linux: @echo "[Creating linux binary ...] $@" - sh linux.sh $(VERSION) + docker run --platform linux/x86_64 --name bwcbuilder01 -v $(shell pwd):/boltz-dart bwcbuilder && docker stop bwcbuilder01 && docker rm bwcbuilder01 @echo "[Done ✅]" jnilib: From 1af5bd0031d295deebc7df755b2e86c607cf3155 Mon Sep 17 00:00:00 2001 From: Anorak Date: Fri, 2 Feb 2024 18:46:11 +0530 Subject: [PATCH 2/3] linux.sh:correction --- rust/linux.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rust/linux.sh b/rust/linux.sh index 6b9add3..cc188bb 100755 --- a/rust/linux.sh +++ b/rust/linux.sh @@ -8,13 +8,13 @@ LINUX_DIR=$ROOT/$NAME.$VERSION/linux # final binaries stored here # armv7-unknown-linux-gnueabi \ # i686-unknown-linux-gnu \ -cd boltz-dart - -#cd ./src || exit +cd boltz-dart || exit for TARGET in \ - x86_64-unknown-linux-gnu + x86_64-unknown-linux-gnu + # aarch64-unknown-linux-gnu install using docker image amd-64/rust:slim-bullseye requires aarch64-linux-gnu-gcc + do rustup target add $TARGET - cargo build --target-dir $LINUX_DIR -r --target=x86_64-unknown-linux-gnu + cargo build --target-dir $LINUX_DIR -r --target=$TARGET done From 9bb7a0274d1bcc9562331dc205749bd7ad3bf13c Mon Sep 17 00:00:00 2001 From: Anorak Date: Sat, 3 Feb 2024 11:39:51 +0530 Subject: [PATCH 3/3] refactor:dockerfile, linux.sh --- rust/Dockerfile | 3 +- rust/docker-entrypoint.sh | 3 +- rust/linux.sh | 9 +- rust/makefile | 20 +- rust/src/api.rs | 612 ++++++++++++++++++++------------------ 5 files changed, 338 insertions(+), 309 deletions(-) diff --git a/rust/Dockerfile b/rust/Dockerfile index 70901df..3981477 100644 --- a/rust/Dockerfile +++ b/rust/Dockerfile @@ -1,5 +1,6 @@ FROM amd64/rust:slim-bullseye ARG USER_ID +ARG VERSION RUN if getent passwd $USER_ID >/dev/null; then \ existing_user=$(getent passwd $USER_ID | cut -d: -f1) && \ @@ -28,7 +29,7 @@ ENV NDK_VERSION=23.0.7599858 VOLUME ["/boltz-dart"] -#WORKDIR /boltz-dart +# WORKDIR /boltz-dart #Used for cargo build to find Cargo.toml diff --git a/rust/docker-entrypoint.sh b/rust/docker-entrypoint.sh index d16b902..07e4840 100755 --- a/rust/docker-entrypoint.sh +++ b/rust/docker-entrypoint.sh @@ -1,3 +1,4 @@ #!/bin/bash -/boltz-dart/linux.sh +cd boltz-dart || exit 1 +bash linux.sh "$VERSION" exec "$@" \ No newline at end of file diff --git a/rust/linux.sh b/rust/linux.sh index cc188bb..fb1eac7 100755 --- a/rust/linux.sh +++ b/rust/linux.sh @@ -1,6 +1,6 @@ #!/bin/bash ROOT="target" -VERSION="0.1.2" +VERSION=$1 NAME="libboltz" LINUX_DIR=$ROOT/$NAME.$VERSION/linux # final binaries stored here # aarch64-unknown-linux-gnu \ @@ -8,13 +8,14 @@ LINUX_DIR=$ROOT/$NAME.$VERSION/linux # final binaries stored here # armv7-unknown-linux-gnueabi \ # i686-unknown-linux-gnu \ -cd boltz-dart || exit - for TARGET in \ x86_64-unknown-linux-gnu # aarch64-unknown-linux-gnu install using docker image amd-64/rust:slim-bullseye requires aarch64-linux-gnu-gcc do rustup target add $TARGET - cargo build --target-dir $LINUX_DIR -r --target=$TARGET + cargo build --release --target=$TARGET done + +mkdir -p $LINUX_DIR/x86_64 +cp $ROOT/x86_64-unknown-linux-gnu/release/libboltzclient.so $LINUX_DIR/x86_64/ diff --git a/rust/makefile b/rust/makefile index 6aa0ee2..8dcb448 100644 --- a/rust/makefile +++ b/rust/makefile @@ -23,19 +23,21 @@ init: all: init jnilib xcframework linux linux: - @echo "[Creating linux binary ...] $@" - docker run --platform linux/x86_64 --name bwcbuilder01 -v $(shell pwd):/boltz-dart bwcbuilder && docker stop bwcbuilder01 && docker rm bwcbuilder01 - @echo "[Done ✅]" + @echo "[Creating linux binary ...] $@" + docker stop bwcbuilder01 ; docker rm bwcbuilder01; \ + docker build --platform linux/x86_64 --build-arg USER_ID=$(shell id -u) --build-arg VERSION=$(VERSION) -t bwcbuilder . && \ + docker run --platform linux/x86_64 --name bwcbuilder01 -v $(shell pwd):/boltz-dart bwcbuilder ; docker stop bwcbuilder01 ; docker rm bwcbuilder01 + @echo "[Done ✅]" jnilib: - @echo "[Creating android jniLibs ...] $@" - sh android.sh $(VERSION) - @echo "[Done ✅]" + @echo "[Creating android jniLibs ...] $@" + sh android.sh $(VERSION) + @echo "[Done ✅]" xcframework: - @echo "[Creating rust_bdk_ffi.xcframework ...] $@" - sh darwin.sh $(VERSION) - @echo "[Done ✅]" + @echo "[Creating rust_bdk_ffi.xcframework ...] $@" + sh darwin.sh $(VERSION) + @echo "[Done ✅]" # compress-build: # @echo "[COMPRESSING BINARIES TO FLUTTER PACKAGE ...] $@" diff --git a/rust/src/api.rs b/rust/src/api.rs index 0d40815..fa23c31 100644 --- a/rust/src/api.rs +++ b/rust/src/api.rs @@ -1,8 +1,8 @@ -use boltz_client::Keypair; -use boltz_client::{network::electrum::ElectrumConfig, swaps::bitcoin::BtcSwapScript}; use boltz_client::swaps::bitcoin::BtcSwapTx; use boltz_client::swaps::liquid::LBtcSwapScript; use boltz_client::swaps::liquid::LBtcSwapTx; +use boltz_client::Keypair; +use boltz_client::{network::electrum::ElectrumConfig, swaps::bitcoin::BtcSwapScript}; // use boltz_client::network::electrum::ElectrumConfig; // use boltz_client::swaps::boltz; use boltz_client::swaps::boltz::BoltzApiClient; @@ -12,37 +12,37 @@ use boltz_client::util::error::ErrorKind; use boltz_client::util::error::S5Error; use boltz_client::util::secrets::Preimage; -use crate::types::{BtcLnSwap, SubmarineSwapFees, ReverseSwapFees, AllFees, Limits}; use crate::types::BoltzError; -use crate::types::LbtcLnSwap; use crate::types::Chain; -use crate::types::SwapType; use crate::types::KeyPair; +use crate::types::LbtcLnSwap; +use crate::types::SwapType; +use crate::types::{AllFees, BtcLnSwap, Limits, ReverseSwapFees, SubmarineSwapFees}; // use crate::types::PreImage; pub struct Api {} impl Api { - pub fn swap_fees(boltz_url: String, output_amount: u64)->anyhow::Result{ + pub fn swap_fees(boltz_url: String, output_amount: u64) -> anyhow::Result { let boltz_client = BoltzApiClient::new(&boltz_url); let boltz_pairs = match boltz_client.get_pairs() { - Ok(result)=>result, - Err(e)=> return Err(e.into()) + Ok(result) => result, + Err(e) => return Err(e.into()), }; let btc_pair = boltz_pairs.get_btc_pair()?; let btc_limits = Limits { minimal: btc_pair.limits.minimal as u64, maximal: btc_pair.limits.maximal as u64, }; - let btc_submarine = SubmarineSwapFees{ + let btc_submarine = SubmarineSwapFees { boltz_fees: btc_pair.fees.submarine_boltz(output_amount)?, lockup_fees_estimate: btc_pair.fees.submarine_lockup_estimate(), - claim_fees: btc_pair.fees.submarine_claim()? + claim_fees: btc_pair.fees.submarine_claim()?, }; - let btc_reverse = ReverseSwapFees{ + let btc_reverse = ReverseSwapFees { boltz_fees: btc_pair.fees.reverse_boltz(output_amount)?, lockup_fees: btc_pair.fees.reverse_lockup()?, - claim_fees_estimate: btc_pair.fees.reverse_claim_estimate() + claim_fees_estimate: btc_pair.fees.reverse_claim_estimate(), }; let lbtc_pair = boltz_pairs.get_lbtc_pair()?; @@ -50,380 +50,404 @@ impl Api { minimal: lbtc_pair.limits.minimal as u64, maximal: lbtc_pair.limits.maximal as u64, }; - let lbtc_submarine = SubmarineSwapFees{ + let lbtc_submarine = SubmarineSwapFees { boltz_fees: lbtc_pair.fees.submarine_boltz(output_amount)?, lockup_fees_estimate: lbtc_pair.fees.submarine_lockup_estimate(), - claim_fees: lbtc_pair.fees.submarine_claim()? + claim_fees: lbtc_pair.fees.submarine_claim()?, }; - let lbtc_reverse = ReverseSwapFees{ + let lbtc_reverse = ReverseSwapFees { boltz_fees: lbtc_pair.fees.reverse_boltz(output_amount)?, lockup_fees: lbtc_pair.fees.reverse_lockup()?, - claim_fees_estimate: lbtc_pair.fees.reverse_claim_estimate() + claim_fees_estimate: lbtc_pair.fees.reverse_claim_estimate(), }; - - Ok(AllFees{btc_limits, lbtc_limits, btc_submarine,btc_reverse,lbtc_submarine,lbtc_reverse}) + + Ok(AllFees { + btc_limits, + lbtc_limits, + btc_submarine, + btc_reverse, + lbtc_submarine, + lbtc_reverse, + }) } // Should take pair hash from previous call as input - pub fn new_btc_ln_submarine( + pub fn new_btc_ln_submarine( mnemonic: String, index: u64, invoice: String, network: Chain, electrum_url: String, boltz_url: String, - ) -> anyhow::Result{ - let swap_type = SwapType::Submarine; - let refund_keypair = match KeyPair::new(mnemonic, network.into(), index, swap_type) { - Ok(keypair) => keypair, - Err(err) => return Err(err.into()), - }; - let boltz_client = BoltzApiClient::new(&boltz_url); - // let network_config = ElectrumConfig::new(network.into(), &electrum_url, true, true, false, None); - let boltz_pairs = match boltz_client.get_pairs() { - Ok(result)=>result, - Err(e)=> return Err(e.into()) - }; - let btc_pair = boltz_pairs.get_btc_pair()?; - let swap_request = CreateSwapRequest::new_btc_submarine(&btc_pair.hash, &invoice, &refund_keypair.public_key); - let response = match boltz_client.create_swap(swap_request){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; - let preimage = match Preimage::from_invoice_str(&invoice){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; - let swap_script = match response.into_btc_sub_swap_script(&preimage, &refund_keypair.clone().into(),network.into()){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; - let script_address = swap_script.to_address(network.into())?.to_string(); - - Ok(BtcLnSwap::new( - response.get_id(), - swap_type, - network, - refund_keypair, - preimage.into(), - response.get_redeem_script()?, - invoice, - script_address, - response.get_funding_amount()?, - electrum_url, - boltz_url, - )) + ) -> anyhow::Result { + let swap_type = SwapType::Submarine; + let refund_keypair = match KeyPair::new(mnemonic, network.into(), index, swap_type) { + Ok(keypair) => keypair, + Err(err) => return Err(err.into()), + }; + let boltz_client = BoltzApiClient::new(&boltz_url); + // let network_config = ElectrumConfig::new(network.into(), &electrum_url, true, true, false, None); + let boltz_pairs = match boltz_client.get_pairs() { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let btc_pair = boltz_pairs.get_btc_pair()?; + let swap_request = CreateSwapRequest::new_btc_submarine( + &btc_pair.hash, + &invoice, + &refund_keypair.public_key, + ); + let response = match boltz_client.create_swap(swap_request) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let preimage = match Preimage::from_invoice_str(&invoice) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let swap_script = match response.into_btc_sub_swap_script( + &preimage, + &refund_keypair.clone().into(), + network.into(), + ) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let script_address = swap_script.to_address(network.into())?.to_string(); + Ok(BtcLnSwap::new( + response.get_id(), + swap_type, + network, + refund_keypair, + preimage.into(), + response.get_redeem_script()?, + invoice, + script_address, + response.get_funding_amount()?, + electrum_url, + boltz_url, + )) } - - pub fn new_btc_ln_reverse( + + pub fn new_btc_ln_reverse( mnemonic: String, index: u64, out_amount: u64, network: Chain, electrum_url: String, boltz_url: String, - ) -> anyhow::Result{ - let swap_type = SwapType::Reverse; - let claim_keypair = match KeyPair::new(mnemonic, network.into(), index, swap_type) { - Ok(keypair) => keypair, - Err(err) => return Err(err.into()), - }; - let preimage = Preimage::new(); - let boltz_client = BoltzApiClient::new(&boltz_url); - // let network_config = ElectrumConfig::new(network.into(), &electrum_url, true, true, false, None); - let boltz_pairs = match boltz_client.get_pairs() { - Ok(result)=>result, - Err(e)=> return Err(e.into()) - }; + ) -> anyhow::Result { + let swap_type = SwapType::Reverse; + let claim_keypair = match KeyPair::new(mnemonic, network.into(), index, swap_type) { + Ok(keypair) => keypair, + Err(err) => return Err(err.into()), + }; + let preimage = Preimage::new(); + let boltz_client = BoltzApiClient::new(&boltz_url); + // let network_config = ElectrumConfig::new(network.into(), &electrum_url, true, true, false, None); + let boltz_pairs = match boltz_client.get_pairs() { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; - let pair_hash = boltz_pairs.get_btc_pair()?.hash; + let pair_hash = boltz_pairs.get_btc_pair()?.hash; - let swap_request = CreateSwapRequest::new_btc_reverse_invoice_amt(&pair_hash, &preimage.sha256.to_string(), &claim_keypair.public_key, out_amount); - let response = match boltz_client.create_swap(swap_request){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; - - let ckp: Keypair = claim_keypair.clone().into(); - let swap_script = match response.into_btc_rev_swap_script(&preimage, &ckp, network.into()){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; - let script_address = swap_script.to_address(network.into())?.to_string(); - - Ok(BtcLnSwap::new( - response.get_id(), - swap_type, - network.into(), - claim_keypair, - preimage.into(), - response.get_redeem_script().unwrap(), - response.get_invoice().unwrap().to_string(), - script_address, - out_amount, - electrum_url, - boltz_url, - )) + let swap_request = CreateSwapRequest::new_btc_reverse_invoice_amt( + &pair_hash, + &preimage.sha256.to_string(), + &claim_keypair.public_key, + out_amount, + ); + let response = match boltz_client.create_swap(swap_request) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let ckp: Keypair = claim_keypair.clone().into(); + let swap_script = match response.into_btc_rev_swap_script(&preimage, &ckp, network.into()) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let script_address = swap_script.to_address(network.into())?.to_string(); + + Ok(BtcLnSwap::new( + response.get_id(), + swap_type, + network.into(), + claim_keypair, + preimage.into(), + response.get_redeem_script().unwrap(), + response.get_invoice().unwrap().to_string(), + script_address, + out_amount, + electrum_url, + boltz_url, + )) } - - pub fn btc_ln_tx_size(swap: BtcLnSwap)->anyhow::Result{ + + pub fn btc_ln_tx_size(swap: BtcLnSwap) -> anyhow::Result { if swap.kind == SwapType::Submarine { return Err(S5Error::new(ErrorKind::Input, "Submarine swaps are not claimable").into()); - } - else{ + } else { () } - let script = match BtcSwapScript::reverse_from_str(&swap.redeem_script){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let script = match BtcSwapScript::reverse_from_str(&swap.redeem_script) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; - let network_config = ElectrumConfig::new(swap.network.into(), &swap.electrum_url, true, true, 10); + let network_config = + ElectrumConfig::new(swap.network.into(), &swap.electrum_url, true, true, 10); // okay to use script address, we are just chekcing size - let tx = match BtcSwapTx::new_claim(script, swap.script_address, &network_config){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let tx = match BtcSwapTx::new_claim(script, swap.script_address, &network_config) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; let ckp: Keypair = swap.keys.into(); - let size = match tx.size( - &ckp, - &swap.preimage.try_into().unwrap(), - ){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let size = match tx.size(&ckp, &swap.preimage.try_into().unwrap()) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; Ok(size) } - pub fn btc_ln_reverse_claim(swap: BtcLnSwap, out_address: String, abs_fee: u64)->anyhow::Result{ + pub fn btc_ln_reverse_claim( + swap: BtcLnSwap, + out_address: String, + abs_fee: u64, + ) -> anyhow::Result { if swap.kind == SwapType::Submarine { return Err(S5Error::new(ErrorKind::Input, "Submarine swaps are not claimable").into()); - } - else{ + } else { () } - let script = match BtcSwapScript::reverse_from_str(&swap.redeem_script){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let script = match BtcSwapScript::reverse_from_str(&swap.redeem_script) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; - let network_config = ElectrumConfig::new(swap.network.into(), &swap.electrum_url, true, true,10); - let script_balance = match script - .get_balance(&network_config){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let network_config = + ElectrumConfig::new(swap.network.into(), &swap.electrum_url, true, true, 10); + let script_balance = match script.get_balance(&network_config) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; if script_balance.0 > 0 || script_balance.1 > 0 { - let tx = match BtcSwapTx::new_claim(script, out_address, &network_config){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let tx = match BtcSwapTx::new_claim(script, out_address, &network_config) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; let ckp: Keypair = swap.keys.into(); - let signed = match tx.sign_claim( - &ckp, - &swap.preimage.try_into().unwrap(), - abs_fee, - ){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let signed = match tx.sign_claim(&ckp, &swap.preimage.try_into().unwrap(), abs_fee) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; - let txid = match tx.broadcast(signed, &network_config){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let txid = match tx.broadcast(signed, &network_config) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; Ok(txid) - } - else{ + } else { return Err(S5Error::new(ErrorKind::Script, "Script is not funded yet!").into()); } } - pub fn new_lbtc_ln_submarine( + pub fn new_lbtc_ln_submarine( mnemonic: String, index: u64, invoice: String, network: Chain, electrum_url: String, boltz_url: String, - ) -> anyhow::Result{ - let swap_type = SwapType::Submarine; - let refund_keypair = match KeyPair::new(mnemonic, network.into(), index, swap_type) { - Ok(keypair) => keypair, - Err(err) => return Err(err.into()), - }; - let boltz_client = BoltzApiClient::new(&boltz_url); - // let network_config = ElectrumConfig::new(network.into(), &electrum_url, true, true, false, None); - let boltz_pairs = match boltz_client.get_pairs() { - Ok(result)=>result, - Err(e)=> return Err(e.into()) - }; + ) -> anyhow::Result { + let swap_type = SwapType::Submarine; + let refund_keypair = match KeyPair::new(mnemonic, network.into(), index, swap_type) { + Ok(keypair) => keypair, + Err(err) => return Err(err.into()), + }; + let boltz_client = BoltzApiClient::new(&boltz_url); + // let network_config = ElectrumConfig::new(network.into(), &electrum_url, true, true, false, None); + let boltz_pairs = match boltz_client.get_pairs() { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; - let pair_hash = boltz_pairs.get_lbtc_pair()?.hash; + let pair_hash = boltz_pairs.get_lbtc_pair()?.hash; - let swap_request = CreateSwapRequest::new_btc_submarine(&pair_hash, &invoice, &refund_keypair.public_key); - let response = match boltz_client.create_swap(swap_request){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; - let preimage = match Preimage::from_invoice_str(&invoice){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; + let swap_request = + CreateSwapRequest::new_btc_submarine(&pair_hash, &invoice, &refund_keypair.public_key); + let response = match boltz_client.create_swap(swap_request) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let preimage = match Preimage::from_invoice_str(&invoice) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; - let script = match response.into_lbtc_sub_swap_script(&preimage, &refund_keypair.clone().into(), network.into()){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; - let out_address = script.to_address(network.into())?.to_string(); - - // let btc_swap_script = BtcSwapScript::submarine_from_str(&response.clone().redeem_script.unwrap()).unwrap(); - - // let payment_address = match btc_swap_script.to_address(network.clone().into()){ - // Ok(result)=>result.to_string(), - // Err(e)=>return Err(e.into()) - // }; - - // if &payment_address != &response.clone().address.unwrap(){ - // return Err(S5Error::new(ErrorKind::BoltzApi, "Payment address in response does not match constructed script! Report to support!").into()); - // } - - Ok(LbtcLnSwap::new( - response.get_id(), - swap_type, - network, - refund_keypair, - preimage.into(), - response.get_redeem_script().unwrap(), - invoice, - response.get_funding_amount().unwrap(), - out_address, - "".to_string(), - electrum_url, - boltz_url, - )) + let script = match response.into_lbtc_sub_swap_script( + &preimage, + &refund_keypair.clone().into(), + network.into(), + ) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let out_address = script.to_address(network.into())?.to_string(); + + // let btc_swap_script = BtcSwapScript::submarine_from_str(&response.clone().redeem_script.unwrap()).unwrap(); + // let payment_address = match btc_swap_script.to_address(network.clone().into()){ + // Ok(result)=>result.to_string(), + // Err(e)=>return Err(e.into()) + // }; + + // if &payment_address != &response.clone().address.unwrap(){ + // return Err(S5Error::new(ErrorKind::BoltzApi, "Payment address in response does not match constructed script! Report to support!").into()); + // } + + Ok(LbtcLnSwap::new( + response.get_id(), + swap_type, + network, + refund_keypair, + preimage.into(), + response.get_redeem_script().unwrap(), + invoice, + response.get_funding_amount().unwrap(), + out_address, + "".to_string(), + electrum_url, + boltz_url, + )) } - - pub fn new_lbtc_ln_reverse( + + pub fn new_lbtc_ln_reverse( mnemonic: String, index: u64, out_amount: u64, network: Chain, electrum_url: String, boltz_url: String, - ) -> anyhow::Result{ - let swap_type = SwapType::Reverse; - let claim_keypair = match KeyPair::new(mnemonic, network.into(), index, swap_type) { - Ok(keypair) => keypair, - Err(err) => return Err(err.into()), - }; - let preimage = Preimage::new(); - let boltz_client = BoltzApiClient::new(&boltz_url); - // let network_config = ElectrumConfig::new(network.into(), &electrum_url, true, true, false, None); - let boltz_pairs = match boltz_client.get_pairs() { - Ok(result)=>result, - Err(e)=> return Err(e.into()) - }; + ) -> anyhow::Result { + let swap_type = SwapType::Reverse; + let claim_keypair = match KeyPair::new(mnemonic, network.into(), index, swap_type) { + Ok(keypair) => keypair, + Err(err) => return Err(err.into()), + }; + let preimage = Preimage::new(); + let boltz_client = BoltzApiClient::new(&boltz_url); + // let network_config = ElectrumConfig::new(network.into(), &electrum_url, true, true, false, None); + let boltz_pairs = match boltz_client.get_pairs() { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; - let pair_hash = boltz_pairs.get_lbtc_pair()?.hash; + let pair_hash = boltz_pairs.get_lbtc_pair()?.hash; - let swap_request = CreateSwapRequest::new_btc_reverse_invoice_amt(&pair_hash, &preimage.sha256.to_string(), &claim_keypair.public_key, out_amount); - let response = match boltz_client.create_swap(swap_request){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; - let ckp = claim_keypair.clone().into(); - let script = match response.into_lbtc_rev_swap_script(&preimage, &ckp, network.into()){ - Ok(result)=>result, - Err(e)=>return Err(e.into()) - }; - let out_address = script.to_address(network.into()).unwrap().to_string(); - // let btc_swap_script = BtcSwapScript::submarine_from_str(&response.clone().redeem_script.unwrap()).unwrap(); - - // let payment_address = match btc_swap_script.to_address(network.clone().into()){ - // Ok(result)=>result.to_string(), - // Err(e)=>return Err(e.into()) - // }; - - // if &payment_address != &response.clone().address.unwrap(){ - // return Err(S5Error::new(ErrorKind::BoltzApi, "Payment address in response does not match constructed script! Report to support!").into()); - // } - - Ok(LbtcLnSwap::new( - response.get_id(), - swap_type, - network, - claim_keypair, - preimage.into(), - response.get_redeem_script().unwrap(), - response.get_invoice()?.to_string(), - out_amount, - out_address, - response.get_blinding_key()?, - electrum_url, - boltz_url, - )) + let swap_request = CreateSwapRequest::new_btc_reverse_invoice_amt( + &pair_hash, + &preimage.sha256.to_string(), + &claim_keypair.public_key, + out_amount, + ); + let response = match boltz_client.create_swap(swap_request) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let ckp = claim_keypair.clone().into(); + let script = match response.into_lbtc_rev_swap_script(&preimage, &ckp, network.into()) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let out_address = script.to_address(network.into()).unwrap().to_string(); + // let btc_swap_script = BtcSwapScript::submarine_from_str(&response.clone().redeem_script.unwrap()).unwrap(); + // let payment_address = match btc_swap_script.to_address(network.clone().into()){ + // Ok(result)=>result.to_string(), + // Err(e)=>return Err(e.into()) + // }; + + // if &payment_address != &response.clone().address.unwrap(){ + // return Err(S5Error::new(ErrorKind::BoltzApi, "Payment address in response does not match constructed script! Report to support!").into()); + // } + + Ok(LbtcLnSwap::new( + response.get_id(), + swap_type, + network, + claim_keypair, + preimage.into(), + response.get_redeem_script().unwrap(), + response.get_invoice()?.to_string(), + out_amount, + out_address, + response.get_blinding_key()?, + electrum_url, + boltz_url, + )) } - - pub fn lbtc_ln_tx_size(swap: LbtcLnSwap)->anyhow::Result{ + + pub fn lbtc_ln_tx_size(swap: LbtcLnSwap) -> anyhow::Result { if swap.kind == SwapType::Submarine { return Err(S5Error::new(ErrorKind::Input, "Submarine swaps are not claimable").into()); - } - else{ + } else { () } - let script = match LBtcSwapScript::reverse_from_str(&swap.redeem_script, &swap.blinding_key){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) - }; - let network_config = ElectrumConfig::new(swap.network.into(), &swap.electrum_url, true, true,10); - let mut tx = match LBtcSwapTx::new_claim(script, swap.script_address, &network_config){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) - }; - let size = match tx.size( - &swap.keys.into(), - &swap.preimage.try_into().unwrap(), - ){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let script = match LBtcSwapScript::reverse_from_str(&swap.redeem_script, &swap.blinding_key) + { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let network_config = + ElectrumConfig::new(swap.network.into(), &swap.electrum_url, true, true, 10); + let tx = match LBtcSwapTx::new_claim(script, swap.script_address, &network_config) { + Ok(result) => result, + Err(e) => return Err(e.into()), + }; + let size = match tx.size(&swap.keys.into(), &swap.preimage.try_into().unwrap()) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; Ok(size) } - pub fn lbtc_ln_reverse_claim(swap: LbtcLnSwap, out_address: String, abs_fee: u64)->anyhow::Result{ + pub fn lbtc_ln_reverse_claim( + swap: LbtcLnSwap, + out_address: String, + abs_fee: u64, + ) -> anyhow::Result { if swap.kind == SwapType::Submarine { return Err(S5Error::new(ErrorKind::Input, "Submarine swaps are not claimable").into()); - } - else{ + } else { () } - let script = match LBtcSwapScript::reverse_from_str(&swap.redeem_script, &swap.blinding_key){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let script = match LBtcSwapScript::reverse_from_str(&swap.redeem_script, &swap.blinding_key) + { + Ok(result) => result, + Err(e) => return Err(e.into()), }; - let network_config = ElectrumConfig::new(swap.network.into(), &swap.electrum_url, true, true,10); + let network_config = + ElectrumConfig::new(swap.network.into(), &swap.electrum_url, true, true, 10); // if script_balance.0 > 0 || script_balance.1 > 0 { - let mut tx = match LBtcSwapTx::new_claim(script, out_address, &network_config){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let mut tx = match LBtcSwapTx::new_claim(script, out_address, &network_config) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; let signed = match tx.sign_claim( - &swap.keys.into(), - &swap.preimage.try_into().unwrap(), - abs_fee - ){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + &swap.keys.into(), + &swap.preimage.try_into().unwrap(), + abs_fee, + ) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; - let txid = match tx.broadcast(signed, &network_config){ - Ok(result)=>result, - Err(e)=> return Err(e.into()) + let txid = match tx.broadcast(signed, &network_config) { + Ok(result) => result, + Err(e) => return Err(e.into()), }; Ok(txid) // } @@ -432,14 +456,14 @@ impl Api { // } } - pub fn swap_status(boltz_url: String, id: String)->anyhow::Result{ + pub fn swap_status(boltz_url: String, id: String) -> anyhow::Result { let boltz_client = BoltzApiClient::new(&boltz_url); // let network_config = ElectrumConfig::new(network.into(), &electrum_url, true, true, false, None); - match boltz_client.swap_status(SwapStatusRequest{id: id}) { - Ok(result)=>Ok(result.status), - Err(e)=> return Err(e.into()) + match boltz_client.swap_status(SwapStatusRequest { id: id }) { + Ok(result) => Ok(result.status), + Err(e) => return Err(e.into()), } - } + } } // flutter_rust_bridge_codegen --rust-input rust/src/api.rs --dart-output lib/bridge_generated.dart --dart-decl-output lib/bridge_definitions.dart