Skip to content

Commit

Permalink
Small QoL improvements for wallet developers (#2651)
Browse files Browse the repository at this point in the history
* Small changes for wallet devs

* Move create_nonce into Keychain trait

* Replace match by map_err

* Add flag to Slate to skip fee check

* Fix secp dependency

* Remove check_fee flag in Slate
  • Loading branch information
jaspervdm authored and yeastplume committed Mar 19, 2019
1 parent 7fad5b0 commit f4d3b2e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
3 changes: 2 additions & 1 deletion core/src/libtx/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub struct Context<'a, K>
where
K: Keychain,
{
keychain: &'a K,
/// The keychain used for key derivation
pub keychain: &'a K,
}

/// Function type returned by the transaction combinators. Transforms a
Expand Down
25 changes: 6 additions & 19 deletions core/src/libtx/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,12 @@

//! Rangeproof library functions
use crate::blake2;
use crate::keychain::{Identifier, Keychain};
use crate::libtx::error::{Error, ErrorKind};
use crate::util::secp::key::SecretKey;
use crate::util::secp::pedersen::{Commitment, ProofInfo, ProofMessage, RangeProof};
use crate::util::secp::{self, Secp256k1};

fn create_nonce<K>(k: &K, commit: &Commitment) -> Result<SecretKey, Error>
where
K: Keychain,
{
// hash(commit|wallet root secret key (m)) as nonce
let root_key = k.derive_key(0, &K::root_key_id())?;
let res = blake2::blake2b::blake2b(32, &commit.0, &root_key.0[..]);
let res = res.as_bytes();
match SecretKey::from_slice(k.secp(), &res) {
Ok(sk) => Ok(sk),
Err(e) => Err(ErrorKind::RangeProof(
format!("Unable to create nonce: {:?}", e).to_string(),
))?,
}
}

/// Create a bulletproof
pub fn create<K>(
k: &K,
Expand All @@ -50,7 +33,9 @@ where
{
let commit = k.commit(amount, key_id)?;
let skey = k.derive_key(amount, key_id)?;
let nonce = create_nonce(k, &commit)?;
let nonce = k
.create_nonce(&commit)
.map_err(|e| ErrorKind::RangeProof(e.to_string()))?;
let message = ProofMessage::from_bytes(&key_id.serialize_path());
Ok(k.secp()
.bullet_proof(amount, skey, nonce, extra_data, Some(message)))
Expand Down Expand Up @@ -80,7 +65,9 @@ pub fn rewind<K>(
where
K: Keychain,
{
let nonce = create_nonce(k, &commit)?;
let nonce = k
.create_nonce(&commit)
.map_err(|e| ErrorKind::RangeProof(e.to_string()))?;
let proof_message = k
.secp()
.rewind_bullet_proof(commit, nonce, extra_data, proof);
Expand Down
9 changes: 9 additions & 0 deletions keychain/src/keychain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ impl Keychain for ExtKeychain {
Ok(BlindingFactor::from_secret_key(sum))
}

fn create_nonce(&self, commit: &Commitment) -> Result<SecretKey, Error> {
// hash(commit|wallet root secret key (m)) as nonce
let root_key = self.derive_key(0, &Self::root_key_id())?;
let res = blake2::blake2b::blake2b(32, &commit.0, &root_key.0[..]);
let res = res.as_bytes();
SecretKey::from_slice(&self.secp, &res)
.map_err(|e| Error::RangeProof(format!("Unable to create nonce: {:?}", e).to_string()))
}

fn sign(&self, msg: &Message, amount: u64, id: &Identifier) -> Result<Signature, Error> {
let skey = self.derive_key(amount, id)?;
let sig = self.secp.sign(msg, &skey)?;
Expand Down
1 change: 1 addition & 0 deletions keychain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ pub trait Keychain: Sync + Send + Clone {
fn derive_key(&self, amount: u64, id: &Identifier) -> Result<SecretKey, Error>;
fn commit(&self, amount: u64, id: &Identifier) -> Result<Commitment, Error>;
fn blind_sum(&self, blind_sum: &BlindSum) -> Result<BlindingFactor, Error>;
fn create_nonce(&self, commit: &Commitment) -> Result<SecretKey, Error>;
fn sign(&self, msg: &Message, amount: u64, id: &Identifier) -> Result<Signature, Error>;
fn sign_with_blinding(&self, _: &Message, _: &BlindingFactor) -> Result<Signature, Error>;
fn set_use_switch_commits(&mut self, value: bool);
Expand Down
4 changes: 4 additions & 0 deletions wallet/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ impl WalletSeed {
Ok(WalletSeed::from_bytes(&bytes))
}

pub fn to_bytes(&self) -> Vec<u8> {
self.0.clone()
}

pub fn to_hex(&self) -> String {
util::to_hex(self.0.to_vec())
}
Expand Down

0 comments on commit f4d3b2e

Please sign in to comment.