Skip to content

Commit 4d47962

Browse files
committed
Sign Soroban auth entries from Ledger identities.
1 parent fb1cd67 commit 4d47962

9 files changed

Lines changed: 144 additions & 86 deletions

File tree

cmd/soroban-cli/src/commands/contract/arg_parsing.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,25 @@ fn running_cmd() -> String {
9393
format!("{} --", args.join(" "))
9494
}
9595

96-
pub async fn build_host_function_parameters(
96+
pub fn build_host_function_parameters(
9797
contract_id: &stellar_strkey::Contract,
9898
slop: &[OsString],
9999
spec_entries: &[ScSpecEntry],
100100
config: &config::Args,
101101
) -> Result<HostFunctionParameters, Error> {
102-
build_host_function_parameters_with_filter(contract_id, slop, spec_entries, config, true).await
102+
build_host_function_parameters_with_filter(contract_id, slop, spec_entries, config, true)
103103
}
104104

105-
pub async fn build_constructor_parameters(
105+
pub fn build_constructor_parameters(
106106
contract_id: &stellar_strkey::Contract,
107107
slop: &[OsString],
108108
spec_entries: &[ScSpecEntry],
109109
config: &config::Args,
110110
) -> Result<HostFunctionParameters, Error> {
111-
build_host_function_parameters_with_filter(contract_id, slop, spec_entries, config, false).await
111+
build_host_function_parameters_with_filter(contract_id, slop, spec_entries, config, false)
112112
}
113113

114-
async fn build_host_function_parameters_with_filter(
114+
fn build_host_function_parameters_with_filter(
115115
contract_id: &stellar_strkey::Contract,
116116
slop: &[OsString],
117117
spec_entries: &[ScSpecEntry],
@@ -122,7 +122,7 @@ async fn build_host_function_parameters_with_filter(
122122
let cmd = build_clap_command(&spec, filter_constructor)?;
123123
let (function, matches_) = parse_command_matches(cmd, slop)?;
124124
let func = get_function_spec(&spec, &function)?;
125-
let (parsed_args, signers) = parse_function_arguments(&func, &matches_, &spec, config).await?;
125+
let (parsed_args, signers) = parse_function_arguments(&func, &matches_, &spec, config)?;
126126
let invoke_args = build_invoke_contract_args(contract_id, &function, parsed_args)?;
127127

128128
Ok((function, spec, invoke_args, signers))
@@ -187,7 +187,7 @@ fn get_function_spec(spec: &Spec, function: &str) -> Result<ScSpecFunctionV0, Er
187187
})
188188
}
189189

190-
async fn parse_function_arguments(
190+
fn parse_function_arguments(
191191
func: &ScSpecFunctionV0,
192192
matches_: &clap::ArgMatches,
193193
spec: &Spec,
@@ -197,13 +197,13 @@ async fn parse_function_arguments(
197197
let mut signers = Vec::<Signer>::new();
198198

199199
for i in func.inputs.iter() {
200-
parse_single_argument(i, matches_, spec, config, &mut signers, &mut parsed_args).await?;
200+
parse_single_argument(i, matches_, spec, config, &mut signers, &mut parsed_args)?;
201201
}
202202

203203
Ok((parsed_args, signers))
204204
}
205205

206-
async fn parse_single_argument(
206+
fn parse_single_argument(
207207
input: &stellar_xdr::curr::ScSpecFunctionInputV0,
208208
matches_: &clap::ArgMatches,
209209
spec: &Spec,
@@ -234,7 +234,7 @@ async fn parse_single_argument(
234234
ScSpecTypeDef::Address | ScSpecTypeDef::MuxedAddress
235235
) {
236236
let trimmed_s = s.trim_matches('"');
237-
if let Some(signer) = resolve_signer(trimmed_s, config).await {
237+
if let Some(signer) = resolve_signer(trimmed_s, config) {
238238
signers.push(signer);
239239
}
240240
}
@@ -464,10 +464,10 @@ fn resolve_address(addr_or_alias: &str, config: &config::Args) -> Result<String,
464464
Ok(account)
465465
}
466466

467-
async fn resolve_signer(addr_or_alias: &str, config: &config::Args) -> Option<Signer> {
467+
fn resolve_signer(addr_or_alias: &str, config: &config::Args) -> Option<Signer> {
468468
let secret = config.locator.get_secret_key(addr_or_alias).ok()?;
469469
let print = Print::new(false);
470-
let signer = secret.signer(config.hd_path(), print).await.ok()?;
470+
let signer = secret.signer(config.hd_path(), print).ok()?;
471471
Some(signer)
472472
}
473473

cmd/soroban-cli/src/commands/contract/deploy/wasm.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ impl Cmd {
385385
&slop,
386386
&entries,
387387
config,
388-
)
389-
.await?
388+
)?
390389
.2,
391390
)
392391
}

cmd/soroban-cli/src/commands/contract/invoke.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl Cmd {
270270

271271
if let Some(spec_entries) = &spec_entries {
272272
// For testing wasm arg parsing
273-
build_host_function_parameters(&contract_id, &self.slop, spec_entries, config).await?;
273+
build_host_function_parameters(&contract_id, &self.slop, spec_entries, config)?;
274274
}
275275

276276
let client = network.rpc_client()?;
@@ -295,7 +295,7 @@ impl Cmd {
295295
.map_err(Error::from)?;
296296

297297
let params =
298-
build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config).await?;
298+
build_host_function_parameters(&contract_id, &self.slop, &spec_entries, config)?;
299299

300300
let (function, spec, host_function_params, signers) = params;
301301

cmd/soroban-cli/src/commands/message/sign.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ impl Cmd {
8585
let secret = self
8686
.locator
8787
.get_secret_key_with_hd_path(key_or_name, self.hd_path)?;
88-
let signer = secret.signer(self.hd_path, print.clone()).await?;
88+
let signer = secret.signer(self.hd_path, print.clone())?;
8989
let public_key = signer.get_public_key()?;
9090

9191
// Encode signature as base64
92-
let signature_base64 = sep_53_sign(&message_bytes, signer)?;
92+
let signature_base64 = sep_53_sign(&message_bytes, signer).await?;
9393

9494
print.infoln(format!("Signer: {public_key}"));
9595
println!("{signature_base64}");
@@ -126,14 +126,14 @@ impl Cmd {
126126
/// Sign the given message bytes with the provided signer, returning the base64-encoded signature.
127127
///
128128
/// Expects the message bytes to be the raw message (without SEP-53 prefix).
129-
fn sep_53_sign(message_bytes: &[u8], signer: Signer) -> Result<String, Error> {
129+
async fn sep_53_sign(message_bytes: &[u8], signer: Signer) -> Result<String, Error> {
130130
// Create SEP-53 payload
131131
let mut payload = Vec::with_capacity(SEP53_PREFIX.len() + message_bytes.len());
132132
payload.extend_from_slice(SEP53_PREFIX.as_bytes());
133133
payload.extend_from_slice(message_bytes);
134134
let hash: [u8; 32] = Sha256::digest(&payload).into();
135135

136-
let signature = signer.sign_payload(hash)?;
136+
let signature = signer.sign_payload(hash).await?;
137137

138138
Ok(BASE64.encode(signature.to_bytes()))
139139
}
@@ -177,8 +177,8 @@ mod tests {
177177
}
178178
}
179179

180-
#[test]
181-
fn test_sign_simple() {
180+
#[tokio::test]
181+
async fn test_sign_simple() {
182182
// SEP-53 - test case 1
183183
let message = "Hello, World!".to_string();
184184
let expected_signature = "fO5dbYhXUhBMhe6kId/cuVq/AfEnHRHEvsP8vXh03M1uLpi5e46yO2Q8rEBzu3feXQewcQE5GArp88u6ePK6BA==";
@@ -194,13 +194,13 @@ mod tests {
194194
let signer = build_signer_for_test_key();
195195

196196
let message_bytes = cmd.get_message_bytes().unwrap();
197-
let signature_base64 = sep_53_sign(&message_bytes, signer).unwrap();
197+
let signature_base64 = sep_53_sign(&message_bytes, signer).await.unwrap();
198198

199199
assert_eq!(signature_base64, expected_signature);
200200
}
201201

202-
#[test]
203-
fn test_sign_japanese() {
202+
#[tokio::test]
203+
async fn test_sign_japanese() {
204204
// SEP-53 - test case 2
205205
let message = "こんにちは、世界!".to_string();
206206
let expected_signature = "CDU265Xs8y3OWbB/56H9jPgUss5G9A0qFuTqH2zs2YDgTm+++dIfmAEceFqB7bhfN3am59lCtDXrCtwH2k1GBA==";
@@ -216,13 +216,13 @@ mod tests {
216216
let signer = build_signer_for_test_key();
217217

218218
let message_bytes = cmd.get_message_bytes().unwrap();
219-
let signature_base64 = sep_53_sign(&message_bytes, signer).unwrap();
219+
let signature_base64 = sep_53_sign(&message_bytes, signer).await.unwrap();
220220

221221
assert_eq!(signature_base64, expected_signature);
222222
}
223223

224-
#[test]
225-
fn test_sign_base64() {
224+
#[tokio::test]
225+
async fn test_sign_base64() {
226226
// SEP-53 - test case 3
227227
let message = "2zZDP1sa1BVBfLP7TeeMk3sUbaxAkUhBhDiNdrksaFo=".to_string();
228228
let expected_signature = "VA1+7hefNwv2NKScH6n+Sljj15kLAge+M2wE7fzFOf+L0MMbssA1mwfJZRyyrhBORQRle10X1Dxpx+UOI4EbDQ==";
@@ -238,7 +238,7 @@ mod tests {
238238
let signer = build_signer_for_test_key();
239239

240240
let message_bytes = cmd.get_message_bytes().unwrap();
241-
let signature_base64 = sep_53_sign(&message_bytes, signer).unwrap();
241+
let signature_base64 = sep_53_sign(&message_bytes, signer).await.unwrap();
242242

243243
assert_eq!(signature_base64, expected_signature);
244244
}

cmd/soroban-cli/src/config/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,10 @@ impl Args {
154154
let client = network.rpc_client()?;
155155
let latest_ledger = client.get_latest_ledger().await?.sequence;
156156
let seq_num = latest_ledger + 60; // ~ 5 min
157-
Ok(signer::sign_soroban_authorizations(
158-
tx,
159-
signers,
160-
seq_num,
161-
&network.network_passphrase,
162-
)?)
157+
Ok(
158+
signer::sign_soroban_authorizations(tx, signers, seq_num, &network.network_passphrase)
159+
.await?,
160+
)
163161
}
164162

165163
pub fn get_network(&self) -> Result<Network, Error> {

cmd/soroban-cli/src/config/secret.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use stellar_strkey::ed25519::{PrivateKey, PublicKey};
66

77
use crate::{
88
print::Print,
9-
signer::{self, ledger, secure_store, LocalKey, SecureStoreEntry, Signer, SignerKind},
9+
signer::{
10+
self, ledger::LedgerEntry, secure_store, LocalKey, SecureStoreEntry, Signer, SignerKind,
11+
},
1012
utils,
1113
};
1214

@@ -207,22 +209,25 @@ impl Secret {
207209
}
208210
}
209211

210-
pub async fn signer(&self, hd_path: Option<usize>, print: Print) -> Result<Signer, Error> {
212+
pub fn signer(&self, hd_path: Option<usize>, print: Print) -> Result<Signer, Error> {
211213
let kind = match self {
212214
Secret::SecretKey { .. } | Secret::SeedPhrase { .. } => {
213215
let key = self.key_pair(hd_path)?;
214216
SignerKind::Local(LocalKey { key })
215217
}
216218
Secret::Ledger {
217219
hardware: HardwareKind::Ledger,
220+
public_key,
218221
hd_path: cached_hd_path,
219-
..
220222
} => {
221223
let effective = hd_path.or(*cached_hd_path).unwrap_or_default();
222224
let hd_path: u32 = effective
223225
.try_into()
224226
.map_err(|_| Error::HdPathOutOfRange(effective))?;
225-
SignerKind::Ledger(ledger::new(hd_path).await?)
227+
SignerKind::Ledger(LedgerEntry {
228+
hd_path,
229+
public_key: Some(PublicKey::from_string(public_key)?),
230+
})
226231
}
227232
Secret::SecureStore {
228233
entry_name,

cmd/soroban-cli/src/config/sign_with.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
config::UnresolvedMuxedAccount,
33
print::Print,
4-
signer::{self, ledger, Signer, SignerKind},
4+
signer::{self, ledger::LedgerEntry, Signer, SignerKind},
55
xdr::{self, TransactionEnvelope},
66
};
77

@@ -88,15 +88,16 @@ impl Args {
8888
print,
8989
}
9090
} else if self.sign_with_ledger {
91-
let ledger = ledger::new(
92-
self.hd_path
93-
.unwrap_or_default()
94-
.try_into()
95-
.unwrap_or_default(),
96-
)
97-
.await?;
91+
let hd_path = self
92+
.hd_path
93+
.unwrap_or_default()
94+
.try_into()
95+
.unwrap_or_default();
9896
Signer {
99-
kind: SignerKind::Ledger(ledger),
97+
kind: SignerKind::Ledger(LedgerEntry {
98+
hd_path,
99+
public_key: None,
100+
}),
100101
print,
101102
}
102103
} else {
@@ -110,7 +111,7 @@ impl Args {
110111
};
111112

112113
let secret = locator.get_secret_key_with_hd_path(key_or_name, self.hd_path)?;
113-
secret.signer(self.hd_path, print).await?
114+
secret.signer(self.hd_path, print)?
114115
};
115116
Ok(signer.sign_tx_env(tx, network).await?)
116117
}

0 commit comments

Comments
 (0)