Remove unnecessary usings, rewrite pubkey serialization code
[NovacoinLibrary.git] / Novacoin / CPubKey.cs
index a6227a6..8ad4c3e 100644 (file)
@@ -1,38 +1,22 @@
-\feffusing System;
-using System.Collections.Generic;
+\feffusing System.Collections.Generic;
 using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Math.EC;
-
-using Org.BouncyCastle.Crypto;
-using Org.BouncyCastle.Crypto.Generators;
 using Org.BouncyCastle.Crypto.Parameters;
 
-using Org.BouncyCastle.Asn1.X9;
-using Org.BouncyCastle.Security;
-using Org.BouncyCastle.Asn1.Sec;
-
 namespace Novacoin
 {
     /// <summary>
     /// Representation of ECDSA public key
     /// </summary>
-    public class CPubKey
+    public class CPubKey : CKey
     {
-        private ECPoint Q;
-        private static X9ECParameters curve = SecNamedCurves.GetByName("secp256k1");
-        private static ECDomainParameters domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());
-
         /// <summary>
         /// Initializes a new instance of CPubKey class as the copy of another instance
         /// </summary>
         /// <param name="pubKey">Another CPubKey instance</param>
         public CPubKey(CPubKey pubKey)
         {
-            Q = pubKey.Q;
+            _Public = pubKey._Public;
         }
 
         /// <summary>
@@ -41,12 +25,8 @@ namespace Novacoin
         /// <param name="bytes">Byte sequence</param>
         public CPubKey(IEnumerable<byte> bytes)
         {
-            Q = ((ECPublicKeyParameters)PublicKeyFactory.CreateKey(bytes.ToArray())).Q;
-        }
-
-        public CPubKey(ECPoint pQ)
-        {
-            Q = pQ;
+            ECPoint pQ = curve.Curve.DecodePoint(bytes.ToArray());
+            _Public = new ECPublicKeyParameters(pQ, domain);
         }
 
         /// <summary>
@@ -55,7 +35,7 @@ namespace Novacoin
         /// <returns>Validation result</returns>
         public bool IsValid
         {
-            get { return !Q.IsInfinity; }
+            get { return !_Public.Q.IsInfinity; }
         }
 
         /// <summary>
@@ -64,41 +44,12 @@ namespace Novacoin
         /// <returns></returns>
         public bool IsCompressed
         {
-            get { return Q.IsCompressed; }
-        }
-
-        /// <summary>
-        /// Calculate Hash160 and create new CKeyID instance.
-        /// </summary>
-        /// <returns>New key ID</returns>
-        public CKeyID GetKeyID()
-        {
-            return new CKeyID(Hash160.Compute160(Raw));
-        }
-
-        public bool Verify(IEnumerable<byte> data, IEnumerable<byte> signature)
-        {
-            byte[] dataBytes = data.ToArray();
-
-            ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA");
-            ECPublicKeyParameters keyParameters = new ECPublicKeyParameters(Q, domain);
-            signer.Init(false, keyParameters);
-            signer.BlockUpdate(dataBytes, 0, dataBytes.Length);
-
-            return signer.VerifySignature(signature.ToArray());
-        }
-
-        /// <summary>
-        /// Accessor for internal representation
-        /// </summary>
-        public IEnumerable<byte> Raw
-        {
-            get { return Q.GetEncoded(); }
+            get { return _Public.Q.IsCompressed; }
         }
 
         public override string ToString()
         {
-            return Interop.ToHex(Raw);
+            return Interop.ToHex(Public);
         }
     }
 }