Skip to main content

midnight_coin_structure/
contract.rs

1// This file is part of midnight-ledger.
2// Copyright (C) Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14use crate::coin::{ShieldedTokenType, UnshieldedTokenType};
15use crate::hash_serde;
16use base_crypto::hash::{HashOutput, persistent_commit};
17use base_crypto::repr::{BinaryHashRepr, MemWrite};
18use fake::Dummy;
19#[cfg(feature = "proptest")]
20use proptest_derive::Arbitrary;
21use serialize::{self, Deserializable, Serializable, Tagged, tag_enforcement_test};
22use storage::Storable;
23use storage::arena::ArenaKey;
24use storage::db::DB;
25use storage::storable::Loader;
26use transient_crypto::curve::Fr;
27use transient_crypto::repr::{FieldRepr, FromFieldRepr};
28
29#[derive(
30    Debug,
31    Default,
32    Copy,
33    Clone,
34    Hash,
35    PartialEq,
36    Eq,
37    PartialOrd,
38    Ord,
39    FieldRepr,
40    FromFieldRepr,
41    BinaryHashRepr,
42    Serializable,
43    Storable,
44    Dummy,
45)]
46#[storable(base)]
47#[tag = "contract-address[v2]"]
48#[cfg_attr(feature = "proptest", derive(Arbitrary))]
49pub struct ContractAddress(pub HashOutput);
50serialize::encoding_shape_as!(ContractAddress, [u8; 32]);
51tag_enforcement_test!(ContractAddress);
52
53impl rand::distributions::Distribution<ContractAddress> for rand::distributions::Standard {
54    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> ContractAddress {
55        ContractAddress(rng.r#gen())
56    }
57}
58
59impl ContractAddress {
60    fn custom_token_type(&self, domain_sep: HashOutput) -> HashOutput {
61        let inner_domain_sep = HashOutput(*b"midnight:derive_token\0\0\0\0\0\0\0\0\0\0\0");
62        persistent_commit(&(domain_sep, self.0), inner_domain_sep)
63    }
64
65    pub fn custom_shielded_token_type(&self, domain_sep: HashOutput) -> ShieldedTokenType {
66        ShieldedTokenType(self.custom_token_type(domain_sep))
67    }
68
69    pub fn custom_unshielded_token_type(&self, domain_sep: HashOutput) -> UnshieldedTokenType {
70        UnshieldedTokenType(self.custom_token_type(domain_sep))
71    }
72}
73
74#[cfg(feature = "proptest")]
75serialize::randomised_serialization_test!(ContractAddress);
76hash_serde!(ContractAddress);