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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use crate::{
    crypto::{AuthorityPen, AuthorityVerifier, Signature},
    NodeCount, NodeIndex, SignatureSet,
};

/// Keychain combines an AuthorityPen and AuthorityVerifier into one object implementing the AlephBFT
/// MultiKeychain trait.
#[derive(Clone)]
pub struct Keychain {
    id: NodeIndex,
    authority_pen: AuthorityPen,
    authority_verifier: AuthorityVerifier,
}

impl Keychain {
    /// Constructs a new keychain from a signing contraption and verifier, with the specified node
    /// index.
    pub fn new(
        id: NodeIndex,
        authority_verifier: AuthorityVerifier,
        authority_pen: AuthorityPen,
    ) -> Self {
        Keychain {
            id,
            authority_pen,
            authority_verifier,
        }
    }

    fn index(&self) -> NodeIndex {
        self.id
    }

    fn node_count(&self) -> NodeCount {
        self.authority_verifier.node_count()
    }

    fn sign(&self, msg: &[u8]) -> Signature {
        self.authority_pen.sign(msg)
    }

    fn verify<I: Into<NodeIndex>>(&self, msg: &[u8], sgn: &Signature, index: I) -> bool {
        self.authority_verifier.verify(msg, sgn, index.into())
    }

    fn is_complete(&self, msg: &[u8], partial: &SignatureSet<Signature>) -> bool {
        self.authority_verifier.is_complete(msg, partial)
    }
}

impl current_aleph_bft::Index for Keychain {
    fn index(&self) -> current_aleph_bft::NodeIndex {
        Keychain::index(self).into()
    }
}

impl legacy_aleph_bft::Index for Keychain {
    fn index(&self) -> legacy_aleph_bft::NodeIndex {
        Keychain::index(self).into()
    }
}

#[async_trait::async_trait]
impl current_aleph_bft::Keychain for Keychain {
    type Signature = Signature;

    fn node_count(&self) -> current_aleph_bft::NodeCount {
        Keychain::node_count(self).into()
    }

    async fn sign(&self, msg: &[u8]) -> Signature {
        Keychain::sign(self, msg)
    }

    fn verify(&self, msg: &[u8], sgn: &Signature, index: current_aleph_bft::NodeIndex) -> bool {
        Keychain::verify(self, msg, sgn, index)
    }
}

#[async_trait::async_trait]
impl legacy_aleph_bft::Keychain for Keychain {
    type Signature = Signature;

    fn node_count(&self) -> legacy_aleph_bft::NodeCount {
        Keychain::node_count(self).into()
    }

    async fn sign(&self, msg: &[u8]) -> Signature {
        Keychain::sign(self, msg)
    }

    fn verify(&self, msg: &[u8], sgn: &Signature, index: legacy_aleph_bft::NodeIndex) -> bool {
        Keychain::verify(self, msg, sgn, index)
    }
}

impl current_aleph_bft::MultiKeychain for Keychain {
    // Using `SignatureSet` is slow, but Substrate has not yet implemented aggregation.
    // We probably should do this for them at some point.
    type PartialMultisignature = SignatureSet<Signature>;

    fn bootstrap_multi(
        &self,
        signature: &Signature,
        index: current_aleph_bft::NodeIndex,
    ) -> Self::PartialMultisignature {
        current_aleph_bft::PartialMultisignature::add_signature(
            SignatureSet(legacy_aleph_bft::SignatureSet::with_size(
                legacy_aleph_bft::Keychain::node_count(self),
            )),
            signature,
            index,
        )
    }

    fn is_complete(&self, msg: &[u8], partial: &Self::PartialMultisignature) -> bool {
        Keychain::is_complete(self, msg, partial)
    }
}

impl legacy_aleph_bft::MultiKeychain for Keychain {
    // Using `SignatureSet` is slow, but Substrate has not yet implemented aggregation.
    // We probably should do this for them at some point.
    type PartialMultisignature = SignatureSet<Signature>;

    fn bootstrap_multi(
        &self,
        signature: &Signature,
        index: legacy_aleph_bft::NodeIndex,
    ) -> Self::PartialMultisignature {
        legacy_aleph_bft::PartialMultisignature::add_signature(
            SignatureSet(legacy_aleph_bft::SignatureSet::with_size(
                legacy_aleph_bft::Keychain::node_count(self),
            )),
            signature,
            index,
        )
    }

    fn is_complete(&self, msg: &[u8], partial: &Self::PartialMultisignature) -> bool {
        Keychain::is_complete(self, msg, partial)
    }
}