Skip to content

Commit

Permalink
add gunToEthAccount method in gun-eth plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
scobru committed Oct 18, 2024
1 parent 714833d commit 74639f3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
30 changes: 30 additions & 0 deletions packages/gun-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
43 changes: 23 additions & 20 deletions packages/svelte/src/routes/inspect/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
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);
});
initNetwork();
}
function initNetwork() {
if (!browser || !container) return;
const data = {
nodes: [],
edges: [],
Expand All @@ -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) {
Expand Down Expand Up @@ -93,16 +91,21 @@
}
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 !== "_") {
nodes.push({ id, label });
}
}
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 });
}
Expand Down

0 comments on commit 74639f3

Please sign in to comment.