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.
Merge branch 'kaspanet:master' into legacy
- Loading branch information
Showing
5 changed files
with
96 additions
and
11 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
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,45 @@ | ||
use crate::imports::*; | ||
use crate::message::*; | ||
use kaspa_consensus_wasm::{PrivateKey, PublicKey}; | ||
|
||
/// Signs a message with the given private key | ||
/// @param {object} value - an object containing { message: String, privateKey: String|PrivateKey } | ||
/// @returns {String} the signature, in hex string format | ||
#[wasm_bindgen(js_name = signMessage, skip_jsdoc)] | ||
pub fn js_sign_message(value: JsValue) -> Result<String, Error> { | ||
if let Some(object) = Object::try_from(&value) { | ||
let private_key = object.get::<PrivateKey>("privateKey")?; | ||
let raw_msg = object.get_string("message")?; | ||
let mut privkey_bytes = [0u8; 32]; | ||
|
||
privkey_bytes.copy_from_slice(&private_key.secret_bytes()); | ||
|
||
let pm = PersonalMessage(&raw_msg); | ||
|
||
let sig_vec = sign_message(&pm, &privkey_bytes)?; | ||
|
||
Ok(faster_hex::hex_string(sig_vec.as_slice())) | ||
} else { | ||
Err(Error::custom("Failed to parse input")) | ||
} | ||
} | ||
|
||
/// Verifies with a public key the signature of the given message | ||
/// @param {object} value - an object containing { message: String, signature: String, publicKey: String|PublicKey } | ||
/// @returns {bool} true if the signature can be verified with the given public key and message, false otherwise | ||
#[wasm_bindgen(js_name = verifyMessage, skip_jsdoc)] | ||
pub fn js_verify_message(value: JsValue) -> Result<bool, Error> { | ||
if let Some(object) = Object::try_from(&value) { | ||
let public_key = object.get::<PublicKey>("publicKey")?; | ||
let raw_msg = object.get_string("message")?; | ||
let signature = object.get_string("signature")?; | ||
|
||
let pm = PersonalMessage(&raw_msg); | ||
let mut signature_bytes = [0u8; 64]; | ||
faster_hex::hex_decode(signature.as_bytes(), &mut signature_bytes)?; | ||
|
||
Ok(verify_message(&pm, &signature_bytes.to_vec(), &public_key.into()).is_ok()) | ||
} else { | ||
Err(Error::custom("Failed to parse input")) | ||
} | ||
} |
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 @@ | ||
let kaspa = require('./kaspa/kaspa_wasm'); | ||
let { | ||
PrivateKey, | ||
PublicKey, | ||
signMessage, | ||
verifyMessage, | ||
} = kaspa; | ||
|
||
kaspa.initConsolePanicHook(); | ||
|
||
let message = 'Hello Kaspa!'; | ||
let privkey = 'B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF'; | ||
let pubkey = 'DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659'; | ||
|
||
function runDemo(message, privateKey, publicKey) { | ||
let signature = signMessage({message, privateKey}); | ||
|
||
console.info(`Message: ${message} => Signature: ${signature}`); | ||
|
||
if (verifyMessage({message, signature, publicKey})) { | ||
console.info('Signature verified!'); | ||
} else { | ||
console.info('Signature is invalid!'); | ||
} | ||
} | ||
|
||
// Using strings: | ||
runDemo(message, privkey, pubkey); | ||
// Using Objects: | ||
runDemo(message, new PrivateKey(privkey), new PublicKey(pubkey)); |