Skip to main content

defuse_crypto/curve/
ed25519.rs

1use crate::Curve;
2
3pub struct Ed25519;
4
5impl Curve for Ed25519 {
6    type PublicKey = [u8; ed25519_dalek::PUBLIC_KEY_LENGTH];
7    type Signature = [u8; ed25519_dalek::SIGNATURE_LENGTH];
8
9    type Message = [u8];
10    type VerifyingKey = Self::PublicKey;
11}
12
13#[cfg(feature = "near-contract")]
14impl crate::VerifiableCurve for Ed25519 {
15    #[inline]
16    fn verify(
17        signature: &Self::Signature,
18        message: &Self::Message,
19        public_key: &Self::VerifyingKey,
20    ) -> Option<Self::PublicKey> {
21        if ed25519_dalek::VerifyingKey::from_bytes(public_key)
22            .ok()?
23            .is_weak()
24        {
25            // prevent using weak (i.e. low order) public keys, see
26            // https://github.com/dalek-cryptography/ed25519-dalek#weak-key-forgery-and-verify_strict
27            return None;
28        }
29
30        near_sdk::env::ed25519_verify(signature, message, public_key)
31            .then_some(public_key)
32            .copied()
33    }
34}
35
36#[cfg_attr(any(feature = "arbitrary", test), derive(arbitrary::Arbitrary))]
37#[cfg_attr(
38    feature = "borsh",
39    derive(::borsh::BorshSerialize, ::borsh::BorshDeserialize),
40    cfg_attr(feature = "abi", derive(::borsh::BorshSchema))
41)]
42#[cfg_attr(
43    feature = "serde",
44    derive(::serde_with::SerializeDisplay, ::serde_with::DeserializeFromStr),
45    cfg_attr(feature = "abi", derive(::schemars::JsonSchema))
46)]
47#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
48#[repr(transparent)]
49pub struct Ed25519PublicKey(
50    // schemars ignores `with` at struct level for newtypes; must be on the field
51    #[cfg_attr(all(feature = "abi", feature = "serde"), schemars(with = "String"))]
52    pub  <Ed25519 as Curve>::PublicKey,
53);
54
55#[cfg_attr(any(feature = "arbitrary", test), derive(arbitrary::Arbitrary))]
56#[cfg_attr(
57    feature = "borsh",
58    derive(::borsh::BorshSerialize, ::borsh::BorshDeserialize),
59    cfg_attr(feature = "abi", derive(::borsh::BorshSchema))
60)]
61#[cfg_attr(
62    feature = "serde",
63    derive(::serde_with::SerializeDisplay, ::serde_with::DeserializeFromStr),
64    cfg_attr(feature = "abi", derive(::schemars::JsonSchema))
65)]
66#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
67#[repr(transparent)]
68pub struct Ed25519Signature(
69    // schemars ignores `with` at struct level for newtypes; must be on the field
70    #[cfg_attr(all(feature = "abi", feature = "serde"), schemars(with = "String"))]
71    pub  <Ed25519 as Curve>::Signature,
72);
73
74#[cfg(feature = "parse")]
75const _: () = {
76    use crate::{CurveType, ParseCurveError, TypedCurve};
77    use core::fmt::{self, Debug, Display};
78    use std::str::FromStr;
79
80    impl TypedCurve for Ed25519 {
81        const CURVE_TYPE: CurveType = CurveType::Ed25519;
82    }
83
84    impl Debug for Ed25519PublicKey {
85        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86            Display::fmt(self, f)
87        }
88    }
89
90    impl Display for Ed25519PublicKey {
91        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92            f.write_str(&<Ed25519 as TypedCurve>::to_base58(self.0))
93        }
94    }
95
96    impl FromStr for Ed25519PublicKey {
97        type Err = ParseCurveError;
98
99        fn from_str(s: &str) -> Result<Self, Self::Err> {
100            Ed25519::parse_base58(s).map(Self)
101        }
102    }
103
104    impl Debug for Ed25519Signature {
105        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106            Display::fmt(self, f)
107        }
108    }
109
110    impl Display for Ed25519Signature {
111        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112            f.write_str(&<Ed25519 as TypedCurve>::to_base58(self.0))
113        }
114    }
115
116    impl FromStr for Ed25519Signature {
117        type Err = ParseCurveError;
118
119        fn from_str(s: &str) -> Result<Self, Self::Err> {
120            Ed25519::parse_base58(s).map(Self)
121        }
122    }
123};