From 9ce3b25f829ce72838efcdd78b4d012da815165c Mon Sep 17 00:00:00 2001 From: QSchlegel Date: Wed, 5 Mar 2025 15:25:17 +0100 Subject: [PATCH 01/28] wallet test 01 --- package-lock.json | 7 + package.json | 1 + .../homepage/wallets/invite/cip146Wallet.tsx | 137 ++++++++++++++++++ .../pages/homepage/wallets/invite/index.tsx | 4 + 4 files changed, 149 insertions(+) create mode 100644 src/components/pages/homepage/wallets/invite/cip146Wallet.tsx diff --git a/package-lock.json b/package-lock.json index 0edb9445..2998b1c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "hasInstallScript": true, "dependencies": { "@auth/prisma-adapter": "^1.6.0", + "@emurgo/cardano-serialization-lib-browser": "^14.1.1", "@hookform/resolvers": "^3.9.0", "@jinglescode/nostr-chat-plugin": "^0.0.11", "@meshsdk/core": "^1.8.5", @@ -1144,6 +1145,12 @@ "integrity": "sha512-PQRc8K8wZshEdmQenNUzVtiI8oJNF/1uAnBhidee5C4o1l2mDLOW+ur46HWHIFKQ6x8mSJTllcjMscHgzju0gQ==", "license": "MIT" }, + "node_modules/@emurgo/cardano-serialization-lib-browser": { + "version": "14.1.1", + "resolved": "https://registry.npmjs.org/@emurgo/cardano-serialization-lib-browser/-/cardano-serialization-lib-browser-14.1.1.tgz", + "integrity": "sha512-/zGHQoXJCQw0yyEdoVQMDlSsyvaCap2lNVKEu+GTi1pwYopn6K3/SntrDFW5/gIMireDwY4+kZcPvXwIBZnMZA==", + "license": "MIT" + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.25.0", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.0.tgz", diff --git a/package.json b/package.json index fae29b14..04a5245d 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "dependencies": { "@auth/prisma-adapter": "^1.6.0", + "@emurgo/cardano-serialization-lib-browser": "^14.1.1", "@hookform/resolvers": "^3.9.0", "@jinglescode/nostr-chat-plugin": "^0.0.11", "@meshsdk/core": "^1.8.5", diff --git a/src/components/pages/homepage/wallets/invite/cip146Wallet.tsx b/src/components/pages/homepage/wallets/invite/cip146Wallet.tsx new file mode 100644 index 00000000..a0b0ea11 --- /dev/null +++ b/src/components/pages/homepage/wallets/invite/cip146Wallet.tsx @@ -0,0 +1,137 @@ +import React, { useState, useRef } from 'react'; +import { Bip32PrivateKey } from '@emurgo/cardano-serialization-lib-browser'; +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; +import { Button } from '@/components/ui/button'; + +const KeyGenerator = () => { + const [keys, setKeys] = useState({ + rootPrivateKey: '', + rootPublicKey: '', + utxoPubKey: '', + stakeKey: '', + drepKey: '' + }); + const fileInputRef = useRef(null); + + // Helper to derive all keys from the root key. + const deriveKeys = (rootKey) => { + const rootPublicKey = rootKey.to_public(); + const accountKey = rootKey + .derive(1854 | 0x80000000) + .derive(1815 | 0x80000000) + .derive(0 | 0x80000000); + const utxoPubKey = accountKey.derive(0).derive(0).to_public(); + const stakeKey = accountKey.derive(2).derive(0).to_public(); + const drepKey = accountKey.derive(3).derive(0).to_public(); + return { + rootPrivateKey: rootKey.to_hex(), + rootPublicKey: rootPublicKey.to_hex(), + utxoPubKey: utxoPubKey.to_hex(), + stakeKey: stakeKey.to_hex(), + drepKey: drepKey.to_hex() + }; + }; + + // Generate a new root key and derive its keys. + const generateKeys = () => { + const entropy = new Uint8Array(32); + window.crypto.getRandomValues(entropy); + const rootKey = Bip32PrivateKey.from_bip39_entropy(entropy, new Uint8Array()); + const newKeys = deriveKeys(rootKey); + setKeys(newKeys); + }; + + // Export only the root private key as JSON. + const exportKeys = () => { + if (!keys.rootPrivateKey) { + alert('No keys to export. Please generate keys first.'); + return; + } + const dataStr = JSON.stringify({ rootPrivateKey: keys.rootPrivateKey }, null, 2); + const blob = new Blob([dataStr], { type: 'application/json' }); + const url = URL.createObjectURL(blob); + const link = document.createElement('a'); + link.href = url; + link.download = 'keys.json'; + link.click(); + URL.revokeObjectURL(url); + }; + + // Import the root key, then re-derive all keys. + const importKeys = (e) => { + const fileReader = new FileReader(); + fileReader.onload = (event) => { + try { + const importedData = JSON.parse(event.target.result); + if (importedData.rootPrivateKey) { + const rootKey = Bip32PrivateKey.from_hex(importedData.rootPrivateKey); + const newKeys = deriveKeys(rootKey); + setKeys(newKeys); + } else { + alert('Invalid key file. Root key not found.'); + } + } catch (error) { + console.error(error); + alert('Failed to import keys.'); + } + }; + if (e.target.files && e.target.files.length > 0) { + fileReader.readAsText(e.target.files[0]); + } + }; + + return ( + + + Generate New Keys + + +
+ +
+ + + +
+
+ {keys.rootPrivateKey && ( +
+

Derived Keys:

+

+ Root Private Key: {keys.rootPrivateKey} +

+

+ Root Public Key: {keys.rootPublicKey} +

+

+ UTXO Public Key: {keys.utxoPubKey} +

+

+ Stake Key: {keys.stakeKey} +

+

+ Drep Key: {keys.drepKey} +

+
+ )} +
+
+ ); +}; + +export default KeyGenerator; \ No newline at end of file diff --git a/src/components/pages/homepage/wallets/invite/index.tsx b/src/components/pages/homepage/wallets/invite/index.tsx index a7042a30..158928e8 100644 --- a/src/components/pages/homepage/wallets/invite/index.tsx +++ b/src/components/pages/homepage/wallets/invite/index.tsx @@ -14,6 +14,7 @@ import { api } from "@/utils/api"; import { useUserStore } from "@/lib/zustand/user"; import { useRouter } from "next/router"; import { useToast } from "@/hooks/use-toast"; +import KeyGenerator from "./cip146Wallet"; export default function PageNewWalletInvite() { const router = useRouter(); @@ -108,6 +109,9 @@ export default function PageNewWalletInvite() { + + +
From 917b43296ace0e89b6710cc292596d0da1fa120b Mon Sep 17 00:00:00 2001 From: QSchlegel Date: Fri, 7 Mar 2025 16:29:57 +0100 Subject: [PATCH 02/28] t 02 --- .../invite/cip146/146ExportMultisig.tsx | 143 ++++++++++++++++++ .../wallets/invite/cip146/146GenAcct.tsx | 139 +++++++++++++++++ .../wallets/invite/cip146/146Import.tsx | 96 ++++++++++++ .../wallets/invite/cip146/146Wallet.tsx | 133 ++++++++++++++++ .../homepage/wallets/invite/cip146Wallet.tsx | 137 ----------------- .../pages/homepage/wallets/invite/index.tsx | 2 +- 6 files changed, 512 insertions(+), 138 deletions(-) create mode 100644 src/components/pages/homepage/wallets/invite/cip146/146ExportMultisig.tsx create mode 100644 src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx create mode 100644 src/components/pages/homepage/wallets/invite/cip146/146Import.tsx create mode 100644 src/components/pages/homepage/wallets/invite/cip146/146Wallet.tsx delete mode 100644 src/components/pages/homepage/wallets/invite/cip146Wallet.tsx diff --git a/src/components/pages/homepage/wallets/invite/cip146/146ExportMultisig.tsx b/src/components/pages/homepage/wallets/invite/cip146/146ExportMultisig.tsx new file mode 100644 index 00000000..6ca6e8f8 --- /dev/null +++ b/src/components/pages/homepage/wallets/invite/cip146/146ExportMultisig.tsx @@ -0,0 +1,143 @@ +// src/components/pages/homepage/wallets/invite/cip146/ExportMultisig.tsx +import React from "react"; +import { Button } from "@/components/ui/button"; +import { Bip32PrivateKey } from "@emurgo/cardano-serialization-lib-browser"; +import { deriveAccountKeys } from "./146GenAcct"; + +interface ExportMultisigProps { + rootKeyHex: string; + exportType: "public" | "private"; + index: number; +} + +const ExportMultisig: React.FC = ({ + rootKeyHex, + exportType, + index, +}) => { + const handleExport = () => { + if (!rootKeyHex) { + alert( + "No root key available. Please generate or import a root key first.", + ); + return; + } + // Use the provided index and roles [0,2,3] + const derived = deriveAccountKeys(rootKeyHex, index, [0, 2, 3]); + let acctKey: string, paymentKey: string, stakeKey: string, drepKey: string; + if (exportType === "private") { + // Use extended secret keys. + acctKey = derived.accountKey; + paymentKey = derived.derivedKeys[0].xsk; + stakeKey = derived.derivedKeys[2].xsk; + drepKey = derived.derivedKeys[3].xsk; + } else { + // Use extended verification keys. + const acctPublic = Bip32PrivateKey.from_hex(derived.accountKey) + .to_public() + .to_hex(); + acctKey = acctPublic; + paymentKey = derived.derivedKeys[0].xvk; + stakeKey = derived.derivedKeys[2].xvk; + drepKey = derived.derivedKeys[3].xvk; + } + // Use the first 20 characters of acctKey as the wallet/account id. + const walletId = acctKey.slice(0, 20); + // Construct the wallet data object with only the fields as modified. + let walletData; + if (exportType === "private") { + walletData = { + wallet: { + id: walletId, + networkId: "preprod", + signType: "mnemonic", + multiSig: [ + { + priv: acctKey, + path: [1854, 1815, index], + keys: { + payment: [ + { + path: [1854, 1815, index, 0, 0], + cred: paymentKey, + used: false, + }, + ], + stake: [ + { + path: [1854, 1815, index, 2, 0], + cred: stakeKey, + used: false, + }, + ], + drep: [ + { + priv: drepKey, + path: [1854, 1815, index, 3, 0], + }, + ], + }, + }, + ], + }, + }; + } else { + walletData = { + wallet: { + id: walletId, + networkId: "preprod", + signType: "mnemonic", + multiSig: [ + { + pub: acctKey, + path: [1854, 1815, index], + keys: { + payment: [ + { + path: [1854, 1815, index, 0, 0], + cred: paymentKey, + used: false, + }, + ], + stake: [ + { + path: [1854, 1815, index, 2, 0], + cred: stakeKey, + used: false, + }, + ], + drep: [ + { + pub: drepKey, + path: [1854, 1815, index, 3, 0], + }, + ], + }, + }, + ], + }, + }; + } + const dataStr = JSON.stringify(walletData, null, 2); + const blob = new Blob([dataStr], { type: "application/json" }); + const url = URL.createObjectURL(blob); + const link = document.createElement("a"); + link.href = url; + link.download = + exportType === "private" + ? "wallet_data_private.json" + : "wallet_data_public.json"; + link.click(); + URL.revokeObjectURL(url); + }; + + return ( + + ); +}; + +export default ExportMultisig; diff --git a/src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx b/src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx new file mode 100644 index 00000000..8140cd6c --- /dev/null +++ b/src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx @@ -0,0 +1,139 @@ +// src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx +import React, { useState, useEffect } from "react"; +import { Bip32PrivateKey, Bip32PublicKey } from "@emurgo/cardano-serialization-lib-browser"; + +export interface DerivedAccountKeys { + accountKey: string; + derivedKeys: { + [role: number]: { + xsk: string; + xvk: string; + }; + }; +} + +/** + * Derives an account key from a root key using the CIP‑1854 path: m/1854'/1815'/index' + * and then derives role keys for each role in roleIds. + */ +export function deriveAccountKeys( + rootKeyHex: string, + index: number, + roleIds: number[] +): DerivedAccountKeys { + const rootKey = Bip32PrivateKey.from_hex(rootKeyHex); + const accountKey = rootKey + .derive(1854 | 0x80000000) + .derive(1815 | 0x80000000) + .derive(index | 0x80000000); + const derivedKeys: { [role: number]: { xsk: string; xvk: string } } = {}; + roleIds.forEach((role) => { + const roleKey = accountKey.derive(role).derive(0); + derivedKeys[role] = { + xsk: roleKey.to_hex(), + xvk: roleKey.to_public().to_hex(), + }; + }); + return { + accountKey: accountKey.to_hex(), + derivedKeys, + }; +} + +/** + * Derives role keys directly from an already derived account key (private version). + */ +export function deriveRoleKeysFromAccount( + accountKeyHex: string, + roleIds: number[] +): { [role: number]: { xsk: string; xvk: string } } { + const accountKey = Bip32PrivateKey.from_hex(accountKeyHex); + const derivedKeys: { [role: number]: { xsk: string; xvk: string } } = {}; + roleIds.forEach((role) => { + const roleKey = accountKey.derive(role).derive(0); + derivedKeys[role] = { + xsk: roleKey.to_hex(), + xvk: roleKey.to_public().to_hex(), + }; + }); + return derivedKeys; +} + +interface GenAcctProps { + /** Either provide a rootKeyHex (with an account index) OR an already derived accountKeyHex */ + rootKeyHex?: string; + accountKeyHex?: string; + index?: number; + roleIds: number[]; +} + +const GenAcct: React.FC = ({ + rootKeyHex, + accountKeyHex, + index, + roleIds, +}) => { + const [derived, setDerived] = useState(null); + const [mounted, setMounted] = useState(false); + + useEffect(() => { + // Ensure derivation happens only on the client to avoid hydration issues. + setMounted(true); + if (accountKeyHex) { + try { + // Try to treat the account key as a private key. + Bip32PrivateKey.from_hex(accountKeyHex); + setDerived({ + accountKey: accountKeyHex, + derivedKeys: deriveRoleKeysFromAccount(accountKeyHex, roleIds), + }); + } catch (err) { + // If that fails, assume it's a public key. + const pubKey = Bip32PublicKey.from_hex(accountKeyHex); + const derivedKeys: { [role: number]: { xsk: string; xvk: string } } = {}; + roleIds.forEach((role) => { + // Public derivation: derive child public keys. + // Note: Only public keys (xvk) can be derived from a public key. + const rolePub = pubKey.derive(role).derive(0); + derivedKeys[role] = { xsk: "", xvk: rolePub.to_hex() }; + }); + setDerived({ + accountKey: accountKeyHex, + derivedKeys, + }); + } + } else if (rootKeyHex && index !== undefined) { + setDerived(deriveAccountKeys(rootKeyHex, index, roleIds)); + } else { + setDerived(null); + } + }, [rootKeyHex, accountKeyHex, index, roleIds]); + + if (!mounted) return null; + if (!derived) return
No account key available.
; + + return ( +
+

Account Key (acct_shared_xsk):

+
{derived.accountKey}
+

Derived Role Keys:

+
    + {roleIds.map((role) => ( +
  • + Role {role}: +
    + XSK: +
    {derived.derivedKeys[role].xsk}
    +
    +
    + XVK: +
    {derived.derivedKeys[role].xvk}
    +
    +
  • + ))} +
+
+ ); +}; + +export default GenAcct; \ No newline at end of file diff --git a/src/components/pages/homepage/wallets/invite/cip146/146Import.tsx b/src/components/pages/homepage/wallets/invite/cip146/146Import.tsx new file mode 100644 index 00000000..444a0e2e --- /dev/null +++ b/src/components/pages/homepage/wallets/invite/cip146/146Import.tsx @@ -0,0 +1,96 @@ +// src/components/pages/homepage/wallets/invite/cip146/146Import.tsx +import React, { useRef, useState } from "react"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; + +interface ImportProps { + onImport: (accountKeyHex: string) => void; +} + +const ImportComponent: React.FC = ({ onImport }) => { + const fileInputRef = useRef(null); + const [directInput, setDirectInput] = useState(""); + + const handleFileChange = (e: React.ChangeEvent) => { + const file = e.target.files?.[0]; + if (!file) return; + const fileReader = new FileReader(); + fileReader.onload = (event) => { + try { + const importedData = JSON.parse(event.target?.result as string); + // Check for our simple export format: directly exported account key. + if (importedData.acct_shared_xsk) { + onImport(importedData.acct_shared_xsk); + return; + } + // Check for our multisig export format. + if ( + importedData.wallet && + importedData.wallet.multiSig && + Array.isArray(importedData.wallet.multiSig) && + importedData.wallet.multiSig.length > 0 + ) { + const ms = importedData.wallet.multiSig[0]; + if (ms.priv) { + onImport(ms.priv); + return; + } else if (ms.pub) { + onImport(ms.pub); + return; + } + } + alert("Invalid file. Account key not found."); + } catch (error) { + console.error(error); + alert("Failed to import account key."); + } + }; + fileReader.readAsText(file); + }; + + const handleFileImport = () => { + fileInputRef.current?.click(); + }; + + const handleDirectImport = () => { + if (!directInput.trim()) { + alert("Please enter an account key."); + return; + } + // Simply pass the direct input to the callback. + onImport(directInput.trim()); + }; + + return ( +
+
+ + +
+
+ + setDirectInput(e.target.value)} + /> + +
+
+ ); +}; + +export default ImportComponent; \ No newline at end of file diff --git a/src/components/pages/homepage/wallets/invite/cip146/146Wallet.tsx b/src/components/pages/homepage/wallets/invite/cip146/146Wallet.tsx new file mode 100644 index 00000000..b57d5a1a --- /dev/null +++ b/src/components/pages/homepage/wallets/invite/cip146/146Wallet.tsx @@ -0,0 +1,133 @@ +// src/components/pages/homepage/wallets/invite/cip146/146Wallet.tsx +import React, { useState } from "react"; +import { Bip32PrivateKey } from "@emurgo/cardano-serialization-lib-browser"; +import bip39 from "bip39"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import ImportComponent from "./146Import"; +import ExportMultisig from "./146ExportMultisig"; +import GenAcct from "./146GenAcct"; + +const hexToUint8Array = (hex: string): Uint8Array => { + if (hex.length % 2 !== 0) throw new Error("Invalid hex string"); + const arr = new Uint8Array(hex.length / 2); + for (let i = 0; i < hex.length; i += 2) { + arr[i / 2] = parseInt(hex.substr(i, 2), 16); + } + return arr; +}; + +const KeyGenerator = () => { + const [rootKeyHex, setRootKeyHex] = useState(""); + const [accountIndex, setAccountIndex] = useState(0); + const [mnemonic, setMnemonic] = useState(""); + const [accountKeyHex, setAccountKeyHex] = useState(""); + + const generateRootKey = () => { + const entropy = new Uint8Array(32); + window.crypto.getRandomValues(entropy); + const rootKey = Bip32PrivateKey.from_bip39_entropy(entropy, new Uint8Array()); + setRootKeyHex(rootKey.to_hex()); + }; + + const importFromMnemonic = () => { + if (!mnemonic.trim()) { + alert("Please enter a mnemonic phrase."); + return; + } + try { + const entropyHex = bip39.mnemonicToEntropy(mnemonic.trim()); + const entropy = hexToUint8Array(entropyHex); + const rootKey = Bip32PrivateKey.from_bip39_entropy(entropy, new Uint8Array()); + setRootKeyHex(rootKey.to_hex()); + } catch (error) { + console.error(error); + alert("Failed to import from mnemonic. Please ensure the mnemonic is valid."); + } + }; + + return ( + + + Wallet Derivation + + +
+ {/* Root Key Generation and Mnemonic Import */} +
+ +
+ + setMnemonic(e.target.value)} + /> + +
+ {rootKeyHex && ( +

+ Root Key: {rootKeyHex} +

+ )} +
+ {/* Account Index Input */} +
+ + setAccountIndex(Number(e.target.value))} + className="w-20" + /> +
+ {/* Display Derived Account and Role Keys using GenAcct */} + {rootKeyHex && !accountKeyHex && ( + + )} + {accountKeyHex && ( + + )} + {/* Import for Account Key */} +
+ { + // Update account key state instead of root key. + setAccountKeyHex(importedAccountKeyHex); + }} + /> +
+ {/* Export Multisig Wallet Data */} +
+ {rootKeyHex && ( + <> + + + + )} +
+
+
+
+ ); +}; + +export default KeyGenerator; \ No newline at end of file diff --git a/src/components/pages/homepage/wallets/invite/cip146Wallet.tsx b/src/components/pages/homepage/wallets/invite/cip146Wallet.tsx deleted file mode 100644 index a0b0ea11..00000000 --- a/src/components/pages/homepage/wallets/invite/cip146Wallet.tsx +++ /dev/null @@ -1,137 +0,0 @@ -import React, { useState, useRef } from 'react'; -import { Bip32PrivateKey } from '@emurgo/cardano-serialization-lib-browser'; -import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; -import { Button } from '@/components/ui/button'; - -const KeyGenerator = () => { - const [keys, setKeys] = useState({ - rootPrivateKey: '', - rootPublicKey: '', - utxoPubKey: '', - stakeKey: '', - drepKey: '' - }); - const fileInputRef = useRef(null); - - // Helper to derive all keys from the root key. - const deriveKeys = (rootKey) => { - const rootPublicKey = rootKey.to_public(); - const accountKey = rootKey - .derive(1854 | 0x80000000) - .derive(1815 | 0x80000000) - .derive(0 | 0x80000000); - const utxoPubKey = accountKey.derive(0).derive(0).to_public(); - const stakeKey = accountKey.derive(2).derive(0).to_public(); - const drepKey = accountKey.derive(3).derive(0).to_public(); - return { - rootPrivateKey: rootKey.to_hex(), - rootPublicKey: rootPublicKey.to_hex(), - utxoPubKey: utxoPubKey.to_hex(), - stakeKey: stakeKey.to_hex(), - drepKey: drepKey.to_hex() - }; - }; - - // Generate a new root key and derive its keys. - const generateKeys = () => { - const entropy = new Uint8Array(32); - window.crypto.getRandomValues(entropy); - const rootKey = Bip32PrivateKey.from_bip39_entropy(entropy, new Uint8Array()); - const newKeys = deriveKeys(rootKey); - setKeys(newKeys); - }; - - // Export only the root private key as JSON. - const exportKeys = () => { - if (!keys.rootPrivateKey) { - alert('No keys to export. Please generate keys first.'); - return; - } - const dataStr = JSON.stringify({ rootPrivateKey: keys.rootPrivateKey }, null, 2); - const blob = new Blob([dataStr], { type: 'application/json' }); - const url = URL.createObjectURL(blob); - const link = document.createElement('a'); - link.href = url; - link.download = 'keys.json'; - link.click(); - URL.revokeObjectURL(url); - }; - - // Import the root key, then re-derive all keys. - const importKeys = (e) => { - const fileReader = new FileReader(); - fileReader.onload = (event) => { - try { - const importedData = JSON.parse(event.target.result); - if (importedData.rootPrivateKey) { - const rootKey = Bip32PrivateKey.from_hex(importedData.rootPrivateKey); - const newKeys = deriveKeys(rootKey); - setKeys(newKeys); - } else { - alert('Invalid key file. Root key not found.'); - } - } catch (error) { - console.error(error); - alert('Failed to import keys.'); - } - }; - if (e.target.files && e.target.files.length > 0) { - fileReader.readAsText(e.target.files[0]); - } - }; - - return ( - - - Generate New Keys - - -
- -
- - - -
-
- {keys.rootPrivateKey && ( -
-

Derived Keys:

-

- Root Private Key: {keys.rootPrivateKey} -

-

- Root Public Key: {keys.rootPublicKey} -

-

- UTXO Public Key: {keys.utxoPubKey} -

-

- Stake Key: {keys.stakeKey} -

-

- Drep Key: {keys.drepKey} -

-
- )} -
-
- ); -}; - -export default KeyGenerator; \ No newline at end of file diff --git a/src/components/pages/homepage/wallets/invite/index.tsx b/src/components/pages/homepage/wallets/invite/index.tsx index 158928e8..bae10b2c 100644 --- a/src/components/pages/homepage/wallets/invite/index.tsx +++ b/src/components/pages/homepage/wallets/invite/index.tsx @@ -14,7 +14,7 @@ import { api } from "@/utils/api"; import { useUserStore } from "@/lib/zustand/user"; import { useRouter } from "next/router"; import { useToast } from "@/hooks/use-toast"; -import KeyGenerator from "./cip146Wallet"; +import KeyGenerator from "./cip146/146Wallet"; export default function PageNewWalletInvite() { const router = useRouter(); From eb0d4e53f6f2f500963ae37a1f4863f660364868 Mon Sep 17 00:00:00 2001 From: QS <34195172+QSchlegel@users.noreply.github.com> Date: Mon, 24 Feb 2025 15:08:20 +0100 Subject: [PATCH 03/28] Add files via upload --- LICENSE.md | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000..fc05a975 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2023 MeshJS + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. From c2b880a00663f63f35cfa21a7c6c3e4572307cb3 Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Mon, 3 Mar 2025 21:18:22 +0800 Subject: [PATCH 04/28] fix: address tx pagination --- .../overall-layout/wallet-data-loader.tsx | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/components/common/overall-layout/wallet-data-loader.tsx b/src/components/common/overall-layout/wallet-data-loader.tsx index 5560e0b7..216bac89 100644 --- a/src/components/common/overall-layout/wallet-data-loader.tsx +++ b/src/components/common/overall-layout/wallet-data-loader.tsx @@ -34,20 +34,31 @@ export default function WalletDataLoader() { async function getTransactionsOnChain() { if (appWallet) { + const maxPage = 100; const _transactions: OnChainTransaction[] = []; const blockchainProvider = getProvider(network); - let transactions: TxInfo[] = await blockchainProvider.get( - `/addresses/${appWallet.address}/transactions`, - ); - transactions = transactions.reverse().splice(0, NUMBER_OF_TRANSACTIONS); - for (const tx of transactions) { - const txData = await blockchainProvider.get(`/txs/${tx.tx_hash}/utxos`); - _transactions.push({ - hash: tx.tx_hash, - tx: tx, - inputs: txData.inputs, - outputs: txData.outputs, - }); + + for (let i = 1; i <= maxPage; i++) { + let transactions: TxInfo[] = await blockchainProvider.get( + `/addresses/${appWallet.address}/transactions?page=${i}&order=desc`, + ); + + if (transactions.length === 0) { + break; + } + + transactions = transactions.splice(0, NUMBER_OF_TRANSACTIONS); + for (const tx of transactions) { + const txData = await blockchainProvider.get( + `/txs/${tx.tx_hash}/utxos`, + ); + _transactions.push({ + hash: tx.tx_hash, + tx: tx, + inputs: txData.inputs, + outputs: txData.outputs, + }); + } } setWalletTransactions(appWallet?.id, _transactions); From 1389cd344e7db83a71250b93384283d32167dd60 Mon Sep 17 00:00:00 2001 From: Jingles Date: Thu, 6 Mar 2025 09:00:48 +0800 Subject: [PATCH 05/28] remove koios from env --- src/components/common/cardano-objects/get-provider.ts | 4 ---- src/env.js | 2 -- 2 files changed, 6 deletions(-) diff --git a/src/components/common/cardano-objects/get-provider.ts b/src/components/common/cardano-objects/get-provider.ts index dd6c8e12..4dcf558d 100644 --- a/src/components/common/cardano-objects/get-provider.ts +++ b/src/components/common/cardano-objects/get-provider.ts @@ -7,8 +7,4 @@ export function getProvider(network: number) { ? env.NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD : env.NEXT_PUBLIC_BLOCKFROST_API_KEY_MAINNET, ); - // return new KoiosProvider( - // network == 0 ? 'preprod' : 'api', - // env.NEXT_PUBLIC_KOIOS_TOKEN, - // ); } diff --git a/src/env.js b/src/env.js index d6f16a55..77962101 100644 --- a/src/env.js +++ b/src/env.js @@ -36,7 +36,6 @@ export const env = createEnv({ client: { NEXT_PUBLIC_BLOCKFROST_API_KEY_MAINNET: z.string(), NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD: z.string(), - NEXT_PUBLIC_KOIOS_TOKEN: z.string(), }, /** @@ -55,7 +54,6 @@ export const env = createEnv({ NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD: process.env.NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD, BLOB_READ_WRITE_TOKEN: process.env.BLOB_READ_WRITE_TOKEN, - NEXT_PUBLIC_KOIOS_TOKEN: process.env.NEXT_PUBLIC_KOIOS_TOKEN, GITHUB_TOKEN: process.env.GITHUB_TOKEN, }, /** From ab7d5136706c0a3d0104eca58e5bfd88aca6630b Mon Sep 17 00:00:00 2001 From: QSchlegel Date: Mon, 10 Mar 2025 11:06:39 +0100 Subject: [PATCH 06/28] Improved vote button. & Bug fix in proposal list. --- .../governance/proposal/voteButtton.tsx | 52 +++++++++---------- .../pages/wallet/governance/proposals.tsx | 2 +- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/components/pages/wallet/governance/proposal/voteButtton.tsx b/src/components/pages/wallet/governance/proposal/voteButtton.tsx index eccf0337..ee364735 100644 --- a/src/components/pages/wallet/governance/proposal/voteButtton.tsx +++ b/src/components/pages/wallet/governance/proposal/voteButtton.tsx @@ -101,7 +101,7 @@ export default function VoteButton({ await newTransaction({ txBuilder, - description: description || undefined, + description: `Vote: ${voteKind} - ${description}`, metadataValue: metadata ? { label: "674", value: metadata } : undefined, }); @@ -149,32 +149,30 @@ export default function VoteButton({ } return ( -
- {/* Unified Button + Selector Design */} -
- {/* Vote Button (Left Side) */} - +
+ - {/* Vote Kind Dropdown (Right Side) */} - -
-
+ +
); } \ No newline at end of file diff --git a/src/components/pages/wallet/governance/proposals.tsx b/src/components/pages/wallet/governance/proposals.tsx index 1bc20690..5b7895b1 100644 --- a/src/components/pages/wallet/governance/proposals.tsx +++ b/src/components/pages/wallet/governance/proposals.tsx @@ -127,7 +127,7 @@ function ProposalRow({ {proposal.governance_type.split("_").join(" ").toUpperCase()} - + ); From 1aa4a68e32033f4efbea9a8fb28e6186917d143c Mon Sep 17 00:00:00 2001 From: QSchlegel Date: Mon, 10 Mar 2025 11:29:35 +0100 Subject: [PATCH 07/28] fix spam protction --- src/pages/api/vercel-storage/image/put.ts | 30 +++++++++++++++++------ 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/pages/api/vercel-storage/image/put.ts b/src/pages/api/vercel-storage/image/put.ts index e1b53825..c3d54ab0 100644 --- a/src/pages/api/vercel-storage/image/put.ts +++ b/src/pages/api/vercel-storage/image/put.ts @@ -10,7 +10,10 @@ export const config = { }, }; -export default async function handler(req: NextApiRequest, res: NextApiResponse) { +export default async function handler( + req: NextApiRequest, + res: NextApiResponse, +) { if (req.method !== "POST") { return res.status(405).json({ error: "Method not allowed" }); } @@ -21,7 +24,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) new Promise((resolve, reject) => { form.parse(req, (err, fields, files) => { if (err) { - return reject(new Error(err instanceof Error ? err.message : "Form parsing error")); + return reject( + new Error( + err instanceof Error ? err.message : "Form parsing error", + ), + ); } resolve({ fields, files }); }); @@ -37,13 +44,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const fileStream = fs.createReadStream(file.filepath); // Validate and retrieve form fields - const rawShortHash = fields.shortHash; + const rawShortHash = Array.isArray(fields.shortHash) + ? fields.shortHash[0] + : fields.shortHash; + if (!rawShortHash || typeof rawShortHash !== "string") { return res.status(400).json({ error: "shortHash is required" }); } const shortHash = rawShortHash; - const rawFilename = fields.filename; + const rawFilename = Array.isArray(fields.filename) + ? fields.filename[0] + : fields.filename; if (!rawFilename || typeof rawFilename !== "string") { return res.status(400).json({ error: "filename is required" }); } @@ -53,7 +65,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const storagePath = `img/${shortHash}/${filename}`; const contentType = - typeof file.mimetype === "string" ? file.mimetype : "application/octet-stream"; + typeof file.mimetype === "string" + ? file.mimetype + : "application/octet-stream"; const response = await put(storagePath, fileStream, { access: "public", @@ -64,6 +78,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) return res.status(200).json({ url: response.url }); } catch (err) { console.error("File upload error:", err); - return res.status(500).json({ error: "Internal Server Error", details: err }); + return res + .status(500) + .json({ error: "Internal Server Error", details: err }); } -} \ No newline at end of file +} From bccea14aaf31b3c75557e8267e097bd08186dd01 Mon Sep 17 00:00:00 2001 From: QSchlegel Date: Wed, 12 Mar 2025 16:27:58 +0100 Subject: [PATCH 08/28] acct_shared_xvk import --- .../{146ExportMultisig.tsx => 146Export.tsx} | 12 +- .../wallets/invite/cip146/146GenAcct.tsx | 116 ++++++++++++++---- .../wallets/invite/cip146/146Import.tsx | 57 +++++++-- .../wallets/invite/cip146/146Wallet.tsx | 2 +- .../wallets/invite/cip146/146lookup.ts | 56 +++++++++ 5 files changed, 203 insertions(+), 40 deletions(-) rename src/components/pages/homepage/wallets/invite/cip146/{146ExportMultisig.tsx => 146Export.tsx} (93%) create mode 100644 src/components/pages/homepage/wallets/invite/cip146/146lookup.ts diff --git a/src/components/pages/homepage/wallets/invite/cip146/146ExportMultisig.tsx b/src/components/pages/homepage/wallets/invite/cip146/146Export.tsx similarity index 93% rename from src/components/pages/homepage/wallets/invite/cip146/146ExportMultisig.tsx rename to src/components/pages/homepage/wallets/invite/cip146/146Export.tsx index 6ca6e8f8..7df1a5b2 100644 --- a/src/components/pages/homepage/wallets/invite/cip146/146ExportMultisig.tsx +++ b/src/components/pages/homepage/wallets/invite/cip146/146Export.tsx @@ -28,18 +28,18 @@ const ExportMultisig: React.FC = ({ if (exportType === "private") { // Use extended secret keys. acctKey = derived.accountKey; - paymentKey = derived.derivedKeys[0].xsk; - stakeKey = derived.derivedKeys[2].xsk; - drepKey = derived.derivedKeys[3].xsk; + paymentKey = derived.derivedKeys[0]!.xsk; + stakeKey = derived.derivedKeys[2]!.xsk; + drepKey = derived.derivedKeys[3]!.xsk; } else { // Use extended verification keys. const acctPublic = Bip32PrivateKey.from_hex(derived.accountKey) .to_public() .to_hex(); acctKey = acctPublic; - paymentKey = derived.derivedKeys[0].xvk; - stakeKey = derived.derivedKeys[2].xvk; - drepKey = derived.derivedKeys[3].xvk; + paymentKey = derived.derivedKeys[0]!.xvk; + stakeKey = derived.derivedKeys[2]!.xvk; + drepKey = derived.derivedKeys[3]!.xvk; } // Use the first 20 characters of acctKey as the wallet/account id. const walletId = acctKey.slice(0, 20); diff --git a/src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx b/src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx index 8140cd6c..e3dc801d 100644 --- a/src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx +++ b/src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx @@ -1,6 +1,12 @@ // src/components/pages/homepage/wallets/invite/cip146/146GenAcct.tsx import React, { useState, useEffect } from "react"; -import { Bip32PrivateKey, Bip32PublicKey } from "@emurgo/cardano-serialization-lib-browser"; +import { + Bip32PrivateKey, + Bip32PublicKey, +} from "@emurgo/cardano-serialization-lib-browser"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import { useSiteStore } from "@/lib/zustand/site"; +import { lookupWallet } from "./146lookup"; export interface DerivedAccountKeys { accountKey: string; @@ -19,7 +25,7 @@ export interface DerivedAccountKeys { export function deriveAccountKeys( rootKeyHex: string, index: number, - roleIds: number[] + roleIds: number[], ): DerivedAccountKeys { const rootKey = Bip32PrivateKey.from_hex(rootKeyHex); const accountKey = rootKey @@ -45,7 +51,7 @@ export function deriveAccountKeys( */ export function deriveRoleKeysFromAccount( accountKeyHex: string, - roleIds: number[] + roleIds: number[], ): { [role: number]: { xsk: string; xvk: string } } { const accountKey = Bip32PrivateKey.from_hex(accountKeyHex); const derivedKeys: { [role: number]: { xsk: string; xvk: string } } = {}; @@ -75,6 +81,16 @@ const GenAcct: React.FC = ({ }) => { const [derived, setDerived] = useState(null); const [mounted, setMounted] = useState(false); + const [usedKeys, setUsedKeys] = useState<{ [role: number]: boolean }>({}); + const network = useSiteStore((state) => state.network); + + + + const shortenKey = (key: string): string => { + return key.length > 20 + ? key.substring(0, 10) + " ... " + key.substring(key.length - 10) + : key; + }; useEffect(() => { // Ensure derivation happens only on the client to avoid hydration issues. @@ -90,7 +106,8 @@ const GenAcct: React.FC = ({ } catch (err) { // If that fails, assume it's a public key. const pubKey = Bip32PublicKey.from_hex(accountKeyHex); - const derivedKeys: { [role: number]: { xsk: string; xvk: string } } = {}; + const derivedKeys: { [role: number]: { xsk: string; xvk: string } } = + {}; roleIds.forEach((role) => { // Public derivation: derive child public keys. // Note: Only public keys (xvk) can be derived from a public key. @@ -109,31 +126,80 @@ const GenAcct: React.FC = ({ } }, [rootKeyHex, accountKeyHex, index, roleIds]); + const getPubKeyHash = (xvk: string): string => { + try { + const bip32Pub = Bip32PublicKey.from_hex(xvk); + return bip32Pub.to_raw_key().hash().to_hex(); + } catch (error) { + console.error("Error generating pub key hash", error); + return ""; + } + }; + + useEffect(() => { + async function fetchUsedKeys() { + if (derived) { + // Extract pub key hashes for each role + const pubKeyHashes = roleIds.map((role) => + getPubKeyHash(derived.derivedKeys[role]!.xvk).toLowerCase() + ); + // Lookup metadata using network id 0 (preprod) + const lookupResult = await lookupWallet(network, pubKeyHashes); + const used: { [role: number]: boolean } = {}; + // For each role, mark as used if its pubkey hash is found in any lookupResult participants + roleIds.forEach((role) => { + const keyHash = getPubKeyHash(derived.derivedKeys[role]!.xvk).toLowerCase(); + used[role] = lookupResult.some((item) => { + const participants = item.json_metadata.participants; + return Object.keys(participants).some((hash) => hash.toLowerCase() === keyHash); + }); + }); + setUsedKeys(used); + } + } + fetchUsedKeys(); + }, [derived, roleIds, network]); + if (!mounted) return null; if (!derived) return
No account key available.
; return ( -
-

Account Key (acct_shared_xsk):

-
{derived.accountKey}
-

Derived Role Keys:

-
    - {roleIds.map((role) => ( -
  • - Role {role}: -
    - XSK: -
    {derived.derivedKeys[role].xsk}
    -
    -
    - XVK: -
    {derived.derivedKeys[role].xvk}
    -
    -
  • - ))} -
-
+ + + Account and Role Keys + + +
+

Account Key (acct_shared_xsk):

+
{shortenKey(derived.accountKey)}
+

Derived Role Keys:

+
    + {roleIds.map((role) => ( +
  • + Role {role}: +
    + XSK: +
    {shortenKey(derived.derivedKeys[role]!.xsk)}
    +
    +
    + XVK: +
    {shortenKey(derived.derivedKeys[role]!.xvk)}
    +
    +
    + XVK Hash: +
    {shortenKey(getPubKeyHash(derived.derivedKeys[role]!.xvk))}
    +
    +
    + Status: +
    {usedKeys[role] ? "Used" : "Unused"}
    +
    +
  • + ))} +
+
+
+
); }; -export default GenAcct; \ No newline at end of file +export default GenAcct; diff --git a/src/components/pages/homepage/wallets/invite/cip146/146Import.tsx b/src/components/pages/homepage/wallets/invite/cip146/146Import.tsx index 444a0e2e..31ee39fd 100644 --- a/src/components/pages/homepage/wallets/invite/cip146/146Import.tsx +++ b/src/components/pages/homepage/wallets/invite/cip146/146Import.tsx @@ -3,6 +3,8 @@ import React, { useRef, useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; +import { Bip32PublicKey, Address } from "@emurgo/cardano-serialization-lib-browser"; +import { bech32 } from "bech32"; // used only for debugging the payload interface ImportProps { onImport: (accountKeyHex: string) => void; @@ -11,6 +13,7 @@ interface ImportProps { const ImportComponent: React.FC = ({ onImport }) => { const fileInputRef = useRef(null); const [directInput, setDirectInput] = useState(""); + const [errorMessage, setErrorMessage] = useState(""); const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; @@ -40,10 +43,10 @@ const ImportComponent: React.FC = ({ onImport }) => { return; } } - alert("Invalid file. Account key not found."); + setErrorMessage("Invalid file. Account key not found."); } catch (error) { console.error(error); - alert("Failed to import account key."); + setErrorMessage("Failed to import account key."); } }; fileReader.readAsText(file); @@ -55,13 +58,45 @@ const ImportComponent: React.FC = ({ onImport }) => { const handleDirectImport = () => { if (!directInput.trim()) { - alert("Please enter an account key."); + setErrorMessage("Please enter an account key."); return; } + setErrorMessage(""); // Simply pass the direct input to the callback. onImport(directInput.trim()); }; + const importFromBech32 = () => { + if (!directInput.trim()) { + setErrorMessage("Please enter a Bech32 address."); + return; + } + + const input = directInput.trim(); + try { + // Only support extended account keys with prefix 'acct_shared_xvk' + if (!input.startsWith("acct_shared_xvk")) { + throw new Error("Unsupported Bech32 format. Expected extended account key with prefix acct_shared_xvk."); + } + + // Manually decode the Bech32 string using the bech32 library + const decoded = bech32.decode(input, 200); + const dataBytes = new Uint8Array(bech32.fromWords(decoded.words)); + const bytesToHex = (bytes: Uint8Array): string => + bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), ""); + const pubKeyHex = bytesToHex(dataBytes); + + // Validate the decoded key by parsing it with from_hex + const bip32Pub = Bip32PublicKey.from_hex(pubKeyHex); + + onImport(pubKeyHex); + setErrorMessage(""); + } catch (error) { + console.error("Bech32 decoding error:", error); + setErrorMessage("Invalid Bech32 address: " + error); + } + }; + return (
@@ -77,17 +112,23 @@ const ImportComponent: React.FC = ({ onImport }) => { />
- + setDirectInput(e.target.value)} /> - + {errorMessage &&

{errorMessage}

} +
+ + +
); diff --git a/src/components/pages/homepage/wallets/invite/cip146/146Wallet.tsx b/src/components/pages/homepage/wallets/invite/cip146/146Wallet.tsx index b57d5a1a..5c657a03 100644 --- a/src/components/pages/homepage/wallets/invite/cip146/146Wallet.tsx +++ b/src/components/pages/homepage/wallets/invite/cip146/146Wallet.tsx @@ -7,7 +7,7 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import ImportComponent from "./146Import"; -import ExportMultisig from "./146ExportMultisig"; +import ExportMultisig from "./146Export"; import GenAcct from "./146GenAcct"; const hexToUint8Array = (hex: string): Uint8Array => { diff --git a/src/components/pages/homepage/wallets/invite/cip146/146lookup.ts b/src/components/pages/homepage/wallets/invite/cip146/146lookup.ts new file mode 100644 index 00000000..284de5dc --- /dev/null +++ b/src/components/pages/homepage/wallets/invite/cip146/146lookup.ts @@ -0,0 +1,56 @@ +import { getProvider } from "@/components/common/cardano-objects/get-provider"; + +export interface MetadataItem { + tx_hash: string; + json_metadata: { + name: string; + types: number[]; + participants: { + [pubKeyHash: string]: { + name: string; + }; + }; + }; +} + +/** + * Looks up wallet metadata using the given network id and an array of public key hashes. + * It fetches metadata labeled with 1854 and returns only the items that have valid participants + * and at least one participant matching one of the provided pubKeyHashes. + */ +export async function lookupWallet( + network: number, + pubKeyHashes: string[] +): Promise { + const provider = getProvider(network); + try { + console.log("lookupWallet: Looking up metadata for pubKeyHashes:", pubKeyHashes); + const response = await provider.get('/metadata/txs/labels/1854'); + console.log("lookupWallet: Raw response:", response); + + if (!Array.isArray(response)) { + throw new Error("Invalid response format from provider"); + } + + // Filter valid items: only consider items that have non-empty participants in json_metadata + const validItems = response.filter((item: any) => { + const participants = item.json_metadata?.participants; + return participants && Object.keys(participants).length > 0; + }); + console.log("lookupWallet: Valid items:", validItems); + + // Match items if any participant's key matches one of the provided pubKeyHashes + const matchedItems = validItems.filter((item: any) => { + const participants = item.json_metadata.participants; + return Object.keys(participants).some((hash: string) => pubKeyHashes.includes(hash.toLowerCase())); + }); + console.log("lookupWallet: Matched items:", matchedItems); + + return matchedItems; + } catch (error) { + console.error("lookupWallet error:", error); + return []; + } +} + + From 8659320c00550e1ec4a4ec21bd03c428f67365b3 Mon Sep 17 00:00:00 2001 From: QSchlegel Date: Fri, 14 Mar 2025 17:18:45 +0100 Subject: [PATCH 09/28] ux improve --- .../common/overall-layout/layout.tsx | 17 ++- src/components/pages/homepage/index.tsx | 18 ++- .../wallets/invite/cip146/146GenAcct.tsx | 142 ++++++++++++------ .../wallets/invite/cip146/146Import.tsx | 115 +++++++------- .../wallets/invite/cip146/146Wallet.tsx | 111 ++++++++------ src/lib/helper/getPubKeyHash.ts | 11 ++ 6 files changed, 255 insertions(+), 159 deletions(-) create mode 100644 src/lib/helper/getPubKeyHash.ts diff --git a/src/components/common/overall-layout/layout.tsx b/src/components/common/overall-layout/layout.tsx index 09cc38f9..458bf987 100644 --- a/src/components/common/overall-layout/layout.tsx +++ b/src/components/common/overall-layout/layout.tsx @@ -1,4 +1,4 @@ -import React, { useEffect } from "react"; +import React, { useEffect, useState } from "react"; import Link from "next/link"; import { api } from "@/utils/api"; import ConnectWallet from "../cardano-objects/connect-wallet"; @@ -25,6 +25,7 @@ import { publicRoutes } from "@/data/public-routes"; import Loading from "./loading"; import DialogReport from "./dialog-report"; import { useRouter } from "next/router"; +import { Menu as MenuIcon } from "lucide-react"; export default function RootLayout({ children, @@ -36,6 +37,7 @@ export default function RootLayout({ const router = useRouter(); const { appWallet } = useAppWallet(); const { generateNsec } = useNostrChat(); + const [mobileMenuOpen, setMobileMenuOpen] = useState(false); const userAddress = useUserStore((state) => state.userAddress); const setUserAddress = useUserStore((state) => state.setUserAddress); @@ -90,6 +92,16 @@ export default function RootLayout({ return (
{isLoading && } + {mobileMenuOpen && ( +
+
+ + + {router.pathname.includes("/wallets/[wallet]") && } +
+
setMobileMenuOpen(false)}>
+
+ )} {/* Sidebar for larger screens */}