Exemplo n.º 1
0
   void wallet_db::import_from_json( const path& filename )
   { try {
      FC_ASSERT( fc::exists( filename ) );
      FC_ASSERT( is_open() );

      auto records = fc::json::from_file<std::vector<generic_wallet_record>>( filename );
      for( const auto& record : records )
      {
          try
          {
              store_and_reload_generic_record( record );
              // Prevent hanging on large wallets
              fc::usleep( fc::milliseconds( 1 ) );
          }
          catch( const fc::canceled_exception& )
          {
              throw;
          }
          catch( const fc::exception& e )
          {
              elog( "Error loading wallet record:\n${r}\nReason: ${e}", ("e",e.to_detail_string())("r",record) );

              switch( wallet_record_type_enum( record.type ) )
              {
                  case master_key_record_type:
                  case key_record_type:
                      throw;
                  default:
                      break;
              }
          }
      }
   } FC_CAPTURE_AND_RETHROW( (filename) ) }
Exemplo n.º 2
0
   void wallet_db::repair_records( const fc::sha512& password )
   { try {
       FC_ASSERT( is_open() );

       vector<generic_wallet_record> records;
       for( auto iter = my->_records.begin(); iter.valid(); ++iter )
           records.push_back( iter.value() );

       // Repair key_data.account_address when possible
       uint32_t count = 0;
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == account_record_type )
               {
                   std::cout << "\rRepairing account record " << std::to_string( ++count ) << std::flush;
                   wallet_account_record account_record = record.as<wallet_account_record>();
                   store_account( account_record );
               }
           }
           catch( const fc::exception& )
           {
           }
       }

       // Repair key_data.public_key when I have the private key and remove if I don't have the private key
       count = 0;
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == key_record_type )
               {
                   std::cout << "\rRepairing key record     " << std::to_string( ++count ) << std::flush;
                   wallet_key_record key_record = record.as<wallet_key_record>();
                   const address key_address = key_record.get_address();
                   if( key_record.has_private_key() )
                   {
                       const private_key_type private_key = key_record.decrypt_private_key( password );
                       const public_key_type public_key = private_key.get_public_key();
                       if( key_record.public_key != public_key )
                       {
                           keys.erase( key_address );
                           btc_to_bts_address.erase( key_address );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 0  ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  0  ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 56 ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  56 ) ) );

                           key_record.public_key = public_key;
                           my->load_key_record( key_record );
                       }
                   }
                   else
                   {
                       keys.erase( key_address );
                       btc_to_bts_address.erase( key_address );
                       btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 0  ) ) );
                       btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  0  ) ) );
                       btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 56 ) ) );
                       btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  56 ) ) );
                       remove_item( key_record.wallet_record_index );
                       continue;
                   }
                   store_key( key_record );
               }
           }
           catch( const fc::exception& )
           {
           }
       }

       // Repair transaction_data.record_id
       count = 0;
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == transaction_record_type )
               {
                   std::cout << "\rRepairing transaction record     " << std::to_string( ++count ) << std::flush;
                   wallet_transaction_record transaction_record = record.as<wallet_transaction_record>();
                   if( transaction_record.trx.id() != signed_transaction().id()  )
                   {
                       const transaction_id_type record_id = transaction_record.trx.id();
                       if( transaction_record.record_id != record_id )
                       {
                           transactions.erase( transaction_record.record_id );
                           id_to_transaction_record_index.erase( transaction_record.record_id );

                           transaction_record.record_id = record_id;
                           my->load_transaction_record( transaction_record );
                       }
                   }
                   store_transaction( transaction_record );
               }
           }
           catch( const fc::exception& )
           {
           }
       }
       std::cout << "\rWallet records repaired.                                  " << std::flush << "\n";
   } FC_CAPTURE_AND_RETHROW() }
Exemplo n.º 3
0
   void wallet_db::repair_records( const fc::sha512& password )
   { try {
       FC_ASSERT( is_open() );

       vector<generic_wallet_record> records;
       for( auto iter = my->_records.begin(); iter.valid(); ++iter )
           records.push_back( iter.value() );

       // Repair account_data.is_my_account and key_data.account_address
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == account_record_type )
               {
                   wallet_account_record account_record = record.as<wallet_account_record>();
                   store_account( account_record );
               }
           }
           catch( ... )
           {
           }
       }

       // Repair key_data.public_key when I have the private key and
       // repair key_data.account_address and account_data.is_my_account
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == key_record_type )
               {
                   wallet_key_record key_record = record.as<wallet_key_record>();
                   if( key_record.has_private_key() )
                   {
                       const private_key_type private_key = key_record.decrypt_private_key( password );
                       const public_key_type public_key = private_key.get_public_key();
                       if( key_record.public_key != public_key )
                       {
                           const address key_address = key_record.get_address();
                           keys.erase( key_address );
                           btc_to_bts_address.erase( key_address );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 0  ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  0  ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, false, 56 ) ) );
                           btc_to_bts_address.erase( address( pts_address( key_record.public_key, true,  56 ) ) );

                           key_record.public_key = public_key;
                           my->load_key_record( key_record );
                       }
                   }
                   store_key( key_record );
               }
           }
           catch( ... )
           {
           }
       }

       // Repair transaction_data.record_id
       for( generic_wallet_record& record : records )
       {
           try
           {
               if( wallet_record_type_enum( record.type ) == transaction_record_type )
               {
                   wallet_transaction_record transaction_record = record.as<wallet_transaction_record>();
                   if( transaction_record.trx.id() != signed_transaction().id()  )
                   {
                       const transaction_id_type record_id = transaction_record.trx.permanent_id();
                       if( transaction_record.record_id != record_id )
                       {
                           transactions.erase( transaction_record.record_id );
                           id_to_transaction_record_index.erase( transaction_record.record_id );

                           transaction_record.record_id = record_id;
                           my->load_transaction_record( transaction_record );
                       }
                   }
                   store_transaction( transaction_record );
               }
           }
           catch( ... )
           {
           }
       }
   } FC_CAPTURE_AND_RETHROW() }