Skip to content

Commit

Permalink
feat: sign blob
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed May 3, 2024
1 parent f0e0adc commit 4e54058
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
22 changes: 17 additions & 5 deletions cmd/crates/stellar-ledger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>, 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<u8> = 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,
Expand All @@ -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<Vec<u8>, 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
Expand Down
18 changes: 12 additions & 6 deletions cmd/soroban-cli/src/commands/txn/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)]
Expand Down Expand Up @@ -115,16 +119,18 @@ impl Cmd {
.await?)
}

pub async fn sign_ledger(&self, txn: Transaction) -> Result<TransactionEnvelope, Error> {
pub async fn sign_ledger(&self, mut txn: Transaction) -> Result<TransactionEnvelope, Error> {
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())
}
Expand Down

0 comments on commit 4e54058

Please sign in to comment.