Skip to content

Commit

Permalink
fix shine
Browse files Browse the repository at this point in the history
  • Loading branch information
scobru committed Oct 16, 2024
1 parent cfccc24 commit 08bcacc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
5 changes: 1 addition & 4 deletions packages/svelte/src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<script lang="ts">
import { onMount } from "svelte";
import { wagmiConfig } from "$lib/wagmi";
import { getAccount } from "@wagmi/core";
import { gun } from "$lib/stores";
import { get } from "svelte/store";
import { notification } from "$lib/utils/scaffold-eth/notification";
import { initializeAuth, signIn, login, logout } from "$lib/gun/auth";
import { signIn, login, logout } from "$lib/gun/auth";
import { useUser, loadUserProfile } from "$lib/gun/user";
import { writable } from 'svelte/store';
Expand Down
78 changes: 75 additions & 3 deletions packages/svelte/src/routes/shine/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import "gun-eth";
import { createAccount } from "@byteatatime/wagmi-svelte";
import { tick } from "svelte";
import { useGun } from "$lib/gun/gun";
import { ethers } from "ethers";
import { notification } from "$lib/utils/scaffold-eth/notification";
let message = $state("");
let nodeId = $state("");
Expand All @@ -15,9 +18,11 @@
let error = $state("");
let verificationResult = $state(null);
let gunInstance = $state(null);
let editMessage = $state("");
let editNodeId = $state("");
onMount(async () => {
gunInstance = get(gun) === null ? new Gun() : get(gun);
gunInstance = useGun() === null ? new Gun() : useGun();
});
const { address, chainId, status, isConnected } = $derived.by(createAccount());
Expand All @@ -32,7 +37,7 @@
error = "";
try {
const data = { message };
const data = message ;
const result = await new Promise((resolve, reject) => {
gunInstance.shine("optimismSepolia", null, data, function (ack) {
Expand Down Expand Up @@ -87,6 +92,50 @@
isLoading = false;
}
}
async function editLocalMessage() {
if (!editNodeId || !editMessage) {
error = "Per favore, inserisci sia il Node ID che il nuovo messaggio.";
return;
}
isLoading = true;
error = "";
try {
// Recupera il dato esistente
const existingData = await new Promise((resolve) => {
gunInstance.get(editNodeId).once((data) => resolve(data));
});
// Prepara i nuovi dati
const newData = {
message: editMessage,
_contentHash: existingData._contentHash // Manteniamo il contentHash precedente per il calcolo
};
// Calcola il nuovo contentHash
const dataString = JSON.stringify(editMessage);
const newContentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
// Aggiorna i dati con il nuovo contentHash
newData._contentHash = newContentHash;
// Modifica il messaggio e il _contentHash localmente su Gun
gunInstance.get(editNodeId).put(newData, (ack) => {
if (ack.err) {
throw new Error(ack.err);
}
notification.success("Messaggio e hash modificati localmente con successo!");
savedMessage = editMessage;
});
} catch (err) {
console.error("Errore durante la modifica locale:", err);
error = `Si è verificato un errore durante la modifica locale del messaggio: ${err.message}`;
} finally {
isLoading = false;
}
}
</script>

<div class="flex flex-grow flex-col font-sans">
Expand Down Expand Up @@ -133,6 +182,29 @@
{isLoading ? "Verifica in corso..." : "Verifica Messaggio"}
</button>
</div>

<div class="bg-ableton-purple p-6 rounded-none text-white">
<h2 class="text-2xl font-semibold mb-4">Modifica Messaggio Locale</h2>
<input
type="text"
bind:value={editNodeId}
placeholder="Node ID"
class="w-full p-2 mb-4 border border-gray-300 focus:outline-none focus:border-blue-500 text-black"
/>
<input
type="text"
bind:value={editMessage}
placeholder="Nuovo messaggio"
class="w-full p-2 mb-4 border border-gray-300 focus:outline-none focus:border-blue-500 text-black"
/>
<button
on:click={editLocalMessage}
disabled={isLoading || !editNodeId || !editMessage}
class="w-full bg-transparent text-white p-2 hover:bg-purple-600 transition duration-300 ease-in-out"
>
{isLoading ? "Modifica in corso..." : "Modifica Messaggio Locale"}
</button>
</div>
</div>

{#if error}
Expand Down Expand Up @@ -184,4 +256,4 @@
{:else}
<p class="text-xl font-semibold bg-yellow-100 p-4 rounded-none">Per favore, connetti il tuo wallet per utilizzare questa funzionalità.</p>
{/if}
</div>
</div>

0 comments on commit 08bcacc

Please sign in to comment.