Skip to content

Commit

Permalink
Merge pull request #1 from AntShares/master
Browse files Browse the repository at this point in the history
优化部分代码逻辑;
  • Loading branch information
angrywindsoffice committed Sep 18, 2015
2 parents 18a3116 + 88e8b3e commit fb42b98
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 253 deletions.
2 changes: 1 addition & 1 deletion AntSharesCore/Core/AgencyTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public override VerificationResult Verify()
VerificationResult result = base.Verify();
foreach (Order order in Orders)
{
result |= order.Verify();
result |= order.VerifySignature();
if (result.HasFlag(VerificationResult.InvalidSignature))
break;
}
Expand Down
1 change: 0 additions & 1 deletion AntSharesCore/Core/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public BlockHeader Header
Nonce = Nonce,
NextMiner = NextMiner,
Script = Script,
TransactionCount = Transactions.Length
};
}
return _header;
Expand Down
70 changes: 12 additions & 58 deletions AntSharesCore/Core/BlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class BlockHeader : IEquatable<BlockHeader>, ISignable
public ulong Nonce;
public UInt160 NextMiner;
public byte[] Script;
public int TransactionCount;

[NonSerialized]
private UInt256 _hash = null;
Expand Down Expand Up @@ -46,6 +45,12 @@ byte[][] ISignable.Scripts
}

void ISerializable.Deserialize(BinaryReader reader)
{
((ISignable)this).DeserializeUnsigned(reader);
this.Script = reader.ReadBytes((int)reader.ReadVarInt());
}

void ISignable.DeserializeUnsigned(BinaryReader reader)
{
if (reader.ReadUInt32() != Version)
throw new FormatException();
Expand All @@ -56,8 +61,6 @@ void ISerializable.Deserialize(BinaryReader reader)
throw new FormatException();
this.Nonce = reader.ReadUInt64();
this.NextMiner = reader.ReadSerializable<UInt160>();
this.Script = reader.ReadBytes((int)reader.ReadVarInt());
this.TransactionCount = (int)reader.ReadVarInt();
}

public bool Equals(BlockHeader other)
Expand All @@ -81,46 +84,11 @@ public static BlockHeader FromTrimmedData(byte[] data, int index)
}
}

void ISignable.FromUnsignedArray(byte[] value)
{
using (MemoryStream ms = new MemoryStream(value, false))
using (BinaryReader reader = new BinaryReader(ms))
{
if (reader.ReadUInt32() != Version)
throw new FormatException();
this.PrevBlock = reader.ReadSerializable<UInt256>();
this.MerkleRoot = reader.ReadSerializable<UInt256>();
this.Timestamp = reader.ReadUInt32();
if (reader.ReadUInt32() != Bits)
throw new FormatException();
this.Nonce = reader.ReadUInt64();
this.NextMiner = reader.ReadSerializable<UInt160>();
this.TransactionCount = (int)reader.ReadVarInt();
}
}

public override int GetHashCode()
{
return Hash.GetHashCode();
}

byte[] ISignable.GetHashForSigning()
{
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms))
{
writer.Write(Version);
writer.Write(PrevBlock);
writer.Write(MerkleRoot);
writer.Write(Timestamp);
writer.Write(Bits);
writer.Write(Nonce);
writer.Write(NextMiner);
writer.Flush();
return ms.ToArray().Sha256();
}
}

UInt160[] ISignable.GetScriptHashesForVerifying()
{
if (PrevBlock == UInt256.Zero)
Expand All @@ -131,6 +99,12 @@ UInt160[] ISignable.GetScriptHashesForVerifying()
}

void ISerializable.Serialize(BinaryWriter writer)
{
((ISignable)this).SerializeUnsigned(writer);
writer.WriteVarInt(Script.Length); writer.Write(Script);
}

void ISignable.SerializeUnsigned(BinaryWriter writer)
{
writer.Write(Version);
writer.Write(PrevBlock);
Expand All @@ -139,26 +113,6 @@ void ISerializable.Serialize(BinaryWriter writer)
writer.Write(Bits);
writer.Write(Nonce);
writer.Write(NextMiner);
writer.WriteVarInt(Script.Length); writer.Write(Script);
writer.WriteVarInt(TransactionCount);
}

byte[] ISignable.ToUnsignedArray()
{
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms))
{
writer.Write(Version);
writer.Write(PrevBlock);
writer.Write(MerkleRoot);
writer.Write(Timestamp);
writer.Write(Bits);
writer.Write(Nonce);
writer.Write(NextMiner);
writer.Write(TransactionCount);
writer.Flush();
return ms.ToArray();
}
}

public VerificationResult Verify()
Expand Down
13 changes: 13 additions & 0 deletions AntSharesCore/Core/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
using AntShares.Core.Scripts;
using AntShares.Cryptography;
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace AntShares.Core
{
public static class Helper
{
private const byte CoinVersion = 0x17;

public static byte[] GetHashForSigning(this ISignable signable)
{
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms, Encoding.UTF8))
{
signable.SerializeUnsigned(writer);
writer.Flush();
return ms.ToArray().Sha256();
}
}

internal static byte[] Sign(this ISignable signable, byte[] prikey, byte[] pubkey)
{
const int ECDSA_PRIVATE_P256_MAGIC = 0x32534345;
Expand Down
7 changes: 3 additions & 4 deletions AntSharesCore/Core/ISignable.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using AntShares.IO;
using System.IO;

namespace AntShares.Core
{
public interface ISignable : ISerializable
{
byte[][] Scripts { get; set; }

void FromUnsignedArray(byte[] value);
byte[] GetHashForSigning();
void DeserializeUnsigned(BinaryReader reader);
UInt160[] GetScriptHashesForVerifying();
byte[] ToUnsignedArray();
VerificationResult Verify();
void SerializeUnsigned(BinaryWriter writer);
}
}
92 changes: 24 additions & 68 deletions AntSharesCore/Core/Order.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AntShares.Cryptography;
using AntShares.IO;
using AntShares.IO;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -35,19 +34,26 @@ byte[][] ISignable.Scripts

void ISerializable.Deserialize(BinaryReader reader)
{
UInt256 asset_id = reader.ReadSerializable<UInt256>();
UInt256 value_asset_id = reader.ReadSerializable<UInt256>();
if (asset_id == value_asset_id) throw new FormatException();
UInt160 agent = reader.ReadSerializable<UInt160>();
DeserializeInternal(reader, asset_id, value_asset_id, agent);
((ISignable)this).DeserializeUnsigned(reader);
this.Scripts = reader.ReadBytesArray();
}

internal void DeserializeInTransaction(BinaryReader reader, AgencyTransaction tx)
{
DeserializeInternal(reader, tx.AssetId, tx.ValueAssetId, tx.Agent);
DeserializeUnsignedInternal(reader, tx.AssetId, tx.ValueAssetId, tx.Agent);
this.Scripts = reader.ReadBytesArray();
}

void ISignable.DeserializeUnsigned(BinaryReader reader)
{
UInt256 asset_id = reader.ReadSerializable<UInt256>();
UInt256 value_asset_id = reader.ReadSerializable<UInt256>();
if (asset_id == value_asset_id) throw new FormatException();
UInt160 agent = reader.ReadSerializable<UInt160>();
DeserializeUnsignedInternal(reader, asset_id, value_asset_id, agent);
}

private void DeserializeInternal(BinaryReader reader, UInt256 asset_id, UInt256 value_asset_id, UInt160 agent)
private void DeserializeUnsignedInternal(BinaryReader reader, UInt256 asset_id, UInt256 value_asset_id, UInt160 agent)
{
this.AssetId = asset_id;
this.ValueAssetId = value_asset_id;
Expand All @@ -62,39 +68,6 @@ private void DeserializeInternal(BinaryReader reader, UInt256 asset_id, UInt256
this.Inputs = reader.ReadSerializableArray<TransactionInput>();
if (Inputs.Distinct().Count() != Inputs.Length)
throw new FormatException();
this.Scripts = reader.ReadBytesArray();
}

void ISignable.FromUnsignedArray(byte[] value)
{
using (MemoryStream ms = new MemoryStream(value, false))
using (BinaryReader reader = new BinaryReader(ms))
{
this.AssetId = reader.ReadSerializable<UInt256>();
this.ValueAssetId = reader.ReadSerializable<UInt256>();
this.Agent = reader.ReadSerializable<UInt160>();
this.Amount = reader.ReadSerializable<Fixed8>();
this.Price = reader.ReadSerializable<Fixed8>();
this.Client = reader.ReadSerializable<UInt160>();
this.Inputs = reader.ReadSerializableArray<TransactionInput>();
}
}

byte[] ISignable.GetHashForSigning()
{
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms))
{
writer.Write(AssetId);
writer.Write(ValueAssetId);
writer.Write(Agent);
writer.Write(Amount);
writer.Write(Price);
writer.Write(Client);
writer.Write(Inputs);
writer.Flush();
return ms.ToArray().Sha256();
}
}

UInt160[] ISignable.GetScriptHashesForVerifying()
Expand All @@ -117,13 +90,7 @@ UInt160[] ISignable.GetScriptHashesForVerifying()

void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write(AssetId);
writer.Write(ValueAssetId);
writer.Write(Agent);
writer.Write(Amount);
writer.Write(Price);
writer.Write(Client);
writer.Write(Inputs);
((ISignable)this).SerializeUnsigned(writer);
writer.Write(Scripts);
}

Expand All @@ -136,26 +103,15 @@ internal void SerializeInTransaction(BinaryWriter writer)
writer.Write(Scripts);
}

byte[] ISignable.ToUnsignedArray()
void ISignable.SerializeUnsigned(BinaryWriter writer)
{
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms))
{
writer.Write(AssetId);
writer.Write(ValueAssetId);
writer.Write(Agent);
writer.Write(Amount);
writer.Write(Price);
writer.Write(Client);
writer.Write(Inputs);
writer.Flush();
return ms.ToArray();
}
}

public VerificationResult Verify()
{
return this.VerifySignature();
writer.Write(AssetId);
writer.Write(ValueAssetId);
writer.Write(Agent);
writer.Write(Amount);
writer.Write(Price);
writer.Write(Client);
writer.Write(Inputs);
}
}
}
16 changes: 14 additions & 2 deletions AntSharesCore/Core/SignatureContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using AntShares.Cryptography;
using AntShares.IO.Json;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace AntShares.Core
{
Expand Down Expand Up @@ -54,7 +56,11 @@ public static SignatureContext Parse(string value)
JObject json = JObject.Parse(value);
string typename = string.Format("{0}.{1}", typeof(SignatureContext).Namespace, json["type"].AsString());
ISignable signable = Assembly.GetExecutingAssembly().CreateInstance(typename) as ISignable;
signable.FromUnsignedArray(json["hex"].AsString().HexToBytes());
using (MemoryStream ms = new MemoryStream(json["hex"].AsString().HexToBytes(), false))
using (BinaryReader reader = new BinaryReader(ms, Encoding.UTF8))
{
signable.DeserializeUnsigned(reader);
}
SignatureContext context = new SignatureContext(signable);
JArray multisignatures = (JArray)json["multi_signatures"];
for (int i = 0; i < multisignatures.Count; i++)
Expand All @@ -79,7 +85,13 @@ public override string ToString()
{
JObject json = new JObject();
json["type"] = Signable.GetType().Name;
json["hex"] = Signable.ToUnsignedArray().ToHexString();
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms, Encoding.UTF8))
{
Signable.SerializeUnsigned(writer);
writer.Flush();
json["hex"] = ms.ToArray().ToHexString();
}
JArray multisignatures = new JArray();
for (int i = 0; i < signatures.Length; i++)
{
Expand Down
Loading

0 comments on commit fb42b98

Please sign in to comment.