jpt/
jpt.rs

1// Copyright 2023 Fondazione LINKS
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7//     http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use jsonprooftoken::{
16    encoding::SerializationType,
17    jpa::algs::ProofAlgorithm,
18    jpt::claims::JptClaims,
19    jwk::{key::Jwk, types::KeyPairSubtype},
20    jwp::{
21        header::{IssuerProtectedHeader, PresentationProtectedHeader},
22        issued::{JwpIssuedBuilder, JwpIssuedDecoder},
23        presented::{JwpPresentedBuilder, JwpPresentedDecoder},
24    },
25};
26
27fn main() {
28    let custom_claims = serde_json::json!({
29        "degree": {
30            "type": "BachelorDegree",
31            "name": "Bachelor of Science and Arts",
32            "ciao": [
33                {"u1": "value1"},
34                {"u2": "value2"}
35                ]
36            },
37        "name": "John Doe"
38    });
39
40    let mut jpt_claims = JptClaims::new();
41    jpt_claims.set_iss("https://issuer.example".to_owned());
42    jpt_claims.set_claim(Some("vc"), custom_claims, true);
43
44    let issued_header = IssuerProtectedHeader::new(ProofAlgorithm::BBS);
45
46    let bbs_jwk = Jwk::generate(KeyPairSubtype::BLS12381G2Sha256).unwrap();
47    println!(
48        "\nBBS Jwk:\n {:#}",
49        serde_json::to_string_pretty(&bbs_jwk).unwrap()
50    );
51
52    let issued_jwp = JwpIssuedBuilder::new(issued_header, jpt_claims)
53        .build(&bbs_jwk)
54        .unwrap();
55
56    let compact_issued_jwp = issued_jwp.encode(SerializationType::COMPACT).unwrap();
57    println!("\nCompact Issued JWP: {}", compact_issued_jwp);
58
59    let decoded_issued_jwp =
60        JwpIssuedDecoder::decode(&compact_issued_jwp, SerializationType::COMPACT)
61            .unwrap()
62            .verify(&bbs_jwk.to_public().unwrap())
63            .unwrap();
64
65    assert_eq!(issued_jwp, decoded_issued_jwp);
66
67    let mut presentation_header = PresentationProtectedHeader::new(
68        decoded_issued_jwp
69            .get_issuer_protected_header()
70            .alg()
71            .into(),
72    );
73    presentation_header.set_aud(Some("https://recipient.example.com".to_owned()));
74    presentation_header.set_nonce(Some("wrmBRkKtXjQ".to_owned()));
75
76    let presented_jwp = JwpPresentedBuilder::new(&decoded_issued_jwp)
77        .set_presentation_protected_header(presentation_header)
78        .set_undisclosed("vc.degree.name")
79        .unwrap()
80        .set_undisclosed("vc.degree.ciao[0].u1")
81        .unwrap()
82        .set_undisclosed("vc.name")
83        .unwrap()
84        .build(&bbs_jwk.to_public().unwrap())
85        .unwrap();
86
87    let compact_presented_jwp = presented_jwp.encode(SerializationType::COMPACT).unwrap();
88
89    println!("\nCompact Presented JWP: {}", compact_presented_jwp);
90
91    let _decoded_presented_jwp =
92        JwpPresentedDecoder::decode(&compact_presented_jwp, SerializationType::COMPACT)
93            .unwrap()
94            .verify(&bbs_jwk.to_public().unwrap())
95            .unwrap();
96}