Skip to content

Commit

Permalink
Restores skipRetryOnWrongWallet functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Jun 23, 2022
1 parent 43eb43e commit 669e18e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
12 changes: 2 additions & 10 deletions src/__test__/utils/initializeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,21 @@ import {
import { testRequest } from './testRequest';

export const initializeClient = () => {
let id = getDeviceId();
const id = getDeviceId();
const client = setupTestClient();

describe('Initializing client', () => {
beforeAll(async () => {
if (id) {
await client.connect(id);
}
});

it('Connecting to Lattice', async () => {
const _id = id
? id
: question('Please enter the ID of your test device: ');
id = _id;
const isPaired = await client.connect(id);
const isPaired = await client.connect(_id);
if (!isPaired) {
expect(client.isPaired).toEqual(false);
const secret = question('Please enter the pairing secret: ');
await client.pair(secret);
expect(!!client.getActiveWallet()).toEqual(true);
}
expect(isPaired).toEqual(true);
expect(client.isPaired).toEqual(true);
expect(!!client.getActiveWallet()).toEqual(true);
});
Expand Down
File renamed without changes.
43 changes: 29 additions & 14 deletions src/shared/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import {
randomBytes,
} from '../util';
import { LatticeResponseError } from './errors';
import { isDeviceBusy, isInvalidEphemeralId, isWrongWallet, shouldUseEVMLegacyConverter } from './predicates';
import {
isDeviceBusy,
isInvalidEphemeralId,
isWrongWallet,
shouldUseEVMLegacyConverter,
} from './predicates';
import {
validateChecksum,
validateRequestError,
Expand Down Expand Up @@ -175,6 +180,7 @@ export const request = async ({
.post(url)
.timeout(timeout)
.send({ data: payload })
.catch(validateRequestError)
.then(async (response) => {
// Handle formatting or generic HTTP errors
if (!response.body || !response.body.message) {
Expand All @@ -189,19 +195,19 @@ export const request = async ({
response.body.message,
);

if ((errorMessage || responseCode)) {
if (errorMessage || responseCode) {
throw new LatticeResponseError(responseCode, errorMessage);
}

return data;
});
})
};

/**
* `sleep()` returns a Promise that resolves after a given number of milliseconds.
*/
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
Expand All @@ -213,31 +219,40 @@ function sleep (ms) {
* @param retries - The number of times to retry the function
* @param client - The Client to use for side-effects
*/
export const retryWrapper = async ({ fn, params, retries, client }) => {
return fn({ ...params }).catch(async err => {
const errorMessage = err.errorMessage
const responseCode = err.responseCode
export const retryWrapper = async ({
fn,
params,
retries,
client,
}) => {
return fn({ ...params }).catch(async (err) => {
const errorMessage = err.errorMessage;
const responseCode = err.responseCode;

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

if (isDeviceBusy(responseCode)) {
await sleep(3000);
return retryWrapper({ fn, params, retries: retries - 1, client });
}

if (isWrongWallet(responseCode)) {
if (isWrongWallet(responseCode) && !client.skipRetryOnWrongWallet) {
await client.fetchActiveWallet();
return retryWrapper({ fn, params, retries: retries - 1, client });
}

if (isInvalidEphemeralId(responseCode)) {
await client.connect(client.deviceId);
return retryWrapper({ fn, params, retries: retries - 1, client });
}

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

/**
* All encrypted responses must be decrypted with the previous shared secret. Per specification,
Expand Down

0 comments on commit 669e18e

Please sign in to comment.