diem_types/account_config/constants/
diem.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::account_config::constants::CORE_CODE_ADDRESS;
9use anyhow::{bail, Result};
10use move_core_types::{
11    identifier::Identifier,
12    language_storage::{ModuleId, StructTag, TypeTag},
13};
14use once_cell::sync::Lazy;
15
16pub const DIEM_MODULE_NAME: &str = "Diem";
17static COIN_MODULE_NAME: Lazy<Identifier> =
18    Lazy::new(|| Identifier::new("Diem").unwrap());
19pub static COIN_MODULE: Lazy<ModuleId> =
20    Lazy::new(|| ModuleId::new(CORE_CODE_ADDRESS, COIN_MODULE_NAME.clone()));
21
22// TODO: This imposes a few implied restrictions:
23//   1) The currency module must be published under the core code address.
24//   2) The module name must be the same as the gas specifier.
25//   3) The struct name must be the same as the module name
26// We need to consider whether we want to switch to a more or fully qualified
27// name.
28pub fn type_tag_for_currency_code(currency_code: Identifier) -> TypeTag {
29    TypeTag::Struct(StructTag {
30        address: CORE_CODE_ADDRESS,
31        module: currency_code.clone(),
32        name: currency_code,
33        type_params: vec![],
34    })
35}
36
37/// In addition to the constraints for valid Move identifiers, currency codes
38/// should consist entirely of uppercase alphanumeric characters (e.g., no
39/// underscores).
40pub fn allowed_currency_code_string(
41    possible_currency_code_string: &str,
42) -> bool {
43    possible_currency_code_string
44        .chars()
45        .all(|chr| matches!(chr, 'A'..='Z' | '0'..='9'))
46        && Identifier::is_valid(possible_currency_code_string)
47}
48
49pub fn from_currency_code_string(
50    currency_code_string: &str,
51) -> Result<Identifier> {
52    if !allowed_currency_code_string(currency_code_string) {
53        bail!("Invalid currency code '{}'", currency_code_string)
54    }
55    Identifier::new(currency_code_string)
56}