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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::{
error::{Error, KeyLoadingError},
InterBtcParachain, InterBtcSigner, ShutdownSender,
};
use clap::Parser;
use sp_core::{sr25519, Pair};
use sp_keyring::AccountKeyring;
use std::{collections::HashMap, num::ParseIntError, str::FromStr, time::Duration};
#[derive(Parser, Debug, Clone)]
pub struct ProviderUserOpts {
#[clap(long, conflicts_with_all = ["keyfile","keyuri"], value_parser = parse_account_keyring)]
pub keyring: Option<AccountKeyring>,
#[clap(long, conflicts_with_all = ["keyring"], requires = "keyname", required_unless_present_any = ["keyring","keyuri"])]
pub keyfile: Option<String>,
#[clap(long, conflicts_with = "keyring", required_unless_present = "keyring")]
pub keyname: Option<String>,
#[clap(long, conflicts_with_all = ["keyring"], requires = "keyname", required_unless_present_any = ["keyring","keyfile"])]
pub keyuri: Option<String>,
}
impl ProviderUserOpts {
pub fn get_key_pair(&self) -> Result<(sr25519::Pair, String), Error> {
let (pair, user_name) = match (
self.keyfile.as_ref(), self.keyname.as_ref(), &self.keyring, self.keyuri.as_ref(), ) {
(Some(file_path), Some(keyname), None, None) => {
(get_credentials_from_file(file_path, keyname)?, keyname.to_string())
}
(None, Some(keyname), None, Some(keyuri)) => (get_pair_from_phrase(keyuri)?, keyname.to_string()),
(Some(_file_path), Some(keyname), None, Some(keyuri)) => {
(get_pair_from_phrase(keyuri)?, keyname.to_string())
}
(None, None, Some(keyring), None) => (keyring.pair(), keyring.to_string()),
_ => {
return Err(Error::KeyringArgumentError);
}
};
Ok((pair, user_name))
}
}
fn get_pair_from_phrase(keyuri: &str) -> Result<sr25519::Pair, KeyLoadingError> {
sr25519::Pair::from_string(keyuri, None).map_err(KeyLoadingError::SecretStringError)
}
fn get_credentials_from_file(file_path: &str, keyname: &str) -> Result<sr25519::Pair, KeyLoadingError> {
let file = std::fs::File::open(file_path)?;
let reader = std::io::BufReader::new(file);
let map: HashMap<String, String> = serde_json::from_reader(reader)?;
let pair_str = map.get(keyname).ok_or(KeyLoadingError::KeyNotFound)?;
let pair = get_pair_from_phrase(pair_str)?;
Ok(pair)
}
pub fn parse_account_keyring(src: &str) -> Result<AccountKeyring, Error> {
AccountKeyring::from_str(src).map_err(|_| Error::KeyringAccountParsingError)
}
pub fn parse_duration_ms(src: &str) -> Result<Duration, ParseIntError> {
Ok(Duration::from_millis(src.parse::<u64>()?))
}
pub fn parse_duration_minutes(src: &str) -> Result<Duration, ParseIntError> {
Ok(Duration::from_secs(src.parse::<u64>()? * 60))
}
#[derive(Parser, Debug, Clone)]
pub struct ConnectionOpts {
#[cfg_attr(
feature = "parachain-metadata-kintsugi",
clap(long, default_value = "wss://api-kusama.interlay.io:443/parachain")
)]
#[cfg_attr(
feature = "parachain-metadata-interlay",
clap(long, default_value = "wss://api.interlay.io:443/parachain")
)]
pub btc_parachain_url: String,
#[clap(long, value_parser = parse_duration_ms, default_value = "60000")]
pub btc_parachain_connection_timeout_ms: Duration,
#[clap(long)]
pub max_concurrent_requests: Option<usize>,
#[clap(long)]
pub max_notifs_per_subscription: Option<usize>,
}
impl ConnectionOpts {
pub async fn try_connect(
&self,
signer: InterBtcSigner,
shutdown_tx: ShutdownSender,
) -> Result<InterBtcParachain, Error> {
InterBtcParachain::from_url_and_config_with_retry(
&self.btc_parachain_url,
signer,
self.max_concurrent_requests,
self.max_notifs_per_subscription,
self.btc_parachain_connection_timeout_ms,
shutdown_tx,
)
.await
}
}