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::{CommonModuleInit, ModuleCommon, ModuleConsensusVersion};
12use fedimint_core::secp256k1::{Keypair, PublicKey, Secp256k1};
13use fedimint_core::{Amount, 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/// Input for a fedimint transaction
33#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
34pub struct DummyInput {
35    pub amount: Amount,
36    /// Associate the input with a user's pubkey
37    pub account: PublicKey,
38}
39
40/// Output for a fedimint transaction
41#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
42pub struct DummyOutput {
43    pub amount: Amount,
44    /// Associate the output with a user's pubkey
45    pub account: PublicKey,
46}
47
48/// Information needed by a client to update output funds
49#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Encodable, Decodable)]
50pub struct DummyOutputOutcome(pub Amount, pub PublicKey);
51
52/// Errors that might be returned by the server
53#[derive(Debug, Clone, Eq, PartialEq, Hash, Error, Encodable, Decodable)]
54pub enum DummyInputError {
55    #[error("Not enough funds")]
56    NotEnoughFunds,
57}
58
59/// Errors that might be returned by the server
60#[derive(Debug, Clone, Eq, PartialEq, Hash, Error, Encodable, Decodable)]
61pub enum DummyOutputError {}
62
63/// Contains the types defined above
64pub struct DummyModuleTypes;
65
66// Wire together the types for this module
67plugin_types_trait_impl_common!(
68    KIND,
69    DummyModuleTypes,
70    DummyClientConfig,
71    DummyInput,
72    DummyOutput,
73    DummyOutputOutcome,
74    DummyConsensusItem,
75    DummyInputError,
76    DummyOutputError
77);
78
79#[derive(Debug)]
80pub struct DummyCommonInit;
81
82impl CommonModuleInit for DummyCommonInit {
83    const CONSENSUS_VERSION: ModuleConsensusVersion = MODULE_CONSENSUS_VERSION;
84    const KIND: ModuleKind = KIND;
85
86    type ClientConfig = DummyClientConfig;
87
88    fn decoder() -> Decoder {
89        DummyModuleTypes::decoder_builder().build()
90    }
91}
92
93impl fmt::Display for DummyClientConfig {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        write!(f, "DummyClientConfig")
96    }
97}
98impl fmt::Display for DummyInput {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        write!(f, "DummyInput {}", self.amount)
101    }
102}
103
104impl fmt::Display for DummyOutput {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        write!(f, "DummyOutput {}", self.amount)
107    }
108}
109
110impl fmt::Display for DummyOutputOutcome {
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        write!(f, "DummyOutputOutcome")
113    }
114}
115
116impl fmt::Display for DummyConsensusItem {
117    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118        write!(f, "DummyConsensusItem")
119    }
120}
121
122/// A special key that creates assets for a test/example
123const FED_SECRET_PHRASE: &str = "Money printer go brrr...........";
124
125const BROKEN_FED_SECRET_PHRASE: &str = "Money printer go <boom>........!";
126
127pub fn fed_public_key() -> PublicKey {
128    fed_key_pair().public_key()
129}
130
131pub fn fed_key_pair() -> Keypair {
132    Keypair::from_seckey_slice(&Secp256k1::new(), FED_SECRET_PHRASE.as_bytes()).expect("32 bytes")
133}
134
135pub fn broken_fed_public_key() -> PublicKey {
136    broken_fed_key_pair().public_key()
137}
138
139// Like fed, but with a broken accounting
140pub fn broken_fed_key_pair() -> Keypair {
141    Keypair::from_seckey_slice(&Secp256k1::new(), BROKEN_FED_SECRET_PHRASE.as_bytes())
142        .expect("32 bytes")
143}