Skip to content

Commit

Permalink
fix(gring): remove ss58 version configuration (#3750)
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop authored Feb 26, 2024
1 parent 5dcc1e0 commit d87efe7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
33 changes: 33 additions & 0 deletions utils/gring/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ use std::{fs, path::PathBuf};
/// gring sub commands.
#[derive(Clone, Debug, Parser)]
pub enum Command {
/// Imports a keystore file generated by polakdot-js extension.
Add {
/// The path of the keystore file.
path: PathBuf,
},
/// Generate a new key.
New {
/// The name of the key.
Expand Down Expand Up @@ -109,6 +114,34 @@ impl Command {
pub fn run(self) -> Result<()> {
let mut keyring = Keyring::load(Command::store()?)?;
match self {
Command::Add { path } => {
let keystore = serde_json::from_str::<Keystore>(&fs::read_to_string(&path)?)?;
let name = path
.file_stem()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or(keystore.meta.name.clone());

keyring
.list()
.iter()
.find(|k| k.meta.name == keystore.meta.name)
.map_or_else(
|| {
fs::copy(&path, keyring.store.join(&name).with_extension("json"))?;
println!(
"Key {} has been imported!",
keystore.meta.name.cyan().bold()
);
Ok(())
},
|_| {
Err(anyhow!(
"Key {} already exists.",
keystore.meta.name.yellow().bold()
))
},
)?;
}
Command::New {
mut name,
vanity,
Expand Down
19 changes: 1 addition & 18 deletions utils/gring/src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

//! Keyring implementation based on the polkadot-js keystore.
use crate::{
ss58::{self, VARA_SS58_PREFIX},
Keystore,
};
use crate::{ss58, Keystore};
use anyhow::Result;
use colored::Colorize;
use schnorrkel::Keypair;
Expand All @@ -41,9 +38,6 @@ pub struct Keyring {
ring: Vec<Keystore>,
/// The primary key.
pub primary: String,
/// The SS58 prefix.
#[serde(default = "ss58::default_ss58_version")]
pub ss58_version: u16,
}

impl Keyring {
Expand Down Expand Up @@ -75,10 +69,6 @@ impl Keyring {
Self::default()
};

if this.ss58_version != VARA_SS58_PREFIX {
ss58::set_default_ss58_version(this.ss58_version);
}

this.ring = ring;
this.store = store;

Expand Down Expand Up @@ -131,13 +121,6 @@ impl Keyring {
Ok(key)
}

/// Set the SS58 version.
pub fn set_ss58_version(&mut self, version: u16) -> Result<()> {
self.ss58_version = version;
fs::write(self.store.join(CONFIG), serde_json::to_vec_pretty(&self)?)?;
Ok(())
}

/// Add keypair to the keyring
pub fn add(
&mut self,
Expand Down

0 comments on commit d87efe7

Please sign in to comment.