implements alternate formatting for address

This commit is contained in:
Riccardo Casatta 2021-04-07 15:49:58 +02:00
parent cac3f460a2
commit 104836a042
No known key found for this signature in database
GPG Key ID: FD986A969E450397
1 changed files with 15 additions and 13 deletions

View File

@ -359,11 +359,7 @@ impl Address {
/// Even inside Bitcoin URI may be more efficient to use the uppercase address since in QR codes /// Even inside Bitcoin URI may be more efficient to use the uppercase address since in QR codes
/// encoding modes can be mixed as needed within a QR symbol. /// encoding modes can be mixed as needed within a QR symbol.
pub fn to_qr_string(&self) -> String { pub fn to_qr_string(&self) -> String {
let address_string = self.to_string(); format!("{:#}", self)
match self.payload {
Payload::WitnessProgram { .. } => address_string.to_ascii_uppercase(),
_ => address_string,
}
} }
} }
@ -392,14 +388,20 @@ impl fmt::Display for Address {
version: ver, version: ver,
program: ref prog, program: ref prog,
} => { } => {
let hrp = match self.network { if fmt.alternate() {
Network::Bitcoin => "bc", //TODO format without allocation when alternate uppercase is in bech32
Network::Testnet | Network::Signet => "tb", let lower = self.to_string();
Network::Regtest => "bcrt", write!(fmt, "{}", lower.to_ascii_uppercase())
}; } else {
let mut bech32_writer = bech32::Bech32Writer::new(hrp, fmt)?; let hrp = match self.network {
bech32::WriteBase32::write_u5(&mut bech32_writer, ver)?; Network::Bitcoin => "bc",
bech32::ToBase32::write_base32(&prog, &mut bech32_writer) Network::Testnet | Network::Signet => "tb",
Network::Regtest => "bcrt",
};
let mut bech32_writer = bech32::Bech32Writer::new(hrp, fmt)?;
bech32::WriteBase32::write_u5(&mut bech32_writer, ver)?;
bech32::ToBase32::write_base32(&prog, &mut bech32_writer)
}
} }
} }
} }