forked from tezedge/tezedge
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto: align Signature with octez crypto impl
- Loading branch information
Showing
7 changed files
with
150 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// SPDX-FileCopyrightText: 2023 TriliTech <[email protected]> | ||
// Copyright (c) SimpleStaking, Viable Systems and Tezedge Contributors | ||
// | ||
// Ported from octez: lib_crypto/signature_v1.ml | ||
// | ||
// Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <[email protected]> | ||
// Copyright (c) 2020 Metastate AG <[email protected]> | ||
// Copyright (c) 2022 Nomadic Labs <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
use crate::base58::FromBase58CheckError; | ||
use crate::hash::{ | ||
BlsSignature, Ed25519Signature, FromBytesError, HashTrait, HashType, P256Signature, | ||
Secp256k1Signature, UnknownSignature, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] | ||
#[serde(untagged)] | ||
pub enum Signature { | ||
Ed25519(Ed25519Signature), | ||
Secp256k1(Secp256k1Signature), | ||
P256(P256Signature), | ||
Bls(BlsSignature), | ||
Unknown(UnknownSignature), | ||
} | ||
|
||
impl Signature { | ||
#[doc(hidden)] | ||
pub fn from_vec(hash: Vec<u8>) -> Result<Self, FromBytesError> { | ||
if hash.len() == HashType::BlsSignature.size() { | ||
Ok(Signature::Bls(BlsSignature(hash))) | ||
} else if hash.len() == HashType::UnknownSignature.size() { | ||
Ok(Signature::Unknown(UnknownSignature(hash))) | ||
} else { | ||
Err(FromBytesError::InvalidSize) | ||
} | ||
} | ||
|
||
pub fn from_base58_check(data: &str) -> Result<Self, FromBase58CheckError> { | ||
if data.starts_with("edsig") { | ||
Ok(Signature::Ed25519(Ed25519Signature::from_b58check(data)?)) | ||
} else if data.starts_with("spsig1") { | ||
Ok(Signature::Secp256k1(Secp256k1Signature::from_b58check( | ||
data, | ||
)?)) | ||
} else if data.starts_with("p2sig") { | ||
Ok(Signature::P256(P256Signature::from_b58check(data)?)) | ||
} else if data.starts_with("BLsig") { | ||
Ok(Signature::Bls(BlsSignature::from_b58check(data)?)) | ||
} else { | ||
Ok(Signature::Unknown(UnknownSignature::from_b58check(data)?)) | ||
} | ||
} | ||
|
||
pub fn to_base58_check(&self) -> String { | ||
match self { | ||
Self::Ed25519(s) => s.to_b58check(), | ||
Self::Secp256k1(s) => s.to_b58check(), | ||
Self::P256(s) => s.to_b58check(), | ||
Self::Bls(s) => s.to_b58check(), | ||
Self::Unknown(s) => s.to_b58check(), | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for Signature { | ||
fn as_ref(&self) -> &[u8] { | ||
match self { | ||
Self::Ed25519(s) => s.0.as_ref(), | ||
Self::Secp256k1(s) => s.0.as_ref(), | ||
Self::P256(s) => s.0.as_ref(), | ||
Self::Bls(s) => s.0.as_ref(), | ||
Self::Unknown(s) => s.0.as_ref(), | ||
} | ||
} | ||
} | ||
|
||
impl ::std::fmt::Display for Signature { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
// TODO - this could be done without the need to perform a heap allocation. | ||
write!(f, "{}", self.to_base58_check()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters