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

Update cacao-rs and testing #11

Merged
merged 4 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
repository: spruceid/cacao-rs
token: ${{ secrets.SPRUCE_CEL_PRIVATE_REPOS_2022 }}
path: cacao-rs
ref: c48b68faccb885f270b0e34f37b61c1b66d48c6a
ref: 61b02a7b0eb0b4b75c04553cbf4f045066d4f1ad

- name: Checkout ssi
uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ hex = "0.4"
anyhow = "1.0"
iri-string = { version = "0.4", features = ["serde", "serde-std"] }
percent-encoding = "2.1"
libipld = "0.12"

[dev-dependencies]
pretty_assertions = "1.2"
75 changes: 75 additions & 0 deletions examples/regenerate-test-vectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use anyhow::{Context, Result};
use cacao::siwe_cacao::SignInWithEthereum;
use cacao::BasicSignature;
use cacao::{Payload, CACAO};
use cacao_zcap::{cacao_to_zcap, CapabilityChainItem};
use siwe::Message;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;

fn read_siwe(path: &PathBuf) -> Result<Message> {
let mut file = File::open(path)?;
let mut string = String::new();
file.read_to_string(&mut string)?;
Message::from_str(&string).context("Unable to parse message")
}

fn read_siwe_sig(path: &PathBuf) -> Result<Vec<u8>> {
let mut file = File::open(path)?;
let mut string = String::new();
file.read_to_string(&mut string)?;
let (_base, sig) = multibase::decode(&format!("f{}", &string)).unwrap();
Ok(sig)
}

fn main() {
let crate_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let siwe0_path = crate_dir.join("tests/delegation0.siwe");
let siwe1_path = crate_dir.join("tests/delegation1.siwe");
let siwe0_sig_path = crate_dir.join("tests/delegation0.siwe.sig");
let siwe1_sig_path = crate_dir.join("tests/delegation1.siwe.sig");
let zcap0_path = crate_dir.join("tests/delegation0-zcap.jsonld");
let zcap1_path = crate_dir.join("tests/delegation1-zcap.jsonld");

let siwe0 = read_siwe(&siwe0_path).unwrap();
let siwe1 = read_siwe(&siwe1_path).unwrap();

// Build zcap0 from siwe0
let payload0 = Payload::from(siwe0);
let sigbytes0 = read_siwe_sig(&siwe0_sig_path).unwrap();
let sig0 = BasicSignature {
s: sigbytes0.try_into().unwrap(),
};
let cacao0 = CACAO::<SignInWithEthereum>::new(payload0, sig0);
let zcap0 = cacao_to_zcap(&cacao0).unwrap();
let zcap0_json = serde_json::to_value(&zcap0).unwrap();
let mut zcap0_out = File::create(zcap0_path).unwrap();
serde_json::to_writer_pretty(&mut zcap0_out, &zcap0_json).unwrap();
write!(zcap0_out, "\n").unwrap();

// Update siwe1 to embed zcap0.
// Update previous delegation in resources array
let parent_capability = CapabilityChainItem::Object(zcap0);
let mut payload1 = Payload::from(siwe1);
payload1.resources.pop();
payload1
.resources
.push(parent_capability.as_resource_uri().unwrap());
let siwe1: Message = payload1.clone().try_into().unwrap();
let mut siwe1_out = File::create(siwe1_path).unwrap();
write!(siwe1_out, "{}", siwe1).unwrap();

// Build zcap1 from siwe1
let sigbytes1 = read_siwe_sig(&siwe1_sig_path).unwrap();
let sig1 = BasicSignature {
s: sigbytes1.try_into().unwrap(),
};
let cacao1 = CACAO::<SignInWithEthereum>::new(payload1, sig1);
let zcap1 = cacao_to_zcap(&cacao1).unwrap();
let zcap1_json = serde_json::to_value(&zcap1).unwrap();
let mut zcap1_out = File::create(zcap1_path).unwrap();
serde_json::to_writer_pretty(&mut zcap1_out, &zcap1_json).unwrap();
write!(zcap1_out, "\n").unwrap();
}
18 changes: 13 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cacao::siwe_cacao::SignInWithEthereum;
use cacao::{Header, Payload, SignatureScheme, Version as CacaoVersion, CACAO};
use chrono::prelude::DateTime;
use iri_string::types::UriString;
use libipld::cbor::DagCbor;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use siwe::TimeStamp;
Expand Down Expand Up @@ -269,7 +270,7 @@ pub fn cacao_to_zcap<S: SignatureScheme>(
cacao: &CACAO<S>,
) -> Result<Delegation<(), CacaoZcapExtraProps>, CacaoToZcapError>
where
S::Signature: AsRef<[u8]>,
S::Signature: AsRef<[u8]> + DagCbor,
{
let header = cacao.header();
let Payload {
Expand Down Expand Up @@ -585,7 +586,7 @@ pub fn zcap_to_cacao<S: SignatureScheme>(
zcap: &Delegation<(), CacaoZcapExtraProps>,
) -> Result<CACAO<S>, ZcapToCacaoError>
where
S::Signature: TryFrom<Vec<u8>>,
S::Signature: TryFrom<Vec<u8>> + DagCbor,
{
let Delegation {
context: contexts,
Expand Down Expand Up @@ -895,14 +896,12 @@ mod tests {
use cacao::BasicSignature;
use pretty_assertions::assert_eq;
use siwe::Message;
use ssi::ldp::resolve_vm;

pub struct ExampleDIDPKH;
use async_trait::async_trait;
use ssi::did::{DIDMethod, Document};
use ssi::did_resolve::{
DIDResolver, DocumentMetadata, ResolutionInputMetadata, ResolutionMetadata,
ERROR_NOT_FOUND, TYPE_DID_LD_JSON,
DIDResolver, DocumentMetadata, ResolutionInputMetadata, ResolutionMetadata, ERROR_NOT_FOUND,
};
const EXAMPLE_DID: &str = "did:pkh:eip155:1:0x6da01670d8fc844e736095918bbe11fe8d564163";
const DOC_JSON: &str = r#"
Expand Down Expand Up @@ -1056,6 +1055,15 @@ Issued At: 2021-12-07T18:28:18.807Z"#,
let cacao = CACAO::<SignInWithEthereum>::new(message, sig);
let zcap = cacao_to_zcap(&cacao).unwrap();
let zcap_json = serde_json::to_value(&zcap).unwrap();

// Ensure last resource matches parent
let parent_expected_str = include_str!("../tests/delegation0-zcap.jsonld");
let parent_expected_json: Value = serde_json::from_str(parent_expected_str).unwrap();
let last_resource = cacao.payload().resources.iter().next_back().unwrap();
let parent_capability = CapabilityChainItem::from_resource_uri(&last_resource).unwrap();
let parent_zcap_json = serde_json::to_value(parent_capability).unwrap();
assert_eq!(parent_zcap_json, parent_expected_json);

let zcap_json_expected: Value =
serde_json::from_str(include_str!("../tests/delegation1-zcap.jsonld")).unwrap();
assert_eq!(zcap_json, zcap_json_expected);
Expand Down