Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anyone-with-the-link example #516

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/taco/nodejs-anyonewiththelink/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# URL for RPC blockchain provider. For TACo testnet you should use a Polygon Amoy endpoint.
# Current default extracted from https://chainlist.org/chain/80002, but ideally, you'll use your own endpoint.
RPC_PROVIDER_URL=https://rpc-amoy.polygon.technology
# Hex-encoded private keys for the Signer, the Encryptor and the Consumer.
# We provide here some defaults, but we encourage you to choose your own.
SIGNER_PRIVATE_KEY=0x900edb9e8214b2353f82aa195e915128f419a92cfb8bbc0f4784f10ef4112b86
ENCRYPTOR_PRIVATE_KEY=0x900edb9e8214b2353f82aa195e915128f419a92cfb8bbc0f4784f10ef4112b86
CONSUMER_PRIVATE_KEY=0xf307e165339cb5deb2b8ec59c31a5c0a957b8e8453ce7fe8a19d9a4c8acf36d4
RITUAL_ID=0
DOMAIN=tapir
33 changes: 33 additions & 0 deletions examples/taco/nodejs-anyonewiththelink/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# `nodejs-taco` integration example

This example shows how to use `@nucypher/taco` in Node.js.

## Setup

This script needs 3 environment variables, that you can set in the `.env` file:

* `RPC_PROVIDER_URL`: For TACo testnet you should use a Polygon Amoy endpoint.
* `ENCRYPTOR_PRIVATE_KEY` and `CONSUMER_PRIVATE_KEY`: Hex-encoded private keys for the Encryptor and the Consumer,
respectively.

Default values for these variables are provided in `.env.example`, so you can run:

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

However, we encourage you to choose your own values for these variables.

## Usage

To run the script, you just need to install this example package and start it:

```bash
pnpm install
pnpm start
```

## Learn more

Please find developer documentation for
TACo [here](https://docs.threshold.network/app-development/threshold-access-control-tac).
17 changes: 17 additions & 0 deletions examples/taco/nodejs-anyonewiththelink/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "taco-nodejs-example",
"version": "0.0.0",
"private": true,
"license": "GPL-3.0-only",
"author": "Piotr Rosłaniec <[email protected]>",
"scripts": {
"check": "pnpm type-check",
"start": "ts-node src/index.ts",
"type-check": "tsc"
},
"dependencies": {
"@nucypher/taco": "workspace:*",
"dotenv": "^16.3.1",
"ethers": "^5.7.2"
}
}
60 changes: 60 additions & 0 deletions examples/taco/nodejs-anyonewiththelink/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { domains, 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 signerPrivateKey = process.env.SIGNER_PRIVATE_KEY;
if (!signerPrivateKey) {
throw new Error('SIGNER_PRIVATE_KEY is not set.');
}

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

const domain = process.env.DOMAIN || domains.TESTNET;
const ritualId = parseInt(process.env.RITUAL_ID || '0');
const provider = new ethers.providers.JsonRpcProvider(rpcProviderUrl);

const generateToken = async () => {
const nonce = 'hello'; // TODO: generate this randomly

const signer = new ethers.Wallet(signerPrivateKey);
const signature = await signer.signMessage(nonce);

const token = {
nonce: ethers.utils.hexlify(ethers.utils.toUtf8Bytes(nonce)), // TODO: convert to hex string using taco functions
signature: signature,
signer: signer.address,
};

console.log('This token to be shared with the decryptor:', token);
return JSON.stringify(token);
};

const encryptToBytes = async (token: string) => {
const encryptorSigner = new ethers.Wallet(encryptorPrivateKey);
};

const runExample = async () => {
// Make sure the provider is connected to Polygon Amoy testnet
const network = await provider.getNetwork();
if (network.chainId !== 80002) {
console.error('Please connect to Polygon Amoy testnet');
}
await initialize();

const token = await generateToken();
const encryptedBytes = await encryptToBytes(token);
};

runExample().then(() => {
console.log('Example finished');
});
14 changes: 14 additions & 0 deletions examples/taco/nodejs-anyonewiththelink/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/taco/tsconfig.cjs.json"
}
]
}
15 changes: 13 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading