-
Notifications
You must be signed in to change notification settings - Fork 237
feat: add backup page #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
d2e39c1
feat: add backup page
gamalielhere 1260f74
Merge branch 'develop' into feat/backup-detected
kvhnuke e81cd7b
devop: backups are functioning
kvhnuke 40d095c
fix: minor ui cleanup
gamalielhere 274981e
devop: cleanup
kvhnuke 00e4e72
devop: backup settings
kvhnuke e1c02d1
devop: backup functionality complete
kvhnuke 0a327c1
devop: update pakages
kvhnuke 3dbaa9c
devop: switch to new api
kvhnuke 75ed672
feat: clean up table, add loading state, add no backup state
gamalielhere 75a393f
devop: new uuid for each restore
kvhnuke 7888dbc
feat: updates
gamalielhere 3cf90f1
Merge branch 'feat/backup-detected' of github.com:enkryptcom/enKrypt …
gamalielhere 73f87c2
devop: generate names for uuids
kvhnuke d11eca8
devop: clean up and loading state
gamalielhere 2171ae8
fix: conflicts
gamalielhere 10cd0ca
fix: change titles
gamalielhere c315046
fix: remove underscore
gamalielhere 5abdb83
Merge branch 'devop/upgrade-packages' of github.com:enkryptcom/enKryp…
gamalielhere 829098a
feat: cleanup, enable identicon
gamalielhere 7748adb
feat: add delete confirmation page for backups
gamalielhere eff1a45
chore: margins
gamalielhere 3a8ab1d
devop: switch url
kvhnuke c47a88f
Merge branch 'feat/backup-detected' of github.com:enkryptcom/enKrypt …
kvhnuke ebe36dc
feat: loading state for backup page
gamalielhere 27086e8
Merge branch 'feat/backup-detected' of github.com:enkryptcom/enKrypt …
gamalielhere ff20eec
chore: copy changes and margin
gamalielhere c811fbd
fix: spacing
gamalielhere fa715f0
fix: backup on new wallets
kvhnuke 213808c
fix: close delete modal after deleting
gamalielhere 3f81643
Merge branch 'feat/backup-detected' of github.com:enkryptcom/enKrypt …
gamalielhere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| const BACKUP_URL = 'https://backupstore.enkrypt.com/'; | ||
| const HEADERS = { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| }; | ||
| export { BACKUP_URL, HEADERS }; |
This file contains hidden or 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,363 @@ | ||
| import BrowserStorage from '../common/browser-storage'; | ||
| import { InternalStorageNamespace } from '@/types/provider'; | ||
| import { | ||
| BackupData, | ||
| BackupResponseType, | ||
| BackupType, | ||
| IState, | ||
| ListBackupType, | ||
| StorageKeys, | ||
| } from './types'; | ||
| import PublicKeyRing from '../keyring/public-keyring'; | ||
| import sendUsingInternalMessengers from '../messenger/internal-messenger'; | ||
| import { InternalMethods } from '@/types/messenger'; | ||
| import EthNetwork from '@/providers/ethereum/networks/eth'; | ||
| import { | ||
| bufferToHex, | ||
| hexToBuffer, | ||
| NACL_VERSION, | ||
| naclEncrypt, | ||
| utf8ToHex, | ||
| } from '@enkryptcom/utils'; | ||
| import { hashPersonalMessage } from '@ethereumjs/util'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
| import { BACKUP_URL, HEADERS } from './configs'; | ||
| import { EnkryptAccount, SignerType, WalletType } from '@enkryptcom/types'; | ||
| import KeyRingBase from '../keyring/keyring'; | ||
|
|
||
| class BackupState { | ||
| private storage: BrowserStorage; | ||
|
|
||
| constructor() { | ||
| this.storage = new BrowserStorage(InternalStorageNamespace.backupState); | ||
| } | ||
|
|
||
| async #getSignature( | ||
| msgHash: string, | ||
| mainWallet: EnkryptAccount, | ||
| ): Promise<string> { | ||
| return sendUsingInternalMessengers({ | ||
| method: InternalMethods.sign, | ||
| params: [msgHash, mainWallet], | ||
| }).then(res => { | ||
| if (res.error) { | ||
| console.error(res); | ||
| return null; | ||
| } else { | ||
| return JSON.parse(res.result as string); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| async getMainWallet(): Promise<EnkryptAccount> { | ||
| const pkr = new PublicKeyRing(); | ||
| const allAccounts = await pkr.getAccounts(); | ||
| const mainWallet = allAccounts.find( | ||
| acc => | ||
| acc.walletType === WalletType.mnemonic && | ||
| acc.pathIndex === 0 && | ||
| acc.signerType === EthNetwork.signer[0] && | ||
| acc.basePath === EthNetwork.basePath, | ||
| ); | ||
| if (!mainWallet) { | ||
| throw new Error('No main wallet found'); | ||
| } | ||
| return mainWallet; | ||
| } | ||
|
|
||
| getListBackupMsgHash(pubkey: string): string { | ||
| const now = new Date(); | ||
| const messageToSign = `${pubkey}-GET-BACKUPS-${(now.getUTCMonth() + 1).toString().padStart(2, '0')}-${now.getUTCDate().toString().padStart(2, '0')}-${now.getUTCFullYear()}`; | ||
| return bufferToHex( | ||
| hashPersonalMessage(hexToBuffer(utf8ToHex(messageToSign))), | ||
| ); | ||
| } | ||
|
|
||
| async listBackups(options?: { | ||
| signature: string; | ||
| pubkey: string; | ||
| }): Promise<ListBackupType[]> { | ||
| let signature: string = ''; | ||
| let pubkey: string = ''; | ||
| if (options) { | ||
| signature = options.signature; | ||
| pubkey = options.pubkey; | ||
| } else { | ||
| const mainWallet = await this.getMainWallet(); | ||
| pubkey = mainWallet.publicKey; | ||
| const msgHash = this.getListBackupMsgHash(mainWallet.publicKey); | ||
| signature = await this.#getSignature(msgHash, mainWallet); | ||
| } | ||
| if (!signature) { | ||
| console.error('No signature found'); | ||
| return []; | ||
| } | ||
| const rawResponse = await fetch( | ||
| `${BACKUP_URL}backups/${pubkey}?signature=${signature}`, | ||
| { | ||
| method: 'GET', | ||
| headers: HEADERS, | ||
| }, | ||
| ); | ||
| const content = (await rawResponse.json()) as { | ||
| backups: ListBackupType[]; | ||
| }; | ||
| return content.backups; | ||
| } | ||
|
|
||
| async getBackup(userId: string): Promise<BackupType | null> { | ||
| const mainWallet = await this.getMainWallet(); | ||
| const now = new Date(); | ||
| const messageToSign = `${userId}-GET-BACKUP-${(now.getUTCMonth() + 1).toString().padStart(2, '0')}-${now.getUTCDate().toString().padStart(2, '0')}-${now.getUTCFullYear()}`; | ||
| const msgHash = bufferToHex( | ||
| hashPersonalMessage(hexToBuffer(utf8ToHex(messageToSign))), | ||
| ); | ||
| const signature = await this.#getSignature(msgHash, mainWallet); | ||
| const rawResponse = await fetch( | ||
| `${BACKUP_URL}backups/${mainWallet.publicKey}/users/${userId}?signature=${signature}`, | ||
| { | ||
| method: 'GET', | ||
| headers: HEADERS, | ||
| }, | ||
| ); | ||
| const content = (await rawResponse.json()) as BackupResponseType; | ||
| return content.backup; | ||
| } | ||
|
|
||
| async deleteBackup(userId: string): Promise<boolean> { | ||
| const mainWallet = await this.getMainWallet(); | ||
| const now = new Date(); | ||
| const messageToSign = `${userId}-DELETE-BACKUP-${(now.getUTCMonth() + 1).toString().padStart(2, '0')}-${now.getUTCDate().toString().padStart(2, '0')}-${now.getUTCFullYear()}`; | ||
| const msgHash = bufferToHex( | ||
| hashPersonalMessage(hexToBuffer(utf8ToHex(messageToSign))), | ||
| ); | ||
| const signature = await this.#getSignature(msgHash, mainWallet); | ||
| if (!signature) { | ||
| console.error('No signature found'); | ||
| return false; | ||
| } | ||
| return fetch( | ||
| `${BACKUP_URL}backups/${mainWallet.publicKey}/users/${userId}?signature=${signature}`, | ||
| { | ||
| method: 'DELETE', | ||
| headers: HEADERS, | ||
| }, | ||
| ) | ||
| .then(res => res.json()) | ||
| .then(content => { | ||
| if ((content as { message: string }).message === 'Ok') { | ||
| return true; | ||
| } | ||
| console.error(content); | ||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| async restoreBackup(userId: string, keyringPassword: string): Promise<void> { | ||
| const mainWallet = await this.getMainWallet(); | ||
| await sendUsingInternalMessengers({ | ||
| method: InternalMethods.unlock, | ||
| params: [keyringPassword, false], | ||
| }); | ||
| const backup = await this.getBackup(userId); | ||
| if (!backup) { | ||
| console.error('No backup found'); | ||
| return; | ||
| } | ||
| await sendUsingInternalMessengers({ | ||
| method: InternalMethods.ethereumDecrypt, | ||
| params: [backup.payload, mainWallet], | ||
| }).then(async res => { | ||
| if (res.error) { | ||
| console.error(res); | ||
| return null; | ||
| } else { | ||
| const kr = new KeyRingBase(); | ||
| await kr.unlock(keyringPassword); | ||
| const existingAccounts = await kr.getKeysArray(); | ||
| const decryptedBackup: BackupData = JSON.parse( | ||
| JSON.parse(res.result as string), | ||
| ); | ||
| const highestPathIndex: Record<string, number> = {}; | ||
| decryptedBackup.accounts.forEach(acc => { | ||
| const id = `${acc.basePath}###${acc.signerType}`; | ||
| const idx = acc.pathIndex; | ||
| if (!highestPathIndex[id] || highestPathIndex[id] < idx) { | ||
| highestPathIndex[id] = idx; | ||
| } | ||
| }); | ||
| const getAccountByIndex = ( | ||
| accounts: Omit<EnkryptAccount, 'address' | 'publicKey'>[], | ||
| basePath: string, | ||
| signerType: SignerType, | ||
| idx: number, | ||
| ): EnkryptAccount | null => { | ||
| for (const acc of accounts) { | ||
| if ( | ||
| acc.basePath === basePath && | ||
| acc.pathIndex === idx && | ||
| acc.signerType === signerType | ||
| ) { | ||
| return acc as EnkryptAccount; | ||
| } | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| for (const key of Object.keys(highestPathIndex)) { | ||
| const [basePath, signerType] = key.split('###'); | ||
| for (let i = 0; i <= highestPathIndex[key]; i++) { | ||
| const newAccount = getAccountByIndex( | ||
| decryptedBackup.accounts, | ||
| basePath, | ||
| signerType as SignerType, | ||
| i, | ||
| ); | ||
| const existingAccount = getAccountByIndex( | ||
| existingAccounts, | ||
| basePath, | ||
| signerType as SignerType, | ||
| i, | ||
| ); | ||
| if (existingAccount && newAccount) { | ||
| await kr.renameAccount(existingAccount.address, newAccount.name); | ||
| continue; | ||
gamalielhere marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else if (newAccount) { | ||
| await kr.saveNewAccount({ | ||
| basePath: newAccount.basePath, | ||
| name: newAccount.name, | ||
| signerType: newAccount.signerType, | ||
| walletType: newAccount.walletType, | ||
| }); | ||
| } else if (!newAccount) { | ||
| await kr.saveNewAccount({ | ||
| basePath: basePath, | ||
| name: `New Account from backup ${i}`, | ||
| signerType: signerType as SignerType, | ||
| walletType: WalletType.mnemonic, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
gamalielhere marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async backup(firstTime: boolean): Promise<boolean> { | ||
| const state = await this.getState(); | ||
| if (firstTime && state.lastBackupTime !== 0) { | ||
| return true; | ||
| } | ||
| if (!state.enabled) { | ||
| return true; | ||
| } | ||
| const pkr = new PublicKeyRing(); | ||
| const allAccounts = await pkr.getAccounts(); | ||
| const mainWallet = await this.getMainWallet(); | ||
| const backupData: BackupData = { | ||
| accounts: allAccounts | ||
| .filter( | ||
| acc => !acc.isTestWallet && acc.walletType !== WalletType.privkey, | ||
| ) | ||
| .map(acc => { | ||
| return { | ||
| basePath: acc.basePath, | ||
| pathIndex: acc.pathIndex, | ||
| name: acc.name, | ||
| signerType: acc.signerType, | ||
| walletType: acc.walletType, | ||
| isHardware: acc.isHardware, | ||
| HWOptions: acc.HWOptions, | ||
| }; | ||
| }), | ||
| uuid: state.userId, | ||
| }; | ||
| const encryptPubKey = await sendUsingInternalMessengers({ | ||
| method: InternalMethods.getEthereumEncryptionPublicKey, | ||
| params: [mainWallet], | ||
| }).then(res => { | ||
| if (res.error) { | ||
| console.error(res); | ||
| return null; | ||
| } else { | ||
| return JSON.parse(res.result as string); | ||
| } | ||
| }); | ||
| if (!encryptPubKey) { | ||
| console.error('No encrypt public key found'); | ||
| return false; | ||
| } | ||
| const encryptedStr = naclEncrypt({ | ||
| publicKey: encryptPubKey, | ||
| data: JSON.stringify(backupData), | ||
| version: NACL_VERSION, | ||
| }); | ||
| const msgHash = bufferToHex(hashPersonalMessage(hexToBuffer(encryptedStr))); | ||
| return this.#getSignature(msgHash, mainWallet).then(async signature => { | ||
| const rawResponse = await fetch( | ||
| `${BACKUP_URL}backups/${mainWallet.publicKey}/users/${state.userId}?signature=${signature}`, | ||
| { | ||
| method: 'POST', | ||
| headers: HEADERS, | ||
| body: JSON.stringify({ | ||
| payload: encryptedStr, | ||
| }), | ||
| }, | ||
| ); | ||
| const content = (await rawResponse.json()) as { message: string }; | ||
| if (content.message === 'Ok') { | ||
| await this.setState({ | ||
| lastBackupTime: new Date().getTime(), | ||
| userId: state.userId, | ||
| enabled: state.enabled, | ||
| }); | ||
| return true; | ||
| } | ||
| console.error(content); | ||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| async setState(state: IState): Promise<void> { | ||
| return this.storage.set(StorageKeys.backupInfo, state); | ||
| } | ||
|
|
||
| async getState(): Promise<IState> { | ||
| const state = await this.storage.get(StorageKeys.backupInfo); | ||
| if (!state) { | ||
| const newState: IState = { | ||
| lastBackupTime: 0, | ||
| userId: uuidv4(), | ||
| enabled: true, | ||
| }; | ||
| await this.setState(newState); | ||
| return newState; | ||
| } | ||
| return state; | ||
| } | ||
|
|
||
| async getLastUpdatedTime(): Promise<Date> { | ||
| const state: IState = await this.getState(); | ||
| return new Date(state.lastBackupTime); | ||
| } | ||
|
|
||
| async getUserId(): Promise<string> { | ||
| const state: IState = await this.getState(); | ||
| return state.userId; | ||
| } | ||
|
|
||
| async disableBackups(): Promise<void> { | ||
| const state: IState = await this.getState(); | ||
| await this.setState({ ...state, enabled: false }); | ||
| } | ||
| async enableBackups(): Promise<void> { | ||
| const state: IState = await this.getState(); | ||
| await this.setState({ ...state, enabled: true }); | ||
| } | ||
| async isBackupEnabled(): Promise<boolean> { | ||
| const state: IState = await this.getState(); | ||
| return state.enabled; | ||
| } | ||
| } | ||
|
|
||
| export default BackupState; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.