@@ -6,23 +6,29 @@ use starknet_core::{
6
6
} ;
7
7
use starknet_crypto:: get_public_key;
8
8
9
+ /// A ECDSA signing (private) key on the STARK curve.
9
10
#[ derive( Debug , Clone ) ]
10
11
pub struct SigningKey {
11
12
secret_scalar : Felt ,
12
13
}
13
14
15
+ /// A ECDSA verifying (public) key on the STARK curve.
14
16
#[ derive( Debug , Clone ) ]
15
17
pub struct VerifyingKey {
16
18
scalar : Felt ,
17
19
}
18
20
21
+ /// Errors using an encrypted JSON keystore.
19
22
#[ cfg( not( target_arch = "wasm32" ) ) ]
20
23
#[ derive( Debug , thiserror:: Error ) ]
21
24
pub enum KeystoreError {
25
+ /// The file path is invalid.
22
26
#[ error( "invalid path" ) ]
23
27
InvalidPath ,
28
+ /// The decrypted secret scalar is not a valid private key.
24
29
#[ error( "invalid decrypted secret scalar" ) ]
25
30
InvalidScalar ,
31
+ /// Upstream `eth-keystore` error propagated.
26
32
#[ error( transparent) ]
27
33
Inner ( eth_keystore:: KeystoreError ) ,
28
34
}
@@ -47,6 +53,7 @@ impl SigningKey {
47
53
Self { secret_scalar }
48
54
}
49
55
56
+ /// Constructs [`SigningKey`] directly from a secret scalar.
50
57
pub const fn from_secret_scalar ( secret_scalar : Felt ) -> Self {
51
58
Self { secret_scalar }
52
59
}
@@ -92,28 +99,35 @@ impl SigningKey {
92
99
Ok ( ( ) )
93
100
}
94
101
102
+ /// Gets the secret scalar in the signing key.
95
103
pub const fn secret_scalar ( & self ) -> Felt {
96
104
self . secret_scalar
97
105
}
98
106
107
+ /// Derives the verifying (public) key that corresponds to the signing key.
99
108
pub fn verifying_key ( & self ) -> VerifyingKey {
100
109
VerifyingKey :: from_scalar ( get_public_key ( & self . secret_scalar ) )
101
110
}
102
111
112
+ /// Signs a raw hash using ECDSA for a signature.
103
113
pub fn sign ( & self , hash : & Felt ) -> Result < Signature , EcdsaSignError > {
104
114
ecdsa_sign ( & self . secret_scalar , hash) . map ( |sig| sig. into ( ) )
105
115
}
106
116
}
107
117
108
118
impl VerifyingKey {
119
+ /// Constructs [`VerifyingKey`] directly from a scalar.
109
120
pub const fn from_scalar ( scalar : Felt ) -> Self {
110
121
Self { scalar }
111
122
}
112
123
124
+ /// Gets the scalar in the verifying key.
113
125
pub const fn scalar ( & self ) -> Felt {
114
126
self . scalar
115
127
}
116
128
129
+ /// Verifies that an ECDSA signature is valid for the verifying key against a certain message
130
+ /// hash.
117
131
pub fn verify ( & self , hash : & Felt , signature : & Signature ) -> Result < bool , EcdsaVerifyError > {
118
132
ecdsa_verify ( & self . scalar , hash, signature)
119
133
}
0 commit comments