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

feat: add signMessageWithoutRand method for kaspa wasm #587

Merged
merged 7 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 30 additions & 0 deletions wallet/core/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ pub fn sign_message(msg: &PersonalMessage, privkey: &[u8; 32]) -> Result<Vec<u8>
Ok(sig.to_vec())
}

/// Sign a message with the given private key without random
pub fn sign_message_without_rand(msg: &PersonalMessage, privkey: &[u8; 32]) -> Result<Vec<u8>, Error> {
witter-deland marked this conversation as resolved.
Show resolved Hide resolved
let hash = calc_personal_message_hash(msg);

let msg = secp256k1::Message::from_digest_slice(hash.as_bytes().as_slice())?;
let schnorr_key = secp256k1::Keypair::from_seckey_slice(secp256k1::SECP256K1, privkey)?;
let sig: [u8; 64] = *secp256k1::SECP256K1.sign_schnorr_no_aux_rand(&msg, &schnorr_key).as_ref();

Ok(sig.to_vec())
}

/// Verifies signed message.
///
/// Produces `Ok(())` if the signature matches the given message and [`secp256k1::Error`]
Expand Down Expand Up @@ -75,6 +86,21 @@ mod tests {
.unwrap();

verify_message(&pm, &sign_message(&pm, &privkey).expect("sign_message failed"), &pubkey).expect("verify_message failed");
verify_message(&pm, &sign_message_without_rand(&pm, &privkey).expect("sign_message failed"), &pubkey)
.expect("verify_message failed");
}

#[test]
fn test_basic_sign_without_rand_twice_should_get_same_signature() {
let pm = PersonalMessage("Hello Kaspa!");
let privkey: [u8; 32] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
];

let signature = sign_message_without_rand(&pm, &privkey).expect("sign_message failed");
let signature_twice = sign_message_without_rand(&pm, &privkey).expect("sign_message failed");
assert_eq!(signature, signature_twice);
}

#[test]
Expand All @@ -91,6 +117,8 @@ mod tests {
.unwrap();

verify_message(&pm, &sign_message(&pm, &privkey).expect("sign_message failed"), &pubkey).expect("verify_message failed");
verify_message(&pm, &sign_message_without_rand(&pm, &privkey).expect("sign_message failed"), &pubkey)
.expect("verify_message failed");
}

#[test]
Expand All @@ -111,6 +139,8 @@ Ut omnis magnam et accusamus earum rem impedit provident eum commodi repellat qu
.unwrap();

verify_message(&pm, &sign_message(&pm, &privkey).expect("sign_message failed"), &pubkey).expect("verify_message failed");
verify_message(&pm, &sign_message_without_rand(&pm, &privkey).expect("sign_message failed"), &pubkey)
.expect("verify_message failed");
}

#[test]
Expand Down
18 changes: 18 additions & 0 deletions wallet/core/src/wasm/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ pub fn js_sign_message(value: ISignMessage) -> Result<HexString, Error> {
}
}

/// Signs a message with the given private key without rand
/// @category Message Signing
#[wasm_bindgen(js_name = signMessageWithoutRand)]
pub fn js_sign_message_without_rand(value: ISignMessage) -> Result<HexString, Error> {
witter-deland marked this conversation as resolved.
Show resolved Hide resolved
if let Some(object) = Object::try_from(&value) {
let private_key = object.cast_into::<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_without_rand(&pm, &privkey_bytes)?;
privkey_bytes.zeroize();
Ok(faster_hex::hex_string(sig_vec.as_slice()).into())
} else {
Err(Error::custom("Failed to parse input"))
}
}

#[wasm_bindgen(typescript_custom_section)]
const TS_MESSAGE_TYPES: &'static str = r#"
/**
Expand Down