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

Improve input signature capability #85

Merged
merged 8 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions consensus/core/src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
sighash::{calc_schnorr_signature_hash, SigHashReusedValues},
sighash_type::{SigHashType, SIG_HASH_ALL},
},
tx::SignableTransaction,
tx::{SignableTransaction, VerifiableTransaction},
};
use itertools::Itertools;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -154,10 +154,10 @@ pub fn sign_with_multiple_v2(mut mutable_tx: SignableTransaction, privkeys: &[[u
}

/// Sign a transaction input with a sighash_type using schnorr
pub fn sign_input(tx: SignableTransaction, input_index: usize, private_key: &[u8; 32], hash_type: SigHashType) -> Vec<u8> {
pub fn sign_input(tx: &impl VerifiableTransaction, input_index: usize, private_key: &[u8; 32], hash_type: SigHashType) -> Vec<u8> {
let mut reused_values = SigHashReusedValues::new();

let hash = calc_schnorr_signature_hash(&tx.as_verifiable(), input_index, hash_type, &mut reused_values);
let hash = calc_schnorr_signature_hash(tx, input_index, hash_type, &mut reused_values);
let msg = secp256k1::Message::from_digest_slice(hash.as_bytes().as_slice()).unwrap();
let schnorr_key = secp256k1::Keypair::from_seckey_slice(secp256k1::SECP256K1, private_key).unwrap();
let sig: [u8; 64] = *schnorr_key.sign_schnorr(msg).as_ref();
Expand All @@ -166,7 +166,7 @@ pub fn sign_input(tx: SignableTransaction, input_index: usize, private_key: &[u8
std::iter::once(65u8).chain(sig).chain([hash_type.to_u8()]).collect()
}

pub fn verify(tx: &impl crate::tx::VerifiableTransaction) -> Result<(), Error> {
pub fn verify(tx: &impl VerifiableTransaction) -> Result<(), Error> {
let mut reused_values = SigHashReusedValues::new();
for (i, (input, entry)) in tx.populated_inputs().enumerate() {
if input.signature_script.is_empty() {
Expand Down
19 changes: 17 additions & 2 deletions wallet/core/src/tx/generator/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,11 @@ impl PendingTransaction {
Ok(())
}

pub fn sign_input(&self, input_index: usize, private_key: &[u8; 32], hash_type: SigHashType) -> Result<Vec<u8>> {
pub fn create_input_signature(&self, input_index: usize, private_key: &[u8; 32], hash_type: SigHashType) -> Result<Vec<u8>> {
let mutable_tx = self.inner.signable_tx.lock()?.clone();
let verifiable_tx = mutable_tx.as_verifiable();

Ok(sign_input(mutable_tx, input_index, private_key, hash_type))
Ok(sign_input(&verifiable_tx, input_index, private_key, hash_type))
}

pub fn fill_input(&self, input_index: usize, signature_script: Vec<u8>) -> Result<()> {
Expand All @@ -238,6 +239,20 @@ impl PendingTransaction {
Ok(())
}

pub fn sign_input(&self, input_index: usize, private_key: &[u8; 32], hash_type: SigHashType) -> Result<()> {
let mut mutable_tx = self.inner.signable_tx.lock()?.clone();

let signature_script = {
let verifiable_tx = &mutable_tx.as_verifiable();
sign_input(verifiable_tx, input_index, private_key, hash_type)
};

mutable_tx.tx.inputs[input_index].signature_script = signature_script;
*self.inner.signable_tx.lock().unwrap() = mutable_tx;

Ok(())
}

pub fn try_sign_with_keys(&self, privkeys: &[[u8; 32]], check_fully_signed: Option<bool>) -> Result<()> {
let mutable_tx = self.inner.signable_tx.lock()?.clone();
let signed = sign_with_multiple_v2(mutable_tx, privkeys);
Expand Down
16 changes: 10 additions & 6 deletions wallet/core/src/wasm/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use js_sys::Array;
use kaspa_consensus_client::{sign_with_multiple_v3, Transaction};
use kaspa_consensus_core::hashing::wasm::SighashType;
use kaspa_consensus_core::sign::sign_input;
use kaspa_consensus_core::tx::{PopulatedTransaction, SignableTransaction};
use kaspa_consensus_core::tx::PopulatedTransaction;
use kaspa_consensus_core::{hashing::sighash_type::SIG_HASH_ALL, sign::verify};
use kaspa_hashes::Hash;
use kaspa_wallet_keys::privatekey::PrivateKey;
Expand Down Expand Up @@ -71,16 +71,20 @@ pub fn sign(tx: Transaction, privkeys: &[[u8; 32]]) -> Result<Transaction> {
/// @category Wallet SDK
#[wasm_bindgen(js_name = "createInputSignature")]
pub fn create_input_signature(
tx: Transaction,
tx: &Transaction,
input_index: u8,
private_key: &PrivateKey,
sighash_type: Option<SighashType>,
) -> Result<HexString> {
let (cctx, _) = tx.tx_and_utxos()?;
let mutable_tx = SignableTransaction::new(cctx);
let (cctx, utxos) = tx.tx_and_utxos()?;
let populated_transaction = PopulatedTransaction::new(&cctx, utxos);

let signature =
sign_input(mutable_tx, input_index.into(), &private_key.secret_bytes(), sighash_type.unwrap_or(SighashType::All).into());
let signature = sign_input(
&populated_transaction,
input_index.into(),
&private_key.secret_bytes(),
sighash_type.unwrap_or(SighashType::All).into(),
);

Ok(signature.to_hex().into())
}
Expand Down
23 changes: 19 additions & 4 deletions wallet/core/src/wasm/tx/generator/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,18 @@ impl PendingTransaction {
self.inner.utxo_entries().values().map(|utxo_entry| JsValue::from(utxo_entry.clone())).collect()
}

#[wasm_bindgen(js_name = signInput)]
pub fn sign_input(&self, input_index: u8, private_key: &PrivateKey, sighash_type: Option<SighashType>) -> Result<HexString> {
let signature =
self.inner.sign_input(input_index.into(), &private_key.secret_bytes(), sighash_type.unwrap_or(SighashType::All).into())?;
#[wasm_bindgen(js_name = createInputSignature)]
pub fn create_input_signature(
&self,
input_index: u8,
private_key: &PrivateKey,
sighash_type: Option<SighashType>,
) -> Result<HexString> {
let signature = self.inner.create_input_signature(
input_index.into(),
&private_key.secret_bytes(),
sighash_type.unwrap_or(SighashType::All).into(),
)?;

Ok(signature.to_hex().into())
}
Expand All @@ -85,6 +93,13 @@ impl PendingTransaction {
self.inner.fill_input(input_index.into(), signature_script.try_as_vec_u8()?)
}

#[wasm_bindgen(js_name = signInput)]
pub fn sign_input(&self, input_index: u8, private_key: &PrivateKey, sighash_type: Option<SighashType>) -> Result<()> {
self.inner.sign_input(input_index.into(), &private_key.secret_bytes(), sighash_type.unwrap_or(SighashType::All).into())?;

Ok(())
}

/// Sign transaction with supplied [`Array`] or [`PrivateKey`] or an array of
/// raw private key bytes (encoded as `Uint8Array` or as hex strings)
pub fn sign(&self, js_value: PrivateKeyArrayT, check_fully_signed: Option<bool>) -> Result<()> {
Expand Down
Loading