Coverage Summary for Class: TypeConverter (org.ethereum.rpc)

Class Class, % Method, % Line, %
TypeConverter 100% (1/1) 68.8% (11/16) 72.2% (26/36)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package org.ethereum.rpc; 20  21 import co.rsk.core.Coin; 22 import org.bouncycastle.util.encoders.Hex; 23 import org.ethereum.util.ByteUtil; 24  25 import java.math.BigInteger; 26 import java.nio.charset.StandardCharsets; 27 import java.util.regex.Pattern; 28  29 /** 30  * Created by Ruben on 19/11/2015. 31  */ 32 public class TypeConverter { 33  private static final Pattern LEADING_ZEROS_PATTERN = Pattern.compile("0x(0)+"); 34  35  private TypeConverter() { 36  throw new IllegalAccessError("Utility class"); 37  } 38  39  public static BigInteger stringNumberAsBigInt(String input) { 40  if (input.startsWith("0x")) { 41  return TypeConverter.stringHexToBigInteger(input); 42  } else { 43  return TypeConverter.stringDecimalToBigInteger(input); 44  } 45  } 46  47  public static BigInteger stringHexToBigInteger(String input) { 48  if(!input.startsWith("0x")) { 49  throw new NumberFormatException("Invalid hex number, expected 0x prefix"); 50  } 51  String hexa = input.substring(2); 52  return new BigInteger(hexa, 16); 53  } 54  55  private static BigInteger stringDecimalToBigInteger(String input) { 56  return new BigInteger(input); 57  } 58  59  public static byte[] stringHexToByteArray(String x) { 60  String result = x; 61  if (x.startsWith("0x")) { 62  result = x.substring(2); 63  } 64  if (result.length() % 2 != 0) { 65  result = "0" + result; 66  } 67  return Hex.decode(result); 68  } 69  70  public static byte[] stringToByteArray(String input) { 71  return input.getBytes(StandardCharsets.UTF_8); 72  } 73  74  public static String toJsonHex(byte[] x) { 75  String result = toUnformattedJsonHex(x); 76  77  if ("0x".equals(result)) { 78  return "0x00"; 79  } 80  81  return result; 82  } 83  84  public static String toJsonHex(Coin x) { 85  return x != null ? x.asBigInteger().toString() : "" ; 86  } 87  88  public static String toJsonHex(String x) { 89  return "0x"+x; 90  } 91  92  /** 93  * @return A Hex representation of n WITHOUT leading zeroes 94  */ 95  public static String toQuantityJsonHex(long n) { 96  return "0x" + Long.toHexString(n); 97  } 98  99  /** 100  * @return A Hex representation of n WITHOUT leading zeroes 101  */ 102  public static String toQuantityJsonHex(BigInteger n) { 103  return "0x"+ n.toString(16); 104  } 105  106  /** 107  * Converts a byte array to a string according to ethereum json-rpc specifications, null and empty 108  * convert to 0x. 109  * 110  * @param x An unformatted byte array 111  * @return A hex representation of the input with two hex digits per byte 112  */ 113  public static String toUnformattedJsonHex(byte[] x) { 114  return "0x" + (x == null ? "" : ByteUtil.toHexString(x)); 115  } 116  117  /** 118  * Converts a byte array representing a quantity according to ethereum json-rpc specifications. 119  * 120  * <p> 121  * 0x000AEF -> 0x2AEF 122  * <p> 123  * 0x00 -> 0x0 124  * @param x A hex string with or without leading zeroes ("0x00AEF"). If null, it is considered as zero. 125  * @return A hex string without leading zeroes ("0xAEF") 126  */ 127  public static String toQuantityJsonHex(byte[] x) { 128  String withoutLeading = LEADING_ZEROS_PATTERN.matcher(toJsonHex(x)).replaceFirst("0x"); 129  if ("0x".equals(withoutLeading)) { 130  return "0x0"; 131  } 132  133  return withoutLeading; 134  } 135  136  /** 137  * Converts "0x876AF" to "876AF" 138  */ 139  public static String removeZeroX(String str) { 140  return str.substring(2); 141  } 142  143  public static long JSonHexToLong(String x) { 144  if (!x.startsWith("0x")) { 145  throw new NumberFormatException("Incorrect hex syntax"); 146  } 147  x = x.substring(2); 148  return Long.parseLong(x, 16); 149  } 150 }