#canister #internet-computer #dfinity #public-key #icp #utility

无 std ic-canister-sig-creation

在互联网计算机上创建 canister 签名的库

2 个稳定版本

新版本 1.1.0 2024 年 8 月 7 日
1.0.1 2024 年 6 月 28 日

18#canister 中排名

Download history 118/week @ 2024-06-26 25/week @ 2024-07-03 1268/week @ 2024-07-10 2219/week @ 2024-07-17 2227/week @ 2024-07-24 2304/week @ 2024-07-31

8,026 每月下载量
用于 ic-signature-verification

Apache-2.0

35KB
601

IC Canister 签名创建

处理 canister 签名公钥和创建 canister 签名的 crate。请参阅 ic-standalone-sig-verifier crate 以获取 canister 签名的验证。

简介

为了创建 canister 签名,canister 需要在其 certified_data 中提交一个公钥 seed 和一个 message_hash。此 crate 提供了使此过程尽可能简单的实用工具。

有关概念的更深入解释,请参阅 canister 签名 的官方规范以及 认证数据 的文档。

创建签名

创建签名是一个两步过程

  1. 签名必须在 update 调用中准备
  2. 签名必须在 query 调用中检索

为了连接这两个步骤,canister 必须保留关于已准备的签名的状态

use ic_canister_sig_creation::signature_map::SignatureMap;

thread_local! {
    /// Prepared canister signatures, no need to keep them in stable memory as they are only kept for one minute
    /// (to give clients time to do the query call).
    static SIGNATURES : RefCell<SignatureMap> = RefCell::new(SignatureMap::default());
}

准备签名

要在消息上准备签名,将它的 hash 添加到签名映射中,并与用于生成公钥的 seed 一起

use ic_canister_sig_creation::hash_bytes;

/// The signature domain should be unique for the context in which the signature is used.
const SIG_DOMAIN: &[u8] = b"ic-example-canister-sig";

fn add_signature(seed: &[u8], message: &[u8]) {
    let sig_inputs = CanisterSigInputs {
        domain: SIG_DOMAIN,
        seed,
        message,
    };
    SIGNATURES.with_borrow_mut(|sigs| {
        sigs.add_signature(&sig_inputs);
    });
}

然后将 certified_data 更新为签名映射的新根哈希

use ic_canister_sig_creation::signature_map::LABEL_SIG;
use ic_cdk::api::set_certified_data;

fn update_root_hash() {
    SIGNATURES.with_borrow(|sigs| {
        set_certified_data(&labeled_hash(LABEL_SIG, &sigs.root_hash()));
    })
}

检索签名

要检索已准备的签名,请在 SignatureMap 实例上使用 get_signature_as_cbor


/// The signature domain should be unique for the context in which the signature is used.
const SIG_DOMAIN: &[u8] = b"ic-example-canister-sig";

fn get_signature(seed: &[u8], message: &[u8]) -> Result<Vec<u8>, String> {
    let sig_inputs = CanisterSigInputs {
        domain: SIG_DOMAIN,
        seed,
        message,
    };
    SIGNATURES.with_borrow(|sigs| {
        sigs.get_signature_as_cbor(&sig_inputs, None)
    });
}

依赖关系

~2.1–3MB
~65K SLoC