Coverage Summary for Class: AuthResponseMessage (org.ethereum.net.rlpx)

Class Class, % Method, % Line, %
AuthResponseMessage 0% (0/1) 0% (0/5) 0% (0/30)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) 5  * 6  * This program is free software: you can redistribute it and/or modify 7  * it under the terms of the GNU Lesser General Public License as published by 8  * the Free Software Foundation, either version 3 of the License, or 9  * (at your option) any later version. 10  * 11  * This program is distributed in the hope that it will be useful, 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14  * GNU Lesser General Public License for more details. 15  * 16  * You should have received a copy of the GNU Lesser General Public License 17  * along with this program. If not, see <http://www.gnu.org/licenses/>. 18  */ 19  20 package org.ethereum.net.rlpx; 21  22 import org.bouncycastle.math.ec.ECPoint; 23 import org.ethereum.crypto.ECKey; 24 import org.ethereum.util.ByteUtil; 25  26 /** 27  * Authentication response message, to be wrapped inside 28  * 29  * Created by devrandom on 2015-04-07. 30  */ 31 public class AuthResponseMessage { 32  ECPoint ephemeralPublicKey; // 64 bytes - uncompressed and no type byte 33  byte[] nonce; // 32 bytes 34  boolean isTokenUsed; // 1 byte - 0x00 or 0x01 35  36  static AuthResponseMessage decode(byte[] wire) { 37  int offset = 0; 38  AuthResponseMessage message = new AuthResponseMessage(); 39  byte[] bytes = new byte[65]; 40  System.arraycopy(wire, offset, bytes, 1, 64); 41  offset += 64; 42  bytes[0] = 0x04; // uncompressed 43  message.ephemeralPublicKey = ECKey.CURVE.getCurve().decodePoint(bytes); 44  message.nonce = new byte[32]; 45  System.arraycopy(wire, offset, message.nonce, 0, 32); 46  offset += message.nonce.length; 47  byte tokenUsed = wire[offset]; 48  offset += 1; 49  if (tokenUsed != 0x00 && tokenUsed != 0x01) { 50  throw new RuntimeException("invalid boolean"); // TODO specific exception 51  } 52  message.isTokenUsed = (tokenUsed == 0x01); 53  return message; 54  } 55  56  public static int getLength() { 57  return 64+32+1; 58  } 59  60  public byte[] encode() { 61  byte[] buffer = new byte[getLength()]; 62  int offset = 0; 63  byte[] publicBytes = ephemeralPublicKey.getEncoded(false); 64  System.arraycopy(publicBytes, 1, buffer, offset, publicBytes.length - 1); 65  offset += publicBytes.length - 1; 66  System.arraycopy(nonce, 0, buffer, offset, nonce.length); 67  offset += nonce.length; 68  buffer[offset] = (byte)(isTokenUsed ? 0x01 : 0x00); 69  offset += 1; 70  return buffer; 71  } 72  73  @Override 74  public String toString() { 75  return "AuthResponseMessage{" + 76  "\n ephemeralPublicKey=" + ephemeralPublicKey + 77  "\n nonce=" + ByteUtil.toHexString(nonce) + 78  "\n isTokenUsed=" + isTokenUsed + 79  '}'; 80  } 81 }