Esempio n. 1
0
        public void BigIntegerToBytesTest()
        {
            // Uncompressed Public Key
            var xValStr  = "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352";
            var yValStr  = "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";
            var expected = "0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";

            var xVal  = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(xValStr);
            var yVal  = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(yValStr);
            var bytes = new byte[65]; // type byte + 32 bytes for X + 32 bytes for Y

            bytes[0] = 0x04;
            xVal.PositiveValToBigEndianBytes(bytes, 1, 32);
            yVal.PositiveValToBigEndianBytes(bytes, 33, 32);
            var hexStr = HexUtilities.BytesToHexString(bytes, false);

            Assert.AreEqual(expected, hexStr);

            // Arbitrary data to arbitrary position in buffer
            var str1 = "010203040506";
            var str2 = "ABCDEF";

            expected = "010203040506000000ABCDEF";

            var val1 = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(str1);
            var val2 = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(str2);

            bytes = new byte[12];
            val1.PositiveValToBigEndianBytes(bytes, 0, 6);
            val2.PositiveValToBigEndianBytes(bytes, 9, 3);
            hexStr = HexUtilities.BytesToHexString(bytes, false);
            Assert.AreEqual(expected, hexStr);

            // Arbitrary data to arbitrary position in buffer, right aligned
            str1     = "010203040506";
            str2     = "ABCDEF";
            expected = "010203040506000000ABCDEF";

            val1  = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(str1);
            val2  = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(str2);
            bytes = new byte[12];
            val1.PositiveValToBigEndianBytes(bytes, 0, 6);
            val2.PositiveValToBigEndianBytes(bytes, 6, 6);
            hexStr = HexUtilities.BytesToHexString(bytes, false);
            Assert.AreEqual(expected, hexStr);
        }
Esempio n. 2
0
        public void GenerateWifStringTest()
        {
            // Compressed
            var prvKeyVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD");
            var expected  = "KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ";
            var prvKey    = new PrivateKey(prvKeyVal, false, true);
            var wifString = prvKey.GetWifString();

            Assert.AreEqual(expected, wifString);

            prvKeyVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD");
            expected  = "L5oLkpV3aqBJ4BgssVAsax1iRa77G5CVYnv9adQ6Z87te7TyUdSC";
            prvKey    = new PrivateKey(BigInteger.Pow(2, 256) - BigInteger.Pow(2, 199), false, true);
            wifString = prvKey.GetWifString();
            Assert.AreEqual(expected, wifString);

            prvKeyVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("1cca23de92fd1862fb5b76e5f4f50eb082165e5191e116c18ed1a6b24be6a53f");
            expected  = "cNYfWuhDpbNM1JWc3c6JTrtrFVxU4AGhUKgw5f93NP2QaBqmxKkg";
            prvKey    = new PrivateKey(prvKeyVal, true, true);
            wifString = prvKey.GetWifString();
            Assert.AreEqual(expected, wifString);

            // Uncompressed
            prvKeyVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD");
            expected  = "5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn";
            prvKey    = new PrivateKey(prvKeyVal, false, false);
            wifString = prvKey.GetWifString();
            Assert.AreEqual(expected, wifString);

            prvKeyVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D");
            expected  = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ";
            prvKey    = new PrivateKey(prvKeyVal, false, false);
            wifString = prvKey.GetWifString();
            Assert.AreEqual(expected, wifString);

            prvKeyVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("0dba685b4511dbd3d368e5c4358a1277de9486447af7b3604a69b8d9d8b7889d");
            expected  = "5HvLFPDVgFZRK9cd4C5jcWki5Skz6fmKqi1GQJf5ZoMofid2Dty";
            prvKey    = new PrivateKey(prvKeyVal, false, false);
            wifString = prvKey.GetWifString();
            Assert.AreEqual(expected, wifString);

            expected  = "93XfLeifX7Jx7n7ELGMAf1SUR6f9kgQs8Xke8WStMwUtrDucMzn";
            prvKey    = new PrivateKey(BigInteger.Pow(2, 256) - BigInteger.Pow(2, 201), true, false);
            wifString = prvKey.GetWifString();
            Assert.AreEqual(expected, wifString);
        }
Esempio n. 3
0
        public void TestVerify()
        {
            var x         = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("887387e452b8eacc4acfde10d9aaf7f6d9a0f975aabb10d006e4da568744d06c");
            var y         = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("61de6d95231cd89026e286df3b6ae4a894a3378e393e93a0f45b666329a0ae34");
            var p         = new PublicKey(x, y);
            var z         = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("ec208baa0fc1c19f708a9ca96fdeff3ac3f230bb4a7ba4aede4942ad003c0f60");
            var r         = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("ac8d1c87e51d0d441be8b3dd5b05c8795b48875dffe00b7ffcfac23010d3a395");
            var s         = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("68342ceff8935ededd102dd876ffd6ba72d6a427a3edb13d26eb0781cb423c4");
            var signature = new Signature(r, s);
            var valid     = p.Verify(z, signature);

            Assert.IsTrue(valid);

            z         = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("7c076ff316692a3d7eb3c3bb0f8b1488cf72e1afcd929e29307032997a838a3d");
            r         = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("eff69ef2b1bd93a66ed5219add4fb51e11a840f404876325a1e8ffe0529a2c");
            s         = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString("c7207fee197d27c618aea621406f6bf5ef6fca38681d82b2f06fddbdce6feab6");
            signature = new Signature(r, s);
            valid     = p.Verify(z, signature);
            Assert.IsTrue(valid);
        }
Esempio n. 4
0
        public void AddressTest()
        {
            // Uncompressed Public Key
            var ecdsaPrivateKey = "18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725";
            var expectedPubXVal = "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352";
            var expectedPubYVal = "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";
            var expectedPubKey  = "0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";
            var expectedAddress = "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM";

            var prvKeyVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(ecdsaPrivateKey);
            var prvKey    = new PrivateKey(prvKeyVal, false, false);
            var pubKey    = prvKey.GetPublicKey();

            Assert.AreEqual(expectedPubXVal, pubKey.X.PositiveValToHexString(false));
            Assert.AreEqual(expectedPubYVal, pubKey.Y.PositiveValToHexString(false));

            var pubKeyBytes  = pubKey.GetPublicKeyBytes();
            var pubKeyString = HexUtilities.BytesToHexString(pubKeyBytes, false);

            Assert.AreEqual(expectedPubKey, pubKeyString);
            var address = pubKey.GetAddress();

            Assert.AreEqual(expectedAddress, address);
        }