/// <summary>
/// Does the signature matches our public key?
/// </summary>
- /// <param name="data">Data bytes</param>
+ /// <param name="sigHash">Data hash</param>
/// <param name="signature">Signature bytes</param>
/// <returns>Checking result</returns>
- public bool VerifySignature(IEnumerable<byte> data, IEnumerable<byte> signature)
+ public bool VerifySignature(Hash256 sigHash, IEnumerable<byte> signature)
{
- byte[] dataBytes = data.ToArray();
-
ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA");
signer.Init(false, _Public);
- signer.BlockUpdate(dataBytes, 0, dataBytes.Length);
+ signer.BlockUpdate(sigHash.hashBytes, 0, sigHash.hashSize);
return signer.VerifySignature(signature.ToArray());
}
/// <summary>
/// Create signature for supplied data
/// </summary>
- /// <param name="data">Data bytes sequence</param>
+ /// <param name="data">Hash to sigh</param>
/// <returns>Signature bytes sequence</returns>
- public IEnumerable<byte> Sign(IEnumerable<byte> data)
+ public IEnumerable<byte> Sign(Hash256 sigHash)
{
- byte[] dataBytes = data.ToArray();
-
ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA");
signer.Init(true, _Private);
- signer.BlockUpdate(dataBytes, 0, dataBytes.Length);
+ signer.BlockUpdate(sigHash.hashBytes, 0, sigHash.hashSize);
return signer.GenerateSignature();
}
/// </summary>
/// <param name="opcode">Small integer opcode (OP_1_NEGATE and OP_0 - OP_16)</param>
/// <returns>Small integer</returns>
- public static int DecodeOP_N(instruction opcode, bool AllowNegate=false)
+ public static int DecodeOP_N(instruction opcode, bool AllowNegate = false)
{
if (AllowNegate && opcode == instruction.OP_1NEGATE)
{
/// </summary>
/// <param name="n">Small integer from the range of -1 up to 16.</param>
/// <returns>Corresponding opcode.</returns>
- public static instruction EncodeOP_N(int n, bool allowNegate=false)
+ public static instruction EncodeOP_N(int n, bool allowNegate = false)
{
if (allowNegate && n == -1)
{
int nOpCount = 0;
int nCodeHashBegin = 0;
- while (true)
+ IEnumerable<byte> pushArg;
+
+ while (GetOp(ref CodeQueue, out opcode, out pushArg)) // Read instructions
{
bool fExec = vfExec.IndexOf(false) != -1;
- //
- // Read instruction
- //
- IEnumerable<byte> pushArg;
- if (!GetOp(ref CodeQueue, out opcode, out pushArg))
- {
- return false; // No instructions left
- }
-
if (pushArg.Count() > 520)
{
return false; // Script element size limit failed
break;
default:
- throw new StackMachineException("invalid opcode");
- break;
+ throw new StackMachineException("invalid instruction");
}
popstack(ref stack);
break;
default:
- throw new StackMachineException("invalid opcode");
- break;
+ throw new StackMachineException("invalid instruction");
}
popstack(ref stack);
return true;
}
-
- static bool CheckSig(IList<byte> sigBytes, IList<byte> pubKeyBytes, CScript scriptCode, CTransaction txTo, int nIn, int nHashType, int flags)
+ static bool CheckSig(IList<byte> vchSig, IList<byte> vchPubKey, CScript scriptCode, CTransaction txTo, int nIn, int nHashType, int flags)
{
- // STUB
+ CPubKey pubkey;
+
+ try
+ {
+ // Trying to initialize the public key instance
+
+ pubkey = new CPubKey(vchPubKey);
+ }
+ catch (Exception)
+ {
+ // Exception occurred while initializing the public key
+
+ return false;
+ }
+
+ if (!pubkey.IsValid)
+ {
+ return false;
+ }
+
+ if (vchSig.Count == 0)
+ {
+ return false;
+ }
+
+ // Hash type is one byte tacked on to the end of the signature
+ if (nHashType == 0)
+ {
+ nHashType = vchSig.Last();
+ }
+ else if (nHashType != vchSig.Last())
+ {
+ return false;
+ }
+
+ // Remove hash type
+ vchSig.RemoveAt(vchSig.Count - 1);
+
+ Hash256 sighash = SignatureHash(scriptCode, txTo, nIn, nHashType);
+
+ if (!pubkey.VerifySignature(sighash, vchSig))
+ {
+ return false;
+ }
+
return true;
}
-
-};
+ };
}
/// ECDSA keypair signing test
string data = "Превед!";
- byte[] dataBytes = Encoding.UTF8.GetBytes(data);
- byte[] signature = keyPair1.Sign(dataBytes).ToArray();
+ Hash256 sigHash = Hash256.Compute256(Encoding.UTF8.GetBytes(data));
+ byte[] signature = keyPair1.Sign(sigHash).ToArray();
Console.WriteLine("Signature: {0}", Interop.ToHex(signature));
- Console.WriteLine("Signature is OK: {0} (CKeyPair)", keyPair1.VerifySignature(dataBytes, signature));
- Console.WriteLine("Signature is OK: {0} (CPubKey)", pubKey.VerifySignature(dataBytes, signature));
+ Console.WriteLine("Signature is OK: {0} (CKeyPair)", keyPair1.VerifySignature(sigHash, signature));
+ Console.WriteLine("Signature is OK: {0} (CPubKey)", pubKey.VerifySignature(sigHash, signature));
/// Donation address