Skip to content

Commit

Permalink
Merge pull request #217 from KeychainMDIP/201-check-and-fix
Browse files Browse the repository at this point in the history
Added wallet check/fix to web UI
  • Loading branch information
macterra authored Jul 3, 2024
2 parents c9df328 + 0810024 commit 0c6626b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 18 deletions.
36 changes: 28 additions & 8 deletions kc-app/src/KeymasterUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function KeymasterUI({ keymaster, title }) {
const [mnemonicString, setMnemonicString] = useState('');
const [walletString, setWalletString] = useState('');
const [manifest, setManifest] = useState(null);
const [checkingWallet, setCheckingWallet] = useState(false);

useEffect(() => {
refreshAll();
Expand Down Expand Up @@ -657,6 +658,25 @@ function KeymasterUI({ keymaster, title }) {
}
}

async function checkWallet() {
setCheckingWallet(true);
try {
const { checked, invalid, deleted } = await keymaster.checkWallet();

if (invalid == 0 && deleted == 0) {
window.alert(`${checked} DIDs checked, no problems found`);
}
else if (window.confirm(`${checked} DIDs checked\n${invalid} invalid DIDs found\n${deleted} deleted DIDs found\n\nFix wallet?`)) {
const { idsRemoved, ownedRemoved, heldRemoved } = await keymaster.fixWallet();
window.alert(`${idsRemoved} IDs removed\n${ownedRemoved} owned DIDs removed\n${heldRemoved} held DIDs removed`);
}

} catch (error) {
window.alert(error);
}
setCheckingWallet(false);
}

async function showWallet() {
try {
const wallet = await keymaster.loadWallet();
Expand Down Expand Up @@ -1420,6 +1440,11 @@ function KeymasterUI({ keymaster, title }) {
Recover...
</Button>
</Grid>
<Grid item>
<Button variant="contained" color="primary" onClick={checkWallet} disabled={checkingWallet}>
Check...
</Button>
</Grid>
</Grid>
<p />
<Grid container direction="row" justifyContent="flex-start" alignItems="center" spacing={3}>
Expand All @@ -1434,14 +1459,6 @@ function KeymasterUI({ keymaster, title }) {
</Button>
)}
</Grid>
<Grid item>
<Box>
<pre>{mnemonicString}</pre>
</Box>
</Grid>
</Grid>
<p />
<Grid container direction="row" justifyContent="flex-start" alignItems="center" spacing={3}>
<Grid item>
{walletString ? (
<Button variant="contained" color="primary" onClick={hideWallet}>
Expand All @@ -1465,6 +1482,9 @@ function KeymasterUI({ keymaster, title }) {
</Grid>
</Grid>
<p />
<Box>
<pre>{mnemonicString}</pre>
</Box>
<Box>
{walletString &&
<textarea
Expand Down
20 changes: 20 additions & 0 deletions kc-app/src/keymaster-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ export async function recoverWallet() {
}
}

export async function checkWallet() {
try {
const response = await axios.post(`/api/v1/check-wallet`);
return response.data;
}
catch (error) {
throwError(error);
}
}

export async function fixWallet() {
try {
const response = await axios.post(`/api/v1/fix-wallet`);
return response.data;
}
catch (error) {
throwError(error);
}
}

export async function decryptMnemonic() {
try {
const response = await axios.get(`/api/v1/mnemonic`);
Expand Down
18 changes: 18 additions & 0 deletions keymaster-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ v1router.post('/recover-wallet', async (req, res) => {
}
});

v1router.post('/check-wallet', async (req, res) => {
try {
const response = await keymaster.checkWallet();
res.json(response);
} catch (error) {
res.status(500).send(error.toString());
}
});

v1router.post('/fix-wallet', async (req, res) => {
try {
const response = await keymaster.fixWallet();
res.json(response);
} catch (error) {
res.status(500).send(error.toString());
}
});

v1router.get('/mnemonic', async (req, res) => {
try {
const mnemonic = keymaster.decryptMnemonic();
Expand Down
36 changes: 28 additions & 8 deletions keymaster-app/src/KeymasterUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function KeymasterUI({ keymaster, title }) {
const [mnemonicString, setMnemonicString] = useState('');
const [walletString, setWalletString] = useState('');
const [manifest, setManifest] = useState(null);
const [checkingWallet, setCheckingWallet] = useState(false);

useEffect(() => {
refreshAll();
Expand Down Expand Up @@ -657,6 +658,25 @@ function KeymasterUI({ keymaster, title }) {
}
}

async function checkWallet() {
setCheckingWallet(true);
try {
const { checked, invalid, deleted } = await keymaster.checkWallet();

if (invalid == 0 && deleted == 0) {
window.alert(`${checked} DIDs checked, no problems found`);
}
else if (window.confirm(`${checked} DIDs checked\n${invalid} invalid DIDs found\n${deleted} deleted DIDs found\n\nFix wallet?`)) {
const { idsRemoved, ownedRemoved, heldRemoved } = await keymaster.fixWallet();
window.alert(`${idsRemoved} IDs removed\n${ownedRemoved} owned DIDs removed\n${heldRemoved} held DIDs removed`);
}

} catch (error) {
window.alert(error);
}
setCheckingWallet(false);
}

async function showWallet() {
try {
const wallet = await keymaster.loadWallet();
Expand Down Expand Up @@ -1420,6 +1440,11 @@ function KeymasterUI({ keymaster, title }) {
Recover...
</Button>
</Grid>
<Grid item>
<Button variant="contained" color="primary" onClick={checkWallet} disabled={checkingWallet}>
Check...
</Button>
</Grid>
</Grid>
<p />
<Grid container direction="row" justifyContent="flex-start" alignItems="center" spacing={3}>
Expand All @@ -1434,14 +1459,6 @@ function KeymasterUI({ keymaster, title }) {
</Button>
)}
</Grid>
<Grid item>
<Box>
<pre>{mnemonicString}</pre>
</Box>
</Grid>
</Grid>
<p />
<Grid container direction="row" justifyContent="flex-start" alignItems="center" spacing={3}>
<Grid item>
{walletString ? (
<Button variant="contained" color="primary" onClick={hideWallet}>
Expand All @@ -1465,6 +1482,9 @@ function KeymasterUI({ keymaster, title }) {
</Grid>
</Grid>
<p />
<Box>
<pre>{mnemonicString}</pre>
</Box>
<Box>
{walletString &&
<textarea
Expand Down
5 changes: 4 additions & 1 deletion rebuildGatekeeper
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#!/usr/bin/env bash
npm --prefix ./keymaster-app run build && node gatekeeper-api.js
set -e

npm --prefix ./keymaster-app run build
node gatekeeper-api.js
5 changes: 4 additions & 1 deletion rebuildKeymaster
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#!/usr/bin/env bash
npm --prefix ./kc-app run build && node keymaster-api.js
set -e

npm --prefix ./kc-app run build
node keymaster-api.js

0 comments on commit 0c6626b

Please sign in to comment.