@@ -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 } ;
@@ -709,9 +725,12 @@ export class WalletRPC {
709725 return ;
710726 }
711727
712- let refresh_start_height = refresh_start_timestamp_or_height ;
728+ let refresh_start_height = Number . parseInt (
729+ refresh_start_timestamp_or_height
730+ ) ;
713731
714- if ( ! Number . isInteger ( refresh_start_height ) ) {
732+ // if the height can't be parsed just start from block 0
733+ if ( ! refresh_start_height ) {
715734 refresh_start_height = 0 ;
716735 }
717736
@@ -738,6 +757,74 @@ export class WalletRPC {
738757 } ) ;
739758 }
740759
760+ restoreWalletWithKeys (
761+ filename ,
762+ password ,
763+ address ,
764+ viewkey ,
765+ spendkey ,
766+ refresh_type ,
767+ refresh_start_timestamp_or_height
768+ ) {
769+ if ( refresh_type == "date" ) {
770+ // Convert timestamp to 00:00 and move back a day
771+ // Core code also moved back some amount of blocks
772+ let timestamp = refresh_start_timestamp_or_height ;
773+ timestamp = timestamp - ( timestamp % 86400000 ) - 86400000 ;
774+
775+ this . backend . daemon . timestampToHeight ( timestamp ) . then ( height => {
776+ if ( height === false ) {
777+ this . sendGateway ( "set_wallet_error" , {
778+ status : {
779+ code : - 1 ,
780+ i18n : "notification.errors.invalidRestoreDate"
781+ }
782+ } ) ;
783+ } else {
784+ this . restoreWalletWithKeys (
785+ filename ,
786+ password ,
787+ address ,
788+ viewkey ,
789+ spendkey ,
790+ "height" ,
791+ height
792+ ) ;
793+ }
794+ } ) ;
795+ return ;
796+ }
797+
798+ let restore_height = Number . parseInt ( refresh_start_timestamp_or_height ) ;
799+
800+ // if the height can't be parsed just start from block 0
801+ if ( ! restore_height ) {
802+ restore_height = 0 ;
803+ }
804+ this . sendRPC ( "generate_from_keys" , {
805+ filename,
806+ password,
807+ address,
808+ viewkey,
809+ spendkey,
810+ restore_height
811+ } ) . then ( data => {
812+ if ( data . hasOwnProperty ( "error" ) ) {
813+ this . sendGateway ( "set_wallet_error" , { status : data . error } ) ;
814+ return ;
815+ }
816+
817+ // store hash of the password so we can check against it later when requesting private keys, or for sending txs
818+ this . wallet_state . password_hash = crypto
819+ . pbkdf2Sync ( password , this . auth [ 2 ] , 1000 , 64 , "sha512" )
820+ . toString ( "hex" ) ;
821+ this . wallet_state . name = filename ;
822+ this . wallet_state . open = true ;
823+
824+ this . finalizeNewWallet ( filename ) ;
825+ } ) ;
826+ }
827+
741828 importWallet ( wallet_name , password , import_path ) {
742829 // Reset the status error
743830 this . sendGateway ( "reset_wallet_error" ) ;
@@ -851,6 +938,12 @@ export class WalletRPC {
851938 } ;
852939 for ( let n of data ) {
853940 if ( n . hasOwnProperty ( "error" ) || ! n . hasOwnProperty ( "result" ) ) {
941+ if ( n . params . key_type == "spend_key" ) {
942+ if ( n . error ?. code == - 29 ) {
943+ wallet . info . view_only = true ;
944+ this . wallet_state . view_only = true ;
945+ }
946+ }
854947 continue ;
855948 }
856949 if ( n . method == "get_address" ) {
@@ -865,6 +958,7 @@ export class WalletRPC {
865958 if ( n . params . key_type == "spend_key" ) {
866959 if ( / ^ 0 * $ / . test ( n . result . key ) ) {
867960 wallet . info . view_only = true ;
961+ this . wallet_state . view_only = true ;
868962 }
869963 }
870964 }
@@ -930,6 +1024,17 @@ export class WalletRPC {
9301024 // Check if we have a view only wallet by querying the spend key
9311025 this . sendRPC ( "query_key" , { key_type : "spend_key" } ) . then ( data => {
9321026 if ( data . hasOwnProperty ( "error" ) || ! data . hasOwnProperty ( "result" ) ) {
1027+ if ( data . params . key_type == "spend_key" ) {
1028+ if ( data . error ?. code == - 29 ) {
1029+ // wallet.info.view_only = true;
1030+ this . wallet_state . view_only = true ;
1031+ this . sendGateway ( "set_wallet_data" , {
1032+ info : {
1033+ view_only : true
1034+ }
1035+ } ) ;
1036+ }
1037+ }
9331038 return ;
9341039 }
9351040 if ( / ^ 0 * $ / . test ( data . result . key ) ) {
@@ -2475,7 +2580,6 @@ export class WalletRPC {
24752580 }
24762581
24772582 deregisterImages ( password ) {
2478- console . log ( "deregister_images.....wallet-rpc" ) ;
24792583 crypto . pbkdf2 (
24802584 password ,
24812585 this . auth [ 2 ] ,
0 commit comments