darkfi_money_contract/model/
mod.rs
1use darkfi_sdk::{
20 crypto::{
21 note::AeadEncryptedNote, pasta_prelude::PrimeField, poseidon_hash, BaseBlind, FuncId,
22 MerkleNode, PublicKey, ScalarBlind,
23 },
24 error::ContractError,
25 pasta::pallas,
26};
27use darkfi_serial::{SerialDecodable, SerialEncodable};
28
29#[cfg(feature = "client")]
30use darkfi_serial::async_trait;
31
32pub mod nullifier;
34pub use nullifier::Nullifier;
35
36pub mod token_id;
38pub use token_id::{TokenId, DARK_TOKEN_ID};
39
40#[derive(Debug, Clone, Copy, Eq, PartialEq, SerialEncodable, SerialDecodable)]
42pub struct Coin(pallas::Base);
43
44impl Coin {
45 pub fn inner(&self) -> pallas::Base {
47 self.0
48 }
49
50 pub fn from_bytes(x: [u8; 32]) -> Result<Self, ContractError> {
53 match pallas::Base::from_repr(x).into() {
54 Some(v) => Ok(Self(v)),
55 None => {
56 Err(ContractError::IoError("Failed to instantiate Coin from bytes".to_string()))
57 }
58 }
59 }
60
61 pub fn to_bytes(&self) -> [u8; 32] {
63 self.0.to_repr()
64 }
65}
66
67use core::str::FromStr;
68darkfi_sdk::fp_from_bs58!(Coin);
69darkfi_sdk::fp_to_bs58!(Coin);
70darkfi_sdk::ty_from_fp!(Coin);
71
72#[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
73pub struct CoinAttributes {
75 pub public_key: PublicKey,
76 pub value: u64,
77 pub token_id: TokenId,
78 pub spend_hook: FuncId,
79 pub user_data: pallas::Base,
80 pub blind: BaseBlind,
82}
83impl CoinAttributes {
86 pub fn to_coin(&self) -> Coin {
87 let (pub_x, pub_y) = self.public_key.xy();
88 let coin = poseidon_hash([
89 pub_x,
90 pub_y,
91 pallas::Base::from(self.value),
92 self.token_id.inner(),
93 self.spend_hook.inner(),
94 self.user_data,
95 self.blind.inner(),
96 ]);
97 Coin(coin)
98 }
99}
100
101#[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
102pub struct TokenAttributes {
103 pub auth_parent: FuncId,
104 pub user_data: pallas::Base,
105 pub blind: BaseBlind,
106}
107
108impl TokenAttributes {
109 pub fn to_token_id(&self) -> TokenId {
110 let token_id =
111 poseidon_hash([self.auth_parent.inner(), self.user_data, self.blind.inner()]);
112 TokenId::from(token_id)
113 }
114}
115
116#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
117pub struct ClearInput {
120 pub value: u64,
122 pub token_id: TokenId,
124 pub value_blind: ScalarBlind,
126 pub token_blind: BaseBlind,
128 pub signature_public: PublicKey,
130}
131#[derive(Clone, Debug, PartialEq, SerialEncodable, SerialDecodable)]
134pub struct Input {
137 pub value_commit: pallas::Point,
139 pub token_commit: pallas::Base,
141 pub nullifier: Nullifier,
143 pub merkle_root: MerkleNode,
145 pub user_data_enc: pallas::Base,
149 pub signature_public: PublicKey,
151}
152#[derive(Clone, Debug, PartialEq, SerialEncodable, SerialDecodable)]
155pub struct Output {
158 pub value_commit: pallas::Point,
160 pub token_commit: pallas::Base,
162 pub coin: Coin,
164 pub note: AeadEncryptedNote,
166}
167#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
171pub struct MoneyFeeParamsV1 {
172 pub input: Input,
174 pub output: Output,
176 pub fee_value_blind: ScalarBlind,
178 pub token_blind: BaseBlind,
180}
181
182#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
184pub struct MoneyFeeUpdateV1 {
185 pub nullifier: Nullifier,
187 pub coin: Coin,
189 pub height: u32,
191 pub fee: u64,
193}
194
195#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
196pub struct MoneyTransferParamsV1 {
199 pub inputs: Vec<Input>,
201 pub outputs: Vec<Output>,
203}
204#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
208pub struct MoneyTransferUpdateV1 {
209 pub nullifiers: Vec<Nullifier>,
211 pub coins: Vec<Coin>,
213}
214
215#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
217pub struct MoneyGenesisMintParamsV1 {
218 pub input: ClearInput,
220 pub outputs: Vec<Output>,
222}
223
224#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
226pub struct MoneyGenesisMintUpdateV1 {
227 pub coins: Vec<Coin>,
229}
230
231#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
233pub struct MoneyTokenMintParamsV1 {
234 pub coin: Coin,
236}
237
238#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
240pub struct MoneyTokenMintUpdateV1 {
241 pub coin: Coin,
243}
244
245#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
247pub struct MoneyAuthTokenMintParamsV1 {
248 pub token_id: TokenId,
249 pub enc_note: AeadEncryptedNote,
250 pub mint_pubkey: PublicKey,
251}
252
253#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
255pub struct MoneyAuthTokenMintUpdateV1 {}
256
257#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
259pub struct MoneyAuthTokenFreezeParamsV1 {
260 pub mint_public: PublicKey,
264 pub token_id: TokenId,
265}
266
267#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
269pub struct MoneyAuthTokenFreezeUpdateV1 {
270 pub token_id: TokenId,
271}
272
273#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
275pub struct MoneyPoWRewardParamsV1 {
276 pub input: ClearInput,
278 pub output: Output,
280}
281
282#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
284pub struct MoneyPoWRewardUpdateV1 {
285 pub coin: Coin,
287 pub height: u32,
289}