1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::jcli::command::votes::committee::MemberKeyCommand;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::{assert::PathAssert, fixture::FileWriteStr, NamedTempFile};
use jortestkit::prelude::ProcessOutput;
use std::path::Path;

pub struct MemberKey {
    member_key_command: MemberKeyCommand,
}

impl MemberKey {
    pub fn new(member_key_command: MemberKeyCommand) -> Self {
        Self { member_key_command }
    }

    #[allow(clippy::wrong_self_convention)]
    pub fn to_public<S: Into<String>>(
        self,
        member_secret_key: S,
    ) -> Result<String, std::io::Error> {
        let input_file = NamedTempFile::new("member_key.tmp").unwrap();
        input_file.write_str(&member_secret_key.into()).unwrap();

        let output_file = NamedTempFile::new("output_key.tmp").unwrap();

        self.member_key_command
            .to_public(input_file.path(), output_file.path())
            .build()
            .assert()
            .success();
        output_file.assert(jortestkit::prelude::file_exists_and_not_empty());
        jortestkit::prelude::read_file(output_file.path())
    }

    pub fn generate<P: AsRef<Path>, S: Into<String>>(
        self,
        communication_key: P,
        crs: S,
        index: u32,
        threshold: u32,
        maybe_seed: Option<String>,
    ) -> String {
        self.member_key_command
            .generate(communication_key, crs, index, threshold, maybe_seed)
            .build()
            .assert()
            .success()
            .get_output()
            .as_single_line()
    }
}