Skip to content

Commit

Permalink
Get default porter uris from github links
Browse files Browse the repository at this point in the history
  • Loading branch information
vzotova committed Jul 25, 2024
1 parent f0027ab commit 23d7146
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/pre/nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function App() {
provider,
provider.getSigner(),
domains.TESTNET,
getPorterUri(domains.TESTNET),
await getPorterUri(domains.TESTNET),
policyParams,
);

Expand Down
2 changes: 1 addition & 1 deletion examples/pre/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const runExample = async () => {
provider,
signer,
domains.TESTNET,
getPorterUri(domains.TESTNET),
await getPorterUri(domains.TESTNET),
policyParams,
);

Expand Down
2 changes: 1 addition & 1 deletion examples/pre/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function App() {
provider,
provider.getSigner(),
domains.TESTNET,
getPorterUri(domains.TESTNET),
await getPorterUri(domains.TESTNET),
policyParams,
);

Expand Down
2 changes: 1 addition & 1 deletion examples/pre/webpack-5/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const runExample = async () => {
provider,
provider.getSigner(),
domains.TESTNET,
getPorterUri(domains.TESTNET),
await getPorterUri(domains.TESTNET),
policyParams,
);

Expand Down
1 change: 0 additions & 1 deletion examples/taco/webpack-5/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
EIP4361AuthProvider,
encrypt,
fromBytes,
getPorterUris,
initialize,
toBytes,
} from '@nucypher/taco';
Expand Down
29 changes: 26 additions & 3 deletions packages/shared/src/porter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const defaultPorterUri: Record<string, string> = {
lynx: 'https://porter-lynx.nucypher.community',
};

const porterUriSource: Record<string, string> = {
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<string, Domain> = {
Expand All @@ -31,20 +37,37 @@ export const domains: Record<string, Domain> = {
MAINNET: 'mainnet',
};

export const getPorterUri = (domain: Domain): string => {
return getPorterUris(domain)[0];
export const getPorterUri = async (domain: Domain): Promise<string> => {
return (await getPorterUris(domain))[0];
};

export const getPorterUris = (domain: Domain, porterUris: string[] = []): string[] => {
export const getPorterUris = async (domain: Domain, porterUris: string[] = []): Promise<string[]> => {
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<string[]> => {
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 = {
Expand Down
20 changes: 20 additions & 0 deletions packages/shared/test/porter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
PorterClient,
toHexString,
Ursula,
getPorterUrisFromSource,
getPorterUris,
domains
} from '../src';
import { fakeUrsulas } from '@nucypher/test-utils';
import { beforeAll, describe, expect, SpyInstance, it, vi } from 'vitest';
Expand Down Expand Up @@ -41,6 +44,23 @@ const mockGetUrsulas = (
});
};

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();
Expand Down
2 changes: 1 addition & 1 deletion packages/taco/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const decryptedMessage = await decrypt(
web3Provider,
domains.TESTNET,
messageKit,
getPorterUri(domains.TESTNET),
await getPorterUri(domains.TESTNET),
web3Provider.getSigner(),
);
```
Expand Down
4 changes: 2 additions & 2 deletions packages/taco/src/taco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, CustomContextParam>} [customParameters] - Optional custom parameters that may be required
* depending on the condition used
Expand All @@ -148,7 +148,7 @@ export const decrypt = async (
porterUris: string[] = [],
customParameters?: Record<string, CustomContextParam>,
): Promise<Uint8Array> => {
const porterUrisFull: string[] = getPorterUris(domain, porterUris);
const porterUrisFull: string[] = await getPorterUris(domain, porterUris);

const ritualId = await DkgCoordinatorAgent.getRitualIdFromPublicKey(
provider,
Expand Down

0 comments on commit 23d7146

Please sign in to comment.