Skip to content

Commit 0268fa3

Browse files
wallet: implement GetInfo endpoint (#170)
1 parent 1e75b76 commit 0268fa3

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

lib/server.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ use crate::{
4747
GetBlockInfoResponse, GetBmmHStarCommitmentRequest, GetBmmHStarCommitmentResponse,
4848
GetChainInfoRequest, GetChainInfoResponse, GetChainTipRequest, GetChainTipResponse,
4949
GetCoinbasePsbtRequest, GetCoinbasePsbtResponse, GetCtipRequest, GetCtipResponse,
50-
GetSidechainProposalsRequest, GetSidechainProposalsResponse, GetSidechainsRequest,
51-
GetSidechainsResponse, GetTwoWayPegDataRequest, GetTwoWayPegDataResponse,
50+
GetInfoRequest, GetInfoResponse, GetSidechainProposalsRequest,
51+
GetSidechainProposalsResponse, GetSidechainsRequest, GetSidechainsResponse,
52+
GetTwoWayPegDataRequest, GetTwoWayPegDataResponse,
5253
ListSidechainDepositTransactionsRequest, ListSidechainDepositTransactionsResponse,
5354
ListTransactionsRequest, ListTransactionsResponse, ListUnspentOutputsRequest,
5455
ListUnspentOutputsResponse, Network, SendTransactionRequest, SendTransactionResponse,
@@ -697,6 +698,36 @@ impl WalletService for crate::wallet::Wallet {
697698

698699
type GenerateBlocksStream = BoxStream<'static, Result<GenerateBlocksResponse, tonic::Status>>;
699700

701+
async fn get_info(
702+
&self,
703+
_request: tonic::Request<GetInfoRequest>,
704+
) -> Result<tonic::Response<GetInfoResponse>, tonic::Status> {
705+
let info = self
706+
.get_wallet_info()
707+
.await
708+
.map_err(|err| err.into_status())?;
709+
710+
let response = GetInfoResponse {
711+
network: info.network.to_string(),
712+
transaction_count: info.transaction_count as u32,
713+
unspent_output_count: info.unspent_output_count as u32,
714+
descriptors: info
715+
.keychain_descriptors
716+
.iter()
717+
.map(|(kind, descriptor)| {
718+
(
719+
match kind {
720+
bdk_wallet::KeychainKind::External => "external".to_string(),
721+
bdk_wallet::KeychainKind::Internal => "internal".to_string(),
722+
},
723+
descriptor.to_string(),
724+
)
725+
})
726+
.collect(),
727+
};
728+
Ok(tonic::Response::new(response))
729+
}
730+
700731
async fn create_sidechain_proposal(
701732
&self,
702733
request: tonic::Request<CreateSidechainProposalRequest>,

lib/wallet/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,17 @@ pub struct CreateTransactionParams {
705705
pub required_utxos: Vec<bdk_wallet::bitcoin::OutPoint>,
706706
}
707707

708+
pub struct WalletInfo {
709+
// Public (i.e. without private keys) descriptors for the wallet
710+
pub keychain_descriptors: std::collections::HashMap<
711+
bdk_wallet::KeychainKind,
712+
bdk_wallet::descriptor::ExtendedDescriptor,
713+
>,
714+
pub network: bdk_wallet::bitcoin::Network,
715+
pub transaction_count: usize,
716+
pub unspent_output_count: usize,
717+
}
718+
708719
/// Cheap to clone, since it uses Arc internally
709720
#[derive(Clone)]
710721
pub struct Wallet {
@@ -1737,6 +1748,21 @@ impl Wallet {
17371748
}
17381749
}
17391750

1751+
pub async fn get_wallet_info(&self) -> Result<WalletInfo> {
1752+
let w = self.inner.read_wallet().await?;
1753+
let mut keychain_descriptors = std::collections::HashMap::new();
1754+
for (kind, _) in w.keychains() {
1755+
keychain_descriptors.insert(kind, w.public_descriptor(kind).clone());
1756+
}
1757+
1758+
Ok(WalletInfo {
1759+
keychain_descriptors,
1760+
network: w.network(),
1761+
transaction_count: w.transactions().count(),
1762+
unspent_output_count: w.list_unspent().count(),
1763+
})
1764+
}
1765+
17401766
#[allow(clippy::significant_drop_tightening)]
17411767
pub async fn get_new_address(&self) -> Result<bdk_wallet::bitcoin::Address> {
17421768
// Using next_unused_address here means that we get a new address

0 commit comments

Comments
 (0)