major change: Add security support & Adjust GUI

Change-Id: I7abef37169dec1ef4b68e760dee5214c147c1915
diff --git a/src/contact-storage.cpp b/src/contact-storage.cpp
index f107f0c..e3c513f 100644
--- a/src/contact-storage.cpp
+++ b/src/contact-storage.cpp
@@ -11,28 +11,32 @@
 #include "contact-storage.h"
 
 #include <boost/filesystem.hpp>
+#include <cryptopp/sha.h>
+#include <cryptopp/filters.h>
+#include <cryptopp/hex.h>
+#include <cryptopp/files.h>
 #include "logging.h"
 
 using namespace std;
 using namespace ndn;
 namespace fs = boost::filesystem;
 
-INIT_LOGGER ("ContactStorage");
+
+INIT_LOGGER ("chronos.ContactStorage");
 
 namespace chronos{
 
 const string INIT_SP_TABLE = "\
 CREATE TABLE IF NOT EXISTS                                           \n \
   SelfProfile(                                                       \n \
-      profile_identity  BLOB NOT NULL,                               \n \
       profile_type      BLOB NOT NULL,                               \n \
       profile_value     BLOB NOT NULL,                               \n \
                                                                      \
-      PRIMARY KEY (profile_identity, profile_type)                   \n \
+      PRIMARY KEY (profile_type)                                     \n \
   );                                                                 \n \
                                                                      \
-CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \
-";
+CREATE INDEX sp_index ON SelfProfile(profile_type);                  \n \
+"; // user's own profile;
 
 const string INIT_SE_TABLE = "\
 CREATE TABLE IF NOT EXISTS                                           \n \
@@ -43,21 +47,24 @@
       PRIMARY KEY (identity)                                         \n \
   );                                                                 \n \
 CREATE INDEX se_index ON SelfEndorse(identity);                      \n \
-";
+"; // user's self endorse cert;
 
 const string INIT_CONTACT_TABLE = "\
 CREATE TABLE IF NOT EXISTS                                           \n \
   Contact(                                                           \n \
       contact_namespace BLOB NOT NULL,                               \n \
       contact_alias     BLOB NOT NULL,                               \n \
-      self_certificate  BLOB NOT NULL,                               \n \
+      contact_keyName   BLOB NOT NULL,                               \n \
+      contact_key       BLOB NOT NULL,                               \n \
+      notBefore         INTEGER DEFAULT 0,                           \n \
+      notAfter          INTEGER DEFAULT 0,                           \n \
       is_introducer     INTEGER DEFAULT 0,                           \n \
                                                                      \
       PRIMARY KEY (contact_namespace)                                \n \
   );                                                                 \n \
                                                                      \
 CREATE INDEX contact_index ON Contact(contact_namespace);            \n \
-";
+"; // contact's basic info
 
 const string INIT_TS_TABLE = "\
 CREATE TABLE IF NOT EXISTS                                           \n \
@@ -68,7 +75,7 @@
   );                                                                 \n \
                                                                      \
 CREATE INDEX ts_index ON TrustScope(contact_namespace);              \n \
-";
+"; // contact's trust scope;
 
 const string INIT_CP_TABLE = "\
 CREATE TABLE IF NOT EXISTS                                           \n \
@@ -82,7 +89,7 @@
   );                                                                 \n \
                                                                      \
 CREATE INDEX cp_index ON ContactProfile(profile_identity);           \n \
-";
+"; // contact's profile
 
 const string INIT_PE_TABLE = "\
 CREATE TABLE IF NOT EXISTS                                           \n \
@@ -94,28 +101,40 @@
   );                                                                 \n \
                                                                      \
 CREATE INDEX pe_index ON ProfileEndorse(identity);                   \n \
-";
+"; // user's endorsement on contacts
 
 const string INIT_CE_TABLE = "\
 CREATE TABLE IF NOT EXISTS                                           \n \
   CollectEndorse(                                                    \n \
-      endorsee          BLOB NOT NULL,                               \n \
       endorser          BLOB NOT NULL,                               \n \
       endorse_name      BLOB NOT NULL,                               \n \
       endorse_data      BLOB NOT NULL,                               \n \
                                                                      \
-      PRIMARY KEY (endorsee, endorser)                               \n \
+      PRIMARY KEY (endorser)                                         \n \
   );                                                                 \n \
-                                                                     \
-CREATE INDEX ce_index ON CollectEndorse(endorsee);                   \n \
-";
+"; // contact's endorsements on the user
 
-ContactStorage::ContactStorage()
+const string INIT_DD_TABLE = "\
+CREATE TABLE IF NOT EXISTS                                           \n \
+  DnsData(                                                           \n \
+      dns_name      BLOB NOT NULL,                                   \n \
+      dns_type      BLOB NOT NULL,                                   \n \
+      data_name     BLOB NOT NULL,                                   \n	\
+      dns_value     BLOB NOT NULL,                                   \n \
+                                                                     \
+      PRIMARY KEY (dns_name, dns_type)                               \n \
+  );                                                                 \
+CREATE INDEX dd_index ON DnsData(dns_name, dns_type);                \n \
+CREATE INDEX dd_index2 ON DnsData(data_name);                        \n \
+"; // dns data;
+
+ContactStorage::ContactStorage(const Name& identity)
+  : m_identity(identity)
 {
   fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
   fs::create_directories (chronosDir);
 
-  int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db);
+  int res = sqlite3_open((chronosDir / getDBName()).c_str (), &m_db);
   if (res != SQLITE_OK)
     throw Error("Chronos DB cannot be open/created");
 
@@ -126,9 +145,28 @@
   initializeTable("ContactProfile", INIT_CP_TABLE);
   initializeTable("ProfileEndorse", INIT_PE_TABLE);
   initializeTable("CollectEndorse", INIT_CE_TABLE);
+  initializeTable("DnsData", INIT_DD_TABLE);
 
 }
 
+string
+ContactStorage::getDBName()
+{
+  string dbName("chronos-");
+
+  stringstream ss;
+  {
+    using namespace CryptoPP;
+
+    SHA256 hash;
+    StringSource(m_identity.wireEncode().wire(), m_identity.wireEncode().size(), true,
+                 new HashFilter(hash, new HexEncoder(new FileSink(ss), false)));
+  }
+  dbName.append(ss.str()).append(".db");
+
+  return dbName;
+}
+
 void
 ContactStorage::initializeTable(const string& tableName, const string& sqlCreateStmt)
 {
@@ -152,16 +190,14 @@
 }
 
 shared_ptr<Profile>
-ContactStorage::getSelfProfile(const Name& identity) const
+ContactStorage::getSelfProfile()
 {  
-  shared_ptr<Profile> profile;
+  shared_ptr<Profile> profile = make_shared<Profile>(m_identity);
   sqlite3_stmt *stmt;
-  sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0);
-  sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
+  sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile", -1, &stmt, 0);
 
   while( sqlite3_step (stmt) == SQLITE_ROW)
     {
-      profile = make_shared<Profile>(identity);
       string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
       string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
       (*profile)[profileType] = profileValue;
@@ -172,61 +208,20 @@
   return profile;
 }
 
-// Block
-// ContactStorage::getSelfEndorseCertificate(const Name& identity)
-// {
-//   sqlite3_stmt *stmt;
-//   sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0);
-//   sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
-
-//   if(sqlite3_step (stmt) == SQLITE_ROW)
-//     {
-//       Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
-//       sqlite3_finalize (stmt);
-//       return result;
-//     }
-
-//   sqlite3_finalize (stmt);
-
-//   throw Error("ContactStorage: No self-endorse certificate found!");
-// }
-
 void
-ContactStorage::addSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate, const Name& identity)
+ContactStorage::addSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate)
 {
   const Block& newEndorseCertificateBlock = newEndorseCertificate.wireEncode();
 
   sqlite3_stmt *stmt;
   sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
-  sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 1, m_identity.toUri().c_str(), m_identity.toUri().size(), SQLITE_TRANSIENT);
   sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT);
-  sqlite3_step(stmt);
+  int res = sqlite3_step(stmt);
 
   sqlite3_finalize (stmt);
 }
 
-// Block
-// ContactStorage::getEndorseCertificate(const Name& identity)
-// {
-//   sqlite3_stmt *stmt;
-//   sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM ProfileEndorse where identity=?", -1, &stmt, 0);
-//   sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
-
-  
-//   if(sqlite3_step (stmt) == SQLITE_ROW)
-//     {
-//       Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
-//       sqlite3_finalize (stmt);
-//       return result;
-//     }
-
-//   sqlite3_finalize (stmt);
-
-//   throw Error("ContactStorage: No endorse certificate found!");
-  
-//   return Block();
-// }
-
 void
 ContactStorage::addEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity)
 {
@@ -235,7 +230,7 @@
   sqlite3_stmt *stmt;
   sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO ProfileEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
   sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.value()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT);
   sqlite3_step(stmt);
 
   sqlite3_finalize (stmt);
@@ -245,33 +240,39 @@
 ContactStorage::updateCollectEndorse(const EndorseCertificate& endorseCertificate)
 {
   Name endorserName = endorseCertificate.getSigner();
-  Name keyName = endorseCertificate.getPublicKeyName();
-  Name endorseeName = keyName.getPrefix(keyName.size()-1);
-  Name getCertName = endorseCertificate.getName();
+  Name certName = endorseCertificate.getName();
 
   sqlite3_stmt *stmt;
-  sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO CollectEndorse (endorser, endorsee, endorse_name, endorse_data) VALUES (?, ?, ?, ?)", -1, &stmt, 0);
+  sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO CollectEndorse (endorser, endorse_name, endorse_data) VALUES (?, ?, ?, ?)", -1, &stmt, 0);
   sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(stmt, 3, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 2, certName.toUri().c_str(), certName.toUri().size(), SQLITE_TRANSIENT);
   const Block &block = endorseCertificate.wireEncode();
-  sqlite3_bind_text(stmt, 4, reinterpret_cast<const char*>(block.wire()), block.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 3, reinterpret_cast<const char*>(block.wire()), block.size(), SQLITE_TRANSIENT);
   int res = sqlite3_step (stmt);
   sqlite3_finalize (stmt); 
   return;
 }
 
 void
-ContactStorage::getCollectEndorseList(const Name& name, vector<Buffer>& endorseList)
+ContactStorage::getCollectEndorse(EndorseCollection& endorseCollection)
 {
   sqlite3_stmt *stmt;
-  sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM CollectEndorse WHERE endorsee=?", -1, &stmt, 0);
-  sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
+  sqlite3_prepare_v2 (m_db, "SELECT endorse_name, endorse_data FROM CollectEndorse", -1, &stmt, 0);
 
   while(sqlite3_step (stmt) == SQLITE_ROW)
     {
-      Buffer blob(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
-      endorseList.push_back(blob);
+      string certName(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes(stmt, 1));
+      stringstream ss;
+      {
+        using namespace CryptoPP;
+        SHA256 hash;
+
+        StringSource(sqlite3_column_text(stmt, 1), sqlite3_column_bytes (stmt, 0), true,
+                     new HashFilter(hash, new FileSink(ss)));
+      }
+      EndorseCollection::Endorsement* endorsement = endorseCollection.add_endorsement();
+      endorsement->set_certname(certName);
+      endorsement->set_hash(ss.str());
     }
 
   sqlite3_finalize (stmt);
@@ -294,9 +295,9 @@
 
 
 void 
-ContactStorage::removeContact(const Name& contactNameSpace)
+ContactStorage::removeContact(const Name& identityName)
 {
-  string identity = contactNameSpace.toUri();
+  string identity = identityName.toUri();
   
   sqlite3_stmt *stmt;  
   sqlite3_prepare_v2 (m_db, "DELETE FROM Contact WHERE contact_namespace=?", -1, &stmt, 0);
@@ -316,32 +317,38 @@
 }
   
 void
-ContactStorage::addContact(const ContactItem& contact)
+ContactStorage::addContact(const Contact& contact)
 {
   if(doesContactExist(contact.getNameSpace()))
     throw Error("Normal Contact has already existed");
 
+  string identity = contact.getNameSpace().toUri();
   bool isIntroducer = contact.isIntroducer();
 
   sqlite3_stmt *stmt;  
   sqlite3_prepare_v2 (m_db, 
-                      "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)", 
-                      -1, 
-                      &stmt, 
+                      "INSERT INTO Contact (contact_namespace, contact_alias, contact_keyName, contact_key, notBefore, notAfter, is_introducer) values (?, ?, ?, ?, ?, ?, ?)", 
+                      -1,
+                      &stmt,
                       0);
 
-  sqlite3_bind_text(stmt, 1, contact.getNameSpace().toUri().c_str(),  contact.getNameSpace().toUri().size (), SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
   sqlite3_bind_text(stmt, 2, contact.getAlias().c_str(), contact.getAlias().size(), SQLITE_TRANSIENT);
-  const Block& selfCertificateBlock = contact.getSelfEndorseCertificate().wireEncode();
-  sqlite3_bind_text(stmt, 3, reinterpret_cast<const char*>(selfCertificateBlock.wire()), selfCertificateBlock.size(), SQLITE_TRANSIENT);
-  sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0));
+  sqlite3_bind_text(stmt, 3, contact.getPublicKeyName().toUri().c_str(), contact.getPublicKeyName().toUri().size(), SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 4, reinterpret_cast<const char*>(contact.getPublicKey().get().buf()), contact.getPublicKey().get().size(), SQLITE_TRANSIENT);
+  sqlite3_bind_int64(stmt, 5, contact.getNotBefore());
+  sqlite3_bind_int64(stmt, 6, contact.getNotAfter());
+  sqlite3_bind_int(stmt, 7, (isIntroducer ? 1 : 0));
 
   int res = sqlite3_step (stmt);
+
+  // _LOG_DEBUG("addContact: " << res);
+
   sqlite3_finalize (stmt);
 
-  const Profile&  profile = contact.getSelfEndorseCertificate().getProfile();
+  const Profile& profile = contact.getProfile();
   Profile::const_iterator it = profile.begin();
-  string identity = contact.getNameSpace().toUri();
+
   for(; it != profile.end(); it++)
     {
       sqlite3_prepare_v2 (m_db, 
@@ -358,17 +365,17 @@
 
   if(isIntroducer)
     {
-      ContactItem::const_iterator it = contact.trustScopeBegin();
-      string nameSpace = contact.getNameSpace().toUri();
-      
-      while(it != contact.trustScopeEnd())
+      Contact::const_iterator it  = contact.trustScopeBegin();
+      Contact::const_iterator end = contact.trustScopeEnd();
+
+      while(it != end)
         {
           sqlite3_prepare_v2 (m_db, 
                               "INSERT INTO TrustScope (contact_namespace, trust_scope) values (?, ?)", 
                               -1, 
                               &stmt, 
                               0);
-          sqlite3_bind_text(stmt, 1, nameSpace.c_str(),  nameSpace.size (), SQLITE_TRANSIENT);
+          sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
           sqlite3_bind_text(stmt, 2, it->first.toUri().c_str(), it->first.toUri().size(), SQLITE_TRANSIENT);
           res = sqlite3_step (stmt);
           sqlite3_finalize (stmt);          
@@ -378,43 +385,55 @@
 }
 
 
-shared_ptr<ContactItem>
-ContactStorage::getContact(const Name& name)
+shared_ptr<Contact>
+ContactStorage::getContact(const Name& identity) const
 {
+  shared_ptr<Contact> contact;
+  Profile profile;
+
   sqlite3_stmt *stmt;
-  sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact where contact_namespace=?", -1, &stmt, 0);
-  sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
-  
-  if( sqlite3_step (stmt) == SQLITE_ROW)
+  sqlite3_prepare_v2 (m_db, "SELECT contact_alias, contact_keyName, contact_key, notBefore, notAfter, is_introducer FROM Contact where contact_namespace=?", -1, &stmt, 0);
+  sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
+
+  if(sqlite3_step (stmt) == SQLITE_ROW)
     {
-      string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
+      string alias(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
+      string keyName(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
+      PublicKey key(sqlite3_column_text(stmt, 2), sqlite3_column_bytes (stmt, 2));
+      int64_t notBefore = sqlite3_column_int64 (stmt, 3);
+      int64_t notAfter = sqlite3_column_int64 (stmt, 4);
+      int isIntroducer = sqlite3_column_int (stmt, 5);
 
-      Data certData;
-      certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
-      EndorseCertificate endorseCertificate(certData);
+      contact = shared_ptr<Contact>(new Contact(identity, alias, Name(keyName), notBefore, notAfter, key, isIntroducer));
+    }
+  sqlite3_finalize (stmt);
 
-      int isIntroducer = sqlite3_column_int (stmt, 2);
+  sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM ContactProfile where profile_identity=?", -1, &stmt, 0);
+  sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
+  
+  while(sqlite3_step (stmt) == SQLITE_ROW)
+    {
+      string type(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
+      string value(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
+      profile[type] = value;
+    }
+  sqlite3_finalize (stmt);
+  contact->setProfile(profile);
 
-      sqlite3_finalize (stmt);
-      
-      shared_ptr<ContactItem> contact = make_shared<ContactItem>(endorseCertificate, isIntroducer, alias);
+  if(contact->isIntroducer())
+    {
+      sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
+      sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
 
-      if(contact->isIntroducer())
+      while(sqlite3_step (stmt) == SQLITE_ROW)
         {
-          sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
-          sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
-
-          while( sqlite3_step (stmt) == SQLITE_ROW)
-            {
-              Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
-              contact->addTrustScope(scope);
-            }
-          sqlite3_finalize (stmt);  
+          Name scope(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
+          contact->addTrustScope(scope);
         }
+      sqlite3_finalize (stmt);
+    }
 
-      return contact;      
-    } 
-  return shared_ptr<ContactItem>();
+  return contact;
 }
 
 
@@ -464,41 +483,85 @@
 }
 
 void
-ContactStorage::getAllContacts(vector<shared_ptr<ContactItem> >& contacts) const
+ContactStorage::getAllContacts(vector<shared_ptr<Contact> >& contacts) const
 {
+  vector<Name> contactNames;
+
   sqlite3_stmt *stmt;
-  sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact", -1, &stmt, 0);
+  sqlite3_prepare_v2 (m_db, "SELECT contact_namespace FROM Contact", -1, &stmt, 0);
   
-  while( sqlite3_step (stmt) == SQLITE_ROW)
+  while(sqlite3_step (stmt) == SQLITE_ROW)
     {
-      string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
-
-      Data certData;
-      certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
-      EndorseCertificate endorseCertificate(certData);
-
-      int isIntroducer = sqlite3_column_int (stmt, 2);
-
-      contacts.push_back(make_shared<ContactItem>(endorseCertificate, isIntroducer, alias));      
+      string identity(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes(stmt, 0));
+      contactNames.push_back(Name(identity));
     }
-  sqlite3_finalize (stmt);  
+  sqlite3_finalize (stmt);
 
-  vector<shared_ptr<ContactItem> >::iterator it = contacts.begin();
-  for(; it != contacts.end(); it++)
+  vector<Name>::iterator it  = contactNames.begin();
+  vector<Name>::iterator end = contactNames.end();
+  for(; it != end; it++)
     {
-      if((*it)->isIntroducer())
-        {
-          sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
-          sqlite3_bind_text(stmt, 1, (*it)->getNameSpace().toUri().c_str(), (*it)->getNameSpace().toUri().size(), SQLITE_TRANSIENT);
-
-          while( sqlite3_step (stmt) == SQLITE_ROW)
-            {
-              Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
-              (*it)->addTrustScope(scope);
-            }
-          sqlite3_finalize (stmt);  
-        }
+      shared_ptr<Contact> contact = getContact(*it);
+      if(static_cast<bool>(contact))
+        contacts.push_back(contact);
     }
 }
 
+void
+ContactStorage::updateDnsData(const Block& data, 
+                              const string& name, 
+                              const string& type, 
+                              const string& dataName)
+{  
+  sqlite3_stmt *stmt;
+  sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO DnsData (dns_name, dns_type, dns_value, data_name) VALUES (?, ?, ?, ?)", -1, &stmt, 0);
+  sqlite3_bind_text(stmt, 1, name.c_str(), name.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 2, type.c_str(), type.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 3, reinterpret_cast<const char*>(data.wire()), data.size(), SQLITE_TRANSIENT); 
+  sqlite3_bind_text(stmt, 4, dataName.c_str(), dataName.size(), SQLITE_TRANSIENT);
+  int res = sqlite3_step(stmt);
+
+  // _LOG_DEBUG("updateDnsData " << res);
+  sqlite3_finalize(stmt);
+}
+
+shared_ptr<Data>
+ContactStorage::getDnsData(const Name& dataName)
+{
+  shared_ptr<Data> data;
+
+  sqlite3_stmt *stmt;
+  sqlite3_prepare_v2 (m_db, "SELECT dns_value FROM DnsData where data_name=?", -1, &stmt, 0);
+  sqlite3_bind_text(stmt, 1, dataName.toUri().c_str(), dataName.toUri().size(), SQLITE_TRANSIENT);
+  
+  if(sqlite3_step (stmt) == SQLITE_ROW)
+    {
+      data = make_shared<Data>();
+      data->wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
+    }
+  sqlite3_finalize(stmt);
+
+  return data;
+}
+
+shared_ptr<Data>
+ContactStorage::getDnsData(const string& name, const string& type)
+{
+  shared_ptr<Data> data;
+
+  sqlite3_stmt *stmt;
+  sqlite3_prepare_v2 (m_db, "SELECT dns_value FROM DnsData where dns_name=? and dns_type=?", -1, &stmt, 0);
+  sqlite3_bind_text(stmt, 1, name.c_str(), name.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_text(stmt, 2, type.c_str(), type.size(), SQLITE_TRANSIENT);
+  
+  if(sqlite3_step (stmt) == SQLITE_ROW)
+    {
+      data = make_shared<Data>();
+      data->wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
+    }
+  sqlite3_finalize(stmt);
+
+  return data;
+}
+
 }//chronos