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
// 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 data_encoding::BASE64URL_NOPAD;
use serde::{Deserialize, Serialize};


pub enum SerializationType {
    COMPACT,
    JSON
}


pub fn base64url_encode<T: AsRef<[u8]>>(bytes: T) -> String {
    BASE64URL_NOPAD.encode(bytes.as_ref())
}


pub fn base64url_decode<T: AsRef<[u8]>>(bytes: T) -> Vec<u8> {
    BASE64URL_NOPAD.decode(bytes.as_ref()).unwrap()
}


// Encodes a struct in base64url
pub fn base64url_encode_serializable<T: Serialize>(value: T) -> String{
    let bytes = serde_json::to_vec(&value).unwrap();
    base64url_encode(bytes)
}



/// Decodes a base64url-encoded string and deserializes it into user-defined structs.
///
/// This function takes a base64-encoded string as input and performs the following steps:
/// 1. Decodes the input from base64 encoding.
/// 2. Deserializes the resulting binary data into a user-provided struct.
pub(crate) struct Base64UrlDecodedSerializable {
    base64url_decoded: Vec<u8>
}

impl Base64UrlDecodedSerializable {
    pub fn from_serializable_values(encoded_serializable_value: impl AsRef<[u8]>) -> Self {
        Self { base64url_decoded: base64url_decode(encoded_serializable_value) }
    }

    pub fn deserialize<'a, T: Deserialize<'a>>(&'a self) -> T {
        serde_json::from_slice(&self.base64url_decoded).unwrap()
    }
}



pub struct EncondingKey {
    //TODO: family attribute implement something like this
    //             ProofAlgorithm::EdDSA => AlgorithmFamily::Ed,
    //             ProofAlgorithm::BBS_X (or BBS_BLS12381_SHA256, BBS_BLS12381_SHAKE256) => AlgorithmFamily::Bls12381
}

//TODO: implement From<Jwk> trait that transform a Jwk into and EncodingKey
//es. if crv

pub struct DecondingKey {

}