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

Class Class, % Method, % Line, %
ExtractPublicKeyFromExtendedPublicKey 0% (0/1) 0% (0/6) 0% (0/16)


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.DeterministicKey; 23 import co.rsk.pcc.ExecutionEnvironment; 24 import co.rsk.pcc.exception.NativeContractIllegalArgumentException; 25 import co.rsk.pcc.NativeMethod; 26 import org.ethereum.core.CallTransaction; 27  28 /** 29  * This implements the "extractPublicKeyFromExtendedPublicKey" method 30  * that belongs to the HDWalletUtils native contract. 31  * 32  * @author Ariel Mendelzon 33  */ 34 public class ExtractPublicKeyFromExtendedPublicKey extends NativeMethod { 35  private final CallTransaction.Function function = CallTransaction.Function.fromSignature( 36  "extractPublicKeyFromExtendedPublicKey", 37  new String[]{"string"}, 38  new String[]{"bytes"} 39  ); 40  41  private final HDWalletUtilsHelper helper; 42  43  private final static String INVALID_EXTENDED_PUBLIC_KEY = "Invalid extended public key '%s"; 44  45  public ExtractPublicKeyFromExtendedPublicKey(ExecutionEnvironment executionEnvironment, HDWalletUtilsHelper helper) { 46  super(executionEnvironment); 47  this.helper = helper; 48  } 49  50  @Override 51  public CallTransaction.Function getFunction() { 52  return function; 53  } 54  55  @Override 56  public Object execute(Object[] arguments) throws NativeContractIllegalArgumentException { 57  if (arguments == null) { 58  throw new NativeContractIllegalArgumentException(String.format(INVALID_EXTENDED_PUBLIC_KEY, null)); 59  } 60  String xpub = (String) arguments[0]; 61  62  NetworkParameters params = helper.validateAndExtractNetworkFromExtendedPublicKey(xpub); 63  DeterministicKey key; 64  try { 65  key = DeterministicKey.deserializeB58(xpub, params); 66  } catch (IllegalArgumentException e) { 67  throw new NativeContractIllegalArgumentException(String.format(INVALID_EXTENDED_PUBLIC_KEY, xpub), e); 68  } 69  70  return key.getPubKeyPoint().getEncoded(true); 71  } 72  73  @Override 74  public boolean isEnabled() { 75  return true; 76  } 77  78  @Override 79  public boolean onlyAllowsLocalCalls() { 80  return false; 81  } 82  83  @Override 84  public long getGas(Object[] parsedArguments, byte[] originalData) { 85  return 11_300L; 86  } 87 }