Skip to content

Commit

Permalink
little bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
scobru committed Oct 12, 2024
1 parent 03f4496 commit 607b878
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 154 deletions.
129 changes: 129 additions & 0 deletions packages/svelte/src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import Gun from "gun";
import "gun-eth";
import { getAccount } from "@wagmi/core";
import { currentUser, gun } from "$lib/stores";
import { get } from "svelte/store";
import { notification } from "$lib/utils/scaffold-eth/notification";
import { wagmiConfig } from "$lib/wagmi";
import type { IGunUserInstance } from "gun/types";

const MESSAGE_TO_SIGN = "Accesso a GunDB con Ethereum";

export function initializeAuth(gunInstance: Gun): IGunUserInstance {
const user: IGunUserInstance = gunInstance.user();

user.recall({ sessionStorage: true }, async ack => {
if ("err" in ack) {
console.error("Errore nel recupero della sessione:", ack.err);
} else if (user.is && user.is.alias) {
currentUser.set(user.is.alias as string);
await loadUserData(user);
}
});

user.on("auth", async () => {
console.log("Utente autenticato:", user.is.alias as string);
currentUser.set(user.is.alias as string);
await loadUserData(user);
});

return user;
}

async function loadUserData(user: IGunUserInstance) {
console.log("Caricamento dati utente...");
const gunInstance = get(gun);
const account = getAccount(wagmiConfig);

if (user.is && user.is.alias) {
const signature = await gunInstance.createSignature(MESSAGE_TO_SIGN);
const userPair = await gunInstance.getAndDecryptPair(account.address, signature);
console.log("Coppia utente:", userPair);
// Qui puoi aggiungere la logica per gestire i dati dell'utente
}
}

export async function registra(user: IGunUserInstance): Promise<string | null> {
console.log("Registrazione in corso...");
const gunInstance = get(gun);
const account = getAccount(wagmiConfig);

try {
if (!account.isConnected) {
return "Per favore connetti il tuo portafoglio Ethereum";
}

const signature = await gunInstance.createSignature(MESSAGE_TO_SIGN);

if (!signature) {
return "Errore durante la firma del messaggio";
}

await gunInstance.createAndStoreEncryptedPair(account.address, signature);

return new Promise(resolve => {
user.create(account.address, signature, async (ack: { ok: 0; pub: string } | { err: string }) => {
if ("err" in ack) {
resolve("Errore durante la registrazione: " + ack.err);
} else {
await loadUserData(user);
currentUser.set(user.is.alias);
resolve(null);
}
});
});
} catch (error) {
return "Errore durante la registrazione: " + error.message;
}
}

export async function accedi(user: IGunUserInstance): Promise<string | null> {
const account = getAccount(wagmiConfig);

if (!account.isConnected) {
notification.error("Nessun account Ethereum connesso");
return "Nessun account Ethereum connesso";
}

console.log("Accesso in corso...");
const gunInstance = get(gun);

try {
if (!account.address) {
return "Nessun account Ethereum connesso";
}

const signature = await gunInstance.createSignature(MESSAGE_TO_SIGN);
if (!signature) {
return "Errore durante la firma del messaggio";
}

const pair = await gunInstance.getAndDecryptPair(account.address, signature);
console.log("Pair:", pair);
if (!pair) {
return "Errore nel recupero del pair dell'utente";
}

return new Promise(resolve => {
user.auth(pair, async (ack: { err: string }) => {
console.log("Risposta di autenticazione:", ack);
if (ack.err) {
resolve("Errore di accesso: " + ack.err);
} else {
console.log("Accesso riuscito");
currentUser.set(user.is.alias);
await loadUserData(user);
resolve(null);
}
});
});
} catch (error) {
return "Errore durante l'accesso: " + error.message;
}
}

export function esci(user: IGunUserInstance): void {
user.leave();
currentUser.set(null);
// Puoi aggiungere qui altre operazioni di pulizia se necessario
}
65 changes: 32 additions & 33 deletions packages/svelte/src/lib/gun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ let gun;
* import { useGun } from '@gun-vue/composables'
* const gun = useGun()
*/
export function createGun(options = { localStorage: false }) {
if (!gun) {
const opts = { peers: peers };
if (typeof options === "object") {
Object.assign(opts, options);
}
console.log(opts.peers);
gun = Gun(opts);
export function useGun(options = { localStorage: false }) {
if (!gun) {
const opts = { peers: peers };
if (typeof options === "object") {
Object.assign(opts, options);
}
return gun;
console.log(opts.peers);
gun = Gun(opts);
}
return gun;
}

/**
* @param {...string} args
* @returns {import('gun').IGunInstance}
*/
export function createGunPath(...args) {
const gun = createGun();
let g;
for (let arg of args) {
g = gun.get(arg);
}
return g || gun;
export function useGunPath(...args) {
const gun = useGun();
let g;
for (let arg of args) {
g = gun.get(arg);
}
return g || gun;
}

/**
Expand All @@ -60,8 +60,8 @@ export function createGunPath(...args) {
* @returns {import('gun').IGunInstance}
*/
export function createGunSecondary(options = { localStorage: false }) {
const gun2 = Gun({ peers: peers, ...options });
return gun2;
const gun2 = Gun({ peers: peers, ...options });
return gun2;
}

/**
Expand All @@ -86,20 +86,19 @@ export const soul = Gun?.node?.soul;
*/
export const genUUID = Gun?.text?.random;


/**
* Authorized Puts
* Authorized Puts
*/
Gun.on('opt', function (ctx) {
if (ctx.once) {
return
}
ctx.on('out', function (msg) {
var to = this.to
// Adds headers for put
msg.headers = {
token: 'test'
}
to.next(msg) // pass to next middleware
})
})
Gun.on("opt", function (ctx) {
if (ctx.once) {
return;
}
ctx.on("out", function (msg) {
var to = this.to;
// Adds headers for put
msg.headers = {
token: "test",
};
to.next(msg); // pass to next middleware
});
});
Loading

0 comments on commit 607b878

Please sign in to comment.