-\feffusing System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+\feff/**
+ * Novacoin classes library
+ * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
-using Org.BouncyCastle.Math;
-using Org.BouncyCastle.Math.EC;
+ * 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.
-using Org.BouncyCastle.Crypto;
-using Org.BouncyCastle.Crypto.Generators;
-using Org.BouncyCastle.Crypto.Parameters;
+ * 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.
-using Org.BouncyCastle.Asn1.X9;
-using Org.BouncyCastle.Security;
-using Org.BouncyCastle.Asn1.Sec;
+ * 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.Crypto.Parameters;
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>
/// 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)
{
- Q = ((ECPublicKeyParameters)PublicKeyFactory.CreateKey(bytes.ToArray())).Q;
+ var pQ = curve.Curve.DecodePoint(bytes);
+ _Public = new ECPublicKeyParameters(pQ, domain);
}
- public CPubKey(ECPoint pQ)
+ /// <summary>
+ /// Init with base58 encoded sequence of bytes
+ /// </summary>
+ /// <param name="strBase58"></param>
+ public CPubKey(string strBase58)
{
- Q = pQ;
+ var pQ = curve.Curve.DecodePoint(AddressTools.Base58DecodeCheck(strBase58));
+ _Public = new ECPublicKeyParameters(pQ, domain);
}
/// <summary>
/// <returns>Validation result</returns>
public bool IsValid
{
- get { return !Q.IsInfinity; }
+ get { return !_Public.Q.IsInfinity; }
}
- /// <summary>
- /// Is this a compressed public key?
- /// </summary>
- /// <returns></returns>
- public bool IsCompressed
+ public string ToHex()
{
- get { return Q.IsCompressed; }
+ return Interop.ToHex(this);
}
/// <summary>
- /// Calculate Hash160 and create new CKeyID instance.
+ /// Public part of key pair
/// </summary>
- /// <returns>New key ID</returns>
- public CKeyID GetKeyID()
+ public static implicit operator byte[] (CPubKey p)
{
- return new CKeyID(Hash160.Compute160(Raw));
+ return p._Public.Q.GetEncoded();
}
- public bool Verify(IEnumerable<byte> data, IEnumerable<byte> signature)
+ public override string ToString()
{
- 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);
+ var r = new List<byte>();
- return signer.VerifySignature(signature.ToArray());
- }
+ r.Add((byte)(AddrType.PUBKEY_ADDRESS));
+ r.AddRange((byte[])this);
- /// <summary>
- /// Accessor for internal representation
- /// </summary>
- public IEnumerable<byte> Raw
- {
- get { return Q.GetEncoded(); }
- }
-
- public override string ToString()
- {
- return Interop.ToHex(Raw);
+ return AddressTools.Base58EncodeCheck(r.ToArray());
}
}
}