avail_rust_core/rpc/
author.rs1use super::Error;
2use primitive_types::H256;
3use std::array::TryFromSliceError;
4use subxt_rpcs::{RpcClient, rpc_params};
5
6#[derive(Debug, Clone)]
7pub struct SessionKeys {
8 pub babe: [u8; 32],
9 pub grandpa: [u8; 32],
10 pub im_online: [u8; 32],
11 pub authority_discovery: [u8; 32],
12}
13
14impl TryFrom<&[u8]> for SessionKeys {
15 type Error = String;
16
17 fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
18 if value.len() != 128 {
19 return Err(String::from("Session keys len cannot have length be more or less than 128"));
20 }
21
22 let err = |e: TryFromSliceError| e.to_string();
23
24 let babe: [u8; 32] = value[0..32].try_into().map_err(err)?;
25 let grandpa: [u8; 32] = value[32..64].try_into().map_err(err)?;
26 let im_online: [u8; 32] = value[64..96].try_into().map_err(err)?;
27 let authority_discovery: [u8; 32] = value[96..128].try_into().map_err(err)?;
28 Ok(Self { babe, grandpa, im_online, authority_discovery })
29 }
30}
31
32pub async fn rotate_keys(client: &RpcClient) -> Result<SessionKeys, Error> {
33 let params = rpc_params![];
34 let value: Vec<u8> = client.request("author_rotateKeys", params).await?;
35 let keys = SessionKeys::try_from(value.as_slice()).map_err(|e| Error::MalformedResponse(e.to_string()))?;
36 Ok(keys)
37}
38
39pub async fn submit_extrinsic(client: &RpcClient, extrinsic: &[u8]) -> Result<H256, Error> {
40 let ext = std::format!("0x{}", const_hex::encode(extrinsic));
41 let params = rpc_params![ext];
42 let value: H256 = client.request("author_submitExtrinsic", params).await?;
43 Ok(value)
44}