-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(dashboard-for-dapps): extract common code
- Loading branch information
Showing
7 changed files
with
172 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
63 changes: 63 additions & 0 deletions
63
apps/dashboard-for-dapps/src/components/secret-key-prompt.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Stack } from "@chakra-ui/react"; | ||
import { | ||
Button, | ||
DialogActionTrigger, | ||
DialogBody, | ||
DialogCloseTrigger, | ||
DialogContent, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogRoot, | ||
DialogTitle, | ||
Field, | ||
PasswordInput, | ||
} from "@idos-network/ui-kit"; | ||
import { useRef, useState } from "react"; | ||
|
||
export function SecretKeyPrompt({ | ||
open, | ||
toggle, | ||
onSubmit, | ||
}: { | ||
open: boolean; | ||
toggle: (value?: boolean) => void; | ||
onSubmit: (key: string) => void; | ||
}) { | ||
const ref = useRef<HTMLInputElement>(null); | ||
const [key, setKey] = useState(""); | ||
|
||
const handleSave = () => { | ||
onSubmit(key); | ||
toggle(false); | ||
}; | ||
|
||
return ( | ||
<DialogRoot | ||
open={open} | ||
placement="center" | ||
onOpenChange={() => { | ||
toggle(false); | ||
}} | ||
> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Enter your secret key</DialogTitle> | ||
</DialogHeader> | ||
<DialogBody> | ||
<Stack gap="4"> | ||
<Field label="Secret key:"> | ||
<PasswordInput ref={ref} onChange={(e) => setKey(e.target.value)} /> | ||
</Field> | ||
</Stack> | ||
</DialogBody> | ||
<DialogFooter> | ||
<DialogActionTrigger asChild> | ||
<Button variant="outline">Cancel</Button> | ||
</DialogActionTrigger> | ||
<Button onClick={handleSave}>Save</Button> | ||
</DialogFooter> | ||
<DialogCloseTrigger /> | ||
</DialogContent> | ||
</DialogRoot> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useLocalStorage } from "@uidotdev/usehooks"; | ||
|
||
export const useSecretKey = () => { | ||
const [secretKey, setSecretKey] = useLocalStorage("SECRET_KEY", ""); | ||
return [secretKey, setSecretKey] as const; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { base64Decode, utf8Decode } from "@idos-network/codecs"; | ||
import nacl from "tweetnacl"; | ||
|
||
export function decrypt(b64FullMessage: string, b64SenderPublicKey: string, secretKey: string) { | ||
const fullMessage = base64Decode(b64FullMessage); | ||
const senderPublicKey = base64Decode(b64SenderPublicKey); | ||
|
||
const nonce = fullMessage.slice(0, nacl.box.nonceLength); | ||
const message = fullMessage.slice(nacl.box.nonceLength, fullMessage.length); | ||
|
||
const decrypted = nacl.box.open(message, nonce, senderPublicKey, base64Decode(secretKey)); | ||
|
||
if (decrypted == null) { | ||
return ""; | ||
} | ||
|
||
return utf8Decode(decrypted); | ||
} | ||
|
||
export function openImageInNewTab(base64Image: string) { | ||
const newWindow = window.open(); | ||
|
||
if (newWindow) { | ||
newWindow.document.write(` | ||
<html> | ||
<head> | ||
<title>Document Image</title> | ||
<style> | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
min-height: 100vh; | ||
background: #000; | ||
} | ||
img { | ||
max-width: 100%; | ||
max-height: 100vh; | ||
object-fit: contain; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<img src="${base64Image}" alt="Document Image"> | ||
</body> | ||
</html> | ||
`); | ||
newWindow.document.close(); | ||
} | ||
} | ||
|
||
export function changeCase(str: string) { | ||
return str.replace(/_/g, " "); | ||
} |
Oops, something went wrong.