From 4e5405847f4f7131f446022a803a63348976bf7f Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Fri, 3 May 2024 11:37:23 -0400 Subject: [PATCH] feat: sign blob --- cmd/crates/stellar-ledger/src/lib.rs | 22 +++++++++++++++++----- cmd/soroban-cli/src/commands/txn/sign.rs | 18 ++++++++++++------ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/cmd/crates/stellar-ledger/src/lib.rs b/cmd/crates/stellar-ledger/src/lib.rs index c281fa55a..5b1a72f74 100644 --- a/cmd/crates/stellar-ledger/src/lib.rs +++ b/cmd/crates/stellar-ledger/src/lib.rs @@ -152,23 +152,23 @@ where self.send_command_to_ledger(command).await } - /// Sign a Stellar transaction hash with the account on the Ledger device + /// Sign a Stellar transaction hash with the account on the Ledger device /// based on impl from [https://github.com/LedgerHQ/ledger-live/blob/develop/libs/ledgerjs/packages/hw-app-str/src/Str.ts#L166](https://github.com/LedgerHQ/ledger-live/blob/develop/libs/ledgerjs/packages/hw-app-str/src/Str.ts#L166) /// # Errors /// Returns an error if there is an issue with connecting with the device or signing the given tx on the device. Or, if the device has not enabled hash signing - async fn sign_transaction_hash( + pub async fn sign_blob( &self, hd_path: slip10::BIP32Path, - transaction_hash: &[u8], + blob: &[u8], ) -> Result, LedgerError> { let mut hd_path_to_bytes = hd_path_to_bytes(&hd_path); - let capacity = 1 + hd_path_to_bytes.len() + transaction_hash.len(); + let capacity = 1 + hd_path_to_bytes.len() + blob.len(); let mut data: Vec = Vec::with_capacity(capacity); data.insert(0, HD_PATH_ELEMENTS_COUNT); data.append(&mut hd_path_to_bytes); - data.extend_from_slice(transaction_hash); + data.extend_from_slice(blob); let command = APDUCommand { cla: CLA, @@ -181,6 +181,18 @@ where self.send_command_to_ledger(command).await } + /// Sign a Stellar transaction hash with the account on the Ledger device + /// based on impl from [https://github.com/LedgerHQ/ledger-live/blob/develop/libs/ledgerjs/packages/hw-app-str/src/Str.ts#L166](https://github.com/LedgerHQ/ledger-live/blob/develop/libs/ledgerjs/packages/hw-app-str/src/Str.ts#L166) + /// # Errors + /// Returns an error if there is an issue with connecting with the device or signing the given tx on the device. Or, if the device has not enabled hash signing + pub async fn sign_transaction_hash( + &self, + hd_path: slip10::BIP32Path, + transaction_hash: &[u8], + ) -> Result, LedgerError> { + self.sign_blob(hd_path, transaction_hash).await + } + /// Sign a Stellar transaction with the account on the Ledger device /// # Errors /// Returns an error if there is an issue with connecting with the device or signing the given tx on the device diff --git a/cmd/soroban-cli/src/commands/txn/sign.rs b/cmd/soroban-cli/src/commands/txn/sign.rs index c5d919a12..092cac5fe 100644 --- a/cmd/soroban-cli/src/commands/txn/sign.rs +++ b/cmd/soroban-cli/src/commands/txn/sign.rs @@ -5,8 +5,10 @@ use std::io; // execute, // terminal::{self, EnterAlternateScreen, LeaveAlternateScreen}, // }; -use soroban_sdk::xdr::{self, Limits, Transaction, TransactionEnvelope, WriteXdr}; -use stellar_ledger::NativeSigner; +use soroban_sdk::xdr::{ + self, Limits, MuxedAccount, Transaction, TransactionEnvelope, Uint256, WriteXdr, +}; +use stellar_ledger::{LedgerError, NativeSigner}; use stellar_strkey::Strkey; use crate::signer::{self, InMemory, Stellar}; @@ -27,6 +29,8 @@ pub enum Error { Io(#[from] io::Error), #[error("User cancelled signing, perhaps need to add -y")] UserCancelledSigning, + #[error(transparent)] + Ledger(#[from] LedgerError), } #[derive(Debug, clap::Parser, Clone)] @@ -115,16 +119,18 @@ impl Cmd { .await?) } - pub async fn sign_ledger(&self, txn: Transaction) -> Result { + pub async fn sign_ledger(&self, mut txn: Transaction) -> Result { let index: u32 = self .config .hd_path .unwrap_or_default() .try_into() .expect("usize bigger than u32"); - let signer: NativeSigner = (self.config.get_network()?.network_passphrase, index).into(); - let account = - Strkey::PublicKeyEd25519(signer.as_ref().get_public_key(index).await.unwrap()); + let signer: NativeSigner = + (self.config.get_network()?.network_passphrase, index).try_into()?; + let key = signer.as_ref().get_public_key(index).await.unwrap(); + let account = Strkey::PublicKeyEd25519(key); + txn.source_account = MuxedAccount::Ed25519(Uint256(key.0)); let bx_signer = Box::new(signer); Ok(bx_signer.sign_txn(txn, &account).await.unwrap()) }