jsonprooftoken/
encoding.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 data_encoding::BASE64URL_NOPAD;
16use serde::Serialize;
17
18pub enum SerializationType {
19    COMPACT,
20    JSON,
21}
22
23pub fn base64url_encode<T: AsRef<[u8]>>(bytes: T) -> String {
24    BASE64URL_NOPAD.encode(bytes.as_ref())
25}
26
27pub fn base64url_decode<T: AsRef<[u8]>>(bytes: T) -> Vec<u8> {
28    BASE64URL_NOPAD.decode(bytes.as_ref()).unwrap()
29}
30
31// Encodes a struct in base64url
32pub fn base64url_encode_serializable<T: Serialize>(value: T) -> String {
33    let bytes = serde_json::to_vec(&value).unwrap();
34    base64url_encode(bytes)
35}
36
37pub struct EncondingKey {
38    //TODO: family attribute implement something like this
39    //             ProofAlgorithm::EdDSA => AlgorithmFamily::Ed,
40    //             ProofAlgorithm::BBS_X (or BBS_BLS12381_SHA256, BBS_BLS12381_SHAKE256) => AlgorithmFamily::Bls12381
41}
42
43//TODO: implement From<Jwk> trait that transform a Jwk into and EncodingKey
44//es. if crv
45
46pub struct DecondingKey {}