From ce91c54539567170fd6d31b7af0b2655d9acaeb7 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Wed, 17 Jul 2024 18:18:05 -0700 Subject: [PATCH 1/3] contract: add my_derived_public_key() --- chain-signatures/contract/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/chain-signatures/contract/src/lib.rs b/chain-signatures/contract/src/lib.rs index c6f3166cf..078ffc7bf 100644 --- a/chain-signatures/contract/src/lib.rs +++ b/chain-signatures/contract/src/lib.rs @@ -13,6 +13,7 @@ use near_sdk::{ PromiseError, PublicKey, }; +use k256::elliptic_curve::sec1::ToEncodedPoint; use primitives::{ CandidateInfo, Candidates, ParticipantInfo, Participants, PkVotes, SignRequest, SignaturePromiseError, SignatureResult, Votes, @@ -373,6 +374,19 @@ impl VersionedMpcContract { } } + /// This is the derived key given path + pub fn my_derived_public_key(&self, path: String) -> PublicKey { + let predecessor = env::predecessor_account_id(); + let epsilon = derive_epsilon(&predecessor, &path); + let derived_public_key = + derive_key(near_public_key_to_affine_point(self.public_key()), epsilon); + let encoded_point = derived_public_key.to_encoded_point(false); + let slice: &[u8] = &encoded_point.as_bytes()[1..65]; + let mut data: Vec = vec![near_sdk::CurveType::SECP256K1 as u8]; + data.extend(slice.to_vec()); + PublicKey::try_from(data).unwrap() + } + /// Key versions refer new versions of the root key that we may choose to generate on cohort changes /// Older key versions will always work but newer key versions were never held by older signers /// Newer key versions may also add new security features, like only existing within a secure enclave From 8f6fa1aa2c71bcc06d8b74c976b8e3881d8f43dd Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Thu, 18 Jul 2024 10:55:18 -0700 Subject: [PATCH 2/3] rename to derived_public_key() --- chain-signatures/contract/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chain-signatures/contract/src/lib.rs b/chain-signatures/contract/src/lib.rs index 078ffc7bf..e389b7a7d 100644 --- a/chain-signatures/contract/src/lib.rs +++ b/chain-signatures/contract/src/lib.rs @@ -374,8 +374,8 @@ impl VersionedMpcContract { } } - /// This is the derived key given path - pub fn my_derived_public_key(&self, path: String) -> PublicKey { + /// This is the derived public key of the caller given path + pub fn derived_public_key(&self, path: String) -> PublicKey { let predecessor = env::predecessor_account_id(); let epsilon = derive_epsilon(&predecessor, &path); let derived_public_key = From 852297e6ddc2305f2a0eca4a1d21f1f59b02a810 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Thu, 18 Jul 2024 12:37:08 -0700 Subject: [PATCH 3/3] optional predecessor --- chain-signatures/contract/src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/chain-signatures/contract/src/lib.rs b/chain-signatures/contract/src/lib.rs index e389b7a7d..1437c47b1 100644 --- a/chain-signatures/contract/src/lib.rs +++ b/chain-signatures/contract/src/lib.rs @@ -374,9 +374,10 @@ impl VersionedMpcContract { } } - /// This is the derived public key of the caller given path - pub fn derived_public_key(&self, path: String) -> PublicKey { - let predecessor = env::predecessor_account_id(); + /// This is the derived public key of the caller given path and predecessor + /// if predecessor is not provided, it will be the caller of the contract + pub fn derived_public_key(&self, path: String, predecessor: Option) -> PublicKey { + let predecessor = predecessor.unwrap_or_else(env::predecessor_account_id); let epsilon = derive_epsilon(&predecessor, &path); let derived_public_key = derive_key(near_public_key_to_affine_point(self.public_key()), epsilon);