Skip to content

Commit 0f98e33

Browse files
committed
Merge branch 'rust-tweak'
2 parents d966071 + b01e86d commit 0f98e33

File tree

5 files changed

+23
-36
lines changed

5 files changed

+23
-36
lines changed

src/keystore.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,18 +1003,8 @@ bool keystore_secp256k1_schnorr_bip86_sign(
10031003
bool keystore_secp256k1_get_private_key(
10041004
const uint32_t* keypath,
10051005
const size_t keypath_len,
1006-
bool tweak_bip86,
10071006
uint8_t* key_out)
10081007
{
1009-
if (tweak_bip86) {
1010-
secp256k1_keypair __attribute__((__cleanup__(_cleanup_keypair))) keypair = {0};
1011-
secp256k1_xonly_pubkey pubkey = {0};
1012-
if (!_schnorr_bip86_keypair(keypath, keypath_len, &keypair, &pubkey)) {
1013-
return false;
1014-
}
1015-
const secp256k1_context* ctx = wally_get_secp_context();
1016-
return secp256k1_keypair_sec(ctx, key_out, &keypair) == 1;
1017-
}
10181008
struct ext_key xprv __attribute__((__cleanup__(keystore_zero_xkey))) = {0};
10191009
if (!_get_xprv_twice(keypath, keypath_len, &xprv)) {
10201010
return false;

src/keystore.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,11 @@ USE_RESULT bool keystore_secp256k1_schnorr_bip86_sign(
308308
*
309309
* @param[in] keypath derivation keypath
310310
* @param[in] keypath_len number of elements in keypath
311-
* @param[in] tweak_bip86 if true, the resulting private key is tweaked with the BIP-86 tweak.
312311
* @param[out] key_out resulting private key, must be 32 bytes.
313312
*/
314313
USE_RESULT bool keystore_secp256k1_get_private_key(
315314
const uint32_t* keypath,
316315
size_t keypath_len,
317-
bool tweak_bip86,
318316
uint8_t* key_out);
319317

320318
#ifdef TESTING

src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use pb::btc_sign_next_response::Type as NextType;
3535
use sha2::{Digest, Sha256};
3636

3737
use bitcoin::hashes::Hash;
38+
use bitcoin::key::TapTweak;
3839

3940
use streaming_silent_payments::SilentPayment;
4041

@@ -720,13 +721,22 @@ async fn _process(request: &pb::BtcSignInitRequest) -> Result<Response, Error> {
720721
}
721722

722723
if let Some(ref mut silent_payment) = silent_payment {
723-
let private_key = bitcoin::secp256k1::SecretKey::from_slice(
724-
&bitbox02::keystore::secp256k1_get_private_key(
725-
&tx_input.keypath,
726-
is_taproot(script_config_account),
727-
)?,
724+
let keypair = bitcoin::key::UntweakedKeypair::from_seckey_slice(
725+
silent_payment.get_secp(),
726+
&bitbox02::keystore::secp256k1_get_private_key(&tx_input.keypath)?,
728727
)
729-
.map_err(|_| Error::Generic)?;
728+
.unwrap();
729+
// For Taproot, only key path spends are allowed in silent payments, and we need to
730+
// provide the key path spend private key, which means the internal key plus the tap
731+
// tweak.
732+
let private_key = if is_taproot(script_config_account) {
733+
keypair
734+
.tap_tweak(silent_payment.get_secp(), None)
735+
.to_inner()
736+
.secret_key()
737+
} else {
738+
keypair.secret_key()
739+
};
730740

731741
silent_payment
732742
.add_input(

src/rust/bitbox02/src/keystore.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,12 @@ pub fn encode_xpub_at_keypath(keypath: &[u32]) -> Result<Vec<u8>, ()> {
224224
}
225225
}
226226

227-
pub fn secp256k1_get_private_key(
228-
keypath: &[u32],
229-
tweak_bip86: bool,
230-
) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
227+
pub fn secp256k1_get_private_key(keypath: &[u32]) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
231228
let mut key = zeroize::Zeroizing::new(vec![0u8; 32]);
232229
match unsafe {
233230
bitbox02_sys::keystore_secp256k1_get_private_key(
234231
keypath.as_ptr(),
235232
keypath.len() as _,
236-
tweak_bip86,
237233
key.as_mut_ptr(),
238234
)
239235
} {
@@ -558,27 +554,16 @@ mod tests {
558554
fn test_secp256k1_get_private_key() {
559555
lock();
560556
let keypath = &[84 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 0];
561-
assert!(secp256k1_get_private_key(keypath, false).is_err());
557+
assert!(secp256k1_get_private_key(keypath).is_err());
562558

563559
mock_unlocked_using_mnemonic(
564560
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
565561
"",
566562
);
567563

568564
assert_eq!(
569-
hex::encode(secp256k1_get_private_key(keypath, false).unwrap()),
565+
hex::encode(secp256k1_get_private_key(keypath).unwrap()),
570566
"4604b4b710fe91f584fff084e1a9159fe4f8408fff380596a604948474ce4fa3"
571567
);
572-
573-
// See first test vector in
574-
// https://github.com/bitcoin/bips/blob/edffe529056f6dfd33d8f716fb871467c3c09263/bip-0086.mediawiki#test-vectors
575-
// The below privte key's public key is: a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c.
576-
assert_eq!(
577-
hex::encode(
578-
secp256k1_get_private_key(&[86 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 0], true)
579-
.unwrap()
580-
),
581-
"eaac016f36e8c18347fbacf05ab7966708fbfce7ce3bf1dc32a09dd0645db038",
582-
);
583568
}
584569
}

src/rust/streaming-silent-payments/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ impl SilentPayment {
169169
}
170170
}
171171

172+
pub fn get_secp(&self) -> &Secp256k1<secp256k1::All> {
173+
&self.secp
174+
}
175+
172176
/// This must be called for *every* input of the transaction.
173177
///
174178
/// Important: if the input type cannot be represented by `InputType`, the transaction must be

0 commit comments

Comments
 (0)