@@ -22,36 +22,28 @@ use common::{
2222} ;
2323use crypto:: key:: {
2424 extended:: ExtendedPublicKey ,
25- hdkd:: {
26- chain_code:: { ChainCode , CHAINCODE_LENGTH } ,
27- derivation_path:: DerivationPath ,
28- } ,
25+ hdkd:: { chain_code:: ChainCode , derivation_path:: DerivationPath } ,
2926 secp256k1:: { extended_keys:: Secp256k1ExtendedPublicKey , Secp256k1PublicKey } ,
3027} ;
31- use serialization:: { Decode , DecodeAll , Encode } ;
28+ use serialization:: Encode ;
3229use utils:: ensure;
3330use wallet_types:: hw_data:: LedgerFullInfo ;
3431
3532use ledger_lib:: Exchange ;
3633use mintlayer_ledger_messages:: {
3734 decode_all as ledger_decode_all, encode as ledger_encode, AddrType , Amount as LAmount ,
38- Bip32Path as LedgerBip32Path , CoinType , InputAdditionalInfoReq , Ins ,
39- OutputValue as LOutputValue , P1SignTx , PubKeyP1 , PublicKeyReq , SignMessageReq , SignTxReq ,
40- TxInput as LTxInput , TxInputReq , TxMetadataReq , TxOutput as LTxOutput , TxOutputReq , APDU_CLASS ,
41- H256 as LH256 , P1_APP_NAME , P1_GET_VERSION , P1_SIGN_NEXT , P1_SIGN_START , P2_DONE , P2_SIGN_MORE ,
35+ Bip32Path as LedgerBip32Path , CoinType , GetPublicKeyRespones , GetVersionRespones ,
36+ InputAdditionalInfoReq , Ins , MsgSignature , OutputValue as LOutputValue , P1SignTx , PubKeyP1 ,
37+ PublicKeyReq , SignMessageReq , SignTxReq , Signature as LedgerSignature , TxInput as LTxInput ,
38+ TxInputReq , TxMetadataReq , TxOutput as LTxOutput , TxOutputReq , APDU_CLASS , H256 as LH256 ,
39+ P1_APP_NAME , P1_GET_VERSION , P1_SIGN_NEXT , P1_SIGN_START , P2_DONE , P2_SIGN_MORE ,
4240} ;
4341
4442const MAX_ADPU_LEN : usize = ( u8:: MAX - 5 ) as usize ; // 4 bytes for the header + 1 for len
4543const TIMEOUT_DUR : Duration = Duration :: from_secs ( 100 ) ;
4644const OK_RESPONSE : u16 = 0x9000 ;
4745const TX_VERSION : u8 = 1 ;
4846
49- #[ derive( Decode ) ]
50- pub struct LedgerSignature {
51- pub signature : [ u8 ; 64 ] ,
52- pub multisig_idx : Option < u32 > ,
53- }
54-
5547struct SignatureResult {
5648 sig : LedgerSignature ,
5749 input_idx : usize ,
@@ -129,10 +121,9 @@ pub async fn sign_challenge<L: Exchange>(
129121
130122 let resp = send_chunked ( ledger, Ins :: SIGN_MSG , P1_SIGN_NEXT , message) . await ?;
131123
132- let sig_len = * resp. first ( ) . ok_or ( LedgerError :: InvalidResponse ) ? as usize ;
133- let sig = resp. as_slice ( ) . get ( 1 ..1 + sig_len) . ok_or ( LedgerError :: InvalidResponse ) ?;
124+ let sig: MsgSignature = ledger_decode_all ( & resp) . ok_or ( LedgerError :: InvalidResponse ) ?;
134125
135- Ok ( sig. to_vec ( ) )
126+ Ok ( sig. signature . to_vec ( ) )
136127}
137128
138129pub async fn get_app_name < L : Exchange > ( ledger : & mut L ) -> Result < Vec < u8 > , ledger_lib:: Error > {
@@ -161,13 +152,12 @@ pub async fn check_current_app<L: Exchange>(ledger: &mut L) -> SignerResult<Ledg
161152 . await
162153 . map_err ( |err| LedgerError :: DeviceError ( err. to_string ( ) ) ) ?;
163154 let ver = ok_response ( resp) ?;
164- let app_version = match ver. as_slice ( ) {
165- [ major, minor, patch] => common:: primitives:: semver:: SemVer {
166- major : * major,
167- minor : * minor,
168- patch : * patch as u16 ,
169- } ,
170- _ => return Err ( SignerError :: LedgerError ( LedgerError :: InvalidResponse ) ) ,
155+ let app_version_resp: GetVersionRespones =
156+ ledger_decode_all ( & ver) . ok_or ( LedgerError :: InvalidResponse ) ?;
157+ let app_version = common:: primitives:: semver:: SemVer {
158+ major : app_version_resp. major ,
159+ minor : app_version_resp. minor ,
160+ patch : app_version_resp. patch as u16 ,
171161 } ;
172162
173163 Ok ( LedgerFullInfo { app_version } )
@@ -191,20 +181,13 @@ pub async fn get_extended_public_key<L: Exchange>(
191181 )
192182 . await ?;
193183
194- let pk_len = * resp. first ( ) . ok_or ( LedgerError :: InvalidResponse ) ? as usize ;
195- let public_key = resp. as_slice ( ) . get ( 1 ..1 + pk_len) . ok_or ( LedgerError :: InvalidResponse ) ?;
196- let chain_code_len = * resp. get ( 1 + pk_len) . ok_or ( LedgerError :: InvalidResponse ) ? as usize ;
197- let chain_code: [ _ ; CHAINCODE_LENGTH ] = resp
198- . as_slice ( )
199- . get ( 2 + pk_len..2 + pk_len + chain_code_len)
200- . ok_or ( LedgerError :: InvalidResponse ) ?
201- . try_into ( )
202- . map_err ( |_| LedgerError :: InvalidKey ) ?;
184+ let resp: GetPublicKeyRespones =
185+ ledger_decode_all ( & resp) . ok_or ( LedgerError :: InvalidResponse ) ?;
203186
204187 let extended_public_key = Secp256k1ExtendedPublicKey :: new_unchecked (
205188 derivation_path,
206- ChainCode :: from ( chain_code) ,
207- Secp256k1PublicKey :: from_bytes ( public_key) . map_err ( |_| LedgerError :: InvalidKey ) ?,
189+ ChainCode :: from ( resp . chain_code ) ,
190+ Secp256k1PublicKey :: from_bytes ( & resp . public_key ) . map_err ( |_| LedgerError :: InvalidKey ) ?,
208191 ) ;
209192
210193 Ok ( ExtendedPublicKey :: new ( extended_public_key) )
@@ -286,8 +269,8 @@ fn decode_signature_response(resp: &[u8]) -> Result<SignatureResult, LedgerError
286269 let input_idx = * resp. first ( ) . ok_or ( LedgerError :: InvalidResponse ) ? as usize ;
287270 let has_more_signatures = * resp. last ( ) . ok_or ( LedgerError :: InvalidResponse ) ? == P2_SIGN_MORE ;
288271
289- let sig = LedgerSignature :: decode_all ( & mut & resp [ ..resp . len ( ) - 1 ] [ 1 .. ] )
290- . map_err ( |_| LedgerError :: InvalidResponse ) ?;
272+ let sig: LedgerSignature =
273+ ledger_decode_all ( & resp [ ..resp . len ( ) - 1 ] [ 1 .. ] ) . ok_or ( LedgerError :: InvalidResponse ) ?;
291274
292275 Ok ( SignatureResult {
293276 sig,
0 commit comments