radix_transactions/signing/
signer.rs1use crate::internal_prelude::*;
2use radix_common::prelude::IsHash;
3
4pub enum PrivateKey {
5 Secp256k1(Secp256k1PrivateKey),
6 Ed25519(Ed25519PrivateKey),
7}
8
9impl PrivateKey {
10 pub fn public_key(&self) -> PublicKey {
11 match self {
12 PrivateKey::Secp256k1(key) => key.public_key().into(),
13 PrivateKey::Ed25519(key) => key.public_key().into(),
14 }
15 }
16}
17
18impl From<Secp256k1PrivateKey> for PrivateKey {
19 fn from(public_key: Secp256k1PrivateKey) -> Self {
20 Self::Secp256k1(public_key)
21 }
22}
23
24impl From<Ed25519PrivateKey> for PrivateKey {
25 fn from(public_key: Ed25519PrivateKey) -> Self {
26 Self::Ed25519(public_key)
27 }
28}
29
30pub trait Signer {
31 fn public_key(&self) -> PublicKey;
32 fn sign_without_public_key(&self, message_hash: &impl IsHash) -> SignatureV1;
33 fn sign_with_public_key(&self, message_hash: &impl IsHash) -> SignatureWithPublicKeyV1;
34}
35
36impl<S: Signer> Signer for &S {
37 fn public_key(&self) -> PublicKey {
38 (*self).public_key()
39 }
40
41 fn sign_without_public_key(&self, message_hash: &impl IsHash) -> SignatureV1 {
42 (*self).sign_without_public_key(message_hash)
43 }
44
45 fn sign_with_public_key(&self, message_hash: &impl IsHash) -> SignatureWithPublicKeyV1 {
46 (*self).sign_with_public_key(message_hash)
47 }
48}
49
50impl Signer for Secp256k1PrivateKey {
51 fn sign_without_public_key(&self, message_hash: &impl IsHash) -> SignatureV1 {
52 self.sign(message_hash).into()
53 }
54
55 fn sign_with_public_key(&self, message_hash: &impl IsHash) -> SignatureWithPublicKeyV1 {
56 self.sign(message_hash).into()
57 }
58
59 fn public_key(&self) -> PublicKey {
60 self.public_key().into()
61 }
62}
63
64impl Signer for Ed25519PrivateKey {
65 fn sign_without_public_key(&self, message_hash: &impl IsHash) -> SignatureV1 {
66 self.sign(message_hash).into()
67 }
68
69 fn sign_with_public_key(&self, message_hash: &impl IsHash) -> SignatureWithPublicKeyV1 {
70 (self.public_key(), self.sign(message_hash)).into()
71 }
72
73 fn public_key(&self) -> PublicKey {
74 self.public_key().into()
75 }
76}
77
78impl Signer for PrivateKey {
79 fn sign_without_public_key(&self, message_hash: &impl IsHash) -> SignatureV1 {
80 match self {
81 PrivateKey::Secp256k1(key) => key.sign_without_public_key(message_hash),
82 PrivateKey::Ed25519(key) => key.sign_without_public_key(message_hash),
83 }
84 }
85
86 fn sign_with_public_key(&self, message_hash: &impl IsHash) -> SignatureWithPublicKeyV1 {
87 match self {
88 PrivateKey::Secp256k1(key) => key.sign_with_public_key(message_hash),
89 PrivateKey::Ed25519(key) => key.sign_with_public_key(message_hash),
90 }
91 }
92
93 fn public_key(&self) -> PublicKey {
94 self.public_key()
95 }
96}