From e1933a7abc58504e2e96412a6056ff03d911d754 Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Thu, 25 Jul 2024 15:07:19 -0400 Subject: [PATCH 01/10] Get default porter uris from github links --- examples/pre/nextjs/src/app/page.tsx | 2 +- examples/pre/nodejs/src/index.ts | 2 +- examples/pre/react/src/App.tsx | 2 +- examples/pre/webpack-5/src/index.ts | 2 +- examples/taco/webpack-5/src/index.ts | 1 - packages/shared/src/porter.ts | 31 ++++++++++++++++++++++++---- packages/shared/test/porter.test.ts | 20 ++++++++++++++++++ packages/taco/README.md | 2 +- packages/taco/src/taco.ts | 4 ++-- 9 files changed, 54 insertions(+), 12 deletions(-) diff --git a/examples/pre/nextjs/src/app/page.tsx b/examples/pre/nextjs/src/app/page.tsx index 6ca463030..eb3b4e4bc 100644 --- a/examples/pre/nextjs/src/app/page.tsx +++ b/examples/pre/nextjs/src/app/page.tsx @@ -109,7 +109,7 @@ function App() { provider, provider.getSigner(), domains.TESTNET, - getPorterUri(domains.TESTNET), + await getPorterUri(domains.TESTNET), policyParams, ); diff --git a/examples/pre/nodejs/src/index.ts b/examples/pre/nodejs/src/index.ts index 2a629e373..3bd080bf1 100644 --- a/examples/pre/nodejs/src/index.ts +++ b/examples/pre/nodejs/src/index.ts @@ -73,7 +73,7 @@ const runExample = async () => { provider, signer, domains.TESTNET, - getPorterUri(domains.TESTNET), + await getPorterUri(domains.TESTNET), policyParams, ); diff --git a/examples/pre/react/src/App.tsx b/examples/pre/react/src/App.tsx index 0afa5e7fe..62bf4b6b7 100644 --- a/examples/pre/react/src/App.tsx +++ b/examples/pre/react/src/App.tsx @@ -105,7 +105,7 @@ function App() { provider, provider.getSigner(), domains.TESTNET, - getPorterUri(domains.TESTNET), + await getPorterUri(domains.TESTNET), policyParams, ); diff --git a/examples/pre/webpack-5/src/index.ts b/examples/pre/webpack-5/src/index.ts index 532b27b57..b1a3b1a3b 100644 --- a/examples/pre/webpack-5/src/index.ts +++ b/examples/pre/webpack-5/src/index.ts @@ -82,7 +82,7 @@ const runExample = async () => { provider, provider.getSigner(), domains.TESTNET, - getPorterUri(domains.TESTNET), + await getPorterUri(domains.TESTNET), policyParams, ); diff --git a/examples/taco/webpack-5/src/index.ts b/examples/taco/webpack-5/src/index.ts index e372053ab..3879dd04f 100644 --- a/examples/taco/webpack-5/src/index.ts +++ b/examples/taco/webpack-5/src/index.ts @@ -5,7 +5,6 @@ import { EIP4361AuthProvider, encrypt, fromBytes, - getPorterUris, initialize, toBytes, } from '@nucypher/taco'; diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index 388b8f5c0..7f05b94f9 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -22,6 +22,12 @@ const defaultPorterUri: Record = { lynx: 'https://porter-lynx.nucypher.community', }; +const porterUriSource: Record = { + mainnet: 'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/pytest.ini', + tapir: 'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/pytest.ini', + lynx: 'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/pytest.ini', +}; + export type Domain = keyof typeof defaultPorterUri; export const domains: Record = { @@ -30,23 +36,40 @@ export const domains: Record = { MAINNET: 'mainnet', }; -export const getPorterUri = (domain: Domain): string => { - return getPorterUris(domain)[0]; +export const getPorterUri = async (domain: Domain): Promise => { + return (await getPorterUris(domain))[0]; }; -export const getPorterUris = ( +export const getPorterUris = async ( domain: Domain, porterUris: string[] = [], -): string[] => { +): Promise { const fullList = [...porterUris]; const uri = defaultPorterUri[domain]; if (!uri) { throw new Error(`No default Porter URI found for domain: ${domain}`); } fullList.push(uri); + const urisFromSource = await getPorterUrisFromSource(domain); + fullList.push(...urisFromSource); return fullList; }; +export const getPorterUrisFromSource = async (domain: Domain): Promise => { + const source = porterUriSource[domain]; + if (!source) { + return []; + } + try { + const resp = await axios.get(source, { + responseType: 'blob', + }); + return resp.data.split(/\r?\n/).filter(Boolean); + } catch (e) { + return []; + } +}; + // /get_ursulas export type Ursula = { diff --git a/packages/shared/test/porter.test.ts b/packages/shared/test/porter.test.ts index 83d65b252..27ac977b4 100644 --- a/packages/shared/test/porter.test.ts +++ b/packages/shared/test/porter.test.ts @@ -7,6 +7,9 @@ import { Ursula, initialize, toHexString, + getPorterUrisFromSource, + getPorterUris, + domains } from '../src'; const fakePorterUris = [ @@ -46,6 +49,23 @@ const mockGetUrsulas = (ursulas: Ursula[] = fakeUrsulas()): SpyInstance => { }); }; +describe('getPorterUris', () => { + beforeAll(async () => { + await initialize(); + }); + + it('Get URIs from source', async () => { + for (const domain of Object.values(domains)) { + const uris = await getPorterUrisFromSource(domain); + expect(uris.length).toBeGreaterThan(0); + const fullList = await getPorterUris(domain); + expect(fullList).toEqual( + expect.arrayContaining(uris) + ); + } + }); +}); + describe('PorterClient', () => { beforeAll(async () => { await initialize(); diff --git a/packages/taco/README.md b/packages/taco/README.md index bd94816f4..05c6d97ad 100644 --- a/packages/taco/README.md +++ b/packages/taco/README.md @@ -60,7 +60,7 @@ const decryptedMessage = await decrypt( web3Provider, domains.TESTNET, messageKit, - getPorterUri(domains.TESTNET), + await getPorterUri(domains.TESTNET), web3Provider.getSigner(), ); ``` diff --git a/packages/taco/src/taco.ts b/packages/taco/src/taco.ts index ce6572bf1..62b0f344a 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(s) 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 @@ -148,7 +148,7 @@ export const decrypt = async ( porterUris: string[] = [], customParameters?: Record, ): Promise => { - const porterUrisFull: string[] = getPorterUris(domain, porterUris); + const porterUrisFull: string[] = await getPorterUris(domain, porterUris); const ritualId = await DkgCoordinatorAgent.getRitualIdFromPublicKey( provider, From a027d7c001a1414fdb3799c147c16755d7b344ac Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Mon, 29 Jul 2024 20:31:51 -0400 Subject: [PATCH 02/10] Updates porter source --- packages/shared/src/porter.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index 7f05b94f9..0222e9e13 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -22,11 +22,8 @@ const defaultPorterUri: Record = { lynx: 'https://porter-lynx.nucypher.community', }; -const porterUriSource: Record = { - mainnet: 'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/pytest.ini', - tapir: 'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/pytest.ini', - lynx: 'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/pytest.ini', -}; +const porterUriSource: string = + 'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/porter_instances.json'; export type Domain = keyof typeof defaultPorterUri; From e3d336dca2b7e2a58d0b9dc60e01a59e2fc6755b Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Tue, 30 Jul 2024 09:47:56 -0400 Subject: [PATCH 03/10] apply pr suggestions --- packages/shared/src/porter.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index 0222e9e13..e19b7ef4b 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -17,13 +17,13 @@ import { Base64EncodedBytes, ChecksumAddress, HexEncodedBytes } from './types'; import { fromBase64, fromHexString, toBase64, toHexString } from './utils'; const defaultPorterUri: Record = { - mainnet: 'https://porter.nucypher.community', - tapir: 'https://porter-tapir.nucypher.community', - lynx: 'https://porter-lynx.nucypher.community', + mainnet: 'https://porter.nucypher.io', + tapir: 'https://porter-tapir.nucypher.io', + lynx: 'https://porter-lynx.nucypher.io', }; const porterUriSource: string = - 'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/porter_instances.json'; + 'https://raw.githubusercontent.com/nucypher/nucypher-porter/development/porter_instances.json'; export type Domain = keyof typeof defaultPorterUri; From ef5f3f2dbfd692574369cb5ba091155ba2213c79 Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Tue, 30 Jul 2024 10:08:45 -0400 Subject: [PATCH 04/10] Fix lost commit: get porter uris from json --- packages/shared/src/porter.ts | 14 +++++++++----- packages/shared/test/porter.test.ts | 10 ++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index e19b7ef4b..bcf18255f 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -26,6 +26,7 @@ const porterUriSource: string = 'https://raw.githubusercontent.com/nucypher/nucypher-porter/development/porter_instances.json'; export type Domain = keyof typeof defaultPorterUri; +export type PorterURISourceResponse = Record; export const domains: Record = { DEVNET: 'lynx', @@ -40,7 +41,7 @@ export const getPorterUri = async (domain: Domain): Promise => { export const getPorterUris = async ( domain: Domain, porterUris: string[] = [], -): Promise { +): Promise => { const fullList = [...porterUris]; const uri = defaultPorterUri[domain]; if (!uri) { @@ -52,16 +53,19 @@ export const getPorterUris = async ( return fullList; }; -export const getPorterUrisFromSource = async (domain: Domain): Promise => { - const source = porterUriSource[domain]; +export const getPorterUrisFromSource = async ( + domain: Domain, +): Promise => { + const source = porterUriSource; if (!source) { return []; } try { - const resp = await axios.get(source, { + const resp = await axios.get(porterUriSource, { responseType: 'blob', }); - return resp.data.split(/\r?\n/).filter(Boolean); + const uris: PorterURISourceResponse = JSON.parse(resp.data); + return uris[domain]; } catch (e) { return []; } diff --git a/packages/shared/test/porter.test.ts b/packages/shared/test/porter.test.ts index 27ac977b4..e4c7e6373 100644 --- a/packages/shared/test/porter.test.ts +++ b/packages/shared/test/porter.test.ts @@ -5,11 +5,11 @@ import { GetUrsulasResult, PorterClient, Ursula, + domains, + getPorterUris, + getPorterUrisFromSource, initialize, toHexString, - getPorterUrisFromSource, - getPorterUris, - domains } from '../src'; const fakePorterUris = [ @@ -59,9 +59,7 @@ describe('getPorterUris', () => { const uris = await getPorterUrisFromSource(domain); expect(uris.length).toBeGreaterThan(0); const fullList = await getPorterUris(domain); - expect(fullList).toEqual( - expect.arrayContaining(uris) - ); + expect(fullList).toEqual(expect.arrayContaining(uris)); } }); }); From 968d4f1af5c2e9f70933bcbf483d040abd2b2023 Mon Sep 17 00:00:00 2001 From: Victoria Date: Tue, 30 Jul 2024 16:21:55 +0200 Subject: [PATCH 05/10] Update packages/shared/src/porter.ts Co-authored-by: piotr-roslaniec <39299780+piotr-roslaniec@users.noreply.github.com> --- packages/shared/src/porter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index bcf18255f..b01d25e28 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -187,7 +187,7 @@ export class PorterClient { throw lastError; } throw new Error( - 'Porter returns bad response: ${resp.status} - ${resp.data}', + `Porter returns bad response: ${resp.status} - ${resp.data}`, ); } From b5d12e86e153dc0fb54ac743d2ca615118baa2ea Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Tue, 30 Jul 2024 14:14:37 -0400 Subject: [PATCH 06/10] Minor refactor and better test for porter client --- packages/shared/src/porter.ts | 8 ++++---- packages/shared/test/porter.test.ts | 16 ++++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index b01d25e28..1808b4359 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -175,15 +175,15 @@ export class PorterClient { const localConfig = { ...config, baseURL: porterUrl.toString() }; try { resp = await axios.request(localConfig); + if (resp.status === HttpStatusCode.Ok) { + return resp; + } } catch (e) { lastError = e; continue; } - if (resp.status === HttpStatusCode.Ok) { - return resp; - } } - if (lastError !== undefined) { + if (lastError) { throw lastError; } throw new Error( diff --git a/packages/shared/test/porter.test.ts b/packages/shared/test/porter.test.ts index e4c7e6373..ab9455b76 100644 --- a/packages/shared/test/porter.test.ts +++ b/packages/shared/test/porter.test.ts @@ -41,10 +41,10 @@ const mockGetUrsulas = (ursulas: Ursula[] = fakeUrsulas()): SpyInstance => { status: HttpStatusCode.Ok, data: fakePorterUrsulas(ursulas), }); + case fakePorterUris[1]: + return Promise.resolve({ status: HttpStatusCode.BadRequest, data: '' }); case fakePorterUris[0]: - throw new Error(); - default: - throw Promise.resolve({ status: HttpStatusCode.BadRequest }); + throw new Error(`Test error`); } }); }; @@ -107,9 +107,13 @@ describe('PorterClient', () => { it('returns error in case all porters fail', async () => { const ursulas = fakeUrsulas(); mockGetUrsulas(ursulas); - let porterClient = new PorterClient([fakePorterUris[0], fakePorterUris[1]]); - expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(); + let porterClient = new PorterClient([fakePorterUris[1]]); + expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError( + Error(`Porter returns bad response: 400 - `), + ); porterClient = new PorterClient([fakePorterUris[1], fakePorterUris[0]]); - expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(); + expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError( + Error(`Test error`), + ); }); }); From 565eed4329b19600a75a2b4380fcddfd534191f1 Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Fri, 9 Aug 2024 09:51:06 -0400 Subject: [PATCH 07/10] Changes error message when porter returns bad answer --- packages/shared/src/porter.ts | 2 +- packages/shared/test/porter.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index 1808b4359..f00c4da3d 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -187,7 +187,7 @@ export class PorterClient { throw lastError; } throw new Error( - `Porter returns bad response: ${resp.status} - ${resp.data}`, + `Porter returned bad response: ${resp.status} - ${resp.data}`, ); } diff --git a/packages/shared/test/porter.test.ts b/packages/shared/test/porter.test.ts index ab9455b76..19d724667 100644 --- a/packages/shared/test/porter.test.ts +++ b/packages/shared/test/porter.test.ts @@ -109,7 +109,7 @@ describe('PorterClient', () => { mockGetUrsulas(ursulas); let porterClient = new PorterClient([fakePorterUris[1]]); expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError( - Error(`Porter returns bad response: 400 - `), + Error(`Porter returned bad response: 400 - `), ); porterClient = new PorterClient([fakePorterUris[1], fakePorterUris[0]]); expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError( From ee51b90569bbc24773db76fc3bf719d6dee4e1a6 Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Wed, 14 Aug 2024 15:33:56 -0400 Subject: [PATCH 08/10] Change github branch for porter uris from development to main --- packages/shared/src/porter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index f00c4da3d..40617ff13 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -23,7 +23,7 @@ const defaultPorterUri: Record = { }; const porterUriSource: string = - 'https://raw.githubusercontent.com/nucypher/nucypher-porter/development/porter_instances.json'; + 'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/porter_instances.json'; export type Domain = keyof typeof defaultPorterUri; export type PorterURISourceResponse = Record; From 9cad3f4bc1b5eb4a4e915f86d19f3fe67e772913 Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Thu, 15 Aug 2024 10:06:17 -0400 Subject: [PATCH 09/10] If user provided porter URIs then don't use default URIs --- packages/shared/src/porter.ts | 3 +-- packages/taco/src/taco.ts | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index 40617ff13..ac47d5171 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -40,9 +40,8 @@ export const getPorterUri = async (domain: Domain): Promise => { export const getPorterUris = async ( domain: Domain, - porterUris: string[] = [], ): Promise => { - const fullList = [...porterUris]; + const fullList = []; const uri = defaultPorterUri[domain]; if (!uri) { throw new Error(`No default Porter URI found for domain: ${domain}`); diff --git a/packages/taco/src/taco.ts b/packages/taco/src/taco.ts index 62b0f344a..647af3a52 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(s) for the Porter service. If not provided, a value will be obtained + * @param {string[]} [porterUris] - 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, - porterUris: string[] = [], + porterUris?: string[], customParameters?: Record, ): Promise => { - const porterUrisFull: string[] = await getPorterUris(domain, porterUris); + const porterUrisFull: string[] = porterUris ? porterUris : await getPorterUris(domain); const ritualId = await DkgCoordinatorAgent.getRitualIdFromPublicKey( provider, From 7027418ae2a1c82a3befc289e244e331d4e090fa Mon Sep 17 00:00:00 2001 From: Victoria Zotova Date: Thu, 15 Aug 2024 14:47:10 -0400 Subject: [PATCH 10/10] apply pr suggestions --- packages/taco/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/taco/README.md b/packages/taco/README.md index 05c6d97ad..4ec9b2724 100644 --- a/packages/taco/README.md +++ b/packages/taco/README.md @@ -60,7 +60,6 @@ const decryptedMessage = await decrypt( web3Provider, domains.TESTNET, messageKit, - await getPorterUri(domains.TESTNET), web3Provider.getSigner(), ); ```