Skip to content

Commit

Permalink
Merge pull request #63 from crisdut/fix/addresscompat
Browse files Browse the repository at this point in the history
Use AddressCompat to display address
  • Loading branch information
dr-orlovsky authored Jan 17, 2023
2 parents 91f870b + 400f085 commit 35f921e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
7 changes: 4 additions & 3 deletions rpc/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -313,9 +313,10 @@ pub struct ChannelInfo {
#[display(FundsInfo::to_yaml_string)]
pub struct FundsInfo {
#[serde_as(as = "BTreeMap<DisplayFromStr, Same>")]
pub bitcoin_funds: BTreeMap<Address, u64>,
pub bitcoin_funds: BTreeMap<AddressCompat, u64>,
pub asset_funds: AssetsBalance,
pub next_address: Address,
#[serde_as(as = "DisplayFromStr")]
pub next_address: AddressCompat,
}

#[cfg_attr(feature = "serde", serde_as)]
Expand Down
7 changes: 4 additions & 3 deletions src/lnpd/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -296,14 +297,14 @@ impl FundingWallet {
Ok(funds)
}

pub fn next_funding_address(&self) -> Result<Address, Error> {
pub fn next_funding_address(&self) -> Result<AddressCompat, Error> {
let descriptor = DeriveDescriptor::<bitcoin::PublicKey>::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)
}
Expand Down
16 changes: 11 additions & 5 deletions src/lnpd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -594,13 +595,18 @@ impl Runtime {
Ok(format!("Launched new instance of {}", handle))
}

fn available_funding(&mut self) -> Result<BTreeMap<Address, u64>, Error> {
fn available_funding(&mut self) -> Result<BTreeMap<AddressCompat, u64>, 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)
},
)
Expand Down

0 comments on commit 35f921e

Please sign in to comment.