11// Copyright 2020-2023 IOTA Stiftung
22// SPDX-License-Identifier: Apache-2.0
33
4+ use identity_ecdsa_verifier:: EcDSAJwsVerifier ;
5+ use identity_eddsa_verifier:: EdDSAJwsVerifier ;
6+ use identity_iota:: verification:: jws:: JwsAlgorithm ;
47use identity_iota:: verification:: jws:: JwsVerifier ;
58use identity_iota:: verification:: jws:: SignatureVerificationError ;
69use identity_iota:: verification:: jws:: SignatureVerificationErrorKind ;
@@ -10,12 +13,12 @@ use wasm_bindgen::prelude::*;
1013use crate :: jose:: WasmJwk ;
1114
1215/// 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 > ) ;
1619
1720impl WasmJwsVerifier {
18- pub ( crate ) fn new ( verifier : IJwsVerifier ) -> Self {
21+ pub ( crate ) fn new ( verifier : Option < IJwsVerifier > ) -> Self {
1922 Self ( verifier)
2023 }
2124}
@@ -26,22 +29,30 @@ impl JwsVerifier for WasmJwsVerifier {
2629 input : identity_iota:: verification:: jws:: VerificationInput ,
2730 public_key : & identity_iota:: verification:: jwk:: Jwk ,
2831 ) -> 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+ }
4556 }
4657}
4758#[ wasm_bindgen( typescript_custom_section) ]
0 commit comments