From 3ec5eff56eaceb6bbac48224d705795de68daa76 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 12 Mar 2024 15:00:05 +1100 Subject: [PATCH] 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. --- bitcoin/src/p2p/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bitcoin/src/p2p/mod.rs b/bitcoin/src/p2p/mod.rs index 3d8709fa..8a3147c7 100644 --- a/bitcoin/src/p2p/mod.rs +++ b/bitcoin/src/p2p/mod.rs @@ -28,6 +28,7 @@ use internals::{debug_from_display, write_err}; use io::{BufRead, Write}; use crate::consensus::encode::{self, Decodable, Encodable}; +use crate::consensus::Params; use crate::prelude::*; use crate::Network; @@ -226,6 +227,11 @@ impl Magic { /// Get network magic bytes. 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) -> Self { + params.as_ref().network.into() + } } impl FromStr for Magic {