Example #1
0
   void wallet_db::store_key( const key_data& key )
   { try {
       FC_ASSERT( is_open() );
       FC_ASSERT( key.public_key != public_key_type() );

       owallet_key_record key_record = lookup_key( key.get_address() );
       if( !key_record.valid() )
           key_record = wallet_key_record();

       key_data& temp = *key_record;
       temp = key;

       store_and_reload_record( *key_record, true );

       if( key_record->has_private_key() )
       {
           owallet_account_record account_record = lookup_account( key.public_key );
           if( !account_record.valid() )
               account_record = lookup_account( key.account_address );

           if( account_record.valid() )
           {
               if( key_record->account_address != account_record->owner_address() )
               {
                   key_record->account_address = account_record->owner_address();
                   store_and_reload_record( *key_record, true );
               }
           }
       }
   } FC_CAPTURE_AND_RETHROW( (key) ) }
Example #2
0
   public_key_type wallet_db::generate_new_account( const fc::sha512& password, const string& account_name,
                                                    const private_key_type& owner_private_key )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record account_record = lookup_account( account_name );
       FC_ASSERT( !account_record.valid(), "Wallet already contains an account with that name!" );

       const public_key_type owner_public_key = owner_private_key.get_public_key();
       const address owner_address = address( owner_public_key );

       const private_key_type active_private_key = get_account_child_key( owner_private_key, 0 );
       const public_key_type active_public_key = active_private_key.get_public_key();
       const address active_address = address( active_public_key );

       account_record = lookup_account( owner_address );
       FC_ASSERT( !account_record.valid(), "Wallet already contains an account with that key!" );

       account_record = lookup_account( active_address );
       FC_ASSERT( !account_record.valid(), "Wallet already contains an account with the new derived active key!" );

       owallet_key_record key_record = lookup_key( owner_address );
       FC_ASSERT( !key_record.valid() || !key_record->has_private_key(), "Wallet already contains that key!" );

       key_record = lookup_key( active_address );
       FC_ASSERT( !key_record.valid() || !key_record->has_private_key(), "Wallet already contains the new derived active key!" );

       key_data active_key;
       active_key.account_address = owner_address;
       active_key.public_key = active_public_key;
       active_key.encrypt_private_key( password, active_private_key );

       key_data owner_key;
       owner_key.account_address = owner_address;
       owner_key.public_key = owner_public_key;
       owner_key.encrypt_private_key( password, owner_private_key );

       account_data account;
       account.name = account_name;
       account.owner_key = owner_public_key;
       account.set_active_key( blockchain::now(), active_public_key );
       account.last_update = blockchain::now();

       store_key( active_key );
       store_key( owner_key );
       store_account( account );

       return owner_public_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
Example #3
0
   public_key_type wallet_db::generate_new_account( const fc::sha512& password, const string& account_name, const variant& private_data )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record account_record = lookup_account( account_name );
       FC_ASSERT( !account_record.valid(), "Wallet already contains an account with that name!" );

       uint32_t key_index = get_last_wallet_child_key_index();
       private_key_type account_private_key;
       public_key_type account_public_key;
       address account_address;
       while( true )
       {
           ++key_index;
           FC_ASSERT( key_index != 0, "Overflow!" );

           account_private_key = get_wallet_child_key( password, key_index );
           account_public_key = account_private_key.get_public_key();
           account_address = address( account_public_key );

           account_record = lookup_account( account_address );
           if( account_record.valid() ) continue;

           owallet_key_record key_record = lookup_key( account_address );
           if( key_record.valid() && key_record->has_private_key() ) continue;

           break;
       }

       key_data key;
       key.account_address = account_address;
       key.public_key = account_public_key;
       key.encrypt_private_key( password, account_private_key );
       key.gen_seq_number = key_index;

       account_data account;
       account.name = account_name;
       account.owner_key = account_public_key;
       account.set_active_key( blockchain::now(), account_public_key );
       account.private_data = private_data;
       account.is_my_account = true;

       set_last_wallet_child_key_index( key_index );
       store_key( key );
       store_account( account );

       return account_public_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
Example #4
0
   void wallet_db::store_account( const account_data& account )
   { try {
       FC_ASSERT( is_open() );
       FC_ASSERT( account.name != string() );
       FC_ASSERT( account.owner_key != public_key_type() );

       owallet_account_record account_record = lookup_account( account.owner_address() );
       if( !account_record.valid() )
           account_record = wallet_account_record();

       account_data& temp = *account_record;
       temp = account;

       store_and_reload_record( *account_record );

       set<public_key_type> account_public_keys;
       account_public_keys.insert( account_record->owner_key );
       for( const auto& active_key_item : account_record->active_key_history )
       {
           const public_key_type& active_key = active_key_item.second;
           if( active_key == public_key_type() ) continue;
           account_public_keys.insert( active_key );
       }
       if( account.is_delegate() )
       {
           for( const auto& item : account.delegate_info->signing_key_history )
           {
               const public_key_type& signing_key = item.second;
               if( signing_key == public_key_type() ) continue;
               account_public_keys.insert( signing_key );
           }
       }

       for( const public_key_type& account_public_key : account_public_keys )
       {
           const address account_address = address( account_public_key );
           owallet_key_record key_record = lookup_key( account_address );
           if( !key_record.valid() )
           {
               key_data key;
               key.account_address = account_address;
               key.public_key = account_public_key;
               store_key( key );
           }
           else if( key_record->has_private_key() )
           {
               if( !account_record->is_my_account )
               {
                   account_record->is_my_account = true;
                   store_and_reload_record( *account_record );
               }

               if( key_record->account_address != account_record->owner_address() )
               {
                   key_record->account_address = account_record->owner_address();
                   store_key( *key_record );
               }
           }
       }
   } FC_CAPTURE_AND_RETHROW( (account) ) }
Example #5
0
   void wallet_db::remove_contact_account( const string& account_name )
   {
       const owallet_account_record account_record = lookup_account( account_name );
       FC_ASSERT( account_record.valid() );
       FC_ASSERT( !account_record->is_my_account );

       const int32_t& record_index = account_record->wallet_record_index;

       accounts.erase( record_index );

       address_to_account_wallet_record_index.erase( address( account_record->owner_key ) );
       for( const auto& item : account_record->active_key_history )
       {
           const public_key_type& active_key = item.second;
           address_to_account_wallet_record_index.erase( address( active_key ) );
       }
       if( account_record->is_delegate() )
       {
           for( const auto& item : account_record->delegate_info->signing_key_history )
           {
               const public_key_type& signing_key = item.second;
               address_to_account_wallet_record_index.erase( address( signing_key ) );
           }
       }

       name_to_account_wallet_record_index.erase( account_record->name );
       account_id_to_wallet_record_index.erase( account_record->id );

       // TODO: Remove key records and indexes
   }
Example #6
0
   void WalletDb::remove_contact_account( const string& account_name )
   {
       const oWalletAccountEntry account_entry = lookup_account( account_name );
       FC_ASSERT( account_entry.valid() );
       FC_ASSERT( !account_entry->is_my_account );

       const int32_t& entry_index = account_entry->wallet_entry_index;

       accounts.erase( entry_index );

       address_to_account_wallet_entry_index.erase( Address( account_entry->owner_key ) );
       for( const auto& item : account_entry->active_key_history )
       {
           const PublicKeyType& active_key = item.second;
           address_to_account_wallet_entry_index.erase( Address( active_key ) );
       }
       if( account_entry->is_delegate() )
       {
           for( const auto& item : account_entry->delegate_info->signing_key_history )
           {
               const PublicKeyType& signing_key = item.second;
               address_to_account_wallet_entry_index.erase( Address( signing_key ) );
           }
       }

       name_to_account_wallet_entry_index.erase( account_entry->name );
       account_id_to_wallet_entry_index.erase( account_entry->id );

       // TODO: Remove key entrys and indexes
   }
Example #7
0
   void WalletDb::store_account( const AccountData& account )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");
       FC_ASSERT( account.name != string() );
       FC_ASSERT( account.owner_key != PublicKeyType() );

       oWalletAccountEntry account_entry = lookup_account( account.owner_address() );
       if( !account_entry.valid() )
           account_entry = WalletAccountEntry();

       AccountData& temp = *account_entry;
       temp = account;

       store_and_reload_entry( *account_entry );

       set<PublicKeyType> account_public_keys;
       account_public_keys.insert( account_entry->owner_key );
       for( const auto& active_key_item : account_entry->active_key_history )
       {
           const PublicKeyType& active_key = active_key_item.second;
           if( active_key == PublicKeyType() ) continue;
           account_public_keys.insert( active_key );
       }
       if( account.is_delegate() )
       {
           for( const auto& item : account.delegate_info->signing_key_history )
           {
               const PublicKeyType& signing_key = item.second;
               if( signing_key == PublicKeyType() ) continue;
               account_public_keys.insert( signing_key );
           }
       }

       for( const PublicKeyType& account_public_key : account_public_keys )
       {
           const Address account_address = Address( account_public_key );
           oWalletKeyEntry key_entry = lookup_key( account_address );
           if( !key_entry.valid() )
           {
               KeyData key;
               key.account_address = account_address;
               key.public_key = account_public_key;
               store_key( key );
           }
           else if( key_entry->has_private_key() )
           {
               if( !account_entry->is_my_account )
               {
                   account_entry->is_my_account = true;
                   store_and_reload_entry( *account_entry );
               }

               if( key_entry->account_address != account_entry->owner_address() )
               {
                   key_entry->account_address = account_entry->owner_address();
                   store_key( *key_entry );
               }
           }
       }
   } FC_CAPTURE_AND_RETHROW( (account) ) }
Example #8
0
   void WalletDb::store_account( const blockchain::AccountEntry& blockchain_account_entry )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");

       oWalletAccountEntry account_entry = lookup_account( blockchain_account_entry.owner_address() );
       if( !account_entry.valid() )
           account_entry = WalletAccountEntry();

       blockchain::AccountEntry& temp = *account_entry;
       temp = blockchain_account_entry;

       store_account( *account_entry );
   } FC_CAPTURE_AND_RETHROW( (blockchain_account_entry) ) }
Example #9
0
   void wallet_db::store_account( const blockchain::account_record& blockchain_account_record )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record account_record = lookup_account( blockchain_account_record.owner_address() );
       if( !account_record.valid() )
           account_record = wallet_account_record();

       blockchain::account_record& temp = *account_record;
       temp = blockchain_account_record;

       store_account( *account_record );
   } FC_CAPTURE_AND_RETHROW( (blockchain_account_record) ) }
Example #10
0
   void wallet_db::store_key( const key_data& key_to_store )
   {
      auto key_itr = keys.find( key_to_store.get_address() );
      if( key_itr != keys.end() )
      {
         key_data& old_data = key_itr->second;
         old_data = key_to_store;

         if( key_to_store.has_private_key())
         {
            auto oacct = lookup_account( key_to_store.account_address );
            FC_ASSERT(oacct.valid(), "expecting an account to existing at this point");
            oacct->is_my_account = true;
            store_record( *oacct );
            cache_account( *oacct );
            ilog( "WALLET: storing private key for ${key} under account '${account_name}' address: (${account})",
                  ("key",key_to_store.public_key)
                  ("account",key_to_store.account_address)
                 ("account_name",get_account_name(key_to_store.account_address)) );
         }
         else
         {
            /*
            ilog( "WALLET: storing public key ${key} under account named '${account_name}' address: (${account})",
                  ("key",key_to_store.public_key)
                  ("account",key_to_store.account_address)
                  ("account_name",get_account_name(key_to_store.account_address)) );
                  */
         }
         ilog( "storing key" );

         store_record( key_itr->second, true );
      }
      else
      {
         auto r = wallet_key_record( key_to_store, new_wallet_record_index() );
         store_record( keys[key_to_store.get_address()] = r, true );

         auto key = key_to_store.public_key;
         auto bts_addr = key_to_store.get_address();
         btc_to_bts_address[ address(key) ] = bts_addr;
         btc_to_bts_address[ address(pts_address(key,false,56) )] = bts_addr;
         btc_to_bts_address[ address(pts_address(key,true,56) ) ] = bts_addr;
         btc_to_bts_address[ address(pts_address(key,false,0) ) ] = bts_addr;
         btc_to_bts_address[ address(pts_address(key,true,0) )  ] = bts_addr;
         ilog( "indexing key ${k}", ("k",address(pts_address(key,false,56) )  ) );
         ilog( "indexing key ${k}", ("k",address(pts_address(key,true,56) )  ) );
      }
   }
Example #11
0
   void WalletDb::store_key( const KeyData& key )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");
       FC_ASSERT( key.public_key != PublicKeyType() );

       oWalletKeyEntry key_entry = lookup_key( key.get_address() );
       if( !key_entry.valid() )
           key_entry = WalletKeyEntry();

       KeyData& temp = *key_entry;
       temp = key;

       store_and_reload_entry( *key_entry, true );

       if( key_entry->has_private_key() )
       {
           oWalletAccountEntry account_entry = lookup_account( key.public_key );
           if( !account_entry.valid() )
               account_entry = lookup_account( key.account_address );

           if( account_entry.valid() )
           {
               if( key_entry->account_address != account_entry->owner_address() )
               {
                   key_entry->account_address = account_entry->owner_address();
                   store_and_reload_entry( *key_entry, true );
               }

               if( !account_entry->is_my_account )
               {
                   account_entry->is_my_account = true;
                   store_account( *account_entry );
               }
           }
       }
   } FC_CAPTURE_AND_RETHROW( (key) ) }
Example #12
0
   PrivateKeyType WalletDb::generate_new_account_child_key( const fc::sha512& password, const string& account_name )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");

       oWalletAccountEntry account_entry = lookup_account( account_name );
       FC_ASSERT( account_entry.valid(), "Account not found!" );
       FC_ASSERT( !account_entry->is_retracted(), "Account has been retracted!" );
       FC_ASSERT( account_entry->is_my_account, "Not my account!" );

       const oWalletKeyEntry key_entry = lookup_key( Address( account_entry->active_key() ) );
       FC_ASSERT( key_entry.valid(), "Active key not found!" );
       FC_ASSERT( key_entry->has_private_key(), "Active private key not found!" );

       const PrivateKeyType active_private_key = key_entry->decrypt_private_key( password );
       uint32_t seq_num = account_entry->last_used_gen_sequence;
       PrivateKeyType account_child_private_key;
       PublicKeyType account_child_public_key;
       Address account_child_address;
       while( true )
       {
           ++seq_num;
           FC_ASSERT( seq_num != 0, "Overflow!" );

           account_child_private_key = get_account_child_key( active_private_key, seq_num );
           account_child_public_key = account_child_private_key.get_public_key();
           account_child_address = Address( account_child_public_key );

           oWalletKeyEntry key_entry = lookup_key( account_child_address );
           if( key_entry.valid() && key_entry->has_private_key() ) continue;

           break;
       }

       account_entry->last_used_gen_sequence = seq_num;

       KeyData key;
       key.account_address = account_entry->owner_address();
       key.public_key = account_child_public_key;
       key.encrypt_private_key( password, account_child_private_key );
       key.gen_seq_number = seq_num;

       store_account( *account_entry );
       store_key( key );

       return account_child_private_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
Example #13
0
   void wallet_db::add_contact_account( const account_record& blockchain_account_record, const variant& private_data )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record record = lookup_account( blockchain_account_record.name );
       FC_ASSERT( !record.valid(), "Wallet already contains an account with that name!" );

       const public_key_type account_address = blockchain_account_record.owner_key;
       owallet_key_record key_record = lookup_key( account_address );
       FC_ASSERT( !key_record.valid(), "Wallet already contains that key" );

       record = wallet_account_record();
       account_record& temp_record = *record;
       temp_record = blockchain_account_record;
       record->private_data = private_data;

       store_account( *record );
   } FC_CAPTURE_AND_RETHROW( (blockchain_account_record) ) }
Example #14
0
   void wallet_db::remove_contact_account( const string& account_name )
   {
      auto opt_account = lookup_account( account_name );
      FC_ASSERT( opt_account.valid() );
      auto acct = *opt_account;
      FC_ASSERT( ! has_private_key(address(acct.owner_key)), "you can only remove contact accounts");

      accounts.erase( acct.wallet_record_index );
      remove_item( acct.wallet_record_index );
      keys.erase( address(acct.owner_key) );
      address_to_account_wallet_record_index.erase( address(acct.owner_key) );
      for( const auto& time_key_pair : acct.active_key_history )
      {
          keys.erase( address(time_key_pair.second) );
          address_to_account_wallet_record_index.erase( address(time_key_pair.second) );
      }
      name_to_account_wallet_record_index.erase( account_name );
   }
Example #15
0
   void WalletDb::add_contact_account( const AccountEntry& blockchain_account_entry, const variant& private_data )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");

       oWalletAccountEntry entry = lookup_account( blockchain_account_entry.name );
       FC_ASSERT( !entry.valid(), "Wallet already contains an account with that name!" );

       const PublicKeyType account_address = blockchain_account_entry.owner_key;
       oWalletKeyEntry key_entry = lookup_key( account_address );
       FC_ASSERT( !key_entry.valid(), "Wallet already contains that key" );

       entry = WalletAccountEntry();
       AccountEntry& temp_entry = *entry;
       temp_entry = blockchain_account_entry;
       entry->private_data = private_data;

       store_account( *entry );
   } FC_CAPTURE_AND_RETHROW( (blockchain_account_entry) ) }
Example #16
0
 vector<wallet_balance_record> wallet_db::get_all_balances( const string& account_name, uint32_t limit )
 {
     auto ret = vector<wallet_balance_record>();
     auto count = 0;
     for( auto item : balances )
     {
         if (count == limit && limit != -1)
             break;
         auto okey = lookup_key(item.second.owner());
         FC_ASSERT(okey.valid(), "expect a key record to exist at this point");
         auto oacct = lookup_account( okey->account_address );
         if ( oacct.valid() && oacct->name == account_name )
         {
             ret.push_back(item.second);
             count++;
         }
     }
     return ret;
 }
Example #17
0
   void WalletDb::import_key( const fc::sha512& password, const string& account_name, const PrivateKeyType& private_key,
                               bool move_existing )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");

       oWalletAccountEntry account_entry = lookup_account( account_name );
       FC_ASSERT( account_entry.valid(), "Account name not found!" );

       const PublicKeyType public_key = private_key.get_public_key();

       oWalletKeyEntry key_entry = lookup_key( Address( public_key ) );
       if( !key_entry.valid() )
           key_entry = WalletKeyEntry();
       else if( !move_existing )
           FC_ASSERT( key_entry->account_address == account_entry->owner_address() );

       key_entry->account_address = account_entry->owner_address();
       key_entry->public_key = public_key;
       key_entry->encrypt_private_key( password, private_key );

       store_key( *key_entry );
   } FC_CAPTURE_AND_RETHROW( (account_name)(move_existing) ) }
Example #18
0
   wallet_account_record wallet_db::store_account( const account_data& account )
   { try {
       FC_ASSERT( is_open() );
       FC_ASSERT( account.name != string() );
       FC_ASSERT( account.owner_key != public_key_type() );

       owallet_account_record account_record = lookup_account( account.owner_address() );
       if( !account_record.valid() )
           account_record = wallet_account_record();

       account_data& temp = *account_record;
       temp = account;

       store_and_reload_record( *account_record );

       const auto index_key = [ & ]( const public_key_type& account_public_key )
       {
           const address account_address = address( account_public_key );
           owallet_key_record key_record = lookup_key( account_address );
           if( !key_record.valid() )
           {
               key_data key;
               key.account_address = account_address;
               key.public_key = account_public_key;
               store_key( key );
           }
           else if( key_record->has_private_key() )
           {
               if( key_record->account_address != account_record->owner_address() )
               {
                   key_record->account_address = account_record->owner_address();
                   store_key( *key_record );
               }
           }
       };
       account_record->scan_public_keys( index_key );

       return *account_record;
   } FC_CAPTURE_AND_RETHROW( (account) ) }
Example #19
0
   void wallet_db::import_key( const fc::sha512& password, const string& account_name, const private_key_type& private_key,
                               bool move_existing )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record account_record = lookup_account( account_name );
       FC_ASSERT( account_record.valid(), "Account name not found!" );

       const public_key_type public_key = private_key.get_public_key();

       owallet_key_record key_record = lookup_key( address( public_key ) );
       if( !key_record.valid() )
           key_record = wallet_key_record();
       else if( !move_existing )
           FC_ASSERT( key_record->account_address == account_record->owner_address() );

       key_record->account_address = account_record->owner_address();
       key_record->public_key = public_key;
       key_record->encrypt_private_key( password, private_key );

       store_key( *key_record );
   } FC_CAPTURE_AND_RETHROW( (account_name)(move_existing) ) }
Example #20
0
   void wallet_db::rename_account(const public_key_type &old_account_key,
                                   const string& new_account_name )
   {
      /* Precondition: check that new_account doesn't exist in wallet and that old_account does
       */
      FC_ASSERT( is_open() );

      auto opt_old_acct = lookup_account( old_account_key );
      FC_ASSERT( opt_old_acct.valid() );
      auto acct = *opt_old_acct;
      auto old_name = acct.name;
      acct.name = new_account_name;
      name_to_account_wallet_record_index[acct.name] = acct.wallet_record_index;
      if( name_to_account_wallet_record_index[old_name] == acct.wallet_record_index )
        //Only remove the old name from the map if it pointed to the record we've just renamed
        name_to_account_wallet_record_index.erase( old_name );
      accounts[acct.wallet_record_index] = acct;
      address_to_account_wallet_record_index[address(acct.owner_key)] = acct.wallet_record_index;
      for( const auto& time_key_pair : acct.active_key_history )
          address_to_account_wallet_record_index[address(time_key_pair.second)] = acct.wallet_record_index;

      store_record( acct );
   }
Example #21
0
   int32_t wallet_db::new_key_child_index( const address& account_address )
   {
      owallet_account_record account_rec = lookup_account( account_address );
      if( account_rec )
      {
          account_rec->last_used_gen_sequence++;
          cache_account( *account_rec );
          return account_rec->last_used_gen_sequence;
      }

      auto next_child_idx = get_property( next_child_key_index );
      int32_t next_child_index = 0;
      if( next_child_idx.is_null() )
      {
         next_child_index = 1;
      }
      else
      {
         next_child_index = next_child_idx.as<int32_t>();
      }
      set_property( property_enum::next_child_key_index, next_child_index + 1 );
      return next_child_index;
   }
Example #22
0
   private_key_type wallet_db::generate_new_account_child_key( const fc::sha512& password, const string& account_name,
                                                               const account_key_type parent_key_type )
   { try {
       FC_ASSERT( is_open() );

       owallet_account_record account_record = lookup_account( account_name );
       FC_ASSERT( account_record.valid(), "Account not found!" );
       FC_ASSERT( !account_record->is_retracted(), "Account has been retracted!" );

       public_key_type parent_public_key;
       switch( parent_key_type )
       {
           case account_key_type::owner_key:
               parent_public_key = account_record->owner_key;
               break;
           case account_key_type::active_key:
               parent_public_key = account_record->active_key();
               break;
           case account_key_type::signing_key:
               FC_ASSERT( account_record->is_delegate() );
               parent_public_key = account_record->signing_key();
               break;
           // No default to force compiler warning
       }

       const owallet_key_record parent_key_record = lookup_key( address( parent_public_key ) );
       FC_ASSERT( parent_key_record.valid(), "Parent key not found!" );
       FC_ASSERT( parent_key_record->has_private_key(), "Parent private key not found!" );

       const private_key_type parent_private_key = parent_key_record->decrypt_private_key( password );
       uint32_t child_key_index = account_record->last_child_key_index;
       private_key_type account_child_private_key;
       public_key_type account_child_public_key;
       address account_child_address;
       while( true )
       {
           ++child_key_index;
           FC_ASSERT( child_key_index != 0, "Overflow!" );

           account_child_private_key = get_account_child_key( parent_private_key, child_key_index );
           account_child_public_key = account_child_private_key.get_public_key();
           account_child_address = address( account_child_public_key );

           owallet_key_record child_key_record = lookup_key( account_child_address );
           if( child_key_record.valid() && child_key_record->has_private_key() ) continue;

           break;
       }

       account_record->last_child_key_index = child_key_index;

       key_data child_key;
       child_key.account_address = account_record->owner_address();
       child_key.child_key_index = child_key_index;
       child_key.public_key = account_child_public_key;
       child_key.encrypt_private_key( password, account_child_private_key );

       store_account( *account_record );
       store_key( child_key );

       return account_child_private_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
Example #23
0
 string wallet_db::get_account_name( const address& account_address )const
 {
    auto opt = lookup_account( account_address );
    if( opt ) return opt->name;
    return "?";
 }
Example #24
0
   PublicKeyType WalletDb::generate_new_account( const fc::sha512& password, const string& account_name, const variant& private_data )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");

       oWalletAccountEntry account_entry = lookup_account( account_name );
       FC_ASSERT( !account_entry.valid(), "Wallet already contains an account with that name!" );

       uint32_t key_index = get_last_wallet_child_key_index();
       PrivateKeyType owner_private_key, active_private_key;
       PublicKeyType owner_public_key, active_public_key;
       Address owner_address, active_address;
       while( true )
       {
           ++key_index;
           FC_ASSERT( key_index != 0, "Overflow!" );

           owner_private_key = get_wallet_child_key( password, key_index );
           owner_public_key = owner_private_key.get_public_key();
           owner_address = Address( owner_public_key );

           account_entry = lookup_account( owner_address );
           if( account_entry.valid() ) continue;

           oWalletKeyEntry key_entry = lookup_key( owner_address );
           if( key_entry.valid() && key_entry->has_private_key() ) continue;

           active_private_key = get_account_child_key( owner_private_key, 0 );
           active_public_key = active_private_key.get_public_key();
           active_address = Address( active_public_key );

           account_entry = lookup_account( active_address );
           if( account_entry.valid() ) continue;

           key_entry = lookup_key( active_address );
           if( key_entry.valid() && key_entry->has_private_key() ) continue;

           break;
       }

       KeyData active_key;
       active_key.account_address = owner_address;
       active_key.public_key = active_public_key;
       active_key.encrypt_private_key( password, active_private_key );

       KeyData owner_key;
       owner_key.account_address = owner_address;
       owner_key.public_key = owner_public_key;
       owner_key.encrypt_private_key( password, owner_private_key );
       owner_key.gen_seq_number = key_index;

       AccountData account;
       account.name = account_name;
       account.owner_key = owner_public_key;
       account.set_active_key( blockchain::now(), active_public_key );
       account.last_update = blockchain::now();
       account.is_my_account = true;
       account.private_data = private_data;

       store_key( active_key );
       set_last_wallet_child_key_index( key_index );
       store_key( owner_key );
       store_account( account );

       return owner_public_key;
   } FC_CAPTURE_AND_RETHROW( (account_name) ) }
Example #25
0
   void WalletDb::remove_account(const string& accountname)
   {
	   try{
		   oWalletAccountEntry accRec = lookup_account(accountname);
		   if (!accRec.valid())
		   {
			   return;
		   }
		   const int32_t& entry_index = accRec->wallet_entry_index;

		   accounts.erase(entry_index);

		   address_to_account_wallet_entry_index.erase(Address(accRec->owner_key));
		   for (const auto& item : accRec->active_key_history)
		   {
			   const PublicKeyType& active_key = item.second;
			   address_to_account_wallet_entry_index.erase(Address(active_key));
		   }
		   if (accRec->is_delegate())
		   {
			   for (const auto& item : accRec->delegate_info->signing_key_history)
			   {
				   const PublicKeyType& signing_key = item.second;
				   address_to_account_wallet_entry_index.erase(Address(signing_key));
			   }
		   }

		   name_to_account_wallet_entry_index.erase(accRec->name);
		   account_id_to_wallet_entry_index.erase(accRec->id);
		   GenericWalletEntry entry(*accRec);
		   my->remove_generic_wallet_entry(entry);
		   //从当前运行时数据中将账号纪录删除
		   address_to_account_wallet_entry_index.erase(Address(accRec->owner_key));
		   name_to_account_wallet_entry_index.erase(accRec->name);
		   account_id_to_wallet_entry_index.erase(accRec->id);
		   btc_to_gop_address.erase(accRec->owner_address());
		   btc_to_gop_address.erase(Address(PtsAddress(accRec->owner_key, false, 0)));
		   btc_to_gop_address.erase(Address(PtsAddress(accRec->owner_key, true, 0)));
		   btc_to_gop_address.erase(Address(PtsAddress(accRec->owner_key, false, 56)));
		   btc_to_gop_address.erase(Address(PtsAddress(accRec->owner_key, true, 56)));
		   if (accRec->is_delegate())
		   {
			btc_to_gop_address.erase(accRec->signing_address());
			btc_to_gop_address.erase(Address(PtsAddress(accRec->signing_key(), false, 0)));
			btc_to_gop_address.erase(Address(PtsAddress(accRec->signing_key(), true, 0)));
			btc_to_gop_address.erase(Address(PtsAddress(accRec->signing_key(), false, 56)));
			btc_to_gop_address.erase(Address(PtsAddress(accRec->signing_key(), true, 56)));
		   }
		   btc_to_gop_address.erase(Address(accRec->active_key()));
		   btc_to_gop_address.erase(Address(PtsAddress(accRec->active_key(), false, 0)));
		   btc_to_gop_address.erase(Address(PtsAddress(accRec->active_key(), true, 0)));
		   btc_to_gop_address.erase(Address(PtsAddress(accRec->active_key(), false, 56)));
		   btc_to_gop_address.erase(Address(PtsAddress(accRec->active_key(), true, 56)));
		   

		   //从db中将key删除,如果不删除,scan_accounts时会将与key相关的账号存入钱包
		   oWalletKeyEntry tmpRec= lookup_key(accRec->owner_address());
		   if (tmpRec.valid())
		   {
			   my->remove_generic_wallet_entry(GenericWalletEntry(*tmpRec));
		   }
		   tmpRec = lookup_key(accRec->active_address());
		   if (tmpRec.valid())
		   {
			   my->remove_generic_wallet_entry(GenericWalletEntry(*tmpRec));
		   }
		   if (accRec->is_delegate())
		   {
			   tmpRec = lookup_key(accRec->signing_address());
			   if (tmpRec.valid())
			   {
				   my->remove_generic_wallet_entry(GenericWalletEntry(*tmpRec));
			   }
		   }

		   auto keyit_owner= keys.find(accRec->owner_address());
		   if (keyit_owner != keys.end())
		   {
			   GenericWalletEntry rec(keyit_owner->second);
			   my->remove_generic_wallet_entry(rec);
			   keys.erase(accRec->owner_address());
		   }
		   auto keyit_activekey = keys.find(Address(accRec->active_key()));
		   if (keyit_activekey != keys.end())
		   {
			   GenericWalletEntry rec(keyit_activekey->second);
			   my->remove_generic_wallet_entry(rec);
			   keys.erase(accRec->active_address());
		   }
		   if (accRec->is_delegate())
		   {
			   auto keyit_sign = keys.find(accRec->signing_address());
			   if (keyit_sign != keys.end())
			   {
				   GenericWalletEntry rec(keyit_sign->second);
				   my->remove_generic_wallet_entry(rec);
			   }
			   keys.erase(accRec->signing_address());
			   accounts.erase(accRec->wallet_entry_index);
		   }
		   //删除对应的交易记录
		   unordered_map<TransactionIdType, WalletTransactionEntry>::iterator mapit = transactions.begin();
		   unordered_map<TransactionIdType, WalletTransactionEntry>::iterator tmpit;
		   while (mapit != transactions.end())
		   {
			   tmpit = mapit;
		
			   GenericWalletEntry rec(tmpit->second);
			   my->remove_generic_wallet_entry(rec);
			   mapit = transactions.erase(tmpit);
		   }
	   }FC_CAPTURE_AND_RETHROW((accountname))}