Skip to content

Commit

Permalink
keychain crate (no more secretkeys in core) (mimblewimble#146)
Browse files Browse the repository at this point in the history
* introduce grin_keychain, encapsulate derivation of secret_keys
* core compiles against keychain, tests don't run yet
* core tests are now passing against keychain
* wip - getting wallet working with keychain
* add util and keychain to travis test matrix
* basic test around key derivation
  • Loading branch information
antiochp authored and ignopeverell committed Oct 3, 2017
1 parent 6b4f2a6 commit 677d0a3
Show file tree
Hide file tree
Showing 38 changed files with 889 additions and 651 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
*.swp
.DS_Store
.grin
.grin2
.grin*
node*
target
Cargo.lock
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ env:
- TEST_DIR=pool
- TEST_DIR=pow
- TEST_DIR=wallet
- TEST_DIR=util
- TEST_DIR=keychain
- RUST_TEST_THREADS=1 TEST_DIR=grin

script: cd $TEST_DIR && cargo test --verbose
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ authors = ["Ignotus Peverell <[email protected]>"]
exclude = ["**/*.grin", "**/*.grin2"]

[workspace]
members = ["api", "chain", "config", "core", "grin", "p2p", "store", "util", "pool", "wallet"]
members = ["api", "chain", "config", "core", "grin", "keychain", "p2p", "store", "util", "pool", "wallet"]

[dependencies]
grin_api = { path = "./api" }
grin_wallet = { path = "./wallet" }
grin_keychain = { path = "./keychain" }
grin_grin = { path = "./grin" }
grin_config = { path = "./config" }
grin_core = { path = "./core" }
Expand Down
1 change: 1 addition & 0 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ serde_derive = "~1.0.8"
time = "^0.1"

grin_core = { path = "../core" }
grin_keychain = { path = "../keychain" }
grin_store = { path = "../store" }
secp256k1zkp = { git = "https://github.com/mimblewimble/rust-secp256k1-zkp" }

Expand Down
20 changes: 11 additions & 9 deletions chain/tests/mine_simple_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

extern crate grin_core as core;
extern crate grin_chain as chain;
extern crate grin_keychain as keychain;
extern crate env_logger;
extern crate time;
extern crate rand;
extern crate secp256k1zkp as secp;
extern crate grin_pow as pow;

use std::fs;
Expand All @@ -33,6 +33,8 @@ use core::consensus;
use core::global;
use core::global::MiningParameterMode;

use keychain::Keychain;

use pow::{types, cuckoo, MiningWorker};

fn clean_output_dir(dir_name: &str) {
Expand Down Expand Up @@ -60,9 +62,9 @@ fn mine_empty_chain() {
let mut rng = OsRng::new().unwrap();
let chain = setup(".grin");

// mine and add a few blocks
let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
let keychain = Keychain::from_random_seed().unwrap();

// mine and add a few blocks
let mut miner_config = types::MinerConfig {
enable_mining: true,
burn_reward: true,
Expand All @@ -77,8 +79,8 @@ fn mine_empty_chain() {
);
for n in 1..4 {
let prev = chain.head_header().unwrap();
let reward_key = secp::key::SecretKey::new(&secp, &mut rng);
let mut b = core::core::Block::new(&prev, vec![], reward_key).unwrap();
let pk = keychain.derive_pubkey(n as u32).unwrap();
let mut b = core::core::Block::new(&prev, vec![], &keychain, pk).unwrap();
b.header.timestamp = prev.timestamp + time::Duration::seconds(60);

let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
Expand Down Expand Up @@ -246,10 +248,10 @@ fn prepare_block(prev: &BlockHeader, chain: &Chain, diff: u64) -> Block {
}

fn prepare_block_nosum(prev: &BlockHeader, diff: u64) -> Block {
let mut rng = OsRng::new().unwrap();
let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
let reward_key = secp::key::SecretKey::new(&secp, &mut rng);
let mut b = core::core::Block::new(prev, vec![], reward_key).unwrap();
let keychain = Keychain::from_random_seed().unwrap();
let pubkey = keychain.derive_pubkey(1).unwrap();

let mut b = core::core::Block::new(prev, vec![], &keychain, pubkey).unwrap();
b.header.timestamp = prev.timestamp + time::Duration::seconds(60);
b.header.total_difficulty = Difficulty::from_num(diff);
b
Expand Down
9 changes: 6 additions & 3 deletions chain/tests/store_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
extern crate env_logger;
extern crate grin_chain as chain;
extern crate grin_core as core;
extern crate grin_keychain as keychain;
extern crate rand;
extern crate secp256k1zkp as secp;

use std::fs;

use chain::ChainStore;
use core::core::hash::Hashed;
use core::core::{Block, BlockHeader};
use secp::key;
use keychain::Keychain;

fn clean_output_dir(dir_name: &str) {
let _ = fs::remove_dir_all(dir_name);
Expand All @@ -34,9 +34,12 @@ fn test_various_store_indices() {
let _ = env_logger::init();
clean_output_dir(".grin");

let keychain = Keychain::from_random_seed().unwrap();
let pubkey = keychain.derive_pubkey(1).unwrap();

let chain_store = &chain::store::ChainKVStore::new(".grin".to_string()).unwrap() as &ChainStore;

let block = Block::new(&BlockHeader::default(), vec![], key::ONE_KEY).unwrap();
let block = Block::new(&BlockHeader::default(), vec![], &keychain, pubkey).unwrap();
let commit = block.outputs[0].commitment();
let block_hash = block.hash();

Expand Down
53 changes: 30 additions & 23 deletions chain/tests/test_coinbase_maturity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

extern crate grin_core as core;
extern crate grin_chain as chain;
extern crate grin_keychain as keychain;
extern crate grin_pow as pow;
extern crate env_logger;
extern crate time;
extern crate rand;
extern crate secp256k1zkp as secp;
extern crate grin_pow as pow;

use std::fs;
use std::sync::Arc;
use rand::os::OsRng;

use chain::types::*;
use core::core::build;
Expand All @@ -31,6 +30,8 @@ use core::consensus;
use core::global;
use core::global::MiningParameterMode;

use keychain::Keychain;

use pow::{types, cuckoo, MiningWorker};

fn clean_output_dir(dir_name: &str) {
Expand All @@ -43,7 +44,6 @@ fn test_coinbase_maturity() {
clean_output_dir(".grin");
global::set_mining_mode(MiningParameterMode::AutomatedTesting);

let mut rng = OsRng::new().unwrap();
let mut genesis_block = None;
if !chain::Chain::chain_exists(".grin".to_string()) {
genesis_block = pow::mine_genesis_block(None);
Expand All @@ -55,8 +55,6 @@ fn test_coinbase_maturity() {
pow::verify_size,
).unwrap();

let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);

let mut miner_config = types::MinerConfig {
enable_mining: true,
burn_reward: true,
Expand All @@ -71,8 +69,14 @@ fn test_coinbase_maturity() {
);

let prev = chain.head_header().unwrap();
let reward_key = secp::key::SecretKey::new(&secp, &mut rng);
let mut block = core::core::Block::new(&prev, vec![], reward_key).unwrap();

let keychain = Keychain::from_random_seed().unwrap();
let pk1 = keychain.derive_pubkey(1).unwrap();
let pk2 = keychain.derive_pubkey(2).unwrap();
let pk3 = keychain.derive_pubkey(3).unwrap();
let pk4 = keychain.derive_pubkey(4).unwrap();

let mut block = core::core::Block::new(&prev, vec![], &keychain, pk1.clone()).unwrap();
block.header.timestamp = prev.timestamp + time::Duration::seconds(60);

let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
Expand All @@ -96,15 +100,17 @@ fn test_coinbase_maturity() {
let prev = chain.head_header().unwrap();

let amount = consensus::REWARD;
let (coinbase_txn, _) = build::transaction(vec![
build::input(amount, reward_key),
build::output_rand(amount - 1),
build::with_fee(1),
]).unwrap();

let reward_key = secp::key::SecretKey::new(&secp, &mut rng);
let mut block = core::core::Block::new(&prev, vec![&coinbase_txn], reward_key).unwrap();
let (coinbase_txn, _) = build::transaction(
vec![
build::input(amount, pk1.clone()),
build::output(amount - 1, pk2),
build::with_fee(1),
],
&keychain,
).unwrap();

let mut block = core::core::Block::new(&prev, vec![&coinbase_txn], &keychain, pk3.clone())
.unwrap();
block.header.timestamp = prev.timestamp + time::Duration::seconds(60);

let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
Expand All @@ -124,13 +130,15 @@ fn test_coinbase_maturity() {
_ => panic!("expected ImmatureCoinbase error here"),
};

// mine 10 blocks so we increase the height sufficiently
// coinbase will mature and be spendable in the block after these
for _ in 0..10 {
// mine enough blocks to increase the height sufficiently for
// coinbase to reach maturity and be spendable in the next block
for _ in 0..3 {
let prev = chain.head_header().unwrap();

let reward_key = secp::key::SecretKey::new(&secp, &mut rng);
let mut block = core::core::Block::new(&prev, vec![], reward_key).unwrap();
let keychain = Keychain::from_random_seed().unwrap();
let pk = keychain.derive_pubkey(1).unwrap();

let mut block = core::core::Block::new(&prev, vec![], &keychain, pk).unwrap();
block.header.timestamp = prev.timestamp + time::Duration::seconds(60);

let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
Expand All @@ -149,8 +157,7 @@ fn test_coinbase_maturity() {

let prev = chain.head_header().unwrap();

let reward_key = secp::key::SecretKey::new(&secp, &mut rng);
let mut block = core::core::Block::new(&prev, vec![&coinbase_txn], reward_key).unwrap();
let mut block = core::core::Block::new(&prev, vec![&coinbase_txn], &keychain, pk4).unwrap();

block.header.timestamp = prev.timestamp + time::Duration::seconds(60);

Expand Down
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ serde_derive = "~1.0.8"
time = "^0.1"
lazy_static = "~0.2.8"
secp256k1zkp = { git = "https://github.com/mimblewimble/rust-secp256k1-zkp" }
grin_keychain = { path = "../keychain" }
grin_util = { path = "../util" }
Loading

0 comments on commit 677d0a3

Please sign in to comment.