Skip to content

Commit

Permalink
PorterClient: combine default and specified porter URI
Browse files Browse the repository at this point in the history
  • Loading branch information
vzotova committed Jul 24, 2024
1 parent ddc24ac commit 7134edb
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 30 deletions.
3 changes: 1 addition & 2 deletions demos/taco-demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
decrypt,
domains,
encrypt,
getPorterUri,
initialize,
ThresholdMessageKit,
toHexString,
Expand Down Expand Up @@ -89,7 +88,7 @@ export default function App() {
provider,
domain,
encryptedMessage,
getPorterUri(domain),
undefined,
provider.getSigner(),
);

Expand Down
5 changes: 2 additions & 3 deletions demos/taco-nft-demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
decrypt,
domains,
encrypt,
getPorterUri,
initialize,
ThresholdMessageKit,
} from '@nucypher/taco';
Expand Down Expand Up @@ -78,8 +77,8 @@ export default function App() {
provider,
domain,
encryptedMessage,
getPorterUri(domain),
provider.getSigner(),
undefined,
provider.getSigner()
);

setDecryptedMessage(new TextDecoder().decode(decryptedMessage));
Expand Down
2 changes: 0 additions & 2 deletions examples/taco/nextjs/src/hooks/useTaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Domain,
EIP4361AuthProvider,
encrypt,
getPorterUri,
initialize,
ThresholdMessageKit,
} from '@nucypher/taco';
Expand Down Expand Up @@ -38,7 +37,6 @@ export default function useTaco({
domain,
messageKit,
authProvider,
getPorterUri(domain),
);
},
[isInit, provider, domain],
Expand Down
2 changes: 0 additions & 2 deletions examples/taco/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
EIP4361AuthProvider,
encrypt,
fromBytes,
getPorterUri,
initialize,
ThresholdMessageKit,
toBytes,
Expand Down Expand Up @@ -99,7 +98,6 @@ const decryptFromBytes = async (encryptedBytes: Uint8Array) => {
domain,
messageKit,
authProvider,
getPorterUri(domain),
);
};

Expand Down
2 changes: 0 additions & 2 deletions examples/taco/react/src/hooks/useTaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Domain,
EIP4361AuthProvider,
encrypt,
getPorterUri,
initialize,
ThresholdMessageKit,
} from '@nucypher/taco';
Expand Down Expand Up @@ -38,7 +37,6 @@ export default function useTaco({
domain,
messageKit,
authProvider,
getPorterUri(domain),
);
},
[isInit, provider, domain],
Expand Down
3 changes: 1 addition & 2 deletions examples/taco/webpack-5/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
EIP4361AuthProvider,
encrypt,
fromBytes,
getPorterUri,
getPorterUris,
initialize,
toBytes,
} from '@nucypher/taco';
Expand Down Expand Up @@ -67,7 +67,6 @@ const runExample = async () => {
domain,
messageKit,
authProvider,
getPorterUri(domain),
);
const decryptedMessage = fromBytes(decryptedBytes);
console.log('Decrypted message:', decryptedMessage);
Expand Down
19 changes: 14 additions & 5 deletions packages/shared/src/porter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import qs from 'qs';
import { Base64EncodedBytes, ChecksumAddress, HexEncodedBytes } from './types';
import { fromBase64, fromHexString, toBase64, toHexString } from './utils';

const porterUri: Record<string, string> = {
const defaultPorterUri: Record<string, string> = {
mainnet: 'https://porter.nucypher.community',
tapir: 'https://porter-tapir.nucypher.community',
oryx: 'https://porter-oryx.nucypher.community',
lynx: 'https://porter-lynx.nucypher.community',
};

export type Domain = keyof typeof porterUri;
export type Domain = keyof typeof defaultPorterUri;

export const domains: Record<string, Domain> = {
DEVNET: 'lynx',
Expand All @@ -32,11 +32,20 @@ export const domains: Record<string, Domain> = {
};

export const getPorterUri = (domain: Domain): string => {
const uri = porterUri[domain];
return getPorterUris(domain)[0];
};

export const getPorterUris = (domain: Domain, porterUri?: string): string[] => {
const porterUris: string[] = [];
if (porterUri) {
porterUris.push(porterUri);
}
const uri = defaultPorterUri[domain];
if (!uri) {
throw new Error(`No default Porter URI found for domain: ${domain}`);
}
return porterUri[domain];
porterUris.push(uri);
return porterUris;
};

// /get_ursulas
Expand Down Expand Up @@ -126,7 +135,7 @@ export type TacoDecryptResult = {
export class PorterClient {
readonly porterUrls: URL[];

constructor(porterUris: string | [string, ...[string]]) {
constructor(porterUris: string | string[]) {
if (porterUris instanceof Array) {
this.porterUrls = porterUris.map((uri) => new URL(uri));
} else {
Expand Down
2 changes: 0 additions & 2 deletions packages/taco/examples/encrypt-decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
domains,
EIP4361AuthProvider,
encrypt,
getPorterUri,
initialize,
ThresholdMessageKit,
toBytes,
Expand Down Expand Up @@ -55,7 +54,6 @@ const run = async () => {
domains.TESTNET,
messageKit,
authProvider,
getPorterUri(domains.TESTNET),
);
return decryptedMessage;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/taco/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export {
Domain,
domains,
fromBytes,
getPorterUri,
getPorterUris,
initialize,
toBytes,
toHexString,
Expand Down
8 changes: 3 additions & 5 deletions packages/taco/src/taco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
DkgCoordinatorAgent,
Domain,
fromHexString,
getPorterUri,
getPorterUris,
GlobalAllowListAgent,
toBytes,
} from '@nucypher/shared';
Expand Down Expand Up @@ -148,9 +148,7 @@ export const decrypt = async (
porterUri?: string,
customParameters?: Record<string, CustomContextParam>,
): Promise<Uint8Array> => {
if (!porterUri) {
porterUri = getPorterUri(domain);
}
const porterUris: string[] = getPorterUris(domain, porterUri);

const ritualId = await DkgCoordinatorAgent.getRitualIdFromPublicKey(
provider,
Expand All @@ -166,7 +164,7 @@ export const decrypt = async (
return retrieveAndDecrypt(
provider,
domain,
porterUri,
porterUris,
messageKit,
ritualId,
ritual.sharesNum,
Expand Down
8 changes: 4 additions & 4 deletions packages/taco/src/tdec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const encryptMessage = async (
export const retrieveAndDecrypt = async (
provider: ethers.providers.Provider,
domain: Domain,
porterUri: string,
porterUris: string[],
thresholdMessageKit: ThresholdMessageKit,
ritualId: number,
sharesNum: number,
Expand All @@ -72,7 +72,7 @@ export const retrieveAndDecrypt = async (
const decryptionShares = await retrieve(
provider,
domain,
porterUri,
porterUris,
thresholdMessageKit,
ritualId,
sharesNum,
Expand All @@ -88,7 +88,7 @@ export const retrieveAndDecrypt = async (
const retrieve = async (
provider: ethers.providers.Provider,
domain: Domain,
porterUri: string,
porterUris: string[],
thresholdMessageKit: ThresholdMessageKit,
ritualId: number,
sharesNum: number,
Expand All @@ -114,7 +114,7 @@ const retrieve = async (
thresholdMessageKit,
);

const porter = new PorterClient(porterUri);
const porter = new PorterClient(porterUris);
const { encryptedResponses, errors } = await porter.tacoDecrypt(
encryptedRequests,
threshold,
Expand Down

0 comments on commit 7134edb

Please sign in to comment.