Improve blocks signing procedure
authoralex <alex@alex-VirtualBox.(none)>
Sun, 17 Feb 2013 22:40:07 +0000 (02:40 +0400)
committeralex <alex@alex-VirtualBox.(none)>
Sun, 17 Feb 2013 22:40:07 +0000 (02:40 +0400)
src/main.cpp

index c02298b..8a76b03 100644 (file)
@@ -2135,21 +2135,57 @@ bool CBlock::SignBlock(const CKeyStore& keystore)
 {
     vector<valtype> vSolutions;
     txnouttype whichType;
-    const CTxOut& txout = IsProofOfStake()? vtx[1].vout[1] : vtx[0].vout[0];
+    int nVouts = nTime < 1361664000 ? 1 : vtx[0].vout.size();
 
-    if (!Solver(txout.scriptPubKey, whichType, vSolutions))
-        return false;
-    if (whichType == TX_PUBKEY)
+    if(!IsProofOfStake())
     {
-        // Sign
-        const valtype& vchPubKey = vSolutions[0];
-        CKey key;
-        if (!keystore.GetKey(Hash160(vchPubKey), key))
-            return false;
-        if (key.GetPubKey() != vchPubKey)
+        for(int i = 0; i < nVouts; i++)
+        {
+            const CTxOut& txout = vtx[0].vout[i];
+
+            if (!Solver(txout.scriptPubKey, whichType, vSolutions))
+                continue;
+
+            if (whichType == TX_PUBKEY)
+            {
+                // Sign
+                valtype& vchPubKey = vSolutions[0];
+                CKey key;
+
+                if (!keystore.GetKey(Hash160(vchPubKey), key))
+                    continue;
+                if (key.GetPubKey() != vchPubKey)
+                    continue;
+                if(!key.Sign(GetHash(), vchBlockSig))
+                    continue;
+
+                return true;
+            }
+        }
+    }
+    else
+    {
+        const CTxOut& txout = vtx[1].vout[1];
+
+        if (!Solver(txout.scriptPubKey, whichType, vSolutions))
             return false;
-        return key.Sign(GetHash(), vchBlockSig);
+
+        if (whichType == TX_PUBKEY)
+        {
+            // Sign
+            valtype& vchPubKey = vSolutions[0];
+            CKey key;
+
+            if (!keystore.GetKey(Hash160(vchPubKey), key))
+                return false;
+            if (key.GetPubKey() != vchPubKey)
+                return false;
+
+            return key.Sign(GetHash(), vchBlockSig);
+        }
     }
+
+    printf("Sign failed\n");
     return false;
 }
 
@@ -2161,26 +2197,52 @@ bool CBlock::CheckBlockSignature() const
 
     vector<valtype> vSolutions;
     txnouttype whichType;
-    const CTxOut& txout = IsProofOfStake()? vtx[1].vout[1] : vtx[0].vout[0];
+    int nVouts = nTime < 1361664000 ? 1 : vtx[0].vout.size();
 
-    if (!Solver(txout.scriptPubKey, whichType, vSolutions))
-        return false;
-    if (whichType == TX_PUBKEY)
+    if(IsProofOfStake())
     {
-        const valtype& vchPubKey = vSolutions[0];
-        CKey key;
-        if (!key.SetPubKey(vchPubKey))
-            return false;
-        if (vchBlockSig.empty())
+        const CTxOut& txout = vtx[1].vout[1];
+
+        if (!Solver(txout.scriptPubKey, whichType, vSolutions))
             return false;
-        return key.Verify(GetHash(), vchBlockSig);
+        if (whichType == TX_PUBKEY)
+        {
+            valtype& vchPubKey = vSolutions[0];
+            CKey key;
+            if (!key.SetPubKey(vchPubKey))
+                return false;
+            if (vchBlockSig.empty())
+                return false;
+            return key.Verify(GetHash(), vchBlockSig);
+        }
     }
-    return false;
-}
-
+    else
+    {
+        for(int i = 0; i < nVouts; i++)
+        {
+            const CTxOut& txout = vtx[0].vout[i];
 
+            if (!Solver(txout.scriptPubKey, whichType, vSolutions))
+                return false;
 
+            if (whichType == TX_PUBKEY)
+            {
+                // Verify
+                valtype& vchPubKey = vSolutions[0];
+                CKey key;
+                if (!key.SetPubKey(vchPubKey))
+                    continue;
+                if (vchBlockSig.empty())
+                    continue;
+                if(!key.Verify(GetHash(), vchBlockSig))
+                    continue;
 
+                return true;
+            }
+        }
+    }
+    return false;
+}
 
 
 bool CheckDiskSpace(uint64 nAdditionalBytes)