@@ -11,10 +11,9 @@ use std::{
1111
1212use curve25519_dalek:: edwards:: CompressedEdwardsY ;
1313use ed25519_dalek:: { SigningKey , VerifyingKey } ;
14- use nested_enum_utils :: common_fields ;
14+ use n0_error :: { ensure , stack_error } ;
1515use rand_core:: CryptoRng ;
1616use serde:: { Deserialize , Serialize , de, ser} ;
17- use snafu:: { Backtrace , Snafu } ;
1817
1918/// A public key.
2019///
@@ -128,7 +127,7 @@ impl PublicKey {
128127 pub fn verify ( & self , message : & [ u8 ] , signature : & Signature ) -> Result < ( ) , SignatureError > {
129128 self . as_verifying_key ( )
130129 . verify_strict ( message, & signature. 0 )
131- . map_err ( |_| SignatureSnafu . build ( ) )
130+ . map_err ( |_| SignatureError :: new ( ) )
132131 }
133132
134133 /// Convert to a hex string limited to the first 5 bytes for a friendly string
@@ -204,26 +203,18 @@ impl Display for PublicKey {
204203}
205204
206205/// Error when deserialising a [`PublicKey`] or a [`SecretKey`].
207- #[ common_fields( {
208- backtrace: Option <Backtrace >,
209- #[ snafu( implicit) ]
210- span_trace: n0_snafu:: SpanTrace ,
211- } ) ]
212- #[ derive( Snafu , Debug ) ]
206+ #[ stack_error( derive, add_meta, from_sources, std_sources) ]
213207#[ allow( missing_docs) ]
214- #[ snafu( visibility( pub ( crate ) ) ) ]
215208pub enum KeyParsingError {
216209 /// Error when decoding.
217- #[ snafu ( transparent) ]
218- Decode { source : data_encoding:: DecodeError } ,
210+ #[ error ( transparent) ]
211+ Decode ( data_encoding:: DecodeError ) ,
219212 /// Error when decoding the public key.
220- #[ snafu( transparent) ]
221- Key {
222- source : ed25519_dalek:: SignatureError ,
223- } ,
213+ #[ error( transparent) ]
214+ Key ( ed25519_dalek:: SignatureError ) ,
224215 /// The encoded information had the wrong length.
225- #[ snafu ( display ( "invalid length" ) ) ]
226- DecodeInvalidLength { } ,
216+ #[ error ( "invalid length" ) ]
217+ DecodeInvalidLength ,
227218}
228219
229220/// Deserialises the [`PublicKey`] from it's base32 encoding.
@@ -420,9 +411,9 @@ impl Signature {
420411}
421412
422413/// Verification of a signature failed.
423- #[ derive ( Debug , Snafu ) ]
424- #[ snafu ( display ( "Invalid signature" ) ) ]
425- pub struct SignatureError ;
414+ #[ stack_error ( derive , add_meta ) ]
415+ #[ error ( "Invalid signature" ) ]
416+ pub struct SignatureError { }
426417
427418fn decode_base32_hex ( s : & str ) -> Result < [ u8 ; 32 ] , KeyParsingError > {
428419 let mut bytes = [ 0u8 ; 32 ] ;
@@ -433,16 +424,18 @@ fn decode_base32_hex(s: &str) -> Result<[u8; 32], KeyParsingError> {
433424 } else {
434425 let input = s. to_ascii_uppercase ( ) ;
435426 let input = input. as_bytes ( ) ;
436- if data_encoding:: BASE32_NOPAD . decode_len ( input. len ( ) ) ? != bytes. len ( ) {
437- return Err ( DecodeInvalidLengthSnafu . build ( ) ) ;
438- }
427+ ensure ! (
428+ data_encoding:: BASE32_NOPAD . decode_len( input. len( ) ) ? == bytes. len( ) ,
429+ KeyParsingError :: DecodeInvalidLength
430+ ) ;
439431 data_encoding:: BASE32_NOPAD . decode_mut ( input, & mut bytes)
440432 } ;
441433 match res {
442434 Ok ( len) => {
443- if len != PublicKey :: LENGTH {
444- return Err ( DecodeInvalidLengthSnafu . build ( ) ) ;
445- }
435+ ensure ! (
436+ len == PublicKey :: LENGTH ,
437+ KeyParsingError :: DecodeInvalidLength
438+ ) ;
446439 }
447440 Err ( partial) => return Err ( partial. error . into ( ) ) ,
448441 }
0 commit comments