Skip to content

Commit

Permalink
Expose message signing primitives to NodeJS (#4655)
Browse files Browse the repository at this point in the history
  • Loading branch information
andiflabs committed May 29, 2024
1 parent 7de8647 commit 4e30e00
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions ironfish-rust-nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod multisig;
pub mod nacl;
pub mod rolling_filter;
pub mod signal_catcher;
pub mod signing;
pub mod structs;

fn to_napi_err(err: impl Display) -> napi::Error {
Expand Down
39 changes: 39 additions & 0 deletions ironfish-rust-nodejs/src/signing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::to_napi_err;
use ironfish::{
keys::{PublicAddress, SaplingKey},
serializing::{bytes_to_hex, hex_to_vec_bytes},
signing::MessageSignature,
};
use napi::{bindgen_prelude::*, JsBuffer};
use napi_derive::napi;
use rand::thread_rng;

#[napi(namespace = "signing")]
pub fn sign_message(secret_key: String, message: JsBuffer) -> Result<String> {
let secret_key = hex_to_vec_bytes(&secret_key).map_err(to_napi_err)?;
let secret_key = SaplingKey::read(&secret_key[..]).map_err(to_napi_err)?;

let message = message.into_value()?;

let signature =
ironfish::signing::sign_message(&secret_key, message.as_ref(), thread_rng()).to_bytes();
Ok(bytes_to_hex(&signature[..]))
}

#[napi(namespace = "signing")]
pub fn verify_message(public_address: String, message: JsBuffer, signature: String) -> Result<()> {
let public_address = hex_to_vec_bytes(&public_address).map_err(to_napi_err)?;
let public_address = PublicAddress::read(&public_address[..]).map_err(to_napi_err)?;

let message = message.into_value()?;

let signature = hex_to_vec_bytes(&signature).map_err(to_napi_err)?;
let signature = MessageSignature::read(&signature[..]).map_err(to_napi_err)?;

ironfish::signing::verify_message(&public_address, message.as_ref(), &signature)
.map_err(to_napi_err)
}
2 changes: 1 addition & 1 deletion ironfish-rust/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl SaplingKey {
}

/// Load a new key from a Read implementation (e.g: socket, file)
pub fn read<R: io::Read>(reader: &mut R) -> Result<Self, IronfishError> {
pub fn read<R: io::Read>(mut reader: R) -> Result<Self, IronfishError> {
let mut spending_key = [0; SPEND_KEY_SIZE];
reader.read_exact(&mut spending_key)?;
Self::new(spending_key)
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust/src/keys/public_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl PublicAddress {
}

/// Load a public address from a Read implementation (e.g: socket, file)
pub fn read<R: io::Read>(reader: &mut R) -> Result<Self, IronfishError> {
pub fn read<R: io::Read>(mut reader: R) -> Result<Self, IronfishError> {
let mut address_bytes = [0; PUBLIC_ADDRESS_SIZE];
reader.read_exact(&mut address_bytes)?;
Self::new(&address_bytes)
Expand Down

0 comments on commit 4e30e00

Please sign in to comment.