lib.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. //! Smart contract implementing money transfers, atomic swaps, token
  19. //! minting and freezing, and staking/unstaking of consensus tokens.
  20. use darkfi_sdk::error::ContractError;
  21. /// Functions available in the contract
  22. #[repr(u8)]
  23. // ANCHOR: money-function
  24. pub enum MoneyFunction {
  25. FeeV1 = 0x00,
  26. GenesisMintV1 = 0x01,
  27. PoWRewardV1 = 0x02,
  28. TransferV1 = 0x03,
  29. OtcSwapV1 = 0x04,
  30. AuthTokenMintV1 = 0x05,
  31. AuthTokenFreezeV1 = 0x06,
  32. TokenMintV1 = 0x07,
  33. }
  34. // ANCHOR_END: money-function
  35. impl TryFrom<u8> for MoneyFunction {
  36. type Error = ContractError;
  37. fn try_from(b: u8) -> core::result::Result<Self, Self::Error> {
  38. match b {
  39. 0x00 => Ok(Self::FeeV1),
  40. 0x01 => Ok(Self::GenesisMintV1),
  41. 0x02 => Ok(Self::PoWRewardV1),
  42. 0x03 => Ok(Self::TransferV1),
  43. 0x04 => Ok(Self::OtcSwapV1),
  44. 0x05 => Ok(Self::AuthTokenMintV1),
  45. 0x06 => Ok(Self::AuthTokenFreezeV1),
  46. 0x07 => Ok(Self::TokenMintV1),
  47. _ => Err(ContractError::InvalidFunction),
  48. }
  49. }
  50. }
  51. /// Internal contract errors
  52. pub mod error;
  53. /// Call parameters definitions
  54. pub mod model;
  55. #[cfg(not(feature = "no-entrypoint"))]
  56. /// WASM entrypoint functions
  57. pub mod entrypoint;
  58. #[cfg(feature = "client")]
  59. /// Client API for interaction with this smart contract
  60. pub mod client;
  61. // These are the different sled trees that will be created
  62. pub const MONEY_CONTRACT_INFO_TREE: &str = "info";
  63. pub const MONEY_CONTRACT_COINS_TREE: &str = "coins";
  64. pub const MONEY_CONTRACT_COIN_ROOTS_TREE: &str = "coin_roots";
  65. pub const MONEY_CONTRACT_NULLIFIERS_TREE: &str = "nullifiers";
  66. pub const MONEY_CONTRACT_NULLIFIER_ROOTS_TREE: &str = "nullifier_roots";
  67. pub const MONEY_CONTRACT_TOKEN_FREEZE_TREE: &str = "token_freezes";
  68. pub const MONEY_CONTRACT_FEES_TREE: &str = "fees";
  69. // These are keys inside the info tree
  70. pub const MONEY_CONTRACT_DB_VERSION: &[u8] = b"db_version";
  71. pub const MONEY_CONTRACT_COIN_MERKLE_TREE: &[u8] = b"coins_tree";
  72. pub const MONEY_CONTRACT_LATEST_COIN_ROOT: &[u8] = b"last_coins_root";
  73. pub const MONEY_CONTRACT_LATEST_NULLIFIER_ROOT: &[u8] = b"last_nullifiers_root";
  74. /// Precalculated root hash for a tree containing only a single Fp::ZERO coin.
  75. /// Used to save gas.
  76. pub const EMPTY_COINS_TREE_ROOT: [u8; 32] = [
  77. 0xb8, 0xc1, 0x07, 0x5a, 0x80, 0xa8, 0x09, 0x65, 0xc2, 0x39, 0x8f, 0x71, 0x1f, 0xe7, 0x3e, 0x05,
  78. 0xb4, 0xed, 0xae, 0xde, 0xf1, 0x62, 0xf2, 0x61, 0xd4, 0xee, 0xd7, 0xcd, 0x72, 0x74, 0x8d, 0x17,
  79. ];
  80. /// zkas fee circuit namespace
  81. pub const MONEY_CONTRACT_ZKAS_FEE_NS_V1: &str = "Fee_V1";
  82. /// zkas mint circuit namespace
  83. pub const MONEY_CONTRACT_ZKAS_MINT_NS_V1: &str = "Mint_V1";
  84. /// zkas burn circuit namespace
  85. pub const MONEY_CONTRACT_ZKAS_BURN_NS_V1: &str = "Burn_V1";
  86. /// zkas token auth mint circuit namespace
  87. pub const MONEY_CONTRACT_ZKAS_AUTH_TOKEN_MINT_NS_V1: &str = "AuthTokenMint_V1";
  88. /// zkas token mint circuit namespace
  89. pub const MONEY_CONTRACT_ZKAS_TOKEN_MINT_NS_V1: &str = "TokenMint_V1";