Skip to content

Commit

Permalink
chore(examples): add taco examples
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Sep 25, 2023
1 parent 0e5470e commit f21b9f1
Show file tree
Hide file tree
Showing 100 changed files with 1,913 additions and 888 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ examples/*/pnpm-lock.yaml
pnpm-debug.log
docs-json
./docs
.env
.next
5 changes: 4 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# `integration-examples`
# `examples`

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

Refer to `./pre` for examples of how to use the `@nucypher/pre` package.
Refer to `./taco` for examples of how to use the `@nucypher/taco` package.
10 changes: 0 additions & 10 deletions examples/nodejs/README.md

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"references": [
{
"path": "../../packages/shared/tsconfig.es.json"
"path": "../../../packages/pre/tsconfig.es.json"
}
]
}
File renamed without changes.
2 changes: 2 additions & 0 deletions examples/pre/nodejs/.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/pre/nodejs/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/pre/nodejs/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
14 changes: 14 additions & 0 deletions examples/pre/nodejs/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/pre/tsconfig.cjs.json"
}
]
}
4 changes: 2 additions & 2 deletions examples/react/README.md → examples/pre/react/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `react-craco` integration example
# `react-pre` integration example

Shows how to integrate `@nucypher/*` into a React application.
Shows how to integrate `@nucypher/pre` into a React application.

In order to load WASM dependencies of `@nucypher/*`, we override the
`react-scripts` configuration with `craco`. For more details, see the
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
10 changes: 3 additions & 7 deletions examples/react/src/App.tsx → examples/pre/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ function App() {
}
const provider = new ethers.providers.Web3Provider(window.ethereum, 'any');

const {chainId} = await provider.getNetwork();
if (chainId !== 80001) {
// Switch to Matic Mumbai testnet
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{chainId: '0x13881'}],
});
const { chainId } = await provider.getNetwork();
if (![137, 80001].includes(chainId)) {
console.error('You need to connect to the Mumbai or Polygon network');
}

await provider.send('eth_requestAccounts', []);
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"references": [
{
"path": "../../packages/shared/tsconfig.es.json"
"path": "../../../packages/pre/tsconfig.es.json"
}
]
}
File renamed without changes.
12 changes: 12 additions & 0 deletions examples/pre/webpack-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# `webpack-5` integration example

Shows how to integrate `@nucypher/taco` with Webpack 5.

## Usage

```bash
pnpm install
pnpm start
```

Go to [localhost:8080](http://localhost:8080/) in your browser and look in the JS console.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ const runExample = async () => {
const provider = new ethers.providers.Web3Provider(window.ethereum!, 'any');
await provider.send('eth_requestAccounts', []);

const { chainId } = await provider.getNetwork();
if (chainId !== 80001) {
// Switch to Matic Mumbai testnet
await window.ethereum!.request!({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x13881' }],
});
}

const remoteBob = makeRemoteBob();
const threshold = 2;
const shares = 3;
Expand Down
14 changes: 14 additions & 0 deletions examples/pre/webpack-5/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../../tsconfig.json",
"include": ["src"],
"compilerOptions": {
"esModuleInterop": true,
"skipLibCheck": true,
"noEmit": true
},
"references": [
{
"path": "../../../packages/pre/tsconfig.es.json"
}
]
}
File renamed without changes.
3 changes: 3 additions & 0 deletions examples/taco/nextjs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions examples/taco/nextjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
48 changes: 48 additions & 0 deletions examples/taco/nextjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with
[`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the
result.

You can start editing the page by modifying `app/page.tsx`. The page
auto-updates as you edit the file.

This project uses
[`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to
automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js
features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out
[the Next.js GitHub repository](https://github.com/vercel/next.js/) - your
feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the
[Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme)
from the creators of Next.js.

Check out our
[Next.js deployment documentation](https://nextjs.org/docs/deployment) for more
details.
4 changes: 4 additions & 0 deletions examples/taco/nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

module.exports = nextConfig;
28 changes: 28 additions & 0 deletions examples/taco/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"private": true,
"scripts": {
"build": "next build",
"check": "pnpm lint && pnpm type-check && pnpm build",
"dev": "next dev",
"lint": "next lint",
"start": "next start",
"type-check": "tsc -p tsconfig.build.json"
},
"dependencies": {
"@nucypher/taco": "workspace:*",
"@types/node": "20.6.3",
"@types/react": "18.2.22",
"@types/react-dom": "18.2.7",
"eslint": "8.49.0",
"eslint-config-next": "13.5.2",
"ethers": "^5.7.2",
"next": "13.5.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "5.2.2"
},
"peerDependencies": {
"ethers": "^5.7.2",
"typescript": "5.2.2"
}
}
21 changes: 21 additions & 0 deletions examples/taco/nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
);
}
Loading

0 comments on commit f21b9f1

Please sign in to comment.