Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
use did:key for peer IDs (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
chunningham authored Jul 20, 2023
1 parent 03b0494 commit 7373d67
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
7 changes: 3 additions & 4 deletions kepler-core/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::events::{epoch_hash, Delegation, Event, HashError, Invocation, Operation, Revocation};
use crate::hash::Hash;
use crate::keys::Secrets;
use crate::keys::{get_did_key, Secrets};
use crate::migrations::Migrator;
use crate::models::*;
use crate::relationships::*;
Expand All @@ -14,7 +14,6 @@ use kepler_lib::{
authorization::{EncodingError, KeplerDelegation},
resource::OrbitId,
};
use libp2p::identity::PublicKey;
use sea_orm::{
entity::prelude::*,
error::{DbErr, RuntimeErr, SqlxError},
Expand Down Expand Up @@ -117,8 +116,8 @@ impl<C, B, K> OrbitDatabase<C, B, K>
where
K: Secrets,
{
pub async fn stage_key(&self, orbit: &OrbitId) -> Result<PublicKey, K::Error> {
self.secrets.stage_keypair(orbit).await
pub async fn stage_key(&self, orbit: &OrbitId) -> Result<String, K::Error> {
self.secrets.stage_keypair(orbit).await.map(get_did_key)
}
}

Expand Down
25 changes: 19 additions & 6 deletions kepler-core/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@ use kepler_lib::{
libipld::cid::multihash::{Blake3_256, Hasher},
resource::OrbitId,
};
use libp2p::{
identity::{
ed25519::{Keypair as EdKP, SecretKey},
DecodingError, Keypair, PublicKey,
},
PeerId,
use libp2p::identity::{
ed25519::{Keypair as EdKP, SecretKey},
DecodingError,
};
use sea_orm_migration::async_trait::async_trait;
use std::error::Error as StdError;

pub use libp2p::{
identity::{Keypair, PublicKey},
PeerId,
};

pub(crate) fn get_did_key(key: PublicKey) -> String {
use kepler_lib::libipld::cid::multibase;
// only ed25519 feature is enabled, so this unwrap should never fail
let ed25519_pk_bytes = key.try_into_ed25519().unwrap().to_bytes();
let multicodec_pk = [[0xed].as_slice(), ed25519_pk_bytes.as_slice()].concat();
format!(
"did:key:{}",
multibase::encode(multibase::Base::Base58Btc, multicodec_pk)
)
}

#[async_trait]
pub trait Secrets {
type Error: StdError;
Expand Down
6 changes: 2 additions & 4 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn open_host_key(
s: &State<Kepler>,
orbit: &str,
) -> Result<String, (Status, &'static str)> {
Ok(s.stage_key(
s.stage_key(
&orbit
.parse()
.map_err(|_| (Status::BadRequest, "Invalid orbit ID"))?,
Expand All @@ -46,9 +46,7 @@ pub async fn open_host_key(
Status::InternalServerError,
"Failed to stage keypair for orbit",
)
})?
.to_peer_id()
.to_base58())
})
}

#[post("/delegate")]
Expand Down

0 comments on commit 7373d67

Please sign in to comment.