Line data Source code
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.
4 :
5 : #include "pubkey.h"
6 :
7 : #include "eccryptoverify.h"
8 :
9 : #include "ecwrapper.h"
10 :
11 4317 : bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
12 4317 : if (!IsValid())
13 : return false;
14 4317 : CECKey key;
15 12951 : if (!key.SetPubKey(begin(), size()))
16 : return false;
17 4317 : if (!key.Verify(hash, vchSig))
18 : return false;
19 4026 : return true;
20 : }
21 :
22 67 : bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
23 134 : if (vchSig.size() != 65)
24 : return false;
25 67 : int recid = (vchSig[0] - 27) & 3;
26 67 : bool fComp = ((vchSig[0] - 27) & 4) != 0;
27 67 : CECKey key;
28 134 : if (!key.Recover(hash, &vchSig[1], recid))
29 : return false;
30 : std::vector<unsigned char> pubkey;
31 67 : key.GetPubKey(pubkey, fComp);
32 67 : Set(pubkey.begin(), pubkey.end());
33 134 : return true;
34 : }
35 :
36 272 : bool CPubKey::IsFullyValid() const {
37 272 : if (!IsValid())
38 : return false;
39 272 : CECKey key;
40 816 : if (!key.SetPubKey(begin(), size()))
41 : return false;
42 272 : return true;
43 : }
44 :
45 7 : bool CPubKey::Decompress() {
46 7 : if (!IsValid())
47 : return false;
48 7 : CECKey key;
49 21 : if (!key.SetPubKey(begin(), size()))
50 : return false;
51 : std::vector<unsigned char> pubkey;
52 7 : key.GetPubKey(pubkey, false);
53 7 : Set(pubkey.begin(), pubkey.end());
54 14 : return true;
55 : }
56 :
57 8 : bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
58 8 : assert(IsValid());
59 8 : assert((nChild >> 31) == 0);
60 16 : assert(begin() + 33 == end());
61 : unsigned char out[64];
62 8 : BIP32Hash(cc, nChild, *begin(), begin()+1, out);
63 16 : memcpy(ccChild.begin(), out+32, 32);
64 8 : CECKey key;
65 24 : bool ret = key.SetPubKey(begin(), size());
66 8 : ret &= key.TweakPublic(out);
67 : std::vector<unsigned char> pubkey;
68 8 : key.GetPubKey(pubkey, true);
69 8 : pubkeyChild.Set(pubkey.begin(), pubkey.end());
70 16 : return ret;
71 : }
72 :
73 24 : void CExtPubKey::Encode(unsigned char code[74]) const {
74 24 : code[0] = nDepth;
75 24 : memcpy(code+1, vchFingerprint, 4);
76 24 : code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
77 24 : code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
78 48 : memcpy(code+9, chaincode.begin(), 32);
79 48 : assert(pubkey.size() == 33);
80 48 : memcpy(code+41, pubkey.begin(), 33);
81 24 : }
82 :
83 12 : void CExtPubKey::Decode(const unsigned char code[74]) {
84 12 : nDepth = code[0];
85 12 : memcpy(vchFingerprint, code+1, 4);
86 12 : nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
87 24 : memcpy(chaincode.begin(), code+9, 32);
88 12 : pubkey.Set(code+41, code+74);
89 12 : }
90 :
91 8 : bool CExtPubKey::Derive(CExtPubKey &out, unsigned int nChild) const {
92 8 : out.nDepth = nDepth + 1;
93 8 : CKeyID id = pubkey.GetID();
94 8 : memcpy(&out.vchFingerprint[0], &id, 4);
95 8 : out.nChild = nChild;
96 8 : return pubkey.Derive(out.pubkey, out.chaincode, nChild, chaincode);
97 : }
|