11// Copyright (c) Zefchain Labs, Inc.
22// SPDX-License-Identifier: Apache-2.0
33
4- use async_trait:: async_trait;
54pub use in_mem:: InMemorySigner ;
65
76use super :: CryptoHash ;
@@ -11,8 +10,11 @@ use crate::{
1110} ;
1211
1312/// A trait for signing keys.
14- #[ async_trait]
15- pub trait Signer : Send + Sync {
13+ #[ cfg_attr( not( web) , trait_variant:: make( Send ) ) ]
14+ pub trait Signer {
15+ /// The type of errors arising from operations on this `Signer`.
16+ type Error : std:: error:: Error + Send + Sync + ' static ;
17+
1618 /// Creates a signature for the given `value` using the provided `owner`.
1719 // DEV: We sign `CryptoHash` type, rather than `&[u8]` to make sure we don't sign
1820 // things accidentally. See [`CryptoHash::new`] for how the type's name is included
@@ -21,38 +23,13 @@ pub trait Signer: Send + Sync {
2123 & self ,
2224 owner : & AccountOwner ,
2325 value : & CryptoHash ,
24- ) -> Result < AccountSignature , Box < dyn std :: error :: Error > > ;
26+ ) -> Result < AccountSignature , Self :: Error > ;
2527
2628 /// Returns the public key corresponding to the given `owner`.
27- async fn get_public_key (
28- & self ,
29- owner : & AccountOwner ,
30- ) -> Result < AccountPublicKey , Box < dyn std:: error:: Error > > ;
29+ async fn get_public_key ( & self , owner : & AccountOwner ) -> Result < AccountPublicKey , Self :: Error > ;
3130
3231 /// Returns whether the given `owner` is a known signer.
33- async fn contains_key ( & self , owner : & AccountOwner ) -> Result < bool , Box < dyn std:: error:: Error > > ;
34- }
35-
36- #[ async_trait]
37- impl Signer for Box < dyn Signer > {
38- async fn sign (
39- & self ,
40- owner : & AccountOwner ,
41- value : & CryptoHash ,
42- ) -> Result < AccountSignature , Box < dyn std:: error:: Error > > {
43- ( * * self ) . sign ( owner, value) . await
44- }
45-
46- async fn get_public_key (
47- & self ,
48- owner : & AccountOwner ,
49- ) -> Result < AccountPublicKey , Box < dyn std:: error:: Error > > {
50- ( * * self ) . get_public_key ( owner) . await
51- }
52-
53- async fn contains_key ( & self , owner : & AccountOwner ) -> Result < bool , Box < dyn std:: error:: Error > > {
54- ( * * self ) . contains_key ( owner) . await
55- }
32+ async fn contains_key ( & self , owner : & AccountOwner ) -> Result < bool , Self :: Error > ;
5633}
5734
5835/// In-memory implementation of the [`Signer`] trait.
@@ -62,7 +39,6 @@ mod in_mem {
6239 sync:: { Arc , RwLock } ,
6340 } ;
6441
65- use async_trait:: async_trait;
6642 use serde:: { Deserialize , Serialize } ;
6743
6844 #[ cfg( with_getrandom) ]
@@ -72,6 +48,12 @@ mod in_mem {
7248 identifiers:: AccountOwner ,
7349 } ;
7450
51+ #[ derive( Debug , thiserror:: Error ) ]
52+ pub enum Error {
53+ #[ error( "no key found for the given owner" ) ]
54+ NoSuchOwner ,
55+ }
56+
7557 /// In-memory signer.
7658 #[ derive( Clone ) ]
7759 pub struct InMemorySigner ( Arc < RwLock < InMemSignerInner > > ) ;
@@ -173,42 +155,39 @@ mod in_mem {
173155 }
174156 }
175157
176- #[ async_trait]
177158 impl Signer for InMemorySigner {
159+ type Error = Error ;
160+
178161 /// Creates a signature for the given `value` using the provided `owner`.
179162 async fn sign (
180163 & self ,
181164 owner : & AccountOwner ,
182165 value : & CryptoHash ,
183- ) -> Result < AccountSignature , Box < dyn std :: error :: Error > > {
166+ ) -> Result < AccountSignature , Error > {
184167 let inner = self . 0 . read ( ) . unwrap ( ) ;
185168 if let Some ( secret) = inner. keys . get ( owner) {
186169 let signature = secret. sign_prehash ( * value) ;
187170 Ok ( signature)
188171 } else {
189- Err ( "No key found for the given owner" . into ( ) )
172+ Err ( Error :: NoSuchOwner )
190173 }
191174 }
192175
193176 /// Returns the public key corresponding to the given `owner`.
194- async fn get_public_key (
195- & self ,
196- owner : & AccountOwner ,
197- ) -> Result < AccountPublicKey , Box < dyn std :: error :: Error > > {
198- let inner = self . 0 . read ( ) . unwrap ( ) ;
199- match inner . keys . get ( owner ) . map ( |s| s . public ( ) ) {
200- Some ( public ) => Ok ( public ) ,
201- None => Err ( "No key found for the given owner" . into ( ) ) ,
202- }
177+ async fn get_public_key ( & self , owner : & AccountOwner ) -> Result < AccountPublicKey , Error > {
178+ Ok ( self
179+ . 0
180+ . read ( )
181+ . unwrap ( )
182+ . keys
183+ . get ( owner )
184+ . ok_or ( Error :: NoSuchOwner ) ?
185+ . public ( ) )
203186 }
204187
205188 /// Returns whether the given `owner` is a known signer.
206- async fn contains_key (
207- & self ,
208- owner : & AccountOwner ,
209- ) -> Result < bool , Box < dyn std:: error:: Error > > {
210- let inner = self . 0 . read ( ) . unwrap ( ) ;
211- Ok ( inner. keys . contains_key ( owner) )
189+ async fn contains_key ( & self , owner : & AccountOwner ) -> Result < bool , Error > {
190+ Ok ( self . 0 . read ( ) . unwrap ( ) . keys . contains_key ( owner) )
212191 }
213192 }
214193
0 commit comments