Feature/add wallet to gateway init (#984)

* Moving sign_text method into common/crypto to dry it up

* Moved bech32 address validation into common/crypto

* ibid

* Gateway now requires a --wallet-address arg on init
This commit is contained in:
Dave Hrycyszyn
2021-12-18 12:45:55 +00:00
committed by GitHub
parent ed2b515a83
commit 8e99ae8979
19 changed files with 101 additions and 48 deletions
+2 -1
View File
@@ -3,9 +3,10 @@
use std::process;
use crate::{config::Config, crypto::bech32_address_validation};
use crate::config::Config;
use clap::ArgMatches;
use colored::Colorize;
use crypto::bech32_address_validation;
use url::Url;
pub(crate) mod describe;
+2 -3
View File
@@ -3,7 +3,6 @@
use crate::commands::*;
use crate::config::{persistence::pathfinder::MixNodePathfinder, Config};
use crate::crypto::ed25519::sign_text;
use clap::{App, Arg, ArgMatches};
use colored::Colorize;
use config::NymConfig;
@@ -51,7 +50,7 @@ pub fn load_identity_keys(pathfinder: &MixNodePathfinder) -> identity::KeyPair {
fn print_signed_address(private_key: &identity::PrivateKey, raw_address: &str) -> String {
let trimmed = raw_address.trim();
validate_bech32_address_or_exit(trimmed);
let signature = sign_text(private_key, trimmed);
let signature = private_key.sign_text(trimmed);
println!(
"The base58-encoded signature on '{}' is: {}",
@@ -66,7 +65,7 @@ fn print_signed_text(private_key: &identity::PrivateKey, text: &str) {
text
);
let signature = sign_text(private_key, text);
let signature = private_key.sign_text(text);
println!(
"The base58-encoded signature on '{}' is: {}",
@@ -1,87 +0,0 @@
use config::defaults;
use subtle_encoding::bech32;
#[derive(Debug, Clone, PartialEq)]
pub enum Bech32Error {
DecodeFailed(String),
WrongPrefix(String),
}
/// Try to decode the address (to make sure it's a valid bech32 encoding)
pub fn try_bech32_decode(address: &str) -> Result<String, Bech32Error> {
match bech32::decode(address) {
Err(e) => Err(Bech32Error::DecodeFailed(e.to_string())),
Ok((prefix, _)) => Ok(prefix),
}
}
pub fn validate_bech32_prefix(address: &str) -> Result<(), Bech32Error> {
let prefix = try_bech32_decode(address)?;
if prefix == defaults::BECH32_PREFIX {
Ok(())
} else {
Err(Bech32Error::WrongPrefix(format!(
"your bech32 address prefix should be {}, not {}",
defaults::BECH32_PREFIX,
prefix
)))
}
}
#[cfg(test)]
mod tests {
use super::*;
mod decoding_bech32_addresses {
use super::*;
#[test]
fn total_crap_fails() {
let res = try_bech32_decode("crap");
assert_eq!(
Err(Bech32Error::DecodeFailed("bad encoding".to_string())),
res
);
}
#[test]
fn bad_checksum_fails() {
let chopped_address = "punk1h3w4nj7kny5dfyjw2le4vm74z03v9vd4dstpu"; // this has the final "0" chopped off
let res = try_bech32_decode(chopped_address);
assert_eq!(
Err(Bech32Error::DecodeFailed("checksum mismatch".to_string())),
res
);
}
#[test]
fn good_address_returns_prefix() {
let prefix = try_bech32_decode("punk1h3w4nj7kny5dfyjw2le4vm74z03v9vd4dstpu0");
assert_eq!(Ok("punk".to_string()), prefix);
}
}
#[cfg(test)]
mod ensuring_correct_bech32_prefix {
use super::*;
#[test]
fn wrong_prefix_fails() {
assert_eq!(
Err(Bech32Error::WrongPrefix(
"your bech32 address prefix should be nymt, not punk".to_string()
)),
validate_bech32_prefix("punk1h3w4nj7kny5dfyjw2le4vm74z03v9vd4dstpu0")
)
}
#[test]
fn correct_prefix_works() {
assert_eq!(
Ok(()),
validate_bech32_prefix("nymt1z9egw0knv47nmur0p8vk4rcx59h9gg4zuxrrr9")
)
}
}
}
-8
View File
@@ -1,8 +0,0 @@
use crypto::asymmetric::identity;
/// Signs text with the provided Ed25519 private key
pub fn sign_text(private_key: &identity::PrivateKey, text: &str) -> String {
let signature_bytes = private_key.sign(text.as_ref()).to_bytes();
let signature = bs58::encode(signature_bytes).into_string();
signature
}
-2
View File
@@ -1,2 +0,0 @@
pub mod bech32_address_validation;
pub mod ed25519;
-1
View File
@@ -8,7 +8,6 @@ use clap::{crate_version, App, ArgMatches};
mod commands;
mod config;
mod crypto;
mod node;
fn main() {
+1 -2
View File
@@ -3,7 +3,6 @@
use crate::commands::validate_bech32_address_or_exit;
use crate::config::Config;
use crate::crypto::ed25519::sign_text;
use crate::node::http::{
description::description,
not_found,
@@ -91,7 +90,7 @@ impl MixNode {
let identity_keypair = load_identity_keys(&pathfinder);
let address = self.config.get_wallet_address();
validate_bech32_address_or_exit(address);
let verification_code = sign_text(identity_keypair.private_key(), address);
let verification_code = identity_keypair.private_key().sign_text(address);
verification_code
}