From ad150089bb2ec2c0802d31c114a3f8fd9178b6fb Mon Sep 17 00:00:00 2001 From: Fran Domovic Date: Tue, 3 Dec 2024 18:03:34 +0100 Subject: [PATCH] feat: with identity calls --- .../context/config/src/client/protocol/icp.rs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/crates/context/config/src/client/protocol/icp.rs b/crates/context/config/src/client/protocol/icp.rs index 6ed9389ef..61b412b3d 100644 --- a/crates/context/config/src/client/protocol/icp.rs +++ b/crates/context/config/src/client/protocol/icp.rs @@ -1,10 +1,10 @@ use std::borrow::Cow; use std::collections::BTreeMap; -use candid::{decode_one, Result as CandidResult}; use ed25519_consensus::SigningKey; use ic_agent::agent::CallResponse; use ic_agent::export::Principal; +use ic_agent::identity::BasicIdentity; use ic_agent::Agent; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -36,6 +36,7 @@ pub struct Credentials { mod serde_creds { use candid::Principal; + use hex::FromHexError; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -46,8 +47,13 @@ mod serde_creds { secret_key: String, } - #[derive(Copy, Clone, Debug, Error)] - pub enum CredentialsError {} + #[derive(Clone, Debug, Error)] + pub enum CredentialsError { + #[error("failed to parse SigningKey from hex")] + ParseError(#[from] FromHexError), + #[error("failed to parse SigningKey from string")] + IntoError(String), + } impl TryFrom for super::Credentials { type Error = CredentialsError; @@ -89,11 +95,18 @@ pub struct IcpTransport<'a> { impl<'a> IcpTransport<'a> { #[must_use] pub fn new(config: &IcpConfig<'a>) -> Self { - let mut networks = BTreeMap::new(); + let mut networks: BTreeMap, Network> = BTreeMap::new(); for (network_id, network_config) in &config.networks { + let secret_key_byes = hex::decode(network_config.secret_key.clone()).unwrap(); + let secret_key_array: [u8; 32] = secret_key_byes.try_into().unwrap(); + let secret_key: SigningKey = secret_key_array.into(); + + let identity = BasicIdentity::from_signing_key(secret_key.clone()); + let client = Agent::builder() .with_url(network_config.rpc_url.clone()) + .with_identity(identity) .build() .unwrap(); @@ -181,7 +194,6 @@ impl Network { method: String, args: Vec, ) -> Result, IcpError> { - println!("Here access to query"); self.client .fetch_root_key() .await