From 74639f37f776d27995961acaee054239cfd99a6c Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 19 Oct 2024 00:20:47 +0200 Subject: [PATCH] add gunToEthAccount method in gun-eth plugin --- packages/gun-eth/src/index.js | 30 +++++++++++++ .../svelte/src/routes/inspect/+page.svelte | 43 ++++++++++--------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/packages/gun-eth/src/index.js b/packages/gun-eth/src/index.js index d073069..89041e7 100644 --- a/packages/gun-eth/src/index.js +++ b/packages/gun-eth/src/index.js @@ -340,4 +340,34 @@ Gun.chain.shine = function (chain, nodeId, data, callback) { return gun; }; + +/** + * Converts a Gun private key to an Ethereum account. + * @param {string} gunPrivateKey - The Gun private key in base64url format. + * @returns {Object} An object containing the Ethereum account and public key. + */ +Gun.chain.gunToEthAccount = function(gunPrivateKey) { + // Function to convert base64url to hex + const base64UrlToHex = (base64url) => { + const padding = "=".repeat((4 - (base64url.length % 4)) % 4); + const base64 = base64url.replace(/-/g, "+").replace(/_/g, "/") + padding; + const binary = atob(base64); + return Array.from(binary, (char) => char.charCodeAt(0).toString(16).padStart(2, "0")).join(""); + }; + + // Convert Gun private key to hex format + const hexPrivateKey = "0x" + base64UrlToHex(gunPrivateKey); + + // Create an Ethereum wallet from the private key + const wallet = new ethers.Wallet(hexPrivateKey); + + // Get the public address (public key) + const publicKey = wallet.address; + + return { + account: wallet, + publicKey: publicKey + }; +}; + module.exports = Gun; diff --git a/packages/svelte/src/routes/inspect/+page.svelte b/packages/svelte/src/routes/inspect/+page.svelte index a5cf7b8..8c6c598 100644 --- a/packages/svelte/src/routes/inspect/+page.svelte +++ b/packages/svelte/src/routes/inspect/+page.svelte @@ -17,14 +17,12 @@ onMount(() => { initGun(); + initNetwork(); }); function initGun() { - if (get(gun) === null) { - gunInstance = gun.set(new Gun(get(customRelay))); - } else { - gunInstance = get(gun); - } + gun.set(new Gun(get(customRelay))); + gunInstance = get(gun) gunInstance.on("hi", (peer: any) => { console.log("Connected to peer:", peer); }); @@ -32,6 +30,8 @@ } function initNetwork() { + if (!browser || !container) return; + const data = { nodes: [], edges: [], @@ -54,18 +54,16 @@ stabilization: { iterations: 150 }, }, }; - if (browser) { - network = new Network(container, data, options); - } - if (network) { - network.on("click", function (params) { - if (params.nodes.length > 0) { - const nodeId = params.nodes[0]; - nodePath.set(nodeId); // Aggiorna l'input box - loadNodeData(nodeId); // Carica i dati del nodo - } - }); - } + + network = new Network(container, data, options); + + network.on("click", function (params) { + if (params.nodes.length > 0) { + const nodeId = params.nodes[0]; + nodePath.set(nodeId); + loadNodeData(nodeId); + } + }); } async function loadNodeData(path = $nodePath) { @@ -93,8 +91,13 @@ } function updateGraph(data: any, rootPath: string) { - const nodes: { id: any; label: any }[] = []; - const edges: { from: any; to: any }[] = []; + if (!network) { + console.error("Network non inizializzato"); + return; + } + + const nodes: { id: string; label: string }[] = []; + const edges: { from: string; to: string }[] = []; function addNode(id: string, label: string) { if (!nodes.some(node => node.id === id) && id !== "#" && id !== "_") { @@ -102,7 +105,7 @@ } } - function addEdge(from: any, to: string) { + function addEdge(from: string, to: string) { if (!edges.some(edge => edge.from === from && edge.to === to)) { edges.push({ from, to }); }