Skip to content

Commit

Permalink
feature(porter): add porter domains for taco and pre
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Oct 6, 2023
1 parent 0a13562 commit 8cf7e21
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 43 deletions.
3 changes: 2 additions & 1 deletion examples/pre/nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
EnactedPolicy,
getPorterUri,
initialize,
PRE_DOMAIN,
SecretKey,
toHexString,
} from '@nucypher/pre';
Expand Down Expand Up @@ -106,7 +107,7 @@ function App() {
const policy = await alice.grant(
provider,
provider.getSigner(),
getPorterUri('tapir'), // Testnet porter
getPorterUri(PRE_DOMAIN.TESTNET),
policyParams,
);

Expand Down
3 changes: 2 additions & 1 deletion examples/pre/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Bob,
getPorterUri,
initialize,
PRE_DOMAIN,
SecretKey,
toBytes,
} from '@nucypher/pre';
Expand Down Expand Up @@ -58,7 +59,7 @@ const runExample = async () => {
startDate: new Date(),
endDate: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30), // In 30 days,
};
const porterUri = getPorterUri('tapir'); // Test network
const porterUri = getPorterUri(PRE_DOMAIN.TESTNET);
const alice = makeAlice();

console.log('Creating policy...');
Expand Down
8 changes: 3 additions & 5 deletions examples/pre/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
EnactedPolicy,
getPorterUri,
initialize,
PRE_DOMAIN,
SecretKey,
toHexString,
toHexString
} from '@nucypher/pre';
import { ethers } from 'ethers';
import { useEffect, useState } from 'react';
Expand Down Expand Up @@ -48,8 +49,6 @@ function App() {
return <div>Loading...</div>;
}

console.log({ Alice, Bob, getPorterUri, SecretKey, toHexString });

const makeAlice = () => {
const alice = Alice.fromSecretKey(SecretKey.random());
setAlice(alice);
Expand Down Expand Up @@ -96,11 +95,10 @@ function App() {
startDate,
endDate,
};

const policy = await alice.grant(
provider,
provider.getSigner(),
getPorterUri('tapir'), // Testnet porter
getPorterUri(PRE_DOMAIN.TESTNET),
policyParams,
);

Expand Down
11 changes: 9 additions & 2 deletions examples/pre/webpack-5/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Alice, Bob, SecretKey, getPorterUri, initialize } from '@nucypher/pre';
import {
Alice,
Bob,
PRE_DOMAIN,
SecretKey,
getPorterUri,
initialize,
} from '@nucypher/pre';
import { ethers } from 'ethers';

declare global {
Expand Down Expand Up @@ -58,7 +65,7 @@ const runExample = async () => {
startDate,
endDate,
};
const porterUri = getPorterUri('tapir'); // Test network
const porterUri = getPorterUri(PRE_DOMAIN.TESTNET);

const alice = makeAlice();
const policy = await alice.grant(
Expand Down
5 changes: 3 additions & 2 deletions examples/taco/nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
encrypt, fromBytes,
getPorterUri,
initialize,
TACO_DOMAIN
} from '@nucypher/taco';
import {ethers} from 'ethers';
import {useEffect, useState} from 'react';
Expand Down Expand Up @@ -89,8 +90,8 @@ function App() {
const messageKit = await encrypt(provider, message, hasPositiveBalance, ritualId, signer);

console.log('Decrypting message...');
const porterUri = getPorterUri('lynx'); // Test network
const decryptedMessage = await decrypt(provider, messageKit, signer, porterUri);
const porterUri = getPorterUri(TACO_DOMAIN.DEV);
const decryptedMessage = await decrypt(provider, messageKit,porterUri,signer);

setDecryptedMessage(fromBytes(decryptedMessage));
};
Expand Down
5 changes: 3 additions & 2 deletions examples/taco/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fromBytes,
getPorterUri,
initialize,
TACO_DOMAIN,
toBytes,
} from '@nucypher/taco';
import * as dotenv from 'dotenv';
Expand Down Expand Up @@ -56,8 +57,8 @@ const runExample = async () => {
);

console.log('Decrypting message...');
const porterUri = getPorterUri('lynx'); // Test network
const decryptedBytes = await decrypt(provider, messageKit, signer, porterUri);
const porterUri = getPorterUri(TACO_DOMAIN.DEV);
const decryptedBytes = await decrypt(provider, messageKit, porterUri, signer);
const decryptedMessage = fromBytes(decryptedBytes);
console.log('Decrypted message:', decryptedMessage);
};
Expand Down
5 changes: 3 additions & 2 deletions examples/taco/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fromBytes,
getPorterUri,
initialize,
TACO_DOMAIN,
} from '@nucypher/taco';
import {ethers} from 'ethers';
import {useEffect, useState} from 'react';
Expand Down Expand Up @@ -89,8 +90,8 @@ function App() {
const messageKit = await encrypt(provider, message, hasPositiveBalance, ritualId, signer);

console.log('Decrypting message...');
const porterUri = getPorterUri('lynx'); // Test network
const decryptedMessage = await decrypt(provider, messageKit, signer, porterUri);
const porterUri = getPorterUri(TACO_DOMAIN.DEV);
const decryptedMessage = await decrypt(provider, messageKit, porterUri, signer);

setDecryptedMessage(fromBytes(decryptedMessage));
};
Expand Down
5 changes: 3 additions & 2 deletions examples/taco/webpack-5/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fromBytes,
getPorterUri,
initialize,
TACO_DOMAIN,
toBytes,
} from '@nucypher/taco';
import { ethers } from 'ethers';
Expand Down Expand Up @@ -47,8 +48,8 @@ const runExample = async () => {
);

console.log('Decrypting message...');
const porterUri = getPorterUri('lynx'); // Test network
const decryptedBytes = await decrypt(provider, messageKit, signer, porterUri);
const porterUri = getPorterUri(TACO_DOMAIN.DEV);
const decryptedBytes = await decrypt(provider, messageKit, porterUri, signer);
const decryptedMessage = fromBytes(decryptedBytes);
console.log('Decrypted message:', decryptedMessage);
};
Expand Down
9 changes: 1 addition & 8 deletions packages/pre/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
export {
PorterClient,
fromHexString,
getPorterUri,
toBytes,
toHexString,
} from '@nucypher/shared';

export {
Ciphertext,
EncryptedTreasureMap,
Expand All @@ -21,3 +13,4 @@ export {
export { Alice, Bob, Enrico } from './characters';
export { Cohort } from './cohort';
export { EnactedPolicy } from './policy';
export * from './porter';
17 changes: 17 additions & 0 deletions packages/pre/src/porter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { getPorterUri as doGetPorterUri } from '@nucypher/shared';

type PreDomain = 'oryx';

export const PRE_DOMAIN: Record<string, PreDomain> = {
TESTNET: 'oryx',
};

export const getPorterUri = (domain: PreDomain): string => {
const preDomainKeys = Object.keys(PRE_DOMAIN);
if (!preDomainKeys.includes(domain)) {
throw new Error(`${domain} is not a valid PRE domain.`);
}
return doGetPorterUri(domain);
};

export { fromBytes, initialize, toBytes, toHexString } from '@nucypher/shared';
11 changes: 5 additions & 6 deletions packages/shared/src/porter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ import qs from 'qs';
import { Base64EncodedBytes, ChecksumAddress, HexEncodedBytes } from './types';
import { fromBase64, fromHexString, toBase64, toHexString } from './utils';

type Network = 'mainnet' | 'tapir' | 'oryx' | 'lynx';

const PORTER_URIS: Record<Network, string> = {
const porterUri: 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 PorterNetwork = keyof typeof porterUri;

export const getPorterUri = (network: Network): string => {
const uri = PORTER_URIS[network];
export const getPorterUri = (network: PorterNetwork): string => {
const uri = porterUri[network];
if (!uri) {
throw new Error(`No default Porter URI found for network: ${network}`);
}
return PORTER_URIS[network];
return porterUri[network];
};

// /get_ursulas
Expand Down
6 changes: 2 additions & 4 deletions packages/taco/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export { DkgPublicKey, ThresholdMessageKit } from '@nucypher/nucypher-core';

export { fromBytes, getPorterUri, initialize, toBytes } from '@nucypher/shared';

export * as conditions from './conditions';

export { decrypt, encrypt } from './taco';
export * from './porter';
export * from './taco';
18 changes: 18 additions & 0 deletions packages/taco/src/porter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getPorterUri as doGetPorterUri } from '@nucypher/shared';

type TacoDomain = 'lynx' | 'tapir';

export const TACO_DOMAIN: Record<string, TacoDomain> = {
DEV: 'lynx',
TESTNET: 'tapir',
};

export const getPorterUri = (domain: TacoDomain): string => {
const tacoDomainKeys = Object.keys(TACO_DOMAIN);
if (!tacoDomainKeys.includes(domain)) {
throw new Error(`${domain} is not a valid TACo domain.`);
}
return doGetPorterUri(domain);
};

export { fromBytes, initialize, toBytes, toHexString } from '@nucypher/shared';
9 changes: 2 additions & 7 deletions packages/taco/src/taco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import {
encryptForDkg,
ThresholdMessageKit,
} from '@nucypher/nucypher-core';
import {
DkgCoordinatorAgent,
fromHexString,
getPorterUri,
toBytes,
} from '@nucypher/shared';
import { DkgCoordinatorAgent, fromHexString, toBytes } from '@nucypher/shared';
import { ethers } from 'ethers';
import { keccak256 } from 'ethers/lib/utils';

Expand Down Expand Up @@ -77,8 +72,8 @@ export const encryptWithPublicKey = async (
export const decrypt = async (
provider: ethers.providers.Provider,
messageKit: ThresholdMessageKit,
porterUri: string,
signer?: ethers.Signer,
porterUri = getPorterUri('tapir'),
): Promise<Uint8Array> => {
const ritualId = await DkgCoordinatorAgent.getRitualIdFromPublicKey(
provider,
Expand Down
2 changes: 1 addition & 1 deletion packages/taco/test/taco.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ describe('taco', () => {
const decryptedMessage = await taco.decrypt(
provider,
messageKit,
signer,
fakePorterUri,
signer,
);
expect(getParticipantsSpy).toHaveBeenCalled();
expect(sessionKeySpy).toHaveBeenCalled();
Expand Down

0 comments on commit 8cf7e21

Please sign in to comment.