From 6c1f32d217a181ef155d0dddf8bf39bff2144685 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 13 Jan 2023 23:40:54 -0800 Subject: [PATCH] Make `ord wallet inscriptions` output JSON --- src/sat_point.rs | 9 +++++++ src/subcommand/wallet/inscriptions.rs | 19 ++++++++++++--- tests/wallet/inscriptions.rs | 34 +++++++++++++++++++-------- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/sat_point.rs b/src/sat_point.rs index 01a0ab8739..e6fa110e35 100644 --- a/src/sat_point.rs +++ b/src/sat_point.rs @@ -30,6 +30,15 @@ impl Decodable for SatPoint { } } +impl Serialize for SatPoint { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.collect_str(self) + } +} + impl FromStr for SatPoint { type Err = Error; diff --git a/src/subcommand/wallet/inscriptions.rs b/src/subcommand/wallet/inscriptions.rs index 5e47cfd4b5..9f3f260580 100644 --- a/src/subcommand/wallet/inscriptions.rs +++ b/src/subcommand/wallet/inscriptions.rs @@ -1,5 +1,11 @@ use super::*; +#[derive(Serialize)] +struct Output { + inscription: InscriptionId, + location: SatPoint, +} + pub(crate) fn run(options: Options) -> Result { let index = Index::open(&options)?; index.update()?; @@ -7,11 +13,18 @@ pub(crate) fn run(options: Options) -> Result { let inscriptions = index.get_inscriptions(None)?; let unspent_outputs = get_unspent_outputs(&options)?; - for (satpoint, inscription_id) in inscriptions { - if unspent_outputs.contains_key(&satpoint.outpoint) { - println!("{}\t{}", inscription_id, satpoint); + let mut output = Vec::new(); + + for (location, inscription) in inscriptions { + if unspent_outputs.contains_key(&location.outpoint) { + output.push(Output { + location, + inscription, + }); } } + serde_json::to_writer_pretty(io::stdout(), &output)?; + Ok(()) } diff --git a/tests/wallet/inscriptions.rs b/tests/wallet/inscriptions.rs index 3874669e3b..8f3f55b989 100644 --- a/tests/wallet/inscriptions.rs +++ b/tests/wallet/inscriptions.rs @@ -1,5 +1,11 @@ use super::*; +#[derive(Deserialize)] +struct Inscription { + inscription: String, + location: String, +} + #[test] fn inscriptions() { let rpc_server = test_bitcoincore_rpc::spawn(); @@ -12,10 +18,14 @@ fn inscriptions() { .. } = inscribe(&rpc_server); - CommandBuilder::new("wallet inscriptions") + let inscriptions = CommandBuilder::new("wallet inscriptions") .rpc_server(&rpc_server) .expected_stdout(format!("{inscription}\t{reveal}:0:0\n")) - .run(); + .output::>(); + + assert_eq!(inscriptions.len(), 1); + assert_eq!(inscriptions[0].inscription, inscription); + assert_eq!(inscriptions[0].location, format!("{reveal}:0:0")); let stdout = CommandBuilder::new("wallet receive") .rpc_server(&rpc_server) @@ -35,12 +45,13 @@ fn inscriptions() { let txid = Txid::from_str(stdout.trim()).unwrap(); - let outpoint = OutPoint::new(txid, 0); - - CommandBuilder::new("wallet inscriptions") + let inscriptions = CommandBuilder::new("wallet inscriptions") .rpc_server(&rpc_server) - .expected_stdout(format!("{inscription}\t{outpoint}:0\n")) - .run(); + .output::>(); + + assert_eq!(inscriptions.len(), 1); + assert_eq!(inscriptions[0].inscription, inscription); + assert_eq!(inscriptions[0].location, format!("{txid}:0:0")); } #[test] @@ -63,8 +74,11 @@ fn inscriptions_includes_locked_utxos() { vout: 0, }); - CommandBuilder::new("wallet inscriptions") + let inscriptions = CommandBuilder::new("wallet inscriptions") .rpc_server(&rpc_server) - .expected_stdout(format!("{inscription}\t{reveal}:0:0\n")) - .run(); + .output::>(); + + assert_eq!(inscriptions.len(), 1); + assert_eq!(inscriptions[0].inscription, inscription); + assert_eq!(inscriptions[0].location, format!("{reveal}:0:0")); }