Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contract: add my_derived_public_key() #691

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions chain-signatures/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -373,6 +374,19 @@ 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add in an option where people can specify the predecessor account id since the one calling this function might or might not be the right one. Like in this case:

user.near calls nft.near
nft.near calls sign
predecessor_id = nft.near

user.near calls derived_public_key
predecessor_id = user.near
Suggested change
pub fn derived_public_key(&self, path: String) -> PublicKey {
let predecessor = env::predecessor_account_id();
pub fn derived_public_key(&self, path: String, predecessor: Option<AccountId>) -> PublicKey {
let predecessor = predecessor.unwrap_or_else(env::predecessor_account_id);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet. Will do.

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<u8> = 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
Expand Down
Loading