Skip to content

Commit

Permalink
fixing bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlesisz committed Jul 30, 2024
1 parent 2b76552 commit 6eff732
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/transport/src/transports/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export class BridgeTransport extends AbstractTransport {
) {
if (this.legacyBridge && protocol?.name === 'v2') {
// TODO: try catch + better condition?
const { body, state } = JSON.parse(response);
const { body, state } = response as any;
protocolState?.deserialize(state);

return body;
Expand Down Expand Up @@ -316,6 +316,7 @@ export class BridgeTransport extends AbstractTransport {
this.messages,
() => Promise.resolve(Buffer.from(payload, 'hex')),
protocol,
protocolState,
);

if (protocolState?.shouldUpdateNonce(name)) {
Expand Down
6 changes: 5 additions & 1 deletion packages/transport/src/utils/bridgeApiResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function isString(payload: UnknownPayload): payload is string {
return typeof payload === 'string';
}

function isObject(payload: UnknownPayload): payload is string {
return typeof payload === 'object';
}

export function info(res: UnknownPayload) {
if (isString(res)) {
return error({ error: ERRORS.WRONG_RESULT_TYPE });
Expand Down Expand Up @@ -80,7 +84,7 @@ export function acquire(res: UnknownPayload) {
}

export function call(res: UnknownPayload) {
if (!isString(res)) {
if (!isString(res) && !isObject(res)) {
return error({ error: ERRORS.WRONG_RESULT_TYPE });
}

Expand Down
35 changes: 31 additions & 4 deletions packages/transport/src/utils/receive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Root } from 'protobufjs/light';

import { decode as decodeProtobuf, createMessageFromType } from '@trezor/protobuf';
import { TransportProtocol } from '@trezor/protocol';
import { TransportProtocol, TransportProtocolState, thp as protocolThp } from '@trezor/protocol';

async function receiveRest(
result: Buffer,
Expand Down Expand Up @@ -43,10 +43,37 @@ export async function receiveAndParse(
messages: Root,
receiver: () => Promise<Buffer>,
protocol: TransportProtocol,
protocolState?: TransportProtocolState,
) {
const { messageType, payload } = await receive(receiver, protocol);
const { Message, messageName } = createMessageFromType(messages, messageType);
const message = decodeProtobuf(Message, payload);
const decoded = await receive(receiver, protocol);

const protobufDecoder = (protobufMessageType: string | number, protobufPayload: Buffer) => {
const { Message, messageName } = createMessageFromType(messages, protobufMessageType);
const message = decodeProtobuf(Message, protobufPayload);

return {
messageName,
message,
};
};

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

const { messageName, message } = protocolThp.decode(
decoded,
protobufDecoder,
protocolState,
);

return {
message,
type: messageName,
};
}

const { messageName, message } = protobufDecoder(decoded.messageType, decoded.payload);

return {
message,
Expand Down

0 comments on commit 6eff732

Please sign in to comment.