From 8cf7e214b7321b07fce6bbcd20dc914a24dd37b5 Mon Sep 17 00:00:00 2001
From: Piotr Roslaniec
Date: Thu, 5 Oct 2023 15:30:47 +0200
Subject: [PATCH] feature(porter): add porter domains for taco and pre
---
examples/pre/nextjs/src/app/page.tsx | 3 ++-
examples/pre/nodejs/src/index.ts | 3 ++-
examples/pre/react/src/App.tsx | 8 +++-----
examples/pre/webpack-5/src/index.ts | 11 +++++++++--
examples/taco/nextjs/src/app/page.tsx | 5 +++--
examples/taco/nodejs/src/index.ts | 5 +++--
examples/taco/react/src/App.tsx | 5 +++--
examples/taco/webpack-5/src/index.ts | 5 +++--
packages/pre/src/index.ts | 9 +--------
packages/pre/src/porter.ts | 17 +++++++++++++++++
packages/shared/src/porter.ts | 11 +++++------
packages/taco/src/index.ts | 6 ++----
packages/taco/src/porter.ts | 18 ++++++++++++++++++
packages/taco/src/taco.ts | 9 ++-------
packages/taco/test/taco.test.ts | 2 +-
15 files changed, 74 insertions(+), 43 deletions(-)
create mode 100644 packages/pre/src/porter.ts
create mode 100644 packages/taco/src/porter.ts
diff --git a/examples/pre/nextjs/src/app/page.tsx b/examples/pre/nextjs/src/app/page.tsx
index 79648862b..65149bd36 100644
--- a/examples/pre/nextjs/src/app/page.tsx
+++ b/examples/pre/nextjs/src/app/page.tsx
@@ -5,6 +5,7 @@ import {
EnactedPolicy,
getPorterUri,
initialize,
+ PRE_DOMAIN,
SecretKey,
toHexString,
} from '@nucypher/pre';
@@ -106,7 +107,7 @@ function App() {
const policy = await alice.grant(
provider,
provider.getSigner(),
- getPorterUri('tapir'), // Testnet porter
+ getPorterUri(PRE_DOMAIN.TESTNET),
policyParams,
);
diff --git a/examples/pre/nodejs/src/index.ts b/examples/pre/nodejs/src/index.ts
index 7cb42d360..5006fa65a 100644
--- a/examples/pre/nodejs/src/index.ts
+++ b/examples/pre/nodejs/src/index.ts
@@ -3,6 +3,7 @@ import {
Bob,
getPorterUri,
initialize,
+ PRE_DOMAIN,
SecretKey,
toBytes,
} from '@nucypher/pre';
@@ -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...');
diff --git a/examples/pre/react/src/App.tsx b/examples/pre/react/src/App.tsx
index d4901ad2b..3c3de670f 100644
--- a/examples/pre/react/src/App.tsx
+++ b/examples/pre/react/src/App.tsx
@@ -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';
@@ -48,8 +49,6 @@ function App() {
return Loading...
;
}
- console.log({ Alice, Bob, getPorterUri, SecretKey, toHexString });
-
const makeAlice = () => {
const alice = Alice.fromSecretKey(SecretKey.random());
setAlice(alice);
@@ -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,
);
diff --git a/examples/pre/webpack-5/src/index.ts b/examples/pre/webpack-5/src/index.ts
index 3417a426d..f1dfa348f 100644
--- a/examples/pre/webpack-5/src/index.ts
+++ b/examples/pre/webpack-5/src/index.ts
@@ -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 {
@@ -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(
diff --git a/examples/taco/nextjs/src/app/page.tsx b/examples/taco/nextjs/src/app/page.tsx
index c9bcea875..a7e3da8fa 100644
--- a/examples/taco/nextjs/src/app/page.tsx
+++ b/examples/taco/nextjs/src/app/page.tsx
@@ -5,6 +5,7 @@ import {
encrypt, fromBytes,
getPorterUri,
initialize,
+ TACO_DOMAIN
} from '@nucypher/taco';
import {ethers} from 'ethers';
import {useEffect, useState} from 'react';
@@ -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));
};
diff --git a/examples/taco/nodejs/src/index.ts b/examples/taco/nodejs/src/index.ts
index ad00311b5..a7062a41c 100644
--- a/examples/taco/nodejs/src/index.ts
+++ b/examples/taco/nodejs/src/index.ts
@@ -5,6 +5,7 @@ import {
fromBytes,
getPorterUri,
initialize,
+ TACO_DOMAIN,
toBytes,
} from '@nucypher/taco';
import * as dotenv from 'dotenv';
@@ -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);
};
diff --git a/examples/taco/react/src/App.tsx b/examples/taco/react/src/App.tsx
index 4073fb53e..67691a937 100644
--- a/examples/taco/react/src/App.tsx
+++ b/examples/taco/react/src/App.tsx
@@ -5,6 +5,7 @@ import {
fromBytes,
getPorterUri,
initialize,
+ TACO_DOMAIN,
} from '@nucypher/taco';
import {ethers} from 'ethers';
import {useEffect, useState} from 'react';
@@ -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));
};
diff --git a/examples/taco/webpack-5/src/index.ts b/examples/taco/webpack-5/src/index.ts
index 370eada34..3554c8e76 100644
--- a/examples/taco/webpack-5/src/index.ts
+++ b/examples/taco/webpack-5/src/index.ts
@@ -5,6 +5,7 @@ import {
fromBytes,
getPorterUri,
initialize,
+ TACO_DOMAIN,
toBytes,
} from '@nucypher/taco';
import { ethers } from 'ethers';
@@ -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);
};
diff --git a/packages/pre/src/index.ts b/packages/pre/src/index.ts
index 4f676b1ca..9b4a13d95 100644
--- a/packages/pre/src/index.ts
+++ b/packages/pre/src/index.ts
@@ -1,11 +1,3 @@
-export {
- PorterClient,
- fromHexString,
- getPorterUri,
- toBytes,
- toHexString,
-} from '@nucypher/shared';
-
export {
Ciphertext,
EncryptedTreasureMap,
@@ -21,3 +13,4 @@ export {
export { Alice, Bob, Enrico } from './characters';
export { Cohort } from './cohort';
export { EnactedPolicy } from './policy';
+export * from './porter';
diff --git a/packages/pre/src/porter.ts b/packages/pre/src/porter.ts
new file mode 100644
index 000000000..8e244265a
--- /dev/null
+++ b/packages/pre/src/porter.ts
@@ -0,0 +1,17 @@
+import { getPorterUri as doGetPorterUri } from '@nucypher/shared';
+
+type PreDomain = 'oryx';
+
+export const PRE_DOMAIN: Record = {
+ 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';
diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts
index 175355766..816cc077a 100644
--- a/packages/shared/src/porter.ts
+++ b/packages/shared/src/porter.ts
@@ -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 = {
+const porterUri: Record = {
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
diff --git a/packages/taco/src/index.ts b/packages/taco/src/index.ts
index b1c36b574..617e917b2 100644
--- a/packages/taco/src/index.ts
+++ b/packages/taco/src/index.ts
@@ -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';
diff --git a/packages/taco/src/porter.ts b/packages/taco/src/porter.ts
new file mode 100644
index 000000000..6572db214
--- /dev/null
+++ b/packages/taco/src/porter.ts
@@ -0,0 +1,18 @@
+import { getPorterUri as doGetPorterUri } from '@nucypher/shared';
+
+type TacoDomain = 'lynx' | 'tapir';
+
+export const TACO_DOMAIN: Record = {
+ 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';
diff --git a/packages/taco/src/taco.ts b/packages/taco/src/taco.ts
index 3a23bd132..317f1fb8e 100644
--- a/packages/taco/src/taco.ts
+++ b/packages/taco/src/taco.ts
@@ -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';
@@ -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 => {
const ritualId = await DkgCoordinatorAgent.getRitualIdFromPublicKey(
provider,
diff --git a/packages/taco/test/taco.test.ts b/packages/taco/test/taco.test.ts
index 6a62e1fdd..ba615f71a 100644
--- a/packages/taco/test/taco.test.ts
+++ b/packages/taco/test/taco.test.ts
@@ -81,8 +81,8 @@ describe('taco', () => {
const decryptedMessage = await taco.decrypt(
provider,
messageKit,
- signer,
fakePorterUri,
+ signer,
);
expect(getParticipantsSpy).toHaveBeenCalled();
expect(sessionKeySpy).toHaveBeenCalled();