-
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.
- Loading branch information
Showing
8 changed files
with
136 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::ucan::JsResult; | ||
use ::ucan::Ucan; | ||
use cid::multihash::Code; | ||
use js_sys::Error; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
|
||
/// Compute CID for a UCAN | ||
/// | ||
/// Hashers include SHA2-256, SHA2-512, SHA3-224 | ||
/// SHA3-256, SHA3-384, SHA3-512, Keccak-224, Keccak-256, Keccak-384 | ||
/// Keccak-512, BLAKE2b-256, BLAKE2b-512, BLAKE2s-128, and BLAKE3-256. | ||
/// | ||
/// Defaults to BLAKE3-256 hash function. | ||
/// | ||
#[wasm_bindgen(js_name = "toCID")] | ||
pub async fn to_cid(token: String, hasher: Option<String>) -> JsResult<String> { | ||
let ucan = Ucan::try_from(token).map_err(|e| Error::new(&format!("{e}")))?; | ||
|
||
let hasher_code = get_hasher_code(&hasher.unwrap_or(String::from("BLAKE3-256"))); | ||
let cid = Ucan::to_cid(&ucan, hasher_code).map_err(|e| Error::new(&format!("{e}")))?; | ||
|
||
Ok(cid.into()) | ||
} | ||
|
||
fn get_hasher_code(hasher: &str) -> Code { | ||
match hasher { | ||
"SHA2-256" => Code::Sha2_256, | ||
"SHA2-512" => Code::Sha2_512, | ||
"SHA3-224" => Code::Sha3_224, | ||
"SHA3-256" => Code::Sha3_256, | ||
"SHA3-384" => Code::Sha3_384, | ||
"SHA3-512" => Code::Sha3_512, | ||
"Keccak-224" => Code::Keccak224, | ||
"Keccak-256" => Code::Keccak256, | ||
"Keccak-384" => Code::Keccak384, | ||
"Keccak-512" => Code::Keccak512, | ||
"BLAKE2b-256" => Code::Blake2b256, | ||
"BLAKE2b-512" => Code::Blake2b512, | ||
"BLAKE2s-128" => Code::Blake2s128, | ||
"BLAKE3-256" => Code::Blake3_256, | ||
_ => Code::Blake3_256, | ||
} | ||
} |
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,3 +1,4 @@ | ||
pub mod cid; | ||
pub mod token; | ||
pub mod verify; | ||
|
||
|
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,13 @@ | ||
|
||
[ | ||
{ | ||
"hasher": "SHA2-256", | ||
"token": "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCIsInVjdiI6IjAuOS4wLWNhbmFyeSJ9.eyJhdHQiOlt7ImNhbiI6ImVtYWlsL3NlbmQiLCJuYiI6bnVsbCwid2l0aCI6Im1haWx0bzphbGljZUBlbWFpbC5jb20ifV0sImF1ZCI6ImRpZDprZXk6ejZNa3RhZlpUUkVqSmt2VjVtZkp4Y0xwTkJvVlB3RExoVHVNZzluZzdkWTR6TUFMIiwiZXhwIjo5MjQ2MjExMjAwLCJmY3QiOlt7ImNoYWxsZW5nZSI6ImFiY2RlZiJ9XSwiaXNzIjoiZGlkOmtleTp6Nk1rZmZEWkNrQ1RXcmVnODg2OGZHMUZHRm9nY0pqNVg2UFk5M3BQY1dEbjlib2IiLCJwcmYiOlsiYmFma3I0aWdmM3N6N2tqNWRoeHJkanVmeHZhdmtraW5wazJpNzNpNXBzdXA2Y3h1dmR5bTJqZWN3MmUiXX0.nTJl6kKrEKYzp6D4tTc-xYgNxH4urv8tfGU7so6ZIf5s86yMnb6bLpSPMeRchbOafVIy9vil9vjjYACzY1GvBg", | ||
"cid": "bafkreigzlstpwdjhxgjexfm3njsidz3iusyys3d2way35re3gbcghjzsqa" | ||
}, | ||
{ | ||
"hasher": "BLAKE3-256", | ||
"token": "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCIsInVjdiI6IjAuOS4wLWNhbmFyeSJ9.eyJhdHQiOlt7ImNhbiI6ImVtYWlsL3NlbmQiLCJuYiI6bnVsbCwid2l0aCI6Im1haWx0bzphbGljZUBlbWFpbC5jb20ifV0sImF1ZCI6ImRpZDprZXk6ejZNa3RhZlpUUkVqSmt2VjVtZkp4Y0xwTkJvVlB3RExoVHVNZzluZzdkWTR6TUFMIiwiZXhwIjo5MjQ2MjExMjAwLCJmY3QiOlt7ImNoYWxsZW5nZSI6ImFiY2RlZiJ9XSwiaXNzIjoiZGlkOmtleTp6Nk1rZmZEWkNrQ1RXcmVnODg2OGZHMUZHRm9nY0pqNVg2UFk5M3BQY1dEbjlib2IiLCJwcmYiOlsiYmFma3I0aWdmM3N6N2tqNWRoeHJkanVmeHZhdmtraW5wazJpNzNpNXBzdXA2Y3h1dmR5bTJqZWN3MmUiXX0.nTJl6kKrEKYzp6D4tTc-xYgNxH4urv8tfGU7so6ZIf5s86yMnb6bLpSPMeRchbOafVIy9vil9vjjYACzY1GvBg", | ||
"cid": "bafkr4igg7afrwmgzugre2zxd62t3ycs73hto3pe24bavabroxbjpfam57y" | ||
} | ||
] |
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,37 @@ | ||
import assert from 'assert' | ||
import { getCIDFixture } from '../fixtures/index.js' | ||
|
||
export function runCIDTests( | ||
impl: { | ||
runner?: { describe, it }, | ||
ucan: { | ||
toCID: (token: string, hasher?: string) => Promise<string> | ||
} | ||
}) { | ||
|
||
// Use runner or fallback to implicit mocha implementations | ||
const describe = impl.runner?.describe ?? globalThis.describe | ||
const it = impl.runner?.it ?? globalThis.it | ||
|
||
const { toCID } = impl.ucan | ||
|
||
describe('toCID', async () => { | ||
it('should compute CID for a UCAN using a SHA2-256 hasher', async () => { | ||
const fixture = getCIDFixture('SHA2-256') | ||
const cid = await toCID(fixture.token, 'SHA2-256') | ||
assert.equal(cid, fixture.cid) | ||
}) | ||
|
||
it('should compute CID for a UCAN using a BLAKE3-256 hasher', async () => { | ||
const fixture = getCIDFixture('BLAKE3-256') | ||
const cid = await toCID(fixture.token, 'BLAKE3-256') | ||
assert.equal(cid, fixture.cid) | ||
}) | ||
|
||
it('should compute CID for a UCAN deafulting to the BLAKE3-256 hasher', async () => { | ||
const fixture = getCIDFixture('BLAKE3-256') | ||
const cid = await toCID(fixture.token) | ||
assert.equal(cid, fixture.cid) | ||
}) | ||
}) | ||
} |