mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 10:01:17 +00:00
Adds author_rotateKeys RPC (#3337)
* Adds `author_rotateKeys` RPC `author_rotateKeys` will rotate all session keys, store them into the keystore and returns their encoded public keys as result. These encoded public keys can be used directly to send the transaction to the chain. * Review comments
This commit is contained in:
committed by
Tomasz Drwięga
parent
6565e1f8aa
commit
0de7d9bb64
@@ -33,9 +33,9 @@ use log::warn;
|
||||
use codec::{Encode, Decode};
|
||||
use primitives::{
|
||||
Bytes, Blake2Hasher, H256, ed25519, sr25519, crypto::{Pair, Public, key_types},
|
||||
traits::BareCryptoStorePtr
|
||||
traits::BareCryptoStorePtr,
|
||||
};
|
||||
use sr_primitives::{generic, traits};
|
||||
use sr_primitives::{generic, traits::{self, ProvideRuntimeApi}};
|
||||
use self::error::{Error, Result};
|
||||
use transaction_pool::{
|
||||
txpool::{
|
||||
@@ -47,6 +47,7 @@ use transaction_pool::{
|
||||
watcher::Status,
|
||||
},
|
||||
};
|
||||
use session::SessionKeys;
|
||||
|
||||
pub use self::gen_client::Client as AuthorClient;
|
||||
|
||||
@@ -68,6 +69,10 @@ pub trait AuthorApi<Hash, BlockHash> {
|
||||
maybe_public: Option<Bytes>
|
||||
) -> Result<Bytes>;
|
||||
|
||||
/// Generate new session keys and returns the corresponding public keys.
|
||||
#[rpc(name = "author_rotateKeys")]
|
||||
fn rotate_keys(&self) -> Result<Bytes>;
|
||||
|
||||
/// Returns all pending extrinsics, potentially grouped by sender.
|
||||
#[rpc(name = "author_pendingExtrinsics")]
|
||||
fn pending_extrinsics(&self) -> Result<Vec<Bytes>>;
|
||||
@@ -138,6 +143,8 @@ impl<B, E, P, RA> AuthorApi<ExHash<P>, BlockHash<P>> for Author<B, E, P, RA> whe
|
||||
P::Block: traits::Block<Hash=H256>,
|
||||
P::Error: 'static,
|
||||
RA: Send + Sync + 'static,
|
||||
Client<B, E, P::Block, RA>: ProvideRuntimeApi,
|
||||
<Client<B, E, P::Block, RA> as ProvideRuntimeApi>::Api: SessionKeys<P::Block>,
|
||||
{
|
||||
type Metadata = crate::metadata::Metadata;
|
||||
|
||||
@@ -170,6 +177,14 @@ impl<B, E, P, RA> AuthorApi<ExHash<P>, BlockHash<P>> for Author<B, E, P, RA> whe
|
||||
Ok(public.into())
|
||||
}
|
||||
|
||||
fn rotate_keys(&self) -> Result<Bytes> {
|
||||
let best_block_hash = self.client.info().chain.best_hash;
|
||||
self.client.runtime_api().generate_session_keys(
|
||||
&generic::BlockId::Hash(best_block_hash),
|
||||
None,
|
||||
).map(Into::into).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn submit_extrinsic(&self, ext: Bytes) -> Result<ExHash<P>> {
|
||||
let xt = Decode::decode(&mut &ext[..])?;
|
||||
let best_block_hash = self.client.info().chain.best_hash;
|
||||
|
||||
@@ -27,7 +27,10 @@ use primitives::{
|
||||
H256, blake2_256, hexdisplay::HexDisplay, traits::BareCryptoStore, testing::KeyStore,
|
||||
ed25519, crypto::key_types,
|
||||
};
|
||||
use test_client::{self, AccountKeyring, runtime::{Extrinsic, Transfer}};
|
||||
use test_client::{
|
||||
self, AccountKeyring, runtime::{Extrinsic, Transfer, SessionKeys}, DefaultTestClientBuilderExt,
|
||||
TestClientBuilderExt,
|
||||
};
|
||||
use tokio::runtime;
|
||||
|
||||
fn uxt(sender: AccountKeyring, nonce: u64) -> Extrinsic {
|
||||
@@ -203,4 +206,35 @@ fn should_insert_key() {
|
||||
.ed25519_key_pair(key_types::ED25519, &key_pair.public()).expect("Key exists in store");
|
||||
|
||||
assert_eq!(key_pair.public(), store_key_pair.public());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_rotate_keys() {
|
||||
let runtime = runtime::Runtime::new().unwrap();
|
||||
let keystore = KeyStore::new();
|
||||
let client = Arc::new(test_client::TestClientBuilder::new().set_keystore(keystore.clone()).build());
|
||||
let p = Author {
|
||||
client: client.clone(),
|
||||
pool: Arc::new(Pool::new(Default::default(), ChainApi::new(client))),
|
||||
subscriptions: Subscriptions::new(Arc::new(runtime.executor())),
|
||||
keystore: keystore.clone(),
|
||||
};
|
||||
|
||||
let new_public_keys = p.rotate_keys().expect("Rotates the keys");
|
||||
|
||||
let session_keys = SessionKeys::decode(&mut &new_public_keys[..])
|
||||
.expect("SessionKeys decode successfully");
|
||||
|
||||
let ed25519_key_pair = keystore.read().ed25519_key_pair(
|
||||
key_types::ED25519,
|
||||
&session_keys.ed25519.clone().into(),
|
||||
).expect("ed25519 key exists in store");
|
||||
|
||||
let sr25519_key_pair = keystore.read().sr25519_key_pair(
|
||||
key_types::SR25519,
|
||||
&session_keys.sr25519.clone().into(),
|
||||
).expect("sr25519 key exists in store");
|
||||
|
||||
assert_eq!(session_keys.ed25519, ed25519_key_pair.public().into());
|
||||
assert_eq!(session_keys.sr25519, sr25519_key_pair.public().into());
|
||||
}
|
||||
@@ -260,7 +260,8 @@ fn should_return_runtime_version() {
|
||||
\"specVersion\":1,\"implVersion\":1,\"apis\":[[\"0xdf6acb689907609b\",2],\
|
||||
[\"0x37e397fc7c91f5e4\",1],[\"0xd2bc9897eed08f15\",1],[\"0x40fe3ad401f8959a\",3],\
|
||||
[\"0xc6e9a76309f39b09\",1],[\"0xdd718d5cc53262d4\",1],[\"0xcbca25e39f142387\",1],\
|
||||
[\"0xf78b278be53f454c\",1]]}";
|
||||
[\"0xf78b278be53f454c\",1],[\"0xab3c0572291feb8b\",1]]}";
|
||||
|
||||
assert_eq!(
|
||||
serde_json::to_string(&api.runtime_version(None.into()).unwrap()).unwrap(),
|
||||
result,
|
||||
|
||||
Reference in New Issue
Block a user