use ed25519_dalek::{Signer, SigningKey};
use crate::adapter::{SIGNATURE_HEADER, TIMESTAMP_HEADER};
#[derive(Debug)]
pub struct TestKey(SigningKey);
#[derive(Debug, Clone)]
pub struct SignedRequest {
pub signature: String,
pub timestamp: String,
pub body: Vec<u8>,
}
impl SignedRequest {
pub fn headers(&self) -> [(&'static str, &str); 2] {
[
(SIGNATURE_HEADER, &self.signature),
(TIMESTAMP_HEADER, &self.timestamp),
]
}
}
const TIMESTAMP: &str = "1760000000";
impl TestKey {
pub fn new(seed: [u8; 32]) -> Self {
Self(SigningKey::from_bytes(&seed))
}
pub fn public_key(&self) -> String {
hex::encode(self.0.verifying_key().as_bytes())
}
pub fn sign(&self, timestamp: &str, body: &[u8]) -> String {
let message = [timestamp.as_bytes(), body].concat();
hex::encode(self.0.sign(&message).to_bytes())
}
pub fn signed(&self, body: impl Into<Vec<u8>>) -> SignedRequest {
let body = body.into();
SignedRequest {
signature: self.sign(TIMESTAMP, &body),
timestamp: TIMESTAMP.into(),
body,
}
}
}
impl Default for TestKey {
fn default() -> Self {
Self::new([7; 32])
}
}