@@ -8,11 +8,11 @@ import {
8
8
THP_READ_ACK_HEADER_BYTE ,
9
9
} from './constants' ;
10
10
import { crc32 } from './crypto/crc32' ;
11
- import { addAckBit , addSequenceBit , getControlBit } from './utils' ;
11
+ import { addAckBit , addSequenceBit , getControlBit , isThpMessageName } from './utils' ;
12
12
13
13
// @trezor /protobuf encodeMessage without direct reference to protobuf root
14
14
type ProtobufEncoder = (
15
- messageName : string | number ,
15
+ messageName : string ,
16
16
messageData : Record < string , unknown > ,
17
17
) => {
18
18
messageType : number ;
@@ -146,7 +146,7 @@ export const encodeAck = (bytesOrState: Buffer | ThpState) => {
146
146
147
147
// Encode protocol-v2 message
148
148
export const encode = ( options : {
149
- messageType : number | string ;
149
+ messageName : string ;
150
150
data : Record < string , unknown > ;
151
151
thpState ?: ThpState ;
152
152
protobufEncoder : ProtobufEncoder ;
@@ -157,15 +157,15 @@ export const encode = (options: {
157
157
}
158
158
159
159
const channel = options . thpState . channel || THP_DEFAULT_CHANNEL ;
160
- const { messageType , protobufEncoder, thpState } = options ;
160
+ const { messageName , data , protobufEncoder, thpState } = options ;
161
161
162
162
let result : Buffer ;
163
- if ( typeof messageType === 'string' ) {
164
- const payload = encodePayload ( messageType , options . data , thpState ) ;
165
- result = encodeThpMessage ( messageType , payload , channel , options . thpState ) ;
163
+ if ( isThpMessageName ( messageName ) ) {
164
+ const payload = encodePayload ( messageName , data , thpState ) ;
165
+ result = encodeThpMessage ( messageName , payload , channel , options . thpState ) ;
166
166
} else {
167
- const { messageType : mt , message } = protobufEncoder ( messageType , options . data ) ;
168
- result = encodeProtobufMessage ( mt , message , channel , options . thpState ) ;
167
+ const { messageType, message } = protobufEncoder ( messageName , data ) ;
168
+ result = encodeProtobufMessage ( messageType , message , channel , thpState ) ;
169
169
}
170
170
171
171
return result ;
0 commit comments