@@ -27,7 +27,8 @@ export class WalletRPC {
2727 password_hash : null ,
2828 balance : null ,
2929 unlocked_balance : null ,
30- bnsRecords : [ ]
30+ bnsRecords : [ ] ,
31+ view_only : false
3132 } ;
3233 this . isRPCSyncing = false ;
3334 this . dirs = null ;
@@ -303,6 +304,21 @@ export class WalletRPC {
303304 ) ;
304305 break ;
305306
307+ case "restore_wallet_with_keys" :
308+ // TODO: Decide if we want this for Beldex
309+ this . restoreWalletWithKeys (
310+ params . name ,
311+ params . password ,
312+ params . address ,
313+ params . viewkey ,
314+ params . spendkey ,
315+ params . refresh_type ,
316+ params . refresh_type == "date"
317+ ? params . refresh_start_date
318+ : params . refresh_start_height
319+ ) ;
320+ break ;
321+
306322 case "import_wallet" :
307323 this . importWallet ( params . name , params . password , params . path ) ;
308324 break ;
@@ -505,7 +521,7 @@ export class WalletRPC {
505521 address : response . result . per_subaddress [ 0 ] . address ,
506522 balance : response . result . balance ,
507523 unlocked_balance : response . result . unlocked_balance ,
508- view_only : false ,
524+ view_only : this . wallet_state . view_only ,
509525 load_balance : false
510526 }
511527 } ;
@@ -738,6 +754,73 @@ export class WalletRPC {
738754 } ) ;
739755 }
740756
757+ restoreWalletWithKeys (
758+ filename ,
759+ password ,
760+ address ,
761+ viewkey ,
762+ spendkey ,
763+ refresh_type ,
764+ refresh_start_timestamp_or_height
765+ ) {
766+ if ( refresh_type == "date" ) {
767+ // Convert timestamp to 00:00 and move back a day
768+ // Core code also moved back some amount of blocks
769+ let timestamp = refresh_start_timestamp_or_height ;
770+ timestamp = timestamp - ( timestamp % 86400000 ) - 86400000 ;
771+
772+ this . backend . daemon . timestampToHeight ( timestamp ) . then ( height => {
773+ if ( height === false ) {
774+ this . sendGateway ( "set_wallet_error" , {
775+ status : {
776+ code : - 1 ,
777+ i18n : "notification.errors.invalidRestoreDate"
778+ }
779+ } ) ;
780+ } else {
781+ this . restoreWalletWithKeys (
782+ filename ,
783+ password ,
784+ address ,
785+ viewkey ,
786+ spendkey ,
787+ "height" ,
788+ height
789+ ) ;
790+ }
791+ } ) ;
792+ return ;
793+ }
794+
795+ let restore_height = refresh_start_timestamp_or_height ;
796+
797+ if ( ! Number . isInteger ( restore_height ) ) {
798+ restore_height = 0 ;
799+ }
800+ this . sendRPC ( "generate_from_keys" , {
801+ filename,
802+ password,
803+ address,
804+ viewkey,
805+ spendkey,
806+ restore_height
807+ } ) . then ( data => {
808+ if ( data . hasOwnProperty ( "error" ) ) {
809+ this . sendGateway ( "set_wallet_error" , { status : data . error } ) ;
810+ return ;
811+ }
812+
813+ // store hash of the password so we can check against it later when requesting private keys, or for sending txs
814+ this . wallet_state . password_hash = crypto
815+ . pbkdf2Sync ( password , this . auth [ 2 ] , 1000 , 64 , "sha512" )
816+ . toString ( "hex" ) ;
817+ this . wallet_state . name = filename ;
818+ this . wallet_state . open = true ;
819+
820+ this . finalizeNewWallet ( filename ) ;
821+ } ) ;
822+ }
823+
741824 importWallet ( wallet_name , password , import_path ) {
742825 // Reset the status error
743826 this . sendGateway ( "reset_wallet_error" ) ;
@@ -851,6 +934,12 @@ export class WalletRPC {
851934 } ;
852935 for ( let n of data ) {
853936 if ( n . hasOwnProperty ( "error" ) || ! n . hasOwnProperty ( "result" ) ) {
937+ if ( n . params . key_type == "spend_key" ) {
938+ if ( n . error ?. code == - 29 ) {
939+ wallet . info . view_only = true ;
940+ this . wallet_state . view_only = true ;
941+ }
942+ }
854943 continue ;
855944 }
856945 if ( n . method == "get_address" ) {
@@ -865,6 +954,7 @@ export class WalletRPC {
865954 if ( n . params . key_type == "spend_key" ) {
866955 if ( / ^ 0 * $ / . test ( n . result . key ) ) {
867956 wallet . info . view_only = true ;
957+ this . wallet_state . view_only = true ;
868958 }
869959 }
870960 }
@@ -930,6 +1020,17 @@ export class WalletRPC {
9301020 // Check if we have a view only wallet by querying the spend key
9311021 this . sendRPC ( "query_key" , { key_type : "spend_key" } ) . then ( data => {
9321022 if ( data . hasOwnProperty ( "error" ) || ! data . hasOwnProperty ( "result" ) ) {
1023+ if ( data . params . key_type == "spend_key" ) {
1024+ if ( data . error ?. code == - 29 ) {
1025+ // wallet.info.view_only = true;
1026+ this . wallet_state . view_only = true ;
1027+ this . sendGateway ( "set_wallet_data" , {
1028+ info : {
1029+ view_only : true
1030+ }
1031+ } ) ;
1032+ }
1033+ }
9331034 return ;
9341035 }
9351036 if ( / ^ 0 * $ / . test ( data . result . key ) ) {
0 commit comments