1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::accounting::account;
use crate::key::{deserialize_public_key, serialize_public_key};
use crate::transaction::WitnessAccountData;
use chain_core::property::WriteError;
use chain_core::{
    packer::Codec,
    property::{DeserializeFromSlice, ReadError, Serialize},
};
use chain_crypto::{Ed25519, PublicKey, Signature};

pub use account::{
    DelegationRatio, DelegationType, LedgerError, SpendingCounter, SpendingCounterIncreasing,
};

pub type AccountAlg = Ed25519;

pub type Witness = Signature<WitnessAccountData, AccountAlg>;

/// Account Identifier (also used as Public Key)
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
    any(test, feature = "property-test-api"),
    derive(test_strategy::Arbitrary)
)]
pub struct Identifier(PublicKey<AccountAlg>);

impl From<PublicKey<AccountAlg>> for Identifier {
    fn from(pk: PublicKey<AccountAlg>) -> Self {
        Identifier(pk)
    }
}

impl From<Identifier> for PublicKey<AccountAlg> {
    fn from(i: Identifier) -> Self {
        i.0
    }
}

impl AsRef<PublicKey<AccountAlg>> for Identifier {
    fn as_ref(&self) -> &PublicKey<AccountAlg> {
        &self.0
    }
}

impl Serialize for Identifier {
    fn serialized_size(&self) -> usize {
        self.0.as_ref().len()
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        serialize_public_key(&self.0, codec)
    }
}

impl DeserializeFromSlice for Identifier {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        deserialize_public_key(codec).map(Identifier)
    }
}

/// The public ledger of all accounts associated with their current state
pub type Ledger = account::Ledger<Identifier, ()>;

impl std::fmt::Display for Identifier {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

#[cfg(any(test, feature = "property-test-api"))]
mod test {
    use super::*;
    #[cfg(test)]
    use crate::testing::serialization::serialization_bijection;
    use chain_crypto::{Ed25519, KeyPair};
    #[cfg(test)]
    use quickcheck::TestResult;
    use quickcheck::{Arbitrary, Gen};

    impl Arbitrary for Identifier {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            let kp: KeyPair<Ed25519> = Arbitrary::arbitrary(g);
            Identifier::from(kp.into_keys().1)
        }
    }

    quickcheck! {
        fn identifier_serialization_bijection(id: Identifier) -> TestResult {
            serialization_bijection(id)
        }
    }
}