xpx_chain_sdk/models/multisig/
multisig.rs

1/*
2 * Copyright 2018 ProximaX Limited. All rights reserved.
3 * Use of this source code is governed by the Apache 2.0
4 * license that can be found in the LICENSE file.
5 */
6
7use {
8    num_enum::IntoPrimitive,
9    std::{collections::HashMap, fmt},
10};
11
12use crate::{account::PublicAccount, errors_const::ERR_UNKNOWN_TYPE};
13use crate::helpers::TransactionHash;
14
15/// The type of the modification:
16/// * 0 - Add cosignatory.
17/// * 1 - Remove cosignatory.
18#[derive(Debug, Clone, Serialize, Deserialize, IntoPrimitive)]
19#[repr(u8)]
20pub enum MultisigModificationType {
21    Add,
22    Remove,
23}
24
25impl MultisigModificationType {
26    pub fn value(self) -> u8 {
27        self.into()
28    }
29}
30
31impl From<u8> for MultisigModificationType {
32    fn from(t: u8) -> Self {
33        assert!(t <= 1, "{}", ERR_UNKNOWN_TYPE);
34        match t {
35            0 => MultisigModificationType::Add,
36            _ => MultisigModificationType::Remove,
37        }
38    }
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct CosignatoryModification {
43    #[serde(rename = "modificationType")]
44    pub modification_type: MultisigModificationType,
45    /// The public key of the cosignatory account.
46    #[serde(rename = "cosignatory_public_key")]
47    pub public_account: PublicAccount,
48}
49
50impl CosignatoryModification {
51    pub fn create(
52        modification_type: MultisigModificationType,
53        public_account: PublicAccount,
54    ) -> Self {
55        CosignatoryModification { modification_type, public_account }
56    }
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60#[serde(rename_all = "camelCase")]
61pub struct Cosignature {
62    /// The signature of the entity.
63    /// The signature was generated by the signer and can be used to validate tha the entity data
64    /// was not modified by a node.
65    pub signature: String,
66    /// The public account of the cosignatory.
67    pub signer: PublicAccount,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
71#[serde(rename_all = "camelCase")]
72pub struct CosignatureInfo {
73    pub signature: String,
74    /// The public account of the cosignatory.
75    pub signer: String,
76    pub parent_hash: TransactionHash,
77}
78
79impl fmt::Display for CosignatureInfo {
80    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81        write!(f, "{}", serde_json::to_string_pretty(self).unwrap_or_default())
82    }
83}
84
85impl CosignatureInfo {
86    pub fn transaction_hash(&self) -> TransactionHash {
87        self.parent_hash.to_owned()
88    }
89}
90
91#[derive(Debug, Serialize, Deserialize)]
92#[serde(rename_all = "camelCase")]
93pub struct MultisigAccountInfo {
94    /// The PublicAccount.
95    #[serde(rename = "account")]
96    pub account: PublicAccount,
97    /// The number of signatures needed to approve a transaction.
98    #[serde(rename = "minApproval")]
99    pub min_approval: i32,
100    /// The number of signatures needed to remove a cosignatory.
101    #[serde(rename = "minRemoval")]
102    pub min_removal: i32,
103    /// The array of PublicAccount of the cosignatory accounts.
104    #[serde(rename = "cosignatories")]
105    pub cosignatories: Vec<PublicAccount>,
106    /// The array of multisig accounts where the account is cosignatory.
107    #[serde(rename = "multisigAccounts")]
108    pub multisig_accounts: Vec<PublicAccount>,
109}
110
111impl fmt::Display for MultisigAccountInfo {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        write!(f, "{}", serde_json::to_string_pretty(self).unwrap_or_default())
114    }
115}
116
117#[derive(Debug, Serialize, Deserialize)]
118pub struct MultisigAccountGraphInfo {
119    #[serde(rename = "MultisigAccounts")]
120    pub multisig_accounts: HashMap<i16, Vec<MultisigAccountInfo>>,
121}
122
123impl fmt::Display for MultisigAccountGraphInfo {
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        write!(f, "{}", serde_json::to_string_pretty(self).unwrap_or_default())
126    }
127}