1 #include <qtum/qtumutils.h>
2 #include <libdevcore/CommonData.h>
3 #include <pubkey.h>
4 #include <util/convert.h>
5
6 using namespace dev;
7
btc_ecrecover(const dev::h256 & hash,const dev::u256 & v,const dev::h256 & r,const dev::h256 & s,dev::h256 & key)8 bool qtumutils::btc_ecrecover(const dev::h256 &hash, const dev::u256 &v, const dev::h256 &r, const dev::h256 &s, dev::h256 &key)
9 {
10 // Check input parameters
11 if(v >= 256)
12 {
13 // Does not fit into 1 byte
14 return false;
15 }
16
17 // Convert the data into format usable for btc
18 CPubKey pubKey;
19 std::vector<unsigned char> vchSig;
20 vchSig.push_back((unsigned char)v);
21 vchSig += r.asBytes();
22 vchSig += s.asBytes();
23 uint256 mesage = h256Touint(hash);
24
25 // Recover public key from compact signature (65 bytes)
26 // The public key can be compressed (33 bytes) or uncompressed (65 bytes)
27 // Pubkeyhash is RIPEMD160 hash of the public key, handled both types
28 if(pubKey.RecoverCompact(mesage, vchSig))
29 {
30 // Get the pubkeyhash
31 CKeyID id = pubKey.GetID();
32 size_t padding = sizeof(key) - sizeof(id);
33 memset(key.data(), 0, padding);
34 memcpy(key.data() + padding, id.begin(), sizeof(id));
35 return true;
36 }
37
38 return false;
39 }
40