From 4f51727c1bdeba1b59c14af45c67d933f53952d3 Mon Sep 17 00:00:00 2001 From: miguel Date: Sun, 18 Aug 2013 22:01:36 -0300 Subject: [PATCH] use bitcoin network time reference, dm creation draft --- .../include/libtorrent/aux_/session_impl.hpp | 2 +- .../libtorrent/kademlia/dht_tracker.hpp | 2 +- .../include/libtorrent/kademlia/node.hpp | 2 +- libtorrent/include/libtorrent/session.hpp | 2 +- libtorrent/src/kademlia/dht_tracker.cpp | 2 +- libtorrent/src/kademlia/node.cpp | 4 +- libtorrent/src/session.cpp | 2 +- libtorrent/src/session_impl.cpp | 2 +- src/twister.cpp | 73 +++++++++++++++++-- 9 files changed, 74 insertions(+), 17 deletions(-) diff --git a/libtorrent/include/libtorrent/aux_/session_impl.hpp b/libtorrent/include/libtorrent/aux_/session_impl.hpp index 0242d31a..2589bc10 100644 --- a/libtorrent/include/libtorrent/aux_/session_impl.hpp +++ b/libtorrent/include/libtorrent/aux_/session_impl.hpp @@ -309,7 +309,7 @@ namespace libtorrent void dht_putData(std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, - int timeutc, int seq); + boost::int64_t timeutc, int seq); void dht_getData(std::string const &username, std::string const &resource, bool multi); diff --git a/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp b/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp index b63e47d8..8bdd05c3 100644 --- a/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp +++ b/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp @@ -96,7 +96,7 @@ namespace libtorrent { namespace dht void putData(std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, - int timeutc, int seq); + boost::int64_t timeutc, int seq); void getData(std::string const &username, std::string const &resource, bool multi, boost::function fdata, diff --git a/libtorrent/include/libtorrent/kademlia/node.hpp b/libtorrent/include/libtorrent/kademlia/node.hpp index fbf712f5..eeee612a 100644 --- a/libtorrent/include/libtorrent/kademlia/node.hpp +++ b/libtorrent/include/libtorrent/kademlia/node.hpp @@ -216,7 +216,7 @@ public: void putData(std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, - int timeutc, int seq); + boost::int64_t timeutc, int seq); void getData(std::string const &username, std::string const &resource, bool multi, boost::function fdata, diff --git a/libtorrent/include/libtorrent/session.hpp b/libtorrent/include/libtorrent/session.hpp index e4b2dc2e..77b9969b 100644 --- a/libtorrent/include/libtorrent/session.hpp +++ b/libtorrent/include/libtorrent/session.hpp @@ -440,7 +440,7 @@ namespace libtorrent // [MF] twister void dht_putData(std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, - int timeutc, int seq); + boost::int64_t timeutc, int seq); void dht_getData(std::string const &username, std::string const &resource, bool multi); diff --git a/libtorrent/src/kademlia/dht_tracker.cpp b/libtorrent/src/kademlia/dht_tracker.cpp index 9fa21b3d..99d3b106 100644 --- a/libtorrent/src/kademlia/dht_tracker.cpp +++ b/libtorrent/src/kademlia/dht_tracker.cpp @@ -425,7 +425,7 @@ namespace libtorrent { namespace dht void dht_tracker::putData(std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, - int timeutc, int seq) + boost::int64_t timeutc, int seq) { m_dht.putData(username,resource, multi, value, sig_user, timeutc, seq); } diff --git a/libtorrent/src/kademlia/node.cpp b/libtorrent/src/kademlia/node.cpp index 3b1bb992..3adcbd5e 100644 --- a/libtorrent/src/kademlia/node.cpp +++ b/libtorrent/src/kademlia/node.cpp @@ -309,7 +309,7 @@ namespace node_impl& node, std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, - int timeutc, int seq) + boost::int64_t timeutc, int seq) { #ifdef TORRENT_DHT_VERBOSE_LOGGING TORRENT_LOG(node) << "sending putData [ username: " << username @@ -440,7 +440,7 @@ void node_impl::announce(std::string const& trackerName, sha1_hash const& info_h void node_impl::putData(std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, - int timeutc, int seq) + boost::int64_t timeutc, int seq) { #ifdef TORRENT_DHT_VERBOSE_LOGGING TORRENT_LOG(node) << "putData [ username: " << info_hash << " res: " << resource << " ]" ; diff --git a/libtorrent/src/session.cpp b/libtorrent/src/session.cpp index 8e7102a4..f38e17c1 100644 --- a/libtorrent/src/session.cpp +++ b/libtorrent/src/session.cpp @@ -853,7 +853,7 @@ namespace libtorrent void session::dht_putData(std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, - int timeutc, int seq) + boost::int64_t timeutc, int seq) { #ifndef TORRENT_DISABLE_DHT TORRENT_ASYNC_CALL7(dht_putData, username, resource, multi, value, sig_user, timeutc, seq); diff --git a/libtorrent/src/session_impl.cpp b/libtorrent/src/session_impl.cpp index 736d7419..259a259c 100644 --- a/libtorrent/src/session_impl.cpp +++ b/libtorrent/src/session_impl.cpp @@ -5745,7 +5745,7 @@ retry: void session_impl::dht_putData(std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, - int timeutc, int seq) + boost::int64_t timeutc, int seq) { if (m_dht) m_dht->putData(username, resource, multi, value, sig_user, timeutc, seq); } diff --git a/src/twister.cpp b/src/twister.cpp index 96569dbe..a6ff3566 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -468,10 +468,8 @@ std::string createSignature(std::string const &strMessage, std::string const &st return std::string((const char *)&vchSig[0], vchSig.size()); } - -bool verifySignature(std::string const &strMessage, std::string const &strUsername, std::string const &strSign) +bool getUserPubKey(std::string const &strUsername, CPubKey &pubkey) { - CPubKey pubkey; { CKeyID keyID; if( pwalletMain->GetKeyIdFromUsername(strUsername, keyID) ) { @@ -486,21 +484,32 @@ bool verifySignature(std::string const &strMessage, std::string const &strUserna uint256 hashBlock; uint256 userhash = SerializeHash(strUsername); if( !GetTransaction(userhash, txOut, hashBlock) ) { - //printf("verifySignature: user unknown '%s'\n", strUsername.c_str()); + //printf("getUserPubKey: user unknown '%s'\n", strUsername.c_str()); return false; } std::vector< std::vector > vData; if( !txOut.pubKey.ExtractPushData(vData) || vData.size() < 1 ) { - printf("verifySignature: broken pubkey for user '%s'\n", strUsername.c_str()); + printf("getUserPubKey: broken pubkey for user '%s'\n", strUsername.c_str()); return false; } pubkey = CPubKey(vData[0]); if( !pubkey.IsValid() ) { - printf("verifySignature: invalid pubkey for user '%s'\n", strUsername.c_str()); + printf("getUserPubKey: invalid pubkey for user '%s'\n", strUsername.c_str()); return false; } } + return true; +} + + +bool verifySignature(std::string const &strMessage, std::string const &strUsername, std::string const &strSign) +{ + CPubKey pubkey; + if( !getUserPubKey(strUsername, pubkey) ) { + printf("verifySignature: no pubkey for user '%s'\n", strUsername.c_str()); + return false; + } vector vchSig((const unsigned char*)strSign.data(), (const unsigned char*)strSign.data() + strSign.size()); @@ -614,14 +623,41 @@ bool validatePostNumberForUser(std::string const &username, int k) "sig_userpost" : signature by userpost.n */ -bool createSignedUserpost(entry &v, std::string const &username, int k) +bool createSignedUserpost(entry &v, std::string const &username, int k, + std::string const &msg, // either msg.size() or + entry const *rt, entry const *sig_rt, // rt != NULL or + entry const *dm, // dm != NULL. + std::string const &reply_n, int reply_k + ) { entry &userpost = v["userpost"]; // userpost["n"] = username; userpost["k"] = k; + userpost["time"] = GetAdjustedTime(); userpost["height"] = getBestHeight() - 1; // be conservative + + if( msg.size() ) { + //userpost["t"] = "post"; + userpost["msg"] = msg; + } else if ( rt != NULL && sig_rt != NULL ) { + //userpost["t"] = "rt"; + userpost["rt"] = *rt; + userpost["sig_rt"] = *sig_rt; + } else if ( dm != NULL ) { + //userpost["t"] = "dm"; + userpost["dm"] = *dm; + } else { + printf("createSignedUserpost: unknown type\n"); + return false; + } + + if( reply_n.size() ) { + entry &reply = userpost["reply"]; + reply["n"]=reply_n; + reply["k"]=reply_k; + } // std::vector buf; @@ -635,6 +671,27 @@ bool createSignedUserpost(entry &v, std::string const &username, int k) } } +bool createDirectMessage(entry &dm, std::string const &to, std::string const &msg) +{ + CPubKey pubkey; + if( !getUserPubKey(to, pubkey) ) { + printf("createDirectMessage: no pubkey for user '%s'\n", to.c_str()); + return false; + } + + ecies_secure_t sec; + bool encrypted = pubkey.Encrypt(msg, sec); + + if( encrypted ) { + dm["key"] = sec.key; + dm["mac"] = sec.mac; + dm["orig"] = sec.orig; + dm["body"] = sec.body; + } + + return encrypted; +} + int getBestHeight() { return nBestHeight; @@ -698,7 +755,7 @@ Value dhtput(const Array& params, bool fHelp) throw JSONRPCError(RPC_WALLET_ERROR, "Username must be the same as sig_user for single"); entry value = entry::string_type(strValue); - int timeutc = time(NULL); + boost::int64_t timeutc = GetAdjustedTime(); ses->dht_putData(strUsername, strResource, multi, value, strSigUser, timeutc, seq);