Coverage Summary for Class: RskAddress (co.rsk.core)

Class Class, % Method, % Line, %
RskAddress 100% (1/1) 100% (12/12) 89.7% (26/29)


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 co.rsk.core; 20  21 import com.google.common.primitives.UnsignedBytes; 22 import org.ethereum.rpc.TypeConverter; 23 import org.ethereum.util.ByteUtil; 24 import org.ethereum.vm.DataWord; 25  26 import java.util.Arrays; 27 import java.util.Comparator; 28  29 /** 30  * Immutable representation of an RSK address. 31  * It is a simple wrapper on the raw byte[]. 32  * 33  * @author Ariel Mendelzon 34  */ 35 public class RskAddress { 36  37  /** 38  * This is the size of an RSK address in bytes. 39  */ 40  public static final int LENGTH_IN_BYTES = 20; 41  42  private static final RskAddress NULL_ADDRESS = new RskAddress(); 43  44  /** 45  * This compares using the lexicographical order of the sender unsigned bytes. 46  */ 47  public static final Comparator<RskAddress> LEXICOGRAPHICAL_COMPARATOR = Comparator.comparing( 48  RskAddress::getBytes, 49  UnsignedBytes.lexicographicalComparator()); 50  51  private final byte[] bytes; 52  53  /** 54  * @param address a data word containing an address in the last 20 bytes. 55  */ 56  public RskAddress(DataWord address) { 57  this(address.getLast20Bytes()); 58  } 59  60  /** 61  * @param address the hex-encoded 20 bytes long address, with or without 0x prefix. 62  */ 63  public RskAddress(String address) { 64  this(TypeConverter.stringHexToByteArray(address)); 65  } 66  67  /** 68  * @param bytes the 20 bytes long raw address bytes. 69  */ 70  public RskAddress(byte[] bytes) { 71  if (bytes.length != LENGTH_IN_BYTES) { 72  throw new RuntimeException(String.format("An RSK address must be %d bytes long", LENGTH_IN_BYTES)); 73  } 74  75  this.bytes = bytes; 76  } 77  78  /** 79  * This instantiates the contract creation address. 80  */ 81  private RskAddress() { 82  this.bytes = new byte[0]; 83  } 84  85  /** 86  * @return the null address, which is the receiver of contract creation transactions. 87  */ 88  public static RskAddress nullAddress() { 89  return NULL_ADDRESS; 90  } 91  92  public byte[] getBytes() { 93  return bytes; 94  } 95  96  public String toHexString() { 97  return ByteUtil.toHexString(bytes); 98  } 99  100  @Override 101  public boolean equals(Object other) { 102  if (this == other) { 103  return true; 104  } 105  106  if (other == null || this.getClass() != other.getClass()) { 107  return false; 108  } 109  110  RskAddress otherSender = (RskAddress) other; 111  return Arrays.equals(bytes, otherSender.bytes); 112  } 113  114  @Override 115  public int hashCode() { 116  return Arrays.hashCode(bytes); 117  } 118  119  /** 120  * @return a DEBUG representation of the address, mainly used for logging. 121  */ 122  @Override 123  public String toString() { 124  return toHexString(); 125  } 126  127  public String toJsonString() { 128  if (NULL_ADDRESS.equals(this)) { 129  return null; 130  } 131  return TypeConverter.toUnformattedJsonHex(this.getBytes()); 132  } 133 }