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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2024 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use darkfi_sdk::{
crypto::{
note::AeadEncryptedNote, pasta_prelude::PrimeField, poseidon_hash, BaseBlind, FuncId,
MerkleNode, PublicKey, ScalarBlind, SecretKey,
},
error::ContractError,
pasta::pallas,
};
use darkfi_serial::{SerialDecodable, SerialEncodable};
#[cfg(feature = "client")]
use darkfi_serial::async_trait;
/// Nullifier definitions
pub mod nullifier;
pub use nullifier::Nullifier;
/// Token ID definitions and methods
pub mod token_id;
pub use token_id::{TokenId, DARK_TOKEN_ID};
/// A `Coin` represented in the Money state
#[derive(Debug, Clone, Copy, Eq, PartialEq, SerialEncodable, SerialDecodable)]
pub struct Coin(pallas::Base);
impl Coin {
/// Reference the raw inner base field element
pub fn inner(&self) -> pallas::Base {
self.0
}
/// Create a `Coin` object from given bytes, erroring if the input
/// bytes are noncanonical.
pub fn from_bytes(x: [u8; 32]) -> Result<Self, ContractError> {
match pallas::Base::from_repr(x).into() {
Some(v) => Ok(Self(v)),
None => {
Err(ContractError::IoError("Failed to instantiate Coin from bytes".to_string()))
}
}
}
/// Convert the `Coin` type into 32 raw bytes
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_repr()
}
}
use core::str::FromStr;
darkfi_sdk::fp_from_bs58!(Coin);
darkfi_sdk::fp_to_bs58!(Coin);
darkfi_sdk::ty_from_fp!(Coin);
#[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
// ANCHOR: coin-attributes
pub struct CoinAttributes {
pub public_key: PublicKey,
pub value: u64,
pub token_id: TokenId,
pub spend_hook: FuncId,
pub user_data: pallas::Base,
/// Simultaneously blinds the coin and ensures uniqueness
pub blind: BaseBlind,
}
// ANCHOR_END: coin-attributes
impl CoinAttributes {
pub fn to_coin(&self) -> Coin {
let (pub_x, pub_y) = self.public_key.xy();
let coin = poseidon_hash([
pub_x,
pub_y,
pallas::Base::from(self.value),
self.token_id.inner(),
self.spend_hook.inner(),
self.user_data,
self.blind.inner(),
]);
Coin(coin)
}
}
#[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
pub struct TokenAttributes {
pub auth_parent: FuncId,
pub user_data: pallas::Base,
pub blind: BaseBlind,
}
impl TokenAttributes {
pub fn to_token_id(&self) -> TokenId {
let token_id =
poseidon_hash([self.auth_parent.inner(), self.user_data, self.blind.inner()]);
TokenId::from(token_id)
}
}
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
// ANCHOR: money-clear-input
/// A contract call's clear input
pub struct ClearInput {
/// Input's value (amount)
pub value: u64,
/// Input's token ID
pub token_id: TokenId,
/// Blinding factor for `value`
pub value_blind: ScalarBlind,
/// Blinding factor for `token_id`
pub token_blind: BaseBlind,
/// Public key for the signature
pub signature_public: PublicKey,
}
// ANCHOR_END: money-clear-input
#[derive(Clone, Debug, PartialEq, SerialEncodable, SerialDecodable)]
// ANCHOR: money-input
/// A contract call's anonymous input
pub struct Input {
/// Pedersen commitment for the input's value
pub value_commit: pallas::Point,
/// Commitment for the input's token ID
pub token_commit: pallas::Base,
/// Revealed nullifier
pub nullifier: Nullifier,
/// Revealed Merkle root
pub merkle_root: MerkleNode,
/// Encrypted user data field. An encrypted commitment to arbitrary data.
/// When spend hook is nonzero, then this field may be used to pass data
/// to the invoked contract.
pub user_data_enc: pallas::Base,
/// Public key for the signature
pub signature_public: PublicKey,
}
// ANCHOR_END: money-input
#[derive(Clone, Debug, PartialEq, SerialEncodable, SerialDecodable)]
// ANCHOR: money-output
/// A contract call's anonymous output
pub struct Output {
/// Pedersen commitment for the output's value
pub value_commit: pallas::Point,
/// Commitment for the output's token ID
pub token_commit: pallas::Base,
/// Minted coin
pub coin: Coin,
/// AEAD encrypted note
pub note: AeadEncryptedNote,
}
// ANCHOR_END: money-output
/// Parameters for `Money::Fee`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyFeeParamsV1 {
/// Anonymous input
pub input: Input,
/// Anonymous outputs
pub output: Output,
/// Fee value blind
pub fee_value_blind: ScalarBlind,
/// Token ID blind
pub token_blind: BaseBlind,
}
/// State update for `Money::Fee`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyFeeUpdateV1 {
/// Revealed nullifier
pub nullifier: Nullifier,
/// Minted coin
pub coin: Coin,
/// Block height the fee was verified against
pub height: u32,
/// Height accumulated fee paid
pub fee: u64,
}
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
// ANCHOR: money-params
/// Parameters for `Money::Transfer` and `Money::OtcSwap`
pub struct MoneyTransferParamsV1 {
/// Anonymous inputs
pub inputs: Vec<Input>,
/// Anonymous outputs
pub outputs: Vec<Output>,
}
// ANCHOR_END: money-params
/// State update for `Money::Transfer` and `Money::OtcSwap`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyTransferUpdateV1 {
/// Revealed nullifiers
pub nullifiers: Vec<Nullifier>,
/// Minted coins
pub coins: Vec<Coin>,
}
/// Parameters for `Money::GenesisMint`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyGenesisMintParamsV1 {
/// Clear input
pub input: ClearInput,
/// Anonymous output
pub output: Output,
}
/// State update for `Money::GenesisMint`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyGenesisMintUpdateV1 {
/// The newly minted coin
pub coin: Coin,
}
/// Parameters for `Money::TokenMint`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyTokenMintParamsV1 {
/// The newly minted coin
pub coin: Coin,
}
/// State update for `Money::TokenMint`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyTokenMintUpdateV1 {
/// The newly minted coin
pub coin: Coin,
}
/// Parameters for `Money::AuthTokenMint`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyAuthTokenMintParamsV1 {
pub token_id: TokenId,
pub enc_note: AeadEncryptedNote,
pub mint_pubkey: PublicKey,
}
/// State update for `Money::AuthTokenMint`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyAuthTokenMintUpdateV1 {}
/// Parameters for `Money::AuthTokenFreeze`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyAuthTokenFreezeParamsV1 {
/// Mint authority public key
///
/// We use this to derive the token ID and verify the signature.
pub mint_public: PublicKey,
pub token_id: TokenId,
}
/// State update for `Money::AuthTokenFreeze`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyAuthTokenFreezeUpdateV1 {
pub token_id: TokenId,
}
/// Parameters for `Money::PoWReward`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyPoWRewardParamsV1 {
/// Clear input
pub input: ClearInput,
/// Anonymous output
pub output: Output,
}
/// State update for `Money::PoWReward`
#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
pub struct MoneyPoWRewardUpdateV1 {
/// The newly minted coin
pub coin: Coin,
/// Block height the call was verified against
pub height: u32,
}