Merge pull request #2942 from benthecarman/node-id-slice

Add NodeId::from_slice
This commit is contained in:
Elias Rohrer 2024-03-18 09:13:00 +00:00 committed by GitHub
commit 282b52f7bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -74,6 +74,16 @@ impl NodeId {
NodeId(pubkey.serialize()) NodeId(pubkey.serialize())
} }
/// Create a new NodeId from a slice of bytes
pub fn from_slice(bytes: &[u8]) -> Result<Self, DecodeError> {
if bytes.len() != PUBLIC_KEY_SIZE {
return Err(DecodeError::InvalidValue);
}
let mut data = [0; PUBLIC_KEY_SIZE];
data.copy_from_slice(bytes);
Ok(NodeId(data))
}
/// Get the public key slice from this NodeId /// Get the public key slice from this NodeId
pub fn as_slice(&self) -> &[u8] { pub fn as_slice(&self) -> &[u8] {
&self.0 &self.0