2
2
3
3
use std:: str:: FromStr ;
4
4
5
+ use masp_primitives:: asset_type:: AssetType ;
5
6
use masp_primitives:: bls12_381:: Scalar ;
6
- use masp_primitives:: sapling:: Nullifier ;
7
+ use masp_primitives:: sapling:: { Node , Nullifier } ;
7
8
use namada_core:: address:: { self , Address } ;
8
9
use namada_core:: hash:: Hash ;
9
10
use namada_core:: storage:: { self , DbKeySeg , KeySeg } ;
@@ -15,6 +16,8 @@ const BALANCE_STORAGE_KEY: &str = "balance";
15
16
pub const MASP_NULLIFIERS_KEY : & str = "nullifiers" ;
16
17
/// The key for the masp reward balance
17
18
pub const MASP_UNDATED_BALANCE_KEY : & str = "undated_balance" ;
19
+ /// Key segment prefix for the conversions
20
+ pub const MASP_CONVERSIONS_KEY : & str = "conversions" ;
18
21
/// Key segment prefix for the note commitment merkle tree
19
22
pub const MASP_NOTE_COMMITMENT_TREE_KEY : & str = "commitment_tree" ;
20
23
/// Key segment prefix for the note commitment anchor
@@ -141,21 +144,26 @@ pub fn is_masp_key(key: &storage::Key) -> bool {
141
144
}
142
145
}
143
146
147
+ /// Check if the given storage key is allowed to be touched by a governance
148
+ /// proposal
149
+ pub fn is_masp_governance_key ( key : & storage:: Key ) -> bool {
150
+ is_masp_token_map_key ( key) || is_masp_conversion_key ( key) . is_some ( )
151
+ }
152
+
144
153
/// Check if the given storage key is allowed to be touched by a masp transfer
145
154
pub fn is_masp_transfer_key ( key : & storage:: Key ) -> bool {
146
- match & key. segments [ ..] {
155
+ is_masp_commitment_tree_key ( key)
156
+ || is_masp_nullifier_key ( key)
157
+ || is_masp_balance_key ( key)
158
+ || is_masp_undated_balance_key ( key) . is_some ( )
159
+ }
160
+
161
+ /// Check if the given storage key is a masp commitment tree key
162
+ pub fn is_masp_commitment_tree_key ( key : & storage:: Key ) -> bool {
163
+ matches ! ( & key. segments[ ..] ,
147
164
[ DbKeySeg :: AddressSeg ( addr) , DbKeySeg :: StringSeg ( key) ]
148
165
if * addr == address:: MASP
149
- && key == MASP_NOTE_COMMITMENT_TREE_KEY =>
150
- {
151
- true
152
- }
153
- _ => {
154
- is_masp_nullifier_key ( key)
155
- || is_masp_balance_key ( key)
156
- || is_masp_undated_balance_key ( key) . is_some ( )
157
- }
158
- }
166
+ && key == MASP_NOTE_COMMITMENT_TREE_KEY )
159
167
}
160
168
161
169
/// Check if the given storage key is a masp nullifier key
@@ -168,12 +176,19 @@ pub fn is_masp_nullifier_key(key: &storage::Key) -> bool {
168
176
}
169
177
170
178
/// Check if the given key is a masp commitment anchor
171
- pub fn is_masp_commitment_anchor_key ( key : & storage:: Key ) -> bool {
172
- matches ! ( & key. segments[ ..] ,
173
- [ DbKeySeg :: AddressSeg ( addr) ,
174
- DbKeySeg :: StringSeg ( prefix) ,
175
- ..
176
- ] if * addr == address:: MASP && prefix == MASP_NOTE_COMMITMENT_ANCHOR_PREFIX )
179
+ pub fn is_masp_commitment_anchor_key ( key : & storage:: Key ) -> Option < Node > {
180
+ match & key. segments [ ..] {
181
+ [
182
+ DbKeySeg :: AddressSeg ( addr) ,
183
+ DbKeySeg :: StringSeg ( prefix) ,
184
+ DbKeySeg :: StringSeg ( anchor) ,
185
+ ] if * addr == address:: MASP
186
+ && prefix == MASP_NOTE_COMMITMENT_ANCHOR_PREFIX =>
187
+ {
188
+ Hash :: from_str ( anchor) . map ( |x| Node :: new ( x. 0 ) ) . ok ( )
189
+ }
190
+ _ => None ,
191
+ }
177
192
}
178
193
179
194
/// Check if the given storage key is a masp token map key
@@ -184,6 +199,20 @@ pub fn is_masp_token_map_key(key: &storage::Key) -> bool {
184
199
] if * addr == address:: MASP && prefix == MASP_TOKEN_MAP_KEY )
185
200
}
186
201
202
+ /// Check if the given storage key is a masp nullifier key
203
+ pub fn is_masp_conversion_key ( key : & storage:: Key ) -> Option < AssetType > {
204
+ match & key. segments [ ..] {
205
+ [
206
+ DbKeySeg :: AddressSeg ( address:: MASP ) ,
207
+ DbKeySeg :: StringSeg ( prefix) ,
208
+ DbKeySeg :: StringSeg ( asset_type) ,
209
+ ] if prefix == MASP_CONVERSIONS_KEY => {
210
+ AssetType :: from_str ( asset_type) . ok ( )
211
+ }
212
+ _ => None ,
213
+ }
214
+ }
215
+
187
216
/// Get a key for a masp nullifier
188
217
pub fn masp_nullifier_key ( nullifier : & Nullifier ) -> storage:: Key {
189
218
storage:: Key :: from ( address:: MASP . to_db_key ( ) )
@@ -193,6 +222,22 @@ pub fn masp_nullifier_key(nullifier: &Nullifier) -> storage::Key {
193
222
. expect ( "Cannot obtain a storage key" )
194
223
}
195
224
225
+ /// Get a key for a masp conversion
226
+ pub fn masp_conversion_key ( asset_type : & AssetType ) -> storage:: Key {
227
+ storage:: Key :: from ( address:: MASP . to_db_key ( ) )
228
+ . push ( & MASP_CONVERSIONS_KEY . to_owned ( ) )
229
+ . expect ( "Cannot obtain a storage key" )
230
+ . push ( & asset_type. to_string ( ) )
231
+ . expect ( "Cannot obtain a storage key" )
232
+ }
233
+
234
+ /// Get the key prefix for masp conversions
235
+ pub fn masp_conversion_key_prefix ( ) -> storage:: Key {
236
+ storage:: Key :: from ( address:: MASP . to_db_key ( ) )
237
+ . push ( & MASP_CONVERSIONS_KEY . to_owned ( ) )
238
+ . expect ( "Cannot obtain a storage key" )
239
+ }
240
+
196
241
/// Get the key for the masp commitment tree
197
242
pub fn masp_commitment_tree_key ( ) -> storage:: Key {
198
243
storage:: Key :: from ( address:: MASP . to_db_key ( ) )
0 commit comments