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

Class Class, % Method, % Line, %
DeriveExtendedPublicKey 0% (0/1) 0% (0/10) 0% (0/37)


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.bitcoinj.core.NetworkParameters; 22 import co.rsk.bitcoinj.crypto.ChildNumber; 23 import co.rsk.bitcoinj.crypto.DeterministicKey; 24 import co.rsk.bitcoinj.crypto.HDKeyDerivation; 25 import co.rsk.bitcoinj.crypto.HDUtils; 26 import co.rsk.pcc.ExecutionEnvironment; 27 import co.rsk.pcc.exception.NativeContractIllegalArgumentException; 28 import co.rsk.pcc.NativeMethod; 29 import org.ethereum.core.CallTransaction; 30  31 import java.util.Arrays; 32 import java.util.List; 33  34 /** 35  * This implements the "deriveExtendedPublicKey" method 36  * that belongs to the HDWalletUtils native contract. 37  * 38  * @author Ariel Mendelzon 39  */ 40 public class DeriveExtendedPublicKey extends NativeMethod { 41  private final CallTransaction.Function function = CallTransaction.Function.fromSignature( 42  "deriveExtendedPublicKey", 43  new String[]{"string", "string"}, 44  new String[]{"string"} 45  ); 46  47  private final HDWalletUtilsHelper helper; 48  49  public DeriveExtendedPublicKey(ExecutionEnvironment executionEnvironment, HDWalletUtilsHelper helper) { 50  super(executionEnvironment); 51  this.helper = helper; 52  } 53  54  @Override 55  public CallTransaction.Function getFunction() { 56  return function; 57  } 58  59  @Override 60  public Object execute(Object[] arguments) throws NativeContractIllegalArgumentException { 61  if (arguments == null) { 62  throw new NativeContractIllegalArgumentException("Must provide xpub and path arguments. None was provided"); 63  } 64  String xpub = (String) arguments[0]; 65  String path = (String) arguments[1]; 66  67  NetworkParameters params = helper.validateAndExtractNetworkFromExtendedPublicKey(xpub); 68  DeterministicKey key; 69  try { 70  key = DeterministicKey.deserializeB58(xpub, params); 71  } catch (IllegalArgumentException e) { 72  throw new NativeContractIllegalArgumentException("Invalid extended public key", e); 73  } 74  75  // Path must be of the form S, with S ::= n || n/S with n an unsigned integer 76  77  // Covering special case: upon splitting a string, if the string ends with the delimiter, then 78  // there is no empty string as a last element. Make sure that the whole path starts and ends with a digit 79  // just in case. 80  if (path == null || path.length() == 0 || !isDecimal(path.substring(0,1)) || !isDecimal(path.substring(path.length()-1, path.length()))) { 81  throwInvalidPath(path); 82  } 83  84  String[] pathChunks = path.split("/"); 85  86  if (pathChunks.length > 10) { 87  throw new NativeContractIllegalArgumentException("Path should contain 10 levels at most"); 88  } 89  90  if (Arrays.stream(pathChunks).anyMatch(s -> !isDecimal(s))) { 91  throwInvalidPath(path); 92  } 93  94  List<ChildNumber> pathList; 95  try { 96  pathList = HDUtils.parsePath(path); 97  } catch (NumberFormatException ex) { 98  throw new NativeContractIllegalArgumentException(getInvalidPathErrorMessage(path), ex); 99  } 100  101  DeterministicKey derived = key; 102  for (ChildNumber pathItem : pathList) { 103  derived = HDKeyDerivation.deriveChildKey(derived, pathItem.getI()); 104  } 105  106  return derived.serializePubB58(params); 107  } 108  109  @Override 110  public boolean isEnabled() { 111  return true; 112  } 113  114  @Override 115  public boolean onlyAllowsLocalCalls() { 116  return false; 117  } 118  119  @Override 120  public long getGas(Object[] parsedArguments, byte[] originalData) { 121  return 107_000L; 122  } 123  124  private void throwInvalidPath(String path) throws NativeContractIllegalArgumentException { 125  throw new NativeContractIllegalArgumentException(getInvalidPathErrorMessage(path)); 126  } 127  128  private String getInvalidPathErrorMessage(String path) { 129  return String.format("Invalid path '%s'", path); 130  } 131  132  private boolean isDecimal(String s) { 133  try { 134  return Integer.parseInt(s) >= 0; 135  } catch(NumberFormatException e) { 136  return false; 137  } 138  } 139 }