Dash Core  0.12.2.1
P2P Digital Currency
darksend-relay.cpp
Go to the documentation of this file.
1 #include "darksend.h"
2 #include "darksend-relay.h"
3 #include "messagesigner.h"
4 
6 {
8  nBlockHeight = 0;
9  nRelayType = 0;
10  in = CTxIn();
11  out = CTxOut();
12 }
13 
14 CDarkSendRelay::CDarkSendRelay(CTxIn& vinMasternodeIn, vector<unsigned char>& vchSigIn, int nBlockHeightIn, int nRelayTypeIn, CTxIn& in2, CTxOut& out2)
15 {
16  vinMasternode = vinMasternodeIn;
17  vchSig = vchSigIn;
18  nBlockHeight = nBlockHeightIn;
19  nRelayType = nRelayTypeIn;
20  in = in2;
21  out = out2;
22 }
23 
25 {
26  std::ostringstream info;
27 
28  info << "vin: " << vinMasternode.ToString() <<
29  " nBlockHeight: " << (int)nBlockHeight <<
30  " nRelayType: " << (int)nRelayType <<
31  " in " << in.ToString() <<
32  " out " << out.ToString();
33 
34  return info.str();
35 }
36 
37 bool CDarkSendRelay::Sign(std::string strSharedKey)
38 {
39  std::string strError = "";
40  std::string strMessage = in.ToString() + out.ToString();
41 
42  CKey key2;
43  CPubKey pubkey2;
44 
45  if(!CMessageSigner::GetKeysFromSecret(strSharedKey, key2, pubkey2)) {
46  LogPrintf("CDarkSendRelay::Sign -- GetKeysFromSecret() failed, invalid shared key %s\n", strSharedKey);
47  return false;
48  }
49 
50  if(!CMessageSigner::SignMessage(strMessage, vchSig2, key2)) {
51  LogPrintf("CDarkSendRelay::Sign -- SignMessage() failed\n");
52  return false;
53  }
54 
55  if(!CMessageSigner::VerifyMessage(pubkey2, vchSig2, strMessage, strError)) {
56  LogPrintf("CDarkSendRelay::Sign -- VerifyMessage() failed, error: %s\n", strError);
57  return false;
58  }
59 
60  return true;
61 }
62 
63 bool CDarkSendRelay::VerifyMessage(std::string strSharedKey)
64 {
65  std::string strError = "";
66  std::string strMessage = in.ToString() + out.ToString();
67 
68  CKey key2;
69  CPubKey pubkey2;
70 
71  if(!CMessageSigner::GetKeysFromSecret(strSharedKey, key2, pubkey2)) {
72  LogPrintf("CDarkSendRelay::VerifyMessage -- GetKeysFromSecret() failed, invalid shared key %s\n", strSharedKey);
73  return false;
74  }
75 
76  if(!CMessageSigner::VerifyMessage(pubkey2, vchSig2, strMessage, strError)) {
77  LogPrintf("CDarkSendRelay::VerifyMessage -- VerifyMessage() failed, error: %s\n", strError);
78  return false;
79  }
80 
81  return true;
82 }
83 
85 {
86  int nCount = std::min(mnodeman.CountEnabled(MIN_PRIVATESEND_PEER_PROTO_VERSION), 20);
87  int nRank1 = (rand() % nCount)+1;
88  int nRank2 = (rand() % nCount)+1;
89 
90  //keep picking another second number till we get one that doesn't match
91  while(nRank1 == nRank2) nRank2 = (rand() % nCount)+1;
92 
93  //printf("rank 1 - rank2 %d %d \n", nRank1, nRank2);
94 
95  //relay this message through 2 separate nodes for redundancy
96  RelayThroughNode(nRank1);
97  RelayThroughNode(nRank2);
98 }
99 
101 {
102  masternode_info_t mnInfo;
103 
105  //printf("RelayThroughNode %s\n", mnInfo.addr.ToString().c_str());
106  // TODO: Pass CConnman instance somehow and don't use global variable.
107  CNode* pnode = g_connman->ConnectNode((CAddress)mnInfo.addr, NULL);
108  if(pnode) {
109  //printf("Connected\n");
110  pnode->PushMessage("dsr", (*this));
111  return;
112  }
113  } else {
114  //printf("RelayThroughNode NULL\n");
115  }
116 }
CMasternodeMan mnodeman
vector< unsigned char > vchSig
void RelayThroughNode(int nRank)
static bool GetKeysFromSecret(const std::string strSecret, CKey &keyRet, CPubKey &pubkeyRet)
Set the private/public key values, returns true if successful.
#define LogPrintf(...)
Definition: util.h:98
Definition: net.h:661
std::string ToString() const
Definition: transaction.cpp:36
static bool VerifyMessage(const CPubKey pubkey, const std::vector< unsigned char > &vchSig, const std::string strMessage, std::string &strErrorRet)
Verify the message signature, returns true if succcessful.
bool GetMasternodeByRank(int nRank, masternode_info_t &mnInfoRet, int nBlockHeight=-1, int nMinProtocol=0)
int CountEnabled(int nProtocolVersion=-1)
std::string ToString()
std::string ToString() const
Definition: transaction.cpp:63
static const int MIN_PRIVATESEND_PEER_PROTO_VERSION
minimum peer version accepted by mixing pool
Definition: privatesend.h:25
std::unique_ptr< CConnman > g_connman
Definition: init.cpp:103
bool Sign(std::string strSharedKey)
Definition: pubkey.h:37
static bool SignMessage(const std::string strMessage, std::vector< unsigned char > &vchSigRet, const CKey key)
Sign the message, returns true if successful.
Definition: key.h:35
bool VerifyMessage(std::string strSharedKey)
vector< unsigned char > vchSig2