#internet-computer #agent #icp #dfinity

no-std ic-agent

Agent library to communicate with the Internet Computer, following the Public Specification

13 releases (breaking)

0.10.0 Nov 16, 2021
0.8.0 Aug 23, 2021
0.7.0 Jul 30, 2021
0.2.0 Mar 4, 2021
0.1.0 Sep 24, 2020

#89 in Magic Beans

Download history 481/week @ 2021-08-12 470/week @ 2021-08-19 352/week @ 2021-08-26 267/week @ 2021-09-02 514/week @ 2021-09-09 593/week @ 2021-09-16 631/week @ 2021-09-23 641/week @ 2021-09-30 649/week @ 2021-10-07 926/week @ 2021-10-14 664/week @ 2021-10-21 1470/week @ 2021-10-28 1204/week @ 2021-11-04 1154/week @ 2021-11-11 1195/week @ 2021-11-18 893/week @ 2021-11-25

4,608 downloads per month
Used in less than 8 crates

Apache-2.0

570KB
15K SLoC

ic-agent is simple to use library to interact with the Internet Computer in Rust. It is the backend for dfx.

Useful links


lib.rs:

The ic-agent is a simple-to-use library that enables you to build applications and interact with the Internet Computer in Rust. It serves as a Rust-based low-level backend for the DFINITY Canister Software Development Kit (SDK) and the Canister SDK command-line execution environment dfx.

Overview

The ic-agent is a Rust crate that can connect directly to the Internet Computer through the Internet Computer protocol (ICP). The key software components of the ICP are broadly referred to as the replica.

The agent is designed to be compatible with multiple versions of the replica API, and to expose both low-level APIs for communicating with Internet Computer protocol components like the replica and to provide higher-level APIs for communicating with software applications deployed as canisters.

Example

The following example illustrates how to use the Agent interface to send a call to an Internet Computer canister that performs network management operations. In this example, the call to the Internet Computer management canister (aaaaa-aa) creates a placeholder for a new canister by registering a network-specific identifier. The management canister then returns the result in the form of the textual representation of the canister identifier to the caller.

# // This test is ignored because it requires an ic to be running. We run these
# // in the ic-ref workflow.
use ic_agent::{Agent, ic_types::Principal};
use candid::{Encode, Decode, CandidType, Nat};
use serde::Deserialize;

#[derive(CandidType)]
struct Argument {
  amount: Option<Nat>,
}

#[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()?;
  // Only do the following call when not contacting the IC main net (e.g. a local emulator).
  // This is important as the main net public key is static and a rogue network could return
  // a different key.
  // If you know the root key ahead of time, you can use `agent.set_root_key(root_key)?;`.
  agent.fetch_root_key().await?;
  let management_canister_id = Principal::from_text("aaaaa-aa")?;

  let waiter = garcon::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, "provisional_create_canister_with_cycles")
    .with_arg(&Encode!(&Argument { amount: None})?)
    .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);
# });

For more information about the Agent interface used in this example, see the Agent documentation.

References

For an introduction to the Internet Computer and the DFINITY Canister SDK, see the following resources:

The Internet Computer protocol and interface specifications are not publicly available yet. When these specifications are made public and generally available, additional details about the versions supported will be available here.

Dependencies

~14–19MB
~480K SLoC

a