Skip to content

Commit 2e959ec

Browse files
committed
runtime-sdk/src/modules/core: add "core.KeyManagerPublicKey" call
1 parent efa11c1 commit 2e959ec

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

runtime-sdk/src/modules/core/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ pub trait Config: 'static {
427427
const GAS_COST_CALL_CALLDATA_PUBLIC_KEY: u64 = 20;
428428
/// The gas cost of the internal call to retrieve the current epoch.
429429
const GAS_COST_CALL_CURRENT_EPOCH: u64 = 10;
430+
/// The gas cost of the internal call to retrieve the current long-term public key
431+
const GAS_COST_CALL_PUBLIC_KEY: u64 = 20;
430432
}
431433

432434
pub struct Module<Cfg: Config> {
@@ -846,6 +848,25 @@ impl<Cfg: Config> Module<Cfg> {
846848
<C::Runtime as Runtime>::Modules::check_invariants(ctx)
847849
}
848850

851+
fn keymanager_public_key_common<C: Context>(
852+
ctx: &C,
853+
) -> Result<types::KeyManagerPublicKeyQueryResponse, Error> {
854+
let key_manager = ctx
855+
.key_manager()
856+
.ok_or_else(|| Error::InvalidArgument(anyhow!("key manager not available")))?;
857+
let epoch = ctx.epoch();
858+
let key_pair_id = callformat::get_key_pair_id(epoch);
859+
let public_key = key_manager
860+
.get_public_key(key_pair_id)
861+
.map_err(|_| Error::InvalidArgument(anyhow!("cannot get public key")))?;
862+
let runtime_id = *ctx.runtime_id();
863+
Ok(types::KeyManagerPublicKeyQueryResponse {
864+
runtime_id,
865+
key_pair_id,
866+
public_key,
867+
})
868+
}
869+
849870
fn calldata_public_key_common<C: Context>(
850871
ctx: &C,
851872
) -> Result<types::CallDataPublicKeyQueryResponse, Error> {
@@ -865,6 +886,25 @@ impl<Cfg: Config> Module<Cfg> {
865886
Ok(types::CallDataPublicKeyQueryResponse { public_key, epoch })
866887
}
867888

889+
/// Retrieve the public key for encrypting call data.
890+
#[handler(query = "core.KeyManagerPublicKey")]
891+
fn query_keymanager_public_key<C: Context>(
892+
ctx: &C,
893+
_args: (),
894+
) -> Result<types::KeyManagerPublicKeyQueryResponse, Error> {
895+
Self::keymanager_public_key_common(ctx)
896+
}
897+
898+
/// Retrieve the public key for encrypting call data (internally exposed call).
899+
#[handler(call = "core.KeyManagerPublicKey", internal)]
900+
fn internal_keymanager_public_key<C: Context>(
901+
ctx: &C,
902+
_args: (),
903+
) -> Result<types::KeyManagerPublicKeyQueryResponse, Error> {
904+
<C::Runtime as Runtime>::Core::use_tx_gas(Cfg::GAS_COST_CALL_CALLDATA_PUBLIC_KEY)?;
905+
Self::keymanager_public_key_common(ctx)
906+
}
907+
868908
/// Retrieve the public key for encrypting call data.
869909
#[handler(query = "core.CallDataPublicKey")]
870910
fn query_calldata_public_key<C: Context>(

runtime-sdk/src/modules/core/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,8 @@ fn test_module_info() {
11741174
methods: vec![
11751175
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.EstimateGas".to_string() },
11761176
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.CheckInvariants".to_string() },
1177+
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.KeyManagerPublicKey".to_string() },
1178+
MethodHandlerInfo { kind: MethodHandlerKind::Call, name: "core.KeyManagerPublicKey".to_string() },
11771179
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.CallDataPublicKey".to_string() },
11781180
MethodHandlerInfo { kind: MethodHandlerKind::Call, name: "core.CallDataPublicKey".to_string() },
11791181
MethodHandlerInfo { kind: MethodHandlerKind::Call, name: "core.CurrentEpoch".to_string() },

runtime-sdk/src/modules/core/types.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use std::collections::BTreeMap;
22

33
use crate::{
4+
core::common::namespace::Namespace,
45
keymanager::SignedPublicKey,
56
types::transaction::{CallResult, CallerAddress, Transaction},
67
};
78

9+
use oasis_core_keymanager::crypto::KeyPairId;
10+
811
/// Key in the versions map used for the global state version.
912
pub const VERSION_GLOBAL_KEY: &str = "";
1013

@@ -41,6 +44,17 @@ pub struct CallDataPublicKeyQueryResponse {
4144
pub epoch: u64,
4245
}
4346

47+
/// Response to the public key query.
48+
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
49+
pub struct KeyManagerPublicKeyQueryResponse {
50+
/// ID of the public key which signs the call data public keys
51+
pub key_pair_id: KeyPairId,
52+
/// Public key which signs the call data public keys
53+
pub public_key: SignedPublicKey,
54+
/// For reference, which runtime ID is this for?
55+
pub runtime_id: Namespace,
56+
}
57+
4458
#[derive(Debug, Copy, Clone, cbor::Encode, cbor::Decode)]
4559
#[cfg_attr(test, derive(PartialEq, Eq))]
4660
pub enum MethodHandlerKind {

0 commit comments

Comments
 (0)