private static bool VerifySignature(byte[] signature, byte[] pubKey, Transaction tx, uint inputIndex, byte[] scriptPubKey) { BigInteger r, s; using (MemoryStream ms = new MemoryStream(signature, false)) using (BinaryReader reader = new BinaryReader(ms)) { if (reader.ReadByte() != 0x30) return false; reader.ReadByte(); if (reader.ReadByte() != 0x02) return false; r = new BigInteger(reader.ReadBytes(reader.ReadByte()).Reverse().ToArray()); if (reader.ReadByte() != 0x02) return false; s = new BigInteger(reader.ReadBytes(reader.ReadByte()).Reverse().ToArray()); } HashType hashType = (HashType)signature[signature.Length - 1]; byte[] hash = tx.GetHashForVerification(hashType, inputIndex, scriptPubKey); ECPoint pubKeyPoint; try { pubKeyPoint = ECPoint.DecodePoint(pubKey, ECCurve.Secp256k1); } catch { return false; } ECDsa signer = new ECDsa(pubKeyPoint); return signer.VerifySignature(hash, r, s); }
private static bool VerifySignature(byte[] signature, byte[] pubKey, Transaction tx, uint inputIndex, byte[] scriptPubKey) { BigInteger r, s; using (MemoryStream ms = new MemoryStream(signature, false)) using (BinaryReader reader = new BinaryReader(ms)) { if (reader.ReadByte() != 0x30) { return(false); } reader.ReadByte(); if (reader.ReadByte() != 0x02) { return(false); } r = new BigInteger(reader.ReadBytes(reader.ReadByte()).Reverse().ToArray()); if (reader.ReadByte() != 0x02) { return(false); } s = new BigInteger(reader.ReadBytes(reader.ReadByte()).Reverse().ToArray()); } HashType hashType = (HashType)signature[signature.Length - 1]; byte[] hash = tx.GetHashForVerification(hashType, inputIndex, scriptPubKey); ECPoint pubKeyPoint; try { pubKeyPoint = ECPoint.DecodePoint(pubKey, ECCurve.Secp256k1); } catch { return(false); } ECDsa signer = new ECDsa(pubKeyPoint); return(signer.VerifySignature(hash, r, s)); }
public Transaction SignTransaction(UnsignedTransaction utx) { const HashType hashType = HashType.SIGHASH_ALL; Transaction tx = new Transaction { Version = utx.Version, Inputs = new TransactionInput[utx.Inputs.Length], Outputs = utx.Outputs, LockTime = utx.LockTime }; for (uint i = 0; i < utx.Inputs.Length; i++) { tx.Inputs[i] = new TransactionInput { PrevHash = utx.Inputs[i].TxId, PrevIndex = utx.Inputs[i].Index, Sequence = uint.MaxValue }; if (utx.Inputs[i].ScriptHash == null) return null; WalletEntry entry = FindEntry(utx.Inputs[i].ScriptHash); if (entry == null) return null; BigInteger[] signature; using (entry.Decrypt()) { ECDsa signer = new ECDsa(entry.PrivateKey, ECCurve.Secp256k1); signature = signer.GenerateSignature(utx.GetHashForSigning(hashType, i)); } byte[] sigEncoded; using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms)) { byte[] r = signature[0].ToByteArray().Reverse().ToArray(); byte[] s = signature[1].ToByteArray().Reverse().ToArray(); writer.Write((byte)0x30); writer.Write((byte)(2 + r.Length + 2 + s.Length)); writer.Write((byte)0x02); writer.Write((byte)r.Length); writer.Write(r); writer.Write((byte)0x02); writer.Write((byte)s.Length); writer.Write(s); writer.Write((byte)hashType); writer.Flush(); sigEncoded = ms.ToArray(); } using (ScriptBuilder sb = new ScriptBuilder()) { sb.Push(sigEncoded); sb.Push(entry.PublicKey.EncodePoint(entry.Compressed)); tx.Inputs[i].Script = sb.ToArray(); } } return tx; }
public Transaction SignTransaction(UnsignedTransaction utx) { const HashType hashType = HashType.SIGHASH_ALL; Transaction tx = new Transaction { Version = utx.Version, Inputs = new TransactionInput[utx.Inputs.Length], Outputs = utx.Outputs, LockTime = utx.LockTime }; for (uint i = 0; i < utx.Inputs.Length; i++) { tx.Inputs[i] = new TransactionInput { PrevHash = utx.Inputs[i].TxId, PrevIndex = utx.Inputs[i].Index, Sequence = uint.MaxValue }; if (utx.Inputs[i].ScriptHash == null) { return(null); } WalletEntry entry = FindEntry(utx.Inputs[i].ScriptHash); if (entry == null) { return(null); } BigInteger[] signature; using (entry.Decrypt()) { ECDsa signer = new ECDsa(entry.PrivateKey, ECCurve.Secp256k1); signature = signer.GenerateSignature(utx.GetHashForSigning(hashType, i)); } byte[] sigEncoded; using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms)) { byte[] r = signature[0].ToByteArray().Reverse().ToArray(); byte[] s = signature[1].ToByteArray().Reverse().ToArray(); writer.Write((byte)0x30); writer.Write((byte)(2 + r.Length + 2 + s.Length)); writer.Write((byte)0x02); writer.Write((byte)r.Length); writer.Write(r); writer.Write((byte)0x02); writer.Write((byte)s.Length); writer.Write(s); writer.Write((byte)hashType); writer.Flush(); sigEncoded = ms.ToArray(); } using (ScriptBuilder sb = new ScriptBuilder()) { sb.Push(sigEncoded); sb.Push(entry.PublicKey.EncodePoint(entry.Compressed)); tx.Inputs[i].Script = sb.ToArray(); } } return(tx); }