Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CacaoZcapProof2022 #436

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ resolver = "2"
exclude = ["json-ld-api/*", "json-ld-normalization/*"]

[features]
default = ["ring"]
default = ["ring", "cacao-zcap"]
http-did = ["http", "percent-encoding"]
libsecp256k1 = ["secp256k1"] # backward compatibility
secp256k1 = ["k256", "rand", "k256/keccak256"]
Expand All @@ -23,6 +23,7 @@ ripemd-160 = ["ripemd160", "secp256k1"]
keccak = ["keccak-hash", "secp256k1", "k256/keccak256"]
sha = ["sha2", "k256/sha256"]
example-http-issuer = []
cacao-zcap = ["libipld", "percent-encoding", "uuid", "cacaos", "sha2"]

[dependencies]
json-ld = "0.4"
Expand Down Expand Up @@ -88,6 +89,11 @@ snarkvm-algorithms = { version= "0.7.9", optional = true }
snarkvm-curves = { version= "0.7.9", optional = true }
snarkvm-utilities = { version = "0.7.9", optional = true }
snarkvm-parameters = { version = "0.7.9", optional = true }
uint = "=0.9.1"
iri-string = { version = "0.4", features = ["serde", "serde-std"] }
libipld = { version = "0.12", optional = true }
uuid = { version = "0.8", features = ["v4", "serde"], optional = true }
cacaos = { version = "0.2", git = "https://github.com/spruceid/cacao-rs.git", rev = "2715184", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
chrono = { version = "0.4", features = ["serde"] }
Expand Down Expand Up @@ -118,7 +124,12 @@ uuid = { version = "0.8", features = ["v4", "serde"] }
difference = "2.0"
tokio = { version = "1.0", features = ["macros"] }
hyper = { version = "0.14", features = ["server", "http1", "stream"] }
pretty_assertions = "1.2"

[package.metadata.docs.rs]
features = ["secp256r1", "secp256k1", "ripemd-160", "http-did"]
rustdoc-args = ["--cfg", "docsrs"]

[[example]]
name = "cacao-zcap-regenerate-test-vectors"
required-features = ["cacao-zcap"]
77 changes: 77 additions & 0 deletions examples/cacao-zcap-regenerate-test-vectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use anyhow::{Context, Result};
use cacaos::siwe::Message;
use cacaos::siwe_cacao::SignInWithEthereum;
use cacaos::BasicSignature;
use cacaos::{Payload, CACAO};
use ssi::cacao_zcap::translation::cacao_to_zcap::cacao_to_zcap;
use ssi::cacao_zcap::CapabilityChainItem;
use std::convert::TryInto;
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/cacao_zcap/delegation0.siwe");
let siwe1_path = crate_dir.join("tests/cacao_zcap/delegation1.siwe");
let siwe0_sig_path = crate_dir.join("tests/cacao_zcap/delegation0.siwe.sig");
let siwe1_sig_path = crate_dir.join("tests/cacao_zcap/delegation1.siwe.sig");
let zcap0_path = crate_dir.join("tests/cacao_zcap/delegation0-zcap.jsonld");
let zcap1_path = crate_dir.join("tests/cacao_zcap/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();
}
Loading