From 197781548eae34b969eda2e283e08540c47baa3f Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Thu, 25 Jul 2024 13:25:11 -0400 Subject: [PATCH] apply pr suggestions --- packages/shared/src/porter.ts | 45 ++++++++++----------- packages/shared/test/porter.test.ts | 62 ++++++++++++++++------------- packages/taco/src/taco.ts | 8 ++-- packages/taco/test/taco.test.ts | 2 +- 4 files changed, 60 insertions(+), 57 deletions(-) diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index 4dc054924..388b8f5c0 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -19,7 +19,6 @@ import { fromBase64, fromHexString, toBase64, toHexString } from './utils'; const defaultPorterUri: Record = { mainnet: 'https://porter.nucypher.community', tapir: 'https://porter-tapir.nucypher.community', - oryx: 'https://porter-oryx.nucypher.community', lynx: 'https://porter-lynx.nucypher.community', }; @@ -35,21 +34,17 @@ export const getPorterUri = (domain: Domain): string => { return getPorterUris(domain)[0]; }; -export const getPorterUris = (domain: Domain, porterUri?: string | string[]): string[] => { - const porterUris: string[] = []; - if (porterUri) { - if (porterUri instanceof Array) { - porterUris.push(...porterUri); - } else { - porterUris.push(porterUri); - } - } +export const getPorterUris = ( + domain: Domain, + porterUris: string[] = [], +): string[] => { + const fullList = [...porterUris]; const uri = defaultPorterUri[domain]; if (!uri) { throw new Error(`No default Porter URI found for domain: ${domain}`); } - porterUris.push(uri); - return porterUris; + fullList.push(uri); + return fullList; }; // /get_ursulas @@ -147,11 +142,13 @@ export class PorterClient { } } - protected async tryAndCall(config: AxiosRequestConfig): Promise> { + protected async tryAndCall( + config: AxiosRequestConfig, + ): Promise> { let resp!: AxiosResponse; let lastError = undefined; for (const porterUrl of this.porterUrls) { - const localConfig = { ...config, baseURL: porterUrl.toString() } + const localConfig = { ...config, baseURL: porterUrl.toString() }; try { resp = await axios.request(localConfig); } catch (e) { @@ -165,7 +162,9 @@ export class PorterClient { if (lastError !== undefined) { throw lastError; } - throw new Error("Porter returns bad response"); + throw new Error( + 'Porter returns bad response: ${resp.status} - ${resp.data}', + ); } public async getUrsulas( @@ -185,8 +184,7 @@ export class PorterClient { paramsSerializer: (params) => { return qs.stringify(params, { arrayFormat: 'comma' }); }, - }, - ); + }); return resp.data.result.ursulas.map((u: UrsulaResponse) => ({ checksumAddress: u.checksum_address, uri: u.uri, @@ -216,7 +214,7 @@ export class PorterClient { await this.tryAndCall({ url: '/retrieve_cfrags', method: 'post', - data: data + data: data, }); return resp.data.result.retrieval_results.map(({ cfrags, errors }) => { @@ -242,12 +240,11 @@ export class PorterClient { ), threshold, }; - const resp: AxiosResponse = - await this.tryAndCall({ - url: '/decrypt', - method: 'post', - data: data - }); + const resp: AxiosResponse = await this.tryAndCall({ + url: '/decrypt', + method: 'post', + data: data, + }); const { encrypted_decryption_responses, errors } = resp.data.result.decryption_results; diff --git a/packages/shared/test/porter.test.ts b/packages/shared/test/porter.test.ts index 113f3012a..83d65b252 100644 --- a/packages/shared/test/porter.test.ts +++ b/packages/shared/test/porter.test.ts @@ -1,19 +1,21 @@ +import { fakeUrsulas } from '@nucypher/test-utils'; +import axios, { HttpStatusCode } from 'axios'; +import { SpyInstance, beforeAll, describe, expect, it, vi } from 'vitest'; import { - initialize, GetUrsulasResult, PorterClient, - toHexString, Ursula, + initialize, + toHexString, } from '../src'; -import { fakeUrsulas } from '@nucypher/test-utils'; -import { beforeAll, describe, expect, SpyInstance, it, vi } from 'vitest'; -import axios, { HttpStatusCode } from 'axios'; -const fakePorterUris = ['https://_this_should_crash.com/', 'https://2_this_should_crash.com/', 'https://_this_should_work.com/']; +const fakePorterUris = [ + 'https://_this_should_crash.com/', + 'https://2_this_should_crash.com/', + 'https://_this_should_work.com/', +]; -const mockGetUrsulas = ( - ursulas: Ursula[] = fakeUrsulas(), -): SpyInstance => { +const mockGetUrsulas = (ursulas: Ursula[] = fakeUrsulas()): SpyInstance => { const fakePorterUrsulas = ( mockUrsulas: readonly Ursula[], ): GetUrsulasResult => { @@ -32,7 +34,10 @@ const mockGetUrsulas = ( return vi.spyOn(axios, 'request').mockImplementation(async (config) => { switch (config.baseURL) { case fakePorterUris[2]: - return Promise.resolve({ status: HttpStatusCode.Ok, data: fakePorterUrsulas(ursulas) }); + return Promise.resolve({ + status: HttpStatusCode.Ok, + data: fakePorterUrsulas(ursulas), + }); case fakePorterUris[0]: throw new Error(); default: @@ -46,37 +51,39 @@ describe('PorterClient', () => { await initialize(); }); - it('Get Ursulas', async () => { + it('should work when at least one ursula URI is valid', async () => { const ursulas = fakeUrsulas(); const getUrsulasSpy = mockGetUrsulas(ursulas); const porterClient = new PorterClient(fakePorterUris); const result = await porterClient.getUrsulas(ursulas.length); - expect(result.every((u: Ursula, index: number) => { - const expectedUrsula = ursulas[index]; - return u.checksumAddress === expectedUrsula.checksumAddress && - u.uri === expectedUrsula.uri && - u.encryptingKey.equals(expectedUrsula.encryptingKey); - })).toBeTruthy(); + expect( + result.every((u: Ursula, index: number) => { + const expectedUrsula = ursulas[index]; + return ( + u.checksumAddress === expectedUrsula.checksumAddress && + u.uri === expectedUrsula.uri && + u.encryptingKey.equals(expectedUrsula.encryptingKey) + ); + }), + ).toBeTruthy(); const params = { method: 'get', - url: "/get_ursulas", + url: '/get_ursulas', params: { exclude_ursulas: [], include_ursulas: [], quantity: ursulas.length, - } + }, }; expect(getUrsulasSpy).toBeCalledTimes(fakePorterUris.length); - expect(getUrsulasSpy).toHaveBeenNthCalledWith(1, expect.objectContaining( - { ...params, baseURL: fakePorterUris[0] }) - ); - expect(getUrsulasSpy).toHaveBeenNthCalledWith(2, expect.objectContaining( - { ...params, baseURL: fakePorterUris[1] }) - ); - expect(getUrsulasSpy).toHaveBeenNthCalledWith(3, expect.objectContaining( - { ...params, baseURL: fakePorterUris[2] })); + fakePorterUris.forEach((value, index) => { + expect(getUrsulasSpy).toHaveBeenNthCalledWith( + index + 1, + expect.objectContaining({ ...params, baseURL: value }), + ); + }); }); it('returns error in case all porters fail', async () => { @@ -87,5 +94,4 @@ describe('PorterClient', () => { porterClient = new PorterClient([fakePorterUris[1], fakePorterUris[0]]); expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(); }); - }); diff --git a/packages/taco/src/taco.ts b/packages/taco/src/taco.ts index 93d29b764..ce6572bf1 100644 --- a/packages/taco/src/taco.ts +++ b/packages/taco/src/taco.ts @@ -130,7 +130,7 @@ export const encryptWithPublicKey = async ( * Must match the `ritualId`. * @param {ThresholdMessageKit} messageKit - The kit containing the message to be decrypted * @param authProvider - The authentication provider that will be used to provide the authorization - * @param {string} [porterUri] - The URI for the Porter service. If not provided, a value will be obtained + * @param {string} [porterUri] - The URI(s) for the Porter service. If not provided, a value will be obtained * from the Domain * @param {Record} [customParameters] - Optional custom parameters that may be required * depending on the condition used @@ -145,10 +145,10 @@ export const decrypt = async ( domain: Domain, messageKit: ThresholdMessageKit, authProvider?: EIP4361AuthProvider, - porterUri?: string | string[], + porterUris: string[] = [], customParameters?: Record, ): Promise => { - const porterUris: string[] = getPorterUris(domain, porterUri); + const porterUrisFull: string[] = getPorterUris(domain, porterUris); const ritualId = await DkgCoordinatorAgent.getRitualIdFromPublicKey( provider, @@ -164,7 +164,7 @@ export const decrypt = async ( return retrieveAndDecrypt( provider, domain, - porterUris, + porterUrisFull, messageKit, ritualId, ritual.sharesNum, diff --git a/packages/taco/test/taco.test.ts b/packages/taco/test/taco.test.ts index f965fa80c..8fd2545a0 100644 --- a/packages/taco/test/taco.test.ts +++ b/packages/taco/test/taco.test.ts @@ -93,7 +93,7 @@ describe('taco', () => { domains.DEVNET, messageKit, authProvider, - fakePorterUri, + [fakePorterUri], ); expect(decryptedMessage).toEqual(toBytes(message)); expect(getParticipantsSpy).toHaveBeenCalled();