Coverage Summary for Class: AddressBasedAuthorizer (co.rsk.peg)

Class Method, % Line, %
AddressBasedAuthorizer 20% (1/5) 33.3% (4/12)
AddressBasedAuthorizer$1 0% (0/1) 0% (0/1)
AddressBasedAuthorizer$MinimumRequiredCalculation 100% (1/1) 100% (1/1)
Total 28.6% (2/7) 35.7% (5/14)


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.peg; 20  21 import co.rsk.core.RskAddress; 22 import org.ethereum.core.Transaction; 23 import org.ethereum.crypto.ECKey; 24  25 import java.util.Arrays; 26 import java.util.List; 27 import java.util.stream.Collectors; 28  29 /** 30  * Authorizes an operation based 31  * on an RSK address. 32  * 33  * @author Ariel Mendelzon 34  */ 35 public class AddressBasedAuthorizer { 36  public enum MinimumRequiredCalculation { ONE, MAJORITY, ALL }; 37  38  protected List<byte[]> authorizedAddresses; 39  protected MinimumRequiredCalculation requiredCalculation; 40  41  public AddressBasedAuthorizer(List<ECKey> authorizedKeys, MinimumRequiredCalculation requiredCalculation) { 42  this.authorizedAddresses = authorizedKeys.stream().map(key -> key.getAddress()).collect(Collectors.toList()); 43  this.requiredCalculation = requiredCalculation; 44  } 45  46  public boolean isAuthorized(RskAddress sender) { 47  return authorizedAddresses.stream() 48  .anyMatch(address -> Arrays.equals(address, sender.getBytes())); 49  } 50  51  public boolean isAuthorized(Transaction tx) { 52  return isAuthorized(tx.getSender()); 53  } 54  55  public int getNumberOfAuthorizedKeys() { 56  return authorizedAddresses.size(); 57  } 58  59  public int getRequiredAuthorizedKeys() { 60  switch (requiredCalculation) { 61  case ONE: 62  return 1; 63  case MAJORITY: 64  return getNumberOfAuthorizedKeys() / 2 + 1; 65  case ALL: 66  default: 67  return getNumberOfAuthorizedKeys(); 68  } 69  } 70 }