Skip to content

Commit

Permalink
feat(dbc_owner): dbc_content now has an owner
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrusu authored and dan-da committed May 19, 2021
1 parent 7b80c95 commit 0eaede9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ thiserror = "1.0.24"
quickcheck = "1"
quickcheck_macros = "1"
rand = "0.7.1"
bls_dkg = "0.3"

[dependencies.tiny-keccak]
version = "2.0.0"
Expand Down
8 changes: 6 additions & 2 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ mod tests {

let outputs = divide(dbc.amount(), n_ways)
.enumerate()
.map(|(i, amount)| DbcContent::new(input_hashes.clone(), amount, i as u8))
.map(|(i, amount)| {
DbcContent::new(input_hashes.clone(), amount, i as u8, crate::bls_dkg_id())
})
.collect();

MintRequest { inputs, outputs }
Expand All @@ -90,6 +92,7 @@ mod tests {
parents: Default::default(),
amount: 100,
output_number: 0,
owner: crate::bls_dkg_id(),
};

let input_content_hashes: BTreeSet<_> = vec![input_content.hash()].into_iter().collect();
Expand Down Expand Up @@ -144,7 +147,7 @@ mod tests {
let input_hashes: BTreeSet<DbcContentHash> =
inputs.iter().map(|in_dbc| in_dbc.name()).collect();

let content = DbcContent::new(input_hashes.clone(), amount, 0);
let content = DbcContent::new(input_hashes.clone(), amount, 0, crate::bls_dkg_id());
let outputs = vec![content].into_iter().collect();

let mint_request = MintRequest { inputs, outputs };
Expand All @@ -167,6 +170,7 @@ mod tests {
fuzzed_parents,
amount + extra_output_amount.coerce::<u64>(),
0,
crate::bls_dkg_id(),
);

let mut fuzzed_transaction_sigs = BTreeMap::new();
Expand Down
13 changes: 10 additions & 3 deletions src/dbc_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@
// permissions and limitations relating to use of the SAFE Network Software.
use std::collections::BTreeSet;

use bls_dkg::PublicKeySet;
use serde::{Deserialize, Serialize};
use tiny_keccak::{Hasher, Sha3};

use crate::DbcContentHash;

#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub struct DbcContent {
pub parents: BTreeSet<DbcContentHash>, // Hash of parent DbcContent. Also used as a nonce
// TODO: pub owner: PubKey
pub parents: BTreeSet<DbcContentHash>, // Parent DBC's, acts as a nonce
pub amount: u64,
pub output_number: u8,
pub owner: PublicKeySet,
}

impl DbcContent {
// Create a new DbcContent for signing. TODO: blind the owner from the mint
pub fn new(parents: BTreeSet<DbcContentHash>, amount: u64, output_number: u8) -> Self {
pub fn new(
parents: BTreeSet<DbcContentHash>,
amount: u64,
output_number: u8,
owner: PublicKeySet,
) -> Self {
// let mut owner = owner;
// for _ in 0..amount % 1000 {
// owner = sha3_256(&owner); // owner not visible to mint, until out_dbc is minted.
Expand All @@ -31,6 +37,7 @@ impl DbcContent {
parents,
amount,
output_number,
owner,
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@ pub use crate::{
mint::{Mint, MintRequest},
};

pub(crate) fn bls_dkg_id() -> bls_dkg::PublicKeySet {
use std::collections::BTreeSet;
use std::iter::FromIterator;

let owner_name = rand::random();
let threshold = 0;
let (mut key_gen, proposal) = match bls_dkg::KeyGen::initialize(
owner_name,
threshold,
BTreeSet::from_iter(vec![owner_name]),
) {
Ok(key_gen_init) => key_gen_init,
Err(e) => panic!("Failed to init key gen {:?}", e),
};

let mut msgs = vec![proposal];
while let Some(msg) = msgs.pop() {
println!("Processing {:?}", msg);
match key_gen.handle_message(&mut rand::thread_rng(), msg) {
Ok(response_msgs) => msgs.extend(response_msgs),
Err(e) => panic!("Error while generating BLS key: {:?}", e),
}
}

println!("After processing messages: {:?}", key_gen.phase());

let (_, outcome) = key_gen.generate_keys().unwrap();
outcome.public_key_set
}

#[cfg(test)]
fn sha3_256(input: &[u8]) -> Hash {
let mut sha3 = Sha3::v256();
Expand Down
32 changes: 24 additions & 8 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Mint {
let genesis_input = [0u8; 32];

let parents = vec![genesis_input].into_iter().collect();
let content = DbcContent::new(parents, amount, 0);
let content = DbcContent::new(parents, amount, 0, crate::bls_dkg_id());

let transaction = DbcTransaction {
inputs: vec![genesis_input].into_iter().collect(),
Expand Down Expand Up @@ -218,7 +218,9 @@ mod tests {
let outputs = output_amounts
.iter()
.enumerate()
.map(|(i, amount)| DbcContent::new(input_hashes.clone(), *amount, i as u8))
.map(|(i, amount)| {
DbcContent::new(input_hashes.clone(), *amount, i as u8, crate::bls_dkg_id())
})
.collect();

let mint_request = MintRequest { inputs, outputs };
Expand Down Expand Up @@ -275,16 +277,21 @@ mod tests {

let mint_request = MintRequest {
inputs: inputs.clone(),
outputs: vec![DbcContent::new(input_hashes.clone(), 1000, 0)]
.into_iter()
.collect(),
outputs: vec![DbcContent::new(
input_hashes.clone(),
1000,
0,
crate::bls_dkg_id(),
)]
.into_iter()
.collect(),
};

let (t, s) = genesis.reissue(mint_request).unwrap();

let double_spend_mint_request = MintRequest {
inputs,
outputs: vec![DbcContent::new(input_hashes, 1000, 1)]
outputs: vec![DbcContent::new(input_hashes, 1000, 1, crate::bls_dkg_id())]
.into_iter()
.collect(),
};
Expand Down Expand Up @@ -334,7 +341,14 @@ mod tests {
let input_content: HashSet<_> = input_amounts
.iter()
.enumerate()
.map(|(i, amount)| DbcContent::new(gen_input_hashes.clone(), *amount, i as u8))
.map(|(i, amount)| {
DbcContent::new(
gen_input_hashes.clone(),
*amount,
i as u8,
crate::bls_dkg_id(),
)
})
.collect();

let mint_request = MintRequest {
Expand Down Expand Up @@ -367,7 +381,7 @@ mod tests {
fuzzed_parents.insert(rand::random());
}

DbcContent::new(fuzzed_parents, *amount, *output_number)
DbcContent::new(fuzzed_parents, *amount, *output_number, crate::bls_dkg_id())
})
.collect();

Expand Down Expand Up @@ -474,6 +488,7 @@ mod tests {
parents: Default::default(),
amount: 100,
output_number: 0,
owner: crate::bls_dkg_id(),
};
let input_content_hashes: BTreeSet<_> = vec![input_content.hash()].into_iter().collect();

Expand All @@ -492,6 +507,7 @@ mod tests {
parents: input_content_hashes,
amount: 100,
output_number: 0,
owner: crate::bls_dkg_id(),
}]
.into_iter()
.collect(),
Expand Down

0 comments on commit 0eaede9

Please sign in to comment.