From ef8dda6220c268458d9fab65d628922f79a1c23a 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 5a878fed0..f321ac4bc 100644 --- a/packages/taco/src/taco.ts +++ b/packages/taco/src/taco.ts @@ -124,7 +124,7 @@ export const encryptWithPublicKey = async ( * @param {Domain} domain - Represents the logical network in which the decryption will be performed. * Must match the `ritualId`. * @param {ThresholdMessageKit} messageKit - The kit containing the message to be decrypted - * @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 {ethers.Signer} [signer] - An optional signer for the decryption * @param {Record} [customParameters] - Optional custom parameters that may be required @@ -139,11 +139,11 @@ export const decrypt = async ( provider: ethers.providers.Provider, domain: Domain, messageKit: ThresholdMessageKit, - porterUri?: string | string[], + porterUris: string[] = [], signer?: ethers.Signer, customParameters?: Record, ): Promise => { - const porterUris: string[] = getPorterUris(domain, porterUri); + const porterUrisFull: string[] = getPorterUris(domain, porterUris); const ritualId = await DkgCoordinatorAgent.getRitualIdFromPublicKey( provider, @@ -154,7 +154,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 f07e7656c..9c36b66ab 100644 --- a/packages/taco/test/taco.test.ts +++ b/packages/taco/test/taco.test.ts @@ -84,7 +84,7 @@ describe('taco', () => { provider, domains.DEVNET, messageKit, - fakePorterUri, + [fakePorterUri], signer, ); expect(getParticipantsSpy).toHaveBeenCalled();