2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 using System.Collections.Generic;
21 using Org.BouncyCastle.Crypto.Parameters;
26 /// Representation of ECDSA public key
28 public class CPubKey : CKey
31 /// Initializes a new instance of CPubKey class as the copy of another instance
33 /// <param name="pubKey">Another CPubKey instance</param>
34 public CPubKey(CPubKey pubKey)
36 _Public = pubKey._Public;
40 /// Initializes a new instance of CPubKey class using supplied sequence of bytes
42 /// <param name="bytes">Byte sequence</param>
43 public CPubKey(byte[] bytes)
45 var pQ = curve.Curve.DecodePoint(bytes);
46 _Public = new ECPublicKeyParameters(pQ, domain);
50 /// Init with base58 encoded sequence of bytes
52 /// <param name="strBase58"></param>
53 public CPubKey(string strBase58)
55 var pQ = curve.Curve.DecodePoint(AddressTools.Base58DecodeCheck(strBase58));
56 _Public = new ECPublicKeyParameters(pQ, domain);
60 /// Quick validity test
62 /// <returns>Validation result</returns>
65 get { return !_Public.Q.IsInfinity; }
70 return Interop.ToHex((byte[])this);
74 /// Public part of key pair
76 public static implicit operator byte[] (CPubKey p)
78 return p._Public.Q.GetEncoded();
81 public override string ToString()
83 var r = new List<byte>();
85 r.Add((byte)(AddrType.PUBKEY_ADDRESS));
86 r.AddRange((byte[])this);
88 return AddressTools.Base58EncodeCheck(r.ToArray());