From 1789b789874059d26491cfd45925ab1b304d18ec Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 30 Jan 2024 23:33:41 +0100 Subject: [PATCH] add import / export --- examples/rln-identity/index.html | 5 ++++ examples/rln-identity/package-lock.json | 14 +++++----- examples/rln-identity/package.json | 2 +- examples/rln-identity/src/rln.js | 4 +-- examples/rln-identity/src/ui.js | 36 +++++++++++++++++++++++-- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/examples/rln-identity/index.html b/examples/rln-identity/index.html index 75752e4..fc1b937 100644 --- a/examples/rln-identity/index.html +++ b/examples/rln-identity/index.html @@ -122,6 +122,10 @@ justify-content: space-between; align-items: center; } + + .hidden { + display: none; + } @@ -142,6 +146,7 @@

Keystore

+
diff --git a/examples/rln-identity/package-lock.json b/examples/rln-identity/package-lock.json index 930912b..9627a11 100644 --- a/examples/rln-identity/package-lock.json +++ b/examples/rln-identity/package-lock.json @@ -8,7 +8,7 @@ "name": "rln-chat", "version": "0.1.0", "dependencies": { - "@waku/rln": "0.1.1-bafbe01", + "@waku/rln": "0.1.1-77ba0a6", "@waku/sdk": "^0.0.22", "@waku/utils": "^0.0.14", "ethers": "^5.7.2", @@ -2209,9 +2209,9 @@ } }, "node_modules/@waku/rln": { - "version": "0.1.1-bafbe01", - "resolved": "https://registry.npmjs.org/@waku/rln/-/rln-0.1.1-bafbe01.tgz", - "integrity": "sha512-2KZ/1EbJH+Q/scW1rO/MemfRaWrygHr3+9lL2CQSMbbB0w9r4zU9w9o1W2VmOlj/KUZUjhVLoCNY5PpzcK3Ayw==", + "version": "0.1.1-77ba0a6", + "resolved": "https://registry.npmjs.org/@waku/rln/-/rln-0.1.1-77ba0a6.tgz", + "integrity": "sha512-qJGlTsL9xosrF9uNtzfKFa60ouOs2i0LidfolF2S381TZqq1vBa+mullLol5m7XMgvL8ULpKd+1KasXd0lKjMA==", "dependencies": { "@chainsafe/bls-keystore": "^3.0.0", "@waku/utils": "^0.0.13", @@ -11607,9 +11607,9 @@ } }, "@waku/rln": { - "version": "0.1.1-bafbe01", - "resolved": "https://registry.npmjs.org/@waku/rln/-/rln-0.1.1-bafbe01.tgz", - "integrity": "sha512-2KZ/1EbJH+Q/scW1rO/MemfRaWrygHr3+9lL2CQSMbbB0w9r4zU9w9o1W2VmOlj/KUZUjhVLoCNY5PpzcK3Ayw==", + "version": "0.1.1-77ba0a6", + "resolved": "https://registry.npmjs.org/@waku/rln/-/rln-0.1.1-77ba0a6.tgz", + "integrity": "sha512-qJGlTsL9xosrF9uNtzfKFa60ouOs2i0LidfolF2S381TZqq1vBa+mullLol5m7XMgvL8ULpKd+1KasXd0lKjMA==", "requires": { "@chainsafe/bls-keystore": "^3.0.0", "@waku/utils": "^0.0.13", diff --git a/examples/rln-identity/package.json b/examples/rln-identity/package.json index d012654..f99c32f 100644 --- a/examples/rln-identity/package.json +++ b/examples/rln-identity/package.json @@ -7,7 +7,7 @@ "start": "webpack-dev-server" }, "dependencies": { - "@waku/rln": "0.1.1-bafbe01", + "@waku/rln": "0.1.1-77ba0a6", "@waku/sdk": "^0.0.22", "@waku/utils": "^0.0.14", "ethers": "^5.7.2", diff --git a/examples/rln-identity/src/rln.js b/examples/rln-identity/src/rln.js index c497fe9..8197294 100644 --- a/examples/rln-identity/src/rln.js +++ b/examples/rln-identity/src/rln.js @@ -1,4 +1,4 @@ -import { createRLN, Keystore } from "@waku/rln"; +import { createRLN, Keystore, extractMetaMaskSigner } from "@waku/rln"; import { randomNumber } from "./utils"; import { SIGNATURE_MESSAGE } from "./const"; @@ -70,7 +70,7 @@ export async function initRLN({ onStatusChange }) { }; const importLocalKeystore = (keystoreStr) => { - rln.keystore = Keystore.fromString(keystoreStr); + rln.keystore = Keystore.fromString(keystoreStr) || Keystore.create(); }; return { diff --git a/examples/rln-identity/src/ui.js b/examples/rln-identity/src/ui.js index d4a7426..157986d 100644 --- a/examples/rln-identity/src/ui.js +++ b/examples/rln-identity/src/ui.js @@ -2,8 +2,9 @@ import { renderBytes } from "./utils"; const status = document.getElementById("status"); const connectWalletButton = document.getElementById("connect"); -const importKeystore = document.getElementById("import"); -const exportKeystore = document.getElementById("export"); +const importKeystoreButton = document.getElementById("import"); +const importFileInput = document.getElementById("import-file"); +const exportKeystoreButton = document.getElementById("export"); const keystoreOptions = document.getElementById("keystore"); const keystorePassword = document.getElementById("password"); const readCredentialButton = document.getElementById("read-credential"); @@ -105,6 +106,37 @@ export function initUI() { const credential = await readCredential(currentHash, password); _renderCredential(currentHash, credential); }); + + importFileInput.addEventListener("change", async (event) => { + const file = event.currentTarget.files[0]; + + if (!file) { + return; + } + + const text = await file.text(); + importLocalKeystore(text); + + const keystoreOptions = readKeystoreOptions(); + _renderKeystoreOptions(keystoreOptions); + }); + + importKeystoreButton.addEventListener("click", async () => { + importFileInput.click(); + }); + + exportKeystoreButton.addEventListener("click", () => { + const filename = "keystore.json"; + const text = saveLocalKeystore(); + const file = new File([text], filename, { + type: "application/json", + }); + + const link = document.createElement("a"); + link.href = URL.createObjectURL(file); + link.download = filename; + link.click(); + }); }; return {