Struct cardano_serialization_lib::chain_crypto::Signature
source · pub struct Signature<T: ?Sized, A: VerificationAlgorithm> { /* private fields */ }
Implementations§
source§impl<A: VerificationAlgorithm, T> Signature<T, A>
impl<A: VerificationAlgorithm, T> Signature<T, A>
sourcepub fn from_binary(sig: &[u8]) -> Result<Self, SignatureError>
pub fn from_binary(sig: &[u8]) -> Result<Self, SignatureError>
Examples found in repository?
src/chain_crypto/sign.rs (line 76)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
fn from_str(hex: &str) -> Result<Self, Self::Err> {
let bytes = hex::decode(hex).map_err(SignatureFromStrError::HexMalformed)?;
Self::from_binary(&bytes).map_err(SignatureFromStrError::Invalid)
}
}
impl fmt::Display for SignatureError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SignatureError::SizeInvalid { expected, got } => write!(
f,
"Invalid Signature size expecting {} got {}",
expected, got
),
SignatureError::StructureInvalid => write!(f, "Invalid Signature structure"),
}
}
}
impl fmt::Display for SignatureFromStrError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SignatureFromStrError::HexMalformed(_) => "hex encoding malformed",
SignatureFromStrError::Invalid(_) => "invalid signature data",
}
.fmt(f)
}
}
impl std::error::Error for SignatureError {}
impl std::error::Error for SignatureFromStrError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
SignatureFromStrError::HexMalformed(e) => Some(e),
SignatureFromStrError::Invalid(e) => Some(e),
}
}
}
impl<A: VerificationAlgorithm, T> Signature<T, A> {
pub fn from_binary(sig: &[u8]) -> Result<Self, SignatureError> {
Ok(Signature {
signdata: A::signature_from_bytes(sig)?,
phantom: PhantomData,
})
}
pub fn coerce<U>(self) -> Signature<U, A> {
Signature {
signdata: self.signdata,
phantom: PhantomData,
}
}
pub fn safe_coerce<U: SafeSignatureCoerce<T>>(self) -> Signature<U, A> {
Signature {
signdata: self.signdata,
phantom: PhantomData,
}
}
}
pub trait SafeSignatureCoerce<T> {}
impl<'a, T> SafeSignatureCoerce<ByteArray<T>> for ByteSlice<'a, T> {}
impl<A: VerificationAlgorithm, T: AsRef<[u8]>> Signature<T, A> {
#[must_use]
pub fn verify(&self, publickey: &key::PublicKey<A>, object: &T) -> Verification {
<A as VerificationAlgorithm>::verify_bytes(&publickey.0, &self.signdata, object.as_ref())
}
}
impl<A: VerificationAlgorithm, T: ?Sized> Signature<T, A> {
#[must_use]
pub fn verify_slice(&self, publickey: &key::PublicKey<A>, slice: &[u8]) -> Verification {
<A as VerificationAlgorithm>::verify_bytes(&publickey.0, &self.signdata, slice)
}
}
/*
impl<A: SigningAlgorithm, T: AsRef<[u8]>> Signature<T, A::Public>
where <A as key::AsymmetricKey>::Public: VerificationAlgorithm,
{
pub fn generate(secretkey: &key::SecretKey<A>, object: &T) -> Signature<T, A::Public> {
Signature {
signdata: <A as SigningAlgorithm>::sign(&secretkey.0, object.as_ref()),
phantom: PhantomData,
}
}
}
*/
impl<A: SigningAlgorithm> key::SecretKey<A>
where
<A as key::AsymmetricKey>::PubAlg: VerificationAlgorithm,
{
pub fn sign<T: AsRef<[u8]>>(&self, object: &T) -> Signature<T, A::PubAlg> {
Signature {
signdata: <A as SigningAlgorithm>::sign(&self.0, object.as_ref()),
phantom: PhantomData,
}
}
pub fn sign_slice<T: ?Sized>(&self, slice: &[u8]) -> Signature<T, A::PubAlg> {
Signature {
signdata: <A as SigningAlgorithm>::sign(&self.0, slice),
phantom: PhantomData,
}
}
}
impl<T, A: VerificationAlgorithm> Clone for Signature<T, A> {
fn clone(&self) -> Self {
Signature {
signdata: self.signdata.clone(),
phantom: std::marker::PhantomData,
}
}
}
impl<T, A: VerificationAlgorithm> PartialEq<Self> for Signature<T, A> {
fn eq(&self, other: &Self) -> bool {
self.signdata.as_ref() == other.signdata.as_ref() && self.phantom == other.phantom
}
}
impl<T, A: VerificationAlgorithm> Eq for Signature<T, A> {}
impl<T: ?Sized, A: VerificationAlgorithm> AsRef<[u8]> for Signature<T, A> {
fn as_ref(&self) -> &[u8] {
self.signdata.as_ref()
}
}
impl<T, A: VerificationAlgorithm> Bech32 for Signature<T, A> {
const BECH32_HRP: &'static str = A::SIGNATURE_BECH32_HRP;
fn try_from_bech32_str(bech32_str: &str) -> Result<Self, bech32::Error> {
let bytes = bech32::try_from_bech32_to_bytes::<Self>(bech32_str)?;
Self::from_binary(&bytes).map_err(bech32::Error::data_invalid)
}
More examples
sourcepub fn coerce<U>(self) -> Signature<U, A>
pub fn coerce<U>(self) -> Signature<U, A>
Examples found in repository?
src/impl_mockchain/key.rs (line 118)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
pub fn make_signature<T, A>(
spending_key: &crypto::SecretKey<A>,
data: &T,
) -> crypto::Signature<T, A::PubAlg>
where
A: SigningAlgorithm,
<A as AsymmetricKey>::PubAlg: VerificationAlgorithm,
T: property::Serialize,
{
let bytes = data.serialize_as_vec().unwrap();
spending_key.sign(&bytes).coerce()
}
pub fn verify_signature<T, A>(
signature: &crypto::Signature<T, A>,
public_key: &crypto::PublicKey<A>,
data: &T,
) -> crypto::Verification
where
A: VerificationAlgorithm,
T: property::Serialize,
{
let bytes = data.serialize_as_vec().unwrap();
signature.clone().coerce().verify(public_key, &bytes)
}
/// A serializable type T with a signature.
pub struct Signed<T, A: VerificationAlgorithm> {
pub data: T,
pub sig: crypto::Signature<T, A>,
}
pub fn signed_new<T: property::Serialize, A: SigningAlgorithm>(
secret_key: &crypto::SecretKey<A>,
data: T,
) -> Signed<T, A::PubAlg>
where
A::PubAlg: VerificationAlgorithm,
{
let bytes = data.serialize_as_vec().unwrap();
let signature = secret_key.sign(&bytes).coerce();
Signed {
data: data,
sig: signature,
}
}
pub fn safe_coerce<U: SafeSignatureCoerce<T>>(self) -> Signature<U, A>
source§impl<A: VerificationAlgorithm, T: AsRef<[u8]>> Signature<T, A>
impl<A: VerificationAlgorithm, T: AsRef<[u8]>> Signature<T, A>
sourcepub fn verify(&self, publickey: &PublicKey<A>, object: &T) -> Verification
pub fn verify(&self, publickey: &PublicKey<A>, object: &T) -> Verification
Examples found in repository?
src/impl_mockchain/key.rs (line 131)
121 122 123 124 125 126 127 128 129 130 131 132
pub fn verify_signature<T, A>(
signature: &crypto::Signature<T, A>,
public_key: &crypto::PublicKey<A>,
data: &T,
) -> crypto::Verification
where
A: VerificationAlgorithm,
T: property::Serialize,
{
let bytes = data.serialize_as_vec().unwrap();
signature.clone().coerce().verify(public_key, &bytes)
}
source§impl<A: VerificationAlgorithm, T: ?Sized> Signature<T, A>
impl<A: VerificationAlgorithm, T: ?Sized> Signature<T, A>
sourcepub fn verify_slice(&self, publickey: &PublicKey<A>, slice: &[u8]) -> Verification
pub fn verify_slice(&self, publickey: &PublicKey<A>, slice: &[u8]) -> Verification
Trait Implementations§
source§impl<T, A: VerificationAlgorithm> Bech32 for Signature<T, A>
impl<T, A: VerificationAlgorithm> Bech32 for Signature<T, A>
const BECH32_HRP: &'static str = A::SIGNATURE_BECH32_HRP
fn try_from_bech32_str(bech32_str: &str) -> Result<Self, Error>
fn to_bech32_str(&self) -> String
source§impl<T, A: VerificationAlgorithm> Clone for Signature<T, A>
impl<T, A: VerificationAlgorithm> Clone for Signature<T, A>
source§impl<A: VerificationAlgorithm, T> Debug for Signature<T, A>
impl<A: VerificationAlgorithm, T> Debug for Signature<T, A>
source§impl<A: VerificationAlgorithm, T> Display for Signature<T, A>
impl<A: VerificationAlgorithm, T> Display for Signature<T, A>
source§impl<T, A: VerificationAlgorithm> FromStr for Signature<T, A>
impl<T, A: VerificationAlgorithm> FromStr for Signature<T, A>
source§impl<T, A: VerificationAlgorithm> PartialEq<Signature<T, A>> for Signature<T, A>
impl<T, A: VerificationAlgorithm> PartialEq<Signature<T, A>> for Signature<T, A>
impl<T, A: VerificationAlgorithm> Eq for Signature<T, A>
Auto Trait Implementations§
impl<T: ?Sized, A> RefUnwindSafe for Signature<T, A>where
T: RefUnwindSafe,
<A as VerificationAlgorithm>::Signature: RefUnwindSafe,
impl<T: ?Sized, A> Send for Signature<T, A>where
T: Send,
<A as VerificationAlgorithm>::Signature: Send,
impl<T: ?Sized, A> Sync for Signature<T, A>where
T: Sync,
<A as VerificationAlgorithm>::Signature: Sync,
impl<T: ?Sized, A> Unpin for Signature<T, A>where
T: Unpin,
<A as VerificationAlgorithm>::Signature: Unpin,
impl<T: ?Sized, A> UnwindSafe for Signature<T, A>where
T: UnwindSafe,
<A as VerificationAlgorithm>::Signature: UnwindSafe,
Blanket Implementations§
source§impl<T> Base32Len for Twhere
T: AsRef<[u8]>,
impl<T> Base32Len for Twhere
T: AsRef<[u8]>,
source§fn base32_len(&self) -> usize
fn base32_len(&self) -> usize
Calculate the base32 serialized length
source§impl<T> ToBase32 for Twhere
T: AsRef<[u8]>,
impl<T> ToBase32 for Twhere
T: AsRef<[u8]>,
source§fn write_base32<W>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err>where
W: WriteBase32,
fn write_base32<W>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err>where
W: WriteBase32,
Encode as base32 and write it to the supplied writer
Implementations shouldn’t allocate.
source§impl<T> ToHex for Twhere
T: AsRef<[u8]>,
impl<T> ToHex for Twhere
T: AsRef<[u8]>,
source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
Encode the hex strict representing
self
into the result. Lower case
letters are used (e.g. f9b4ca
)source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
Encode the hex strict representing
self
into the result. Upper case
letters are used (e.g. F9B4CA
)