From 30963ab27db9cd8fa501f1e91f4b80f4e42fe043 Mon Sep 17 00:00:00 2001 From: Harry Pierson Date: Wed, 2 Sep 2020 22:26:21 -0700 Subject: [PATCH 1/3] add magic parameter overloads to GetHashData and Sign --- src/neo/Network/P2P/Helper.cs | 7 ++++++- src/neo/Wallets/Helper.cs | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/neo/Network/P2P/Helper.cs b/src/neo/Network/P2P/Helper.cs index 1d4797cf9e..d6fd9f68c9 100644 --- a/src/neo/Network/P2P/Helper.cs +++ b/src/neo/Network/P2P/Helper.cs @@ -6,11 +6,16 @@ namespace Neo.Network.P2P public static class Helper { public static byte[] GetHashData(this IVerifiable verifiable) + { + return GetHashData(verifiable, ProtocolSettings.Default.Magic); + } + + public static byte[] GetHashData(this IVerifiable verifiable, uint magic) { using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms)) { - writer.Write(ProtocolSettings.Default.Magic); + writer.Write(magic); verifiable.SerializeUnsigned(writer); writer.Flush(); return ms.ToArray(); diff --git a/src/neo/Wallets/Helper.cs b/src/neo/Wallets/Helper.cs index 712c2585de..4dd06b39e5 100644 --- a/src/neo/Wallets/Helper.cs +++ b/src/neo/Wallets/Helper.cs @@ -10,7 +10,12 @@ public static class Helper { public static byte[] Sign(this IVerifiable verifiable, KeyPair key) { - return Crypto.Sign(verifiable.GetHashData(), key.PrivateKey, key.PublicKey.EncodePoint(false)[1..]); + return Sign(verifiable, key, ProtocolSettings.Default.Magic); + } + + public static byte[] Sign(this IVerifiable verifiable, KeyPair key, uint magic) + { + return Crypto.Sign(verifiable.GetHashData(magic), key.PrivateKey, key.PublicKey.EncodePoint(false)[1..]); } public static string ToAddress(this UInt160 scriptHash) From 4cea6d0939a90fc3df3bf194987815226c78abd1 Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 3 Sep 2020 09:13:32 -0700 Subject: [PATCH 2/3] added CalculateHash helper --- src/neo/Network/P2P/Helper.cs | 11 +++++++++++ src/neo/Network/P2P/Payloads/BlockBase.cs | 2 +- src/neo/Network/P2P/Payloads/ConsensusPayload.cs | 2 +- src/neo/Network/P2P/Payloads/Transaction.cs | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/neo/Network/P2P/Helper.cs b/src/neo/Network/P2P/Helper.cs index d6fd9f68c9..a86bf40d8f 100644 --- a/src/neo/Network/P2P/Helper.cs +++ b/src/neo/Network/P2P/Helper.cs @@ -1,3 +1,4 @@ +using Neo.Cryptography; using Neo.Network.P2P.Payloads; using System.IO; @@ -21,5 +22,15 @@ public static byte[] GetHashData(this IVerifiable verifiable, uint magic) return ms.ToArray(); } } + + public static UInt256 CalculateHash(this IVerifiable verifiable) + { + return CalculateHash(verifiable, ProtocolSettings.Default.Magic); + } + + public static UInt256 CalculateHash(this IVerifiable verifiable, uint magic) + { + return new UInt256(Crypto.Hash256(verifiable.GetHashData(magic))); + } } } diff --git a/src/neo/Network/P2P/Payloads/BlockBase.cs b/src/neo/Network/P2P/Payloads/BlockBase.cs index e737466602..b724f5e074 100644 --- a/src/neo/Network/P2P/Payloads/BlockBase.cs +++ b/src/neo/Network/P2P/Payloads/BlockBase.cs @@ -26,7 +26,7 @@ public UInt256 Hash { if (_hash == null) { - _hash = new UInt256(Crypto.Hash256(this.GetHashData())); + _hash = this.CalculateHash(); } return _hash; } diff --git a/src/neo/Network/P2P/Payloads/ConsensusPayload.cs b/src/neo/Network/P2P/Payloads/ConsensusPayload.cs index 1174431695..dbf2732fbc 100644 --- a/src/neo/Network/P2P/Payloads/ConsensusPayload.cs +++ b/src/neo/Network/P2P/Payloads/ConsensusPayload.cs @@ -45,7 +45,7 @@ public UInt256 Hash { if (_hash == null) { - _hash = new UInt256(Crypto.Hash256(this.GetHashData())); + _hash = this.CalculateHash(); } return _hash; } diff --git a/src/neo/Network/P2P/Payloads/Transaction.cs b/src/neo/Network/P2P/Payloads/Transaction.cs index 02ae5e775b..bf689093b9 100644 --- a/src/neo/Network/P2P/Payloads/Transaction.cs +++ b/src/neo/Network/P2P/Payloads/Transaction.cs @@ -62,7 +62,7 @@ public UInt256 Hash { if (_hash == null) { - _hash = new UInt256(Crypto.Hash256(this.GetHashData())); + _hash = this.CalculateHash(); } return _hash; } From 5041bc952ceb3c542314fc7d2d193a3e8e1126f7 Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 3 Sep 2020 09:40:00 -0700 Subject: [PATCH 3/3] CR Feedback --- src/neo/Network/P2P/Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/Network/P2P/Helper.cs b/src/neo/Network/P2P/Helper.cs index a86bf40d8f..db95362bd6 100644 --- a/src/neo/Network/P2P/Helper.cs +++ b/src/neo/Network/P2P/Helper.cs @@ -25,7 +25,7 @@ public static byte[] GetHashData(this IVerifiable verifiable, uint magic) public static UInt256 CalculateHash(this IVerifiable verifiable) { - return CalculateHash(verifiable, ProtocolSettings.Default.Magic); + return new UInt256(Crypto.Hash256(verifiable.GetHashData(ProtocolSettings.Default.Magic))); } public static UInt256 CalculateHash(this IVerifiable verifiable, uint magic)