fedimint_dummy_common/
lib.rs

1#![deny(clippy::pedantic)]
2#![allow(clippy::missing_panics_doc)]
3#![allow(clippy::module_name_repetitions)]
4#![allow(clippy::must_use_candidate)]
5
6use std::fmt;
7
8use config::DummyClientConfig;
9use fedimint_core::core::{Decoder, ModuleInstanceId, ModuleKind};
10use fedimint_core::encoding::{Decodable, Encodable};
11use fedimint_core::module::{AmountUnit, CommonModuleInit, ModuleCommon, ModuleConsensusVersion};
12use fedimint_core::secp256k1::{Keypair, PublicKey, Secp256k1};
13use fedimint_core::{Amount, extensible_associated_module_type, plugin_types_trait_impl_common};
14use serde::{Deserialize, Serialize};
15use thiserror::Error;
16
17// Common contains types shared by both the client and server
18
19// The client and server configuration
20pub mod config;
21
22/// Unique name for this module
23pub const KIND: ModuleKind = ModuleKind::from_static_str("dummy");
24
25/// Modules are non-compatible with older versions
26pub const MODULE_CONSENSUS_VERSION: ModuleConsensusVersion = ModuleConsensusVersion::new(2, 0);
27
28/// Non-transaction items that will be submitted to consensus
29#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, Encodable, Decodable)]
30pub struct DummyConsensusItem;
31
32#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
33pub struct DummyInputV0 {
34    pub amount: Amount,
35    /// Associate the input with a user's pubkey
36    pub account: PublicKey,
37}
38
39/// Input for a fedimint transaction
40#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
41pub struct DummyInputV1 {
42    pub amount: Amount,
43    pub unit: AmountUnit,
44    /// Associate the input with a user's pubkey
45    pub account: PublicKey,
46}
47
48extensible_associated_module_type!(DummyInput, DummyInputV1, UnknownDummyInputVariantError);
49
50#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
51pub struct DummyOutputV0 {
52    pub amount: Amount,
53    /// Associate the output with a user's pubkey
54    pub account: PublicKey,
55}
56
57/// Output for a fedimint transaction
58#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
59pub struct DummyOutputV1 {
60    pub amount: Amount,
61    pub unit: AmountUnit,
62    /// Associate the output with a user's pubkey
63    pub account: PublicKey,
64}
65
66extensible_associated_module_type!(DummyOutput, DummyOutputV1, UnknownDummyOutputVariantError);
67
68#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
69pub struct DummyOutputOutcomeV0(pub Amount, pub PublicKey);
70
71/// Information needed by a client to update output funds
72#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
73pub struct DummyOutputOutcome(pub Amount, pub AmountUnit, pub PublicKey);
74
75/// Errors that might be returned by the server
76#[derive(Debug, Clone, Eq, PartialEq, Hash, Error, Encodable, Decodable)]
77pub enum DummyInputError {
78    #[error("Not enough funds")]
79    NotEnoughFunds,
80    #[error("Invalid version")]
81    InvalidVersion,
82}
83
84/// Errors that might be returned by the server
85#[derive(Debug, Clone, Eq, PartialEq, Hash, Error, Encodable, Decodable)]
86pub enum DummyOutputError {
87    #[error("Invalid version")]
88    InvalidVersion,
89}
90
91/// Contains the types defined above
92pub struct DummyModuleTypes;
93
94// Wire together the types for this module
95plugin_types_trait_impl_common!(
96    KIND,
97    DummyModuleTypes,
98    DummyClientConfig,
99    DummyInput,
100    DummyOutput,
101    DummyOutputOutcome,
102    DummyConsensusItem,
103    DummyInputError,
104    DummyOutputError
105);
106
107#[derive(Debug)]
108pub struct DummyCommonInit;
109
110impl CommonModuleInit for DummyCommonInit {
111    const CONSENSUS_VERSION: ModuleConsensusVersion = MODULE_CONSENSUS_VERSION;
112    const KIND: ModuleKind = KIND;
113
114    type ClientConfig = DummyClientConfig;
115
116    fn decoder() -> Decoder {
117        DummyModuleTypes::decoder_builder().build()
118    }
119}
120
121impl fmt::Display for DummyClientConfig {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        write!(f, "DummyClientConfig")
124    }
125}
126impl fmt::Display for DummyInputV1 {
127    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128        write!(f, "DummyInput {}", self.amount)
129    }
130}
131
132impl fmt::Display for DummyOutputV1 {
133    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134        write!(f, "DummyOutput {}", self.amount)
135    }
136}
137
138impl fmt::Display for DummyOutputOutcome {
139    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140        write!(f, "DummyOutputOutcome")
141    }
142}
143
144impl fmt::Display for DummyConsensusItem {
145    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146        write!(f, "DummyConsensusItem")
147    }
148}
149
150/// A special key that creates assets for a test/example
151const FED_SECRET_PHRASE: &str = "Money printer go brrr...........";
152
153const BROKEN_FED_SECRET_PHRASE: &str = "Money printer go <boom>........!";
154
155pub fn fed_public_key() -> PublicKey {
156    fed_key_pair().public_key()
157}
158
159pub fn fed_key_pair() -> Keypair {
160    Keypair::from_seckey_slice(&Secp256k1::new(), FED_SECRET_PHRASE.as_bytes()).expect("32 bytes")
161}
162
163pub fn broken_fed_public_key() -> PublicKey {
164    broken_fed_key_pair().public_key()
165}
166
167// Like fed, but with a broken accounting
168pub fn broken_fed_key_pair() -> Keypair {
169    Keypair::from_seckey_slice(&Secp256k1::new(), BROKEN_FED_SECRET_PHRASE.as_bytes())
170        .expect("32 bytes")
171}