Insert key via node RPC for subkey (#4514)

* Insert key via node RPC.

* somewhat address the reivew

* Update bin/utils/subkey/src/rpc.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update bin/utils/subkey/src/rpc.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update bin/utils/subkey/src/main.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Nikolay Volf
2020-01-02 14:46:45 +03:00
committed by Bastian Köcher
parent 6d06a19f41
commit 65e78b8129
4 changed files with 85 additions and 0 deletions
+28
View File
@@ -34,6 +34,7 @@ use std::{
convert::{TryInto, TryFrom}, io::{stdin, Read}, str::FromStr, path::PathBuf, fs, fmt,
};
mod rpc;
mod vanity;
trait Crypto: Sized {
@@ -231,6 +232,15 @@ fn get_app<'a, 'b>(usage: &'a str) -> App<'a, 'b> {
If the value is a file, the file content is used as URI. \
If not given, you will be prompted for the URI.'
"),
SubCommand::with_name("insert")
.about("Insert a key to the keystore of a node")
.args_from_usage("
<suri> 'The secret key URI. \
If the value is a file, the file content is used as URI. \
If not given, you will be prompted for the URI.'
<key-type> 'Key type, examples: \"gran\", or \"imon\" '
[node-url] 'Node JSON-RPC endpoint, default \"http:://localhost:9933\"'
"),
])
}
@@ -384,6 +394,24 @@ where
print_extrinsic(extrinsic);
}
("insert", Some(matches)) => {
let suri = get_uri("suri", &matches)?;
let pair = read_pair::<C>(Some(&suri), password)?;
let node_url = matches.value_of("node-url").unwrap_or("http://localhost:9933");
let key_type = matches.value_of("key-type").ok_or(Error::Static("Key type id is required"))?;
// Just checking
let _key_type_id = sp_core::crypto::KeyTypeId::try_from(key_type)
.map_err(|_| Error::Static("Cannot convert argument to keytype: argument should be 4-character string"))?;
let rpc = rpc::RpcClient::new(node_url.to_string());
rpc.insert_key(
key_type.to_string(),
suri,
sp_core::Bytes(pair.public().as_ref().to_vec()),
);
}
_ => print_usage(&matches),
}
+49
View File
@@ -0,0 +1,49 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Helper to run commands against current node RPC
use futures::Future;
use hyper::rt;
use node_primitives::Hash;
use sc_rpc::author::AuthorClient;
use jsonrpc_core_client::transports::http;
use sp_core::Bytes;
pub struct RpcClient { url: String }
impl RpcClient {
pub fn new(url: String) -> Self { Self { url } }
pub fn insert_key(
&self,
key_type: String,
suri: String,
public: Bytes,
) {
let url = self.url.clone();
rt::run(
http::connect(&url)
.and_then(|client: AuthorClient<Hash, Hash>| {
client.insert_key(key_type, suri, public).map(|_| ())
})
.map_err(|e| {
println!("Error inserting key: {:?}", e);
})
);
}
}