Пример #1
0
        public static string AddChecksum(
            string addressString)
        {
            ValidateFormatAndThrowIfInvalid(addressString);

            addressString = addressString.Remove(0, 2).ToLowerInvariant();

            var addressBytes = Encoding.UTF8.GetBytes(addressString);
            var caseMapBytes = Keccak256.Sum(addressBytes);

            var addressBuilder = new StringBuilder("0x");

            for (var i = 0; i < 40; i++)
            {
                var addressChar = addressString[i];

                if (char.IsLetter(addressChar))
                {
                    var leftShift     = i % 2 == 0 ? 7 : 3;
                    var shouldBeUpper = (caseMapBytes[i / 2] & (1 << leftShift)) != 0;

                    if (shouldBeUpper)
                    {
                        addressChar = char.ToUpper(addressChar);
                    }
                }

                addressBuilder.Append(addressChar);
            }

            return(addressBuilder.ToString());
        }
Пример #2
0
        private static bool ValidateChecksum(
            string addressString)
        {
            addressString = addressString.Remove(0, 2);

            var addressBytes = Encoding.UTF8.GetBytes(addressString.ToLowerInvariant());
            var caseMapBytes = Keccak256.Sum(addressBytes);

            for (var i = 0; i < 40; i++)
            {
                var addressChar = addressString[i];

                if (!char.IsLetter(addressChar))
                {
                    continue;
                }

                var leftShift     = i % 2 == 0 ? 7 : 3;
                var shouldBeUpper = (caseMapBytes[i / 2] & (1 << leftShift)) != 0;
                var shouldBeLower = !shouldBeUpper;

                if (shouldBeUpper && char.IsLower(addressChar) ||
                    shouldBeLower && char.IsUpper(addressChar))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        private static string GetTransactionHash(
            string signedTxData)
        {
            var txDataBytes = signedTxData.HexToByteArray();

            return(Keccak256
                   .Sum(txDataBytes)
                   .ToHex(true));
        }