multiversx_sdk/gateway/
gateway_chain_simulator_add_keys.rs

1use std::collections::HashMap;
2
3use anyhow::anyhow;
4
5use crate::{gateway::ADD_KEYS, utils::base64_encode};
6
7use super::{
8    gateway_chain_simulator_blocks::GenerateBlocksResponse, GatewayRequest, GatewayRequestType,
9};
10
11/// Allows to add new validator private keys in the multi key handler.
12pub struct ChainSimulatorAddKeysRequest {
13    payload: HashMap<&'static str, Vec<String>>,
14}
15
16impl ChainSimulatorAddKeysRequest {
17    pub fn with_keys(keys: Vec<Vec<u8>>) -> Self {
18        let mut payload = HashMap::new();
19        let keys_str_vec: Vec<String> = keys
20            .into_iter()
21            .map(|key| base64_encode(key).to_string())
22            .collect();
23        payload.insert("privateKeysBase64", keys_str_vec);
24        Self { payload }
25    }
26}
27
28impl GatewayRequest for ChainSimulatorAddKeysRequest {
29    type Payload = HashMap<&'static str, Vec<String>>;
30    type DecodedJson = GenerateBlocksResponse;
31    type Result = String;
32
33    fn request_type(&self) -> GatewayRequestType {
34        GatewayRequestType::Post
35    }
36
37    fn get_endpoint(&self) -> String {
38        ADD_KEYS.to_owned()
39    }
40
41    fn get_payload(&self) -> Option<&Self::Payload> {
42        Some(&self.payload)
43    }
44
45    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> {
46        match decoded.code.as_str() {
47            "successful" => Ok(decoded.code),
48            _ => Err(anyhow!("{}", decoded.error)),
49        }
50    }
51}