Skip to content

Commit

Permalink
runtime-sdk/src/modules/core: add "core.PublicKey" call
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMist committed Sep 21, 2024
1 parent efa11c1 commit 64c77d4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
38 changes: 38 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,23 @@ impl<Cfg: Config> Module<Cfg> {
<C::Runtime as Runtime>::Modules::check_invariants(ctx)
}

fn public_key_common<C: Context>(ctx: &C) -> Result<types::PublicKeyQueryResponse, Error> {
let key_manager = ctx

Check warning on line 852 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-L852

Added lines #L851 - L852 were not covered by tests
.key_manager()
.ok_or_else(|| Error::InvalidArgument(anyhow!("key manager not available")))?;
let epoch = ctx.epoch();
let key_pair_id = callformat::get_key_pair_id(epoch);
let public_key = key_manager
.get_public_key(key_pair_id)
.map_err(|_| Error::InvalidArgument(anyhow!("cannot get public key")))?;
let runtime_id = *ctx.runtime_id();
Ok(types::PublicKeyQueryResponse {
runtime_id,
key_pair_id,
public_key,

Check warning on line 864 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-L864

Added lines #L854 - L864 were not covered by tests
})
}

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L893 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L902 - L903 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.PublicKey".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Call, name: "core.PublicKey".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
14 changes: 14 additions & 0 deletions runtime-sdk/src/modules/core/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::collections::BTreeMap;

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

use oasis_core_keymanager::crypto::KeyPairId;

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

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

/// Response to the public key query.
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct PublicKeyQueryResponse {
/// ID of the public key which signs the call data public keys
pub key_pair_id: KeyPairId,
/// Public key which signs the call data public keys
pub public_key: SignedPublicKey,
/// 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 64c77d4

Please sign in to comment.