Skip to content

Commit

Permalink
Add method to sign raw data, enabling easier device app testing (sola…
Browse files Browse the repository at this point in the history
…na-labs#8221)

* Add method to sign raw data, enabling easier device app testing

* Rename ugly derivation method, params
  • Loading branch information
CriesofCarrots authored Feb 12, 2020
1 parent fcac910 commit 0bbee94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
33 changes: 25 additions & 8 deletions remote-wallet/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,19 @@ impl LedgerWallet {
ver[3].into(),
))
}

pub fn sign_raw_data(
&self,
derivation_path: &DerivationPath,
data: &[u8],
) -> Result<Vec<u8>, RemoteWalletError> {
let mut payload = extend_and_serialize(&derivation_path);
for byte in (data.len() as u16).to_be_bytes().iter() {
payload.push(*byte);
}
payload.extend_from_slice(data);
self.send_apdu(0x03, 1, 0, &payload)
}
}

impl RemoteWallet for LedgerWallet {
Expand Down Expand Up @@ -265,8 +278,8 @@ impl RemoteWallet for LedgerWallet {
})
}

fn get_pubkey(&self, derivation: &DerivationPath) -> Result<Pubkey, RemoteWalletError> {
let derivation_path = get_derivation_path(derivation);
fn get_pubkey(&self, derivation_path: &DerivationPath) -> Result<Pubkey, RemoteWalletError> {
let derivation_path = extend_and_serialize(derivation_path);

let key = self.send_apdu(
commands::GET_SOL_PUBKEY,
Expand All @@ -282,10 +295,10 @@ impl RemoteWallet for LedgerWallet {

fn sign_transaction(
&self,
derivation: &DerivationPath,
derivation_path: &DerivationPath,
transaction: Transaction,
) -> Result<Signature, RemoteWalletError> {
let mut payload = get_derivation_path(derivation);
let mut payload = extend_and_serialize(derivation_path);
let mut data = transaction.message_data();
if data.len() > u16::max_value() as usize {
return Err(RemoteWalletError::InvalidInput(
Expand Down Expand Up @@ -321,13 +334,17 @@ pub fn is_valid_ledger(vendor_id: u16, product_id: u16) -> bool {
}

/// Build the derivation path byte array from a DerivationPath selection
fn get_derivation_path(derivation: &DerivationPath) -> Vec<u8> {
let byte = if derivation.change.is_some() { 4 } else { 3 };
fn extend_and_serialize(derivation_path: &DerivationPath) -> Vec<u8> {
let byte = if derivation_path.change.is_some() {
4
} else {
3
};
let mut concat_derivation = vec![byte];
concat_derivation.extend_from_slice(&SOL_DERIVATION_PATH_BE);
concat_derivation.extend_from_slice(&[0x80, 0]);
concat_derivation.extend_from_slice(&derivation.account.to_be_bytes());
if let Some(change) = derivation.change {
concat_derivation.extend_from_slice(&derivation_path.account.to_be_bytes());
if let Some(change) = derivation_path.change {
concat_derivation.extend_from_slice(&[0x80, 0]);
concat_derivation.extend_from_slice(&change.to_be_bytes());
}
Expand Down
4 changes: 2 additions & 2 deletions remote-wallet/src/remote_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ pub trait RemoteWallet {
) -> Result<RemoteWalletInfo, RemoteWalletError>;

/// Get solana pubkey from a RemoteWallet
fn get_pubkey(&self, derivation: &DerivationPath) -> Result<Pubkey, RemoteWalletError>;
fn get_pubkey(&self, derivation_path: &DerivationPath) -> Result<Pubkey, RemoteWalletError>;

/// Sign transaction data with wallet managing pubkey at derivation path m/44'/501'/<account>'/<change>'.
fn sign_transaction(
&self,
derivation: &DerivationPath,
derivation_path: &DerivationPath,
transaction: Transaction,
) -> Result<Signature, RemoteWalletError>;
}
Expand Down

0 comments on commit 0bbee94

Please sign in to comment.