From 03fbd7904903928b0d1c8542a3d597aaf5bdd31b Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 5 Jul 2011 16:42:32 +0200 Subject: get rid of mapPubKeys Make CKeyStore's interface work on uint160's instead of pubkeys, so no separate global mapPubKeys is necessary anymore. --- src/script.cpp | 64 ++++++++++++++++------------------------------------------ 1 file changed, 17 insertions(+), 47 deletions(-) (limited to 'src/script.cpp') diff --git a/src/script.cpp b/src/script.cpp index 654aaa10e3..b92f7b08b6 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1041,7 +1041,9 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash // Sign const valtype& vchPubKey = item.second; CKey key; - if (!keystore.GetPrivKey(vchPubKey, key)) + if (!keystore.GetKey(Hash160(vchPubKey), key)) + return false; + if (key.GetPubKey() != vchPubKey) return false; if (hash != 0) { @@ -1055,12 +1057,8 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash else if (item.first == OP_PUBKEYHASH) { // Sign and give pubkey - map::iterator mi = mapPubKeys.find(uint160(item.second)); - if (mi == mapPubKeys.end()) - return false; - const vector& vchPubKey = (*mi).second; CKey key; - if (!keystore.GetPrivKey(vchPubKey, key)) + if (!keystore.GetKey(uint160(item.second), key)) return false; if (hash != 0) { @@ -1068,7 +1066,7 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash if (!key.Sign(hash, vchSig)) return false; vchSig.push_back((unsigned char)nHashType); - scriptSigRet << vchSig << vchPubKey; + scriptSigRet << vchSig << key.GetPubKey(); } } else @@ -1102,19 +1100,16 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) { if (item.first == OP_PUBKEY) { - // Sign const valtype& vchPubKey = item.second; - if (!keystore.HaveKey(vchPubKey)) + vector vchPubKeyFound; + if (!keystore.GetPubKey(Hash160(vchPubKey), vchPubKeyFound)) + return false; + if (vchPubKeyFound != vchPubKey) return false; } else if (item.first == OP_PUBKEYHASH) { - // Sign and give pubkey - map::iterator mi = mapPubKeys.find(uint160(item.second)); - if (mi == mapPubKeys.end()) - return false; - const vector& vchPubKey = (*mi).second; - if (!keystore.HaveKey(vchPubKey)) + if (!keystore.HaveKey(uint160(item.second))) return false; } else @@ -1128,33 +1123,28 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) } -bool ExtractPubKey(const CScript& scriptPubKey, const CKeyStore* keystore, vector& vchPubKeyRet) +bool ExtractHash160(const CScript& scriptPubKey, const CKeyStore* keystore, uint160& hash160Ret) { - vchPubKeyRet.clear(); - vector > vSolution; if (!Solver(scriptPubKey, vSolution)) return false; - CRITICAL_BLOCK(cs_mapPubKeys) + CRITICAL_BLOCK(keystore->cs_KeyStore) { BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution) { - valtype vchPubKey; + uint160 hash160; if (item.first == OP_PUBKEY) { - vchPubKey = item.second; + hash160 = Hash160(item.second); } else if (item.first == OP_PUBKEYHASH) { - map::iterator mi = mapPubKeys.find(uint160(item.second)); - if (mi == mapPubKeys.end()) - continue; - vchPubKey = (*mi).second; + hash160 = uint160(item.second); } - if (keystore == NULL || keystore->HaveKey(vchPubKey)) + if (keystore == NULL || keystore->HaveKey(hash160)) { - vchPubKeyRet = vchPubKey; + hash160Ret = hash160; return true; } } @@ -1163,26 +1153,6 @@ bool ExtractPubKey(const CScript& scriptPubKey, const CKeyStore* keystore, vecto } -bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret) -{ - hash160Ret = 0; - - vector > vSolution; - if (!Solver(scriptPubKey, vSolution)) - return false; - - BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution) - { - if (item.first == OP_PUBKEYHASH) - { - hash160Ret = uint160(item.second); - return true; - } - } - return false; -} - - bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int nHashType) { vector > stack; -- cgit v1.2.3 From 2ffba736e9102d016b96c2e5de2ce7757e612667 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 5 Jul 2011 20:53:43 +0200 Subject: Use CBitcoinAddress instead of string/uint160 Instead of conversion functions between pubkey/uint160/address in base58.h, have a fully fledged class CBitcoinAddress (CAddress was already taken) to represent addresses. --- src/script.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'src/script.cpp') diff --git a/src/script.cpp b/src/script.cpp index b92f7b08b6..f917600848 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1123,7 +1123,7 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) } -bool ExtractHash160(const CScript& scriptPubKey, const CKeyStore* keystore, uint160& hash160Ret) +bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet) { vector > vSolution; if (!Solver(scriptPubKey, vSolution)) @@ -1135,18 +1135,11 @@ bool ExtractHash160(const CScript& scriptPubKey, const CKeyStore* keystore, uint { uint160 hash160; if (item.first == OP_PUBKEY) - { - hash160 = Hash160(item.second); - } + addressRet.SetAddress(item.second); else if (item.first == OP_PUBKEYHASH) - { - hash160 = uint160(item.second); - } - if (keystore == NULL || keystore->HaveKey(hash160)) - { - hash160Ret = hash160; + addressRet.SetAddress(uint160(item.second)); + if (keystore == NULL || keystore->HaveKey(addressRet)) return true; - } } } return false; -- cgit v1.2.3 From cb61b8dc4ce2f24332fdfe9b47e5f87905a9da71 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 11 Jul 2011 11:09:00 +0200 Subject: split off CBase58Data from CBitcoinAddress Split off features unrelated to addresses from CBitcoinAddress to CBase58Data, so they can be reused. --- src/script.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/script.cpp') diff --git a/src/script.cpp b/src/script.cpp index f917600848..652240f686 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1135,9 +1135,9 @@ bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBit { uint160 hash160; if (item.first == OP_PUBKEY) - addressRet.SetAddress(item.second); + addressRet.SetPubKey(item.second); else if (item.first == OP_PUBKEYHASH) - addressRet.SetAddress(uint160(item.second)); + addressRet.SetHash160((uint160)item.second); if (keystore == NULL || keystore->HaveKey(addressRet)) return true; } -- cgit v1.2.3