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
use std::collections::BTreeSet;
use serde::{Deserialize, Serialize};
use threshold_crypto::PublicKey;
use tiny_keccak::{Hasher, Sha3};
use crate::{DbcContentHash, Error, Hash};
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub struct BlindedOwner(Hash);
impl BlindedOwner {
pub fn new(
owner: &PublicKey,
parents: &BTreeSet<DbcContentHash>,
amount: u64,
output_number: u32,
) -> Self {
let mut sha3 = Sha3::v256();
for parent in parents.iter() {
sha3.update(parent);
}
sha3.update(&amount.to_be_bytes());
sha3.update(&output_number.to_be_bytes());
sha3.update(&owner.to_bytes());
let mut hash = [0; 32];
sha3.finalize(&mut hash);
Self(Hash(hash))
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub struct DbcContent {
pub parents: BTreeSet<DbcContentHash>,
pub amount: u64,
pub output_number: u32,
pub owner: BlindedOwner,
}
impl DbcContent {
pub fn new(
parents: BTreeSet<DbcContentHash>,
amount: u64,
output_number: u32,
owner_key: PublicKey,
) -> Self {
let owner = BlindedOwner::new(&owner_key, &parents, amount, output_number);
DbcContent {
parents,
amount,
output_number,
owner,
}
}
pub fn validate_unblinding(&self, owner_key: &PublicKey) -> Result<(), Error> {
let blinded = BlindedOwner::new(owner_key, &self.parents, self.amount, self.output_number);
if blinded == self.owner {
Ok(())
} else {
Err(Error::FailedUnblinding)
}
}
pub fn hash(&self) -> DbcContentHash {
let mut sha3 = Sha3::v256();
for parent in self.parents.iter() {
sha3.update(parent);
}
sha3.update(&self.amount.to_be_bytes());
sha3.update(&self.output_number.to_be_bytes());
sha3.update(&self.owner.0);
let mut hash = [0; 32];
sha3.finalize(&mut hash);
Hash(hash)
}
}