Skip to content

Commit

Permalink
Do not use any Versioning in massa client (for generating keypairs) (#…
Browse files Browse the repository at this point in the history
…4238)

Co-authored-by: sydhds <[email protected]>
  • Loading branch information
sydhds and sydhds authored Jul 10, 2023
1 parent 1e65d66 commit e4875d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 57 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion massa-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ massa_signature = { path = "../massa-signature" }
massa_time = { path = "../massa-time" }
massa_sdk = { path = "../massa-sdk" }
massa_wallet = { path = "../massa-wallet" }
massa-proto-rs = { git = "https://github.com/massalabs/massa-proto-rs", rev = "18ec02f", features = ["tonic"] }
# massa-proto-rs = { git = "https://github.com/massalabs/massa-proto-rs", rev = "18ec02f", features = ["tonic"] }

[dev-dependencies]
toml_edit = "0.19"
71 changes: 16 additions & 55 deletions massa-client/src/cmds.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 MASSA LABS <[email protected]>

use crate::display::Output;
use crate::{client_warning, grpc_error, rpc_error};
use crate::{client_warning, rpc_error};
use anyhow::{anyhow, bail, Result};
use console::style;
use massa_api_exports::{
Expand All @@ -22,8 +22,6 @@ use massa_models::{
operation::{Operation, OperationId, OperationType},
slot::Slot,
};
use massa_proto_rs::massa::api::v1 as grpc_api;
use massa_proto_rs::massa::model::v1 as grpc_model;
use massa_sdk::Client;
use massa_signature::KeyPair;
use massa_time::MassaTime;
Expand Down Expand Up @@ -784,61 +782,24 @@ impl Command {
Command::wallet_generate_secret_key => {
let wallet = wallet_opt.as_mut().unwrap();

// In order to generate a KeyPair we need to get the MIP statuses and use the latest
// active version
let req = grpc_api::GetMipStatusRequest { id: "".to_string() };
// Note: keypair version is hardcoded here (no Versioning check via grpc done)
// Requirements is to ALWAYS have the generation done without a node (for security)
// If a new keypair version is accepted (through a MIP), we will update the version here
// manually.
// This will allow to generate keypair on hardware ledger / computer without internet...

if let Some(ref mut grpc) = client.grpc {
let versioning_status = match grpc.get_mip_status(req).await {
Ok(resp_) => {
let resp = resp_.into_inner();
resp.entries
}
Err(e) => {
grpc_error!(e)
}
};
let keypair_version: u64 = 0;
let key = KeyPair::generate(keypair_version).expect("Unable to generate key pair");

// Note: this is a bit duplicated with KeyPairFactory code but it avoids
// sending the whole mip store through the API
let keypair_version = versioning_status
.into_iter()
.rev()
.find_map(|entry| {
let is_about_keypair = match entry.mip_info {
None => false,
Some(mip_info) => mip_info.components.iter().any(|st_entry| {
grpc_model::MipComponent::from_i32(st_entry.kind)
.unwrap_or(grpc_model::MipComponent::Unspecified)
== grpc_model::MipComponent::Keypair
}),
};

let state = grpc_model::ComponentStateId::from_i32(entry.state_id)
.unwrap_or(grpc_model::ComponentStateId::Error);

if is_about_keypair && state == grpc_model::ComponentStateId::Active {
Some(entry.state_id)
} else {
None
}
})
.unwrap_or(0);
let key = KeyPair::generate(keypair_version as u64)
.expect("Unable to generate key pair");

let ad = wallet.add_keypairs(vec![key])?[0];
if json {
Ok(Box::new(ad.to_string()))
} else {
println!("Generated {} address and added it to the wallet", ad);
println!(
"Type `wallet_info` to show wallet info (keys, addresses, balances ...) and/or `node_start_staking <your address>` to start staking.\n"
);
Ok(Box::new(()))
}
let ad = wallet.add_keypairs(vec![key])?[0];
if json {
Ok(Box::new(ad.to_string()))
} else {
bail!("Failed to establish connection. Please ensure that the gRPC API is enabled and accessible.");
println!("Generated {} address and added it to the wallet", ad);
println!(
"Type `wallet_info` to show wallet info (keys, addresses, balances ...) and/or `node_start_staking <your address>` to start staking.\n"
);
Ok(Box::new(()))
}
}

Expand Down

0 comments on commit e4875d5

Please sign in to comment.