Coverage Summary for Class: ToBase58Check (co.rsk.pcc.bto)

Class Method, % Line, %
ToBase58Check 0% (0/6) 0% (0/18)
ToBase58Check$ExtendedVersionedChecksummedBytes 0% (0/1) 0% (0/1)
Total 0% (0/7) 0% (0/19)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 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.pcc.bto; 20  21 import co.rsk.pcc.ExecutionEnvironment; 22 import co.rsk.pcc.exception.NativeContractIllegalArgumentException; 23 import co.rsk.pcc.NativeMethod; 24 import org.ethereum.core.CallTransaction; 25 import org.ethereum.util.ByteUtil; 26  27 import java.math.BigInteger; 28  29 /** 30  * This implements the "toBase58Check" method 31  * that belongs to the HDWalletUtils native contract. 32  * 33  * @author Ariel Mendelzon 34  */ 35 public class ToBase58Check extends NativeMethod { 36  /** 37  * This is here since the bitcoinj Address class performs version 38  * validation depending on the actual network passed in, 39  * and the given constructor within VersionedChecksummedBytes is protected. 40  */ 41  private static class ExtendedVersionedChecksummedBytes extends co.rsk.bitcoinj.core.VersionedChecksummedBytes { 42  private static final long serialVersionUID = 3721215336363685421L; 43  44  public ExtendedVersionedChecksummedBytes(int version, byte[] bytes) { 45  super(version, bytes); 46  } 47  } 48  49  private final CallTransaction.Function function = CallTransaction.Function.fromSignature( 50  "toBase58Check", 51  new String[]{"bytes", "int256"}, 52  new String[]{"string"} 53  ); 54  55  private final static String HASH_NOT_PRESENT = "hash160 must be present"; 56  private final static String HASH_INVALID = "Invalid hash160 '%s' (should be 20 bytes and is %d bytes)"; 57  private final static String INVALID_VERSION = "version must be a numeric value between 0 and 255"; 58  59  public ToBase58Check(ExecutionEnvironment executionEnvironment) { 60  super(executionEnvironment); 61  } 62  63  @Override 64  public CallTransaction.Function getFunction() { 65  return function; 66  } 67  68  @Override 69  public Object execute(Object[] arguments) throws NativeContractIllegalArgumentException { 70  if (arguments == null) { 71  throw new NativeContractIllegalArgumentException(HASH_NOT_PRESENT); 72  } 73  byte[] hash = (byte[]) arguments[0]; 74  if (hash == null) { 75  throw new NativeContractIllegalArgumentException(HASH_NOT_PRESENT); 76  } 77  if (hash.length != 20) { 78  throw new NativeContractIllegalArgumentException(String.format( 79  HASH_INVALID, 80  ByteUtil.toHexString(hash), hash.length 81  )); 82  } 83  84  int version = ((BigInteger) arguments[1]).intValueExact(); 85  if (version < 0 || version >= 256) { 86  throw new NativeContractIllegalArgumentException(INVALID_VERSION); 87  } 88  89  return new ExtendedVersionedChecksummedBytes(version, hash).toBase58(); 90  } 91  92  @Override 93  public boolean isEnabled() { 94  return true; 95  } 96  97  @Override 98  public boolean onlyAllowsLocalCalls() { 99  return false; 100  } 101  102  @Override 103  public long getGas(Object[] parsedArguments, byte[] originalData) { 104  return 13_000L; 105  } 106 }