(
'',
);
@@ -32,21 +31,28 @@ function App() {
const loadWeb3Provider = async () => {
if (!window.ethereum) {
console.error('You need to connect to your wallet first');
+ return;
}
- const provider = new ethers.providers.Web3Provider(window.ethereum, 'any');
- const { chainId } = await provider.getNetwork();
+ const publicClient = createPublicClient({
+ transport: custom(window.ethereum),
+ });
+ const walletClient = createWalletClient({
+ transport: custom(window.ethereum),
+ });
+
+ const chainId = await walletClient.getChainId();
const mumbaiChainId = 80001;
if (chainId !== mumbaiChainId) {
// Switch to Polygon Mumbai testnet
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
- params: [{ chainId: hexlify(mumbaiChainId) }],
+ params: [{ chainId: toHex(mumbaiChainId) }],
});
}
- await provider.send('eth_requestAccounts', []);
- setProvider(provider);
+ setWalletClient(walletClient);
+ setPublicClient(publicClient);
};
useEffect(() => {
@@ -55,19 +61,21 @@ function App() {
const { isInit, encryptDataToBytes, decryptDataFromBytes } = useTaco({
domain,
- provider,
ritualId,
+ walletClient,
+ publicClient,
});
- if (!isInit || !provider) {
+ if (!isInit || !walletClient) {
return Loading...
;
}
const encryptMessage = async () => {
- if(!provider) return;
- setEncrypting(true)
+ if (!walletClient) {
+ return;
+ }
+ setEncrypting(true);
try {
- const signer = provider.getSigner();
const hasPositiveBalance = new conditions.RpcCondition({
chain: 80001,
method: 'eth_getBalance',
@@ -82,41 +90,60 @@ function App() {
const encryptedBytes = await encryptDataToBytes(
message,
hasPositiveBalance,
- signer,
+ publicClient,
+ walletClient,
);
if (encryptedBytes) {
- setEncryptedText(toHexString(encryptedBytes))
+ setEncryptedText(toHexString(encryptedBytes));
}
} catch (e) {
- console.log(e)
+ console.log(e);
}
- setEncrypting(false)
- }
+ setEncrypting(false);
+ };
const decryptMessage = async () => {
- if(!encryptedText || !provider) return
+ if (!encryptedText || !walletClient || !publicClient) {
+ return;
+ }
try {
- setDecrypting(true)
- const signer = provider.getSigner();
-
+ setDecrypting(true);
console.log('Decrypting message...');
const decryptedMessage = await decryptDataFromBytes(
fromHexString(encryptedText),
- signer,
+ publicClient,
+ walletClient,
);
if (decryptedMessage) {
setDecryptedMessage(fromBytes(decryptedMessage));
}
} catch (e) {
- console.log(e)
+ console.log(e);
}
- setDecrypting(false)
+ setDecrypting(false);
};
return (
-
Secret message: setMessage(e.target.value)} onClick={encryptMessage}/> {encrypting && 'Encrypting...'}
- Encrypted message: setEncryptedText(e.target.value)} /> {decrypting && 'Decrypting...'}
+
+ Secret message:{' '}
+ setMessage(e.target.value)}
+ onClick={encryptMessage}
+ />
+
+ {encrypting && 'Encrypting...'}
+
+
+ Encrypted message:{' '}
+ setEncryptedText(e.target.value)}
+ />
+
+ {decrypting && 'Decrypting...'}
+
{decryptedMessage && Decrypted message: {decryptedMessage}
}
);
diff --git a/examples/taco/nextjs/src/hooks/useTaco.ts b/examples/taco/nextjs/src/hooks/useTaco.ts
index be78a7bb5..653a15005 100644
--- a/examples/taco/nextjs/src/hooks/useTaco.ts
+++ b/examples/taco/nextjs/src/hooks/useTaco.ts
@@ -7,17 +7,17 @@ import {
initialize,
ThresholdMessageKit,
} from '@nucypher/taco';
-import { ethers } from 'ethers';
import { useCallback, useEffect, useState } from 'react';
+import { PublicClient, WalletClient } from 'viem';
export default function useTaco({
ritualId,
domain,
- provider,
}: {
ritualId: number;
domain: Domain;
- provider: ethers.providers.Provider | undefined;
+ publicClient?: PublicClient;
+ walletClient?: WalletClient;
}) {
const [isInit, setIsInit] = useState(false);
@@ -26,38 +26,47 @@ export default function useTaco({
}, []);
const decryptDataFromBytes = useCallback(
- async (encryptedBytes: Uint8Array, signer?: ethers.Signer) => {
- if (!isInit || !provider) return;
+ async (
+ encryptedBytes: Uint8Array,
+ publicClient?: PublicClient,
+ walletClient?: WalletClient,
+ ) => {
+ if (!isInit || !publicClient || !walletClient) {
+ return;
+ }
const messageKit = ThresholdMessageKit.fromBytes(encryptedBytes);
return decrypt(
- provider,
+ publicClient,
domain,
messageKit,
getPorterUri(domain),
- signer,
+ walletClient,
);
},
- [isInit, provider, domain],
+ [isInit, domain],
);
const encryptDataToBytes = useCallback(
async (
message: string,
condition: conditions.Condition,
- encryptorSigner: ethers.Signer,
+ publicClient?: PublicClient,
+ walletClient?: WalletClient,
) => {
- if (!isInit || !provider) return;
+ if (!isInit || !publicClient || !walletClient) {
+ return;
+ }
const messageKit = await encrypt(
- provider,
+ publicClient,
domain,
message,
condition,
ritualId,
- encryptorSigner,
+ walletClient,
);
return messageKit.toBytes();
},
- [isInit, provider, domain, ritualId],
+ [isInit, domain, ritualId],
);
return {
diff --git a/examples/taco/nodejs/README.md b/examples/taco/nodejs/README.md
index a0a7578b8..9537598d3 100644
--- a/examples/taco/nodejs/README.md
+++ b/examples/taco/nodejs/README.md
@@ -10,7 +10,12 @@ This script needs 3 environment variables, that you can set in the `.env` file:
* `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:
+- `RPC_PROVIDER_URL`: For TACo testnet you should use a Polygon Mumbai 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
diff --git a/examples/taco/nodejs/package.json b/examples/taco/nodejs/package.json
index ecf9cdb3b..6dc6085e4 100644
--- a/examples/taco/nodejs/package.json
+++ b/examples/taco/nodejs/package.json
@@ -11,7 +11,9 @@
},
"dependencies": {
"@nucypher/taco": "workspace:*",
- "dotenv": "^16.3.1",
- "ethers": "^5.7.2"
+ "viem": "^1.19.9"
+ },
+ "devDependencies": {
+ "dotenv": "^16.3.1"
}
}
diff --git a/examples/taco/nodejs/src/index.ts b/examples/taco/nodejs/src/index.ts
index ea2262605..3defb36d6 100644
--- a/examples/taco/nodejs/src/index.ts
+++ b/examples/taco/nodejs/src/index.ts
@@ -13,7 +13,9 @@ import {
toHexString,
} from '@nucypher/taco';
import * as dotenv from 'dotenv';
-import { ethers } from 'ethers';
+import { Hex, createPublicClient, createWalletClient, http } from 'viem';
+import { privateKeyToAccount } from 'viem/accounts';
+import { polygonMumbai } from 'viem/chains';
dotenv.config();
@@ -33,24 +35,44 @@ if (!consumerPrivateKey) {
}
const domain = process.env.DOMAIN || domains.TESTNET;
-const ritualId = parseInt(process.env.RITUAL_ID || "5");
-const provider = new ethers.providers.JsonRpcProvider(rpcProviderUrl);
+const ritualId = parseInt(process.env.RITUAL_ID || '5');
+
+const publicClient = createPublicClient({
+ transport: http(rpcProviderUrl),
+ chain: polygonMumbai,
+});
+
+const encryptorAccount = privateKeyToAccount(encryptorPrivateKey);
+const encryptorWalletClient = createWalletClient({
+ transport: http(rpcProviderUrl),
+ account: encryptorAccount,
+ chain: polygonMumbai,
+});
+
+const consumerAccount = privateKeyToAccount(consumerPrivateKey);
+const consumerWalletClient = createWalletClient({
+ transport: http(rpcProviderUrl),
+ account: consumerAccount,
+ chain: polygonMumbai,
+});
console.log('Domain:', domain);
console.log('Ritual ID:', ritualId);
const encryptToBytes = async (messageString: string) => {
- const encryptorSigner = new ethers.Wallet(encryptorPrivateKey);
- console.log(
- "Encryptor signer's address:",
- await encryptorSigner.getAddress(),
- );
+ // Make sure the provider is connected to Mumbai testnet
+ const chainId = await encryptorWalletClient.getChainId();
+ if (chainId !== polygonMumbai.id) {
+ console.error('Please connect to Mumbai testnet');
+ }
+
+ console.log("Encryptor signer's address:", encryptorAccount.address);
const message = toBytes(messageString);
console.log(format('Encrypting message ("%s") ...', messageString));
const hasPositiveBalance = new conditions.RpcCondition({
- chain: 80001,
+ chain: polygonMumbai.id,
method: 'eth_getBalance',
parameters: [':userAddress', 'latest'],
returnValueTest: {
@@ -64,39 +86,35 @@ const encryptToBytes = async (messageString: string) => {
);
const messageKit = await encrypt(
- provider,
+ publicClient,
domain,
message,
hasPositiveBalance,
ritualId,
- encryptorSigner,
+ encryptorWalletClient,
);
return messageKit.toBytes();
};
const decryptFromBytes = async (encryptedBytes: Uint8Array) => {
- const consumerSigner = new ethers.Wallet(consumerPrivateKey);
- console.log(
- "\nConsumer signer's address:",
- await consumerSigner.getAddress(),
- );
+ console.log("\nConsumer signer's address:", consumerAccount.address);
const messageKit = ThresholdMessageKit.fromBytes(encryptedBytes);
console.log('Decrypting message ...');
return decrypt(
- provider,
+ publicClient,
domain,
messageKit,
getPorterUri(domain),
- consumerSigner,
+ consumerWalletClient,
);
};
const runExample = async () => {
// Make sure the provider is connected to Mumbai testnet
- const network = await provider.getNetwork();
- if (network.chainId !== 80001) {
+ const chainId = await publicClient.getChainId();
+ if (chainId !== polygonMumbai.id) {
console.error('Please connect to Mumbai testnet');
}
await initialize();
diff --git a/examples/taco/react/package.json b/examples/taco/react/package.json
index 4085765a4..23d07c6b2 100644
--- a/examples/taco/react/package.json
+++ b/examples/taco/react/package.json
@@ -26,15 +26,13 @@
"@nucypher/shared": "workspace:*",
"@nucypher/taco": "workspace:*",
"react": "^18.2.0",
- "react-dom": "^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",
"react-scripts": "^5.0.1"
- },
- "peerDependencies": {
- "ethers": "^5.7.2"
}
}
diff --git a/examples/taco/react/src/App.tsx b/examples/taco/react/src/App.tsx
index 9820d7d65..2b83e6751 100644
--- a/examples/taco/react/src/App.tsx
+++ b/examples/taco/react/src/App.tsx
@@ -1,8 +1,14 @@
-import {fromHexString} from "@nucypher/shared";
-import {conditions, domains, fromBytes, toHexString} from '@nucypher/taco';
-import {ethers} from 'ethers';
-import {hexlify} from 'ethers/lib/utils';
-import {useEffect, useState} from 'react';
+import { fromHexString } from '@nucypher/shared';
+import { conditions, domains, fromBytes, toHexString } from '@nucypher/taco';
+import { useEffect, useState } from 'react';
+import {
+ createPublicClient,
+ createWalletClient,
+ custom,
+ PublicClient,
+ toHex,
+ WalletClient,
+} from 'viem';
import useTaco from './hooks/useTaco';
@@ -13,58 +19,65 @@ const ritualId = 5; // Replace with your own ritual ID
const domain = domains.TESTNET;
function App() {
- const [provider, setProvider] = useState<
- ethers.providers.Web3Provider | undefined
- >();
- const [message, setMessage] = useState('this is a secret')
- const [encrypting, setEncrypting] = useState(false)
- const [encryptedText, setEncryptedText] = useState(
- '',
- );
- const [decrypting, setDecrypting] = useState(false)
+ const [walletClient, setWalletClient] = useState();
+ const [publicClient, setPublicClient] = useState();
+ const [message, setMessage] = useState('this is a secret');
+ const [encrypting, setEncrypting] = useState(false);
+ const [encryptedText, setEncryptedText] = useState('');
+ const [decrypting, setDecrypting] = useState(false);
const [decryptedMessage, setDecryptedMessage] = useState(
'',
);
- const loadWeb3Provider = async () => {
+ const loadWeb3Clients = async () => {
if (!window.ethereum) {
console.error('You need to connect to your wallet first');
+ return;
}
- const provider = new ethers.providers.Web3Provider(window.ethereum, 'any');
- const { chainId } = await provider.getNetwork();
+ const publicClient = createPublicClient({
+ transport: custom(window.ethereum),
+ });
+ const walletClient = createWalletClient({
+ transport: custom(window.ethereum),
+ });
+ const chainId = await walletClient.getChainId();
+
const mumbaiChainId = 80001;
if (chainId !== mumbaiChainId) {
// Switch to Polygon Mumbai testnet
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
- params: [{ chainId: hexlify(mumbaiChainId) }],
+ params: [{ chainId: toHex(mumbaiChainId) }],
});
}
- await provider.send('eth_requestAccounts', []);
- setProvider(provider);
+ await walletClient.getAddresses();
+ setPublicClient(publicClient);
+ setWalletClient(walletClient);
};
useEffect(() => {
- loadWeb3Provider();
+ loadWeb3Clients();
}, []);
const { isInit, encryptDataToBytes, decryptDataFromBytes } = useTaco({
domain,
- provider,
ritualId,
+ walletClient,
+ publicClient,
});
- if (!isInit || !provider) {
+ if (!isInit || !walletClient) {
return Loading...
;
}
const encryptMessage = async () => {
- if(!provider) return;
- setEncrypting(true)
+ if (!walletClient) {
+ return;
+ }
+ setEncrypting(true);
try {
- const signer = provider.getSigner();
const hasPositiveBalance = new conditions.RpcCondition({
chain: 80001,
method: 'eth_getBalance',
@@ -79,41 +92,60 @@ function App() {
const encryptedBytes = await encryptDataToBytes(
message,
hasPositiveBalance,
- signer,
+ publicClient,
+ walletClient,
);
if (encryptedBytes) {
- setEncryptedText(toHexString(encryptedBytes))
+ setEncryptedText(toHexString(encryptedBytes));
}
} catch (e) {
- console.log(e)
+ console.log(e);
}
- setEncrypting(false)
- }
+ setEncrypting(false);
+ };
const decryptMessage = async () => {
- if(!encryptedText || !provider) return
+ if (!encryptedText || !walletClient) {
+ return;
+ }
try {
- setDecrypting(true)
- const signer = provider.getSigner();
-
+ setDecrypting(true);
console.log('Decrypting message...');
const decryptedMessage = await decryptDataFromBytes(
fromHexString(encryptedText),
- signer,
+ publicClient,
+ walletClient,
);
if (decryptedMessage) {
setDecryptedMessage(fromBytes(decryptedMessage));
}
} catch (e) {
- console.log(e)
+ console.log(e);
}
- setDecrypting(false)
+ setDecrypting(false);
};
return (
-
Secret message: setMessage(e.target.value)} onClick={encryptMessage}/> {encrypting && 'Encrypting...'}
- Encrypted message: setEncryptedText(e.target.value)} /> {decrypting && 'Decrypting...'}
+
+ Secret message:{' '}
+ setMessage(e.target.value)}
+ onClick={encryptMessage}
+ />
+
+ {encrypting && 'Encrypting...'}
+
+
+ Encrypted message:{' '}
+ setEncryptedText(e.target.value)}
+ />
+
+ {decrypting && 'Decrypting...'}
+
{decryptedMessage && Decrypted message: {decryptedMessage}
}
);
diff --git a/examples/taco/react/src/hooks/useTaco.ts b/examples/taco/react/src/hooks/useTaco.ts
index ed7732eb6..653a15005 100644
--- a/examples/taco/react/src/hooks/useTaco.ts
+++ b/examples/taco/react/src/hooks/useTaco.ts
@@ -1,4 +1,5 @@
import {
+ conditions,
decrypt,
Domain,
encrypt,
@@ -6,18 +7,17 @@ import {
initialize,
ThresholdMessageKit,
} from '@nucypher/taco';
-import { Condition } from '@nucypher/taco/src/conditions';
-import { ethers } from 'ethers';
import { useCallback, useEffect, useState } from 'react';
+import { PublicClient, WalletClient } from 'viem';
export default function useTaco({
ritualId,
domain,
- provider,
}: {
ritualId: number;
domain: Domain;
- provider: ethers.providers.Provider | undefined;
+ publicClient?: PublicClient;
+ walletClient?: WalletClient;
}) {
const [isInit, setIsInit] = useState(false);
@@ -26,38 +26,47 @@ export default function useTaco({
}, []);
const decryptDataFromBytes = useCallback(
- async (encryptedBytes: Uint8Array, signer?: ethers.Signer) => {
- if (!isInit || !provider) return;
+ async (
+ encryptedBytes: Uint8Array,
+ publicClient?: PublicClient,
+ walletClient?: WalletClient,
+ ) => {
+ if (!isInit || !publicClient || !walletClient) {
+ return;
+ }
const messageKit = ThresholdMessageKit.fromBytes(encryptedBytes);
return decrypt(
- provider,
+ publicClient,
domain,
messageKit,
getPorterUri(domain),
- signer,
+ walletClient,
);
},
- [isInit, provider, domain],
+ [isInit, domain],
);
const encryptDataToBytes = useCallback(
async (
message: string,
- condition: Condition,
- encryptorSigner: ethers.Signer,
+ condition: conditions.Condition,
+ publicClient?: PublicClient,
+ walletClient?: WalletClient,
) => {
- if (!isInit || !provider) return;
+ if (!isInit || !publicClient || !walletClient) {
+ return;
+ }
const messageKit = await encrypt(
- provider,
+ publicClient,
domain,
message,
condition,
ritualId,
- encryptorSigner,
+ walletClient,
);
return messageKit.toBytes();
},
- [isInit, provider, domain, ritualId],
+ [isInit, domain, ritualId],
);
return {
diff --git a/examples/taco/react/src/index.tsx b/examples/taco/react/src/index.tsx
index 253d3f9b3..3f83eeaea 100644
--- a/examples/taco/react/src/index.tsx
+++ b/examples/taco/react/src/index.tsx
@@ -1,8 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
-import './index.css'
import App from './App';
+import './index.css';
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement,
diff --git a/examples/taco/react/src/react-app-env.d.ts b/examples/taco/react/src/react-app-env.d.ts
deleted file mode 100644
index 8f8826530..000000000
--- a/examples/taco/react/src/react-app-env.d.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-///
-import { ExternalProvider } from '@ethersproject/providers';
-
-declare global {
- interface Window {
- ethereum?: ExternalProvider;
- }
-}
diff --git a/examples/taco/webpack-5/package.json b/examples/taco/webpack-5/package.json
index 4266b882a..3a67f4853 100644
--- a/examples/taco/webpack-5/package.json
+++ b/examples/taco/webpack-5/package.json
@@ -12,7 +12,9 @@
"type-check": "tsc"
},
"dependencies": {
- "@nucypher/taco": "workspace:*"
+ "@nucypher/taco": "workspace:*",
+ "viem": "^1.19.9",
+ "ethers": "^5.7.2"
},
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
@@ -20,8 +22,5 @@
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.7.4"
- },
- "peerDependencies": {
- "ethers": "^5.7.2"
}
}
diff --git a/examples/taco/webpack-5/src/index.html b/examples/taco/webpack-5/src/index.html
index ad1f3fd35..6aaef3537 100644
--- a/examples/taco/webpack-5/src/index.html
+++ b/examples/taco/webpack-5/src/index.html
@@ -8,7 +8,8 @@
+ javascript in your browser.
Check console for results