verity_ic/crypto/
ecdsa.rs1#![allow(dead_code)]
2
3use candid::{CandidType, Principal};
4use serde::{Deserialize, Serialize};
5
6use super::config::Config;
7
8#[derive(CandidType, Serialize, Debug)]
10pub struct PublicKeyReply {
11 pub sec1_pk: String, pub etherum_pk: String, }
14
15#[derive(CandidType, Serialize, Debug)]
17pub struct SignatureReply {
18 pub signature_hex: String, }
20
21#[derive(CandidType, Serialize, Debug)]
23pub struct SignatureVerificationReply {
24 pub is_signature_valid: bool, }
26
27type CanisterId = Principal;
29
30#[derive(CandidType, Serialize, Debug)]
32pub struct ECDSAPublicKey {
33 pub canister_id: Option<CanisterId>, pub derivation_path: Vec<Vec<u8>>, pub key_id: EcdsaKeyId, }
37
38#[derive(CandidType, Deserialize, Debug)]
40pub struct ECDSAPublicKeyReply {
41 pub public_key: Vec<u8>, pub chain_code: Vec<u8>, }
44
45#[derive(CandidType, Serialize, Debug)]
47pub struct SignWithECDSA {
48 pub message_hash: Vec<u8>, pub derivation_path: Vec<Vec<u8>>, pub key_id: EcdsaKeyId, }
52
53#[derive(CandidType, Deserialize, Debug)]
55pub struct SignWithECDSAReply {
56 pub signature: Vec<u8>, }
58
59#[derive(CandidType, Serialize, Debug, Clone)]
61pub struct EcdsaKeyId {
62 pub curve: EcdsaCurve, pub name: String, }
65
66#[derive(CandidType, Serialize, Debug, Clone)]
68pub enum EcdsaCurve {
69 #[serde(rename = "secp256k1")]
70 Secp256k1, }
72
73#[derive(CandidType, Deserialize, Debug, Clone)]
75pub enum EcdsaKeyIds {
76 #[allow(unused)]
77 TestKeyLocalDevelopment, #[allow(unused)]
79 TestKey1, #[allow(unused)]
81 ProductionKey1, }
83
84impl EcdsaKeyIds {
86 pub fn to_key_id(&self) -> EcdsaKeyId {
88 EcdsaKeyId {
89 curve: EcdsaCurve::Secp256k1, name: (match self {
91 Self::TestKeyLocalDevelopment => "dfx_test_key",
92 Self::TestKey1 => "test_key_1",
93 Self::ProductionKey1 => "key_1",
94 })
95 .to_string(),
96 }
97 }
98}
99
100pub async fn derive_pk(config: &Config) -> Vec<u8> {
102 let request = ECDSAPublicKey {
104 canister_id: None, derivation_path: vec![], key_id: config.key.to_key_id(), };
108
109 let (res,): (ECDSAPublicKeyReply,) = ic_cdk::call(
111 Principal::management_canister(),
112 "ecdsa_public_key",
113 (request,),
114 )
115 .await
116 .map_err(|e| format!("ECDSA_PUBLIC_KEY_FAILED: {}\t,Error_code:{:?}", e.1, e.0))
117 .unwrap();
118
119 res.public_key
121}