Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed May 16, 2024
1 parent 91e83d1 commit 69069a0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 50 deletions.
61 changes: 22 additions & 39 deletions cmd/crates/stellar-ledger/src/emulator_tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::signer::Stellar;
use ledger_transport::Exchange;
use serde::Deserialize;
use soroban_env_host::xdr::Transaction;
use soroban_env_host::xdr::{self, Operation, OperationBody, Uint256};
use std::vec;

use crate::emulator_http_transport::EmulatorHttpTransport;
use crate::hd_path::HdPath;
use crate::speculos::Speculos;
use crate::{LedgerError, LedgerOptions, LedgerSigner};
use crate::{native, test_network_hash, Blob, Error, LedgerSigner};

use std::sync::Arc;
use std::{collections::HashMap, str::FromStr, time::Duration};
Expand Down Expand Up @@ -45,13 +45,10 @@ async fn test_get_public_key() {
wait_for_emulator_start_text(ui_host_port).await;

let transport = get_zemu_transport("127.0.0.1", host_port).unwrap();
let ledger_options = Some(LedgerOptions {
exchange: transport,
hd_path: slip10::BIP32Path::from_str("m/44'/148'/0'").unwrap(),
});
let ledger = LedgerSigner::new(TEST_NETWORK_PASSPHRASE, ledger_options);

match ledger.get_public_key(0).await {
let ledger = native().unwrap();

match ledger.get_public_key(&0.into()).await {
Ok(public_key) => {
let public_key_string = public_key.to_string();
// This is determined by the seed phrase used to start up the emulator
Expand Down Expand Up @@ -79,11 +76,7 @@ async fn test_get_app_configuration() {
wait_for_emulator_start_text(ui_host_port).await;

let transport = get_zemu_transport("127.0.0.1", host_port).unwrap();
let ledger_options = Some(LedgerOptions {
exchange: transport,
hd_path: slip10::BIP32Path::from_str("m/44'/148'/0'").unwrap(),
});
let ledger = LedgerSigner::new(TEST_NETWORK_PASSPHRASE, ledger_options);
let ledger = native().unwrap();

match ledger.get_app_configuration().await {
Ok(config) => {
Expand All @@ -109,13 +102,9 @@ async fn test_sign_tx() {
wait_for_emulator_start_text(ui_host_port).await;

let transport = get_zemu_transport("127.0.0.1", host_port).unwrap();
let ledger_options = Some(LedgerOptions {
exchange: transport,
hd_path: slip10::BIP32Path::from_str("m/44'/148'/0'").unwrap(),
});
let ledger = Arc::new(LedgerSigner::new(TEST_NETWORK_PASSPHRASE, ledger_options));
let ledger = Arc::new(native().unwrap());

let path = slip10::BIP32Path::from_str("m/44'/148'/0'").unwrap();
let path = HdPath(0);

let source_account_str = "GAQNVGMLOXSCWH37QXIHLQJH6WZENXYSVWLPAEF4673W64VRNZLRHMFM";
let source_account_bytes = match stellar_strkey::Strkey::from_string(source_account_str) {
Expand Down Expand Up @@ -169,7 +158,7 @@ async fn test_sign_tx() {

let sign = tokio::task::spawn({
let ledger = Arc::clone(&ledger);
async move { ledger.sign_transaction(path, tx).await }
async move { ledger.sign_transaction(path, tx, test_network_hash()).await }
});
let approve = tokio::task::spawn(approve_tx_signature(ui_host_port));

Expand Down Expand Up @@ -204,17 +193,14 @@ async fn test_sign_tx_hash_when_hash_signing_is_not_enabled() {
// for some things, waiting for the screen to change... but prob dont need that for this

let transport = get_zemu_transport("127.0.0.1", host_port).unwrap();
let ledger_options = Some(LedgerOptions {
exchange: transport,
hd_path: slip10::BIP32Path::from_str("m/44'/148'/0'").unwrap(),
});
let ledger = LedgerSigner::new(TEST_NETWORK_PASSPHRASE, ledger_options);

let ledger = native().unwrap();

let path = slip10::BIP32Path::from_str("m/44'/148'/0'").unwrap();
let test_hash = "3389e9f0f1a65f19736cacf544c2e825313e8447f569233bb8db39aa607c8889".as_bytes();
let path = 0;
let test_hash = b"313e8447f569233bb8db39aa607c8889";

let result = ledger.sign_transaction_hash(path, test_hash.into()).await;
if let Err(LedgerError::APDUExchangeError(msg)) = result {
let result = ledger.sign_transaction_hash(path, test_hash).await;
if let Err(Error::APDUExchangeError(msg)) = result {
assert_eq!(msg, "Ledger APDU retcode: 0x6C66");
// this error code is SW_TX_HASH_SIGNING_MODE_NOT_ENABLED https://github.com/LedgerHQ/app-stellar/blob/develop/docs/COMMANDS.md
} else {
Expand All @@ -237,17 +223,14 @@ async fn test_sign_tx_hash_when_hash_signing_is_enabled() {
enable_hash_signing(ui_host_port).await;

let transport = get_zemu_transport("127.0.0.1", host_port).unwrap();
let ledger_options = Some(LedgerOptions {
exchange: transport,
hd_path: slip10::BIP32Path::from_str("m/44'/148'/0'").unwrap(),
});
let ledger = Arc::new(LedgerSigner::new(TEST_NETWORK_PASSPHRASE, ledger_options));

let ledger = Arc::new(native().unwrap());

let path = slip10::BIP32Path::from_str("m/44'/148'/0'").unwrap();
let mut test_hash = vec![0u8; 32];
let path = 0;
let mut test_hash = [0u8; 32];

match hex::decode_to_slice(
"3389e9f0f1a65f19736cacf544c2e825313e8447f569233bb8db39aa607c8889",
"313e8447f569233bb8db39aa607c8889",
&mut test_hash as &mut [u8],
) {
Ok(()) => {}
Expand All @@ -259,7 +242,7 @@ async fn test_sign_tx_hash_when_hash_signing_is_enabled() {

let sign = tokio::task::spawn({
let ledger = Arc::clone(&ledger);
async move { ledger.sign_transaction_hash(path, test_hash).await }
async move { ledger.sign_transaction_hash(path, &test_hash).await }
});
let approve = tokio::task::spawn(approve_tx_hash_signature(ui_host_port));

Expand Down Expand Up @@ -343,7 +326,7 @@ struct EventsResponse {
events: Vec<EmulatorEvent>,
}

fn get_zemu_transport(host: &str, port: u16) -> Result<impl Exchange, LedgerError> {
fn get_zemu_transport(host: &str, port: u16) -> Result<impl Exchange, Error> {
Ok(EmulatorHttpTransport::new(host, port))
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/crates/stellar-ledger/src/hd_path.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Error;

#[derive(Clone, Copy)]
pub struct HdPath(u32);
pub struct HdPath(pub(crate) u32);

impl HdPath {
pub fn depth(&self) -> u8 {
Expand All @@ -16,6 +16,12 @@ impl From<u32> for HdPath {
}
}

impl From<&u32> for HdPath {
fn from(index: &u32) -> Self {
HdPath(*index)
}
}

impl HdPath {
pub fn to_vec(&self) -> Result<Vec<u8>, Error> {
hd_path_to_bytes(&self.into())
Expand Down
26 changes: 16 additions & 10 deletions cmd/crates/stellar-ledger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ pub struct LedgerSigner<T: Exchange> {
unsafe impl<T> Send for LedgerSigner<T> where T: Exchange {}
unsafe impl<T> Sync for LedgerSigner<T> where T: Exchange {}

pub fn native() -> Result<LedgerSigner<TransportNativeHID>, Error> {
Ok(LedgerSigner {
transport: get_transport()?,
})
}

impl<T> LedgerSigner<T>
where
T: Exchange,
Expand Down Expand Up @@ -291,11 +297,17 @@ fn get_transport() -> Result<TransportNativeHID, Error> {
let hidapi = HidApi::new().map_err(Error::HidApiError)?;
TransportNativeHID::new(&hidapi).map_err(Error::LedgerHidError)
}

pub const TEST_NETWORK_PASSPHRASE: &[u8] = b"Test SDF Network ; September 2015";

fn test_network_hash() -> Hash {
use sha2::Digest;
Hash(sha2::Sha256::digest(TEST_NETWORK_PASSPHRASE).into())
}
#[cfg(test)]
mod test {
use httpmock::prelude::*;
use serde_json::json;
use sha2::{Digest, Sha256};

use crate::{emulator_http_transport::EmulatorHttpTransport, Blob};

Expand All @@ -304,18 +316,12 @@ mod test {

use soroban_env_host::xdr::{self, Operation, OperationBody, Uint256};

use crate::{Error, LedgerSigner};
use crate::{Error, LedgerSigner, test_network_hash};

use stellar_xdr::curr::{
Hash, Memo, MuxedAccount, PaymentOp, Preconditions, SequenceNumber, TransactionExt,
Memo, MuxedAccount, PaymentOp, Preconditions, SequenceNumber, TransactionExt,
};

const TEST_NETWORK_PASSPHRASE: &[u8] = b"Test SDF Network ; September 2015";

fn network_hash() -> Hash {
Hash(Sha256::digest(TEST_NETWORK_PASSPHRASE).into())
}

fn ledger(server: &MockServer) -> LedgerSigner<EmulatorHttpTransport> {
let transport = EmulatorHttpTransport::new(&server.host(), server.port());
LedgerSigner::new(transport)
Expand Down Expand Up @@ -412,7 +418,7 @@ mod test {
};

let response = ledger
.sign_transaction(0, tx, network_hash())
.sign_transaction(0, tx, test_network_hash())
.await
.unwrap();
assert_eq!(
Expand Down

0 comments on commit 69069a0

Please sign in to comment.