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
//! The Signature type is defined here. They are used in ChainHeaders as
//! a way of providing cryptographically verifiable proof of a given agent
//! as having been the author of a given data entry.

use holochain_persistence_api::cas::content::Address;

use holochain_json_api::{error::JsonError, json::JsonString};

/// Provenance is a tuple of initiating agent public key and signature of some item being signed
/// this type is used in headers and in capability requests where the item being signed
/// is implicitly known by context
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Hash, Eq, DefaultJson)]
pub struct Provenance(pub Address, pub Signature);

impl Provenance {
    /// Creates a new provenance instance with source typically
    /// being an agent address (public key) and the signature
    /// some signed data using the private key associated with
    /// the public key.
    pub fn new(source: Address, signature: Signature) -> Self {
        Provenance(source, signature)
    }
    pub fn source(&self) -> Address {
        self.0.clone()
    }
    pub fn signature(&self) -> Signature {
        self.1.clone()
    }
}
/// Signature is a wrapper structure for a cryptographic signature
/// it is stored as a string and can be validated as having been signed
/// by the private key associated with a given public key.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Hash, Eq, DefaultJson)]
pub struct Signature(String);

impl Signature {
    pub fn fake() -> Signature {
        test_signature()
    }
}

impl From<&'static str> for Signature {
    fn from(s: &str) -> Signature {
        Signature(s.to_owned())
    }
}

impl From<String> for Signature {
    fn from(s: String) -> Signature {
        Signature(s)
    }
}

pub fn test_signatures() -> Vec<Signature> {
    vec![test_signature()]
}

pub fn test_signature() -> Signature {
    Signature::from("fake-signature")
}

pub fn test_signature_b() -> Signature {
    Signature::from("another-fake-signature")
}

pub fn test_signature_c() -> Signature {
    Signature::from("sig-c")
}

impl From<Signature> for String {
    fn from(s: Signature) -> String {
        s.0
    }
}