Skip to content

Commit

Permalink
feat: initial ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Sep 24, 2024
1 parent d56fa09 commit 832623a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ version = "21.4.0"
version = "21.2.0"
default-features = true

[workspace.dependencies.stellar-ledger]
version = "=21.5.0"
path = "cmd/crates/stellar-ledger"

[workspace.dependencies]
stellar-strkey = "0.0.8"
sep5 = "0.0.4"
Expand Down
8 changes: 6 additions & 2 deletions cmd/crates/stellar-ledger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use hd_path::HdPath;
use ledger_transport::{APDUCommand, Exchange};
use ledger_transport::APDUCommand;
pub use ledger_transport::Exchange;

use ledger_transport_hid::{
hidapi::{HidApi, HidError},
LedgerHIDError, TransportNativeHID,
LedgerHIDError,
};

pub use ledger_transport_hid::TransportNativeHID;

use soroban_env_host::xdr::{Hash, Transaction};
use std::vec;
use stellar_strkey::DecodeError;
Expand Down
4 changes: 3 additions & 1 deletion cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ soroban-ledger-snapshot = { workspace = true }
stellar-strkey = { workspace = true }
soroban-sdk = { workspace = true }
soroban-rpc = { workspace = true }
stellar-ledger = { workspace = true }

clap = { workspace = true, features = [
"derive",
"env",
Expand Down Expand Up @@ -113,7 +115,7 @@ async-compression = { version = "0.4.12", features = [ "tokio", "gzip" ] }
tempfile = "3.8.1"
toml_edit = "0.21.0"
rust-embed = { version = "8.2.0", features = ["debug-embed"] }
bollard = { workspace=true }
bollard = { workspace = true }
futures-util = "0.3.30"
futures = "0.3.30"
home = "0.5.9"
Expand Down
15 changes: 15 additions & 0 deletions cmd/soroban-cli/src/config/secret.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use clap::arg;
use serde::{Deserialize, Serialize};
use std::{io::Write, str::FromStr};

use stellar_strkey::ed25519::{PrivateKey, PublicKey};

use crate::print::Print;
use crate::signer::native_ledger;
use crate::{
signer::{self, LocalKey, Signer, SignerKind},
utils,
Expand All @@ -27,6 +29,8 @@ pub enum Error {
InvalidAddress(String),
#[error(transparent)]
Signer(#[from] signer::Error),
#[error("Ledger does not reveal secret key")]
LedgerDoesNotRevealSecretKey,
}

#[derive(Debug, clap::Args, Clone)]
Expand Down Expand Up @@ -78,6 +82,7 @@ impl Args {
pub enum Secret {
SecretKey { secret_key: String },
SeedPhrase { seed_phrase: String },
Ledger,
}

impl FromStr for Secret {
Expand All @@ -92,6 +97,8 @@ impl FromStr for Secret {
Ok(Secret::SeedPhrase {
seed_phrase: s.to_string(),
})
} else if s == "ledger" {
Ok(Secret::Ledger)
} else {
Err(Error::InvalidAddress(s.to_string()))
}
Expand All @@ -116,6 +123,7 @@ impl Secret {
.private()
.0,
)?,
Secret::Ledger => panic!("Ledger does not reveal secret key"),
})
}

Expand All @@ -131,6 +139,13 @@ impl Secret {
Secret::SecretKey { .. } | Secret::SeedPhrase { .. } => {
SignerKind::Local(LocalKey::new(self.key_pair(index)?, prompt))
}
Secret::Ledger => {
let hd_path: u32 = index
.unwrap_or_default()
.try_into()
.expect("uszie bigger than u32");
SignerKind::Ledger(native_ledger(hd_path)?)
}
};
Ok(Signer {
kind,
Expand Down
18 changes: 18 additions & 0 deletions cmd/soroban-cli/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use soroban_env_host::xdr::{
SorobanAuthorizedFunction, SorobanCredentials, Transaction, TransactionEnvelope,
TransactionV1Envelope, Uint256, VecM, WriteXdr,
};
use stellar_ledger::{Exchange, LedgerSigner, TransportNativeHID};

use crate::{config::network::Network, print::Print, utils::transaction_hash};

Expand All @@ -24,6 +25,8 @@ pub enum Error {
#[error("User cancelled signing, perhaps need to add -y")]
UserCancelledSigning,
#[error(transparent)]
Ledger(#[from] stellar_ledger::Error),
#[error(transparent)]
Xdr(#[from] xdr::Error),
#[error("Only Transaction envelope V1 type is supported")]
UnsupportedTransactionEnvelopeType,
Expand Down Expand Up @@ -200,6 +203,20 @@ pub struct Signer {
#[allow(clippy::module_name_repetitions)]
pub enum SignerKind {
Local(LocalKey),
Ledger(Ledger<TransportNativeHID>),
}

pub struct Ledger<T: Exchange> {
index: u32,
signer: LedgerSigner<T>,
}

pub fn native_ledger(hd_path: u32) -> Result<Ledger<TransportNativeHID>, Error> {
let signer = stellar_ledger::native()?;
Ok(Ledger {
index: hd_path,
signer,
})
}

impl Signer {
Expand Down Expand Up @@ -229,6 +246,7 @@ impl Signer {
));
let decorated_signature = match &self.kind {
SignerKind::Local(key) => key.sign_tx_hash(tx_hash)?,
SignerKind::Ledger(ledger) => todo!(),
};
let mut sigs = signatures.into_vec();
sigs.push(decorated_signature);
Expand Down

0 comments on commit 832623a

Please sign in to comment.