1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
//! Cryptographic signatures.
use std::convert::TryFrom;

use digest::{typenum::Unsigned as _, Digest as _};
use rand_core::{CryptoRng, RngCore};
use thiserror::Error;

use crate::core::common::crypto::signature::{PublicKey as CorePublicKey, Signer as CoreSigner};

pub mod context;
mod digests;
pub mod ed25519;
pub mod secp256k1;
pub mod secp256r1;
pub mod secp384r1;
pub mod sr25519;

/// A specific combination of signature and hash.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, cbor::Encode, cbor::Decode)]
pub enum SignatureType {
    #[cbor(rename = "ed25519_oasis")]
    Ed25519_Oasis,
    #[cbor(rename = "ed25519_pure")]
    Ed25519_Pure,
    #[cbor(rename = "ed25519_prehashed_sha512")]
    Ed25519_PrehashedSha512,
    #[cbor(rename = "secp256k1_oasis")]
    Secp256k1_Oasis,
    #[cbor(rename = "secp256k1_prehashed_keccak256")]
    Secp256k1_PrehashedKeccak256,
    #[cbor(rename = "secp256k1_prehashed_sha256")]
    Secp256k1_PrehashedSha256,
    #[cbor(rename = "sr25519_pure")]
    Sr25519_Pure,
    #[cbor(rename = "secp256r1_prehashed_sha256")]
    Secp256r1_PrehashedSha256,
    #[cbor(rename = "secp384r1_prehashed_sha384")]
    Secp384r1_PrehashedSha384,
}

impl SignatureType {
    pub fn as_int(&self) -> u8 {
        match self {
            Self::Ed25519_Oasis => 0,
            Self::Ed25519_Pure => 1,
            Self::Ed25519_PrehashedSha512 => 2,
            Self::Secp256k1_Oasis => 3,
            Self::Secp256k1_PrehashedKeccak256 => 4,
            Self::Secp256k1_PrehashedSha256 => 5,
            Self::Sr25519_Pure => 6,
            Self::Secp256r1_PrehashedSha256 => 7,
            Self::Secp384r1_PrehashedSha384 => 8,
        }
    }

    pub fn is_prehashed(&self) -> bool {
        matches!(
            self,
            Self::Ed25519_PrehashedSha512
                | Self::Secp256k1_PrehashedKeccak256
                | Self::Secp256k1_PrehashedSha256
                | Self::Secp256r1_PrehashedSha256
                | Self::Secp384r1_PrehashedSha384
        )
    }

    pub fn is_ed25519_variant(&self) -> bool {
        matches!(
            self,
            Self::Ed25519_Oasis | Self::Ed25519_Pure | Self::Ed25519_PrehashedSha512
        )
    }

    pub fn is_secp256k1_variant(&self) -> bool {
        matches!(
            self,
            Self::Secp256k1_Oasis
                | Self::Secp256k1_PrehashedKeccak256
                | Self::Secp256k1_PrehashedSha256
        )
    }

    pub fn is_secp256r1_variant(&self) -> bool {
        matches!(self, Self::Secp256r1_PrehashedSha256)
    }

    pub fn is_secp384r1_variant(&self) -> bool {
        matches!(self, Self::Secp384r1_PrehashedSha384)
    }

    pub fn is_sr25519_variant(&self) -> bool {
        matches!(self, Self::Sr25519_Pure)
    }
}

impl TryFrom<u8> for SignatureType {
    type Error = Error;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Ed25519_Oasis),
            1 => Ok(Self::Ed25519_Pure),
            2 => Ok(Self::Ed25519_PrehashedSha512),
            3 => Ok(Self::Secp256k1_Oasis),
            4 => Ok(Self::Secp256k1_PrehashedKeccak256),
            5 => Ok(Self::Secp256k1_PrehashedSha256),
            6 => Ok(Self::Sr25519_Pure),
            7 => Ok(Self::Secp256r1_PrehashedSha256),
            8 => Ok(Self::Secp384r1_PrehashedSha384),
            _ => Err(Error::InvalidArgument),
        }
    }
}

/// A public key used for signing.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, cbor::Encode, cbor::Decode)]
pub enum PublicKey {
    #[cbor(rename = "ed25519")]
    Ed25519(ed25519::PublicKey),

    #[cbor(rename = "secp256k1")]
    Secp256k1(secp256k1::PublicKey),

    #[cbor(rename = "secp256r1")]
    Secp256r1(secp256r1::PublicKey),

    #[cbor(rename = "secp384r1")]
    Secp384r1(secp384r1::PublicKey),

    #[cbor(rename = "sr25519")]
    Sr25519(sr25519::PublicKey),
}

/// Error.
#[derive(Error, Debug)]
pub enum Error {
    #[error("malformed public key")]
    MalformedPublicKey,
    #[error("malformed private key")]
    MalformedPrivateKey,
    #[error("malformed signature")]
    MalformedSignature,
    #[error("signature verification failed")]
    VerificationFailed,
    #[error("invalid argument")]
    InvalidArgument,
    #[error("invalid digest length")]
    InvalidDigestLength,
    #[error("other signing error")]
    SigningError,
}

impl PublicKey {
    /// Return the key type as string.
    pub fn key_type(&self) -> &str {
        match self {
            Self::Ed25519(_) => "ed25519",
            Self::Secp256k1(_) => "secp256k1",
            Self::Secp256r1(_) => "secp256r1",
            Self::Secp384r1(_) => "secp384r1",
            Self::Sr25519(_) => "sr25519",
        }
    }

    /// Return a byte representation of this public key.
    pub fn as_bytes(&self) -> &[u8] {
        match self {
            PublicKey::Ed25519(pk) => pk.as_bytes(),
            PublicKey::Secp256k1(pk) => pk.as_bytes(),
            PublicKey::Secp256r1(pk) => pk.as_bytes(),
            PublicKey::Secp384r1(pk) => pk.as_bytes(),
            PublicKey::Sr25519(pk) => pk.as_bytes(),
        }
    }

    /// Construct a public key from a slice of bytes.
    pub fn from_bytes(sig_type: SignatureType, bytes: &[u8]) -> Result<Self, Error> {
        match sig_type {
            SignatureType::Ed25519_Oasis
            | SignatureType::Ed25519_Pure
            | SignatureType::Ed25519_PrehashedSha512 => {
                Ok(Self::Ed25519(ed25519::PublicKey::from_bytes(bytes)?))
            }
            SignatureType::Secp256k1_Oasis
            | SignatureType::Secp256k1_PrehashedKeccak256
            | SignatureType::Secp256k1_PrehashedSha256 => {
                Ok(Self::Secp256k1(secp256k1::PublicKey::from_bytes(bytes)?))
            }
            SignatureType::Secp256r1_PrehashedSha256 => {
                Ok(Self::Secp256r1(secp256r1::PublicKey::from_bytes(bytes)?))
            }
            SignatureType::Secp384r1_PrehashedSha384 => {
                Ok(Self::Secp384r1(secp384r1::PublicKey::from_bytes(bytes)?))
            }
            SignatureType::Sr25519_Pure => {
                Ok(Self::Sr25519(sr25519::PublicKey::from_bytes(bytes)?))
            }
        }
    }

    /// Verify a signature.
    pub fn verify(
        &self,
        context: &[u8],
        message: &[u8],
        signature: &Signature,
    ) -> Result<(), Error> {
        match self {
            PublicKey::Ed25519(pk) => pk.verify(context, message, signature),
            PublicKey::Secp256k1(pk) => pk.verify(context, message, signature),
            PublicKey::Secp256r1(pk) => pk.verify(context, message, signature),
            PublicKey::Secp384r1(pk) => pk.verify(context, message, signature),
            PublicKey::Sr25519(pk) => pk.verify(context, message, signature),
        }
    }

    /// Verify signature raw using the underlying method, without the domain
    /// separation schema.
    pub fn verify_raw(&self, message: &[u8], signature: &Signature) -> Result<(), Error> {
        match self {
            PublicKey::Ed25519(pk) => pk.verify_raw(message, signature),
            PublicKey::Secp256k1(pk) => pk.verify_raw(message, signature),
            PublicKey::Secp256r1(pk) => pk.verify_raw(message, signature),
            PublicKey::Secp384r1(pk) => pk.verify_raw(message, signature),
            PublicKey::Sr25519(_) => Err(Error::InvalidArgument),
        }
    }

    /// Verify the signature of a message.
    pub fn verify_by_type(
        &self,
        signature_type: SignatureType,
        context_or_hash: &[u8],
        message: &[u8],
        signature: &Signature,
    ) -> Result<(), Error> {
        match self {
            Self::Ed25519(pk) => match signature_type {
                SignatureType::Ed25519_Oasis => pk.verify(context_or_hash, message, signature),
                SignatureType::Ed25519_Pure => pk.verify_raw(message, signature),
                SignatureType::Ed25519_PrehashedSha512 => {
                    if context_or_hash.len()
                        != <sha2::Sha512 as sha2::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    let digest =
                        digests::DummyDigest::<sha2::Sha512>::new_precomputed(context_or_hash);
                    pk.verify_digest(digest, signature)
                }
                _ => Err(Error::InvalidArgument),
            },
            Self::Secp256k1(pk) => match signature_type {
                SignatureType::Secp256k1_Oasis => pk.verify(context_or_hash, message, signature),
                SignatureType::Secp256k1_PrehashedKeccak256 => {
                    if context_or_hash.len()
                        != <sha3::Keccak256 as sha3::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    // Use SHA-256 for RFC6979 even if Keccak256 was used for the message.
                    let digest = digests::DummyDigest::<k256::sha2::Sha256>::new_precomputed(
                        context_or_hash,
                    );
                    pk.verify_digest(digest, signature)
                }
                SignatureType::Secp256k1_PrehashedSha256 => {
                    if context_or_hash.len()
                        != <sha2::Sha256 as sha2::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    let digest = digests::DummyDigest::<k256::sha2::Sha256>::new_precomputed(
                        context_or_hash,
                    );
                    pk.verify_digest(digest, signature)
                }
                _ => Err(Error::InvalidArgument),
            },
            Self::Secp256r1(pk) => match signature_type {
                SignatureType::Secp256r1_PrehashedSha256 => {
                    if context_or_hash.len()
                        != <sha2::Sha256 as sha2::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    let digest =
                        digests::DummyDigest::<sha2::Sha256>::new_precomputed(context_or_hash);
                    pk.verify_digest(digest, signature)
                }
                _ => Err(Error::InvalidArgument),
            },
            Self::Secp384r1(pk) => match signature_type {
                SignatureType::Secp384r1_PrehashedSha384 => {
                    if context_or_hash.len()
                        != <sha2::Sha384 as sha2::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    let digest =
                        digests::DummyDigest::<sha2::Sha384>::new_precomputed(context_or_hash);
                    pk.verify_digest(digest, signature)
                }
                _ => Err(Error::InvalidArgument),
            },
            Self::Sr25519(pk) => match signature_type {
                SignatureType::Sr25519_Pure => pk.verify_raw(context_or_hash, message, signature),
                _ => Err(Error::InvalidArgument),
            },
        }
    }

    /// Verify a batch of signatures of the same message.
    pub fn verify_batch_multisig(
        context: &[u8],
        message: &[u8],
        public_keys: &[PublicKey],
        signatures: &[Signature],
    ) -> Result<(), Error> {
        if public_keys.len() != signatures.len() {
            return Err(Error::InvalidArgument);
        }

        // TODO: Use actual batch verification.
        for (pk, sig) in public_keys.iter().zip(signatures.iter()) {
            pk.verify(context, message, sig)?;
        }
        Ok(())
    }
}

impl AsRef<[u8]> for PublicKey {
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl PartialEq<CorePublicKey> for PublicKey {
    fn eq(&self, other: &CorePublicKey) -> bool {
        match self {
            PublicKey::Ed25519(pk) => pk.as_bytes() == other.as_ref(),
            _ => false,
        }
    }
}

impl TryFrom<PublicKey> for CorePublicKey {
    type Error = &'static str;

    fn try_from(pk: PublicKey) -> Result<Self, Self::Error> {
        match pk {
            PublicKey::Ed25519(pk) => Ok(pk.into()),
            _ => Err("not an Ed25519 public key"),
        }
    }
}

impl From<CorePublicKey> for PublicKey {
    fn from(pk: CorePublicKey) -> Self {
        Self::Ed25519(pk.into())
    }
}

/// Variable-length opaque signature.
#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
#[cbor(transparent)]
pub struct Signature(Vec<u8>);

impl AsRef<[u8]> for Signature {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl From<Vec<u8>> for Signature {
    fn from(v: Vec<u8>) -> Signature {
        Signature(v)
    }
}

impl From<Signature> for Vec<u8> {
    fn from(s: Signature) -> Vec<u8> {
        s.0
    }
}

/// Common trait for memory signers.
pub trait Signer: Send + Sync {
    /// Create a new random signer.
    fn random(rng: &mut (impl RngCore + CryptoRng)) -> Result<Self, Error>
    where
        Self: Sized;

    /// Create a new signer from the given seed.
    fn new_from_seed(seed: &[u8]) -> Result<Self, Error>
    where
        Self: Sized;

    /// Recreate signer from a byte serialization.
    fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
    where
        Self: Sized;

    /// Serialize the signer into bytes.
    fn to_bytes(&self) -> Vec<u8>;

    /// Return the public key counterpart to the signer's secret key.
    fn public_key(&self) -> PublicKey;

    /// Generate a signature over the context and message.
    fn sign(&self, context: &[u8], message: &[u8]) -> Result<Signature, Error>;

    /// Generate a signature over the message.
    fn sign_raw(&self, message: &[u8]) -> Result<Signature, Error>;
}

impl<T: Signer + ?Sized> Signer for std::sync::Arc<T> {
    fn random(_rng: &mut (impl RngCore + CryptoRng)) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Err(Error::InvalidArgument)
    }

    fn new_from_seed(_seed: &[u8]) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Err(Error::InvalidArgument)
    }

    fn from_bytes(_bytes: &[u8]) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Err(Error::InvalidArgument)
    }

    fn to_bytes(&self) -> Vec<u8> {
        T::to_bytes(self)
    }

    fn public_key(&self) -> PublicKey {
        T::public_key(self)
    }

    fn sign(&self, context: &[u8], message: &[u8]) -> Result<Signature, Error> {
        T::sign(self, context, message)
    }

    fn sign_raw(&self, message: &[u8]) -> Result<Signature, Error> {
        T::sign_raw(self, message)
    }
}

impl<T: CoreSigner> Signer for &T {
    fn random(_rng: &mut (impl RngCore + CryptoRng)) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Err(Error::InvalidArgument)
    }

    fn new_from_seed(_seed: &[u8]) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Err(Error::InvalidArgument)
    }

    fn from_bytes(_bytes: &[u8]) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Err(Error::InvalidArgument)
    }

    fn to_bytes(&self) -> Vec<u8> {
        vec![]
    }

    fn public_key(&self) -> PublicKey {
        PublicKey::Ed25519(self.public().into())
    }

    fn sign(&self, context: &[u8], message: &[u8]) -> Result<Signature, Error> {
        let raw_sig = CoreSigner::sign(*self, context, message).map_err(|_| Error::SigningError)?;
        Ok(Signature(raw_sig.as_ref().into()))
    }

    fn sign_raw(&self, _message: &[u8]) -> Result<Signature, Error> {
        Err(Error::InvalidArgument)
    }
}

impl Signer for crate::core::identity::Identity {
    fn random(_rng: &mut (impl RngCore + CryptoRng)) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Err(Error::InvalidArgument)
    }

    fn new_from_seed(_seed: &[u8]) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Err(Error::InvalidArgument)
    }

    fn from_bytes(_bytes: &[u8]) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Err(Error::InvalidArgument)
    }

    fn to_bytes(&self) -> Vec<u8> {
        vec![]
    }

    fn public_key(&self) -> PublicKey {
        PublicKey::Ed25519(self.public().into())
    }

    fn sign(&self, context: &[u8], message: &[u8]) -> Result<Signature, Error> {
        let raw_sig = CoreSigner::sign(self, context, message).map_err(|_| Error::SigningError)?;
        Ok(Signature(raw_sig.as_ref().into()))
    }

    fn sign_raw(&self, _message: &[u8]) -> Result<Signature, Error> {
        Err(Error::InvalidArgument)
    }
}

/// A memory-backed signer.
pub enum MemorySigner {
    Ed25519(ed25519::MemorySigner),
    Secp256k1(secp256k1::MemorySigner),
    Secp256r1(secp256r1::MemorySigner),
    Secp384r1(secp384r1::MemorySigner),
    Sr25519(sr25519::MemorySigner),
}

impl MemorySigner {
    /// Create a new memory signer from a seed.
    pub fn new_from_seed(sig_type: SignatureType, seed: &[u8]) -> Result<Self, Error> {
        if sig_type.is_ed25519_variant() {
            Ok(Self::Ed25519(ed25519::MemorySigner::new_from_seed(seed)?))
        } else if sig_type.is_secp256k1_variant() {
            Ok(Self::Secp256k1(secp256k1::MemorySigner::new_from_seed(
                seed,
            )?))
        } else if sig_type.is_secp256r1_variant() {
            Ok(Self::Secp256r1(secp256r1::MemorySigner::new_from_seed(
                seed,
            )?))
        } else if sig_type.is_secp384r1_variant() {
            Ok(Self::Secp384r1(secp384r1::MemorySigner::new_from_seed(
                seed,
            )?))
        } else if sig_type.is_sr25519_variant() {
            Ok(Self::Sr25519(sr25519::MemorySigner::new_from_seed(seed)?))
        } else {
            Err(Error::InvalidArgument)
        }
    }

    /// Create a new signer for testing purposes.
    pub fn new_test(sig_type: SignatureType, name: &str) -> Self {
        if sig_type.is_secp384r1_variant() {
            Self::new_from_seed(sig_type, &sha2::Sha384::digest(name)).unwrap()
        } else {
            Self::new_from_seed(sig_type, &sha2::Sha512_256::digest(name)).unwrap()
        }
    }

    /// Reconstruct the signer from its byte representation.
    pub fn from_bytes(sig_type: SignatureType, bytes: &[u8]) -> Result<Self, Error> {
        if sig_type.is_ed25519_variant() {
            Ok(Self::Ed25519(ed25519::MemorySigner::from_bytes(bytes)?))
        } else if sig_type.is_secp256k1_variant() {
            Ok(Self::Secp256k1(secp256k1::MemorySigner::from_bytes(bytes)?))
        } else if sig_type.is_secp256r1_variant() {
            Ok(Self::Secp256r1(secp256r1::MemorySigner::from_bytes(bytes)?))
        } else if sig_type.is_secp384r1_variant() {
            Ok(Self::Secp384r1(secp384r1::MemorySigner::from_bytes(bytes)?))
        } else if sig_type.is_sr25519_variant() {
            Ok(Self::Sr25519(sr25519::MemorySigner::from_bytes(bytes)?))
        } else {
            Err(Error::InvalidArgument)
        }
    }

    /// Return a byte representation of the signer.
    pub fn to_bytes(&self) -> Vec<u8> {
        match self {
            Self::Ed25519(signer) => signer.to_bytes(),
            Self::Secp256k1(signer) => signer.to_bytes(),
            Self::Secp256r1(signer) => signer.to_bytes(),
            Self::Secp384r1(signer) => signer.to_bytes(),
            Self::Sr25519(signer) => signer.to_bytes(),
        }
    }

    /// Public key corresponding to the signer.
    pub fn public_key(&self) -> PublicKey {
        match self {
            Self::Ed25519(signer) => signer.public_key(),
            Self::Secp256k1(signer) => signer.public_key(),
            Self::Secp256r1(signer) => signer.public_key(),
            Self::Secp384r1(signer) => signer.public_key(),
            Self::Sr25519(signer) => signer.public_key(),
        }
    }

    /// Generate a signature with the private key over the context and message.
    pub fn sign(&self, context: &[u8], message: &[u8]) -> Result<Signature, Error> {
        match self {
            Self::Ed25519(signer) => signer.sign(context, message),
            Self::Secp256k1(signer) => signer.sign(context, message),
            Self::Secp256r1(signer) => signer.sign(context, message),
            Self::Secp384r1(signer) => signer.sign(context, message),
            Self::Sr25519(signer) => signer.sign(context, message),
        }
    }

    /// Generate a signature with the private key over the message.
    pub fn sign_raw(&self, message: &[u8]) -> Result<Signature, Error> {
        match self {
            Self::Ed25519(signer) => signer.sign_raw(message),
            Self::Secp256k1(signer) => signer.sign_raw(message),
            Self::Secp256r1(signer) => signer.sign_raw(message),
            Self::Secp384r1(signer) => signer.sign_raw(message),
            Self::Sr25519(signer) => signer.sign_raw(message),
        }
    }

    /// Generate a signature for the specified message and optional context.
    pub fn sign_by_type(
        &self,
        signature_type: SignatureType,
        context_or_hash: &[u8],
        message: &[u8],
    ) -> Result<Signature, Error> {
        match self {
            Self::Ed25519(signer) => match signature_type {
                SignatureType::Ed25519_Oasis => signer.sign(context_or_hash, message),
                SignatureType::Ed25519_Pure => signer.sign_raw(message),
                SignatureType::Ed25519_PrehashedSha512 => {
                    if context_or_hash.len()
                        != <sha2::Sha512 as sha2::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    let digest =
                        digests::DummyDigest::<sha2::Sha512>::new_precomputed(context_or_hash);
                    signer.sign_digest(digest)
                }
                _ => Err(Error::InvalidArgument),
            },
            Self::Secp256k1(signer) => match signature_type {
                SignatureType::Secp256k1_Oasis => signer.sign(context_or_hash, message),
                SignatureType::Secp256k1_PrehashedKeccak256 => {
                    if context_or_hash.len()
                        != <sha3::Keccak256 as sha3::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    // Use SHA-256 for RFC6979 even if Keccak256 was used for the message.
                    let digest = digests::DummyDigest::<k256::sha2::Sha256>::new_precomputed(
                        context_or_hash,
                    );
                    signer.sign_digest(digest)
                }
                SignatureType::Secp256k1_PrehashedSha256 => {
                    if context_or_hash.len()
                        != <sha2::Sha256 as sha2::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    let digest = digests::DummyDigest::<k256::sha2::Sha256>::new_precomputed(
                        context_or_hash,
                    );
                    signer.sign_digest(digest)
                }
                _ => Err(Error::InvalidArgument),
            },
            Self::Secp256r1(signer) => match signature_type {
                SignatureType::Secp256r1_PrehashedSha256 => {
                    if context_or_hash.len()
                        != <sha2::Sha256 as sha2::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    let digest =
                        digests::DummyDigest::<sha2::Sha256>::new_precomputed(context_or_hash);
                    signer.sign_digest(digest)
                }
                _ => Err(Error::InvalidArgument),
            },
            Self::Secp384r1(signer) => match signature_type {
                SignatureType::Secp384r1_PrehashedSha384 => {
                    if context_or_hash.len()
                        != <sha2::Sha384 as sha2::digest::OutputSizeUser>::OutputSize::USIZE
                    {
                        return Err(Error::InvalidArgument);
                    }
                    let digest =
                        digests::DummyDigest::<sha2::Sha384>::new_precomputed(context_or_hash);
                    signer.sign_digest(digest)
                }
                _ => Err(Error::InvalidArgument),
            },
            Self::Sr25519(signer) => match signature_type {
                SignatureType::Sr25519_Pure => signer.sign(context_or_hash, message),
                _ => Err(Error::InvalidArgument),
            },
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_signature_conversion() {
        let raw = vec![0x00, 0x01, 0x02, 0x03];
        let sig = Signature::from(raw.clone());
        let v: Vec<u8> = sig.clone().into();
        assert_eq!(v, raw);

        let vref: &[u8] = v.as_ref();
        assert_eq!(vref, sig.as_ref());
    }

    #[test]
    fn test_memory_signer() {
        let ctx = b"oasis-core/test: context";
        let corrupt_ctx = b"oasis-core/test: wrong context";
        let message = b"this is a message";
        let corrupt_message = b"this isn't a message";

        for sig_type in [
            SignatureType::Ed25519_Oasis,
            SignatureType::Ed25519_Pure,
            SignatureType::Secp256k1_Oasis,
            SignatureType::Sr25519_Pure,
        ] {
            let signer = MemorySigner::new_test(sig_type, "memory signer test");
            let pk = signer.public_key();

            let signature = signer
                .sign_by_type(sig_type, ctx, message)
                .expect("signing should succeed");

            pk.verify_by_type(sig_type, ctx, message, &signature)
                .expect("signature should verify");
            pk.verify_by_type(sig_type, ctx, corrupt_message, &signature)
                .expect_err("signature should fail verification");
            if matches!(sig_type, SignatureType::Ed25519_Oasis)
                || matches!(sig_type, SignatureType::Secp256k1_Oasis)
            {
                pk.verify_by_type(sig_type, corrupt_ctx, message, &signature)
                    .expect_err("signature should fail verification");
                pk.verify_by_type(sig_type, corrupt_ctx, corrupt_message, &signature)
                    .expect_err("signature should fail verification");
            }
        }
    }

    #[test]
    fn test_memory_signer_prehashed() {
        let message = b"this is a message";
        let corrupt_message = b"this isn't a message";

        let sig_types: &[(SignatureType, Box<dyn Fn(&[u8]) -> Vec<u8>>)] = &[
            (
                SignatureType::Ed25519_PrehashedSha512,
                Box::new(|message| sha2::Sha512::digest(message).to_vec()),
            ),
            (
                SignatureType::Secp256k1_PrehashedKeccak256,
                Box::new(|message| sha3::Keccak256::digest(message).to_vec()),
            ),
            (
                SignatureType::Secp256k1_PrehashedSha256,
                Box::new(|message| sha2::Sha256::digest(message).to_vec()),
            ),
            (
                SignatureType::Secp256r1_PrehashedSha256,
                Box::new(|message| sha2::Sha256::digest(message).to_vec()),
            ),
            (
                SignatureType::Secp384r1_PrehashedSha384,
                Box::new(|message| sha2::Sha384::digest(message).to_vec()),
            ),
        ];

        for (sig_type, hasher) in sig_types {
            let hash = hasher(message);
            let corrupt_hash = hasher(corrupt_message);

            let signer = MemorySigner::new_test(*sig_type, "memory signer test");
            let pk = signer.public_key();

            let signature = signer
                .sign_by_type(*sig_type, &hash, b"")
                .expect("signing should succeed");
            pk.verify_by_type(*sig_type, &hash, b"", &signature)
                .expect("signature should verify");
            pk.verify_by_type(*sig_type, &corrupt_hash, b"", &signature)
                .expect_err("corrupt hash shouldn't verify");
        }
    }
}