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
use alloc::boxed::Box;
use alloc::string::String;

use serde::{Deserialize, Serialize};
use umbral_pre::{PublicKey, Signature, Signer};

use crate::address::Address;
use crate::key_frag::EncryptedKeyFrag;
use crate::versioning::{
    messagepack_deserialize, messagepack_serialize, ProtocolObject, ProtocolObjectInner,
};

/// Represents a string used by characters to perform a revocation on a specific Ursula.
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct RevocationOrder {
    staker_address: Address,
    encrypted_kfrag: EncryptedKeyFrag,
    signature: Signature,
}

impl RevocationOrder {
    /// Create and sign a new revocation order.
    pub fn new(
        signer: &Signer,
        staker_address: &Address,
        encrypted_kfrag: &EncryptedKeyFrag,
    ) -> Self {
        Self {
            staker_address: *staker_address,
            encrypted_kfrag: encrypted_kfrag.clone(),
            signature: signer
                .sign(&[staker_address.as_ref(), &encrypted_kfrag.to_bytes()].concat()),
        }
    }

    /// Verifies the revocation order against Alice's key.
    pub fn verify_signature(&self, alice_verifying_key: &PublicKey) -> bool {
        // TODO: return an Option of something instead of returning `bool`?
        let message = [
            self.staker_address.as_ref(),
            &self.encrypted_kfrag.to_bytes(),
        ]
        .concat();
        self.signature.verify(alice_verifying_key, &message)
    }
}

impl<'a> ProtocolObjectInner<'a> for RevocationOrder {
    fn brand() -> [u8; 4] {
        *b"Revo"
    }

    fn version() -> (u16, u16) {
        (1, 0)
    }

    fn unversioned_to_bytes(&self) -> Box<[u8]> {
        messagepack_serialize(&self)
    }

    fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
        if minor_version == 0 {
            Some(messagepack_deserialize(bytes))
        } else {
            None
        }
    }
}

impl<'a> ProtocolObject<'a> for RevocationOrder {}