From dd1b353a36feb04b1af5553e9c71763957996af0 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Fri, 5 Aug 2022 18:26:05 +0200 Subject: [PATCH] commands: derive serde::Serialize for results --- src/commands/mod.rs | 18 +++++++++++++----- src/lib.rs | 6 +++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 8069e7bb..767e19ee 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -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, } /// 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, +} diff --git a/src/lib.rs b/src/lib.rs index 8910971b..9207173a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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);