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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Re-export of `libp2p_identity::Keypair`
use libp2p_identity::*;

/// Generate a new ed25519 keypair for signing cbor data.
pub fn generate() -> Keypair {
    Keypair::generate_ed25519()
}

pub struct Signables {
    pub v1: Vec<u8>,
    pub v2: Vec<u8>,
}

pub struct Signed {
    pub v1: Vec<u8>,
    pub v2: Vec<u8>,
}

pub struct Signer {
    keypair: Keypair,
}

impl Default for Signer {
    fn default() -> Self {
        Self {
            keypair: generate(),
        }
    }
}

impl Signer {
    pub fn new(keypair: Keypair) -> Self {
        Self { keypair }
    }

    pub fn sign(&self, signables: Signables) -> Result<Signed, SigningError> {
        let v1 = self.keypair.sign(&signables.v1)?;
        let v2 = self.keypair.sign(&signables.v2)?;

        Ok(Signed { v1, v2 })
    }

    pub fn public(&self) -> PublicKey {
        self.keypair.public()
    }
}

/// Used to sign bytes created by `cbor::InputData{}.to_bytes()`
pub struct V2Signer {
    keypair: ed25519::Keypair,
}

impl V2Signer {
    pub fn new(keypair: &ed25519::Keypair) -> Self {
        Self {
            keypair: keypair.clone(),
        }
    }

    pub fn sign(&self, data: &[u8]) -> Vec<u8> {
        // Create bytes for signing by concatenating ipns-signature: prefix
        // (bytes in hex: 69706e732d7369676e61747572653a)
        // with raw CBOR bytes from data
        let bytes_for_signing = vec!["ipns-signature:".as_bytes(), data].concat();

        self.keypair.sign(&bytes_for_signing)
    }

    pub fn verify(&self, data: &[u8], signature: &[u8]) -> bool {
        // Create bytes for signing by concatenating ipns-signature: prefix
        // (bytes in hex: 69706e732d7369676e61747572653a)
        // with raw CBOR bytes from data
        let bytes_for_signing = vec!["ipns-signature:".as_bytes(), data].concat();

        self.keypair.public().verify(&bytes_for_signing, signature)
    }
}

/// *Do not use.* For backward compatibility only.
pub struct V1Signer {
    pub keypair: ed25519::Keypair,
    pub value: &'static [u8],
    pub validity: &'static [u8],
    pub validity_type: u8,
}

impl V1Signer {
    /// Creates a new V1Signer
    pub fn new(
        keypair: &ed25519::Keypair,
        value: &'static [u8],
        validity: &'static [u8],
        validity_type: u8,
    ) -> Self {
        Self {
            keypair: keypair.clone(),
            value,
            validity,
            validity_type,
        }
    }

    /// Creates a IpnsEntry.signatureV1 by concatenating IpnsEntry.value + IpnsEntry.validity + string(IpnsEntry.validityType)
    pub fn sign(&self) -> Vec<u8> {
        let bytes_for_signing = vec![
            self.value,
            self.validity,
            self.validity_type.to_string().as_bytes(),
        ]
        .concat();

        self.keypair.sign(&bytes_for_signing)
    }

    /// Verify the V1Signer data using the keypair and signature
    pub fn verify(&self, signature: &[u8]) -> bool {
        let bytes_for_signing = vec![
            self.value,
            self.validity,
            self.validity_type.to_string().as_bytes(),
        ]
        .concat();

        self.keypair.public().verify(&bytes_for_signing, signature)
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use crate::cbor;

    #[test]
    fn test_signers() {
        let keypair = Keypair::generate_ed25519()
            .try_into_ed25519()
            .expect("A ed25519 keypair");

        let value = "QmWEekX7EZLUd9VXRNMRXW3LXe4F6x7mB8oPxY5XLptrBq";
        let validity = "2033-05-18T03:33:20.000000000Z";
        let sequence = 0;
        let ttl = 0;

        let data = cbor::Data {
            value: value.as_bytes().to_vec(),
            validity: validity.as_bytes().to_vec(),
            validity_type: 0,
            sequence,
            ttl,
        }
        .to_bytes();

        let v2_signer = V2Signer::new(&keypair);
        let sig_v2 = v2_signer.sign(&data);

        let v1_signer = V1Signer {
            keypair,
            validity: validity.as_bytes(),
            value: value.as_bytes(),
            validity_type: 0,
        };
        let sig_v1 = v1_signer.sign();

        // verify signatures
        assert!(v2_signer.verify(&data, &sig_v2));
        assert!(v1_signer.verify(&sig_v1));
    }
}