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); }
private static string EncodeBool(bool value) { var rawValue = new byte[MAX_BYTE_LENGTH]; if (value) { rawValue[rawValue.Length - 1] = 1; } return(ByteUtils.ToHexString(rawValue, null)); }
public static string EncodeNumeric(BigInteger numericType, bool isSigned) { var rawValue = ToByteArray(numericType, isSigned); byte paddingValue = GetPaddingValue(numericType); var paddedRawValue = new byte[MAX_BYTE_LENGTH]; if (paddingValue != 0) { for (int i = 0; i < paddedRawValue.Length; i++) { paddedRawValue[i] = paddingValue; } } Array.Copy(rawValue, 0, paddedRawValue, MAX_BYTE_LENGTH - rawValue.Length, rawValue.Length); return(ByteUtils.ToHexString(paddedRawValue, null)); }
private static string EncodeBytes(byte[] value) { int length = value.Length; int mod = length % MAX_BYTE_LENGTH; byte[] dest; if (mod != 0) { int padding = MAX_BYTE_LENGTH - mod; dest = new byte[length + padding]; Array.Copy(value, 0, dest, 0, length); } else { dest = value; } return(ByteUtils.ToHexString(dest, null)); }
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)); } }