use crate::{Error, Namespace, PrimaryKey, Result};
use hypercore::{PartialKeypair, SigningKey, VerifyingKey};
pub const DEFAULT_NAMESPACE: Namespace = [0; 32];
const SEED_SIZE: usize = 32;
const SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES: usize = 32;
const SODIUM_CRYPTO_SIGN_SECRETKEYBYTES: usize = 64;
const NS: [u8; 32] = [
172, 92, 76, 191, 177, 6, 71, 118, 64, 70, 55, 162, 128, 199, 31, 172, 130, 50, 129, 211, 81,
235, 236, 237, 3, 21, 23, 67, 39, 13, 239, 41,
];
unsafe fn derive_seed(
primary_key: PrimaryKey,
namespace: &Namespace,
name: &str,
) -> Result<Vec<u8>> {
let mut out = vec![0; SEED_SIZE];
let mut input = Vec::new();
let name_bytes: Vec<u8> = name.into();
input.extend_from_slice(&NS);
input.extend_from_slice(namespace);
input.extend_from_slice(&name_bytes);
let ret = libsodium_sys::crypto_generichash(
out.as_mut_ptr(),
SEED_SIZE,
input.as_mut_ptr(),
input.len() as u64,
primary_key.as_ptr(),
32,
);
if ret != 0 {
return Err(Error::LibSodiumGenericHash(ret));
}
Ok(out)
}
pub fn key_pair_from_name(
primary_key: PrimaryKey,
namespace: &Namespace,
name: &str,
) -> Result<PartialKeypair> {
let mut public_key: Vec<u8> = vec![0; SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES];
let mut secret_key: Vec<u8> = vec![0; SODIUM_CRYPTO_SIGN_SECRETKEYBYTES];
unsafe {
let seed = derive_seed(primary_key, namespace, name)?;
let ret = libsodium_sys::crypto_sign_seed_keypair(
public_key.as_mut_ptr(),
secret_key.as_mut_ptr(),
seed.as_ptr(),
);
if ret != 0 {
return Err(Error::LibSodiumSignSeedKeypair(ret));
}
};
let signing_key = SigningKey::from_keypair_bytes(&secret_key.try_into().unwrap())?;
let verifying_key = VerifyingKey::from_bytes(&public_key.try_into().unwrap())?;
Ok(PartialKeypair {
public: verifying_key,
secret: Some(signing_key),
})
}
#[test]
fn check_derive_seed() -> Result<()> {
let expected: [u8; 32] = [
20, 199, 106, 232, 158, 115, 83, 70, 13, 9, 129, 194, 50, 190, 160, 158, 46, 252, 91, 200,
138, 10, 110, 49, 141, 167, 190, 36, 160, 145, 113, 106,
];
let name = "foo";
let primary_key: [u8; 32] = [
45, 168, 29, 102, 53, 33, 238, 58, 210, 74, 33, 141, 133, 20, 97, 2, 65, 13, 85, 203, 174,
189, 180, 85, 43, 11, 202, 208, 12, 156, 36, 122,
];
let result = unsafe { derive_seed(primary_key, &DEFAULT_NAMESPACE, name)? };
assert_eq!(result, expected);
Ok(())
}