Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add magic parameter overloads to GetHashData and Sign #1902

Merged
merged 4 commits into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/neo/Network/P2P/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.Cryptography;
using Neo.Network.P2P.Payloads;
using System.IO;

Expand All @@ -6,15 +7,30 @@ 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();
}
}

public static UInt256 CalculateHash(this IVerifiable verifiable)
{
return CalculateHash(verifiable, ProtocolSettings.Default.Magic);
devhawk marked this conversation as resolved.
Show resolved Hide resolved
}

public static UInt256 CalculateHash(this IVerifiable verifiable, uint magic)
{
return new UInt256(Crypto.Hash256(verifiable.GetHashData(magic)));
}
}
}
2 changes: 1 addition & 1 deletion src/neo/Network/P2P/Payloads/BlockBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public UInt256 Hash
{
if (_hash == null)
{
_hash = new UInt256(Crypto.Hash256(this.GetHashData()));
_hash = this.CalculateHash();
}
return _hash;
}
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Network/P2P/Payloads/ConsensusPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public UInt256 Hash
{
if (_hash == null)
{
_hash = new UInt256(Crypto.Hash256(this.GetHashData()));
_hash = this.CalculateHash();
}
return _hash;
}
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public UInt256 Hash
{
if (_hash == null)
{
_hash = new UInt256(Crypto.Hash256(this.GetHashData()));
_hash = this.CalculateHash();
}
return _hash;
}
Expand Down
7 changes: 6 additions & 1 deletion src/neo/Wallets/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down