From 400f0850d1371154305a01ae843a2db705ae5585 Mon Sep 17 00:00:00 2001 From: Armando Dutra Date: Fri, 6 Jan 2023 19:25:14 -0300 Subject: [PATCH] use address compat to display address --- Cargo.lock | 1 + rpc/Cargo.toml | 1 + rpc/src/messages.rs | 7 ++++--- src/lnpd/funding.rs | 7 ++++--- src/lnpd/runtime.rs | 16 +++++++++++----- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6d97b1a..3daac60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1345,6 +1345,7 @@ version = "0.9.0-rc.1" dependencies = [ "amplify", "bitcoin", + "bitcoin_scripts", "colored", "descriptor-wallet", "internet2", diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index ea45e37..70cdd3b 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -16,6 +16,7 @@ name = "lnp_rpc" [dependencies] amplify = "3.13.0" strict_encoding = "0.9.0-rc.2" +bitcoin_scripts = "0.9.0-rc.1" lnp-core = { version = "0.9.0-rc.1", default-features = false } lnpbp = "0.9.0-rc.1" bitcoin = { version = "0.29.2", features = ["rand"] } diff --git a/rpc/src/messages.rs b/rpc/src/messages.rs index 44a2127..0e44cd9 100644 --- a/rpc/src/messages.rs +++ b/rpc/src/messages.rs @@ -19,7 +19,7 @@ use std::str::FromStr; use std::time::Duration; use amplify::{Slice32, ToYamlString, Wrapper}; -use bitcoin::Address; +use bitcoin_scripts::address::AddressCompat; use internet2::addr::{InetSocketAddr, NodeAddr, NodeId}; use lightning_invoice::Invoice; use lnp::addr::LnpAddr; @@ -313,9 +313,10 @@ pub struct ChannelInfo { #[display(FundsInfo::to_yaml_string)] pub struct FundsInfo { #[serde_as(as = "BTreeMap")] - pub bitcoin_funds: BTreeMap, + pub bitcoin_funds: BTreeMap, pub asset_funds: AssetsBalance, - pub next_address: Address, + #[serde_as(as = "DisplayFromStr")] + pub next_address: AddressCompat, } #[cfg_attr(feature = "serde", serde_as)] diff --git a/src/lnpd/funding.rs b/src/lnpd/funding.rs index 5ee34dd..edcbc41 100644 --- a/src/lnpd/funding.rs +++ b/src/lnpd/funding.rs @@ -21,8 +21,9 @@ use amplify::{IoError, Slice32, Wrapper}; use bitcoin::psbt::PartiallySignedTransaction; use bitcoin::secp256k1::{self, Secp256k1}; use bitcoin::util::bip32::ChildNumber; -use bitcoin::{Address, EcdsaSighashType, Network, OutPoint, Txid}; +use bitcoin::{EcdsaSighashType, Network, OutPoint, Txid}; use bitcoin_blockchain::locks::SeqNo; +use bitcoin_scripts::address::AddressCompat; use bitcoin_scripts::PubkeyScript; use electrum_client::{Client as ElectrumClient, ElectrumApi}; use lnp::channel::PsbtLnpFunding; @@ -295,14 +296,14 @@ impl FundingWallet { Ok(funds) } - pub fn next_funding_address(&self) -> Result { + pub fn next_funding_address(&self) -> Result { let descriptor = DeriveDescriptor::::derive_descriptor( &self.wallet_data.descriptor, &self.secp, &[UnhardenedIndex::zero(), self.wallet_data.last_normal_index], )?; let spk = descriptor.script_pubkey(); - let address = Address::from_script(&spk, self.network) + let address = AddressCompat::from_script(&spk.into(), self.network.into()) .expect("Incorrect scriptPubkey to represents address"); Ok(address) } diff --git a/src/lnpd/runtime.rs b/src/lnpd/runtime.rs index ca3babd..b797150 100644 --- a/src/lnpd/runtime.rs +++ b/src/lnpd/runtime.rs @@ -16,7 +16,8 @@ use std::path::PathBuf; use std::time::{Duration, SystemTime}; use amplify::{DumbDefault, Wrapper}; -use bitcoin::{Address, Txid}; +use bitcoin::Txid; +use bitcoin_scripts::address::AddressCompat; use internet2::addr::{NodeAddr, NodeId}; use lnp::addr::LnpAddr; use lnp::channel::bolt::{CommonParams, LocalKeyset, PeerParams, Policy}; @@ -594,13 +595,18 @@ impl Runtime { Ok(format!("Launched new instance of {}", handle)) } - fn available_funding(&mut self) -> Result, Error> { + fn available_funding(&mut self) -> Result, Error> { self.funding_wallet.list_funds()?.into_iter().try_fold( bmap! {}, |mut acc, f| -> Result<_, Error> { - let addr = Address::from_script(&f.script_pubkey, self.funding_wallet.network()) - .map_err(|_| funding::Error::NoAddressRepresentation)?; - *acc.entry(addr).or_insert(0) += f.amount; + let addr = match AddressCompat::from_script( + &f.script_pubkey, + self.funding_wallet.network().into(), + ) { + Some(address) => Ok(address), + _ => Err(funding::Error::NoAddressRepresentation), + }; + *acc.entry(addr?).or_insert(0) += f.amount; Ok(acc) }, )