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
This commit is contained in:
jaspervdm 2019-03-19 17:13:49 +01:00 committed by Yeastplume
parent 7fad5b040f
commit f4d3b2e204
5 changed files with 22 additions and 20 deletions

View file

@ -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

View file

@ -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,
@ -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<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);

View file

@ -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)?;

View file

@ -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);

View file

@ -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())
}