commands: derive serde::Serialize for results

This commit is contained in:
Antoine Poinsot 2022-08-05 18:26:05 +02:00
parent 86c1d32662
commit dd1b353a36
No known key found for this signature in database
GPG Key ID: E13FC145CD3F4304
2 changed files with 16 additions and 8 deletions

View File

@ -9,6 +9,7 @@ use miniscript::{
descriptor::{self, DescriptorTrait},
TranslatePk2,
};
use serde::{Deserialize, Serialize};
impl DaemonControl {
/// Get information about the current state of the daemon
@ -26,29 +27,31 @@ impl DaemonControl {
/// Get a new deposit address. This will always generate a new deposit address, regardless of
/// whether it was actually used.
pub fn get_new_address(&self) -> bitcoin::Address {
pub fn get_new_address(&self) -> GetAddressResult {
let mut db_conn = self.db.connection();
let index = db_conn.derivation_index();
// TODO: handle should we wrap around instead of failing?
db_conn.update_derivation_index(index.increment().expect("TODO: handle wraparound"));
self.config
let address = self
.config
.main_descriptor
// TODO: have a descriptor newtype along with a derived descriptor one.
.derive(index.into())
.translate_pk2(|xpk| xpk.derive_public_key(&self.secp))
.expect("All pubkeys were derived, no wildcard.")
.address(self.config.bitcoind_config.network)
.expect("It's a wsh() descriptor")
.expect("It's a wsh() descriptor");
GetAddressResult { address }
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub struct GetInfoDescriptors {
pub main: descriptor::Descriptor<descriptor::DescriptorPublicKey>,
}
/// Information about the daemon
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub struct GetInfoResult {
pub version: String,
pub network: bitcoin::Network,
@ -56,3 +59,8 @@ pub struct GetInfoResult {
pub sync: f64,
pub descriptors: GetInfoDescriptors,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetAddressResult {
pub address: bitcoin::Address,
}

View File

@ -483,8 +483,8 @@ mod tests {
// TODO: avoid scope creep. We should move the bitcoind-specific checks to the
// bitcoind module, test the startup with a mocked bitcoind interface, and not test
// commands here but in the commands module.
let addr = handle.control.get_new_address();
let addr2 = handle.control.get_new_address();
let addr = handle.control.get_new_address().address;
let addr2 = handle.control.get_new_address().address;
assert_eq!(
addr,
bitcoin::Address::from_str(
@ -511,7 +511,7 @@ mod tests {
let daemon_thread = thread::spawn(move || {
let handle = DaemonHandle::start(config).unwrap();
// TODO: avoid scope creep. See above comment.
assert_ne!(handle.control.get_new_address(), addr);
assert_ne!(handle.control.get_new_address().address, addr);
handle.shutdown();
});
complete_sanity_check(&server);