1
1
// Copyright 2020-2023 IOTA Stiftung
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
+ use identity_ecdsa_verifier:: EcDSAJwsVerifier ;
5
+ use identity_eddsa_verifier:: EdDSAJwsVerifier ;
6
+ use identity_iota:: verification:: jws:: JwsAlgorithm ;
4
7
use identity_iota:: verification:: jws:: JwsVerifier ;
5
8
use identity_iota:: verification:: jws:: SignatureVerificationError ;
6
9
use identity_iota:: verification:: jws:: SignatureVerificationErrorKind ;
@@ -10,12 +13,12 @@ use wasm_bindgen::prelude::*;
10
13
use crate :: jose:: WasmJwk ;
11
14
12
15
/// Wrapper that enables custom TS JWS signature verification plugins to be used where the
13
- /// JwsVerifier trait is required. Falls back to the default implementation if a custom
14
- /// implementation was not passed.
15
- pub ( crate ) struct WasmJwsVerifier ( IJwsVerifier ) ;
16
+ /// JwsVerifier trait is required. Falls back to the default implementation capable of handling
17
+ /// EdDSA (ED25519), ES256, ES256K if a custom implementation is not passed.
18
+ pub ( crate ) struct WasmJwsVerifier ( Option < IJwsVerifier > ) ;
16
19
17
20
impl WasmJwsVerifier {
18
- pub ( crate ) fn new ( verifier : IJwsVerifier ) -> Self {
21
+ pub ( crate ) fn new ( verifier : Option < IJwsVerifier > ) -> Self {
19
22
Self ( verifier)
20
23
}
21
24
}
@@ -26,22 +29,30 @@ impl JwsVerifier for WasmJwsVerifier {
26
29
input : identity_iota:: verification:: jws:: VerificationInput ,
27
30
public_key : & identity_iota:: verification:: jwk:: Jwk ,
28
31
) -> Result < ( ) , identity_iota:: verification:: jws:: SignatureVerificationError > {
29
- let VerificationInput {
30
- alg,
31
- signing_input,
32
- decoded_signature,
33
- } = input;
34
- let verification_result = IJwsVerifier :: verify (
35
- & self . 0 ,
36
- alg. name ( ) . to_owned ( ) ,
37
- signing_input. into ( ) ,
38
- decoded_signature. into ( ) ,
39
- WasmJwk ( public_key. to_owned ( ) ) ,
40
- ) ;
41
- // Convert error
42
- crate :: error:: stringify_js_error ( verification_result) . map_err ( |error_string| {
43
- SignatureVerificationError :: new ( SignatureVerificationErrorKind :: Unspecified ) . with_custom_message ( error_string)
44
- } )
32
+ if let Some ( verifier) = & self . 0 {
33
+ let VerificationInput {
34
+ alg,
35
+ signing_input,
36
+ decoded_signature,
37
+ } = input;
38
+ let verification_result = IJwsVerifier :: verify (
39
+ verifier,
40
+ alg. name ( ) . to_owned ( ) ,
41
+ signing_input. into ( ) ,
42
+ decoded_signature. into ( ) ,
43
+ WasmJwk ( public_key. to_owned ( ) ) ,
44
+ ) ;
45
+ // Convert error
46
+ crate :: error:: stringify_js_error ( verification_result) . map_err ( |error_string| {
47
+ SignatureVerificationError :: new ( SignatureVerificationErrorKind :: Unspecified ) . with_custom_message ( error_string)
48
+ } )
49
+ } else {
50
+ match input. alg {
51
+ JwsAlgorithm :: EdDSA => EdDSAJwsVerifier :: default ( ) . verify ( input, public_key) ,
52
+ JwsAlgorithm :: ES256 | JwsAlgorithm :: ES256K => EcDSAJwsVerifier :: default ( ) . verify ( input, public_key) ,
53
+ _ => Err ( identity_iota:: verification:: jws:: SignatureVerificationErrorKind :: UnsupportedAlg . into ( ) ) ,
54
+ }
55
+ }
45
56
}
46
57
}
47
58
#[ wasm_bindgen( typescript_custom_section) ]
0 commit comments