@@ -17,36 +17,59 @@ import { Base64EncodedBytes, ChecksumAddress, HexEncodedBytes } from './types';
1717import { fromBase64 , fromHexString , toBase64 , toHexString } from './utils' ;
1818
1919const 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+
2528export type Domain = keyof typeof defaultPorterUri ;
29+ export type PorterURISourceResponse = Record < string , string [ ] > ;
2630
2731export 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
5275export 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
0 commit comments