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

Classes for PublicKey and Signature #990

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
43 changes: 43 additions & 0 deletions neo/Cryptography/PublicKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Neo.IO;
using Neo.IO.Json;
using System.IO;
using Neo.Cryptography.ECC;

namespace Neo.Cryptography
{
public class PublicKey : ISerializable
{
// standard elliptic curve: secp256r1
public ECPoint Data;

public int Size => Data.Size;

void ISerializable.Deserialize(BinaryReader reader)
{
Data = ECPoint.DeserializeFrom(reader, ECCurve.Secp256r1);
}

void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write(Data.ToArray());
}

public JObject ToJson()
{
JObject json = new JObject();
json["data"] = Data.ToArray().ToHexString();
return json;
}

public static PublicKey FromJson(JObject json)
{
PublicKey pubkey = new PublicKey();
using (MemoryStream ms = new MemoryStream(json["data"].AsString().HexToBytes(), false))
using (BinaryReader reader = new BinaryReader(ms))
{
pubkey.Data = ECPoint.DeserializeFrom(reader, ECCurve.Secp256r1);
}
return pubkey;
}
}
}
38 changes: 38 additions & 0 deletions neo/Cryptography/Signature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Neo.IO;
using Neo.IO.Json;
using System.IO;

namespace Neo.Cryptography
{
public class Signature : ISerializable
{
// standard size for secp256r1: 64 bytes
public byte[] Data;

public int Size => Data.Length;

void ISerializable.Deserialize(BinaryReader reader)
{
Data = reader.ReadBytes(64);
}

void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write(Data);
}

public JObject ToJson()
{
JObject json = new JObject();
json["data"] = Data.ToHexString();
return json;
}

public static Signature FromJson(JObject json)
{
Signature signature = new Signature();
signature.Data = json["data"].AsString().HexToBytes();
return signature;
}
}
}