Add UpdatedBlockTip signal to CMainSignals and CValidationInterface
[bitcoinplatinum.git] / src / pubkey.cpp
blobbdab1376000ecab38bf2305bfa3f88c0bc17e045
1 // Copyright (c) 2009-2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "pubkey.h"
7 #include "eccryptoverify.h"
9 #include "ecwrapper.h"
11 bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
12 if (!IsValid())
13 return false;
14 CECKey key;
15 if (!key.SetPubKey(begin(), size()))
16 return false;
17 if (!key.Verify(hash, vchSig))
18 return false;
19 return true;
22 bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
23 if (vchSig.size() != 65)
24 return false;
25 int recid = (vchSig[0] - 27) & 3;
26 bool fComp = ((vchSig[0] - 27) & 4) != 0;
27 CECKey key;
28 if (!key.Recover(hash, &vchSig[1], recid))
29 return false;
30 std::vector<unsigned char> pubkey;
31 key.GetPubKey(pubkey, fComp);
32 Set(pubkey.begin(), pubkey.end());
33 return true;
36 bool CPubKey::IsFullyValid() const {
37 if (!IsValid())
38 return false;
39 CECKey key;
40 if (!key.SetPubKey(begin(), size()))
41 return false;
42 return true;
45 bool CPubKey::Decompress() {
46 if (!IsValid())
47 return false;
48 CECKey key;
49 if (!key.SetPubKey(begin(), size()))
50 return false;
51 std::vector<unsigned char> pubkey;
52 key.GetPubKey(pubkey, false);
53 Set(pubkey.begin(), pubkey.end());
54 return true;
57 bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
58 assert(IsValid());
59 assert((nChild >> 31) == 0);
60 assert(begin() + 33 == end());
61 unsigned char out[64];
62 BIP32Hash(cc, nChild, *begin(), begin()+1, out);
63 memcpy(ccChild.begin(), out+32, 32);
64 CECKey key;
65 bool ret = key.SetPubKey(begin(), size());
66 ret &= key.TweakPublic(out);
67 std::vector<unsigned char> pubkey;
68 key.GetPubKey(pubkey, true);
69 pubkeyChild.Set(pubkey.begin(), pubkey.end());
70 return ret;
73 void CExtPubKey::Encode(unsigned char code[74]) const {
74 code[0] = nDepth;
75 memcpy(code+1, vchFingerprint, 4);
76 code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
77 code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
78 memcpy(code+9, chaincode.begin(), 32);
79 assert(pubkey.size() == 33);
80 memcpy(code+41, pubkey.begin(), 33);
83 void CExtPubKey::Decode(const unsigned char code[74]) {
84 nDepth = code[0];
85 memcpy(vchFingerprint, code+1, 4);
86 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
87 memcpy(chaincode.begin(), code+9, 32);
88 pubkey.Set(code+41, code+74);
91 bool CExtPubKey::Derive(CExtPubKey &out, unsigned int nChild) const {
92 out.nDepth = nDepth + 1;
93 CKeyID id = pubkey.GetID();
94 memcpy(&out.vchFingerprint[0], &id, 4);
95 out.nChild = nChild;
96 return pubkey.Derive(out.pubkey, out.chaincode, nChild, chaincode);