-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
282 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use { | ||
crate::{blank_to_sign, to_spend, Wallet}, | ||
base64::{engine::general_purpose, Engine}, | ||
bitcoin::{ | ||
consensus::Encodable, | ||
key::{Keypair, TapTweak}, | ||
psbt::Psbt, | ||
secp256k1::{self, Secp256k1, XOnlyPublicKey}, | ||
sighash::{self, SighashCache, TapSighashType}, | ||
Address, Amount, PrivateKey, Transaction, TxOut, Witness, | ||
}, | ||
}; | ||
|
||
fn to_sign_psbt( | ||
to_spend_tx: Transaction, | ||
to_sign_tx: Transaction, | ||
) -> Result<Psbt, bitcoin::psbt::Error> { | ||
let mut psbt = Psbt::from_unsigned_tx(to_sign_tx)?; | ||
psbt.inputs[0].witness_utxo = Some(TxOut { | ||
value: Amount::from_sat(0), | ||
script_pubkey: to_spend_tx.output[0].script_pubkey.clone(), | ||
}); | ||
|
||
Ok(psbt) | ||
} | ||
|
||
fn sig_witness(to_spend_tx: Transaction, to_sign: Psbt, private_key: PrivateKey) -> Witness { | ||
let mut to_sign = to_sign.clone(); | ||
|
||
let secp = Secp256k1::new(); | ||
let private_key = private_key; | ||
let key_pair = Keypair::from_secret_key(&secp, &private_key.inner); | ||
let (x_only_public_key, _parity) = XOnlyPublicKey::from_keypair(&key_pair); | ||
to_sign.inputs[0].tap_internal_key = Some(x_only_public_key); | ||
|
||
let sighash_type = TapSighashType::All; | ||
|
||
let mut sighash_cache = SighashCache::new(to_sign.unsigned_tx.clone()); | ||
|
||
let sighash = sighash_cache | ||
.taproot_key_spend_signature_hash( | ||
0, | ||
&sighash::Prevouts::All(&[TxOut { | ||
value: Amount::from_sat(0), | ||
script_pubkey: to_spend_tx.output[0].clone().script_pubkey, | ||
}]), | ||
sighash_type, | ||
) | ||
.expect("signature hash should compute"); | ||
|
||
let key_pair = key_pair | ||
.tap_tweak(&secp, to_sign.inputs[0].tap_merkle_root) | ||
.to_inner(); | ||
|
||
let sig = secp.sign_schnorr_no_aux_rand( | ||
&secp256k1::Message::from_digest_slice(sighash.as_ref()) | ||
.expect("should be cryptographically secure hash"), | ||
&key_pair, | ||
); | ||
|
||
let witness = sighash_cache | ||
.witness_mut(0) | ||
.expect("getting mutable witness reference should work"); | ||
|
||
witness.push( | ||
bitcoin::taproot::Signature { | ||
sig, | ||
hash_ty: sighash_type, | ||
} | ||
.to_vec(), | ||
); | ||
|
||
witness.to_owned() | ||
} | ||
|
||
pub fn simple_sign(address: &Address, message: &str, wallet: &Wallet) -> String { | ||
let to_spend_tx = to_spend(address, message); | ||
let to_sign_tx = blank_to_sign(to_spend_tx.clone()); | ||
let psbt = to_sign_psbt(to_spend_tx.clone(), to_sign_tx).unwrap(); | ||
|
||
let witness = sig_witness(to_spend_tx, psbt, wallet.private_key); | ||
|
||
let mut buffer = Vec::new(); | ||
witness.consensus_encode(&mut buffer).unwrap(); | ||
|
||
general_purpose::STANDARD.encode(buffer) | ||
} | ||
|
||
pub fn full_sign(address: &Address, message: &str, wallet: &Wallet) -> String { | ||
let to_spend_tx = to_spend(address, message); | ||
let mut to_sign_tx = blank_to_sign(to_spend_tx.clone()); | ||
let psbt = to_sign_psbt(to_spend_tx.clone(), to_sign_tx.clone()).unwrap(); | ||
|
||
to_sign_tx.input[0].witness = sig_witness(to_spend_tx, psbt, wallet.private_key); | ||
|
||
let mut buffer = Vec::new(); | ||
to_sign_tx.consensus_encode(&mut buffer).unwrap(); | ||
|
||
general_purpose::STANDARD.encode(buffer) | ||
} |
Oops, something went wrong.