]>
Commit | Line | Data |
---|---|---|
f914f1a7 | 1 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
b4347f60 | 2 | // Distributed under the MIT software license, see the accompanying |
d2e74c55 CF |
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | ||
5 | #include "pubkey.h" | |
6 | ||
d2e74c55 CF |
7 | #include "eccryptoverify.h" |
8 | ||
d2e74c55 | 9 | #include "ecwrapper.h" |
d2e74c55 CF |
10 | |
11 | bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const { | |
12 | if (!IsValid()) | |
13 | return false; | |
d2e74c55 CF |
14 | CECKey key; |
15 | if (!key.SetPubKey(begin(), size())) | |
16 | return false; | |
17 | if (!key.Verify(hash, vchSig)) | |
18 | return false; | |
d2e74c55 CF |
19 | return true; |
20 | } | |
21 | ||
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; | |
d2e74c55 CF |
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()); | |
d2e74c55 CF |
33 | return true; |
34 | } | |
35 | ||
36 | bool CPubKey::IsFullyValid() const { | |
37 | if (!IsValid()) | |
38 | return false; | |
d2e74c55 CF |
39 | CECKey key; |
40 | if (!key.SetPubKey(begin(), size())) | |
41 | return false; | |
d2e74c55 CF |
42 | return true; |
43 | } | |
44 | ||
45 | bool CPubKey::Decompress() { | |
46 | if (!IsValid()) | |
47 | return false; | |
d2e74c55 CF |
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()); | |
d2e74c55 CF |
54 | return true; |
55 | } | |
56 | ||
a5748996 | 57 | bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const { |
d2e74c55 CF |
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); | |
a5748996 | 63 | memcpy(ccChild.begin(), out+32, 32); |
d2e74c55 CF |
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()); | |
d2e74c55 CF |
70 | return ret; |
71 | } | |
72 | ||
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; | |
a5748996 | 78 | memcpy(code+9, chaincode.begin(), 32); |
d2e74c55 CF |
79 | assert(pubkey.size() == 33); |
80 | memcpy(code+41, pubkey.begin(), 33); | |
81 | } | |
82 | ||
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]; | |
a5748996 | 87 | memcpy(chaincode.begin(), code+9, 32); |
d2e74c55 CF |
88 | pubkey.Set(code+41, code+74); |
89 | } | |
90 | ||
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; | |
a5748996 | 96 | return pubkey.Derive(out.pubkey, out.chaincode, nChild, chaincode); |
d2e74c55 | 97 | } |