A Svelte-friendly wrapper for GunDB, integrating Ethereum and Web3 functionality for decentralized database operations in Svelte applications.
SE-2Gun is based on Scaffold-ETH, a framework for rapid development of decentralized applications (dApps) on Ethereum.
The project integrates GunDB for decentralized data management and Wagmi for Ethereum interaction.
SE-2Gun utilizes gun-eth, a GunDB plugin for generating key pairs through Ethereum signatures. Learn more about gun-eth.
For rapid testing of GunDB within the Scaffold-ETH environment, there's a dedicated extension available. Check out the Scaffold-ETH GunDB extension.
<script>
import { createAccount } from "@byteatatime/wagmi-svelte";
const gun = useGun(); // Initialize GunDB
const { user } = useUser(); // Initialize User
const { address, chainId, status, isConnected } = $derived.by(createAccount()); // Initialize Wagmi
</script>
SE-2Gun provides Ethereum-based authentication:
import { signIn, login, logout } from "$lib/gun/auth";
// Registration
async function handleSignIn() {
const result = await signIn();
if (result) {
console.error(result);
} else {
console.log("Registration successful");
}
}
// Login
async function handleLogin() {
const result = await login();
if (result) {
console.error(result);
} else {
console.log("Login successful");
}
}
// Logout
function handleLogout() {
logout();
console.log("Logged out");
}
Manage user profiles with these functions:
import { loadUserProfile, saveUserProfile, addProfileField } from '$lib/gun/user';
// Load user profile
const profile = await loadUserProfile();
// Save user profile
await saveUserProfile(updatedProfile);
// Add a new field to the profile
addProfileField('city');
Use SHINE for data verification on the blockchain:
async function saveMessage() {
const gunInstance = useGun();
const result = await gunInstance.shine("optimismSepolia", message);
if (result.ok) {
console.log("Message saved and verified on blockchain");
console.log("Node ID:", result.nodeId);
console.log("Transaction Hash:", result.txHash);
} else {
console.error("Error saving message:", result.error);
}
}
async function verifyMessage(nodeId) {
const gunInstance = useGun();
const result = await gunInstance.shine("optimismSepolia", nodeId);
if (result.ok) {
console.log("Message verified on blockchain");
console.log("Timestamp:", result.timestamp);
console.log("Updater:", result.updater);
console.log("Latest Record:", result.latestRecord);
} else {
console.log("Message not verified or not found");
}
}
Displays account information including the public key, color, pulse, blink status, and last login.
import AccountProfile from "$lib/components/gun/account/AccountProfile.svelte";
<AccountProfile />
Shows the user's avatar with options to upload a new one if it's the current user's profile.
import AccountAvatar from "$lib/components/gun/account/AccountAvatar.svelte";
<AccountAvatar size={96} border={2} />
Allows viewing and editing user profile information with dynamic addition and removal of fields.
import ProfileDisplay from "$lib/components/gun/profile/ProfileDisplay.svelte";
<ProfileDisplay />
Here's how you might use these components together on a user profile page:
<script>
import { useUser } from '$lib/gun/user';
import AccountProfile from "$lib/components/gun/account/AccountProfile.svelte";
import AccountAvatar from "$lib/components/gun/account/AccountAvatar.svelte";
import ProfileDisplay from "$lib/components/gun/profile/ProfileDisplay.svelte";
const { user } = useUser();
</script>
<div class="flex flex-col md:flex-row gap-4">
<div class="w-full md:w-1/3">
<AccountAvatar pub={$user.pub} size={128} />
<AccountProfile pub={$user.pub} />
</div>
<div class="w-full md:w-2/3">
<ProfileDisplay />
</div>
</div>
These components and functions work together to provide a comprehensive user profile management system, integrating GunDB, Ethereum authentication, and blockchain verification for decentralized applications.