mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-29 05:50:31 +08:00
66 lines
1.3 KiB
Rust
66 lines
1.3 KiB
Rust
use crate::crypto::{KeyPair, PublicKey, SecretKey, Seed, Signature, Signer};
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct MockSigner {
|
|
pk: PublicKey,
|
|
sk: SecretKey,
|
|
}
|
|
|
|
impl MockSigner {
|
|
pub fn new(rng: &mut fastrand::Rng) -> Self {
|
|
let mut bytes: [u8; 32] = [0; 32];
|
|
|
|
for byte in &mut bytes {
|
|
*byte = rng.u8(..);
|
|
}
|
|
let seed = Seed::new(bytes);
|
|
let keypair = KeyPair::from_seed(seed);
|
|
|
|
Self::from(keypair.sk)
|
|
}
|
|
}
|
|
|
|
impl From<SecretKey> for MockSigner {
|
|
fn from(sk: SecretKey) -> Self {
|
|
let pk = sk.public_key().into();
|
|
Self { sk, pk }
|
|
}
|
|
}
|
|
|
|
impl Default for MockSigner {
|
|
fn default() -> Self {
|
|
let seed = Seed::generate();
|
|
let keypair = KeyPair::from_seed(seed);
|
|
let sk = keypair.sk;
|
|
|
|
Self {
|
|
pk: sk.public_key().into(),
|
|
sk,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl PartialEq for MockSigner {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.pk == other.pk
|
|
}
|
|
}
|
|
|
|
impl Eq for MockSigner {}
|
|
|
|
impl std::hash::Hash for MockSigner {
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
self.pk.hash(state)
|
|
}
|
|
}
|
|
|
|
impl Signer for MockSigner {
|
|
fn public_key(&self) -> &PublicKey {
|
|
&self.pk
|
|
}
|
|
|
|
fn sign(&self, msg: &[u8]) -> Signature {
|
|
self.sk.sign(msg, None).into()
|
|
}
|
|
}
|