-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(demos): add demos/taco-nft-demo
- Loading branch information
1 parent
54bf317
commit 620b755
Showing
19 changed files
with
3,554 additions
and
2,863 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# `@nucypher/*` Demos | ||
|
||
- [`taco-demo`](./taco-demo) - A demo of the `@nucypher/taco` library. | ||
- [`taco-nft-demo`](./taco-nft-demo) - A demo an NFT-based condition using the `@nucypher/taco` library. | ||
- [`nucypher-ts-demo`](https://github.com/nucypher/nucypher-ts-demo) - A demo of PRE in the `nucypher-ts` library. | ||
- [`tdec-sandbox`](https://github.com/nucypher/tdec-sandbox) - A demo of tDec in the `nucypher-ts` library. | ||
- [`tdec-nft-example`](https://github.com/nucypher/tdec-nft-example) - A demo of tDec in the `nucypher-ts` library. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# taco-nft-example | ||
|
||
## Installation | ||
|
||
```bash | ||
pnpm install | ||
pnpm start | ||
``` | ||
|
||
## Usage | ||
|
||
In order to run this demo will need a MetaMask with an account funded with some | ||
$MATIC. | ||
|
||
In order to connect with the network, the demo uses a public instances of | ||
[Porter](https://docs.nucypher.com/en/latest/application_development/web_development.html#running-porter). | ||
|
||
### Polygon | ||
|
||
`@nucypher/taco` is in an early release. We recommend **not** using it in | ||
production _just yet_. | ||
|
||
### Tapir - Mumbai Testnet | ||
|
||
The current release of `@nucypher/taco` supports Ursulas working on Tapir | ||
network and contracts deployed on Mumbai testnet. | ||
|
||
## References | ||
|
||
This dApp is based on | ||
[`useDapp` example](https://github.com/EthWorks/useDapp/tree/master/packages/example). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "taco-nft-demo", | ||
"version": "0.1.0", | ||
"description": "A usage example for @nucypher/taco", | ||
"private": true, | ||
"author": "Piotr Rosłaniec <p.roslaniec@gmail.com>", | ||
"scripts": { | ||
"start": "webpack serve --mode development", | ||
"build": "tsc --noEmit && rimraf build && webpack --mode production --progress" | ||
}, | ||
"dependencies": { | ||
"@nucypher/taco": "workspace:*", | ||
"@usedapp/core": "^1.2.13", | ||
"buffer": "^6.0.3", | ||
"ethers": "^5.7.1", | ||
"file-loader": "^6.2.0", | ||
"react": "^18.2.0", | ||
"react-copy-to-clipboard": "^5.1.0", | ||
"react-dom": "^18.2.0", | ||
"react-spinners": "^0.13.6" | ||
}, | ||
"devDependencies": { | ||
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7", | ||
"@types/react": "^18.0.20", | ||
"@types/react-copy-to-clipboard": "^5.0.4", | ||
"@types/react-dom": "^18.0.6", | ||
"copy-webpack-plugin": "^11.0.0", | ||
"esbuild-loader": "^2.20.0", | ||
"html-webpack-plugin": "^5.5.0", | ||
"react-refresh": "^0.14.0", | ||
"rimraf": "^3.0.2", | ||
"stream-browserify": "^3.0.0", | ||
"typescript": "^4.8.3", | ||
"webpack": "^5.74.0", | ||
"webpack-cli": "^4.10.0", | ||
"webpack-dev-server": "^4.11.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { | ||
conditions, | ||
decrypt, | ||
encrypt, | ||
getPorterUri, | ||
initialize, | ||
ThresholdMessageKit, | ||
} from '@nucypher/taco'; | ||
import {Mumbai, useEthers} from '@usedapp/core'; | ||
import {ethers} from 'ethers'; | ||
import React, {useEffect, useState} from 'react'; | ||
|
||
import {Decrypt} from './Decrypt'; | ||
import {Encrypt} from './Encrypt'; | ||
import {NFTConditionBuilder} from './NFTConditionBuilder'; | ||
import {Spinner} from './Spinner'; | ||
|
||
export default function App() { | ||
const {activateBrowserWallet, deactivate, account, switchNetwork } = | ||
useEthers(); | ||
|
||
const [loading, setLoading] = useState(false); | ||
const [condition, setCondition] = useState<conditions.Condition>(); | ||
const [encryptedMessage, setEncryptedMessage] = | ||
useState<ThresholdMessageKit>(); | ||
const [decryptedMessage, setDecryptedMessage] = useState<string>(); | ||
const [decryptionErrors, setDecryptionErrors] = useState<string[]>([]); | ||
|
||
useEffect(() => { | ||
initialize(); | ||
}, []); | ||
|
||
const encryptMessage = async (message: string) => { | ||
if (!condition) { | ||
return; | ||
} | ||
setLoading(true); | ||
|
||
await switchNetwork(Mumbai.chainId); | ||
|
||
const provider = new ethers.providers.Web3Provider(window.ethereum); | ||
const ritualId = 2; // Replace with your own ritual ID | ||
const encryptedMessage = await encrypt( | ||
provider, | ||
message, | ||
condition, | ||
ritualId, | ||
provider.getSigner(), | ||
); | ||
|
||
setEncryptedMessage(encryptedMessage); | ||
setLoading(false); | ||
}; | ||
|
||
const decryptMessage = async (encryptedMessage: ThresholdMessageKit) => { | ||
if (!condition) { | ||
return; | ||
} | ||
setLoading(true); | ||
setDecryptedMessage(''); | ||
setDecryptionErrors([]); | ||
|
||
const provider = new ethers.providers.Web3Provider(window.ethereum); | ||
const porterUri = getPorterUri('lynx'); | ||
const decryptedMessage = await decrypt( | ||
provider, | ||
encryptedMessage, | ||
provider.getSigner(), | ||
porterUri, | ||
); | ||
|
||
setDecryptedMessage(new TextDecoder().decode(decryptedMessage)); | ||
setLoading(false); | ||
}; | ||
|
||
if (!account) { | ||
return ( | ||
<div> | ||
<h2>Web3 Provider</h2> | ||
<button onClick={() => activateBrowserWallet()}>Connect Wallet</button> | ||
</div> | ||
); | ||
} | ||
|
||
if (loading) { | ||
return <Spinner loading={loading}/>; | ||
} | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<h2>Web3 Provider</h2> | ||
<button onClick={deactivate}> Disconnect Wallet</button> | ||
{account && <p>Account: {account}</p>} | ||
</div> | ||
|
||
<NFTConditionBuilder | ||
enabled={true} | ||
condition={condition} | ||
setConditions={setCondition} | ||
/> | ||
|
||
<Encrypt | ||
enabled={!!condition} | ||
encrypt={encryptMessage} | ||
encryptedMessage={encryptedMessage!} | ||
/> | ||
|
||
<Decrypt | ||
enabled={!!encryptedMessage} | ||
decrypt={decryptMessage} | ||
decryptedMessage={decryptedMessage} | ||
decryptionErrors={decryptionErrors} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { ThresholdMessageKit } from '@nucypher/taco'; | ||
import React, { useState } from 'react'; | ||
|
||
interface Props { | ||
enabled: boolean; | ||
decrypt: (encryptedMessage: ThresholdMessageKit) => void; | ||
decryptedMessage?: string | undefined; | ||
decryptionErrors: string[]; | ||
} | ||
|
||
export const Decrypt = ({ | ||
decrypt, | ||
decryptedMessage, | ||
decryptionErrors, | ||
enabled, | ||
}: Props) => { | ||
const [encryptedMessage, setEncryptedMessage] = useState(''); | ||
|
||
if (!enabled) { | ||
return <></>; | ||
} | ||
|
||
const onDecrypt = () => { | ||
if (!encryptedMessage) { | ||
return; | ||
} | ||
const mkBytes = Buffer.from(encryptedMessage, 'base64'); | ||
const mk = ThresholdMessageKit.fromBytes(mkBytes); | ||
decrypt(mk); | ||
}; | ||
|
||
const DecryptedMessage = () => { | ||
if (!decryptedMessage) { | ||
return <></>; | ||
} | ||
return ( | ||
<> | ||
<h3>Decrypted Message:</h3> | ||
<p>{decryptedMessage}</p> | ||
</> | ||
); | ||
}; | ||
|
||
const DecryptionErrors = () => { | ||
if (decryptionErrors.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>Decryption Errors</h2> | ||
<p>Not enough decryption shares to decrypt the message.</p> | ||
<p>Some Ursulas have failed with errors:</p> | ||
<ul> | ||
{decryptionErrors.map((error, index) => ( | ||
<li key={index}>{error}</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h2>Step 3 - Decrypt Encrypted Message</h2> | ||
<input | ||
value={encryptedMessage} | ||
placeholder="Enter encrypted message" | ||
onChange={(e) => setEncryptedMessage(e.currentTarget.value)} | ||
/> | ||
<button onClick={onDecrypt}>Decrypt</button> | ||
{DecryptedMessage()} | ||
{DecryptionErrors()} | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.