use super::client::TendermintValidatorApp;
use crate::keyring::ed25519::{Signature, VerifyingKey};
use signature::{Error, Signer};
use std::sync::{Arc, Mutex};
pub(super) struct Ed25519LedgerTmAppSigner {
app: Arc<Mutex<TendermintValidatorApp>>,
}
impl Ed25519LedgerTmAppSigner {
pub fn connect() -> Result<Self, Error> {
let validator_app = TendermintValidatorApp::connect().map_err(Error::from_source)?;
let app = Arc::new(Mutex::new(validator_app));
Ok(Ed25519LedgerTmAppSigner { app })
}
}
impl From<&Ed25519LedgerTmAppSigner> for VerifyingKey {
fn from(signer: &Ed25519LedgerTmAppSigner) -> VerifyingKey {
let app = signer.app.lock().unwrap();
VerifyingKey::try_from(app.public_key().unwrap().as_ref())
.expect("invalid Ed25519 public key")
}
}
impl Signer<Signature> for Ed25519LedgerTmAppSigner {
fn try_sign(&self, msg: &[u8]) -> Result<Signature, Error> {
let app = self.app.lock().unwrap();
let sig = app.sign(msg).map_err(Error::from_source)?;
Ok(Signature::from(sig))
}
}
#[cfg(test)]
mod tests {
use super::{Ed25519LedgerTmAppSigner, VerifyingKey};
use signature::Signer;
#[test]
#[ignore]
fn public_key() {
let signer = Ed25519LedgerTmAppSigner::connect().unwrap();
let pk = VerifyingKey::from(&signer);
println!("PK {pk:0X?}");
}
#[test]
#[ignore]
fn sign() {
let signer = Ed25519LedgerTmAppSigner::connect().unwrap();
let some_message1 = [
33, 0x8, 0x1, 0x11, 0x10, 0x00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x22, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1,
];
signer.sign(&some_message1);
}
#[test]
#[ignore]
fn sign2() {
let signer = Ed25519LedgerTmAppSigner::connect().unwrap();
let some_message1 = [
33, 0x8, 0x1, 0x11, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x22, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1,
];
signer.sign(&some_message1);
let some_message2 = [
33, 0x8, 0x1, 0x11, 0x10, 0x00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x22, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1,
];
signer.sign(&some_message2);
}
#[test]
#[ignore]
fn sign_many() {
let signer = Ed25519LedgerTmAppSigner::connect().unwrap();
let pk = VerifyingKey::from(&signer);
println!("PK {pk:0X?}");
for index in 50u8..254u8 {
let some_message = [
33, 0x8, 0x1, 0x11, 0x40, 0x00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19, index, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x22, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1,
];
signer.sign(&some_message);
}
}
}