Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Apr 25, 2024
1 parent 5bc41f2 commit b5b20f7
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 65 deletions.
1 change: 0 additions & 1 deletion cmd/crates/stellar-ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ home = "0.5.9"
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11", features = ["json"]}
soroban-rpc.workspace = true
async-trait.workspace = true
testcontainers = { git = "https://github.com/testcontainers/testcontainers-rs.git", rev = "4b3e4f08a2c0bdf521636b03f959f004e6d216aa" }
phf = { version = "0.11.2", features = ["macros"] }
futures = "0.3.30"
Expand Down
2 changes: 0 additions & 2 deletions cmd/crates/stellar-ledger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod transport_zemu_http;
use async_trait::async_trait;
use futures::executor::block_on;
use ledger_transport::{APDUCommand, Exchange};
use ledger_transport_hid::{
Expand Down Expand Up @@ -272,7 +271,6 @@ where
}
}

#[async_trait]
impl<T: Exchange> Stellar for LedgerSigner<T> {
type Init = LedgerOptions<T>;

Expand Down
63 changes: 1 addition & 62 deletions cmd/crates/stellar-ledger/src/signer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use ed25519_dalek::Signer;
use ledger_transport::async_trait;
use sha2::{Digest, Sha256};
use soroban_env_host::xdr::{
self, DecoratedSignature, Limits, Signature, SignatureHint, Transaction, TransactionEnvelope,
self, DecoratedSignature, Limits, Transaction, TransactionEnvelope,
TransactionSignaturePayload, TransactionSignaturePayloadTaggedTransaction,
TransactionV1Envelope, WriteXdr,
};
Expand All @@ -16,7 +14,6 @@ pub enum Error {
}

/// A trait for signing Stellar transactions and Soroban authorization entries
#[async_trait]
pub trait Stellar {
/// The type of the options that can be passed when creating a new signer
type Init;
Expand Down Expand Up @@ -56,61 +53,3 @@ pub trait Stellar {
}))
}
}
struct DefaultSigner {
network_passphrase: String,
keypairs: Vec<ed25519_dalek::SigningKey>,
}

impl DefaultSigner {
pub fn get_key(
&self,
key: &stellar_strkey::Strkey,
) -> Result<&ed25519_dalek::SigningKey, Error> {
match key {
stellar_strkey::Strkey::PublicKeyEd25519(stellar_strkey::ed25519::PublicKey(bytes)) => {
self.keypairs
.iter()
.find(|k| k.verifying_key().to_bytes() == *bytes)
}
_ => None,
}
.ok_or_else(|| {
Error::RpcError(RpcError::MissingSignerForAddress {
address: key.to_string(),
})
})
}
}

#[async_trait]
impl Stellar for DefaultSigner {
type Init = Vec<ed25519_dalek::SigningKey>;
fn new(network_passphrase: &str, options: Option<Vec<ed25519_dalek::SigningKey>>) -> Self {
DefaultSigner {
network_passphrase: network_passphrase.to_string(),
keypairs: options.unwrap_or_default(),
}
}

fn sign_txn_hash(
&self,
txn: [u8; 32],
source_account: &stellar_strkey::Strkey,
) -> Result<DecoratedSignature, Error> {
let source_account = self.get_key(source_account)?;
let tx_signature = source_account.sign(&txn);
Ok(DecoratedSignature {
// TODO: remove this unwrap. It's safe because we know the length of the array
hint: SignatureHint(
source_account.verifying_key().to_bytes()[28..]
.try_into()
.unwrap(),
),
signature: Signature(tx_signature.to_bytes().try_into().unwrap()), //FIXME: remove unwrap
})
}

fn network_hash(&self) -> xdr::Hash {
xdr::Hash(Sha256::digest(self.network_passphrase.as_bytes()).into())
}
}

0 comments on commit b5b20f7

Please sign in to comment.