use signatory::ed25519::{PublicKey, PUBLIC_KEY_SIZE};
pub const AMINO_NAME: &str = "tendermint/remotesigner/PubKeyRequest";
#[derive(Clone, PartialEq, Message)]
#[amino_name = "tendermint/remotesigner/PubKeyResponse"]
pub struct PubKeyResponse {
#[prost(bytes, tag = "1", amino_name = "tendermint/PubKeyEd25519")]
pub pub_key_ed25519: Vec<u8>,
}
#[derive(Clone, PartialEq, Message)]
#[amino_name = "tendermint/remotesigner/PubKeyRequest"]
pub struct PubKeyRequest {}
impl Into<PublicKey> for PubKeyResponse {
fn into(self) -> PublicKey {
let mut public_key = [0u8; PUBLIC_KEY_SIZE];
public_key.copy_from_slice(self.pub_key_ed25519.as_ref());
PublicKey(public_key)
}
}
impl Into<PubKeyResponse> for PublicKey {
fn into(self) -> PubKeyResponse {
let pk = self.0.to_vec();
PubKeyResponse {
pub_key_ed25519: pk,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use prost::Message;
use std::error::Error;
#[test]
fn test_empty_pubkey_msg() {
let want = vec![0x4, 0xcb, 0x94, 0xd6, 0x20];
let msg = PubKeyRequest {};
let mut got = vec![];
let _have = msg.encode(&mut got);
assert_eq!(got, want);
match PubKeyRequest::decode(&want) {
Ok(have) => assert_eq!(have, msg),
Err(err) => assert!(false, err.description().to_string()),
}
}
#[test]
fn test_ed25519_pubkey_msg() {
let encoded = vec![
0x2b, 0x17, 0xe, 0xd5, 0x7c, 0xa, 0x25, 0x16, 0x24, 0xde, 0x64, 0x20, 0x79, 0xce, 0xd, 0xe0, 0x43, 0x33, 0x4a, 0xec,
0xe0, 0x8b, 0x7b, 0xb5, 0x61, 0xbc, 0xe7, 0xc1, 0xd4, 0x69, 0xc3, 0x44, 0x26, 0xec,
0xef, 0xc0, 0x72, 0xa, 0x52, 0x4d, 0x37, 0x32, 0xef, 0xed,
];
let msg = PubKeyResponse {
pub_key_ed25519: vec![
0x79, 0xce, 0xd, 0xe0, 0x43, 0x33, 0x4a, 0xec, 0xe0, 0x8b, 0x7b, 0xb5, 0x61, 0xbc,
0xe7, 0xc1, 0xd4, 0x69, 0xc3, 0x44, 0x26, 0xec, 0xef, 0xc0, 0x72, 0xa, 0x52, 0x4d,
0x37, 0x32, 0xef, 0xed,
],
};
let mut got = vec![];
let _have = msg.encode(&mut got);
assert_eq!(got, encoded);
match PubKeyResponse::decode(&encoded) {
Ok(have) => assert_eq!(have, msg),
Err(err) => assert!(false, err),
}
}
#[test]
fn test_into() {
let raw_pk: [u8; PUBLIC_KEY_SIZE] = [
0x79, 0xce, 0xd, 0xe0, 0x43, 0x33, 0x4a, 0xec, 0xe0, 0x8b, 0x7b, 0xb5, 0x61, 0xbc,
0xe7, 0xc1, 0xd4, 0x69, 0xc3, 0x44, 0x26, 0xec, 0xef, 0xc0, 0x72, 0xa, 0x52, 0x4d,
0x37, 0x32, 0xef, 0xed,
];
let want = PublicKey(raw_pk);
let pk = PubKeyResponse {
pub_key_ed25519: vec![
0x79, 0xce, 0xd, 0xe0, 0x43, 0x33, 0x4a, 0xec, 0xe0, 0x8b, 0x7b, 0xb5, 0x61, 0xbc,
0xe7, 0xc1, 0xd4, 0x69, 0xc3, 0x44, 0x26, 0xec, 0xef, 0xc0, 0x72, 0xa, 0x52, 0x4d,
0x37, 0x32, 0xef, 0xed,
],
};
let orig = pk.clone();
let got: PublicKey = pk.into();
assert_eq!(got, want);
let round_trip_pk: PubKeyResponse = got.into();
assert_eq!(round_trip_pk, orig);
}
#[test]
#[should_panic]
fn test_empty_into() {
let empty_msg = PubKeyResponse {
pub_key_ed25519: vec![],
};
let _got: PublicKey = empty_msg.into();
}
}