Add Magic::from_params

Currently `Magic` has per network consts but no way to dynamically get
the magic bytes for a network. Note also that we are currently trying to
reduce the usage of `Network` in the public API.

Add a public constructor to the `Magic` type that accepts a `Params`
parameter to determine the network to use.
This commit is contained in:
Tobin C. Harding 2024-03-12 15:00:05 +11:00
parent 499f36f972
commit 3ec5eff56e
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 6 additions and 0 deletions

View File

@ -28,6 +28,7 @@ use internals::{debug_from_display, write_err};
use io::{BufRead, Write}; use io::{BufRead, Write};
use crate::consensus::encode::{self, Decodable, Encodable}; use crate::consensus::encode::{self, Decodable, Encodable};
use crate::consensus::Params;
use crate::prelude::*; use crate::prelude::*;
use crate::Network; use crate::Network;
@ -226,6 +227,11 @@ impl Magic {
/// Get network magic bytes. /// Get network magic bytes.
pub fn to_bytes(self) -> [u8; 4] { self.0 } pub fn to_bytes(self) -> [u8; 4] { self.0 }
/// Returns the magic bytes for the network defined by `params`.
pub fn from_params(params: impl AsRef<Params>) -> Self {
params.as_ref().network.into()
}
} }
impl FromStr for Magic { impl FromStr for Magic {