soroban_cli/config/
mod.rs1use clap::{arg, command};
2use serde::{Deserialize, Serialize};
3use std::{
4 fs::{self, File},
5 io::Write,
6};
7
8use crate::{
9 signer,
10 xdr::{self, SequenceNumber, Transaction, TransactionEnvelope, TransactionV1Envelope, VecM},
11 Pwd,
12};
13use network::Network;
14
15pub mod address;
16pub mod alias;
17pub mod data;
18pub mod key;
19pub mod locator;
20pub mod network;
21pub mod sc_address;
22pub mod secret;
23pub mod sign_with;
24pub mod upgrade_check;
25
26use crate::config::locator::cli_config_file;
27pub use address::UnresolvedMuxedAccount;
28pub use alias::UnresolvedContract;
29pub use sc_address::UnresolvedScAddress;
30
31#[derive(thiserror::Error, Debug)]
32pub enum Error {
33 #[error(transparent)]
34 Network(#[from] network::Error),
35 #[error(transparent)]
36 Secret(#[from] secret::Error),
37 #[error(transparent)]
38 Config(#[from] locator::Error),
39 #[error(transparent)]
40 Rpc(#[from] soroban_rpc::Error),
41 #[error(transparent)]
42 Signer(#[from] signer::Error),
43 #[error(transparent)]
44 SignWith(#[from] sign_with::Error),
45 #[error(transparent)]
46 StellarStrkey(#[from] stellar_strkey::DecodeError),
47 #[error(transparent)]
48 Address(#[from] address::Error),
49}
50
51#[derive(Debug, clap::Args, Clone, Default)]
52#[group(skip)]
53pub struct Args {
54 #[command(flatten)]
55 pub network: network::Args,
56
57 #[arg(long, short = 's', visible_alias = "source", env = "STELLAR_ACCOUNT")]
58 pub source_account: UnresolvedMuxedAccount,
65
66 #[command(flatten)]
67 pub locator: locator::Args,
68
69 #[command(flatten)]
70 pub sign_with: sign_with::Args,
71}
72
73impl Args {
74 pub async fn source_account(&self) -> Result<xdr::MuxedAccount, Error> {
76 Ok(self
77 .source_account
78 .resolve_muxed_account(&self.locator, self.hd_path())
79 .await?)
80 }
81
82 pub fn key_pair(&self) -> Result<ed25519_dalek::SigningKey, Error> {
83 let key = &self.source_account.resolve_secret(&self.locator)?;
84 Ok(key.key_pair(self.hd_path())?)
85 }
86
87 pub async fn sign(&self, tx: Transaction) -> Result<TransactionEnvelope, Error> {
88 let tx_env = TransactionEnvelope::Tx(TransactionV1Envelope {
89 tx,
90 signatures: VecM::default(),
91 });
92 Ok(self
93 .sign_with
94 .sign_tx_env(
95 &tx_env,
96 &self.locator,
97 &self.network.get(&self.locator)?,
98 false,
99 Some(&self.source_account),
100 )
101 .await?)
102 }
103
104 pub async fn sign_soroban_authorizations(
105 &self,
106 tx: &Transaction,
107 signers: &[ed25519_dalek::SigningKey],
108 ) -> Result<Option<Transaction>, Error> {
109 let network = self.get_network()?;
110 let source_key = self.key_pair()?;
111 let client = network.rpc_client()?;
112 let latest_ledger = client.get_latest_ledger().await?.sequence;
113 let seq_num = latest_ledger + 60; Ok(signer::sign_soroban_authorizations(
115 tx,
116 &source_key,
117 signers,
118 seq_num,
119 &network.network_passphrase,
120 )?)
121 }
122
123 pub fn get_network(&self) -> Result<Network, Error> {
124 Ok(self.network.get(&self.locator)?)
125 }
126
127 pub async fn next_sequence_number(
128 &self,
129 account: impl Into<xdr::AccountId>,
130 ) -> Result<SequenceNumber, Error> {
131 let network = self.get_network()?;
132 let client = network.rpc_client()?;
133 Ok((client
134 .get_account(&account.into().to_string())
135 .await?
136 .seq_num
137 .0
138 + 1)
139 .into())
140 }
141
142 pub fn hd_path(&self) -> Option<usize> {
143 self.sign_with.hd_path
144 }
145}
146
147impl Pwd for Args {
148 fn set_pwd(&mut self, pwd: &std::path::Path) {
149 self.locator.set_pwd(pwd);
150 }
151}
152
153#[derive(Debug, clap::Args, Clone, Default)]
154#[group(skip)]
155pub struct ArgsLocatorAndNetwork {
156 #[command(flatten)]
157 pub network: network::Args,
158
159 #[command(flatten)]
160 pub locator: locator::Args,
161}
162
163impl ArgsLocatorAndNetwork {
164 pub fn get_network(&self) -> Result<Network, Error> {
165 Ok(self.network.get(&self.locator)?)
166 }
167}
168
169#[derive(Serialize, Deserialize, Debug, Default)]
170pub struct Config {
171 pub defaults: Defaults,
172}
173
174#[derive(Serialize, Deserialize, Debug, Default)]
175pub struct Defaults {
176 pub network: Option<String>,
177 pub identity: Option<String>,
178}
179
180impl Config {
181 pub fn new() -> Result<Config, locator::Error> {
182 let path = cli_config_file()?;
183
184 if path.exists() {
185 let data = fs::read_to_string(&path).map_err(|_| locator::Error::FileRead { path })?;
186 Ok(toml::from_str(&data)?)
187 } else {
188 Ok(Config::default())
189 }
190 }
191
192 #[must_use]
193 pub fn set_network(mut self, s: &str) -> Self {
194 self.defaults.network = Some(s.to_string());
195 self
196 }
197
198 #[must_use]
199 pub fn set_identity(mut self, s: &str) -> Self {
200 self.defaults.identity = Some(s.to_string());
201 self
202 }
203
204 pub fn save(&self) -> Result<(), locator::Error> {
205 let toml_string = toml::to_string(&self)?;
206 let path = cli_config_file()?;
207 let mut file = File::create(locator::ensure_directory(path)?)?;
209 file.write_all(toml_string.as_bytes())?;
210
211 Ok(())
212 }
213}