Skip to content

Commit

Permalink
possible conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlesisz committed Aug 13, 2024
1 parent 5cc8265 commit 1d9bd53
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
37 changes: 21 additions & 16 deletions packages/transport/src/transports/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,20 +263,15 @@ export class BridgeTransport extends AbstractTransport {
return body.toString('hex');
}

private getResponseBody(
response: string,
protocol?: TransportProtocol,
protocolState?: TransportProtocolState,
) {
private getResponseBody(response: string, protocol?: TransportProtocol) {
if (this.legacyBridge && protocol?.name === 'v2') {
// TODO: try catch + better condition?
const { body, state } = JSON.parse(response);
protocolState?.deserialize(state);
const { body, state } = response as any;

return body;
return { body, state };
}

return response;
return { body: response, state: undefined };
}

// https://github.dev/trezor/trezord-go/blob/f559ee5079679aeb5f897c65318d3310f78223ca/core/core.go#L534
Expand Down Expand Up @@ -310,17 +305,27 @@ export class BridgeTransport extends AbstractTransport {
return response;
}

const payload = this.getResponseBody(response.payload, protocol, protocolState);
const { body, state } = this.getResponseBody(response.payload, protocol);
console.warn('State in the response', response.payload, protocolState);
const message = await receiveAndParse(
this.messages,
() => Promise.resolve(Buffer.from(payload, 'hex')),
() => Promise.resolve(Buffer.from(body, 'hex')),
protocol,
protocolState,
);

if (protocolState?.shouldUpdateNonce(name)) {
protocolState?.updateNonce('send');
protocolState?.updateNonce('recv');
}
// protocolState?.deserialize(state);
console.warn('State after the response', state, protocolState);
protocolState?.updateState(message.type);

// const isAckExpected = protocolThp.isAckExpected(protocolState?.expectedResponses || []);
// if (isAckExpected) {
// protocolState?.updateSyncBit('recv');
// }

// if (protocolState?.shouldUpdateNonce(message.type)) {
// protocolState?.updateNonce('send');
// protocolState?.updateNonce('recv');
// }

return this.success(message);
},
Expand Down
9 changes: 8 additions & 1 deletion packages/transport/src/utils/bridgeApiResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ function isString(payload: UnknownPayload): payload is string {
return typeof payload === 'string';
}

// TODO: related PR
function isObject(payload: UnknownPayload): payload is string {
console.warn('isObject', payload);

return typeof payload === 'object';
}

export function info(res: UnknownPayload) {
if (isString(res)) {
return error({ error: ERRORS.WRONG_RESULT_TYPE });
Expand Down Expand Up @@ -79,7 +86,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

0 comments on commit 1d9bd53

Please sign in to comment.