1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
use std::fmt;
use hedera_proto::services;
use crate::{
PublicKey,
ToProtobuf,
};
#[derive(Debug)]
pub struct SignaturePair {
pub(crate) signature: Signature,
pub(crate) public: PublicKey,
}
pub struct Signature(SignatureData);
impl Signature {
pub(crate) fn ed25519(signature: ed25519_dalek::Signature) -> Self {
Self(SignatureData::Ed25519(signature))
}
pub(crate) fn ecdsa(signature: k256::ecdsa::Signature) -> Self {
Self(SignatureData::Ecdsa(signature))
}
pub(crate) fn as_ed25519(&self) -> Option<&ed25519_dalek::Signature> {
if let SignatureData::Ed25519(v) = &self.0 {
Some(v)
} else {
None
}
}
pub(crate) fn as_ecdsa(&self) -> Option<&k256::ecdsa::Signature> {
if let SignatureData::Ecdsa(v) = &self.0 {
Some(v)
} else {
None
}
}
}
impl fmt::Debug for Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
SignatureData::Ed25519(signature) => {
write!(f, "Signature::Ed25519({})", hex::encode(signature.to_bytes()))
}
SignatureData::Ecdsa(signature) => {
write!(f, "Signature::Ecdsa({})", hex::encode(signature.to_vec()))
}
}
}
}
#[derive(Debug)]
enum SignatureData {
Ed25519(ed25519_dalek::Signature),
Ecdsa(k256::ecdsa::Signature),
}
impl SignaturePair {
pub(crate) fn ed25519(signature: ed25519_dalek::Signature, public: PublicKey) -> Self {
Self { public, signature: Signature::ed25519(signature) }
}
pub(crate) fn ecdsa(signature: k256::ecdsa::Signature, public: PublicKey) -> Self {
Self { public, signature: Signature::ecdsa(signature) }
}
}
impl ToProtobuf for SignaturePair {
type Protobuf = services::SignaturePair;
fn to_protobuf(&self) -> Self::Protobuf {
let signature = match self.signature.0 {
SignatureData::Ed25519(signature) => {
services::signature_pair::Signature::Ed25519(signature.to_bytes().to_vec())
}
SignatureData::Ecdsa(signature) => {
services::signature_pair::Signature::EcdsaSecp256k1(signature.to_vec())
}
};
services::SignaturePair {
signature: Some(signature),
pub_key_prefix: self.public.to_bytes_raw(),
}
}
}