From ba2879cfac7e2413bde949221a06845f028f97b5 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 26 Jul 2018 10:51:15 +1000 Subject: [PATCH 1/3] Add test for regtest network --- src/network/constants.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/network/constants.rs b/src/network/constants.rs index bdd6fac7..22387f5f 100644 --- a/src/network/constants.rs +++ b/src/network/constants.rs @@ -95,9 +95,11 @@ mod tests { fn string_test() { assert_eq!(Network::Bitcoin.to_string(), "bitcoin"); assert_eq!(Network::Testnet.to_string(), "testnet"); + assert_eq!(Network::Regtest.to_string(), "regtest"); assert_eq!("bitcoin".parse::().unwrap(), Network::Bitcoin); assert_eq!("testnet".parse::().unwrap(), Network::Testnet); + assert_eq!("regtest".parse::().unwrap(), Network::Regtest); assert!("fakenet".parse::().is_err()); } } From 0f31608796d46a2b3f5ba843cec1d0737c1f16aa Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 26 Jul 2018 10:51:37 +1000 Subject: [PATCH 2/3] Add support for bech32 regtest address format --- src/util/address.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/util/address.rs b/src/util/address.rs index 722a5ff1..6a1609b9 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -166,7 +166,8 @@ impl Address { fn bech_network (network: Network) -> bitcoin_bech32::constants::Network { match network { 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 { // bech32 (note that upper or lowercase is allowed but NOT mixed case) 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"TB1") { + (&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..5] == b"bcrt1" || &s.as_bytes()[0..5] == b"BCRT1") { let witprog = try!(WitnessProgram::from_address(s)); let network = match witprog.network() { bitcoin_bech32::constants::Network::Bitcoin => Network::Bitcoin, bitcoin_bech32::constants::Network::Testnet => Network::Testnet, + bitcoin_bech32::constants::Network::Regtest => Network::Regtest, _ => panic!("unknown network") }; if witprog.version().to_u8() != 0 { @@ -318,7 +321,7 @@ mod tests { use serialize::hex::FromHex; use blockdata::script::Script; - use network::constants::Network::{Bitcoin, Testnet}; + use network::constants::Network::{Bitcoin, Testnet, Regtest}; use util::hash::Hash160; use super::*; @@ -424,6 +427,12 @@ mod tests { assert_eq!(addr.script_pubkey(), hex_script!("0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433")); 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 let addrstr = "tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty"; // invalid hrp assert!(Address::from_str(addrstr).is_err()); From f05f83156954ee0021701b2244f7cb82bbd2bf6a Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 26 Jul 2018 17:06:35 +1000 Subject: [PATCH 3/3] Regtest has a longer hrp, need to adjust length safety check --- src/util/address.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/util/address.rs b/src/util/address.rs index 6a1609b9..69319e45 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -246,11 +246,13 @@ impl FromStr for Address { fn from_str(s: &str) -> Result { // 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"BC1" || - &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)); + &s.as_bytes()[0..3] == b"tb1" || &s.as_bytes()[0..3] == b"TB1" )) || + (s.len() >= 5 && + (&s.as_bytes()[0..5] == b"bcrt1" || &s.as_bytes()[0..5] == b"BCRT1")) + { + let witprog = WitnessProgram::from_address(s)?; let network = match witprog.network() { bitcoin_bech32::constants::Network::Bitcoin => Network::Bitcoin, bitcoin_bech32::constants::Network::Testnet => Network::Testnet,