-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support generic hashers in UcanBuilder and ProofChain.
* Add `UcanBuilder::with_hasher()` to provide a hasher when encoding proofs. * Change `TryFrom<&Ucan> for Cid` into `Ucan::into_cid(hasher)`.
- Loading branch information
Showing
5 changed files
with
127 additions
and
23 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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod capabilities; | ||
mod crypto; | ||
mod identities; | ||
mod store; | ||
|
||
pub use capabilities::*; | ||
pub use crypto::*; | ||
pub use identities::*; | ||
pub use store::*; |
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,42 @@ | ||
use crate::store::{UcanStore, UcanStoreConditionalSend}; | ||
use anyhow::{anyhow, Result}; | ||
use async_trait::async_trait; | ||
use cid::multihash::{Code, MultihashDigest}; | ||
use cid::Cid; | ||
use libipld_core::codec::{Codec, Decode, Encode}; | ||
use libipld_core::raw::RawCodec; | ||
use std::collections::HashMap; | ||
use std::io::Cursor; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Clone, Default, Debug)] | ||
pub struct Blake2bMemoryStore { | ||
dags: Arc<Mutex<HashMap<Cid, Vec<u8>>>>, | ||
} | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
impl UcanStore<RawCodec> for Blake2bMemoryStore { | ||
async fn read<T: Decode<RawCodec>>(&self, cid: &Cid) -> Result<Option<T>> { | ||
let dags = self.dags.lock().map_err(|_| anyhow!("poisoned mutex!"))?; | ||
|
||
Ok(match dags.get(cid) { | ||
Some(bytes) => Some(T::decode(RawCodec, &mut Cursor::new(bytes))?), | ||
None => None, | ||
}) | ||
} | ||
|
||
async fn write<T: Encode<RawCodec> + UcanStoreConditionalSend + core::fmt::Debug>( | ||
&mut self, | ||
token: T, | ||
) -> Result<Cid> { | ||
let codec = RawCodec; | ||
let block = codec.encode(&token)?; | ||
let cid = Cid::new_v1(codec.into(), Code::Blake2b256.digest(&block)); | ||
|
||
let mut dags = self.dags.lock().map_err(|_| anyhow!("poisoned mutex!"))?; | ||
dags.insert(cid, block); | ||
|
||
Ok(cid) | ||
} | ||
} |
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