예제 #1
0
        public void CoordinateTest()
        {
            // Uncompressed Public Key
            var xValStr = "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352";
            var yValStr = "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";

            var xVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(xValStr);
            var yVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(yValStr);

            var point    = new Secp256k1Point(xVal, yVal);
            var xValStr2 = point.X.Num.PositiveValToHexString(false);
            var yValStr2 = point.Y.Num.PositiveValToHexString(false);

            Assert.AreEqual(xValStr, xValStr2);
            Assert.AreEqual(yValStr, yValStr2);
        }
예제 #2
0
        public static PublicKey Parse(byte[] bytes, bool testNet = false)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            else if (bytes.Length == 33)
            {
                bool yIsEven;

                if (bytes[0] == 0x02)
                {
                    yIsEven = true;
                }
                else if (bytes[0] == 0x03)
                {
                    yIsEven = false;
                }
                else
                {
                    throw new Exception(string.Format("Compressed public key found (33 bytes) with invalid prefix. Expected 0x02 or 0x03, found 0x{0:X2}", bytes[0]));
                }

                var buffer = new byte[32];
                Buffer.BlockCopy(bytes, 1, buffer, 0, buffer.Length);
                var x = BigIntegerUtilities.CreateFromUnsignedBigEndianBytes(buffer);
                var y = Secp256k1Point.GetYCoordinate(new Secp256k1FieldElement(x), yIsEven);
                return(new PublicKey(x, y.Num, testNet, true));
            }
            else if (bytes.Length == 65)
            {
                if (bytes[0] != 0x04)
                {
                    throw new Exception(string.Format("Uncompressed public key found (65 bytes) with invalid prefix. Expected 0x04, found 0x{0:X2}", bytes[0]));
                }
                var buffer = new byte[32];
                Buffer.BlockCopy(bytes, 1, buffer, 0, buffer.Length);
                var x = BigIntegerUtilities.CreateFromUnsignedBigEndianBytes(buffer);
                Buffer.BlockCopy(bytes, 33, buffer, 0, buffer.Length);
                var y = BigIntegerUtilities.CreateFromUnsignedBigEndianBytes(buffer);
                return(new PublicKey(x, y, testNet, false));
            }
            else
            {
                throw new Exception(nameof(bytes) + " length is invalid, expected 33 bytes (compressed pub key) or 65 bytes (uncompressed pub key)");
            }
        }
예제 #3
0
 public PublicKey(BigInteger x, BigInteger y, bool testNet = false, bool compressed = true)
 {
     _K         = new Secp256k1Point(x, y);
     TestNet    = testNet;
     Compressed = compressed;
 }