2 个版本
0.24.1 | 2023 年 5 月 20 日 |
---|---|
0.24.0 | 2023 年 5 月 20 日 |
#49 在 #icp 中
每月下载量:24
205KB
4K SLoC
ic-agent
是一个简单易用的库,用于在 Rust 中与互联网计算机交互。它是 互联网计算机 的后端。
有用链接
lib.rs
:
ic-agent
是一个简单易用的库,允许您使用 Rust 构建应用程序并与 互联网计算机 交互。它作为 DFINITY Canister 软件开发工具包(SDK)和 Canister SDK 命令行执行环境的 Rust 基础低级后端。
概述
ic-agent
是一个 Rust 包,可以通过互联网计算机协议(ICP)直接连接到互联网计算机。ICP 的关键软件组件通常被称为 副本。
该代理设计为与多个版本的副本 API 兼容,并公开低级 API 以与互联网计算机协议组件(如副本)通信,并提供高级 API 以与作为 canister 部署的应用程序通信。
示例
以下示例说明如何使用代理接口向执行网络管理操作的互联网计算机 canister 发送调用。在此示例中,对互联网计算机管理 canister(aaaaa-aa
)的调用通过注册网络特定标识符为新的 canister 创建占位符。然后,管理 canister 以 canister 标识符的文本表示形式将结果返回给调用者。
# // This test is ignored because it requires an ic to be running. We run these
# // in the ic-ref workflow.
use ic_agent::{Agent, export::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")?;
// Create a call to the management canister to create a new canister ID,
// and wait for a result.
// The effective canister id must belong to the canister ranges of the subnet at which the canister is created.
let effective_canister_id = Principal::from_text("rwlgt-iiaaa-aaaaa-aaaaa-cai").unwrap();
let response = agent.update(&management_canister_id, "provisional_create_canister_with_cycles")
.with_effective_canister_id(effective_canister_id)
.with_arg(&Encode!(&Argument { amount: None})?)
.call_and_wait()
.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);
# });
有关此示例中使用的代理接口的更多信息,请参阅 [Agent] 文档。
参考资料
有关互联网计算机和 DFINITY Canister SDK 的介绍,请参阅以下资源
互联网计算机协议和接口规范尚未公开发布。当这些规范公开发布并广泛可用时,此处将提供有关支持版本的详细信息。
依赖项
~18–34MB
~630K SLoC