soroban_sdk/testutils/
sign.rs

1#![cfg(any(test, feature = "testutils"))]
2
3/// Sign implementations produce signatures for types that can be represented as
4/// the MSG.
5pub trait Sign<MSG> {
6    type Signature;
7    type Error;
8    /// Sign produces a signature for MSGs.
9    fn sign(&self, m: MSG) -> Result<Self::Signature, Self::Error>;
10}
11
12// TODO: Add a Verify interface and ed25519 implementation to counter the Sign
13// interface.
14
15pub mod ed25519 {
16    use crate::xdr;
17    use xdr::{Limited, Limits, WriteXdr};
18
19    #[derive(Debug)]
20    pub enum Error<E: std::error::Error> {
21        XdrError(xdr::Error),
22        Ed25519SignatureError(ed25519_dalek::SignatureError),
23        ConversionError(E),
24    }
25
26    impl<E: std::error::Error> std::error::Error for Error<E> {
27        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28            match self {
29                Self::XdrError(e) => e.source(),
30                Self::Ed25519SignatureError(e) => e.source(),
31                Self::ConversionError(e) => e.source(),
32            }
33        }
34    }
35
36    impl<E: std::error::Error> std::fmt::Display for Error<E> {
37        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
38            match self {
39                Self::XdrError(e) => write!(f, "{}", e),
40                Self::Ed25519SignatureError(e) => write!(f, "{}", e),
41                Self::ConversionError(e) => write!(f, "{}", e),
42            }
43        }
44    }
45
46    impl<E: std::error::Error> From<xdr::Error> for Error<E> {
47        fn from(e: xdr::Error) -> Self {
48            Error::XdrError(e)
49        }
50    }
51
52    impl<E: std::error::Error> From<ed25519_dalek::SignatureError> for Error<E> {
53        fn from(e: ed25519_dalek::SignatureError) -> Self {
54            Error::Ed25519SignatureError(e)
55        }
56    }
57
58    pub use super::Sign;
59
60    impl<S, M> Sign<M> for S
61    where
62        S: ed25519_dalek::Signer<ed25519_dalek::Signature>,
63        M: TryInto<xdr::ScVal>,
64        <M as TryInto<xdr::ScVal>>::Error: std::error::Error,
65    {
66        type Error = Error<<M as TryInto<xdr::ScVal>>::Error>;
67        type Signature = [u8; 64];
68        fn sign(&self, m: M) -> Result<Self::Signature, Self::Error> {
69            let mut buf = Vec::<u8>::new();
70            let val: xdr::ScVal = m.try_into().map_err(Self::Error::ConversionError)?;
71            val.write_xdr(&mut Limited::new(&mut buf, Limits::none()))?;
72            Ok(self.try_sign(&buf)?.to_bytes())
73        }
74    }
75
76    #[cfg(test)]
77    mod test {
78        use super::Sign;
79        use ed25519_dalek::SigningKey;
80
81        #[test]
82        fn sign() {
83            let sk = SigningKey::from_bytes(
84                &hex::decode("5acc7253295dfc356c046297925a369f3d2762d00afdf2583ecbe92180b07c37")
85                    .unwrap()
86                    .try_into()
87                    .unwrap(),
88            );
89            let sig = sk.sign(128i64).unwrap();
90            assert_eq!(
91                hex::encode(sig),
92                // Verified with https://go.dev/play/p/XiK8sOmvPsh
93                "a9b9dfac10bc1e5c8bc565e9515e5d086e3264b71bf4daf2c7340e1d10fae86e2563fa1d639ff153559a9710dfa270a9462fe87faa0e18a7a54a8a1a6151e909",
94            );
95        }
96    }
97}