Skip to content

Commit 3f4bbe4

Browse files
committed
Refactored PGP public key comparison
1 parent f229a7c commit 3f4bbe4

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

src/app/app.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,19 @@ class Blink extends React.Component {
408408
}
409409
}
410410

411+
enableEncryption(publicKey, privateKey, addKeys) {
412+
if (addKeys) {
413+
this.state.account.addPGPKeys({ publicKey, privateKey });
414+
}
415+
keyStorage.getAll().then(key =>
416+
this.state.account.pgp.addPublicPGPKeys(key)
417+
);
418+
this.state.account.pgp.on('publicKeyAdded', (key) => {
419+
keyStorage.add(key);
420+
});
421+
this.enableMessaging();
422+
this.loadMessages();
423+
}
411424

412425
registrationStateChanged(oldState, newState, data) {
413426
DEBUG('Registration state changed! ' + newState);
@@ -457,36 +470,29 @@ class Blink extends React.Component {
457470
}
458471
this.state.account.checkIfKeyExists((fetchedPublicKey) => {
459472
fetchedPublicKey = fetchedPublicKey !== null ? fetchedPublicKey.trim() : null;
460-
if (fetchedPublicKey !== publicKey.trim()) {
461-
if (fetchedPublicKey == null) {
462-
// We have a key, but the server has not
463-
this.setState({ loading: null, transmitKeys: true })
464-
465-
this.state.account.addPGPKeys({ publicKey, privateKey });
466-
keyStorage.getAll().then(key =>
467-
this.state.account.pgp.addPublicPGPKeys(key)
468-
);
469-
this.state.account.pgp.on('publicKeyAdded', (key) => {
470-
keyStorage.add(key);
471-
});
472-
this.enableMessaging();
473-
this.loadMessages();
474-
} else {
475-
// We have a key, but the server has a different key
473+
sylkrtc.utils.comparePGPKeys(fetchedPublicKey, publicKey)
474+
.then(match => {
475+
if (match) {
476+
// Our key and the key on the server match
477+
DEBUG('Our key and the key on the server match');
478+
this.enableEncryption(publicKey, privateKey, true);
479+
} else {
480+
if (fetchedPublicKey == null) {
481+
// We have a key, but the server has not
482+
DEBUG('We have a key, but the server has not');
483+
this.setState({ loading: null, transmitKeys: true })
484+
this.enableEncryption(publicKey, privateKey, true);
485+
} else {
486+
// We have a key, but the server has a different key
487+
DEBUG('We have a key, but the server has a different key');
488+
this.setState({ showEncryptionModal: true, export: false });
489+
}
490+
}
491+
})
492+
.catch(() => {
493+
// Handle errors (e.g., failed comparison)
476494
this.setState({ showEncryptionModal: true, export: false });
477-
}
478-
} else {
479-
// Our key and the key on the server matches
480-
this.state.account.addPGPKeys({ publicKey, privateKey });
481-
keyStorage.getAll().then(key =>
482-
this.state.account.pgp.addPublicPGPKeys(key)
483-
);
484-
this.state.account.pgp.on('publicKeyAdded', (key) => {
485-
keyStorage.add(key);
486495
});
487-
this.enableMessaging();
488-
this.loadMessages();
489-
}
490496
});
491497
}).catch(() => {
492498
DEBUG('No keys found in storage');
@@ -499,14 +505,7 @@ class Blink extends React.Component {
499505
this.state.account.generatePGPKeys((result) => {
500506
storage.set(`pgpKeys-${this.state.accountId}`, result);
501507
this.setState({ loading: null, transmitKeys: true });
502-
keyStorage.getAll().then(key =>
503-
this.state.account.pgp.addPublicPGPKeys(key)
504-
);
505-
this.state.account.pgp.on('publicKeyAdded', (key) => {
506-
keyStorage.add(key);
507-
});
508-
this.enableMessaging();
509-
this.loadMessages();
508+
this.enableEncryption(result.publicKey, result.privateKey, false);
510509
});
511510
});
512511
} else {
@@ -516,8 +515,9 @@ class Blink extends React.Component {
516515
this.loadMessages();
517516
}
518517
} else {
519-
this.setState({ showNewDeviceModal: true });
520518
// There is a key on the server and we have none
519+
DEBUG('There is a key on the server and we have none');
520+
this.setState({ showNewDeviceModal: true });
521521
}
522522
});
523523
})

0 commit comments

Comments
 (0)