Skip to content

Commit

Permalink
chore: support identity.pem for anda_web3_client
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Feb 18, 2025
1 parent ae8b7f6 commit 2a1623e
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 117 deletions.
76 changes: 38 additions & 38 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 agents/anda_bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ license.workspace = true
anda_core = { path = "../../anda_core", version = "0.4" }
anda_engine = { path = "../../anda_engine", version = "0.4" }
anda_lancedb = { path = "../../anda_lancedb", version = "0.1" }
anda_web3_client = { path = "../../anda_web3_client", version = "0.1" }
anda_web3_client = { path = "../../anda_web3_client", version = "0.2" }
anda_icp = { path = "../../tools/anda_icp", version = "0.2" }
axum = { workspace = true }
candid = { workspace = true }
Expand Down
20 changes: 13 additions & 7 deletions agents/anda_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use anda_lancedb::{
knowledge::KnowledgeStore,
lancedb::{DynObjectStore, LanceVectorStore, LocalFileSystem},
};
use anda_web3_client::client::Client as Web3Client;
use anda_web3_client::client::{load_identity, Client as Web3Client};
use axum::{routing, Router};
use candid::Principal;
use clap::{Parser, Subcommand};
Expand Down Expand Up @@ -107,9 +107,11 @@ pub enum Commands {
object_store_canister: String,
},
StartLocal {
#[arg(long, env = "ID_SECRET")]
/// Path to ICP identity pem file or 32 bytes identity secret in hex.
#[arg(short, long, env = "ID_SECRET")]
id_secret: String,

/// 48 bytes root secret in hex to derive keys
#[arg(long, env = "ROOT_SECRET")]
root_secret: String,

Expand Down Expand Up @@ -194,16 +196,14 @@ async fn bootstrap(cli: Cli) -> Result<(), BoxError> {
}) => {
let cfg = config::Conf::from_file(&config)?;
log::debug!("{:?}", cfg);
let id_secret = const_hex::decode(id_secret)?;
let id_secret: [u8; 32] = id_secret.try_into().map_err(|_| "invalid id_secret")?;
let root_secret = const_hex::decode(root_secret)?;
let root_secret: [u8; 48] =
root_secret.try_into().map_err(|_| "invalid root_secret")?;

bootstrap_local(
cli.port,
cli.ic_host,
id_secret,
&id_secret,
root_secret,
cfg,
character,
Expand Down Expand Up @@ -395,7 +395,7 @@ async fn bootstrap_tee(
async fn bootstrap_local(
port: u16,
ic_host: String,
id_secret: [u8; 32],
id_secret: &str,
root_secret: [u8; 48],
cfg: config::Conf,
character: Character,
Expand All @@ -409,7 +409,13 @@ async fn bootstrap_local(
let default_agent = character.username.clone();
let knowledge_table: Path = default_agent.to_ascii_lowercase().into();

let web3 = Web3Client::new(&ic_host, id_secret, root_secret, None, None).await?;
let identity = load_identity(id_secret)?;
let web3 = Web3Client::builder()
.with_ic_host(&ic_host)
.with_identity(Arc::new(identity))
.with_root_secret(root_secret)
.build()
.await?;
let my_principal = web3.get_principal();
log::info!(target: LOG_TARGET, "start local service, principal: {:?}", my_principal.to_text());

Expand Down
2 changes: 1 addition & 1 deletion anda_engine_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license.workspace = true

[dependencies]
anda_core = { path = "../anda_core", version = "0.4" }
anda_web3_client = { path = "../anda_web3_client", version = "0.1" }
anda_web3_client = { path = "../anda_web3_client", version = "0.2" }
base64 = { workspace = true }
clap = { workspace = true }
dotenv = { workspace = true }
Expand Down
33 changes: 22 additions & 11 deletions anda_engine_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anda_core::{AgentOutput, BoxError, HttpFeatures};
use anda_web3_client::client::Client as Web3Client;
use anda_web3_client::client::{load_identity, Client as Web3Client};
use base64::{prelude::BASE64_URL_SAFE_NO_PAD, Engine};
use clap::{Parser, Subcommand};
use rand::{thread_rng, RngCore};
use std::sync::Arc;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand All @@ -27,7 +28,8 @@ pub enum Commands {
#[arg(short, long, default_value = "http://127.0.0.1:8042/default")]
endpoint: String,

#[arg(short, long, env = "ID_SECRET")]
/// Path to ICP identity pem file or 32 bytes identity secret in hex.
#[arg(short, long, env = "ID_SECRET", default_value = "Anonymous")]
id_secret: String,

#[arg(short, long)]
Expand All @@ -40,7 +42,8 @@ pub enum Commands {
#[arg(short, long, default_value = "http://127.0.0.1:8042/default")]
endpoint: String,

#[arg(short, long, env = "ID_SECRET")]
/// Path to ICP identity pem file or 32 bytes identity secret in hex.
#[arg(short, long, env = "ID_SECRET", default_value = "Anonymous")]
id_secret: String,

#[arg(short, long)]
Expand Down Expand Up @@ -80,10 +83,14 @@ async fn main() -> Result<(), BoxError> {
prompt,
name,
}) => {
let id_secret = const_hex::decode(id_secret)?;
let id_secret: [u8; 32] = id_secret.try_into().map_err(|_| "invalid id_secret")?;
let web3 =
Web3Client::new(&cli.ic_host, id_secret, [0u8; 48], None, Some(true)).await?;
let identity = load_identity(id_secret)?;
let web3 = Web3Client::builder()
.with_ic_host(&cli.ic_host)
.with_identity(Arc::new(identity))
.with_allow_http(true)
.build()
.await?;

println!("principal: {}", web3.get_principal());

let res: AgentOutput = web3
Expand All @@ -98,10 +105,14 @@ async fn main() -> Result<(), BoxError> {
name,
args,
}) => {
let id_secret = const_hex::decode(id_secret)?;
let id_secret: [u8; 32] = id_secret.try_into().map_err(|_| "invalid id_secret")?;
let web3 =
Web3Client::new(&cli.ic_host, id_secret, [0u8; 48], None, Some(true)).await?;
let identity = load_identity(id_secret)?;
let web3 = Web3Client::builder()
.with_ic_host(&cli.ic_host)
.with_identity(Arc::new(identity))
.with_allow_http(true)
.build()
.await?;

println!("principal: {}", web3.get_principal());

let res: (String, bool) = web3
Expand Down
Loading

0 comments on commit 2a1623e

Please sign in to comment.