Skip to content

Commit

Permalink
runtime-sdk/src/modules/core: add "core.KeyManagerPublicKey" call
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMist committed Oct 24, 2024
1 parent c274d21 commit 3d06649
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
39 changes: 39 additions & 0 deletions runtime-sdk/src/modules/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ pub trait Config: 'static {
const GAS_COST_CALL_CALLDATA_PUBLIC_KEY: u64 = 20;
/// The gas cost of the internal call to retrieve the current epoch.
const GAS_COST_CALL_CURRENT_EPOCH: u64 = 10;
/// The gas cost of the internal call to retrieve the current long-term public key
const GAS_COST_CALL_PUBLIC_KEY: u64 = 20;
}

pub struct Module<Cfg: Config> {
Expand Down Expand Up @@ -846,6 +848,24 @@ impl<Cfg: Config> Module<Cfg> {
<C::Runtime as Runtime>::Modules::check_invariants(ctx)
}

fn keymanager_public_key_common<C: Context>(

Check warning on line 851 in runtime-sdk/src/modules/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L851 was not covered by tests
ctx: &C,
) -> Result<types::KeyManagerPublicKeyQueryResponse, Error> {
let key_manager = ctx

Check warning on line 854 in runtime-sdk/src/modules/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L854 was not covered by tests
.key_manager()
.ok_or_else(|| Error::InvalidArgument(anyhow!("key manager not available")))?;
let runtime_id = key_manager.runtime_id().ok_or_else(|| Error::InvalidArgument(anyhow!("cannot get runtime_id")))?;
let public_key = key_manager.runtime_signing_key().ok_or_else(|| Error::InvalidArgument(anyhow!("cannot get runtime signing key")))?;
let epoch = ctx.epoch();
let key_pair_id = callformat::get_key_pair_id(epoch);
let runtime_id = *ctx.runtime_id();
Ok(types::KeyManagerPublicKeyQueryResponse {
runtime_id,
key_pair_id,
public_key,

Check warning on line 865 in runtime-sdk/src/modules/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

runtime-sdk/src/modules/core/mod.rs#L856-L865

Added lines #L856 - L865 were not covered by tests
})
}

fn calldata_public_key_common<C: Context>(
ctx: &C,
) -> Result<types::CallDataPublicKeyQueryResponse, Error> {
Expand All @@ -865,6 +885,25 @@ impl<Cfg: Config> Module<Cfg> {
Ok(types::CallDataPublicKeyQueryResponse { public_key, epoch })
}

/// Retrieve the public key for encrypting call data.
#[handler(query = "core.KeyManagerPublicKey")]
fn query_keymanager_public_key<C: Context>(

Check warning on line 890 in runtime-sdk/src/modules/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L890 was not covered by tests
ctx: &C,
_args: (),
) -> Result<types::KeyManagerPublicKeyQueryResponse, Error> {
Self::keymanager_public_key_common(ctx)

Check warning on line 894 in runtime-sdk/src/modules/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L894 was not covered by tests
}

/// Retrieve the public key for encrypting call data (internally exposed call).
#[handler(call = "core.KeyManagerPublicKey", internal)]
fn internal_keymanager_public_key<C: Context>(

Check warning on line 899 in runtime-sdk/src/modules/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L899 was not covered by tests
ctx: &C,
_args: (),
) -> Result<types::KeyManagerPublicKeyQueryResponse, Error> {
<C::Runtime as Runtime>::Core::use_tx_gas(Cfg::GAS_COST_CALL_CALLDATA_PUBLIC_KEY)?;
Self::keymanager_public_key_common(ctx)

Check warning on line 904 in runtime-sdk/src/modules/core/mod.rs

View check run for this annotation

Codecov / codecov/patch

runtime-sdk/src/modules/core/mod.rs#L903-L904

Added lines #L903 - L904 were not covered by tests
}

/// Retrieve the public key for encrypting call data.
#[handler(query = "core.CallDataPublicKey")]
fn query_calldata_public_key<C: Context>(
Expand Down
2 changes: 2 additions & 0 deletions runtime-sdk/src/modules/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,8 @@ fn test_module_info() {
methods: vec![
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.EstimateGas".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.CheckInvariants".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.KeyManagerPublicKey".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Call, name: "core.KeyManagerPublicKey".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.CallDataPublicKey".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Call, name: "core.CallDataPublicKey".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Call, name: "core.CurrentEpoch".to_string() },
Expand Down
15 changes: 15 additions & 0 deletions runtime-sdk/src/modules/core/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::collections::BTreeMap;

use crate::{
core::common::namespace::Namespace,
keymanager::SignedPublicKey,
types::transaction::{CallResult, CallerAddress, Transaction},
};

use oasis_core_keymanager::crypto::KeyPairId;
use oasis_core_runtime::common::crypto::signature::PublicKey;

/// Key in the versions map used for the global state version.
pub const VERSION_GLOBAL_KEY: &str = "";

Expand Down Expand Up @@ -41,6 +45,17 @@ pub struct CallDataPublicKeyQueryResponse {
pub epoch: u64,
}

/// Response to the public key query.
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct KeyManagerPublicKeyQueryResponse {
/// ID of the public key which signs the call data public keys
pub key_pair_id: KeyPairId,
/// Runtime signing key which signs the call data public keys
pub public_key: PublicKey,
/// For reference, which runtime ID is this for?
pub runtime_id: Namespace,
}

#[derive(Debug, Copy, Clone, cbor::Encode, cbor::Decode)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum MethodHandlerKind {
Expand Down

0 comments on commit 3d06649

Please sign in to comment.