Add support for bech32 regtest address format

This commit is contained in:
Franck Royer 2018-07-26 10:51:37 +10:00
parent ba2879cfac
commit 0f31608796
1 changed files with 13 additions and 4 deletions

View File

@ -166,7 +166,8 @@ impl Address {
fn bech_network (network: Network) -> bitcoin_bech32::constants::Network { fn bech_network (network: Network) -> bitcoin_bech32::constants::Network {
match network { match network {
Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin, Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin,
Network::Testnet | Network::Regtest => bitcoin_bech32::constants::Network::Testnet, Network::Testnet => bitcoin_bech32::constants::Network::Testnet,
Network::Regtest => bitcoin_bech32::constants::Network::Regtest,
} }
} }
@ -246,12 +247,14 @@ impl FromStr for Address {
fn from_str(s: &str) -> Result<Address, Error> { fn from_str(s: &str) -> Result<Address, Error> {
// bech32 (note that upper or lowercase is allowed but NOT mixed case) // bech32 (note that upper or lowercase is allowed but NOT mixed case)
if s.len() >= 3 && if s.len() >= 3 &&
(&s.as_bytes()[0..3] == b"bc1" || &s.as_bytes()[0..3] == b"tb1" || (&s.as_bytes()[0..3] == b"bc1" || &s.as_bytes()[0..3] == b"BC1" ||
&s.as_bytes()[0..3] == b"BC1" || &s.as_bytes()[0..3] == b"TB1") { &s.as_bytes()[0..3] == b"tb1" || &s.as_bytes()[0..3] == b"TB1" ||
&s.as_bytes()[0..5] == b"bcrt1" || &s.as_bytes()[0..5] == b"BCRT1") {
let witprog = try!(WitnessProgram::from_address(s)); let witprog = try!(WitnessProgram::from_address(s));
let network = match witprog.network() { let network = match witprog.network() {
bitcoin_bech32::constants::Network::Bitcoin => Network::Bitcoin, bitcoin_bech32::constants::Network::Bitcoin => Network::Bitcoin,
bitcoin_bech32::constants::Network::Testnet => Network::Testnet, bitcoin_bech32::constants::Network::Testnet => Network::Testnet,
bitcoin_bech32::constants::Network::Regtest => Network::Regtest,
_ => panic!("unknown network") _ => panic!("unknown network")
}; };
if witprog.version().to_u8() != 0 { if witprog.version().to_u8() != 0 {
@ -318,7 +321,7 @@ mod tests {
use serialize::hex::FromHex; use serialize::hex::FromHex;
use blockdata::script::Script; use blockdata::script::Script;
use network::constants::Network::{Bitcoin, Testnet}; use network::constants::Network::{Bitcoin, Testnet, Regtest};
use util::hash::Hash160; use util::hash::Hash160;
use super::*; use super::*;
@ -424,6 +427,12 @@ mod tests {
assert_eq!(addr.script_pubkey(), hex_script!("0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433")); assert_eq!(addr.script_pubkey(), hex_script!("0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433"));
assert_eq!(addr.to_string(), addrstr); assert_eq!(addr.to_string(), addrstr);
let addrstr = "bcrt1q2nfxmhd4n3c8834pj72xagvyr9gl57n5r94fsl";
let addr = Address::from_str(addrstr).unwrap();
assert_eq!(addr.network, Regtest);
assert_eq!(addr.script_pubkey(), hex_script!("001454d26dddb59c7073c6a197946ea1841951fa7a74"));
assert_eq!(addr.to_string(), addrstr);
// bad vectors // bad vectors
let addrstr = "tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty"; // invalid hrp let addrstr = "tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty"; // invalid hrp
assert!(Address::from_str(addrstr).is_err()); assert!(Address::from_str(addrstr).is_err());