1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
//! `ic-agent` is a simple to use library to interact with the //! [Internet Computer](https://dfinity.org) in Rust. It is the backend for //! [`dfx`](https://sdk.dfinity.org). //! //! ## About //! `ic-agent` is a crate to talk directly to an ICP replica. It can handle multiple version //! of the Replica API, and exposes with both low level and high level APIs (like talking to //! canisters). //! //! ## Example //! The following examples show how to use the `Agent` low-level API to send a call to the management //! canister to create a new canister ID. //! //! ```ignore //! # // This test is ignored because it requires an ic to be running. We run these //! # // in the ic-ref workflow. //! use ic_agent::Agent; //! use ic_types::Principal; //! use candid::{Encode, Decode, CandidType}; //! use serde::Deserialize; //! //! #[derive(CandidType, Deserialize)] //! struct CreateCanisterResult { //! canister_id: Principal, //! } //! //! # fn create_identity() -> impl ic_agent::Identity { //! # let rng = ring::rand::SystemRandom::new(); //! # let key_pair = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng) //! # .expect("Could not generate a key pair."); //! # //! # ic_agent::identity::BasicIdentity::from_key_pair( //! # ring::signature::Ed25519KeyPair::from_pkcs8(key_pair.as_ref()) //! # .expect("Could not read the key pair."), //! # ) //! # } //! # //! # const URL: &'static str = concat!("http://localhost:", env!("IC_REF_PORT")); //! # //! async fn create_a_canister() -> Result<Principal, Box<dyn std::error::Error>> { //! let agent = Agent::builder() //! .with_url(URL) //! .with_identity(create_identity()) //! .build()?; //! let management_canister_id = Principal::from_text("aaaaa-aa")?; //! //! let waiter = delay::Delay::builder() //! .throttle(std::time::Duration::from_millis(500)) //! .timeout(std::time::Duration::from_secs(60 * 5)) //! .build(); //! //! // Create a call to the management canister to create a new canister ID, //! // and wait for a result. //! let response = agent.update(&management_canister_id, "create_canister") //! .with_arg(&Encode!()?) // Empty Candid. //! .call_and_wait(waiter) //! .await?; //! //! let result = Decode!(response.as_slice(), CreateCanisterResult)?; //! let canister_id: Principal = result.canister_id; //! Ok(canister_id) //! } //! //! # let mut runtime = tokio::runtime::Runtime::new().unwrap(); //! # runtime.block_on(async { //! let canister_id = create_a_canister().await.unwrap(); //! eprintln!("{}", canister_id); //! # }); //! ``` //! //! See the Documentation for more information. //! //! ## References //! The public specification of the Internet Computer is, at this moment, privately shared. When it //! is made public a reference to the version(s) supported will be available here. //! pub mod agent; pub mod export; pub mod identity; pub mod request_id; pub use agent::{agent_error::AgentError, nonce::NonceFactory, Agent, PasswordManager}; pub use identity::{Identity, Signature}; pub use request_id::{to_request_id, RequestId, RequestIdError};