From 741d148a0df69d2ae658f543374c0a6d9f11426d Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Wed, 12 Feb 2020 15:38:51 -0700 Subject: [PATCH] Simplify remote wallet (#8249) automerge --- remote-wallet/src/ledger.rs | 30 ++++++++---------------------- remote-wallet/src/remote_wallet.rs | 6 +++--- sdk/src/message.rs | 4 ++++ sdk/src/transaction.rs | 3 +-- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/remote-wallet/src/ledger.rs b/remote-wallet/src/ledger.rs index 7a7424779..56da1a5f1 100644 --- a/remote-wallet/src/ledger.rs +++ b/remote-wallet/src/ledger.rs @@ -4,7 +4,7 @@ use crate::remote_wallet::{ use dialoguer::{theme::ColorfulTheme, Select}; use log::*; use semver::Version as FirmwareVersion; -use solana_sdk::{pubkey::Pubkey, signature::Signature, transaction::Transaction}; +use solana_sdk::{pubkey::Pubkey, signature::Signature}; use std::{cmp::min, fmt, sync::Arc}; const APDU_TAG: u8 = 0x05; @@ -38,8 +38,8 @@ const HID_PREFIX_ZERO: usize = 0; mod commands { #[allow(dead_code)] pub const GET_APP_CONFIGURATION: u8 = 0x06; - pub const GET_SOL_PUBKEY: u8 = 0x02; - pub const SIGN_SOL_TRANSACTION: u8 = 0x03; + pub const GET_PUBKEY: u8 = 0x02; + pub const SIGN_MESSAGE: u8 = 0x03; } /// Ledger Wallet device @@ -233,19 +233,6 @@ impl LedgerWallet { ver[3].into(), )) } - - pub fn sign_raw_data( - &self, - derivation_path: &DerivationPath, - data: &[u8], - ) -> Result, 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 { @@ -282,7 +269,7 @@ impl RemoteWallet for LedgerWallet { let derivation_path = extend_and_serialize(derivation_path); let key = self.send_apdu( - commands::GET_SOL_PUBKEY, + commands::GET_PUBKEY, 0, // In the naive implementation, default request is for no device confirmation 0, &derivation_path, @@ -293,13 +280,12 @@ impl RemoteWallet for LedgerWallet { Ok(Pubkey::new(&key)) } - fn sign_transaction( + fn sign_message( &self, derivation_path: &DerivationPath, - transaction: Transaction, + data: &[u8], ) -> Result { 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( "Message to sign is too long".to_string(), @@ -308,11 +294,11 @@ impl RemoteWallet for LedgerWallet { for byte in (data.len() as u16).to_be_bytes().iter() { payload.push(*byte); } - payload.append(&mut data); + payload.extend_from_slice(data); trace!("Serialized payload length {:?}", payload.len()); let result = self.send_apdu( - commands::SIGN_SOL_TRANSACTION, + commands::SIGN_MESSAGE, 1, // In the naive implementation, default request is for requred device confirmation 0, &payload, diff --git a/remote-wallet/src/remote_wallet.rs b/remote-wallet/src/remote_wallet.rs index f5ccb84cb..0689b814d 100644 --- a/remote-wallet/src/remote_wallet.rs +++ b/remote-wallet/src/remote_wallet.rs @@ -1,7 +1,7 @@ use crate::ledger::{is_valid_ledger, LedgerWallet}; use log::*; use parking_lot::{Mutex, RwLock}; -use solana_sdk::{pubkey::Pubkey, signature::Signature, transaction::Transaction}; +use solana_sdk::{pubkey::Pubkey, signature::Signature}; use std::{ fmt, str::FromStr, @@ -156,10 +156,10 @@ pub trait RemoteWallet { fn get_pubkey(&self, derivation_path: &DerivationPath) -> Result; /// Sign transaction data with wallet managing pubkey at derivation path m/44'/501'/'/'. - fn sign_transaction( + fn sign_message( &self, derivation_path: &DerivationPath, - transaction: Transaction, + data: &[u8], ) -> Result; } diff --git a/sdk/src/message.rs b/sdk/src/message.rs index 294159d5a..82260220e 100644 --- a/sdk/src/message.rs +++ b/sdk/src/message.rs @@ -208,6 +208,10 @@ impl Message { ) } + pub fn serialize(&self) -> Vec { + bincode::serialize(self).unwrap() + } + pub fn program_ids(&self) -> Vec<&Pubkey> { self.instructions .iter() diff --git a/sdk/src/transaction.rs b/sdk/src/transaction.rs index 27a100f66..c0e8525d7 100644 --- a/sdk/src/transaction.rs +++ b/sdk/src/transaction.rs @@ -9,7 +9,6 @@ use crate::{ signature::{KeypairUtil, Signature}, system_instruction, }; -use bincode::serialize; use std::result; use thiserror::Error; @@ -211,7 +210,7 @@ impl Transaction { /// Return the serialized message data to sign. pub fn message_data(&self) -> Vec { - serialize(&self.message()).unwrap() + self.message().serialize() } /// Check keys and keypair lengths, then sign this transaction.