Пример #1
0
        private bool CheckSig(byte[] vchSig, byte[] vchPubKey, Script scriptCode, Transaction txTo, int nIn)
        {
            //static CSignatureCache signatureCache;
            if (!PubKey.IsValidSize(vchPubKey.Length))
            {
                return(false);
            }
            PubKey pubkey = null;

            try
            {
                pubkey = new PubKey(vchPubKey);
            }
            catch (Exception)
            {
                return(false);
            }


            // Hash type is one byte tacked on to the end of the signature
            if (vchSig.Length == 0)
            {
                return(false);
            }

            var scriptSig = new TransactionSignature(vchSig);

            if (!IsAllowedSignature(scriptSig.SigHash))
            {
                return(false);
            }

            uint256 sighash = scriptCode.SignatureHash(txTo, nIn, scriptSig.SigHash);

            //if (signatureCache.Get(sighash, vchSig, pubkey))
            //	return true;

            if (!pubkey.Verify(sighash, scriptSig.Signature))
            {
                if ((ScriptVerify & ScriptVerify.StrictEnc) != 0)
                {
                    return(false);
                }

                //Replicate OpenSSL bug on 23b397edccd3740a74adb603c9756370fafcde9bcc4483eb271ecad09a94dd63 (http://r6.ca/blog/20111119T211504Z.html)
                var nLenR = vchSig[3];
                var nLenS = vchSig[5 + nLenR];
                var R     = 4;
                var S     = 6 + nLenR;
                var newS  = new Org.BouncyCastle.Math.BigInteger(1, vchSig, S, nLenS);
                var newR  = new Org.BouncyCastle.Math.BigInteger(1, vchSig, R, nLenR);
                var sig2  = new ECDSASignature(newR, newS);
                if (sig2.R != scriptSig.Signature.R || sig2.S != scriptSig.Signature.S)
                {
                    if (!pubkey.Verify(sighash, sig2))
                    {
                        return(false);
                    }
                }
            }

            //if (!(flags & SCRIPT_VERIFY_NOCACHE))
            //	signatureCache.Set(sighash, vchSig, pubkey);

            return(true);
        }