This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl host key derivation from static secret (#146)
* impl host key derivation from static secret * update readme, note on backing up the secret * remove libp2p, now unnecessary in main bin * require secret be provided in config
- Loading branch information
1 parent
20824ea
commit c75fc3f
Showing
12 changed files
with
313 additions
and
171 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use kepler_lib::{ | ||
libipld::cid::multihash::{Blake3_256, Hasher}, | ||
resource::OrbitId, | ||
}; | ||
use libp2p::{ | ||
identity::{ | ||
ed25519::{Keypair as EdKP, SecretKey}, | ||
DecodingError, Keypair, PublicKey, | ||
}, | ||
PeerId, | ||
}; | ||
use sea_orm_migration::async_trait::async_trait; | ||
use std::error::Error as StdError; | ||
|
||
#[async_trait] | ||
pub trait Secrets { | ||
type Error: StdError; | ||
async fn get_keypair(&self, orbit: &OrbitId) -> Result<Keypair, Self::Error>; | ||
async fn get_pubkey(&self, orbit: &OrbitId) -> Result<PublicKey, Self::Error> { | ||
Ok(self.get_keypair(orbit).await?.public()) | ||
} | ||
async fn stage_keypair(&self, orbit: &OrbitId) -> Result<PublicKey, Self::Error>; | ||
async fn save_keypair(&self, orbit: &OrbitId) -> Result<(), Self::Error>; | ||
async fn get_peer_id(&self, orbit: &OrbitId) -> Result<PeerId, Self::Error> { | ||
Ok(self.get_pubkey(orbit).await?.to_peer_id()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
pub trait SecretsSetup { | ||
type Error: StdError; | ||
type Input; | ||
type Output: Secrets; | ||
async fn setup(&self, input: Self::Input) -> Result<Self::Output, Self::Error>; | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct StaticSecret { | ||
secret: Vec<u8>, | ||
} | ||
|
||
impl StaticSecret { | ||
pub fn new(secret: Vec<u8>) -> Result<Self, Vec<u8>> { | ||
if secret.len() < 32 { | ||
Err(secret) | ||
} else { | ||
Ok(Self { secret }) | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Secrets for StaticSecret { | ||
type Error = DecodingError; | ||
async fn get_keypair(&self, orbit: &OrbitId) -> Result<Keypair, Self::Error> { | ||
let mut hasher = Blake3_256::default(); | ||
hasher.update(&self.secret); | ||
hasher.update(orbit.to_string().as_bytes()); | ||
let derived = hasher.finalize().to_vec(); | ||
Ok(EdKP::from(SecretKey::try_from_bytes(derived)?).into()) | ||
} | ||
async fn stage_keypair(&self, orbit: &OrbitId) -> Result<PublicKey, Self::Error> { | ||
self.get_pubkey(orbit).await | ||
} | ||
async fn save_keypair(&self, _orbit: &OrbitId) -> Result<(), Self::Error> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl SecretsSetup for StaticSecret { | ||
type Error = std::convert::Infallible; | ||
type Input = (); | ||
type Output = Self; | ||
async fn setup(&self, _input: Self::Input) -> Result<Self::Output, Self::Error> { | ||
Ok(self.clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.