Skip to content

Commit

Permalink
switch identity
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Apr 30, 2021
1 parent 35ac33d commit 5327311
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
20 changes: 10 additions & 10 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
lalrpop = "0.19"

[dependencies]
candid = { git = "https://github.com/dfinity/candid.git", branch = "fix-beta", features = ["random"] }
candid = { git = "https://github.com/dfinity/candid.git", branch = "beta", features = ["random"] }
rustyline = "8.0"
rustyline-derive = "0.4"
ansi_term = "0.12"
Expand Down
31 changes: 18 additions & 13 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,25 @@ impl Command {
println!("{}", v);
}
Command::Identity(id) => {
// TODO use existing identity
use ic_agent::Identity;
let identity = create_identity()?;
let keypair = if let Some(keypair) = helper.identity_map.0.get(id) {
keypair.to_vec()
} else {
let rng = ring::rand::SystemRandom::new();
let keypair = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng)?
.as_ref()
.to_vec();
helper
.identity_map
.0
.insert(id.to_string(), keypair.clone());
keypair
};
let identity = ic_agent::identity::BasicIdentity::from_key_pair(
ring::signature::Ed25519KeyPair::from_pkcs8(&keypair)?,
);
let sender = identity.sender().map_err(|e| anyhow!("{}", e))?;
println!("Create identity {}", sender);
println!("Current identity {}", sender);
let agent = Agent::builder()
.with_transport(
ic_agent::agent::http_transport::ReqwestHttpReplicaV2Transport::create(
Expand All @@ -176,6 +190,7 @@ impl Command {
runtime.block_on(agent.fetch_root_key())?;
}
helper.agent = agent;
helper.current_identity = id.to_string();
helper
.env
.0
Expand Down Expand Up @@ -259,16 +274,6 @@ async fn call(
Ok(IDLArgs::from_bytes_with_types(&bytes, env, &func.rets)?)
}

fn create_identity() -> anyhow::Result<impl ic_agent::Identity> {
let rng = ring::rand::SystemRandom::new();
let pkcs8_bytes = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng)?
.as_ref()
.to_vec();
Ok(ic_agent::identity::BasicIdentity::from_key_pair(
ring::signature::Ed25519KeyPair::from_pkcs8(&pkcs8_bytes)?,
))
}

// Return position at the end of principal, principal, method, args
pub fn extract_canister(
line: &str,
Expand Down
6 changes: 6 additions & 0 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use std::collections::BTreeMap;
#[derive(Default)]
pub struct CanisterMap(pub BTreeMap<Principal, CanisterInfo>);
#[derive(Default)]
pub struct IdentityMap(pub BTreeMap<String, Vec<u8>>);
#[derive(Default)]
pub struct Env(pub BTreeMap<String, IDLValue>);
#[derive(Default)]
pub struct NameEnv(pub BTreeMap<String, Principal>);
Expand Down Expand Up @@ -61,6 +63,8 @@ pub struct MyHelper {
hinter: HistoryHinter,
pub colored_prompt: String,
pub canister_map: RefCell<CanisterMap>,
pub identity_map: IdentityMap,
pub current_identity: String,
pub agent_url: String,
pub agent: Agent,
pub config: Configs,
Expand All @@ -79,6 +83,8 @@ impl MyHelper {
colored_prompt: "".to_owned(),
validator: MatchingBracketValidator::new(),
canister_map: RefCell::new(CanisterMap::default()),
identity_map: IdentityMap::default(),
current_identity: "anon".to_owned(),
config: Configs::from_dhall("{=}").unwrap(),
env: Env::default(),
canister_env: NameEnv::default(),
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ fn repl(opts: Opts) -> anyhow::Result<()> {

let mut count = 1;
loop {
let p = format!("{} {}> ", replica, count);
let identity = &rl.helper().unwrap().current_identity;
let p = format!("{}@{} {}> ", identity, replica, count);
rl.helper_mut().unwrap().colored_prompt = format!("{}", Color::Green.bold().paint(&p));
let input = rl.readline(&p);
match input {
Expand Down

0 comments on commit 5327311

Please sign in to comment.