1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2023 Fondazione LINKS

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.




use std::collections::HashMap;

use jsonprooftoken::{jpt::claims::JptClaims, jwp::{header::{IssuerProtectedHeader, PresentationProtectedHeader}, issued::{JwpIssued, JwpIssuedBuilder, JwpIssuedDecoder}, presented::{JwpPresented, JwpPresentedBuilder, JwpPresentedDecoder}}, jpa::algs::ProofAlgorithm, encoding::{base64url_encode, SerializationType}, jwk::{key::Jwk, types::KeyPairSubtype, alg_parameters::JwkAlgorithmParameters}};
use serde::Serialize;
use serde_json::{Value, json};


fn main() {

    let custom_claims = serde_json::json!({
        "degree": {
            "type": "BachelorDegree",
            "name": "Bachelor of Science and Arts",
            "ciao": [
                {"u1": "value1"}, 
                {"u2": "value2"}
                ]
            },
        "name": "John Doe"
    });


    let mut jpt_claims = JptClaims::new();
    jpt_claims.set_iss("https://issuer.example".to_owned());
    jpt_claims.set_claim(Some("vc"), custom_claims, true);
    

    let issued_header = IssuerProtectedHeader::new(ProofAlgorithm::BLS12381_SHA256);

    let bbs_jwk = Jwk::generate(KeyPairSubtype::BLS12381SHA256).unwrap();
    println!("\nBBS Jwk:\n {:#}", serde_json::to_string_pretty(&bbs_jwk).unwrap());

    let issued_jwp = JwpIssuedBuilder::new()
    .issuer_protected_header(issued_header)
    .jpt_claims(jpt_claims)
    .build(&bbs_jwk)
    .unwrap();

    let compact_issued_jwp = issued_jwp.encode(SerializationType::COMPACT).unwrap();
    println!("\nCompact Issued JWP: {}", compact_issued_jwp);


    let decoded_issued_jwp = JwpIssuedDecoder::decode(&compact_issued_jwp, SerializationType::COMPACT).unwrap()
    .verify(&bbs_jwk.to_public().unwrap())
    .unwrap();


    assert_eq!(issued_jwp, decoded_issued_jwp);

    let mut presentation_header = PresentationProtectedHeader::new(decoded_issued_jwp.get_issuer_protected_header().alg().into());
    presentation_header.set_aud(Some("https://recipient.example.com".to_owned()));
    presentation_header.set_nonce(Some("wrmBRkKtXjQ".to_owned()));


    let presented_jwp = JwpPresentedBuilder::new(&decoded_issued_jwp)
    .presentation_protected_header(presentation_header)
    .set_undisclosed("vc.degree.name").unwrap()
    .set_undisclosed("vc.degree.ciao[0].u1").unwrap()
    .set_undisclosed("vc.name").unwrap()
    .build(&bbs_jwk.to_public().unwrap())
    .unwrap();
    

    let compact_presented_jwp = presented_jwp.encode(SerializationType::COMPACT).unwrap();

    println!("\nCompact Presented JWP: {}", compact_presented_jwp);


    let _decoded_presented_jwp = JwpPresentedDecoder::decode(&compact_presented_jwp, SerializationType::COMPACT).unwrap()
    .verify(&bbs_jwk.to_public().unwrap())
    .unwrap();


}