avail_rust_core/rpc/
author.rs1use crate::error::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(
20 "Session keys len cannot have length be more or less than 128",
21 ));
22 }
23
24 let err = |e: TryFromSliceError| e.to_string();
25
26 let babe: [u8; 32] = value[0..32].try_into().map_err(err)?;
27 let grandpa: [u8; 32] = value[32..64].try_into().map_err(err)?;
28 let im_online: [u8; 32] = value[64..96].try_into().map_err(err)?;
29 let authority_discovery: [u8; 32] = value[96..128].try_into().map_err(err)?;
30 Ok(Self {
31 babe,
32 grandpa,
33 im_online,
34 authority_discovery,
35 })
36 }
37}
38
39pub async fn rotate_keys(client: &RpcClient) -> Result<SessionKeys, Error> {
40 let params = rpc_params![];
41 let value: Vec<u8> = client.request("author_rotateKeys", params).await?;
42 let keys = SessionKeys::try_from(value.as_slice())?;
43 Ok(keys)
44}
45
46pub async fn submit_extrinsic(client: &RpcClient, extrinsic: &[u8]) -> Result<H256, subxt_rpcs::Error> {
47 let ext = std::format!("0x{}", hex::encode(extrinsic));
48 let params = rpc_params![ext];
49 let value: H256 = client.request("author_submitExtrinsic", params).await?;
50 Ok(value)
51}