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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

//! Account based permissions

use crate::from_graphql_json::FromGraphQLJson;
use mina_serialization_types_macros::AutoFrom;
use proof_systems::{bitvec::prelude::BitVec, ChunkedROInput, ToChunkedROInput};
use std::str::FromStr;
use strum::EnumString;

/// The level of auth required to perform a particular action with an account
#[derive(Clone, Debug, EnumString, AutoFrom)]
#[auto_from(mina_serialization_types::account::AuthRequired)]
pub enum AuthRequired {
    /// None required
    None,
    /// Either a proof or a signature
    Either,
    /// Proof must be provided
    Proof,
    /// Signature must be provided
    Signature,
    /// Both proof and signature must be provided
    Both,
    /// This action can never occur
    Impossible,
}

impl ToChunkedROInput for AuthRequired {
    fn to_chunked_roinput(&self) -> ChunkedROInput {
        let mut roi = ChunkedROInput::new();
        let constant = matches!(self, Self::Impossible | Self::None);
        let signature_necessary = matches!(self, Self::Impossible | Self::Signature);
        let signature_sufficient = matches!(self, Self::Either | Self::Signature | Self::None);
        for b in [constant, signature_necessary, signature_sufficient] {
            let mut bits = BitVec::with_capacity(1);
            bits.push(b);
            roi = roi.append_packed(ChunkedROInput::bits_to_fp_unsafe(bits), 1);
        }
        roi
    }
}

/// Permissions associated with the account
#[derive(Clone, Debug, AutoFrom)]
#[auto_from(mina_serialization_types::account::PermissionsLegacy)]
pub struct PermissionsLegacy {
    /// If the account can stake
    pub stake: bool,
    /// Permission required to edit state
    pub edit_state: AuthRequired,
    /// Permission required to send a balance
    pub send: AuthRequired,
    /// Permission required to receive balance
    pub receive: AuthRequired,
    /// Permission required to set the delegate
    pub set_delegate: AuthRequired,
    /// Permission required to cange permissions
    pub set_permissions: AuthRequired,
    /// Permission require to set verification key
    pub set_verification_key: AuthRequired,
}

/// Permissions associated with the account
#[derive(Clone, Debug, AutoFrom)]
#[auto_from(mina_serialization_types::account::Permissions)]
pub struct Permissions {
    /// Permission required to edit state
    pub edit_state: AuthRequired,
    /// Permission required to send a balance
    pub send: AuthRequired,
    /// Permission required to receive balance
    pub receive: AuthRequired,
    /// Permission required to set the delegate
    pub set_delegate: AuthRequired,
    /// Permission required to cange permissions
    pub set_permissions: AuthRequired,
    /// Permission require to set verification key
    pub set_verification_key: AuthRequired,
    /// Permission require to set zkapp uri
    pub set_zkapp_uri: AuthRequired,
    /// Permission require to edit sequence state
    pub edit_sequence_state: AuthRequired,
    /// Permission require to set token symbol
    pub set_token_symbol: AuthRequired,
    /// Permission require to increment nonce
    pub increment_nonce: AuthRequired,
    /// Permission require to set voting for
    pub set_voting_for: AuthRequired,
}

impl ToChunkedROInput for Permissions {
    fn to_chunked_roinput(&self) -> ChunkedROInput {
        ChunkedROInput::new()
            .append_chunked(&self.edit_state)
            .append_chunked(&self.send)
            .append_chunked(&self.receive)
            .append_chunked(&self.set_delegate)
            .append_chunked(&self.set_permissions)
            .append_chunked(&self.set_verification_key)
            .append_chunked(&self.set_zkapp_uri)
            .append_chunked(&self.edit_sequence_state)
            .append_chunked(&self.set_token_symbol)
            .append_chunked(&self.increment_nonce)
            .append_chunked(&self.set_voting_for)
    }
}

impl FromGraphQLJson for Permissions {
    fn from_graphql_json(json: &serde_json::Value) -> anyhow::Result<Self>
    where
        Self: Sized,
    {
        Ok(Self {
            edit_state: AuthRequired::from_str(json["editState"].as_str().unwrap_or_default())?,
            send: AuthRequired::from_str(json["send"].as_str().unwrap_or_default())?,
            receive: AuthRequired::from_str(json["receive"].as_str().unwrap_or_default())?,
            set_delegate: AuthRequired::from_str(json["setDelegate"].as_str().unwrap_or_default())?,
            set_permissions: AuthRequired::from_str(
                json["setPermissions"].as_str().unwrap_or_default(),
            )?,
            set_verification_key: AuthRequired::from_str(
                json["setVerificationKey"].as_str().unwrap_or_default(),
            )?,
            set_zkapp_uri: AuthRequired::from_str(
                json["setZkappUri"].as_str().unwrap_or_default(),
            )?,
            edit_sequence_state: AuthRequired::from_str(
                json["editSequenceState"].as_str().unwrap_or_default(),
            )?,
            set_token_symbol: AuthRequired::from_str(
                json["setTokenSymbol"].as_str().unwrap_or_default(),
            )?,
            increment_nonce: AuthRequired::from_str(
                json["editState"].as_str().unwrap_or_default(),
            )?,
            set_voting_for: AuthRequired::from_str(
                json["setVotingFor"].as_str().unwrap_or_default(),
            )?,
        })
    }
}

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

    #[test]
    fn permissions_from_graphql_json() -> anyhow::Result<()> {
        const JSON_STR: &str = r###"
        {
            "editSequenceState": "Signature",
            "editState": "Signature",
            "incrementNonce": "Signature",
            "receive": "None",
            "send": "Signature",
            "setDelegate": "Signature",
            "setPermissions": "Signature",
            "setTokenSymbol": "Signature",
            "setVerificationKey": "Signature",
            "setVotingFor": "Signature",
            "setZkappUri": "Signature"
          }
        "###;
        let json = serde_json::from_str(JSON_STR)?;
        _ = Permissions::from_graphql_json(&json)?;
        Ok(())
    }
}