Skip to content

Commit

Permalink
Add HashToAddress() helper to determine Bitcoin address from hash160 …
Browse files Browse the repository at this point in the history
…& version
  • Loading branch information
zathras-crypto committed Dec 11, 2017
1 parent 74b9f9e commit 454dfca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/omnicore/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

#include "omnicore/utils.h"

#include "base58.h"
#include "utilstrencodings.h"

// TODO: use crypto/sha256 instead of openssl
#include "openssl/sha.h"

#include "omnicore/script.h"

#include <boost/algorithm/string.hpp>

#include <assert.h>
Expand Down Expand Up @@ -55,3 +58,19 @@ void PrepareObfuscatedHashes(const std::string& strSeed, int hashCount, std::str
strcpy((char *)sha_input, vstrHashes[j].c_str());
}
}

std::string HashToAddress(unsigned char version, uint160 hash)
{
CBitcoinAddress address;
if (version == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS)[0]) {
CKeyID keyId = hash;
address.Set(keyId);
return address.ToString();
} else if (version == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS)[0]) {
CScriptID scriptId = hash;
address.Set(scriptId);
return address.ToString();
}

return "";
}
4 changes: 4 additions & 0 deletions src/omnicore/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

#include <string>

#include "uint256.h"

#define MAX_SHA256_OBFUSCATION_TIMES 255

/** Generates hashes used for obfuscation via ToUpper(HexStr(SHA256(x))). */
void PrepareObfuscatedHashes(const std::string& strSeed, int hashCount, std::string(&vstrHashes)[1+MAX_SHA256_OBFUSCATION_TIMES]);

/** Determines the Bitcoin address associated with a given hash and version. */
std::string HashToAddress(unsigned char version, uint160 hash);

#endif // OMNICORE_UTILS_H

0 comments on commit 454dfca

Please sign in to comment.