Skip to content

Commit

Permalink
add import / export
Browse files Browse the repository at this point in the history
  • Loading branch information
weboko committed Jan 30, 2024
1 parent 75438f3 commit 1789b78
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
5 changes: 5 additions & 0 deletions examples/rln-identity/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@
justify-content: space-between;
align-items: center;
}

.hidden {
display: none;
}
</style>
</head>
<body>
Expand All @@ -142,6 +146,7 @@ <h2>Wallet</h2>
<h2>Keystore</h2>
<div>
<button id="import">Import</button>
<input id="import-file" class="hidden" type="file" />
<button id="export">Export</button>
</div>
</div>
Expand Down
14 changes: 7 additions & 7 deletions examples/rln-identity/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/rln-identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions examples/rln-identity/src/rln.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 34 additions & 2 deletions examples/rln-identity/src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1789b78

Please sign in to comment.