diff --git a/src/__test__/utils/initializeClient.ts b/src/__test__/utils/initializeClient.ts index 8508d2a1..3a6dd076 100644 --- a/src/__test__/utils/initializeClient.ts +++ b/src/__test__/utils/initializeClient.ts @@ -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); }); diff --git a/src/vectors.jsonc b/src/__test__/vectors.jsonc similarity index 100% rename from src/vectors.jsonc rename to src/__test__/vectors.jsonc diff --git a/src/shared/functions.ts b/src/shared/functions.ts index 9c764a21..a5533a7e 100644 --- a/src/shared/functions.ts +++ b/src/shared/functions.ts @@ -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, @@ -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) { @@ -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)); } /** @@ -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,