Skip to content

Commit

Permalink
bindings signer layout
Browse files Browse the repository at this point in the history
  • Loading branch information
smartgoo committed Nov 22, 2024
1 parent 263e5b4 commit d44cbbc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
2 changes: 2 additions & 0 deletions wallet/core/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[cfg(feature = "py-sdk")]
pub mod python;

pub mod signer;

#[cfg(any(feature = "wasm32-sdk", feature = "wasm32-core"))]
pub mod wasm;
6 changes: 4 additions & 2 deletions wallet/core/src/bindings/python/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use kaspa_wallet_keys::publickey::PublicKey;

#[pyfunction]
#[pyo3(name = "sign_message")]
pub fn py_sign_message(message: String, private_key: PrivateKey) -> PyResult<String> {
#[pyo3(signature = (message, private_key, no_aux_rand=false))]
pub fn py_sign_message(message: String, private_key: PrivateKey, no_aux_rand: bool) -> PyResult<String> {
let mut privkey_bytes = [0u8; 32];
privkey_bytes.copy_from_slice(&private_key.secret_bytes());
let pm = PersonalMessage(&message);
let sig_vec = sign_message(&pm, &privkey_bytes).map_err(|err| PyException::new_err(format!("{}", err)))?;
let sign_options = SignMessageOptions { no_aux_rand };
let sig_vec = sign_message(&pm, &privkey_bytes, &sign_options).map_err(|err| PyException::new_err(format!("{}", err)))?;
privkey_bytes.zeroize();
Ok(faster_hex::hex_string(sig_vec.as_slice()).into())
}
Expand Down
2 changes: 1 addition & 1 deletion wallet/core/src/bindings/python/signer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bindings::wasm::signer::{sign_hash, sign_transaction};
use crate::bindings::signer::{sign_hash, sign_transaction};
use crate::imports::*;
use kaspa_consensus_client::Transaction;
use kaspa_consensus_core::hashing::wasm::SighashType;
Expand Down
30 changes: 30 additions & 0 deletions wallet/core/src/bindings/signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::result::Result;
use kaspa_consensus_client::{sign_with_multiple_v3, Transaction};
use kaspa_consensus_core::tx::PopulatedTransaction;
use kaspa_consensus_core::{hashing::sighash_type::SIG_HASH_ALL, sign::verify};
use kaspa_hashes::Hash;

pub fn sign_transaction<'a>(tx: &'a Transaction, private_keys: &[[u8; 32]], verify_sig: bool) -> Result<&'a Transaction> {
let tx = sign(tx, private_keys)?;
if verify_sig {
let (cctx, utxos) = tx.tx_and_utxos()?;
let populated_transaction = PopulatedTransaction::new(&cctx, utxos);
verify(&populated_transaction)?;
}
Ok(tx)
}

/// Sign a transaction using schnorr, returns a new transaction with the signatures added.
/// The resulting transaction may be partially signed if the supplied keys are not sufficient
/// to sign all of its inputs.
pub fn sign<'a>(tx: &'a Transaction, privkeys: &[[u8; 32]]) -> Result<&'a Transaction> {
Ok(sign_with_multiple_v3(tx, privkeys)?.unwrap())
}

pub fn sign_hash(sig_hash: Hash, privkey: &[u8; 32]) -> Result<Vec<u8>> {
let msg = secp256k1::Message::from_digest_slice(sig_hash.as_bytes().as_slice())?;
let schnorr_key = secp256k1::Keypair::from_seckey_slice(secp256k1::SECP256K1, privkey)?;
let sig: [u8; 64] = *schnorr_key.sign_schnorr(msg).as_ref();
let signature = std::iter::once(65u8).chain(sig).chain([SIG_HASH_ALL.to_u8()]).collect();
Ok(signature)
}
30 changes: 2 additions & 28 deletions wallet/core/src/bindings/wasm/signer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::bindings::signer::{sign_hash, sign_transaction};
use crate::imports::*;
use crate::result::Result;
use js_sys::Array;
use kaspa_consensus_client::{sign_with_multiple_v3, Transaction};
use kaspa_consensus_client::Transaction;
use kaspa_consensus_core::hashing::wasm::SighashType;
use kaspa_consensus_core::sign::sign_input;
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;
use kaspa_wasm_core::types::HexString;
use serde_wasm_bindgen::from_value;
Expand Down Expand Up @@ -50,23 +49,6 @@ pub fn js_sign_transaction(tx: &Transaction, signer: &PrivateKeyArrayT, verify_s
}
}

fn sign_transaction<'a>(tx: &'a Transaction, private_keys: &[[u8; 32]], verify_sig: bool) -> Result<&'a Transaction> {
let tx = sign(tx, private_keys)?;
if verify_sig {
let (cctx, utxos) = tx.tx_and_utxos()?;
let populated_transaction = PopulatedTransaction::new(&cctx, utxos);
verify(&populated_transaction)?;
}
Ok(tx)
}

/// Sign a transaction using schnorr, returns a new transaction with the signatures added.
/// The resulting transaction may be partially signed if the supplied keys are not sufficient
/// to sign all of its inputs.
pub fn sign<'a>(tx: &'a Transaction, privkeys: &[[u8; 32]]) -> Result<&'a Transaction> {
Ok(sign_with_multiple_v3(tx, privkeys)?.unwrap())
}

/// `createInputSignature()` is a helper function to sign a transaction input with a specific SigHash type using a private key.
/// @category Wallet SDK
#[wasm_bindgen(js_name = "createInputSignature")]
Expand Down Expand Up @@ -96,11 +78,3 @@ pub fn sign_script_hash(script_hash: JsValue, privkey: &PrivateKey) -> Result<St
let result = sign_hash(script_hash, &privkey.into())?;
Ok(result.to_hex())
}

fn sign_hash(sig_hash: Hash, privkey: &[u8; 32]) -> Result<Vec<u8>> {
let msg = secp256k1::Message::from_digest_slice(sig_hash.as_bytes().as_slice())?;
let schnorr_key = secp256k1::Keypair::from_seckey_slice(secp256k1::SECP256K1, privkey)?;
let sig: [u8; 64] = *schnorr_key.sign_schnorr(msg).as_ref();
let signature = std::iter::once(65u8).chain(sig).chain([SIG_HASH_ALL.to_u8()]).collect();
Ok(signature)
}

0 comments on commit d44cbbc

Please sign in to comment.