darkfi_money_contract/model/
mod.rs1use 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 pub tx_local: bool,
153}
154#[derive(Clone, Debug, PartialEq, SerialEncodable, SerialDecodable)]
157pub struct Output {
160 pub value_commit: pallas::Point,
162 pub token_commit: pallas::Base,
164 pub coin: Coin,
166 pub note: AeadEncryptedNote,
168 pub tx_local: bool,
170}
171#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
175pub struct MoneyFeeParamsV1 {
176 pub input: Input,
178 pub output: Output,
180 pub fee_value_blind: ScalarBlind,
182 pub token_blind: BaseBlind,
184}
185
186#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
188pub struct MoneyFeeUpdateV1 {
189 pub nullifier: Nullifier,
191 pub coin: Coin,
193 pub tx_local: bool,
195 pub height: u32,
197 pub fee: u64,
199}
200
201#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
202pub struct MoneyTransferParamsV1 {
205 pub inputs: Vec<Input>,
207 pub outputs: Vec<Output>,
209}
210#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
214pub struct MoneyTransferUpdateV1 {
215 pub nullifiers: Vec<Nullifier>,
217 pub global_coins: Vec<Coin>,
219 pub local_coins: Vec<Coin>,
221}
222
223#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
225pub struct MoneyGenesisMintParamsV1 {
226 pub input: ClearInput,
228 pub outputs: Vec<Output>,
230}
231
232#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
234pub struct MoneyGenesisMintUpdateV1 {
235 pub coins: Vec<Coin>,
237}
238
239#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
241pub struct MoneyTokenMintParamsV1 {
242 pub coin: Coin,
244}
245
246#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
248pub struct MoneyTokenMintUpdateV1 {
249 pub coin: Coin,
251}
252
253#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
255pub struct MoneyAuthTokenMintParamsV1 {
256 pub token_id: TokenId,
257 pub enc_note: AeadEncryptedNote,
258 pub mint_pubkey: PublicKey,
259}
260
261#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
263pub struct MoneyAuthTokenMintUpdateV1 {}
264
265#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
267pub struct MoneyAuthTokenFreezeParamsV1 {
268 pub mint_public: PublicKey,
272 pub token_id: TokenId,
273}
274
275#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
277pub struct MoneyAuthTokenFreezeUpdateV1 {
278 pub token_id: TokenId,
279}
280
281#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
287pub struct MoneyBurnParamsV1 {
288 pub inputs: Vec<Input>,
290}
291
292#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
294pub struct MoneyBurnUpdateV1 {
295 pub nullifiers: Vec<Nullifier>,
297}
298
299#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
301pub struct MoneyPoWRewardParamsV1 {
302 pub input: ClearInput,
304 pub output: Output,
306}
307
308#[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
310pub struct MoneyPoWRewardUpdateV1 {
311 pub coin: Coin,
313 pub height: u32,
315}