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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Aleo SDK library.

// The Aleo SDK library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Aleo SDK library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.

mod credits;
pub use credits::*;

use crate::types::native::{FromBytes, ProvingKeyNative, ToBytes};

use sha2::Digest;
use wasm_bindgen::prelude::wasm_bindgen;

use std::{ops::Deref, str::FromStr};

/// Proving key for a function within an Aleo program
#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct ProvingKey(ProvingKeyNative);

#[wasm_bindgen]
impl ProvingKey {
    /// Return the checksum of the proving key
    ///
    /// @returns {string} Checksum of the proving key
    pub fn checksum(&self) -> String {
        hex::encode(sha2::Sha256::digest(self.to_bytes().unwrap()))
    }

    /// Create a copy of the proving key
    ///
    /// @returns {ProvingKey} A copy of the proving key
    #[wasm_bindgen]
    pub fn copy(&self) -> ProvingKey {
        self.0.clone().into()
    }

    /// Construct a new proving key from a byte array
    ///
    /// @param {Uint8Array} bytes Byte array representation of a proving key
    /// @returns {ProvingKey | Error}
    #[wasm_bindgen(js_name = "fromBytes")]
    pub fn from_bytes(bytes: &[u8]) -> Result<ProvingKey, String> {
        Ok(Self(ProvingKeyNative::from_bytes_le(bytes).map_err(|e| e.to_string())?))
    }

    /// Create a proving key from string
    ///
    /// @param {string | Error} String representation of the proving key
    #[wasm_bindgen(js_name = "fromString")]
    pub fn from_string(string: &str) -> Result<ProvingKey, String> {
        Ok(Self(ProvingKeyNative::from_str(string).map_err(|e| e.to_string())?))
    }

    /// Return the byte representation of a proving key
    ///
    /// @returns {Uint8Array | Error} Byte array representation of a proving key
    #[wasm_bindgen(js_name = "toBytes")]
    pub fn to_bytes(&self) -> Result<Vec<u8>, String> {
        self.0.to_bytes_le().map_err(|_| "Failed to serialize proving key".to_string())
    }

    /// Get a string representation of the proving key
    ///
    /// @returns {string} String representation of the proving key
    #[wasm_bindgen(js_name = "toString")]
    #[allow(clippy::inherent_to_string)]
    pub fn to_string(&self) -> String {
        format!("{:?}", self.0)
    }
}

impl Deref for ProvingKey {
    type Target = ProvingKeyNative;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl From<ProvingKey> for ProvingKeyNative {
    fn from(proving_key: ProvingKey) -> ProvingKeyNative {
        proving_key.0
    }
}

impl From<ProvingKeyNative> for ProvingKey {
    fn from(proving_key: ProvingKeyNative) -> ProvingKey {
        ProvingKey(proving_key)
    }
}

impl PartialEq for ProvingKey {
    fn eq(&self, other: &Self) -> bool {
        *self.0 == *other.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wasm_bindgen_test::*;

    const TRANSFER_PUBLIC_PROVER: &str = "https://testnet3.parameters.aleo.org/transfer_public.prover.a74565e";

    #[wasm_bindgen_test]
    async fn test_proving_key_roundtrip() {
        let fee_proving_key_bytes = reqwest::get(TRANSFER_PUBLIC_PROVER).await.unwrap().bytes().await.unwrap().to_vec();
        let fee_proving_key = ProvingKey::from_bytes(&fee_proving_key_bytes).unwrap();
        let bytes = fee_proving_key.to_bytes().unwrap();
        assert_eq!(bytes, fee_proving_key_bytes);
    }

    #[wasm_bindgen_test]
    async fn test_from_string() {
        let transfer_public_prover =
            reqwest::get(TRANSFER_PUBLIC_PROVER).await.unwrap().bytes().await.unwrap().to_vec();
        let transfer_public_proving_key = ProvingKey::from_bytes(&transfer_public_prover).unwrap();
        let transfer_public_proving_key_string = transfer_public_proving_key.to_string();
        let transfer_public_proving_key_from_string =
            ProvingKey::from_string(&transfer_public_proving_key_string).unwrap();
        assert_eq!(transfer_public_proving_key, transfer_public_proving_key_from_string);
    }

    #[wasm_bindgen_test]
    async fn test_prover_checksum() {
        let transfer_public_prover =
            reqwest::get(TRANSFER_PUBLIC_PROVER).await.unwrap().bytes().await.unwrap().to_vec();
        let transfer_public_proving_key = ProvingKey::from_bytes(&transfer_public_prover).unwrap();
        let transfer_public_proving_key_checksum = transfer_public_proving_key.checksum();
        assert_eq!(
            transfer_public_proving_key_checksum,
            "a74565e4fd408a90b2d04b0e6c0dea6bf0ab6a27926ef28049da62d18727f6c6"
        );
    }
}