From d18554e756cea42586a782e44a4623163dc65599 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Mon, 22 Mar 2021 13:58:59 +0100 Subject: [PATCH] Address to string conversion optimized for qr codes --- src/util/address.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/util/address.rs b/src/util/address.rs index 1a1f83f7..d0192507 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -352,6 +352,15 @@ impl Address { pub fn script_pubkey(&self) -> script::Script { self.payload.script_pubkey() } + + /// Creates a string optimized to be encoded in QR codes, meaning it becomes uppercase if bech32 + pub fn to_qr_string(&self) -> String { + let address_string = self.to_string(); + match self.payload { + Payload::WitnessProgram { .. } => address_string.to_ascii_uppercase(), + _ => address_string, + } + } } impl fmt::Display for Address { @@ -743,4 +752,18 @@ mod tests { hex_script!("001454d26dddb59c7073c6a197946ea1841951fa7a74") ); } + + #[test] + fn test_qr_string() { + for el in ["132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM", "33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k"].iter() { + let addr = Address::from_str(el).unwrap(); + assert_eq!(addr.to_qr_string(), *el); + } + + for el in ["bcrt1q2nfxmhd4n3c8834pj72xagvyr9gl57n5r94fsl", "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej"].iter() { + let addr = Address::from_str(el).unwrap(); + assert_eq!(addr.to_qr_string(), el.to_ascii_uppercase()); + } + } + }