|
| 1 | +import React, { Component } from "react"; |
| 2 | +import { Box } from "../ui/Box"; |
| 3 | +import { ArrowButton, BackArrowButton } from "../ui/Button"; |
| 4 | +import { Card } from "../ui/Card"; |
| 5 | +import { Container } from "../ui/Container"; |
| 6 | +import { Input } from "../ui/Input"; |
| 7 | +import { SecureFileIcon } from "../ui/Images"; |
| 8 | +import { H3, Text } from "../ui/Text"; |
| 9 | +import { getAesEncryptor } from "../../shared/AesEncryption"; |
| 10 | +import { ValidationResult } from "../../shared/ValidationResult"; |
| 11 | + |
| 12 | +export interface WalletSecureFilePasswordProps { |
| 13 | + mnemonic: string; |
| 14 | + onCancel: () => void; |
| 15 | + validationResult?: ValidationResult<string>; |
| 16 | +} |
| 17 | + |
| 18 | +export interface WalletSecureFilePasswordState { |
| 19 | + password: string; |
| 20 | + confirmPassword: string; |
| 21 | + error?: string; |
| 22 | + fileCipherText?: string; |
| 23 | +} |
| 24 | + |
| 25 | +export class WalletSecureFilePassword extends Component< |
| 26 | + WalletSecureFilePasswordProps, |
| 27 | + WalletSecureFilePasswordState |
| 28 | +> { |
| 29 | + constructor(props: WalletSecureFilePasswordProps) { |
| 30 | + super(props); |
| 31 | + // bind events |
| 32 | + this.componentDidMount = this.componentDidMount.bind(this); |
| 33 | + this.componentDidUnmount = this.componentDidUnmount.bind(this); |
| 34 | + this.encryptSecureMnemonicFile = this.encryptSecureMnemonicFile.bind(this); |
| 35 | + |
| 36 | + this.state = { password: "", confirmPassword: "" }; |
| 37 | + } |
| 38 | + |
| 39 | + componentDidMount(): void {} |
| 40 | + |
| 41 | + componentDidUnmount(): void {} |
| 42 | + |
| 43 | + private encryptSecureMnemonicFile(): void { |
| 44 | + if (this.state.password !== this.state.confirmPassword) { |
| 45 | + this.setState({ error: "Passwords do not match" }); |
| 46 | + return; |
| 47 | + } else if (!/.{6,}/.test(this.state.password)) { |
| 48 | + this.setState({ error: "Password must be > 6 characters" }); |
| 49 | + return; |
| 50 | + } |
| 51 | + if (this.state.password) { |
| 52 | + try { |
| 53 | + const { encrypt } = getAesEncryptor(this.state.password); |
| 54 | + const encryptedMnemonic = "data:application/json;base64," + encrypt(this.props.mnemonic); |
| 55 | + console.log(encryptedMnemonic); |
| 56 | + this.setState({ fileCipherText: encryptedMnemonic }); |
| 57 | + } catch (e) { |
| 58 | + console.log("Error:", e); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + render() { |
| 64 | + const { onCancel } = this.props; |
| 65 | + const { error } = this.state; |
| 66 | + return ( |
| 67 | + <> |
| 68 | + <Container height="50vh" margin="10% 5% 0 0"> |
| 69 | + <Box direction="column" align="center" width="100%"> |
| 70 | + <Box |
| 71 | + direction="column" |
| 72 | + width="800px" |
| 73 | + align="start" |
| 74 | + margin="0 auto 0 auto" |
| 75 | + > |
| 76 | + <div style={{ display: "flex" }}> |
| 77 | + <BackArrowButton onClick={() => onCancel()} /> |
| 78 | + <Card |
| 79 | + width="100%" |
| 80 | + align="center" |
| 81 | + minHeight="225px" |
| 82 | + padding="2em 4em 2em 2em" |
| 83 | + > |
| 84 | + <Box display="flex" direction="row" margin="0"> |
| 85 | + <Box width="120px" margin="0"> |
| 86 | + <SecureFileIcon width="60px" height="60px" /> |
| 87 | + </Box> |
| 88 | + <Box margin="0 0 0 2em"> |
| 89 | + <H3>Secure file</H3> |
| 90 | + <Text fontSize="14px"> |
| 91 | + Create a secure file password{" "} |
| 92 | + </Text> |
| 93 | + <Input |
| 94 | + value={this.state.password} |
| 95 | + name="password" |
| 96 | + onChange={(e) => |
| 97 | + this.setState({ password: e.target.value }) |
| 98 | + } |
| 99 | + placeholder="Password" |
| 100 | + type="password" |
| 101 | + margin="1em 0 1em 0" |
| 102 | + padding="0 1em 0 1em" |
| 103 | + autoFocus={true} |
| 104 | + error={error ? true : false} |
| 105 | + /> |
| 106 | + <Text fontSize="14px">Confirm Password</Text> |
| 107 | + <Input |
| 108 | + value={this.state.confirmPassword} |
| 109 | + name="confirmPassword" |
| 110 | + onChange={(e) => |
| 111 | + this.setState({ confirmPassword: e.target.value }) |
| 112 | + } |
| 113 | + placeholder="Password" |
| 114 | + type="password" |
| 115 | + margin="1em 0 1em 0" |
| 116 | + padding="0 1em 0 1em" |
| 117 | + error={error ? true : false} |
| 118 | + /> |
| 119 | + {this.state && this.state.error ? ( |
| 120 | + <Text align="center" color="#e30429"> |
| 121 | + {this.state.error} |
| 122 | + </Text> |
| 123 | + ) : ( |
| 124 | + <></> |
| 125 | + )} |
| 126 | + </Box> |
| 127 | + </Box> |
| 128 | + </Card> |
| 129 | + </div> |
| 130 | + </Box> |
| 131 | + <Box |
| 132 | + direction="column" |
| 133 | + width="800px" |
| 134 | + align="right" |
| 135 | + margin="0 auto 0 auto" |
| 136 | + > |
| 137 | + <ArrowButton |
| 138 | + label="Continue" |
| 139 | + type="button" |
| 140 | + onClick={() => this.encryptSecureMnemonicFile()} |
| 141 | + /> |
| 142 | + </Box> |
| 143 | + </Box> |
| 144 | + </Container> |
| 145 | + </> |
| 146 | + ); |
| 147 | + } |
| 148 | +} |
0 commit comments