From f4d3b2e204e18dcd98ae1df6c8fc98e179184a6e Mon Sep 17 00:00:00 2001 From: jaspervdm Date: Tue, 19 Mar 2019 17:13:49 +0100 Subject: [PATCH] Small QoL improvements for wallet developers (#2651) * 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 --- core/src/libtx/build.rs | 3 ++- core/src/libtx/proof.rs | 25 ++++++------------------- keychain/src/keychain.rs | 9 +++++++++ keychain/src/types.rs | 1 + wallet/src/types.rs | 4 ++++ 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/core/src/libtx/build.rs b/core/src/libtx/build.rs index 7ed3b2ec8..0930623fc 100644 --- a/core/src/libtx/build.rs +++ b/core/src/libtx/build.rs @@ -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 diff --git a/core/src/libtx/proof.rs b/core/src/libtx/proof.rs index 8a523dd63..cc0a6bb50 100644 --- a/core/src/libtx/proof.rs +++ b/core/src/libtx/proof.rs @@ -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, commit: &Commitment) -> Result -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, @@ -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))) @@ -80,7 +65,9 @@ pub fn rewind( 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); diff --git a/keychain/src/keychain.rs b/keychain/src/keychain.rs index 31a5db408..9f1948990 100644 --- a/keychain/src/keychain.rs +++ b/keychain/src/keychain.rs @@ -142,6 +142,15 @@ impl Keychain for ExtKeychain { Ok(BlindingFactor::from_secret_key(sum)) } + fn create_nonce(&self, commit: &Commitment) -> Result { + // 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 { let skey = self.derive_key(amount, id)?; let sig = self.secp.sign(msg, &skey)?; diff --git a/keychain/src/types.rs b/keychain/src/types.rs index 66e5ba95f..51f41bf71 100644 --- a/keychain/src/types.rs +++ b/keychain/src/types.rs @@ -468,6 +468,7 @@ pub trait Keychain: Sync + Send + Clone { fn derive_key(&self, amount: u64, id: &Identifier) -> Result; fn commit(&self, amount: u64, id: &Identifier) -> Result; fn blind_sum(&self, blind_sum: &BlindSum) -> Result; + fn create_nonce(&self, commit: &Commitment) -> Result; fn sign(&self, msg: &Message, amount: u64, id: &Identifier) -> Result; fn sign_with_blinding(&self, _: &Message, _: &BlindingFactor) -> Result; fn set_use_switch_commits(&mut self, value: bool); diff --git a/wallet/src/types.rs b/wallet/src/types.rs index 5031e0fb7..105090e2a 100644 --- a/wallet/src/types.rs +++ b/wallet/src/types.rs @@ -131,6 +131,10 @@ impl WalletSeed { Ok(WalletSeed::from_bytes(&bytes)) } + pub fn to_bytes(&self) -> Vec { + self.0.clone() + } + pub fn to_hex(&self) -> String { util::to_hex(self.0.to_vec()) }