Skip to content

Commit

Permalink
Responds to feedback from PR #413
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Jun 27, 2022
1 parent 3ba8c3b commit 991ebaa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
sign
} from './functions/index';
import { retryWrapper } from './shared/functions';
import { validateEphemeralPub } from './shared/validators';
import { getP256KeyPair, getP256KeyPairFromPub, randomBytes } from './util';

/**
Expand Down Expand Up @@ -109,9 +110,8 @@ export class Client {

/** @internal */
public set ephemeralPub (ephemeralPub: KeyPair) {
if (ephemeralPub) {
this._ephemeralPub = ephemeralPub;
}
validateEphemeralPub(ephemeralPub)
this._ephemeralPub = ephemeralPub;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/shared/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { responseMsgs } from '../constants';
const buildLatticeResponseErrorMessage = ({ responseCode, errorMessage }) => {
const msg: string[] = [];
if (responseCode) {
msg.push(`Response Code: ${responseCode}`);
msg.push(`Message: ${responseMsgs[responseCode]}`);
msg.push(`${responseMsgs[responseCode]}`);
}
if (errorMessage) {
msg.push('Error Message: ');
Expand Down
48 changes: 25 additions & 23 deletions src/shared/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ENC_MSG_LEN,
EXTERNAL,
REQUEST_TYPE_BYTE,
responseMsgs,
VERSION_BYTE,
} from '../constants';
import ethereum from '../ethereum';
Expand Down Expand Up @@ -225,33 +224,36 @@ export const retryWrapper = async ({
retries,
client,
}) => {
return fn({ ...params }).catch(async (err) => {
const errorMessage = err.errorMessage;
const responseCode = err.responseCode;
return fn({ ...params })
.catch(async (err) => {
/** `string` returned from the Lattice if there's an error */
const errorMessage = err.errorMessage;
/** `number` returned from the Lattice if there's an error */
const responseCode = err.responseCode;

if ((errorMessage || responseCode) && retries) {
if ((errorMessage || responseCode) && retries) {

if (isDeviceBusy(responseCode)) {
await sleep(3000);
}
if (isDeviceBusy(responseCode)) {
await sleep(3000);
}

if (isWrongWallet(responseCode) && !client.skipRetryOnWrongWallet) {
await client.fetchActiveWallet();
}
if (isWrongWallet(responseCode) && !client.skipRetryOnWrongWallet) {
await client.fetchActiveWallet();
}

if (isInvalidEphemeralId(responseCode)) {
await client.connect(client.deviceId);
}
if (isInvalidEphemeralId(responseCode)) {
await client.connect(client.deviceId);
}

return retryWrapper({
fn,
params,
retries: retries - 1,
client,
});
}
throw err;
});
return retryWrapper({
fn,
params,
retries: retries - 1,
client,
});
}
throw err;
});
};

/**
Expand Down
3 changes: 1 addition & 2 deletions src/shared/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const validateIsUInt4 = (n?: number) => {
};

export const validateNAddresses = (n: number) => {
validateIsUInt4(n);
if (n > MAX_ADDR)
throw new Error(`You may only request ${MAX_ADDR} addresses at once.`);
};
Expand Down Expand Up @@ -84,7 +83,7 @@ export const validateFwConstants = (fwConstants?: FirmwareConstants) => {
return fwConstants;
};
export const validateFwVersion = (fwVersion?: Buffer) => {
if (!fwVersion) {
if (!fwVersion || fwVersion.byteLength > 4) {
throw new Error('Firmware version does not exist. Please reconnect.');
}
return fwVersion;
Expand Down

0 comments on commit 991ebaa

Please sign in to comment.