-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move CID operations to dwn-rs-core
- Loading branch information
Showing
8 changed files
with
109 additions
and
12 deletions.
There are no files selected for viewing
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,66 @@ | ||
pub mod cid; | ||
|
||
use partially::Partial; | ||
use rand::{distributions::Alphanumeric, Rng}; | ||
use secp256k1::{Keypair, Secp256k1}; | ||
use ssi_dids_core::DIDBuf; | ||
use std::str::FromStr; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum PersonaError { | ||
#[error("DID error: {0}")] | ||
DIDError(#[from] ssi_dids_core::InvalidDID<String>), | ||
} | ||
|
||
#[derive(Partial, Debug)] | ||
#[partially(derive(Default))] | ||
pub struct Persona { | ||
pub did: DIDBuf, | ||
pub key_id: String, | ||
keypair: secp256k1::Keypair, | ||
} | ||
|
||
impl Persona { | ||
pub fn generate( | ||
PartialPersona { | ||
did, | ||
key_id, | ||
keypair, | ||
}: PartialPersona, | ||
) -> Result<Self, PersonaError> { | ||
let did = did.unwrap_or_else(|| { | ||
let suffix = generate_random_string(32); | ||
DIDBuf::from_str(&format!("did:example:{}", suffix)).unwrap() | ||
}); | ||
|
||
let keypair = keypair.unwrap_or_else(|| { | ||
let secp = Secp256k1::new(); | ||
|
||
Keypair::new(&secp, &mut rand::thread_rng()) | ||
}); | ||
|
||
let key_id = key_id.unwrap_or_else(|| { | ||
let suffix = generate_random_string(16); | ||
format!("{}#{}", did, suffix) | ||
}); | ||
|
||
Ok(Self { | ||
did, | ||
key_id, | ||
keypair, | ||
}) | ||
} | ||
|
||
pub fn public_key(&self) -> secp256k1::PublicKey { | ||
self.keypair.public_key() | ||
} | ||
} | ||
|
||
pub fn generate_random_string(len: usize) -> String { | ||
rand::thread_rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(len) | ||
.map(char::from) | ||
.collect() | ||
} |
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 |
---|---|---|
@@ -1,6 +1,3 @@ | ||
pub mod cid; | ||
pub use cid::*; | ||
|
||
#[cfg(feature = "surrealdb")] | ||
pub mod surrealdb; | ||
#[cfg(feature = "surrealdb")] | ||
|
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