From 47e89df2d186b90e92714bb0e9ba656a3894d01c Mon Sep 17 00:00:00 2001 From: thrmdyy <113827218+thrmdyy@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:44:36 +0300 Subject: [PATCH 1/5] added templates (template-react-aleo-hooks | template-react-aleo-hooks-ts) --- create-aleo-app/src/index.ts | 12 ++- .../.eslintrc.cjs | 18 ++++ .../template-react-aleo-hooks-ts/.gitignore | 24 ++++++ .../template-react-aleo-hooks-ts/README.md | 30 +++++++ .../template-react-aleo-hooks-ts/index.html | 13 +++ .../template-react-aleo-hooks-ts/package.json | 30 +++++++ .../public/aleo-hooks.svg | 6 ++ .../template-react-aleo-hooks-ts/src/App.css | 42 ++++++++++ .../template-react-aleo-hooks-ts/src/App.tsx | 42 ++++++++++ .../template-react-aleo-hooks-ts/src/Demo.tsx | 80 ++++++++++++++++++ .../src/assets/react.svg | 1 + .../src/index.css | 68 +++++++++++++++ .../template-react-aleo-hooks-ts/src/main.tsx | 10 +++ .../src/vite-env.d.ts | 1 + .../tsconfig.json | 25 ++++++ .../tsconfig.node.json | 10 +++ .../vite.config.ts | 7 ++ .../template-react-aleo-hooks/.eslintrc.cjs | 20 +++++ .../template-react-aleo-hooks/.gitignore | 24 ++++++ .../template-react-aleo-hooks/README.md | 8 ++ .../template-react-aleo-hooks/index.html | 13 +++ .../template-react-aleo-hooks/package.json | 28 +++++++ .../public/aleo-hooks.svg | 6 ++ .../template-react-aleo-hooks/src/App.css | 42 ++++++++++ .../template-react-aleo-hooks/src/App.jsx | 42 ++++++++++ .../template-react-aleo-hooks/src/Demo.jsx | 82 +++++++++++++++++++ .../src/assets/react.svg | 1 + .../template-react-aleo-hooks/src/index.css | 68 +++++++++++++++ .../template-react-aleo-hooks/src/main.jsx | 10 +++ .../template-react-aleo-hooks/vite.config.js | 7 ++ 30 files changed, 769 insertions(+), 1 deletion(-) create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/.eslintrc.cjs create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/.gitignore create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/README.md create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/index.html create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/package.json create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/public/aleo-hooks.svg create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/src/App.css create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/src/App.tsx create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/src/Demo.tsx create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/src/assets/react.svg create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/src/index.css create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/src/main.tsx create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/src/vite-env.d.ts create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/tsconfig.json create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/tsconfig.node.json create mode 100644 create-aleo-app/template-react-aleo-hooks-ts/vite.config.ts create mode 100644 create-aleo-app/template-react-aleo-hooks/.eslintrc.cjs create mode 100644 create-aleo-app/template-react-aleo-hooks/.gitignore create mode 100644 create-aleo-app/template-react-aleo-hooks/README.md create mode 100644 create-aleo-app/template-react-aleo-hooks/index.html create mode 100644 create-aleo-app/template-react-aleo-hooks/package.json create mode 100644 create-aleo-app/template-react-aleo-hooks/public/aleo-hooks.svg create mode 100644 create-aleo-app/template-react-aleo-hooks/src/App.css create mode 100644 create-aleo-app/template-react-aleo-hooks/src/App.jsx create mode 100644 create-aleo-app/template-react-aleo-hooks/src/Demo.jsx create mode 100644 create-aleo-app/template-react-aleo-hooks/src/assets/react.svg create mode 100644 create-aleo-app/template-react-aleo-hooks/src/index.css create mode 100644 create-aleo-app/template-react-aleo-hooks/src/main.jsx create mode 100644 create-aleo-app/template-react-aleo-hooks/vite.config.js diff --git a/create-aleo-app/src/index.ts b/create-aleo-app/src/index.ts index 1e8d1bb6c..fb28b2dbc 100755 --- a/create-aleo-app/src/index.ts +++ b/create-aleo-app/src/index.ts @@ -3,7 +3,7 @@ import path from "node:path"; import { fileURLToPath } from "node:url"; import minimist from "minimist"; import prompts from "prompts"; -import {lightGreen, cyan, red, reset, yellow, lightGray, green} from "kolorist"; +import {lightGreen, cyan, red, reset, yellow, lightGray, green, blue} from "kolorist"; const argv = minimist<{ t?: string; @@ -58,6 +58,16 @@ const FRAMEWORKS: Framework[] = [ display: "TypeScript + Next.js", color: lightGray, }, + { + name: "react-aleo-hooks", + display: "React + JavaScript + Aleo Hooks", + color: yellow, + }, + { + name: "react-aleo-hooks-ts", + display: "React + TypeScript + Aleo Hooks", + color: blue, + }, ], }, { diff --git a/create-aleo-app/template-react-aleo-hooks-ts/.eslintrc.cjs b/create-aleo-app/template-react-aleo-hooks-ts/.eslintrc.cjs new file mode 100644 index 000000000..d6c953795 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/.eslintrc.cjs @@ -0,0 +1,18 @@ +module.exports = { + root: true, + env: { browser: true, es2020: true }, + extends: [ + 'eslint:recommended', + 'plugin:@typescript-eslint/recommended', + 'plugin:react-hooks/recommended', + ], + ignorePatterns: ['dist', '.eslintrc.cjs'], + parser: '@typescript-eslint/parser', + plugins: ['react-refresh'], + rules: { + 'react-refresh/only-export-components': [ + 'warn', + { allowConstantExport: true }, + ], + }, +} diff --git a/create-aleo-app/template-react-aleo-hooks-ts/.gitignore b/create-aleo-app/template-react-aleo-hooks-ts/.gitignore new file mode 100644 index 000000000..a547bf36d --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/.gitignore @@ -0,0 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/create-aleo-app/template-react-aleo-hooks-ts/README.md b/create-aleo-app/template-react-aleo-hooks-ts/README.md new file mode 100644 index 000000000..0d6babedd --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/README.md @@ -0,0 +1,30 @@ +# React + TypeScript + Vite + +This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. + +Currently, two official plugins are available: + +- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh +- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh + +## Expanding the ESLint configuration + +If you are developing a production application, we recommend updating the configuration to enable type aware lint rules: + +- Configure the top-level `parserOptions` property like this: + +```js +export default { + // other rules... + parserOptions: { + ecmaVersion: 'latest', + sourceType: 'module', + project: ['./tsconfig.json', './tsconfig.node.json'], + tsconfigRootDir: __dirname, + }, +} +``` + +- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked` +- Optionally add `plugin:@typescript-eslint/stylistic-type-checked` +- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list diff --git a/create-aleo-app/template-react-aleo-hooks-ts/index.html b/create-aleo-app/template-react-aleo-hooks-ts/index.html new file mode 100644 index 000000000..b7764c334 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/index.html @@ -0,0 +1,13 @@ + + + + + + + Aleo Hooks + React + TS + + +
+ + + diff --git a/create-aleo-app/template-react-aleo-hooks-ts/package.json b/create-aleo-app/template-react-aleo-hooks-ts/package.json new file mode 100644 index 000000000..a0239807c --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/package.json @@ -0,0 +1,30 @@ +{ + "name": "template-react-aleo-hooks-ts", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "tsc && vite build", + "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", + "preview": "vite preview" + }, + "dependencies": { + "@demox-labs/aleo-wallet-adapter-leo": "^0.0.19", + "aleo-hooks": "^1.0.0", + "react": "^18.2.0", + "react-dom": "^18.2.0" + }, + "devDependencies": { + "@types/react": "^18.2.37", + "@types/react-dom": "^18.2.15", + "@typescript-eslint/eslint-plugin": "^6.10.0", + "@typescript-eslint/parser": "^6.10.0", + "@vitejs/plugin-react": "^4.2.0", + "eslint": "^8.53.0", + "eslint-plugin-react-hooks": "^4.6.0", + "eslint-plugin-react-refresh": "^0.4.4", + "typescript": "^5.2.2", + "vite": "^5.0.0" + } +} diff --git a/create-aleo-app/template-react-aleo-hooks-ts/public/aleo-hooks.svg b/create-aleo-app/template-react-aleo-hooks-ts/public/aleo-hooks.svg new file mode 100644 index 000000000..25074a78f --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/public/aleo-hooks.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/create-aleo-app/template-react-aleo-hooks-ts/src/App.css b/create-aleo-app/template-react-aleo-hooks-ts/src/App.css new file mode 100644 index 000000000..b9d355df2 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/src/App.css @@ -0,0 +1,42 @@ +#root { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} + +.logo { + height: 6em; + padding: 1.5em; + will-change: filter; + transition: filter 300ms; +} +.logo:hover { + filter: drop-shadow(0 0 2em #646cffaa); +} +.logo.react:hover { + filter: drop-shadow(0 0 2em #61dafbaa); +} + +@keyframes logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} + +@media (prefers-reduced-motion: no-preference) { + a:nth-of-type(2) .logo { + animation: logo-spin infinite 20s linear; + } +} + +.card { + padding: 2em; +} + +.read-the-docs { + color: #888; +} diff --git a/create-aleo-app/template-react-aleo-hooks-ts/src/App.tsx b/create-aleo-app/template-react-aleo-hooks-ts/src/App.tsx new file mode 100644 index 000000000..1d38d2513 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/src/App.tsx @@ -0,0 +1,42 @@ +import { useMemo } from 'react' +import { WalletProvider, DecryptPermission } from "aleo-hooks" +import { LeoWalletAdapter } from '@demox-labs/aleo-wallet-adapter-leo' +import { Demo } from './Demo' + +import reactLogo from './assets/react.svg' +import aleoHooksLogo from '/aleo-hooks.svg' + +import './App.css' + +function App() { + const wallets = useMemo(() => [new LeoWalletAdapter({ appName: 'Aleo Hooks + React + TS' })], []) + + return ( + +
+ + Vite logo + + + React logo + +
+

Aleo Hooks + React + TS

+
+ +

+ Edit src/App.tsx and save to test HMR +

+
+

+ Click on the Aleo Hooks and React logos to learn more +

+
+ ) +} + +export default App diff --git a/create-aleo-app/template-react-aleo-hooks-ts/src/Demo.tsx b/create-aleo-app/template-react-aleo-hooks-ts/src/Demo.tsx new file mode 100644 index 000000000..c20465d22 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/src/Demo.tsx @@ -0,0 +1,80 @@ +import { LeoWalletName } from "@demox-labs/aleo-wallet-adapter-leo" +import { useAccount, useConnect, useDisconnect, useTransaction, Transaction, WalletAdapterNetwork } from "aleo-hooks" +import { useState, useCallback, useMemo } from "react" + +export const Demo = () => { + const {connect} = useConnect() + const {disconnect} = useDisconnect() + const {publicKey} = useAccount() + + const connectWalletHandler = useCallback(() => { + connect(LeoWalletName) + }, [connect]) + + const walletContent = useMemo(() => { + if (publicKey) { + return ( + <> +

+ Wallet address: {publicKey} +

+ + + ) + } + + return ( + + ) + }, [connectWalletHandler, disconnect, publicKey]) + + const [receiverAddress, setReceiverAddress] = useState(""); + const [amount, setAmount] = useState(`${1 * 10 ** 6}u64`); + + const receiverAddressChangeHandler = useCallback((e) => setReceiverAddress(e.target.value), []) + const amountChangeHandler = useCallback((e) => setAmount(e.target.value), []) + + const { executeTransaction } = useTransaction() + + const sendCreditsClickHandler = useCallback(() => { + const inputs = [receiverAddress, amount] + + const transaction = Transaction.createTransaction( + receiverAddress, + WalletAdapterNetwork.Testnet, + "credits.aleo", + "transfer_public", + inputs, + 1_000_000, + false + ) + + executeTransaction(transaction) + }, [executeTransaction, receiverAddress, amount]) + + const sendCreditsContent = useMemo(() => { + if (!publicKey) return null; + + return ( +
+

Send credits

+ + + + + +
+ ) + }, [publicKey, receiverAddress, amount, receiverAddressChangeHandler, amountChangeHandler, sendCreditsClickHandler]) + + return ( +
+ {walletContent} + {sendCreditsContent} +
+ ) +} \ No newline at end of file diff --git a/create-aleo-app/template-react-aleo-hooks-ts/src/assets/react.svg b/create-aleo-app/template-react-aleo-hooks-ts/src/assets/react.svg new file mode 100644 index 000000000..6c87de9bb --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/src/assets/react.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/create-aleo-app/template-react-aleo-hooks-ts/src/index.css b/create-aleo-app/template-react-aleo-hooks-ts/src/index.css new file mode 100644 index 000000000..6119ad9a8 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/src/index.css @@ -0,0 +1,68 @@ +:root { + font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; + line-height: 1.5; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} +button:hover { + border-color: #646cff; +} +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; + } + a:hover { + color: #747bff; + } + button { + background-color: #f9f9f9; + } +} diff --git a/create-aleo-app/template-react-aleo-hooks-ts/src/main.tsx b/create-aleo-app/template-react-aleo-hooks-ts/src/main.tsx new file mode 100644 index 000000000..3d7150da8 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/src/main.tsx @@ -0,0 +1,10 @@ +import React from 'react' +import ReactDOM from 'react-dom/client' +import App from './App.tsx' +import './index.css' + +ReactDOM.createRoot(document.getElementById('root')!).render( + + + , +) diff --git a/create-aleo-app/template-react-aleo-hooks-ts/src/vite-env.d.ts b/create-aleo-app/template-react-aleo-hooks-ts/src/vite-env.d.ts new file mode 100644 index 000000000..11f02fe2a --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/src/vite-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/create-aleo-app/template-react-aleo-hooks-ts/tsconfig.json b/create-aleo-app/template-react-aleo-hooks-ts/tsconfig.json new file mode 100644 index 000000000..a7fc6fbf2 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "target": "ES2020", + "useDefineForClassFields": true, + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "module": "ESNext", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react-jsx", + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true + }, + "include": ["src"], + "references": [{ "path": "./tsconfig.node.json" }] +} diff --git a/create-aleo-app/template-react-aleo-hooks-ts/tsconfig.node.json b/create-aleo-app/template-react-aleo-hooks-ts/tsconfig.node.json new file mode 100644 index 000000000..42872c59f --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/tsconfig.node.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "composite": true, + "skipLibCheck": true, + "module": "ESNext", + "moduleResolution": "bundler", + "allowSyntheticDefaultImports": true + }, + "include": ["vite.config.ts"] +} diff --git a/create-aleo-app/template-react-aleo-hooks-ts/vite.config.ts b/create-aleo-app/template-react-aleo-hooks-ts/vite.config.ts new file mode 100644 index 000000000..5a33944a9 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks-ts/vite.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite' +import react from '@vitejs/plugin-react' + +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [react()], +}) diff --git a/create-aleo-app/template-react-aleo-hooks/.eslintrc.cjs b/create-aleo-app/template-react-aleo-hooks/.eslintrc.cjs new file mode 100644 index 000000000..4dcb43901 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/.eslintrc.cjs @@ -0,0 +1,20 @@ +module.exports = { + root: true, + env: { browser: true, es2020: true }, + extends: [ + 'eslint:recommended', + 'plugin:react/recommended', + 'plugin:react/jsx-runtime', + 'plugin:react-hooks/recommended', + ], + ignorePatterns: ['dist', '.eslintrc.cjs'], + parserOptions: { ecmaVersion: 'latest', sourceType: 'module' }, + settings: { react: { version: '18.2' } }, + plugins: ['react-refresh'], + rules: { + 'react-refresh/only-export-components': [ + 'warn', + { allowConstantExport: true }, + ], + }, +} diff --git a/create-aleo-app/template-react-aleo-hooks/.gitignore b/create-aleo-app/template-react-aleo-hooks/.gitignore new file mode 100644 index 000000000..a547bf36d --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/.gitignore @@ -0,0 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/create-aleo-app/template-react-aleo-hooks/README.md b/create-aleo-app/template-react-aleo-hooks/README.md new file mode 100644 index 000000000..f768e33fc --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/README.md @@ -0,0 +1,8 @@ +# React + Vite + +This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. + +Currently, two official plugins are available: + +- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh +- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh diff --git a/create-aleo-app/template-react-aleo-hooks/index.html b/create-aleo-app/template-react-aleo-hooks/index.html new file mode 100644 index 000000000..4eec0b62e --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/index.html @@ -0,0 +1,13 @@ + + + + + + + Aleo Hooks + React + + +
+ + + diff --git a/create-aleo-app/template-react-aleo-hooks/package.json b/create-aleo-app/template-react-aleo-hooks/package.json new file mode 100644 index 000000000..a76e7141a --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/package.json @@ -0,0 +1,28 @@ +{ + "name": "template-react-aleo-hooks", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0", + "preview": "vite preview" + }, + "dependencies": { + "@demox-labs/aleo-wallet-adapter-leo": "^0.0.19", + "aleo-hooks": "^1.0.0", + "react": "^18.2.0", + "react-dom": "^18.2.0" + }, + "devDependencies": { + "@types/react": "^18.2.37", + "@types/react-dom": "^18.2.15", + "@vitejs/plugin-react": "^4.2.0", + "eslint": "^8.53.0", + "eslint-plugin-react": "^7.33.2", + "eslint-plugin-react-hooks": "^4.6.0", + "eslint-plugin-react-refresh": "^0.4.4", + "vite": "^5.0.0" + } +} diff --git a/create-aleo-app/template-react-aleo-hooks/public/aleo-hooks.svg b/create-aleo-app/template-react-aleo-hooks/public/aleo-hooks.svg new file mode 100644 index 000000000..25074a78f --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/public/aleo-hooks.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/create-aleo-app/template-react-aleo-hooks/src/App.css b/create-aleo-app/template-react-aleo-hooks/src/App.css new file mode 100644 index 000000000..b9d355df2 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/src/App.css @@ -0,0 +1,42 @@ +#root { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} + +.logo { + height: 6em; + padding: 1.5em; + will-change: filter; + transition: filter 300ms; +} +.logo:hover { + filter: drop-shadow(0 0 2em #646cffaa); +} +.logo.react:hover { + filter: drop-shadow(0 0 2em #61dafbaa); +} + +@keyframes logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} + +@media (prefers-reduced-motion: no-preference) { + a:nth-of-type(2) .logo { + animation: logo-spin infinite 20s linear; + } +} + +.card { + padding: 2em; +} + +.read-the-docs { + color: #888; +} diff --git a/create-aleo-app/template-react-aleo-hooks/src/App.jsx b/create-aleo-app/template-react-aleo-hooks/src/App.jsx new file mode 100644 index 000000000..7cf8bb25d --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/src/App.jsx @@ -0,0 +1,42 @@ +import { useMemo } from 'react' +import { WalletProvider, DecryptPermission } from "aleo-hooks" +import { LeoWalletAdapter } from '@demox-labs/aleo-wallet-adapter-leo' + +import reactLogo from './assets/react.svg' +import aleoHooksLogo from '/aleo-hooks.svg' + +import './App.css' +import { Demo } from './Demo' + +function App() { + const wallets = useMemo(() => [new LeoWalletAdapter({ appName: 'Aleo Hooks + React' })], []) + + return ( + +
+ + Vite logo + + + React logo + +
+

Aleo Hooks + React

+
+ +

+ Edit src/App.jsx and save to test HMR +

+
+

+ Click on the Aleo Hooks and React logos to learn more +

+
+ ) +} + +export default App diff --git a/create-aleo-app/template-react-aleo-hooks/src/Demo.jsx b/create-aleo-app/template-react-aleo-hooks/src/Demo.jsx new file mode 100644 index 000000000..2047f971f --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/src/Demo.jsx @@ -0,0 +1,82 @@ +import { LeoWalletName } from "@demox-labs/aleo-wallet-adapter-leo" +import { useAccount, useConnect, useDisconnect, useTransaction, Transaction, WalletAdapterNetwork } from "aleo-hooks" +import { useState } from "react" +import { useCallback } from "react" +import { useMemo } from "react" + +export const Demo = () => { + const {connect} = useConnect() + const {disconnect} = useDisconnect() + const {publicKey} = useAccount() + + const connectWalletHandler = useCallback(() => { + connect(LeoWalletName) + }, [connect]) + + const walletContent = useMemo(() => { + if (publicKey) { + return ( + <> +

+ Wallet address: {publicKey} +

+ + + ) + } + + return ( + + ) + }, [connectWalletHandler, disconnect, publicKey]) + + const [receiverAddress, setReceiverAddress] = useState(""); + const [amount, setAmount] = useState(`${1 * 10 ** 6}u64`); + + const receiverAddressChangeHandler = useCallback((e) => setReceiverAddress(e.target.value), []) + const amountChangeHandler = useCallback((e) => setAmount(e.target.value), []) + + const { executeTransaction } = useTransaction() + + const sendCreditsClickHandler = useCallback(() => { + const inputs = [receiverAddress, amount] + + const transaction = Transaction.createTransaction( + receiverAddress, + WalletAdapterNetwork.Testnet, + "credits.aleo", + "transfer_public", + inputs, + 1_000_000, + false + ) + + executeTransaction(transaction) + }, [executeTransaction, receiverAddress, amount]) + + const sendCreditsContent = useMemo(() => { + if (!publicKey) return null; + + return ( +
+

Send credits

+ + + + + +
+ ) + }, [publicKey, receiverAddress, amount, receiverAddressChangeHandler, amountChangeHandler, sendCreditsClickHandler]) + + return ( +
+ {walletContent} + {sendCreditsContent} +
+ ) +} \ No newline at end of file diff --git a/create-aleo-app/template-react-aleo-hooks/src/assets/react.svg b/create-aleo-app/template-react-aleo-hooks/src/assets/react.svg new file mode 100644 index 000000000..6c87de9bb --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/src/assets/react.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/create-aleo-app/template-react-aleo-hooks/src/index.css b/create-aleo-app/template-react-aleo-hooks/src/index.css new file mode 100644 index 000000000..6119ad9a8 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/src/index.css @@ -0,0 +1,68 @@ +:root { + font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; + line-height: 1.5; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} +button:hover { + border-color: #646cff; +} +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; + } + a:hover { + color: #747bff; + } + button { + background-color: #f9f9f9; + } +} diff --git a/create-aleo-app/template-react-aleo-hooks/src/main.jsx b/create-aleo-app/template-react-aleo-hooks/src/main.jsx new file mode 100644 index 000000000..54b39dd1d --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/src/main.jsx @@ -0,0 +1,10 @@ +import React from 'react' +import ReactDOM from 'react-dom/client' +import App from './App.jsx' +import './index.css' + +ReactDOM.createRoot(document.getElementById('root')).render( + + + , +) diff --git a/create-aleo-app/template-react-aleo-hooks/vite.config.js b/create-aleo-app/template-react-aleo-hooks/vite.config.js new file mode 100644 index 000000000..5a33944a9 --- /dev/null +++ b/create-aleo-app/template-react-aleo-hooks/vite.config.js @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite' +import react from '@vitejs/plugin-react' + +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [react()], +}) From 1b1926b7d929c3b0f15a5ad6b94d20d8120d3d84 Mon Sep 17 00:00:00 2001 From: lexx Date: Sun, 3 Dec 2023 20:54:21 +0300 Subject: [PATCH 2/5] Update README.md Signed-off-by: lexx --- .../template-react-aleo-hooks-ts/README.md | 137 +++++++++++++++--- 1 file changed, 117 insertions(+), 20 deletions(-) diff --git a/create-aleo-app/template-react-aleo-hooks-ts/README.md b/create-aleo-app/template-react-aleo-hooks-ts/README.md index 0d6babedd..cc4394bb1 100644 --- a/create-aleo-app/template-react-aleo-hooks-ts/README.md +++ b/create-aleo-app/template-react-aleo-hooks-ts/README.md @@ -1,30 +1,127 @@ -# React + TypeScript + Vite +# Aleo + Aleo React Hooks + Leo -This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. +This template includes a simple Aleo dApp that allows a user to connect Leo wallet with Aleo Hooks library. -Currently, two official plugins are available: +To build and run in development mode: -- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh -- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh +``` +npm i +npm run dev +``` + +## How to use + +The detailed documentation can be found here: https://aleo-hooks.gitbook.io/aleo-hooks/ + +A few most commonly used hooks: -## Expanding the ESLint configuration +#### useConnect -If you are developing a production application, we recommend updating the configuration to enable type aware lint rules: +The connection flow for the aleo wallets extension goes like this: -- Configure the top-level `parserOptions` property like this: +```javascript +import { FC } from 'react' +import { useConnect } from 'aleo-hooks' +import { LeoWalletName } from '@demox-labs/aleo-wallet-adapter-leo' -```js -export default { - // other rules... - parserOptions: { - ecmaVersion: 'latest', - sourceType: 'module', - project: ['./tsconfig.json', './tsconfig.node.json'], - tsconfigRootDir: __dirname, - }, +export const UseConnect: FC = () => { + const { connect, connected, connecting, error } = useConnect() + + const connectHandler = () => connect(LeoWalletName) + + return ( +
+ {connected &&

Successfuly connected

} + {error &&

Something went wrong {JSON.stringify(error)}

} + +
+ ) } ``` -- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked` -- Optionally add `plugin:@typescript-eslint/stylistic-type-checked` -- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list +#### useSelect + +Use this hook for selecting current Aleo wallet. This hook is required when your frontend supports more than one wallet, e.g. Leo and Puzzle. + +```javascript +import { FC } from 'react' +import { useSelect } from 'aleo-hooks' +import { LeoWalletName } from '@demox-labs/aleo-wallet-adapter-leo' + +export const UseSelect: FC = () => { + const { select } = useSelect() + + const selectHandler = () => select(LeoWalletName) + + return ( +
+ +
+ ) +} +``` + +#### useDisconnect + +Use this hook to disconnect current Aleo wallet: + +```javascript +import { FC } from 'react' +import { useDisconnect } from 'aleo-hooks' + +export const UseDisconnect: FC = () => { + const { disconnect, disconnecting, error } = useDisconnect() + + const disconnectHandler = () => disconnect() + + return ( +
+ {error &&

Something went wrong {JSON.stringify(error)}

} + +
+ ) +} +``` + +#### useDecrypt + +For decrypting ciphered text + +```javascript +import { FC } from 'react' +import { useDecrypt } from 'aleo-hooks' + +export const UseDecrypt: FC = () => { + const { decryptedText, loading, error } = useDecrypt({ cipherText: 'ciphertext1qgqtzwpwj2r0rw0md3zxlnnj9h7azun02f6tdm27u8ywxcsuw4pssp7xsp7edm749l4pd9s47wksc475dkhmjnl7yrzzylgnfyx2kfwkpqlsynj2' }) + + return ( +
+ {loading &&

Loading...

} + {error &&

Something went wrong {JSON.stringify(error)}

} + {decryptedText &&

Decrypted text: {decryptedText}

} +
+ ) +} +``` + +#### useViewKey + +If adapter supporting fetching a view key, this hook returns a plaintext view key. + +```javascript +import { FC } from 'react' +import { useViewKey } from 'aleo-hooks' + +export const UseViewKey: FC = () => { + const { viewKey, requestViewKey, error, loading } = useViewKey() + + return ( +
+ {loading &&

Loading...

} + {error &&

Something went wrong {JSON.stringify(error)}

} + {viewKey &&

View key: {viewKey}

} + +
+ ) +} +``` From a2424b74a5b476b630867070eef4f370a1a1df67 Mon Sep 17 00:00:00 2001 From: lexx Date: Sun, 3 Dec 2023 20:56:31 +0300 Subject: [PATCH 3/5] Update README.md Signed-off-by: lexx --- .../template-react-aleo-hooks-ts/README.md | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/create-aleo-app/template-react-aleo-hooks-ts/README.md b/create-aleo-app/template-react-aleo-hooks-ts/README.md index cc4394bb1..a19ce728b 100644 --- a/create-aleo-app/template-react-aleo-hooks-ts/README.md +++ b/create-aleo-app/template-react-aleo-hooks-ts/README.md @@ -125,3 +125,156 @@ export const UseViewKey: FC = () => { ) } ``` + +#### useTransaction + + +``` +import { FC, useCallback, useState } from 'react' +import { Button } from '../button' +import { Input } from '../input' +import { cn } from '@bem-react/classname' +import { useTransaction, Transaction, WalletAdapterNetwork, useConnect } from 'aleo-hooks' + +import './execute-transaction.scss' + +const CnExecuteTransaction = cn('sign-message') + +interface IFormData { + program: string + function: string + fee: number + address: string + amount: string +} + +type IFormDataKeys = keyof IFormData + +const placeholderFromFormDataKey: Record = { + program: 'Program', + function: 'Function', + fee: 'Fee', + address: 'address', + amount: `Amount`, +} + +export const ExecuteTransaction: FC = () => { + const { transactionId, executeTransaction, error, loading } = useTransaction() + const { address, connected } = useConnect() + + const [formData, setFormData] = useState({ + program: 'credits.aleo', + function: 'transfer_public', + fee: 1_000_000, + amount: `${1 * 10 ** 6}u64`, + address: '', + }) + + const formChangeHandler = useCallback((key: IFormDataKeys) => { + return (e: React.ChangeEvent) => { + setFormData((prev) => ({ + ...prev, + [key]: key === 'fee' ? Number(e.target.value) : e.target.value, + })) + } + }, []) + + const submit = useCallback(() => { + const inputs: any = [formData.address, formData.amount] + + const aleoTransaction = Transaction.createTransaction( + address as string, + WalletAdapterNetwork.Testnet, + formData.program, + formData.function, + inputs, + 1_000_000, + false, + ) + + executeTransaction(aleoTransaction) + }, [executeTransaction, address, formData]) + + return ( +
+

Execute transaction

+ +
+
Loading: {String(loading)}
+
Error: {error?.message}
+
+ Transaction id: {transactionId} +
+
+ +
+ {Object.keys(formData).map((key) => { + const value = formData[key as IFormDataKeys] + + return ( + + ) + })} + + +
+
+ ) +} +``` + +#### useTransaction + + +``` +import { useTransactionStatus } from 'aleo-hooks' +import { FC, useCallback, useState } from 'react' +import { Button } from '../button' +import { Input } from '../input' +import { cn } from '@bem-react/classname' + +import './transaction-status.scss' + +const CnTransactionStatus = cn('transactionStatus') + +export const TransactionStatus: FC = () => { + const { data, error, loading, requestTransactionStatus } = useTransactionStatus() + const [transactionId, setTransactionId] = useState('') + + const transactionIdChangeCallback = useCallback((e: React.ChangeEvent) => { + setTransactionId(e.target.value) + }, []) + + const submit = useCallback(() => { + requestTransactionStatus(transactionId) + }, [transactionId, requestTransactionStatus]) + + return ( +
+

Request transaction status

+ +
+
Loading: {String(loading)}
+
Error: {error?.message}
+
Transaction status: {data}
+
+ + + +
+ ) +} +``` From b5c435943d17e1ba16df2060a1928dc9d9205d7d Mon Sep 17 00:00:00 2001 From: lexx Date: Sun, 3 Dec 2023 20:56:55 +0300 Subject: [PATCH 4/5] Update README.md Signed-off-by: lexx --- create-aleo-app/template-react-aleo-hooks-ts/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/create-aleo-app/template-react-aleo-hooks-ts/README.md b/create-aleo-app/template-react-aleo-hooks-ts/README.md index a19ce728b..4d0c97d93 100644 --- a/create-aleo-app/template-react-aleo-hooks-ts/README.md +++ b/create-aleo-app/template-react-aleo-hooks-ts/README.md @@ -129,7 +129,7 @@ export const UseViewKey: FC = () => { #### useTransaction -``` +```javascript import { FC, useCallback, useState } from 'react' import { Button } from '../button' import { Input } from '../input' @@ -235,7 +235,7 @@ export const ExecuteTransaction: FC = () => { #### useTransaction -``` +```javascript import { useTransactionStatus } from 'aleo-hooks' import { FC, useCallback, useState } from 'react' import { Button } from '../button' From b61d9b3c3d253c66731ba1c89dfd3305b8a43e6b Mon Sep 17 00:00:00 2001 From: lexx Date: Sun, 3 Dec 2023 20:57:16 +0300 Subject: [PATCH 5/5] Update README.md Signed-off-by: lexx --- .../template-react-aleo-hooks/README.md | 282 +++++++++++++++++- 1 file changed, 277 insertions(+), 5 deletions(-) diff --git a/create-aleo-app/template-react-aleo-hooks/README.md b/create-aleo-app/template-react-aleo-hooks/README.md index f768e33fc..4d0c97d93 100644 --- a/create-aleo-app/template-react-aleo-hooks/README.md +++ b/create-aleo-app/template-react-aleo-hooks/README.md @@ -1,8 +1,280 @@ -# React + Vite +# Aleo + Aleo React Hooks + Leo -This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. +This template includes a simple Aleo dApp that allows a user to connect Leo wallet with Aleo Hooks library. -Currently, two official plugins are available: +To build and run in development mode: -- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh -- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh +``` +npm i +npm run dev +``` + +## How to use + +The detailed documentation can be found here: https://aleo-hooks.gitbook.io/aleo-hooks/ + +A few most commonly used hooks: + +#### useConnect + +The connection flow for the aleo wallets extension goes like this: + +```javascript +import { FC } from 'react' +import { useConnect } from 'aleo-hooks' +import { LeoWalletName } from '@demox-labs/aleo-wallet-adapter-leo' + +export const UseConnect: FC = () => { + const { connect, connected, connecting, error } = useConnect() + + const connectHandler = () => connect(LeoWalletName) + + return ( +
+ {connected &&

Successfuly connected

} + {error &&

Something went wrong {JSON.stringify(error)}

} + +
+ ) +} +``` + +#### useSelect + +Use this hook for selecting current Aleo wallet. This hook is required when your frontend supports more than one wallet, e.g. Leo and Puzzle. + +```javascript +import { FC } from 'react' +import { useSelect } from 'aleo-hooks' +import { LeoWalletName } from '@demox-labs/aleo-wallet-adapter-leo' + +export const UseSelect: FC = () => { + const { select } = useSelect() + + const selectHandler = () => select(LeoWalletName) + + return ( +
+ +
+ ) +} +``` + +#### useDisconnect + +Use this hook to disconnect current Aleo wallet: + +```javascript +import { FC } from 'react' +import { useDisconnect } from 'aleo-hooks' + +export const UseDisconnect: FC = () => { + const { disconnect, disconnecting, error } = useDisconnect() + + const disconnectHandler = () => disconnect() + + return ( +
+ {error &&

Something went wrong {JSON.stringify(error)}

} + +
+ ) +} +``` + +#### useDecrypt + +For decrypting ciphered text + +```javascript +import { FC } from 'react' +import { useDecrypt } from 'aleo-hooks' + +export const UseDecrypt: FC = () => { + const { decryptedText, loading, error } = useDecrypt({ cipherText: 'ciphertext1qgqtzwpwj2r0rw0md3zxlnnj9h7azun02f6tdm27u8ywxcsuw4pssp7xsp7edm749l4pd9s47wksc475dkhmjnl7yrzzylgnfyx2kfwkpqlsynj2' }) + + return ( +
+ {loading &&

Loading...

} + {error &&

Something went wrong {JSON.stringify(error)}

} + {decryptedText &&

Decrypted text: {decryptedText}

} +
+ ) +} +``` + +#### useViewKey + +If adapter supporting fetching a view key, this hook returns a plaintext view key. + +```javascript +import { FC } from 'react' +import { useViewKey } from 'aleo-hooks' + +export const UseViewKey: FC = () => { + const { viewKey, requestViewKey, error, loading } = useViewKey() + + return ( +
+ {loading &&

Loading...

} + {error &&

Something went wrong {JSON.stringify(error)}

} + {viewKey &&

View key: {viewKey}

} + +
+ ) +} +``` + +#### useTransaction + + +```javascript +import { FC, useCallback, useState } from 'react' +import { Button } from '../button' +import { Input } from '../input' +import { cn } from '@bem-react/classname' +import { useTransaction, Transaction, WalletAdapterNetwork, useConnect } from 'aleo-hooks' + +import './execute-transaction.scss' + +const CnExecuteTransaction = cn('sign-message') + +interface IFormData { + program: string + function: string + fee: number + address: string + amount: string +} + +type IFormDataKeys = keyof IFormData + +const placeholderFromFormDataKey: Record = { + program: 'Program', + function: 'Function', + fee: 'Fee', + address: 'address', + amount: `Amount`, +} + +export const ExecuteTransaction: FC = () => { + const { transactionId, executeTransaction, error, loading } = useTransaction() + const { address, connected } = useConnect() + + const [formData, setFormData] = useState({ + program: 'credits.aleo', + function: 'transfer_public', + fee: 1_000_000, + amount: `${1 * 10 ** 6}u64`, + address: '', + }) + + const formChangeHandler = useCallback((key: IFormDataKeys) => { + return (e: React.ChangeEvent) => { + setFormData((prev) => ({ + ...prev, + [key]: key === 'fee' ? Number(e.target.value) : e.target.value, + })) + } + }, []) + + const submit = useCallback(() => { + const inputs: any = [formData.address, formData.amount] + + const aleoTransaction = Transaction.createTransaction( + address as string, + WalletAdapterNetwork.Testnet, + formData.program, + formData.function, + inputs, + 1_000_000, + false, + ) + + executeTransaction(aleoTransaction) + }, [executeTransaction, address, formData]) + + return ( +
+

Execute transaction

+ +
+
Loading: {String(loading)}
+
Error: {error?.message}
+
+ Transaction id: {transactionId} +
+
+ +
+ {Object.keys(formData).map((key) => { + const value = formData[key as IFormDataKeys] + + return ( + + ) + })} + + +
+
+ ) +} +``` + +#### useTransaction + + +```javascript +import { useTransactionStatus } from 'aleo-hooks' +import { FC, useCallback, useState } from 'react' +import { Button } from '../button' +import { Input } from '../input' +import { cn } from '@bem-react/classname' + +import './transaction-status.scss' + +const CnTransactionStatus = cn('transactionStatus') + +export const TransactionStatus: FC = () => { + const { data, error, loading, requestTransactionStatus } = useTransactionStatus() + const [transactionId, setTransactionId] = useState('') + + const transactionIdChangeCallback = useCallback((e: React.ChangeEvent) => { + setTransactionId(e.target.value) + }, []) + + const submit = useCallback(() => { + requestTransactionStatus(transactionId) + }, [transactionId, requestTransactionStatus]) + + return ( +
+

Request transaction status

+ +
+
Loading: {String(loading)}
+
Error: {error?.message}
+
Transaction status: {data}
+
+ + + +
+ ) +} +```