Mercurial > hg > bitcoin
annotate src/keystore.cpp @ 3644:e62eeca19443 draft
Add CTxUndo: transaction undo information
The CTxUndo class encapsulates data necessary to undo the effects of
a transaction on the txout set, namely the previous outputs consumed
by it (script + amount), and potentially transaction meta-data when
it is spent entirely.
author | Pieter Wuille <pieter.wuille@gmail.com> |
---|---|
date | Mon, 18 Jun 2012 16:55:29 +0200 |
parents | 2c65e5d626eb |
children |
rev | line source |
---|---|
846 | 1 // Copyright (c) 2009-2010 Satoshi Nakamoto |
1818
20667468f95b
Update all copyrights to 2012
Gavin Andresen <gavinandresen@gmail.com>
parents:
1752
diff
changeset
|
2 // Copyright (c) 2009-2012 The Bitcoin developers |
689
9df222fa1ef1
move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents:
diff
changeset
|
3 // Distributed under the MIT/X11 software license, see the accompanying |
2607 | 4 // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
689
9df222fa1ef1
move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents:
diff
changeset
|
5 |
2274 | 6 #include "keystore.h" |
1604
2d635cbb6a2d
Use block times for 'hard' OP_EVAL switchover, and refactored EvalScript
Gavin Andresen <gavinandresen@gmail.com>
parents:
1600
diff
changeset
|
7 #include "script.h" |
689
9df222fa1ef1
move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents:
diff
changeset
|
8 |
2688
2c65e5d626eb
Refactor: split CKeyID/CScriptID/CTxDestination from CBitcoinAddress
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2687
diff
changeset
|
9 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
10 { |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
11 CKey key; |
814
346b6b1b54f3
Use CBitcoinAddress instead of string/uint160
Pieter Wuille <pieter.wuille@gmail.com>
parents:
813
diff
changeset
|
12 if (!GetKey(address, key)) |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
13 return false; |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
14 vchPubKeyOut = key.GetPubKey(); |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
15 return true; |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
16 } |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
17 |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
18 bool CBasicKeyStore::AddKey(const CKey& key) |
689
9df222fa1ef1
move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents:
diff
changeset
|
19 { |
1734
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
20 bool fCompressed = false; |
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
21 CSecret secret = key.GetSecret(fCompressed); |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
22 { |
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
23 LOCK(cs_KeyStore); |
2688
2c65e5d626eb
Refactor: split CKeyID/CScriptID/CTxDestination from CBitcoinAddress
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2687
diff
changeset
|
24 mapKeys[key.GetPubKey().GetID()] = make_pair(secret, fCompressed); |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
25 } |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
26 return true; |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
27 } |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
28 |
1752
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1734
diff
changeset
|
29 bool CBasicKeyStore::AddCScript(const CScript& redeemScript) |
1600
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
30 { |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
31 { |
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
32 LOCK(cs_KeyStore); |
2688
2c65e5d626eb
Refactor: split CKeyID/CScriptID/CTxDestination from CBitcoinAddress
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2687
diff
changeset
|
33 mapScripts[redeemScript.GetID()] = redeemScript; |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
34 } |
1600
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
35 return true; |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
36 } |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
37 |
2688
2c65e5d626eb
Refactor: split CKeyID/CScriptID/CTxDestination from CBitcoinAddress
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2687
diff
changeset
|
38 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const |
1600
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
39 { |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
40 bool result; |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
41 { |
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
42 LOCK(cs_KeyStore); |
1604
2d635cbb6a2d
Use block times for 'hard' OP_EVAL switchover, and refactored EvalScript
Gavin Andresen <gavinandresen@gmail.com>
parents:
1600
diff
changeset
|
43 result = (mapScripts.count(hash) > 0); |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
44 } |
1600
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
45 return result; |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
46 } |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
47 |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
48 |
2688
2c65e5d626eb
Refactor: split CKeyID/CScriptID/CTxDestination from CBitcoinAddress
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2687
diff
changeset
|
49 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const |
1600
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
50 { |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
51 { |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
52 LOCK(cs_KeyStore); |
1604
2d635cbb6a2d
Use block times for 'hard' OP_EVAL switchover, and refactored EvalScript
Gavin Andresen <gavinandresen@gmail.com>
parents:
1600
diff
changeset
|
53 ScriptMap::const_iterator mi = mapScripts.find(hash); |
2d635cbb6a2d
Use block times for 'hard' OP_EVAL switchover, and refactored EvalScript
Gavin Andresen <gavinandresen@gmail.com>
parents:
1600
diff
changeset
|
54 if (mi != mapScripts.end()) |
1600
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
55 { |
1604
2d635cbb6a2d
Use block times for 'hard' OP_EVAL switchover, and refactored EvalScript
Gavin Andresen <gavinandresen@gmail.com>
parents:
1600
diff
changeset
|
56 redeemScriptOut = (*mi).second; |
1600
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
57 return true; |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
58 } |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
59 } |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
60 return false; |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
61 } |
dc663e46a784
OP_EVAL implementation
Gavin Andresen <gavinandresen@gmail.com>
parents:
1580
diff
changeset
|
62 |
896
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
63 bool CCryptoKeyStore::SetCrypted() |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
64 { |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
65 { |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
66 LOCK(cs_KeyStore); |
896
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
67 if (fUseCrypto) |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
68 return true; |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
69 if (!mapKeys.empty()) |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
70 return false; |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
71 fUseCrypto = true; |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
72 } |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
73 return true; |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
74 } |
03e517c58ffe
SetCrypted() obtains keystore lock, to be safe.
Gavin Andresen <gavinandresen@gmail.com>
parents:
892
diff
changeset
|
75 |
2639
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
76 bool CCryptoKeyStore::Lock() |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
77 { |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
78 if (!SetCrypted()) |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
79 return false; |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
80 |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
81 { |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
82 LOCK(cs_KeyStore); |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
83 vMasterKey.clear(); |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
84 } |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
85 |
2640
60b19c042950
Convert UI interface to boost::signals2.
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2639
diff
changeset
|
86 NotifyStatusChanged(this); |
2639
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
87 return true; |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
88 } |
c4ef3fe2ae15
Fine-grained UI updates
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2277
diff
changeset
|
89 |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
90 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
91 { |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
92 { |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
93 LOCK(cs_KeyStore); |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
94 if (!SetCrypted()) |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
95 return false; |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
96 |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
97 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin(); |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
98 for (; mi != mapCryptedKeys.end(); ++mi) |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
99 { |
2687
2cca172f83a9
Encapsulate public keys in CPubKey
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2646
diff
changeset
|
100 const CPubKey &vchPubKey = (*mi).second.first; |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
101 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second; |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
102 CSecret vchSecret; |
2687
2cca172f83a9
Encapsulate public keys in CPubKey
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2646
diff
changeset
|
103 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
104 return false; |
2162
d5e317f83474
Verify status of encrypt/decrypt calls to detect failed padding
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1874
diff
changeset
|
105 if (vchSecret.size() != 32) |
d5e317f83474
Verify status of encrypt/decrypt calls to detect failed padding
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1874
diff
changeset
|
106 return false; |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
107 CKey key; |
1734
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
108 key.SetPubKey(vchPubKey); |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
109 key.SetSecret(vchSecret); |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
110 if (key.GetPubKey() == vchPubKey) |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
111 break; |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
112 return false; |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
113 } |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
114 vMasterKey = vMasterKeyIn; |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
115 } |
2640
60b19c042950
Convert UI interface to boost::signals2.
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2639
diff
changeset
|
116 NotifyStatusChanged(this); |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
117 return true; |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
118 } |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
119 |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
120 bool CCryptoKeyStore::AddKey(const CKey& key) |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
121 { |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
122 { |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
123 LOCK(cs_KeyStore); |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
124 if (!IsCrypted()) |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
125 return CBasicKeyStore::AddKey(key); |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
126 |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
127 if (IsLocked()) |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
128 return false; |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
129 |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
130 std::vector<unsigned char> vchCryptedSecret; |
2687
2cca172f83a9
Encapsulate public keys in CPubKey
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2646
diff
changeset
|
131 CPubKey vchPubKey = key.GetPubKey(); |
1734
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
132 bool fCompressed; |
2687
2cca172f83a9
Encapsulate public keys in CPubKey
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2646
diff
changeset
|
133 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret)) |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
134 return false; |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
135 |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
136 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret)) |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
137 return false; |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
138 } |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
139 return true; |
689
9df222fa1ef1
move wallet code to separate file
Pieter Wuille <pieter.wuille@gmail.com>
parents:
diff
changeset
|
140 } |
690 | 141 |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
142 |
2687
2cca172f83a9
Encapsulate public keys in CPubKey
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2646
diff
changeset
|
143 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
144 { |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
145 { |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
146 LOCK(cs_KeyStore); |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
147 if (!SetCrypted()) |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
148 return false; |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
149 |
2688
2c65e5d626eb
Refactor: split CKeyID/CScriptID/CTxDestination from CBitcoinAddress
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2687
diff
changeset
|
150 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret); |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
151 } |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
152 return true; |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
153 } |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
154 |
2688
2c65e5d626eb
Refactor: split CKeyID/CScriptID/CTxDestination from CBitcoinAddress
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2687
diff
changeset
|
155 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
156 { |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
157 { |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
158 LOCK(cs_KeyStore); |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
159 if (!IsCrypted()) |
1734
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
160 return CBasicKeyStore::GetKey(address, keyOut); |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
161 |
814
346b6b1b54f3
Use CBitcoinAddress instead of string/uint160
Pieter Wuille <pieter.wuille@gmail.com>
parents:
813
diff
changeset
|
162 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
163 if (mi != mapCryptedKeys.end()) |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
164 { |
2687
2cca172f83a9
Encapsulate public keys in CPubKey
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2646
diff
changeset
|
165 const CPubKey &vchPubKey = (*mi).second.first; |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
166 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second; |
1734
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
167 CSecret vchSecret; |
2687
2cca172f83a9
Encapsulate public keys in CPubKey
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2646
diff
changeset
|
168 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) |
1734
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
169 return false; |
2162
d5e317f83474
Verify status of encrypt/decrypt calls to detect failed padding
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1874
diff
changeset
|
170 if (vchSecret.size() != 32) |
d5e317f83474
Verify status of encrypt/decrypt calls to detect failed padding
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1874
diff
changeset
|
171 return false; |
1734
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
172 keyOut.SetPubKey(vchPubKey); |
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
173 keyOut.SetSecret(vchSecret); |
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
174 return true; |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
175 } |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
176 } |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
177 return false; |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
178 } |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
179 |
2688
2c65e5d626eb
Refactor: split CKeyID/CScriptID/CTxDestination from CBitcoinAddress
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2687
diff
changeset
|
180 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
181 { |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
182 { |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
183 LOCK(cs_KeyStore); |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
184 if (!IsCrypted()) |
814
346b6b1b54f3
Use CBitcoinAddress instead of string/uint160
Pieter Wuille <pieter.wuille@gmail.com>
parents:
813
diff
changeset
|
185 return CKeyStore::GetPubKey(address, vchPubKeyOut); |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
186 |
814
346b6b1b54f3
Use CBitcoinAddress instead of string/uint160
Pieter Wuille <pieter.wuille@gmail.com>
parents:
813
diff
changeset
|
187 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
188 if (mi != mapCryptedKeys.end()) |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
189 { |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
190 vchPubKeyOut = (*mi).second.first; |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
191 return true; |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
192 } |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
193 } |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
194 return false; |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
195 } |
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
196 |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
197 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
198 { |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
199 { |
2190
60fe81a4849b
Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2162
diff
changeset
|
200 LOCK(cs_KeyStore); |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
201 if (!mapCryptedKeys.empty() || IsCrypted()) |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
202 return false; |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
203 |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
204 fUseCrypto = true; |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
205 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys) |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
206 { |
1734
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
207 CKey key; |
1829
081808d84018
Fix wallet encryption with compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1818
diff
changeset
|
208 if (!key.SetSecret(mKey.second.first, mKey.second.second)) |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
209 return false; |
2687
2cca172f83a9
Encapsulate public keys in CPubKey
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2646
diff
changeset
|
210 const CPubKey vchPubKey = key.GetPubKey(); |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
211 std::vector<unsigned char> vchCryptedSecret; |
1734
adac2d60ce43
Compressed pubkeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1616
diff
changeset
|
212 bool fCompressed; |
2687
2cca172f83a9
Encapsulate public keys in CPubKey
Pieter Wuille <pieter.wuille@gmail.com>
parents:
2646
diff
changeset
|
213 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret)) |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
214 return false; |
813
62b46930aaa4
get rid of mapPubKeys
Pieter Wuille <pieter.wuille@gmail.com>
parents:
766
diff
changeset
|
215 if (!AddCryptedKey(vchPubKey, vchCryptedSecret)) |
760
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
216 return false; |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
217 } |
fd7e5b1cfc08
Add wallet privkey encryption.
Matt Corallo <matt@bluematt.me>
parents:
757
diff
changeset
|
218 mapKeys.clear(); |
757
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
219 } |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
220 return true; |
ee9183806240
Prepare codebase for Encrypted Keys.
Pieter Wuille <pieter.wuille@gmail.com>
parents:
690
diff
changeset
|
221 } |