@@ -408,6 +408,19 @@ class Blink extends React.Component {
408
408
}
409
409
}
410
410
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
+ }
411
424
412
425
registrationStateChanged ( oldState , newState , data ) {
413
426
DEBUG ( 'Registration state changed! ' + newState ) ;
@@ -457,36 +470,29 @@ class Blink extends React.Component {
457
470
}
458
471
this . state . account . checkIfKeyExists ( ( fetchedPublicKey ) => {
459
472
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)
476
494
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 ) ;
486
495
} ) ;
487
- this . enableMessaging ( ) ;
488
- this . loadMessages ( ) ;
489
- }
490
496
} ) ;
491
497
} ) . catch ( ( ) => {
492
498
DEBUG ( 'No keys found in storage' ) ;
@@ -499,14 +505,7 @@ class Blink extends React.Component {
499
505
this . state . account . generatePGPKeys ( ( result ) => {
500
506
storage . set ( `pgpKeys-${ this . state . accountId } ` , result ) ;
501
507
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 ) ;
510
509
} ) ;
511
510
} ) ;
512
511
} else {
@@ -516,8 +515,9 @@ class Blink extends React.Component {
516
515
this . loadMessages ( ) ;
517
516
}
518
517
} else {
519
- this . setState ( { showNewDeviceModal : true } ) ;
520
518
// 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 } ) ;
521
521
}
522
522
} ) ;
523
523
} )
0 commit comments