Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlesisz committed Oct 23, 2024
1 parent d3628bf commit 26daf9d
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 14 deletions.
5 changes: 2 additions & 3 deletions packages/connect/src/device/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,11 @@ export class Device extends TypedEmitter<DeviceEvents> {
return this._runInner(() => Promise.resolve({}), options);
}
if (error.code === 'Failure_InvalidProtocol') {
if (this.protocol.name === 'v2') {
// TODO: is this possible?
}
_log.info('Changing device protocol to THP');
// switch to THP and try again
this.protocol = protocolV2;
// TODO: this can throw?
await this.transport.loadMessages('thp', protocolThp.getThpProtobufMessages);

return this._runInner(fn, options);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/src/protocol-thp/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './loadProtobuf';
export { getThpProtobufMessages } from './protobufDefinitions';
export * from './messageTypes';
export * from './protobufTypes';
3 changes: 0 additions & 3 deletions packages/transport/src/thp/receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ export type ParseThpMessageProps = {
export const parseThpMessage = ({ decoded, messages, protocolState }: ParseThpMessageProps) => {
const isAckExpected = protocolThp.isAckExpected(protocolState?.expectedResponses || []);

// make sure that THP protobuf messages are loaded
protocolThp.loadProtobuf(messages);

const protobufDecoder = (protobufMessageType: string | number, protobufPayload: Buffer) => {
const { Message, messageName } = createMessageFromType(messages, protobufMessageType);
const message = decodeProtobuf(Message, protobufPayload);
Expand Down
6 changes: 5 additions & 1 deletion packages/transport/src/transports/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TransportProtocolState,
thp as protocolThp,
} from '@trezor/protocol';
import type { MessageFromTrezor as ProtobufMessageType } from '@trezor/protobuf';
import { type MessageFromTrezor as ProtobufMessageType, loadProtobuf } from '@trezor/protobuf';

import {
Session,
Expand Down Expand Up @@ -447,6 +447,10 @@ export abstract class AbstractTransport extends TransportEmitter {
this.messages = protobuf.Root.fromJSON(messages);
}

public loadMessages(packageName: string, packageLoader: Parameters<typeof loadProtobuf>[2]) {
return loadProtobuf(this.messages, packageName, packageLoader);
}

protected success<T>(payload: T): Success<T> {
return success(payload);
}
Expand Down
3 changes: 0 additions & 3 deletions packages/transport/src/utils/receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ export async function receiveAndParse<T extends () => ReturnType<AbstractApi['re
};

if (protocol.name === 'v2') {
// make sure that THP protobuf messages are loaded
protocolThp.loadProtobuf(messages);

const { messageName, message } = protocolThp.decode(
readResult.payload,
protobufDecoder,
Expand Down
3 changes: 0 additions & 3 deletions packages/transport/src/utils/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ export const buildMessage = ({
};

if (protocol.name === 'v2') {
// make sure that THP protobuf messages are loaded
protocolThp.loadProtobuf(messages);

return protocolThp.encode({
data,
messageType: name,
Expand Down

0 comments on commit 26daf9d

Please sign in to comment.