Skip to content

Commit

Permalink
Address to string conversion optimized for qr codes
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Mar 22, 2021
1 parent b9d5200 commit d18554e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/util/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}
}

}

0 comments on commit d18554e

Please sign in to comment.