-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from CESSProject/feat/pubkeys_service
feat:add get public service for miner to ensure which ceseal connect with
- Loading branch information
Showing
13 changed files
with
259 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
syntax = "proto3"; | ||
|
||
package ceseal.pubkeys; | ||
|
||
// Provide the storage miners with Ceseal's various public key queries | ||
service CesealPubkeysProvider { | ||
// Get the Ceseal identity public key | ||
rpc get_identity_pubkey(Request) returns (IdentityPubkeyResponse) {} | ||
// Get the master public key | ||
rpc get_master_pubkey(Request) returns (MasterPubkeyResponse) {} | ||
// Get the PORD2 public key | ||
rpc get_podr2_pubkey(Request) returns (Podr2PubkeyResponse) {} | ||
} | ||
|
||
message Request { | ||
// The account id that the storage miner registered on the chain | ||
bytes storage_miner_account_id = 1; | ||
} | ||
|
||
message IdentityPubkeyResponse { | ||
// the identity public key | ||
bytes pubkey = 1; | ||
// The timestamp for the processing of the request | ||
int64 timestamp = 2; | ||
// Use the sr25519 algorithm to sign the timestamp fields above (use it's Big-Endian bytes) | ||
bytes signature = 3; | ||
} | ||
|
||
message MasterPubkeyResponse { | ||
// the master public key | ||
bytes pubkey = 1; | ||
// The timestamp for the processing of the request | ||
int64 timestamp = 2; | ||
// Use the sr25519 algorithm to sign the timestamp fields above (use it's Big-Endian bytes) | ||
bytes signature = 3; | ||
} | ||
|
||
message Podr2PubkeyResponse { | ||
// the PODR2 public key | ||
bytes pubkey = 1; | ||
// The timestamp for the processing of the request | ||
int64 timestamp = 2; | ||
// Use the RSA algorithm to sign the timestamp fields above (use it's Big-Endian bytes) | ||
bytes signature = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::expert::CesealExpertStub; | ||
use cestory_api::pubkeys::{ | ||
ceseal_pubkeys_provider_server::{ | ||
CesealPubkeysProvider, CesealPubkeysProviderServer as CesealPubkeysProviderServerPb, | ||
}, | ||
IdentityPubkeyResponse, MasterPubkeyResponse, Podr2PubkeyResponse, Request as InnerReq, | ||
}; | ||
use sp_core::{crypto::AccountId32, ByteArray, Pair}; | ||
use std::result::Result as StdResult; | ||
use tonic::{Request, Response, Status}; | ||
|
||
pub type Result<T> = StdResult<Response<T>, Status>; | ||
pub type CesealPubkeysProviderServer = CesealPubkeysProviderServerPb<CesealPubkeysProviderImpl>; | ||
|
||
pub struct CesealPubkeysProviderImpl { | ||
ceseal_expert: CesealExpertStub, | ||
} | ||
|
||
pub fn new_pubkeys_provider_server(ceseal_expert: CesealExpertStub) -> CesealPubkeysProviderServer { | ||
CesealPubkeysProviderServerPb::new(CesealPubkeysProviderImpl { ceseal_expert }) | ||
} | ||
|
||
async fn is_storage_miner_registered_on_chain( | ||
ceseal_expert: &CesealExpertStub, | ||
account_id: &[u8], | ||
) -> StdResult<(), Status> { | ||
let account_id = AccountId32::from_slice(account_id).map_err(|_| Status::internal("invalid input account"))?; | ||
let registered = ceseal_expert | ||
.using_chain_storage(move |opt| { | ||
if let Some(cs) = opt { | ||
cs.is_storage_miner_registered_ignore_state(account_id) | ||
} else { | ||
false | ||
} | ||
}) | ||
.await | ||
.map_err(|e| Status::internal(format!("internal error: {}", e.to_string())))?; | ||
if !registered { | ||
return Err(Status::internal("the storage miner is not registered on the chain")) | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[tonic::async_trait] | ||
impl CesealPubkeysProvider for CesealPubkeysProviderImpl { | ||
async fn get_identity_pubkey(&self, request: Request<InnerReq>) -> Result<IdentityPubkeyResponse> { | ||
let request = request.into_inner(); | ||
is_storage_miner_registered_on_chain(&self.ceseal_expert, &request.storage_miner_account_id[..]).await?; | ||
let now_ts = chrono::Utc::now().timestamp_millis(); | ||
let pubkey = self.ceseal_expert.identify_public_key(); | ||
let sign = self.ceseal_expert.identity_key().sign(&now_ts.to_be_bytes()); | ||
Ok(Response::new(IdentityPubkeyResponse { | ||
pubkey: pubkey.to_raw_vec(), | ||
timestamp: now_ts, | ||
signature: sign.0.to_vec(), | ||
})) | ||
} | ||
|
||
async fn get_master_pubkey(&self, request: Request<InnerReq>) -> Result<MasterPubkeyResponse> { | ||
let request = request.into_inner(); | ||
is_storage_miner_registered_on_chain(&self.ceseal_expert, &request.storage_miner_account_id[..]).await?; | ||
let now_ts = chrono::Utc::now().timestamp_millis(); | ||
let pubkey = self.ceseal_expert.master_key().public(); | ||
let sign = self.ceseal_expert.master_key().sign(&now_ts.to_be_bytes()); | ||
Ok(Response::new(MasterPubkeyResponse { | ||
pubkey: pubkey.to_raw_vec(), | ||
timestamp: now_ts, | ||
signature: sign.0.to_vec(), | ||
})) | ||
} | ||
|
||
async fn get_podr2_pubkey(&self, request: Request<InnerReq>) -> Result<Podr2PubkeyResponse> { | ||
use rsa::{pkcs1::EncodeRsaPublicKey, Pkcs1v15Sign}; | ||
|
||
let request = request.into_inner(); | ||
is_storage_miner_registered_on_chain(&self.ceseal_expert, &request.storage_miner_account_id[..]).await?; | ||
let now_ts = chrono::Utc::now().timestamp_millis(); | ||
let pubkey = self.ceseal_expert.podr2_key().pkey.clone(); | ||
let pubkey = pubkey | ||
.to_pkcs1_der() | ||
.map_err(|e| Status::internal(format!("PKCS#1-encoding Podr2 public key error: {:?}", e)))? | ||
.to_vec(); | ||
let sign = self | ||
.ceseal_expert | ||
.podr2_key() | ||
.skey | ||
.sign(Pkcs1v15Sign::new_raw(), &now_ts.to_be_bytes()) | ||
.map_err(|e| Status::internal(format!("Podr2 key sign error: {:?}", e)))?; | ||
Ok(Response::new(Podr2PubkeyResponse { pubkey, timestamp: now_ts, signature: sign })) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.