Example #1
0
        public static string GetChecksumAddress(string address)
        {
            // remove prefix 0x
            address = ByteUtils.CleanHexPrefix(address);
            address = address.ToLower();

            // do keccak256 once
            var bytes   = CryptoUtils.Keccak256(Encoding.UTF8.GetBytes(address));
            var builder = new StringBuilder();
            var hex     = ByteUtils.ToHexString(bytes, null);

            var chars = hex.ToCharArray();
            int size  = address.Length;

            var raws = address.ToCharArray();

            for (int i = 0; i < size; i++)
            {
                if (ParseInt(chars[i]) >= 8)
                {
                    builder.Append(("" + raws[i]).ToUpper());
                }
                else
                {
                    builder.Append(raws[i]);
                }
            }

            return("0x" + builder);
        }
Example #2
0
        public static string BuildSignature(byte[] cert, byte[] txRawHash, string privateKey)
        {
            var txRawBytes  = CryptoUtils.Blake2b(txRawHash);
            var cerHexBytes = CryptoUtils.Sha256(cert);

            var message = new byte[txRawBytes.Length + cerHexBytes.Length];

            Array.Copy(cerHexBytes, 0, message, 0, cerHexBytes.Length);
            Array.Copy(txRawBytes, 0, message, cerHexBytes.Length, txRawBytes.Length);

            var key       = ECKeyPair.Create(ByteUtils.ToByteArray(privateKey));
            var signature = ECDSASign.SignMessage(CryptoUtils.Sha256(message), key, false);

            var signBytes = signature.ToByteArray();

            _logger.Info("signature: {} {}", ByteUtils.ToHexString(signBytes, null),
                         ByteUtils.CleanHexPrefix(ByteUtils.ToHexString(signature.R, Prefix.ZeroLowerX))
                         + ByteUtils.CleanHexPrefix(ByteUtils.ToHexString(signature.S, Prefix.ZeroLowerX)) + "0"
                         + signature.V);
            return(ByteUtils.ToHexString(signBytes, null));
        }
        private static string EncodeAddress(string address)
        {
            if (!StringUtils.IsHex(address))
            {
                throw new InvalidArgumentException("Parameter format is not hex string");
            }
            var paramBytes = ByteUtils.ToByteArray(address);

            if (paramBytes == null || paramBytes.Length > MAX_BYTE_LENGTH)
            {
                throw new InvalidArgumentException("Parameter format is hex string size too large, or null");
            }
            if (paramBytes.Length < MAX_BYTE_LENGTH)
            {
                var fillingZero = new byte[MAX_BYTE_LENGTH];
                Array.Copy(paramBytes, 0, fillingZero, MAX_BYTE_LENGTH - paramBytes.Length, paramBytes.Length);
                return(ByteUtils.ToHexString(fillingZero, null));
            }
            else
            {
                return(ByteUtils.CleanHexPrefix(address));
            }
        }