Skip to content

Commit aaff760

Browse files
fix: build error
1 parent afd39e0 commit aaff760

3 files changed

Lines changed: 44 additions & 34 deletions

File tree

kms/src/ckd.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use blstrs::{G1Affine, G1Projective, G2Affine, G2Projective, Scalar};
2121
use elliptic_curve::{group::prime::PrimeCurveAffine as _, Field as _, Group as _};
2222
use hkdf::Hkdf;
2323
use rand_core::OsRng;
24+
use serde::Deserialize;
2425
use sha2::Sha256;
2526
use sha3::{Digest, Sha3_256};
26-
use serde::{Deserialize, Serialize};
2727

2828
// Constants matching NEAR MPC contract
2929
const BLS12381G1_PUBLIC_KEY_SIZE: usize = 48;
@@ -47,11 +47,11 @@ pub struct MpcConfig {
4747
pub near_rpc_url: String,
4848
}
4949

50-
#[derive(Deserialize)]
51-
struct Bls12381G1PublicKey(String);
50+
#[derive(Clone, Debug, Deserialize)]
51+
pub struct Bls12381G1PublicKey(String);
5252

5353
/// MPC CKD response (big_y, big_c from MPC network)
54-
#[derive(Debug, Clone, Deserialize)]
54+
#[derive(Clone, Debug, Deserialize)]
5555
pub struct CkdResponse {
5656
pub big_y: Bls12381G1PublicKey,
5757
pub big_c: Bls12381G1PublicKey,
@@ -136,15 +136,15 @@ pub fn near_format_to_g2(s: &str) -> Result<G2Projective> {
136136

137137
/// Decrypt MPC response and verify signature
138138
pub fn decrypt_and_verify_mpc_response(
139-
big_y: &str,
140-
big_c: &str,
139+
big_y: &Bls12381G1PublicKey,
140+
big_c: &Bls12381G1PublicKey,
141141
ephemeral_private_key: Scalar,
142142
mpc_public_key: &str,
143143
app_id: &[u8],
144144
) -> Result<[u8; BLS12381G1_PUBLIC_KEY_SIZE]> {
145145
// Parse G1 points
146-
let big_y_point = near_format_to_g1(big_y)?;
147-
let big_c_point = near_format_to_g1(big_c)?;
146+
let big_y_point = near_format_to_g1(&big_y.0)?;
147+
let big_c_point = near_format_to_g1(&big_c.0)?;
148148

149149
// Parse MPC public key (G2)
150150
let mpc_pk = near_format_to_g2(mpc_public_key)?;
@@ -206,7 +206,7 @@ pub fn derive_final_key(
206206
/// 3. Derives the final 32-byte key using HKDF
207207
/// 4. Returns the key that can be used as K256 signing key
208208
pub fn derive_root_key_from_mpc(
209-
mpc_response: &CKDResponse,
209+
mpc_response: &CkdResponse,
210210
ephemeral_private_key: Scalar,
211211
mpc_config: &MpcConfig,
212212
kms_account_id: &str,

kms/src/near_kms_client.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use anyhow::{Context, Result};
1111
use fs_err as fs;
1212
use hex;
1313
use near_api::{
14-
SecretKey,
1514
types::{AccountId, Data},
16-
Contract, NetworkConfig, Signer,
15+
Contract, NetworkConfig, SecretKey, Signer,
1716
};
18-
use std::sync::Arc;
17+
use near_crypto::InMemorySigner;
1918
use serde::{Deserialize, Serialize};
2019
use std::str::FromStr;
20+
use std::sync::Arc;
2121

2222
use crate::ckd::CkdResponse;
2323
use crate::config::KmsConfig;
@@ -46,12 +46,14 @@ impl NearKmsClient {
4646
network_config: NetworkConfig,
4747
mpc_contract_id: String,
4848
kms_contract_id: String,
49-
signer: Option<near_crypto::InMemorySigner>,
49+
signer: Option<InMemorySigner>,
5050
) -> Result<Self> {
51-
let mpc_contract_id: AccountId = mpc_contract_id.parse()
51+
let mpc_contract_id: AccountId = mpc_contract_id
52+
.parse()
5253
.context("Failed to parse MPC contract ID")?;
5354

54-
let kms_contract_id: AccountId = kms_contract_id.parse()
55+
let kms_contract_id: AccountId = kms_contract_id
56+
.parse()
5557
.context("Failed to parse KMS contract ID")?;
5658

5759
let (signer_account_id, near_api_signer) = if let Some(in_memory_signer) = signer {
@@ -64,7 +66,9 @@ impl NearKmsClient {
6466
let signer = Signer::from_secret_key(near_api_secret_key)
6567
.context("Failed to create near-api Signer")?;
6668

67-
let account_id: AccountId = in_memory_signer.account_id.to_string()
69+
let account_id: AccountId = in_memory_signer
70+
.account_id
71+
.to_string()
6872
.parse()
6973
.context("Failed to parse account ID")?;
7074

@@ -87,7 +91,8 @@ impl NearKmsClient {
8791

8892
/// Get MPC public key from the contract
8993
pub async fn get_mpc_public_key(&self, domain_id: u64) -> Result<String> {
90-
let current_value: Data<String> = self.mpc_contract
94+
let current_value: Data<String> = self
95+
.mpc_contract
9196
.call_function("public_key", serde_json::json!({ "domain_id": domain_id }))
9297
.read_only()
9398
.fetch_from(&self.network_config)
@@ -113,7 +118,10 @@ impl NearKmsClient {
113118
.signer
114119
.as_ref()
115120
.context("NEAR signer required for KMS root key requests")?;
116-
let signer_account_id = self.signer_account_id.expect("Signer account ID required for KMS root key requests")?;
121+
let signer_account_id = self
122+
.signer_account_id
123+
.as_ref()
124+
.context("Signer account ID required for KMS root key requests")?;
117125

118126
// Create request arguments
119127
let args = RequestKmsRootKeyArgs {
@@ -128,7 +136,7 @@ impl NearKmsClient {
128136
.kms_contract
129137
.call_function("request_kms_root_key", args)
130138
.transaction()
131-
.with_signer(self.signer_account_id.clone(), signer.clone())
139+
.with_signer(signer_account_id, signer.clone())
132140
.send_to(&self.network_config)
133141
.await
134142
.context("Failed to call KMS contract")?;
@@ -145,7 +153,7 @@ impl NearKmsClient {
145153
// The return value from Promise callbacks needs to be extracted from the appropriate receipt outcome
146154
// This may require checking the receipt IDs and following the Promise chain, or using
147155
// a helper method if the API provides one
148-
156+
149157
// For now, return an error indicating this needs implementation
150158
// The actual implementation will depend on how the near-api library exposes
151159
// the return values from Promise callbacks in receipt outcomes
@@ -172,16 +180,14 @@ impl NearKmsClient {
172180

173181
/// Generate a random NEAR implicit account signer
174182
/// The account ID is derived from the Ed25519 public key (64 hex characters)
175-
pub fn generate_near_implicit_signer() -> Result<near_crypto::InMemorySigner> {
176-
use near_crypto::{InMemorySigner, SecretKey};
177-
183+
pub fn generate_near_implicit_signer() -> Result<InMemorySigner> {
178184
// Generate random Ed25519 keypair
179-
let secret_key = SecretKey::from_random(near_crypto::KeyType::ED25519);
185+
let secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);
180186

181187
// Derive implicit account ID from public key (hex encode the 32-byte public key)
182188
let public_key = secret_key.public_key();
183-
use byte_slice_cast::AsByteSlice;
184-
let account_id = match public_key {
189+
use byte_slice_cast::AsByteSlice;
190+
let account_id = match public_key {
185191
near_crypto::PublicKey::ED25519(pk) => {
186192
// Implicit account ID is the hex-encoded public key (64 characters)
187193
hex::encode(pk.as_byte_slice())
@@ -226,8 +232,6 @@ pub fn load_or_generate_near_signer(
226232
.as_str()
227233
.context("Missing secret_key in signer file")?;
228234

229-
use near_crypto::{InMemorySigner, SecretKey};
230-
231235
let account_id_parsed: near_account_id::AccountId = account_id_str
232236
.parse()
233237
.context("Failed to parse account ID from signer file")?;
@@ -236,7 +240,8 @@ pub fn load_or_generate_near_signer(
236240

237241
tracing::info!("Loaded existing NEAR signer: {}", account_id_str);
238242
Ok(Some(InMemorySigner::from_secret_key(
239-
account_id_parsed, secret_key,
243+
account_id_parsed,
244+
secret_key,
240245
)))
241246
} else {
242247
// Generate new signer

kms/src/onboard_service.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ impl Keys {
162162
quote_enabled: bool,
163163
) -> Result<Self> {
164164
// Derive CA key from root key using deterministic key derivation
165-
let ca_key = kdf::derive_ecdsa_key_pair_from_bytes(&root_key, &[b"ca-key"])
165+
let ca_key = kdf::derive_p256_key_pair_from_bytes(&root_key, &[b"ca-key"])
166166
.context("Failed to derive CA key from MPC root key")?;
167167

168168
// Derive tmp CA key from root key
169-
let tmp_ca_key = kdf::derive_ecdsa_key_pair_from_bytes(&root_key, &[b"tmp-ca-key"])
169+
let tmp_ca_key = kdf::derive_p256_key_pair_from_bytes(&root_key, &[b"tmp-ca-key"])
170170
.context("Failed to derive tmp CA key from MPC root key")?;
171171

172172
// Derive RPC key from root key
173-
let rpc_key = kdf::derive_ecdsa_key_pair_from_bytes(&root_key, &[b"rpc-key"])
173+
let rpc_key = kdf::derive_p256_key_pair_from_bytes(&root_key, &[b"rpc-key"])
174174
.context("Failed to derive RPC key from MPC root key")?;
175175

176176
// Use root key directly as K256 key (it's already 32 bytes)
@@ -426,7 +426,7 @@ async fn try_derive_keys_from_mpc(
426426
let mpc_domain_id = near.mpc_domain_id;
427427

428428
let network_id = near.network_id.as_deref().unwrap_or("testnet");
429-
let network_config = NetworkConfig::from_rpc_url(network_id, &rpc_url);
429+
let network_config = NetworkConfig::from_rpc_url(network_id, rpc_url.parse().unwrap());
430430

431431
// Load or generate NEAR signer (implicit account)
432432
let signer = load_or_generate_near_signer(cfg)?;
@@ -446,7 +446,12 @@ async fn try_derive_keys_from_mpc(
446446
tracing::info!(" Domain ID: {}", mpc_domain_id);
447447
tracing::info!(" Signer Account ID: {}", signer.account_id);
448448

449-
let kms_client = NearKmsClient::new(network_config, mpc_contract_id.clone(), kms_contract_id.clone(), Some(signer))?;
449+
let kms_client = NearKmsClient::new(
450+
network_config,
451+
mpc_contract_id.clone(),
452+
kms_contract_id.clone(),
453+
Some(signer),
454+
)?;
450455

451456
let mpc_public_key = kms_client.get_mpc_public_key(mpc_domain_id).await?;
452457

0 commit comments

Comments
 (0)