From fb178d97677871249e9cae83a801935db579e269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Wed, 1 Mar 2023 12:18:46 +0000 Subject: [PATCH] using base58-encoded serialization --- gateway/src/commands/sign.rs | 30 ++++++++++++++----- mixnode/src/commands/sign.rs | 30 ++++++++++++++----- .../signatures/ed25519_signing_payload.rs | 4 +-- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/gateway/src/commands/sign.rs b/gateway/src/commands/sign.rs index 3bffdf8e01..5037346b4b 100644 --- a/gateway/src/commands/sign.rs +++ b/gateway/src/commands/sign.rs @@ -97,15 +97,29 @@ fn print_signed_text(private_key: &identity::PrivateKey, text: &str) { } fn print_signed_contract_msg(private_key: &identity::PrivateKey, raw_msg: &str) { + let trimmed = raw_msg.trim(); + println!(">>> attempting to sign {trimmed}"); + + let Ok(decoded) = bs58::decode(trimmed).into_vec() else { + println!("it seems you have incorrectly copied the message to sign. Make sure you didn't accidentally skip any characters"); + return; + }; + + println!(">>> decoding the message..."); + // we don't really care about what particular information is embedded inside of it, - // we just want to know if user correctly copied the string, i.e. whether it's a valid json - if serde_json::from_str::(raw_msg).is_err() { - println!("it seems you have incorrectly copied the message to sign. Make sure you didn't accidentally skip any characters") - } else { - let msg = raw_msg.trim(); - let signature = private_key.sign(msg.trim().as_bytes()).to_base58_string(); - println!("The base58-encoded signature on\n{msg}\nis:\n{signature}"); - } + // we just want to know if user correctly copied the string, i.e. whether it's a valid bs58 encoded json + if serde_json::from_slice::(&decoded).is_err() { + println!("it seems you have incorrectly copied the message to sign. Make sure you didn't accidentally skip any characters"); + return; + }; + + // if this is a valid json, it MUST be a valid string + let decoded_string = String::from_utf8(decoded.clone()).unwrap(); + println!(">>> message to sign: {decoded_string}"); + + let signature = private_key.sign(&decoded).to_base58_string(); + println!(">>> The base58-encoded signature is:\n{signature}"); } pub fn execute(args: Sign) -> Result<(), Box> { diff --git a/mixnode/src/commands/sign.rs b/mixnode/src/commands/sign.rs index c9ace438ab..8189c6d7de 100644 --- a/mixnode/src/commands/sign.rs +++ b/mixnode/src/commands/sign.rs @@ -78,15 +78,29 @@ fn print_signed_text(private_key: &identity::PrivateKey, text: &str) { } fn print_signed_contract_msg(private_key: &identity::PrivateKey, raw_msg: &str) { + let trimmed = raw_msg.trim(); + println!(">>> attempting to sign {trimmed}"); + + let Ok(decoded) = bs58::decode(trimmed).into_vec() else { + println!("it seems you have incorrectly copied the message to sign. Make sure you didn't accidentally skip any characters"); + return; + }; + + println!(">>> decoding the message..."); + // we don't really care about what particular information is embedded inside of it, - // we just want to know if user correctly copied the string, i.e. whether it's a valid json - if serde_json::from_str::(raw_msg).is_err() { - println!("it seems you have incorrectly copied the message to sign. Make sure you didn't accidentally skip any characters") - } else { - let msg = raw_msg.trim(); - let signature = private_key.sign(msg.trim().as_bytes()).to_base58_string(); - println!("The base58-encoded signature on\n{msg}\nis:\n{signature}"); - } + // we just want to know if user correctly copied the string, i.e. whether it's a valid bs58 encoded json + if serde_json::from_slice::(&decoded).is_err() { + println!("it seems you have incorrectly copied the message to sign. Make sure you didn't accidentally skip any characters"); + return; + }; + + // if this is a valid json, it MUST be a valid string + let decoded_string = String::from_utf8(decoded.clone()).unwrap(); + println!(">>> message to sign: {decoded_string}"); + + let signature = private_key.sign(&decoded).to_base58_string(); + println!(">>> The base58-encoded signature is:\n{signature}"); } pub(crate) fn execute(args: &Sign) { diff --git a/nym-wallet/src-tauri/src/operations/signatures/ed25519_signing_payload.rs b/nym-wallet/src-tauri/src/operations/signatures/ed25519_signing_payload.rs index ba5f0a2a6d..d539cf5eb3 100644 --- a/nym-wallet/src-tauri/src/operations/signatures/ed25519_signing_payload.rs +++ b/nym-wallet/src-tauri/src/operations/signatures/ed25519_signing_payload.rs @@ -35,7 +35,7 @@ async fn mixnode_bonding_msg_payload( let msg = create_mixnode_bonding_sign_payload(client, mixnode, cost_params, pledge_base, vesting) .await?; - Ok(msg.to_json_string()?) + Ok(msg.to_base58_string()?) } async fn gateway_bonding_msg_payload( @@ -58,7 +58,7 @@ async fn gateway_bonding_msg_payload( // TODO: decide on exact structure here. Json? base58? some hash? // to be determined let msg = create_gateway_bonding_sign_payload(client, gateway, pledge_base, vesting).await?; - Ok(msg.to_json_string()?) + Ok(msg.to_base58_string()?) } #[tauri::command]