Skip to content

Commit

Permalink
Remove new RPC and fix up some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bkchr committed Sep 28, 2023
1 parent d004457 commit cca49bf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 54 deletions.
9 changes: 0 additions & 9 deletions substrate/client/rpc-api/src/author/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ pub trait AuthorApi<Hash, BlockHash> {
#[method(name = "author_rotateKeys")]
fn rotate_keys(&self) -> RpcResult<Bytes>;

/// Generate new session keys and returns the corresponding public keys.
///
/// The `owner` should be something that can be used on chain for verifying the ownership of the
/// generated keys using the returned `proof`. For example `owner` could be set to the account
/// id of the account registering the returned public session keys. The actual data to pass for
/// `owner` depends on the runtime logic verifying the `proof`.
#[method(name = "author_rotateKeysWithOwner")]
fn rotate_keys_with_owner(&self, owner: Bytes) -> RpcResult<GeneratedSessionKeys>;

/// Checks if the keystore has private keys for the given session public keys.
///
/// `session_keys` is the SCALE encoded session keys object from the runtime.
Expand Down
71 changes: 28 additions & 43 deletions substrate/client/rpc/src/author/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,6 @@ impl<P, Client> Author<P, Client> {
}
}

impl<P, Client> Author<P, Client>
where
P: TransactionPool + Sync + Send + 'static,
Client: HeaderBackend<P::Block> + ProvideRuntimeApi<P::Block> + Send + Sync + 'static,
Client::Api: SessionKeys<P::Block>,
P::Hash: Unpin,
<P::Block as BlockT>::Hash: Unpin,
{
fn rotate_keys_impl(&self, owner: Vec<u8>) -> RpcResult<GeneratedSessionKeys> {
self.deny_unsafe.check_if_safe()?;

let best_block_hash = self.client.info().best_hash;
let mut runtime_api = self.client.runtime_api();

runtime_api.register_extension(KeystoreExt::from(self.keystore.clone()));

let version = runtime_api
.api_version::<dyn SessionKeys<P::Block>>(best_block_hash)
.map_err(|api_err| Error::Client(Box::new(api_err)))?
.ok_or_else(|| Error::MissingSessionKeysApi)?;

if version < 2 {
#[allow(deprecated)]
runtime_api
.generate_session_keys_before_version_2(best_block_hash, None)
.map(|sk| GeneratedSessionKeys { keys: sk.into(), proof: None })
.map_err(|api_err| Error::Client(Box::new(api_err)).into())
} else {
runtime_api
.generate_session_keys(best_block_hash, owner, None)
.map(|sk| GeneratedSessionKeys {
keys: sk.keys.into(),
proof: Some(sk.proof.into()),
})
.map_err(|api_err| Error::Client(Box::new(api_err)).into())
}
}
}

/// Currently we treat all RPC transactions as externals.
///
/// Possibly in the future we could allow opt-in for special treatment
Expand Down Expand Up @@ -158,11 +119,35 @@ where
}

fn rotate_keys(&self) -> RpcResult<Bytes> {
self.rotate_keys_impl(Vec::new()).map(|k| k.keys)
}
self.deny_unsafe.check_if_safe()?;

let best_block_hash = self.client.info().best_hash;
let mut runtime_api = self.client.runtime_api();

runtime_api.register_extension(KeystoreExt::from(self.keystore.clone()));

let version = runtime_api
.api_version::<dyn SessionKeys<P::Block>>(best_block_hash)
.map_err(|api_err| Error::Client(Box::new(api_err)))?
.ok_or_else(|| Error::MissingSessionKeysApi)?;

let res = if version < 2 {
#[allow(deprecated)]
runtime_api
.generate_session_keys_before_version_2(best_block_hash, None)
.map(|sk| GeneratedSessionKeys { keys: sk.into(), proof: None })
.map_err(|api_err| Error::Client(Box::new(api_err)))
} else {
runtime_api
.generate_session_keys(best_block_hash, Vec::new(), None)
.map(|sk| GeneratedSessionKeys {
keys: sk.keys.into(),
proof: Some(sk.proof.into()),
})
.map_err(|api_err| Error::Client(Box::new(api_err)))
}?;

fn rotate_keys_with_owner(&self, owner: Bytes) -> RpcResult<GeneratedSessionKeys> {
self.rotate_keys_impl(owner.0)
Ok(res.keys)
}

fn has_session_keys(&self, session_keys: Bytes) -> RpcResult<bool> {
Expand Down
3 changes: 2 additions & 1 deletion substrate/primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,8 @@ macro_rules! impl_opaque_keys_inner {
/// Contains the public session keys and a `proof` to verify the ownership of these keys.
///
/// To generate session keys the [`impl_opaque_keys!`](crate::impl_opaque_keys) needs to be used
/// first to create the session keys type and this type provides the `generate` function.
/// first to create the session keys type and this type provides the `generate` function which
/// output is this type.
#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
pub struct GeneratedSessionKeys {
/// The opaque public session keys for registering on-chain.
Expand Down
8 changes: 7 additions & 1 deletion substrate/primitives/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ sp_api::decl_runtime_apis! {
#[api_version(2)]
pub trait SessionKeys {
/// Generate a set of session keys with optionally using the given seed.
///
/// The keys should be stored within the keystore exposed via runtime
/// externalities.
///
/// The seed needs to be a valid `utf8` string.
/// - `owner`: The `owner` will be used for constructing a `proof` of ownership of the
/// generated session keys. This is used by the on-chain logic to verify the ownership.
/// The data for `owner` depends on the runtime implementation, e.g. for FRAME this should
/// be the SCALE encoded account id.
/// - `seed`: A `seed/phrase` that is used to construct the private key. If `None`,
/// a random private key is generated.
///
/// Returns the concatenated SCALE encoded public keys.
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> GeneratedSessionKeys;
Expand Down

0 comments on commit cca49bf

Please sign in to comment.