forked from kaspanet/rusty-kaspa
-
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
5 changed files
with
39 additions
and
31 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
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; |
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
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,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) | ||
} |
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