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
use crate::{chain::Id as ChainId, prelude::*};

/// PubKeyRequest requests the consensus public key from the remote signer.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PubKeyRequest {
    /// Chain ID
    pub chain_id: ChainId,
}

tendermint_pb_modules! {
    use super::PubKeyRequest;
    use crate::{chain::Id as ChainId, prelude::*};
    use pb::privval::PubKeyRequest as RawPubKeyRequest;

    impl Protobuf<RawPubKeyRequest> for PubKeyRequest {}

    impl TryFrom<RawPubKeyRequest> for PubKeyRequest {
        type Error = crate::Error;

        fn try_from(value: RawPubKeyRequest) -> Result<Self, Self::Error> {
            Ok(PubKeyRequest {
                chain_id: ChainId::try_from(value.chain_id)?,
            })
        }
    }

    impl From<PubKeyRequest> for RawPubKeyRequest {
        fn from(value: PubKeyRequest) -> Self {
            RawPubKeyRequest {
                chain_id: value.chain_id.as_str().to_string(),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    tendermint_pb_modules! {
        use super::super::PubKeyRequest;
        use pb::privval::PubKeyRequest as RawPubKeyRequest;
        use crate::{chain::Id as ChainId, prelude::*};
        use core::str::FromStr;

        #[test]
        fn test_empty_pubkey_msg() {
            // test-vector generated via the following go code:
            // import (
            // "fmt"
            // "github.com/tendermint/tendermint/proto/tendermint/privval"
            // )
            // func ed25519_empty() {
            // pkr := &privval.PubKeyRequest{
            // ChainId: "A",
            // }
            // pbpk, _ := pkr.Marshal()
            // fmt.Printf("%#v\n", pbpk)
            //
            // }

            let want: Vec<u8> = vec![10, 1, 65];
            let msg = PubKeyRequest {
                chain_id: ChainId::from_str("A").unwrap(),
            };
            let mut got = vec![];
            Protobuf::<RawPubKeyRequest>::encode(msg.clone(), &mut got).unwrap();

            assert_eq!(got, want);

            match <PubKeyRequest as Protobuf<RawPubKeyRequest>>::decode(want.as_ref()) {
                Ok(have) => assert_eq!(have, msg),
                Err(err) => panic!("{}", err.to_string()),
            }
        }
    }
}