Skip to content

Commit

Permalink
requested changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfinetuned committed Oct 28, 2024
1 parent 6b695b7 commit 20b6d7d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 75 deletions.
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, UntweakedKeypair},
key::{Keypair, TapTweak},
opcodes,
psbt::Psbt,
script::PushBytes,
secp256k1::{self, Secp256k1, schnorr::Signature, Message, SecretKey, XOnlyPublicKey},
secp256k1::{self, Secp256k1, Message, XOnlyPublicKey},
sighash::{self, SighashCache, TapSighashType},
transaction::Version,
Address, Amount, EcdsaSighashType, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence,
Expand Down
43 changes: 0 additions & 43 deletions src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,49 +95,6 @@ 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] {
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
6 changes: 2 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::*;

pub type BIP322Result<T = (), E = error::Error> = std::result::Result<T, E>;

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

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

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

/// Create the `to_sign` transaction.
pub fn create_to_sign(to_spend: &Transaction, witness: Option<Witness>) -> BIP322Result<Psbt> {
pub fn create_to_sign(to_spend: &Transaction, witness: Option<Witness>) -> Result<Psbt> {
let inputs = vec![TxIn {
previous_output: OutPoint {
txid: to_spend.compute_txid(),
Expand Down
29 changes: 3 additions & 26 deletions src/verify.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::*;

use crate::util::BIP322Result;

/// Verifies the BIP-322 simple from spec-compliant string encodings.
pub fn verify_simple_encoded(address: &str, message: &str, signature: &str) -> Result<()> {
let address = Address::from_str(address)
Expand Down Expand Up @@ -42,7 +40,7 @@ pub fn verify_full_encoded(address: &str, message: &str, to_sign: &str) -> Resul
}

/// Verifies the BIP-322 simple from proper Rust types.
pub fn verify_simple(address: &Address, message: &[u8], signature: Witness) -> BIP322Result<()> {
pub fn verify_simple(address: &Address, message: &[u8], signature: Witness) -> Result<()> {
verify_full(
address,
message,
Expand All @@ -53,7 +51,7 @@ pub fn verify_simple(address: &Address, message: &[u8], signature: Witness) -> B
}

/// Verifies the BIP-322 full from proper Rust types.
pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> BIP322Result<()> {
pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> Result<()> {
match address.to_address_data() {
// Handle P2TR (Taproot) addresses
AddressData::Segwit { witness_program }
Expand Down Expand Up @@ -87,27 +85,6 @@ pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> B
}
}

pub fn verify_message_bip322(
msg: &[u8],
pubkey: [u8; 32],
signature: [u8; 64],
uses_sighash_all: bool,
network: bitcoin::Network,
) -> BIP322Result<()> {
let mut signature = signature.to_vec();
if uses_sighash_all {
signature.push(1);
}
let mut witness = Witness::new();
witness.push(&signature);

let secp = Secp256k1::new();
let xpubk = XOnlyPublicKey::from_slice(&pubkey).unwrap();
let address = Address::p2tr(&secp, xpubk, None, network);

verify_simple(&address, msg, witness)
}

fn verify_full_p2wpkh(
address: &Address,
message: &[u8],
Expand Down Expand Up @@ -198,7 +175,7 @@ fn verify_full_p2tr(
message: &[u8],
to_sign: Transaction,
pub_key: XOnlyPublicKey,
) -> BIP322Result<()> {
) -> Result<()> {
use bitcoin::secp256k1::{schnorr::Signature, Message};

let to_spend = create_to_spend(address, message)?;
Expand Down

0 comments on commit 20b6d7d

Please sign in to comment.