From 1e8b2af9822a3b974968e09e8c89ee7e89ba295d Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 27 Oct 2023 16:46:24 +0200 Subject: [PATCH] add import export --- .../src/app/home/components/Keystore.tsx | 57 ++++++++++++++++++- examples/rln-js/src/services/rln.ts | 9 ++- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/examples/rln-js/src/app/home/components/Keystore.tsx b/examples/rln-js/src/app/home/components/Keystore.tsx index 270de4e..3a62c6f 100644 --- a/examples/rln-js/src/app/home/components/Keystore.tsx +++ b/examples/rln-js/src/app/home/components/Keystore.tsx @@ -2,7 +2,7 @@ import React from "react"; import { Block, BlockTypes } from "@/components/Block"; import { Button } from "@/components/Button"; import { Subtitle } from "@/components/Subtitle"; -import { useStore, useWallet } from "@/hooks"; +import { useRLN, useStore, useWallet } from "@/hooks"; import { useKeystore } from "@/hooks/useKeystore"; export const Keystore: React.FunctionComponent<{}> = () => { @@ -12,6 +12,8 @@ export const Keystore: React.FunctionComponent<{}> = () => { const { password, onPasswordChanged } = usePassword(); const { selectedKeystore, onKeystoreChanged } = useSelectedKeystore(); + const { onExportKeystore, onImportKeystoreFileChange } = + useImportExportKeystore(); const credentialsNodes = React.useMemo( () => @@ -28,8 +30,20 @@ export const Keystore: React.FunctionComponent<{}> = () => { Keystore
- - + + +
@@ -105,3 +119,40 @@ function useSelectedKeystore() { onKeystoreChanged, }; } + +function useImportExportKeystore() { + const { rln } = useRLN(); + + const onExportKeystore = () => { + if (!rln) { + return; + } + + const filename = "keystore.json"; + const text = rln.keystore.toString(); + const file = new File([text], filename, { + type: "application/json", + }); + + const link = document.createElement("a"); + link.href = URL.createObjectURL(file); + link.download = filename; + link.click(); + }; + + const onImportKeystoreFileChange = async ( + event: React.FormEvent + ) => { + const file = event.currentTarget?.files?.[0]; + if (!file || !rln) { + return; + } + const text = await file.text(); + rln.importKeystore(text); + }; + + return { + onExportKeystore, + onImportKeystoreFileChange, + }; +} diff --git a/examples/rln-js/src/services/rln.ts b/examples/rln-js/src/services/rln.ts index 79aa66b..edd2b91 100644 --- a/examples/rln-js/src/services/rln.ts +++ b/examples/rln-js/src/services/rln.ts @@ -42,7 +42,7 @@ export class RLN implements IRLN { public rlnInstance: undefined | RLNInstance; public rlnContract: undefined | RLNContract; - public readonly keystore: Keystore; + public keystore: Keystore; private initialized = false; private initializing = false; @@ -135,9 +135,14 @@ export class RLN implements IRLN { } public async saveKeystore() { - localStorage.setItem("keystore", this.keystore.toString()); + // localStorage.setItem("keystore", this.keystore.toString()); this.emitKeystoreKeys(); } + + public importKeystore(value: string) { + this.keystore = Keystore.fromString(value) || Keystore.create(); + this.saveKeystore(); + } } export const rln = typeof window === "undefined" ? undefined : new RLN();