Skip to content

Commit

Permalink
fix: compressBackupFile throws
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Dec 19, 2019
1 parent d30ffc7 commit c077e7c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
11 changes: 1 addition & 10 deletions src/extension/options-page/Backup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,7 @@ export default function BackupDialog() {
Services.Welcome.createBackupFile({ download: false, onlyBackupWhoAmI: true })
.then(backupObj => {
setBackupObj(backupObj)
setQRText(
compressBackupFile(
backupObj,
backupObj.profiles.findIndex(y =>
currentIdentifier.equals(
Identifier.fromString(y.identifier).unwrap('Invalid identifier in backup file'),
),
),
),
)
setQRText(compressBackupFile(backupObj, currentIdentifier))
})
.catch(e => {
alert(e)
Expand Down
2 changes: 1 addition & 1 deletion src/extension/options-page/DashboardDialogs/Persona.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export function PersonaBackupDialog(props: PersonaBackupDialogProps) {
personas: [target],
}
setBase64Value(btoa(JSON.stringify(value)))
setCompressedQRString(compressBackupFile(value, 0))
setCompressedQRString(compressBackupFile(value))
})
}, [persona.identifier])

Expand Down
26 changes: 15 additions & 11 deletions src/utils/type-transform/BackupFileShortRepresentation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProfileIdentifier, Identifier, ECKeyIdentifier } from '../../database/type'
import { ProfileIdentifier, Identifier, ECKeyIdentifier, PersonaIdentifier } from '../../database/type'
import { compressSecp256k1Key, decompressSecp256k1Key } from './SECP256k1-Compression'
import { BackupJSONFileLatest } from './BackupFormat/JSON/latest'
import { ProfileRecord } from '../../database/Persona/Persona.db'
Expand All @@ -17,19 +17,23 @@ export type BackupJSONFileLatestShort = [
// BackupJSONFileLatest['grantedHostPermissions'].join(';'),
string,
]
export function compressBackupFile(file: BackupJSONFileLatest, index: number): string {
export function compressBackupFile(file: BackupJSONFileLatest, profileIdentifier?: ProfileIdentifier): string {
const { grantedHostPermissions, profiles, personas } = file
if (!personas[index ?? 0]) throw new Error('Empty backup file')
const { localKey, nickname, privateKey, identifier } = personas[index]
if (!privateKey) throw new Error('Invalid private key')
const id = Identifier.fromString(identifier, ProfileIdentifier).unwrap('Invalid identifier')
if (!profileIdentifier)
profileIdentifier = Identifier.fromString(profiles[0].identifier, ProfileIdentifier).unwrap('Cast error')
const profile = profiles.find(x => x.identifier === profileIdentifier!.toText())
if (!profile?.linkedPersona) throw new Error('Target profile/persona not found')
const persona = personas.find(x => x.identifier === profile.linkedPersona)
if (!persona?.privateKey) throw new Error('Target persona not found')
const { localKey, nickname, privateKey, linkedProfiles } = persona
return ([
'2',
id.network,
id.userId,
'1',
profileIdentifier.network,
profileIdentifier.userId,
nickname,
localKey?.k ||
profiles.filter(x => x.linkedPersona === identifier).filter(x => x.localKey)[0]?.localKey?.k ||
profiles.filter(x => x.linkedPersona === profileIdentifier!.toText()).filter(x => x.localKey)[0]?.localKey
?.k ||
'',
compressSecp256k1Key(privateKey, 'private'),
grantedHostPermissions.join(';'),
Expand All @@ -48,7 +52,7 @@ export function decompressBackupFile(short: string): BackupJSONFileLatest {
'🤔',
) as BackupJSONFileLatestShort

if (version !== '2') throw new Error(`QR Code cannot be shared between different version of Maskbook`)
if (version !== '1') throw new Error(`QR Code cannot be shared between different version of Maskbook`)

const localKeyJWK: JsonWebKey = {
alg: 'A256GCM',
Expand Down

0 comments on commit c077e7c

Please sign in to comment.