Skip to content

Commit

Permalink
Update network magic bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Sep 10, 2019
1 parent cc3de1a commit 433f3db
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/network/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! let network = Network::Bitcoin;
//! let bytes = serialize(&network.magic());
//!
//! assert_eq!(&bytes[..], &[0xF9, 0xBE, 0xB4, 0xD9]);
//! assert_eq!(&bytes[..], &[0x01, 0xFF, 0xF0, 0x00]);
//! ```
/// Version of the protocol as appearing in network message headers
Expand Down Expand Up @@ -65,14 +65,14 @@ impl Network {
/// ```rust
/// use bitcoin::network::constants::Network;
///
/// assert_eq!(Some(Network::Bitcoin), Network::from_magic(0xD9B4BEF9));
/// assert_eq!(Some(Network::Bitcoin), Network::from_magic(0x00F0FF01));
/// assert_eq!(None, Network::from_magic(0xFFFFFFFF));
/// ```
pub fn from_magic(magic: u32) -> Option<Network> {
// Note: any new entries here must be added to `magic` below
match magic {
0xD9B4BEF9 => Some(Network::Bitcoin),
0x0709110B => Some(Network::Testnet),
0x00F0FF01 => Some(Network::Bitcoin),
0x74839A75 => Some(Network::Testnet),
0xDAB5BFFA => Some(Network::Regtest),
_ => None
}
Expand All @@ -87,13 +87,13 @@ impl Network {
/// use bitcoin::network::constants::Network;
///
/// let network = Network::Bitcoin;
/// assert_eq!(network.magic(), 0xD9B4BEF9);
/// assert_eq!(network.magic(), 0x00F0FF01);
/// ```
pub fn magic(&self) -> u32 {
// Note: any new entries here must be added to `from_magic` above
match *self {
Network::Bitcoin => 0xD9B4BEF9,
Network::Testnet => 0x0709110B,
Network::Bitcoin => 0x00F0FF01,
Network::Testnet => 0x74839A75,
Network::Regtest => 0xDAB5BFFA,
}
}
Expand All @@ -108,23 +108,23 @@ mod tests {
fn serialize_test() {
assert_eq!(
serialize(&Network::Bitcoin.magic()),
&[0xf9, 0xbe, 0xb4, 0xd9]
&[0x01, 0xff, 0xf0, 0x00]
);
assert_eq!(
serialize(&Network::Testnet.magic()),
&[0x0b, 0x11, 0x09, 0x07]
&[0x75, 0x9a, 0x83, 0x74]
);
assert_eq!(
serialize(&Network::Regtest.magic()),
&[0xfa, 0xbf, 0xb5, 0xda]
);

assert_eq!(
deserialize(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(),
deserialize(&[0x01, 0xff, 0xf0, 0x00]).ok(),
Some(Network::Bitcoin.magic())
);
assert_eq!(
deserialize(&[0x0b, 0x11, 0x09, 0x07]).ok(),
deserialize(&[0x75, 0x9a, 0x83, 0x74]).ok(),
Some(Network::Testnet.magic())
);
assert_eq!(
Expand Down

0 comments on commit 433f3db

Please sign in to comment.