Skip to content

Commit ae5186f

Browse files
authored
GitHub Porter URIs (#555)
2 parents cf17b4f + 7027418 commit ae5186f

File tree

9 files changed

+72
-29
lines changed

9 files changed

+72
-29
lines changed

examples/pre/nextjs/src/app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function App() {
109109
provider,
110110
provider.getSigner(),
111111
domains.TESTNET,
112-
getPorterUri(domains.TESTNET),
112+
await getPorterUri(domains.TESTNET),
113113
policyParams,
114114
);
115115

examples/pre/nodejs/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const runExample = async () => {
7373
provider,
7474
signer,
7575
domains.TESTNET,
76-
getPorterUri(domains.TESTNET),
76+
await getPorterUri(domains.TESTNET),
7777
policyParams,
7878
);
7979

examples/pre/react/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function App() {
105105
provider,
106106
provider.getSigner(),
107107
domains.TESTNET,
108-
getPorterUri(domains.TESTNET),
108+
await getPorterUri(domains.TESTNET),
109109
policyParams,
110110
);
111111

examples/pre/webpack-5/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const runExample = async () => {
8282
provider,
8383
provider.getSigner(),
8484
domains.TESTNET,
85-
getPorterUri(domains.TESTNET),
85+
await getPorterUri(domains.TESTNET),
8686
policyParams,
8787
);
8888

examples/taco/webpack-5/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
EIP4361AuthProvider,
66
encrypt,
77
fromBytes,
8-
getPorterUris,
98
initialize,
109
toBytes,
1110
} from '@nucypher/taco';

packages/shared/src/porter.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,59 @@ import { Base64EncodedBytes, ChecksumAddress, HexEncodedBytes } from './types';
1717
import { fromBase64, fromHexString, toBase64, toHexString } from './utils';
1818

1919
const defaultPorterUri: Record<string, string> = {
20-
mainnet: 'https://porter.nucypher.community',
21-
tapir: 'https://porter-tapir.nucypher.community',
22-
lynx: 'https://porter-lynx.nucypher.community',
20+
mainnet: 'https://porter.nucypher.io',
21+
tapir: 'https://porter-tapir.nucypher.io',
22+
lynx: 'https://porter-lynx.nucypher.io',
2323
};
2424

25+
const porterUriSource: string =
26+
'https://raw.githubusercontent.com/nucypher/nucypher-porter/main/porter_instances.json';
27+
2528
export type Domain = keyof typeof defaultPorterUri;
29+
export type PorterURISourceResponse = Record<string, string[]>;
2630

2731
export const domains: Record<string, Domain> = {
2832
DEVNET: 'lynx',
2933
TESTNET: 'tapir',
3034
MAINNET: 'mainnet',
3135
};
3236

33-
export const getPorterUri = (domain: Domain): string => {
34-
return getPorterUris(domain)[0];
37+
export const getPorterUri = async (domain: Domain): Promise<string> => {
38+
return (await getPorterUris(domain))[0];
3539
};
3640

37-
export const getPorterUris = (
41+
export const getPorterUris = async (
3842
domain: Domain,
39-
porterUris: string[] = [],
40-
): string[] => {
41-
const fullList = [...porterUris];
43+
): Promise<string[]> => {
44+
const fullList = [];
4245
const uri = defaultPorterUri[domain];
4346
if (!uri) {
4447
throw new Error(`No default Porter URI found for domain: ${domain}`);
4548
}
4649
fullList.push(uri);
50+
const urisFromSource = await getPorterUrisFromSource(domain);
51+
fullList.push(...urisFromSource);
4752
return fullList;
4853
};
4954

55+
export const getPorterUrisFromSource = async (
56+
domain: Domain,
57+
): Promise<string[]> => {
58+
const source = porterUriSource;
59+
if (!source) {
60+
return [];
61+
}
62+
try {
63+
const resp = await axios.get(porterUriSource, {
64+
responseType: 'blob',
65+
});
66+
const uris: PorterURISourceResponse = JSON.parse(resp.data);
67+
return uris[domain];
68+
} catch (e) {
69+
return [];
70+
}
71+
};
72+
5073
// /get_ursulas
5174

5275
export type Ursula = {
@@ -151,19 +174,19 @@ export class PorterClient {
151174
const localConfig = { ...config, baseURL: porterUrl.toString() };
152175
try {
153176
resp = await axios.request(localConfig);
177+
if (resp.status === HttpStatusCode.Ok) {
178+
return resp;
179+
}
154180
} catch (e) {
155181
lastError = e;
156182
continue;
157183
}
158-
if (resp.status === HttpStatusCode.Ok) {
159-
return resp;
160-
}
161184
}
162-
if (lastError !== undefined) {
185+
if (lastError) {
163186
throw lastError;
164187
}
165188
throw new Error(
166-
'Porter returns bad response: ${resp.status} - ${resp.data}',
189+
`Porter returned bad response: ${resp.status} - ${resp.data}`,
167190
);
168191
}
169192

packages/shared/test/porter.test.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
GetUrsulasResult,
66
PorterClient,
77
Ursula,
8+
domains,
9+
getPorterUris,
10+
getPorterUrisFromSource,
811
initialize,
912
toHexString,
1013
} from '../src';
@@ -38,14 +41,29 @@ const mockGetUrsulas = (ursulas: Ursula[] = fakeUrsulas()): SpyInstance => {
3841
status: HttpStatusCode.Ok,
3942
data: fakePorterUrsulas(ursulas),
4043
});
44+
case fakePorterUris[1]:
45+
return Promise.resolve({ status: HttpStatusCode.BadRequest, data: '' });
4146
case fakePorterUris[0]:
42-
throw new Error();
43-
default:
44-
throw Promise.resolve({ status: HttpStatusCode.BadRequest });
47+
throw new Error(`Test error`);
4548
}
4649
});
4750
};
4851

52+
describe('getPorterUris', () => {
53+
beforeAll(async () => {
54+
await initialize();
55+
});
56+
57+
it('Get URIs from source', async () => {
58+
for (const domain of Object.values(domains)) {
59+
const uris = await getPorterUrisFromSource(domain);
60+
expect(uris.length).toBeGreaterThan(0);
61+
const fullList = await getPorterUris(domain);
62+
expect(fullList).toEqual(expect.arrayContaining(uris));
63+
}
64+
});
65+
});
66+
4967
describe('PorterClient', () => {
5068
beforeAll(async () => {
5169
await initialize();
@@ -89,9 +107,13 @@ describe('PorterClient', () => {
89107
it('returns error in case all porters fail', async () => {
90108
const ursulas = fakeUrsulas();
91109
mockGetUrsulas(ursulas);
92-
let porterClient = new PorterClient([fakePorterUris[0], fakePorterUris[1]]);
93-
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError();
110+
let porterClient = new PorterClient([fakePorterUris[1]]);
111+
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(
112+
Error(`Porter returned bad response: 400 - `),
113+
);
94114
porterClient = new PorterClient([fakePorterUris[1], fakePorterUris[0]]);
95-
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError();
115+
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(
116+
Error(`Test error`),
117+
);
96118
});
97119
});

packages/taco/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ const decryptedMessage = await decrypt(
6060
web3Provider,
6161
domains.TESTNET,
6262
messageKit,
63-
getPorterUri(domains.TESTNET),
6463
web3Provider.getSigner(),
6564
);
6665
```

packages/taco/src/taco.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export const encryptWithPublicKey = async (
130130
* Must match the `ritualId`.
131131
* @param {ThresholdMessageKit} messageKit - The kit containing the message to be decrypted
132132
* @param authProvider - The authentication provider that will be used to provide the authorization
133-
* @param {string} [porterUri] - The URI(s) for the Porter service. If not provided, a value will be obtained
133+
* @param {string[]} [porterUris] - The URI(s) for the Porter service. If not provided, a value will be obtained
134134
* from the Domain
135135
* @param {Record<string, CustomContextParam>} [customParameters] - Optional custom parameters that may be required
136136
* depending on the condition used
@@ -145,10 +145,10 @@ export const decrypt = async (
145145
domain: Domain,
146146
messageKit: ThresholdMessageKit,
147147
authProvider?: EIP4361AuthProvider,
148-
porterUris: string[] = [],
148+
porterUris?: string[],
149149
customParameters?: Record<string, CustomContextParam>,
150150
): Promise<Uint8Array> => {
151-
const porterUrisFull: string[] = getPorterUris(domain, porterUris);
151+
const porterUrisFull: string[] = porterUris ? porterUris : await getPorterUris(domain);
152152

153153
const ritualId = await DkgCoordinatorAgent.getRitualIdFromPublicKey(
154154
provider,

0 commit comments

Comments
 (0)