Use the new bech32 functions

This commit is contained in:
Steven Roose 2019-08-13 08:58:38 +01:00
parent 3a93f8522c
commit 170abaa82f
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
1 changed files with 10 additions and 10 deletions

View File

@ -43,7 +43,7 @@
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use std::str::FromStr; use std::str::FromStr;
use bech32::{self, u5, FromBase32, ToBase32}; use bech32;
use bitcoin_hashes::{hash160, sha256, Hash}; use bitcoin_hashes::{hash160, sha256, Hash};
use blockdata::opcodes; use blockdata::opcodes;
@ -165,7 +165,7 @@ pub enum Payload {
/// Segwit address /// Segwit address
WitnessProgram { WitnessProgram {
/// The witness program version /// The witness program version
version: u5, version: bech32::u5,
/// The witness program /// The witness program
program: Vec<u8>, program: Vec<u8>,
}, },
@ -270,7 +270,7 @@ impl Address {
Address { Address {
network: network, network: network,
payload: Payload::WitnessProgram { payload: Payload::WitnessProgram {
version: u5::try_from_u8(0).expect("0<32"), version: bech32::u5::try_from_u8(0).expect("0<32"),
program: hash160::Hash::from_engine(hash_engine)[..].to_vec(), program: hash160::Hash::from_engine(hash_engine)[..].to_vec(),
}, },
} }
@ -297,7 +297,7 @@ impl Address {
Address { Address {
network: network, network: network,
payload: Payload::WitnessProgram { payload: Payload::WitnessProgram {
version: u5::try_from_u8(0).expect("0<32"), version: bech32::u5::try_from_u8(0).expect("0<32"),
program: sha256::Hash::hash(&script[..])[..].to_vec(), program: sha256::Hash::hash(&script[..])[..].to_vec(),
}, },
} }
@ -388,14 +388,14 @@ impl Display for Address {
version: ver, version: ver,
program: ref prog, program: ref prog,
} => { } => {
let mut b32_data = vec![ver];
b32_data.extend_from_slice(&prog.to_base32());
let hrp = match self.network { let hrp = match self.network {
Network::Bitcoin => "bc", Network::Bitcoin => "bc",
Network::Testnet => "tb", Network::Testnet => "tb",
Network::Regtest => "bcrt", Network::Regtest => "bcrt",
}; };
bech32::encode_to_fmt(fmt, &hrp, &b32_data).expect("only errors on invalid HRP") 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)
} }
} }
} }
@ -431,9 +431,9 @@ impl FromStr for Address {
} }
// Get the script version and program (converted from 5-bit to 8-bit) // Get the script version and program (converted from 5-bit to 8-bit)
let (version, program) = { let (version, program): (bech32::u5, Vec<u8>) = {
let (v, p5) = payload.split_at(1); let (v, p5) = payload.split_at(1);
(v[0], Vec::from_base32(p5)?) (v[0], bech32::FromBase32::from_base32(p5)?)
}; };
// Generic segwit checks. // Generic segwit checks.
@ -622,7 +622,7 @@ mod tests {
); );
let addr = Address { let addr = Address {
payload: Payload::WitnessProgram { payload: Payload::WitnessProgram {
version: u5::try_from_u8(version).expect("0<32"), version: bech32::u5::try_from_u8(version).expect("0<32"),
program: program, program: program,
}, },
network: Network::Bitcoin, network: Network::Bitcoin,