Turn ByteQueue into MemoryStream wrapper, use MemoryStream for serialization of COutP...
[NovacoinLibrary.git] / Novacoin / CPubKey.cs
index 8ad4c3e..33d1b92 100644 (file)
@@ -1,6 +1,23 @@
-\feffusing System.Collections.Generic;
+\feff/**
+ *  Novacoin classes library
+ *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License, or (at your option) any later version.
+
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System.Collections.Generic;
 using System.Linq;
-using Org.BouncyCastle.Math.EC;
 using Org.BouncyCastle.Crypto.Parameters;
 
 namespace Novacoin
@@ -23,9 +40,19 @@ namespace Novacoin
         /// Initializes a new instance of CPubKey class using supplied sequence of bytes
         /// </summary>
         /// <param name="bytes">Byte sequence</param>
-        public CPubKey(IEnumerable<byte> bytes)
+        public CPubKey(byte[] bytes)
         {
-            ECPoint pQ = curve.Curve.DecodePoint(bytes.ToArray());
+            var pQ = curve.Curve.DecodePoint(bytes);
+            _Public = new ECPublicKeyParameters(pQ, domain);
+        }
+
+        /// <summary>
+        /// Init with base58 encoded sequence of bytes
+        /// </summary>
+        /// <param name="strBase58"></param>
+        public CPubKey(string strBase58)
+        {
+            var pQ = curve.Curve.DecodePoint(AddressTools.Base58DecodeCheck(strBase58));
             _Public = new ECPublicKeyParameters(pQ, domain);
         }
 
@@ -38,18 +65,27 @@ namespace Novacoin
             get { return !_Public.Q.IsInfinity; }
         }
 
+        public string ToHex()
+        {
+            return Interop.ToHex(this);
+        }
+
         /// <summary>
-        /// Is this a compressed public key?
+        /// Public part of key pair
         /// </summary>
-        /// <returns></returns>
-        public bool IsCompressed
+        public static implicit operator byte[] (CPubKey p)
         {
-            get { return _Public.Q.IsCompressed; }
+            return p._Public.Q.GetEncoded();
         }
 
         public override string ToString()
         {
-            return Interop.ToHex(Public);
+            var r = new List<byte>();
+
+            r.Add((byte)(AddrType.PUBKEY_ADDRESS));
+            r.AddRange((byte[])this);
+
+            return AddressTools.Base58EncodeCheck(r.ToArray());
         }
     }
 }