Skip to content

Commit

Permalink
Refactoring keybase structure (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
andynog committed Oct 15, 2020
1 parent 7af390f commit 5b92633
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
1 change: 1 addition & 0 deletions relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bitcoin = { version= "0.25"}
bitcoin-wallet = "1.1.0"
hdpath = { version = "0.2.0", features = ["with-bitcoin"] }
rust-crypto = "0.2.36"
bech32 = "0.7.2"

[dependencies.tendermint]
version = "0.16.0"
Expand Down
33 changes: 18 additions & 15 deletions relayer/src/crypto/keybase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ use bitcoin::secp256k1::{All, Message, Secp256k1, Signature};
use std::convert::TryFrom;
use crate::error;

pub type Address = Vec<u8>;

pub enum KeyStore {
MemoryKeyStore { store: BTreeMap<String, KeyEntry> }
MemoryKeyStore { store: BTreeMap<Address, KeyEntry> }
}

pub enum StoreBackend {
Memory
}

trait KeyRing {
fn init(backend: StoreBackend) -> KeyStore;
fn new_from_mnemonic(&mut self, name: String, mnemonic_words: &str) -> Result<bool, Error>;
}

/// Key entry stores the Private Key and Public Key as well the address
#[derive(Clone, Debug)]
pub struct KeyEntry {
/// Address
pub address: Vec<u8>,

/// Public key
pub public_key: ExtendedPubKey,

Expand All @@ -40,14 +44,14 @@ impl KeyStore {
pub fn init(backend: StoreBackend) -> KeyStore {
match backend {
StoreBackend::Memory => {
let store: BTreeMap<String, KeyEntry> = BTreeMap::new();
let store: BTreeMap<Address, KeyEntry> = BTreeMap::new();
KeyStore::MemoryKeyStore { store }
}
}
}

/// Add a key entry in the store using a mnemonic.
pub fn add_from_mnemonic(&mut self, name: String, mnemonic_words: &str) -> Result<bool, Error> {
pub fn add_from_mnemonic(&mut self, name: String, mnemonic_words: &str) -> Result<Address, Error> {

// Generate seed from mnemonic
let mnemonic = Mnemonic::from_str(mnemonic_words).map_err(|e| error::Kind::KeyBase.context(e))?;
Expand All @@ -65,35 +69,34 @@ impl KeyStore {
let address = get_address(public_key);

let key = KeyEntry {
address,
public_key,
private_key
};

self.insert(name, key);
self.insert(address.clone(), key);

Ok(true)
Ok(address)
}

/// Return a key entry from a key name
pub fn get(&self, name: String) -> Option<&KeyEntry> {
pub fn get(&self, address: Vec<u8>) -> Option<&KeyEntry> {
match &self {
KeyStore::MemoryKeyStore { store: s } => {
if !s.contains_key(&name) {
if !s.contains_key(&address) {
None
}
else {
s.get(&name)
s.get(&address)
}
}
}
}

/// Insert an entry in the key store
pub fn insert(&mut self, name: String, key: KeyEntry) -> Option<KeyEntry> {
pub fn insert(&mut self, addr: Vec<u8>, key: KeyEntry) -> Option<KeyEntry> {
match self {
KeyStore::MemoryKeyStore { store: s} => {
let ke = s.insert(name, key);
let ke = s.insert(addr, key);
ke
}
}
Expand All @@ -115,4 +118,4 @@ fn get_address(pk: ExtendedPubKey) -> Vec<u8> {
let mut acct = vec![0; hash.output_bytes()];
hash.result(&mut acct);
acct.to_vec()
}
}
5 changes: 2 additions & 3 deletions relayer/src/keys/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub fn restore_key(opts: KeysRestoreOptions) -> Result<Vec<u8>, Error> {
// Get the destination chain
let mut chain = CosmosSDKChain::from_config(opts.clone().chain_config)?;

chain.keybase.add_from_mnemonic(opts.clone().name, &opts.mnemonic).map_err(|e| error::Kind::KeyBase.context(e))?;
let address = chain.keybase.add_from_mnemonic(opts.clone().name, &opts.mnemonic).map_err(|e| error::Kind::KeyBase.context(e))?;

let key = chain.keybase.get(opts.name).unwrap();
Ok(key.clone().address)
Ok(address)
}

0 comments on commit 5b92633

Please sign in to comment.