#![forbid(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/tendermint/signatory/develop/img/signatory-rustacean.png",
html_root_url = "https://docs.rs/signatory-ledger-tm/0.14.0"
)]
use ledger_tendermint::ledgertm::TendermintValidatorApp;
use signatory::{
ed25519::{PublicKey, Signature},
public_key::PublicKeyed,
signature::{Error, Signer},
};
use std::sync::{Arc, Mutex};
pub struct Ed25519LedgerTmAppSigner {
app: Arc<Mutex<TendermintValidatorApp>>,
}
impl Ed25519LedgerTmAppSigner {
pub fn connect() -> Result<Self, Error> {
let validator_app = TendermintValidatorApp::connect().map_err(Error::from_cause)?;
let app = Arc::new(Mutex::new(validator_app));
let signer = Ed25519LedgerTmAppSigner { app };
let _pk = signer.public_key().unwrap();
Ok(signer)
}
}
impl PublicKeyed<PublicKey> for Ed25519LedgerTmAppSigner {
fn public_key(&self) -> Result<PublicKey, Error> {
let app = self.app.lock().unwrap();
let pk = app.public_key().map_err(Error::from_cause)?;
Ok(PublicKey(pk))
}
}
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_cause)?;
Ok(Signature(sig))
}
}
#[cfg(test)]
mod tests {
use crate::Ed25519LedgerTmAppSigner;
#[test]
fn public_key() {
use signatory::public_key::PublicKeyed;
let signer = Ed25519LedgerTmAppSigner::connect().unwrap();
let _pk = signer.public_key().unwrap();
println!("PK {:0X?}", _pk);
}
#[test]
fn sign() {
use crate::Ed25519LedgerTmAppSigner;
use signatory::signature::Signer;
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]
fn sign2() {
use signatory::signature::Signer;
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]
fn sign_many() {
use signatory::public_key::PublicKeyed;
use signatory::signature::Signer;
use Ed25519LedgerTmAppSigner;
let signer = Ed25519LedgerTmAppSigner::connect().unwrap();
let _pk = signer.public_key().unwrap();
println!("PK {:0X?}", _pk);
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);
}
}
}