tendermint/public_key/
pub_key_request.rs

1use crate::{chain::Id as ChainId, prelude::*};
2
3/// PubKeyRequest requests the consensus public key from the remote signer.
4#[derive(Clone, PartialEq, Eq, Debug)]
5pub struct PubKeyRequest {
6    /// Chain ID
7    pub chain_id: ChainId,
8}
9
10tendermint_pb_modules! {
11    use super::PubKeyRequest;
12    use crate::{chain::Id as ChainId, prelude::*};
13    use pb::privval::PubKeyRequest as RawPubKeyRequest;
14
15    impl Protobuf<RawPubKeyRequest> for PubKeyRequest {}
16
17    impl TryFrom<RawPubKeyRequest> for PubKeyRequest {
18        type Error = crate::Error;
19
20        fn try_from(value: RawPubKeyRequest) -> Result<Self, Self::Error> {
21            Ok(PubKeyRequest {
22                chain_id: ChainId::try_from(value.chain_id)?,
23            })
24        }
25    }
26
27    impl From<PubKeyRequest> for RawPubKeyRequest {
28        fn from(value: PubKeyRequest) -> Self {
29            RawPubKeyRequest {
30                chain_id: value.chain_id.as_str().to_string(),
31            }
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    tendermint_pb_modules! {
39        use super::super::PubKeyRequest;
40        use pb::privval::PubKeyRequest as RawPubKeyRequest;
41        use crate::{chain::Id as ChainId, prelude::*};
42        use core::str::FromStr;
43
44        #[test]
45        fn test_empty_pubkey_msg() {
46            // test-vector generated via the following go code:
47            // import (
48            // "fmt"
49            // "github.com/tendermint/tendermint/proto/tendermint/privval"
50            // )
51            // func ed25519_empty() {
52            // pkr := &privval.PubKeyRequest{
53            // ChainId: "A",
54            // }
55            // pbpk, _ := pkr.Marshal()
56            // fmt.Printf("%#v\n", pbpk)
57            //
58            // }
59
60            let want: Vec<u8> = vec![10, 1, 65];
61            let msg = PubKeyRequest {
62                chain_id: ChainId::from_str("A").unwrap(),
63            };
64            let mut got = vec![];
65            Protobuf::<RawPubKeyRequest>::encode(msg.clone(), &mut got).unwrap();
66
67            assert_eq!(got, want);
68
69            match <PubKeyRequest as Protobuf<RawPubKeyRequest>>::decode(want.as_ref()) {
70                Ok(have) => assert_eq!(have, msg),
71                Err(err) => panic!("{}", err.to_string()),
72            }
73        }
74    }
75}