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

Migrate from ethers-v5 to viem #421

Draft
wants to merge 5 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
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.md
**/dist/**
**/build/**
19 changes: 9 additions & 10 deletions .github/workflows/lynx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ on:
schedule:
- cron: "0 * * * *" # Every hour
pull_request:
# branches:
# - main
# - development
# - epic-*
workflow_dispatch:

env: # TODO: Use variables when GH supports it for forks. See https://github.com/orgs/community/discussions/44322
# TODO: Use variables when GH supports it for forks. See https://github.com/orgs/community/discussions/44322
env:
RPC_PROVIDER_URL: "https://polygon-mumbai.infura.io/v3/3747007a284045d483c342fb39889a30"
ENCRYPTOR_PRIVATE_KEY: "0x900edb9e8214b2353f82aa195e915128f419a92cfb8bbc0f4784f10ef4112b86"
CONSUMER_PRIVATE_KEY: "0xf307e165339cb5deb2b8ec59c31a5c0a957b8e8453ce7fe8a19d9a4c8acf36d4"
RITUAL_ID: "0"

jobs:
build_project:
networks:
name: '🔎 Lynx Testnet Example on Node ${{ matrix.node }} and ${{ matrix.os }}'

runs-on: ${{ matrix.os }}
Expand All @@ -37,19 +35,20 @@ jobs:
node-version: ${{ matrix.node }}
cache: 'pnpm'

- name: Lynx testnet node-js script installation
- name: Install dependencies
working-directory: ./examples/taco/nodejs
run: pnpm install

- name: Lynx testnet node-js script environment
- name: Setup environment
working-directory: ./examples/taco/nodejs
# TODO: Use variables when GH supports it for forks. See https://github.com/orgs/community/discussions/44322
run: |
echo RPC_PROVIDER_URL=$RPC_PROVIDER_URL > .env
echo ENCRYPTOR_PRIVATE_KEY=$ENCRYPTOR_PRIVATE_KEY >> .env
echo CONSUMER_PRIVATE_KEY=$CONSUMER_PRIVATE_KEY >> .env
echo RITUAL_ID=$RITUAL_ID >> .env
echo DOMAIN=lynx >> .env

- name: Lynx testnet node-js script execution
id: lynx_script
- name: Run taco/nodejs script
working-directory: ./examples/taco/nodejs
run: pnpm start
56 changes: 56 additions & 0 deletions .github/workflows/tapir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: '🔎 Tapir Testnet Example'

on:
schedule:
- cron: "0 * * * *" # Every hour
pull_request:
branches:
- main
workflow_dispatch:

# TODO: Use variables when GH supports it for forks. See https://github.com/orgs/community/discussions/44322
env:
RPC_PROVIDER_URL: "https://polygon-mumbai.infura.io/v3/3747007a284045d483c342fb39889a30"
ENCRYPTOR_PRIVATE_KEY: "0x900edb9e8214b2353f82aa195e915128f419a92cfb8bbc0f4784f10ef4112b86"
CONSUMER_PRIVATE_KEY: "0xf307e165339cb5deb2b8ec59c31a5c0a957b8e8453ce7fe8a19d9a4c8acf36d4"
RITUAL_ID: "5"

jobs:
networks:
name: '🔎 Tapir Testnet Example on Node ${{ matrix.node }} and ${{ matrix.os }}'

runs-on: ${{ matrix.os }}
strategy:
matrix:
node: [ '18.x' ]
os: [ ubuntu-latest ]

steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v2
with:
version: 8.1

- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: 'pnpm'

- name: Install dependencies
working-directory: ./examples/taco/nodejs
run: pnpm install

- name: Setup environment
working-directory: ./examples/taco/nodejs
# TODO: Use variables when GH supports it for forks. See https://github.com/orgs/community/discussions/44322
run: |
echo RPC_PROVIDER_URL=$RPC_PROVIDER_URL > .env
echo ENCRYPTOR_PRIVATE_KEY=$ENCRYPTOR_PRIVATE_KEY >> .env
echo CONSUMER_PRIVATE_KEY=$CONSUMER_PRIVATE_KEY >> .env
echo RITUAL_ID=$RITUAL_ID >> .env
echo DOMAIN=tapir >> .env

- name: Run taco/nodejs script
working-directory: ./examples/taco/nodejs
run: pnpm start
9 changes: 6 additions & 3 deletions demos/taco-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"build": "pnpm clean && webpack --mode production --progress",
"clean": "rimraf build",
"check": "pnpm type-check && pnpm build",
"type-check": "tsc --noEmit"
"type-check": "tsc --noEmit",
"lint": "eslint --ext .ts src",
"lint:fix": "pnpm lint --fix"
},
"dependencies": {
"@nucypher/taco": "^0.1.0-rc.6",
Expand All @@ -21,7 +23,8 @@
"react": "^18.2.0",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.2.0",
"react-spinners": "^0.13.6"
"react-spinners": "^0.13.6",
"viem": "^1.19.9"
},
"devDependencies": {
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
Expand All @@ -34,7 +37,7 @@
"react-refresh": "^0.14.0",
"rimraf": "^5.0.5",
"stream-browserify": "^3.0.0",
"typescript": "^4.8.3",
"typescript": "^5.2.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.11.1"
Expand Down
36 changes: 28 additions & 8 deletions demos/taco-demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import {
ThresholdMessageKit,
} from '@nucypher/taco';
import { Mumbai, useEthers } from '@usedapp/core';
import { ethers } from 'ethers';
import React, { useEffect, useState } from 'react';
import 'viem/window';

import { createPublicClient, createWalletClient, custom } from 'viem';

import { ConditionBuilder } from './ConditionBuilder';
import { DEFAULT_DOMAIN, DEFAULT_RITUAL_ID } from './config';
import { Decrypt } from './Decrypt';
import { Encrypt } from './Encrypt';
import { Spinner } from './Spinner';
import { DEFAULT_DOMAIN, DEFAULT_RITUAL_ID } from './config';

export default function App() {
const { activateBrowserWallet, deactivate, account, switchNetwork } =
Expand All @@ -36,42 +38,60 @@ export default function App() {
}, []);

const encryptMessage = async (message: string) => {
if (!window.ethereum) {
console.error('You need to connect to your wallet first');
return;
}
if (!condition) {
return;
}
setLoading(true);

await switchNetwork(Mumbai.chainId);

const provider = new ethers.providers.Web3Provider(window.ethereum);
const publicClient = createPublicClient({
transport: custom(window.ethereum),
});
const walletClient = createWalletClient({
transport: custom(window.ethereum),
});
const encryptedMessage = await encrypt(
provider,
publicClient,
domain,
message,
condition,
ritualId,
provider.getSigner(),
walletClient,
);

setEncryptedMessage(encryptedMessage);
setLoading(false);
};

const decryptMessage = async (encryptedMessage: ThresholdMessageKit) => {
if (!window.ethereum) {
console.error('You need to connect to your wallet first');
return;
}
if (!condition) {
return;
}
setLoading(true);
setDecryptedMessage('');
setDecryptionErrors([]);

const provider = new ethers.providers.Web3Provider(window.ethereum);
const publicClient = createPublicClient({
transport: custom(window.ethereum),
});
const walletClient = createWalletClient({
transport: custom(window.ethereum),
});
const decryptedMessage = await decrypt(
provider,
publicClient,
domain,
encryptedMessage,
getPorterUri(domain),
provider.getSigner(),
walletClient,
);

setDecryptedMessage(new TextDecoder().decode(decryptedMessage));
Expand Down
2 changes: 1 addition & 1 deletion demos/taco-demo/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export const DEFAULT_RITUAL_ID =
(process.env.DEFAULT_RITUAL_ID && parseInt(process.env.DEFAULT_RITUAL_ID)) ||
24;

export const DEFAULT_DOMAIN = process.env.DEFAULT_DOMAIN || domains.DEV;
export const DEFAULT_DOMAIN = process.env.DEFAULT_DOMAIN || domains.DEVNET;
5 changes: 0 additions & 5 deletions demos/taco-demo/src/react-app-env.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions demos/taco-demo/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module.exports = {
DEFAULT_RITUAL_ID: JSON.stringify(process.env.DEFAULT_RITUAL_ID),
DEFAULT_DOMAIN: JSON.stringify(process.env.DEFAULT_DOMAIN),
},
}
})
},
}),
].filter(Boolean),
module: {
rules: [
Expand Down
9 changes: 6 additions & 3 deletions demos/taco-nft-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"build": "pnpm clean && webpack --mode production --progress",
"clean": "rimraf build",
"check": "pnpm type-check && pnpm build",
"type-check": "tsc --noEmit"
"type-check": "tsc --noEmit",
"lint": "eslint --ext .ts src",
"lint:fix": "pnpm lint --fix"
},
"dependencies": {
"@nucypher/taco": "^0.1.0-rc.6",
Expand All @@ -21,7 +23,8 @@
"react": "^18.2.0",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.2.0",
"react-spinners": "^0.13.6"
"react-spinners": "^0.13.6",
"viem": "^1.19.9"
},
"devDependencies": {
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
Expand All @@ -34,7 +37,7 @@
"react-refresh": "^0.14.0",
"rimraf": "^5.0.5",
"stream-browserify": "^3.0.0",
"typescript": "^4.8.3",
"typescript": "^5.2.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.11.1"
Expand Down
33 changes: 26 additions & 7 deletions demos/taco-nft-demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
ThresholdMessageKit,
} from '@nucypher/taco';
import { Mumbai, useEthers } from '@usedapp/core';
import { ethers } from 'ethers';
import React, { useEffect, useState } from 'react';
import 'viem/window';
import { createPublicClient, createWalletClient, custom } from 'viem';

import { Decrypt } from './Decrypt';
import { Encrypt } from './Encrypt';
Expand All @@ -35,42 +36,60 @@ export default function App() {
}, []);

const encryptMessage = async (message: string) => {
if (!window.ethereum) {
console.error('You need to connect to your wallet first');
return;
}
if (!condition) {
return;
}
setLoading(true);

await switchNetwork(Mumbai.chainId);

const provider = new ethers.providers.Web3Provider(window.ethereum);
const publicClient = createPublicClient({
transport: custom(window.ethereum),
});
const walletClient = createWalletClient({
transport: custom(window.ethereum),
});
const encryptedMessage = await encrypt(
provider,
publicClient,
domain,
message,
condition,
ritualId,
provider.getSigner(),
walletClient,
);

setEncryptedMessage(encryptedMessage);
setLoading(false);
};

const decryptMessage = async (encryptedMessage: ThresholdMessageKit) => {
if (!window.ethereum) {
console.error('You need to connect to your wallet first');
return;
}
if (!condition) {
return;
}
setLoading(true);
setDecryptedMessage('');
setDecryptionErrors([]);

const provider = new ethers.providers.Web3Provider(window.ethereum);
const publicClient = createPublicClient({
transport: custom(window.ethereum),
});
const walletClient = createWalletClient({
transport: custom(window.ethereum),
});
const decryptedMessage = await decrypt(
provider,
publicClient,
domain,
encryptedMessage,
getPorterUri(domain),
provider.getSigner(),
walletClient,
);

setDecryptedMessage(new TextDecoder().decode(decryptedMessage));
Expand Down
2 changes: 1 addition & 1 deletion demos/taco-nft-demo/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export const DEFAULT_RITUAL_ID =
(process.env.DEFAULT_RITUAL_ID && parseInt(process.env.DEFAULT_RITUAL_ID)) ||
24;

export const DEFAULT_DOMAIN = process.env.DEFAULT_DOMAIN || domains.DEV;
export const DEFAULT_DOMAIN = process.env.DEFAULT_DOMAIN || domains.DEVNET;
5 changes: 0 additions & 5 deletions demos/taco-nft-demo/src/react-app-env.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions demos/taco-nft-demo/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module.exports = {
DEFAULT_RITUAL_ID: JSON.stringify(process.env.DEFAULT_RITUAL_ID),
DEFAULT_DOMAIN: JSON.stringify(process.env.DEFAULT_DOMAIN),
},
}
})
},
}),
].filter(Boolean),
module: {
rules: [
Expand Down
6 changes: 6 additions & 0 deletions examples/pre/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
},
"dependencies": {
"@nucypher/pre": "workspace:*",
"next": "14.0.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"viem": "^1.19.9"
},
"devDependencies": {
"@types/node": "20.10.0",
"@types/react": "18.2.45",
"@types/react-dom": "18.2.14",
Expand Down
Loading
Loading