Skip to content

Commit

Permalink
chore(examples): add a nodejs-taco example
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Sep 25, 2023
1 parent 0a71684 commit 3d564b6
Show file tree
Hide file tree
Showing 30 changed files with 301 additions and 123 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ examples/*/pnpm-lock.yaml
pnpm-debug.log
docs-json
./docs
.env
15 changes: 14 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# `integration-examples`
# `examples`

This directory contains a set of examples showing how to integrate `@nucypher/*`
into your application.

## Examples

- [**`nextjs`**](./nextjs): A Next.js application that uses `@nucypher/pre` to
encrypt and decrypt data.
- [**`nodejs-pre`**](./nodejs-pre): A Node.js application that uses `@nucypher/pre` to
encrypt and decrypt data.
- [**`nodejs-taco`**](./nodejs-taco): A Node.js application that uses `@nucypher/taco` to
encrypt and decrypt data.
- [**`react`**](./react): A React application that uses `@nucypher/pre` to
encrypt and decrypt data.
- [**`webpack`**](./webpack): A Webpack application that uses `@nucypher/pre` to
encrypt and decrypt data.
2 changes: 2 additions & 0 deletions examples/nodejs-pre/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RPC_PROVIDER_URL=
PRIVATE_KEY=
20 changes: 20 additions & 0 deletions examples/nodejs-pre/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `nodejs-pre` integration example

Shows how to use `@nucypher/pre in Node.js.

## Setup

Setup environment variables:

```bash
cp .env.example .env
```

Update `.env` with your values.

## Usage

```bash
pnpm install
pnpm start
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"type-check": "tsc"
},
"dependencies": {
"@nucypher/pre": "workspace:*"
"@nucypher/pre": "workspace:*",
"dotenv": "^16.3.1"
},
"peerDependencies": {
"ethers": "^5.7.2"
Expand Down
42 changes: 23 additions & 19 deletions examples/nodejs/src/index.ts → examples/nodejs-pre/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ import {
SecretKey,
toBytes,
} from '@nucypher/pre';
import * as dotenv from 'dotenv';
import { ethers } from 'ethers';

dotenv.config();

const rpcProviderUrl = process.env.RPC_PROVIDER_URL;
if (!rpcProviderUrl) {
throw new Error('RPC_PROVIDER_URL is not set.');
}

const privateKey = process.env.PRIVATE_KEY;
if (!privateKey) {
throw new Error('PRIVATE_KEY is not set.');
}

const makeAlice = () => {
const secretKey = SecretKey.fromBEBytes(
toBytes('fake-secret-key-32-bytes-alice-x'),
Expand Down Expand Up @@ -35,30 +48,21 @@ const getRandomLabel = () => `label-${new Date().getTime()}`;
const runExample = async () => {
await initialize();

const provider = ethers.Wallet.createRandom();

const remoteBob = makeRemoteBob();
const threshold = 2;
const shares = 3;
const startDate = new Date();
const endDate = new Date(Date.now() + 1000 * 60 * 60 * 24 * 30); // In 30 days
const provider = new ethers.providers.JsonRpcProvider(rpcProviderUrl);
const signer = new ethers.Wallet(privateKey);
const policyParams = {
bob: remoteBob,
bob: makeRemoteBob(),
label: getRandomLabel(),
threshold,
shares,
startDate,
endDate,
threshold: 2,
shares: 3,
startDate: new Date(),
endDate: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30), // In 30 days,
};
const porterUri = getPorterUri('tapir'); // Test network

const alice = makeAlice();
const policy = await alice.grant(
provider.provider,
provider,
porterUri,
policyParams,
);

console.log('Creating policy...');
const policy = await alice.grant(provider, signer, porterUri, policyParams);

console.log('Policy created:');
console.log({ policy });
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions examples/nodejs-taco/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RPC_PROVIDER_URL=
PRIVATE_KEY=
20 changes: 20 additions & 0 deletions examples/nodejs-taco/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `nodejs-taco` integration example

Shows how to use `@nucypher/taco` in Node.js.

## Setup

Setup environment variables:

```bash
cp .env.example .env
```

Update `.env` with your values.

## Usage

```bash
pnpm install
pnpm start
```
18 changes: 18 additions & 0 deletions examples/nodejs-taco/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "0.1.0",
"private": true,
"license": "GPL-3.0-only",
"author": "Piotr Rosłaniec <p.roslaniec@gmail.com>",
"scripts": {
"check": "pnpm type-check",
"start": "ts-node src/index.ts",
"type-check": "tsc"
},
"dependencies": {
"@nucypher/taco": "workspace:*",
"dotenv": "^16.3.1"
},
"peerDependencies": {
"ethers": "^5.7.2"
}
}
57 changes: 57 additions & 0 deletions examples/nodejs-taco/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
conditions,
decrypt,
encrypt,
getPorterUri,
initialize,
toBytes,
} from '@nucypher/taco';
import * as dotenv from 'dotenv';
import { ethers } from 'ethers';

dotenv.config();

const rpcProviderUrl = process.env.RPC_PROVIDER_URL;
if (!rpcProviderUrl) {
throw new Error('RPC_PROVIDER_URL is not set.');
}

const privateKey = process.env.PRIVATE_KEY;
if (!privateKey) {
throw new Error('PRIVATE_KEY is not set.');
}

const runExample = async () => {
await initialize();

console.log('Encrypting message...');
const provider = new ethers.providers.JsonRpcProvider(rpcProviderUrl);
const message = toBytes('this is a secret');
const ownsNFT = new conditions.ERC721Ownership({
contractAddress: '0x1e988ba4692e52Bc50b375bcC8585b95c48AaD77',
parameters: [3591],
chain: 5,
});
const ritualId = 91; // Replace with your own ritual ID
const messageKit = await encrypt(provider, message, ownsNFT, ritualId);

console.log('Decrypting message...');
const porterUri = getPorterUri('tapir'); // Test network
const signer = new ethers.Wallet(privateKey);
const decryptedMessage = await decrypt(
provider,
messageKit,
signer,
porterUri,
);

console.assert(decryptedMessage === message);
};

runExample()
.then(() => {
console.log('Example finished.');
})
.catch((err) => {
console.error('Example failed:', err);
});
14 changes: 14 additions & 0 deletions examples/nodejs-taco/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"include": ["src"],
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"noEmit": true
},
"references": [
{
"path": "../../packages/shared/tsconfig.cjs.json"
}
]
}
10 changes: 0 additions & 10 deletions examples/nodejs/README.md

This file was deleted.

17 changes: 9 additions & 8 deletions packages/pre/test/acceptance/alice-grants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ import {
PublicKey,
VerifiedKeyFrag,
} from '@nucypher/nucypher-core';
import {
ChecksumAddress,
EnactedPolicy,
Enrico,
initialize,
toBytes,
Ursula,
} from '@nucypher/shared';
import {
bytesEqual,
fakeAlice,
Expand All @@ -32,6 +24,15 @@ import {
} from '@nucypher/test-utils';
import { beforeAll, expect, test } from 'vitest';

import {
ChecksumAddress,
EnactedPolicy,
Enrico,
initialize,
toBytes,
Ursula,
} from '../../src';

test('story: alice shares message with bob through policy', () => {
const message = 'secret-message-from-alice';
const threshold = 2;
Expand Down
3 changes: 2 additions & 1 deletion packages/pre/test/acceptance/delay-enact.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { initialize } from '@nucypher/shared';
import {
bytesEqual,
fakeAlice,
Expand All @@ -13,6 +12,8 @@ import {
} from '@nucypher/test-utils';
import { beforeAll, expect, test } from 'vitest';

import { initialize } from '../../src';

test('story: alice creates a policy but someone else enacts it', () => {
const threshold = 2;
const shares = 3;
Expand Down
24 changes: 12 additions & 12 deletions packages/pre/test/docs.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
import {
Cohort,
ConditionExpression,
ContractCondition,
ContractConditionProps,
ERC721Ownership,
getPorterUri,
initialize,
SecretKey,
toBytes,
} from '@nucypher/shared';
import {
fakeProvider,
fakeUrsulas,
Expand All @@ -23,7 +12,18 @@ import {
import { providers } from 'ethers';
import { beforeAll, expect, test, vi } from 'vitest';

import { PreStrategy } from '../src';
import {
Cohort,
ConditionExpression,
ContractCondition,
ContractConditionProps,
ERC721Ownership,
getPorterUri,
initialize,
PreStrategy,
SecretKey,
toBytes,
} from '../src';

test('doc tests', async () => {
beforeAll(async () => {
Expand Down
17 changes: 9 additions & 8 deletions packages/pre/test/enrico.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
// Disabling because we want to access Alice.keyring which is a private property
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
ConditionExpression,
Enrico,
ERC721Ownership,
PolicyMessageKit,
RetrievalResult,
toBytes,
} from '@nucypher/shared';
import {
bytesEqual,
fakeAlice,
Expand All @@ -17,6 +9,15 @@ import {
} from '@nucypher/test-utils';
import { expect, test } from 'vitest';

import {
ConditionExpression,
Enrico,
ERC721Ownership,
PolicyMessageKit,
RetrievalResult,
toBytes,
} from '../src';

test('enrico', () => {
test('alice decrypts message encrypted by enrico', async () => {
const label = 'fake-label';
Expand Down
3 changes: 2 additions & 1 deletion packages/pre/test/message-kit.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { MessageKit, toBytes } from '@nucypher/shared';
import { fakeBob } from '@nucypher/test-utils';
import { expect, test } from 'vitest';

import { MessageKit, toBytes } from '../src';

test('message kit', () => {
test('bob decrypts', () => {
const bob = fakeBob();
Expand Down
Loading

0 comments on commit 3d564b6

Please sign in to comment.