Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update taproot signatures to use a deterministic nonce #41

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bitcoin = "0.32.3"
hex = "0.4.3"
miniscript = "12.2.0"
snafu = { version = "0.8.5", default-features = false, features = ["rust_1_61", "std"] }
secp256k1 = { version = "0.29.1", features = ["global-context", "rand-std"] }

[dev-dependencies]
pretty_assertions = "1.4.1"
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use {
blockdata::script,
consensus::Decodable,
consensus::Encodable,
key::{Keypair, TapTweak},
key::{Keypair, Secp256k1, TapTweak, UntweakedKeypair},
0xfinetuned marked this conversation as resolved.
Show resolved Hide resolved
opcodes,
psbt::Psbt,
script::PushBytes,
secp256k1::{self, schnorr::Signature, Message, Secp256k1, XOnlyPublicKey},
secp256k1::{self, schnorr::Signature, Message, SecretKey, XOnlyPublicKey},
0xfinetuned marked this conversation as resolved.
Show resolved Hide resolved
sighash::{self, SighashCache, TapSighashType},
transaction::Version,
Address, Amount, EcdsaSighashType, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence,
Expand Down
49 changes: 46 additions & 3 deletions src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,49 @@ pub fn sign_full(
to_sign.extract_tx().context(error::TransactionExtract)
}

pub fn sign_message_bip322(
keypair: &UntweakedKeypair,
msg: &[u8],
network: bitcoin::Network,
) -> [u8; 64] {
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
let secp = Secp256k1::new();
let xpubk = XOnlyPublicKey::from_keypair(keypair).0;
let private_key = PrivateKey::new(SecretKey::from_keypair(keypair), network);

let address = Address::p2tr(&secp, xpubk, None, network);

let to_spend = create_to_spend(&address, msg).unwrap();
let mut to_sign = create_to_sign(&to_spend, None).unwrap();

let witness = match address.witness_program() {
Some(witness_program) => {
let version = witness_program.version().to_num();
let program_len = witness_program.program().len();

match version {
1 => {
if program_len != 32 {
panic!("not key spend path");
}
create_message_signature_taproot(&to_spend, &to_sign, private_key)
}
_ => {
panic!("unsuported address");
}
}
}
None => {
panic!("unsuported address");
}
};

to_sign.inputs[0].final_script_witness = Some(witness);

let signature = to_sign.extract_tx().unwrap().input[0].witness.clone();

signature.to_vec()[0][..64].try_into().unwrap()
}

fn create_message_signature_p2wpkh(
to_spend_tx: &Transaction,
to_sign: &Psbt,
Expand Down Expand Up @@ -175,8 +218,8 @@ fn create_message_signature_taproot(
.tap_tweak(&secp, to_sign.inputs[0].tap_merkle_root)
.to_inner();

let signature = secp.sign_schnorr_no_aux_rand(
&secp256k1::Message::from_digest_slice(sighash.as_ref())
let sig = secp.sign_schnorr(
0xfinetuned marked this conversation as resolved.
Show resolved Hide resolved
&bitcoin::secp256k1::Message::from_digest_slice(sighash.as_ref())
0xfinetuned marked this conversation as resolved.
Show resolved Hide resolved
.expect("should be cryptographically secure hash"),
&key_pair,
);
Expand All @@ -187,7 +230,7 @@ fn create_message_signature_taproot(

witness.push(
bitcoin::taproot::Signature {
signature,
signature: sig,
0xfinetuned marked this conversation as resolved.
Show resolved Hide resolved
sighash_type,
}
.to_vec(),
Expand Down
6 changes: 4 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;

pub type BIP322Result<T = (), E = error::Error> = std::result::Result<T, E>;
raphjaph marked this conversation as resolved.
Show resolved Hide resolved

const TAG: &str = "BIP0322-signed-message";

/// Create the tagged message hash.
Expand All @@ -14,7 +16,7 @@ pub fn message_hash(message: &[u8]) -> Vec<u8> {
}

/// Create the `to_spend` transaction.
pub fn create_to_spend(address: &Address, message: &[u8]) -> Result<Transaction> {
pub fn create_to_spend(address: &Address, message: &[u8]) -> BIP322Result<Transaction> {
Ok(Transaction {
version: Version(0),
lock_time: LockTime::ZERO,
Expand All @@ -40,7 +42,7 @@ pub fn create_to_spend(address: &Address, message: &[u8]) -> Result<Transaction>
}

/// Create the `to_sign` transaction.
pub fn create_to_sign(to_spend: &Transaction, witness: Option<Witness>) -> Result<Psbt> {
pub fn create_to_sign(to_spend: &Transaction, witness: Option<Witness>) -> BIP322Result<Psbt> {
let inputs = vec![TxIn {
previous_output: OutPoint {
txid: to_spend.compute_txid(),
Expand Down
Loading
Loading