|
| 1 | +use crate::trezor::error::TrezorError; |
| 2 | +use crate::trezor::proto::{bitcoin as btc, common as pb, management as mgmt}; |
| 3 | +use prost::Message; |
| 4 | + |
| 5 | +const HEADER: [u8; 2] = *b"##"; |
| 6 | + |
| 7 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 8 | +#[repr(u16)] |
| 9 | +pub enum MessageType { |
| 10 | + Initialize = 0, |
| 11 | + Success = 2, |
| 12 | + Failure = 3, |
| 13 | + GetPublicKey = 11, |
| 14 | + PublicKey = 12, |
| 15 | + Features = 17, |
| 16 | + PinMatrixRequest = 18, |
| 17 | + ButtonRequest = 26, |
| 18 | + ButtonAck = 27, |
| 19 | + GetAddress = 29, |
| 20 | + Address = 30, |
| 21 | + PassphraseRequest = 41, |
| 22 | + PassphraseAck = 42, |
| 23 | + GetFeatures = 55, |
| 24 | +} |
| 25 | + |
| 26 | +pub fn frame(msg_type: u16, payload: &[u8]) -> Vec<u8> { |
| 27 | + let mut out = Vec::with_capacity(8 + payload.len()); |
| 28 | + out.extend_from_slice(&HEADER); |
| 29 | + out.extend_from_slice(&msg_type.to_be_bytes()); |
| 30 | + out.extend_from_slice(&(payload.len() as u32).to_be_bytes()); |
| 31 | + out.extend_from_slice(payload); |
| 32 | + out |
| 33 | +} |
| 34 | + |
| 35 | +pub fn parse_frame(data: &[u8]) -> Result<(u16, Vec<u8>), TrezorError> { |
| 36 | + if data.len() < 8 || data[0..2] != HEADER { |
| 37 | + return Err(TrezorError::MalformedFrame); |
| 38 | + } |
| 39 | + let msg_type = u16::from_be_bytes([data[2], data[3]]); |
| 40 | + let len = u32::from_be_bytes([data[4], data[5], data[6], data[7]]) as usize; |
| 41 | + let payload = data |
| 42 | + .get(8..8 + len) |
| 43 | + .ok_or(TrezorError::MalformedFrame)? |
| 44 | + .to_vec(); |
| 45 | + Ok((msg_type, payload)) |
| 46 | +} |
| 47 | + |
| 48 | +pub fn decode<M: Message + Default>(payload: &[u8]) -> Result<M, TrezorError> { |
| 49 | + M::decode(payload).map_err(TrezorError::Decode) |
| 50 | +} |
| 51 | + |
| 52 | +fn encode<M: Message>(msg_type: MessageType, msg: &M) -> Vec<u8> { |
| 53 | + frame(msg_type as u16, &msg.encode_to_vec()) |
| 54 | +} |
| 55 | + |
| 56 | +pub fn initialize() -> Vec<u8> { |
| 57 | + encode(MessageType::Initialize, &mgmt::Initialize::default()) |
| 58 | +} |
| 59 | + |
| 60 | +pub fn get_features() -> Vec<u8> { |
| 61 | + encode(MessageType::GetFeatures, &mgmt::GetFeatures::default()) |
| 62 | +} |
| 63 | + |
| 64 | +pub fn button_ack() -> Vec<u8> { |
| 65 | + encode(MessageType::ButtonAck, &pb::ButtonAck::default()) |
| 66 | +} |
| 67 | + |
| 68 | +pub fn passphrase_ack_on_device() -> Vec<u8> { |
| 69 | + let msg = pb::PassphraseAck { |
| 70 | + on_device: Some(true), |
| 71 | + passphrase: None, |
| 72 | + ..Default::default() |
| 73 | + }; |
| 74 | + encode(MessageType::PassphraseAck, &msg) |
| 75 | +} |
| 76 | + |
| 77 | +pub fn get_public_key( |
| 78 | + address_n: Vec<u32>, |
| 79 | + show_display: bool, |
| 80 | + script_type: btc::InputScriptType, |
| 81 | + coin_name: String, |
| 82 | +) -> Vec<u8> { |
| 83 | + let msg = btc::GetPublicKey { |
| 84 | + address_n, |
| 85 | + show_display: Some(show_display), |
| 86 | + coin_name: Some(coin_name), |
| 87 | + script_type: Some(script_type as i32), |
| 88 | + ignore_xpub_magic: Some(true), |
| 89 | + ..Default::default() |
| 90 | + }; |
| 91 | + encode(MessageType::GetPublicKey, &msg) |
| 92 | +} |
| 93 | + |
| 94 | +pub fn get_address( |
| 95 | + address_n: Vec<u32>, |
| 96 | + show_display: bool, |
| 97 | + script_type: btc::InputScriptType, |
| 98 | + coin_name: String, |
| 99 | +) -> Vec<u8> { |
| 100 | + let msg = btc::GetAddress { |
| 101 | + address_n, |
| 102 | + show_display: Some(show_display), |
| 103 | + coin_name: Some(coin_name), |
| 104 | + script_type: Some(script_type as i32), |
| 105 | + ..Default::default() |
| 106 | + }; |
| 107 | + encode(MessageType::GetAddress, &msg) |
| 108 | +} |
0 commit comments