Skip to content

Commit

Permalink
Trying utxo_psbt
Browse files Browse the repository at this point in the history
  • Loading branch information
ademar111190 committed Aug 17, 2023
1 parent 5ee6bcf commit 74bc6b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 72 deletions.
105 changes: 41 additions & 64 deletions libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,37 @@ use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{anyhow, Result};
use bitcoin::bech32::{ToBase32, u5};
use bitcoin::bech32::{u5, ToBase32};
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, RecoveryId};
use bitcoin::secp256k1::PublicKey;
use bitcoin::secp256k1::Secp256k1;
use bitcoin::util::bip32::{ChildNumber, ExtendedPrivKey};
use ecies::utils::{aes_decrypt, aes_encrypt};
use gl_client::{node, pb, utils};
use gl_client::node::ClnClient;
use gl_client::pb::amount::Unit;
use gl_client::pb::cln::{
self, CloseRequest, ListclosedchannelsClosedchannels, ListclosedchannelsRequest,
ListpeerchannelsRequest,
};
use gl_client::pb::{
Amount, Invoice, InvoiceRequest, InvoiceStatus, ListFundsResponse, OffChainPayment, PayStatus,
Peer, WithdrawResponse,
};
use gl_client::pb::amount::Unit;
use gl_client::pb::cln::{self, CloseRequest, ListclosedchannelsClosedchannels, ListclosedchannelsRequest, ListpeerchannelsRequest};
use gl_client::scheduler::Scheduler;
use gl_client::signer::Signer;
use gl_client::tls::TlsConfig;
use gl_client::{node, pb, utils};
use lightning::util::message_signing::verify;
use lightning_invoice::{RawInvoice, SignedRawInvoice};
use regex::Regex;
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString};
use tokio::sync::{mpsc, Mutex};
use tonic::{Code, Streaming};
use tonic::Streaming;

use crate::{Channel, ChannelState, NodeConfig, PrepareWithdrawRequest, PrepareWithdrawResponse};
use crate::invoice::parse_invoice;
use crate::models::*;
use crate::persist::db::SqliteStorage;
use crate::{Channel, ChannelState, NodeConfig, PrepareWithdrawRequest, PrepareWithdrawResponse};

const MAX_PAYMENT_AMOUNT_MSAT: u64 = 4294967000;
const MAX_INBOUND_LIQUIDITY_MSAT: u64 = 4000000000;
Expand Down Expand Up @@ -64,8 +66,8 @@ impl Greenlight {
&signer,
vec![ChildNumber::from_hardened_idx(140)?, ChildNumber::from(0)],
)?
.to_priv()
.to_bytes();
.to_priv()
.to_bytes();
let encryption_key_slice = encryption_key.as_slice();

let register_credentials = match config.node_config.clone() {
Expand Down Expand Up @@ -107,7 +109,7 @@ impl Greenlight {
register_credentials.partner_credentials,
register_credentials.invite_code,
)
.await?;
.await?;
Ok(credentials)
}
}
Expand Down Expand Up @@ -190,7 +192,7 @@ impl Greenlight {
utils::scheduler_uri(),
&tls_config,
)
.await?;
.await?;
let recover_res: pb::scheduler::RegistrationResponse =
scheduler.register(&signer, invite_code).await?;

Expand Down Expand Up @@ -466,74 +468,49 @@ impl NodeAPI for Greenlight {
prepare_withdraw_request: PrepareWithdrawRequest,
) -> Result<PrepareWithdrawResponse> {
let funds = list_funds(self).await?;
let mut amount: u64 = 0;
let utxos: Vec<cln::Outpoint> = utxos(self, Some(funds))
.await?
.into_iter()
.map(|it| cln::Outpoint {
txid: it.txid,
outnum: it.outnum,
.map(|it| {
amount += it.amount_millisatoshi;
cln::Outpoint {
txid: it.txid,
outnum: it.outnum,
}
})
.collect();

let request = cln::TxprepareRequest {
outputs: vec![cln::OutputDesc {
address: prepare_withdraw_request.to_address.clone(),
amount: Some(cln::Amount { msat: 1000 }),
}],
let request = cln::UtxopsbtRequest {
satoshi: Some(cln::Amount { msat: 1000 }),
feerate: Some(cln::Feerate {
style: Some(cln::feerate::Style::Perkw(
prepare_withdraw_request.fee_rate_sats_per_vbyte.clone(),
prepare_withdraw_request.fee_rate_sats_per_vbyte,
)),
}),
minconf: None,
utxos: utxos.clone(),
startweight: 0,
utxos,
reserve: Some(0),
reservedok: Some(true),
locktime: None,
min_witness_weight: None,
excess_as_change: None,
};
println!("request {:?}", request);

let mut node_client = self.get_node_client().await?;
let tx_prepare = node_client.tx_prepare(request).await;
if tx_prepare.is_err() {
let status = tx_prepare.unwrap_err();
println!("tx_prepare error: {:?}", status);
if status.code() == Code::Unknown {
let regex = Regex::new(
r#"Error calling method TxPrepare: RpcError \{ code: Some\(-?\d+\), message: "UTXO .* already reserved" }"#
).unwrap();
let already_reserved = regex.is_match(&status.message());
if already_reserved {
println!("tx_prepare error, funds already reserved, trying to release and retry");
let mut all_released = true;
for utxo in utxos {
let released = self.release_prepared_withdraw(utxo.txid).await?;
all_released = released && all_released;
}
return if all_released {
self.prepare_withdraw(prepare_withdraw_request).await
} else {
Err(anyhow!("could not release funds"))
};
}
}
}

// let response = tx_prepare?.into_inner();
// println!("tx_prepare response: {:?}", response);
// println!("raw_tx_hex: {:?}", String::from_utf8(response.unsigned_tx)?);
println!("Request {:?}", request);
let response = self
.get_node_client()
.await?
.utxo_psbt(request)
.await?
.into_inner();
println!("Response {:?}", response);
let weight = response.estimated_final_weight;
return Ok(PrepareWithdrawResponse {
weight: 10,
fee_sat: 50,
weight,
fee_sat: (response.feerate_per_kw as u64) * (weight as u64),
});
}

async fn release_prepared_withdraw(&self, txid: Vec<u8>) -> Result<bool> {
let request = cln::TxdiscardRequest {
txid,
};
let response = self.get_node_client().await?.tx_discard(request).await;
println!("release prepared withdraw response: {:?}", response);
Ok(response.is_ok())
}

async fn start_signer(&self, shutdown: mpsc::Receiver<()>) {
_ = self.signer.run_forever(shutdown).await;
error!("signer exited");
Expand Down
13 changes: 5 additions & 8 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::max;
use std::str::FromStr;

use anyhow::{anyhow, ensure, Result};
Expand All @@ -10,19 +11,17 @@ use bitcoin::util::bip32::{ChildNumber, ExtendedPrivKey};
use bitcoin::{Address, Script};
use chrono::{DateTime, Utc};
use gl_client::pb::{Invoice, ListFundsResponse, WithdrawResponse};
use gl_client::signer::model::greenlight::Peer;
use lightning_invoice::RawInvoice;
use ripemd::Digest;
use ripemd::Ripemd160;
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
use rusqlite::ToSql;
use serde::{Deserialize, Serialize};
use std::cmp::max;
use gl_client::pb::cln::TxdiscardResponse;
use strum_macros::{Display, EnumString};
use tokio::sync::mpsc;
use tonic::Streaming;

use gl_client::signer::model::greenlight::Peer;

use crate::boltzswap::{BoltzApiCreateReverseSwapResponse, BoltzApiReverseSwapStatus};
use crate::fiat::{FiatCurrency, Rate};
use crate::grpc::{self, PaymentInformation, RegisterPaymentReply};
Expand All @@ -31,8 +30,6 @@ use crate::lsp::LspInformation;
use crate::models::Network::*;
use crate::{LNInvoice, LnUrlErrorData};

use strum_macros::{Display, EnumString};

/// Different types of supported payments
#[derive(Clone, PartialEq, Eq, Debug, EnumString, Display, Deserialize, Serialize)]
pub enum PaymentType {
Expand Down Expand Up @@ -72,7 +69,6 @@ pub trait NodeAPI: Send + Sync {
&self,
prepare_withdraw_request: PrepareWithdrawRequest,
) -> Result<PrepareWithdrawResponse>;
async fn release_prepared_withdraw(&self, txid: Vec<u8>) -> Result<bool>;
async fn start_signer(&self, shutdown: mpsc::Receiver<()>);
async fn list_peers(&self) -> Result<Vec<Peer>>;
async fn connect_peer(&self, node_id: String, addr: String) -> Result<()>;
Expand Down Expand Up @@ -1040,7 +1036,6 @@ impl FromStr for BuyBitcoinProvider {

#[cfg(test)]
mod tests {
use super::OpeningFeeParamsMenu;
use anyhow::Result;
use prost::Message;
use rand::random;
Expand All @@ -1049,6 +1044,8 @@ mod tests {
use crate::test_utils::{get_test_ofp, rand_vec_u8};
use crate::OpeningFeeParams;

use super::OpeningFeeParamsMenu;

#[test]
fn test_ofp_menu_validation() -> Result<()> {
// Menu with one entry is valid
Expand Down

0 comments on commit 74bc6b3

Please sign in to comment.