Coverage Summary for Class: AuthInitiateMessageV4 (org.ethereum.net.rlpx)
Class |
Class, %
|
Method, %
|
Line, %
|
AuthInitiateMessageV4 |
0%
(0/1)
|
0%
(0/6)
|
0%
(0/44)
|
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.crypto.signature.ECDSASignature;
25 import org.ethereum.util.ByteUtil;
26 import org.ethereum.util.RLP;
27 import org.ethereum.util.RLPList;
28
29 import static org.bouncycastle.util.BigIntegers.asUnsignedByteArray;
30 import static org.ethereum.util.ByteUtil.merge;
31
32 /**
33 * Auth Initiate message defined by EIP-8
34 *
35 * @author mkalinin
36 * @since 17.02.2016
37 */
38 public class AuthInitiateMessageV4 {
39
40 private ECDSASignature signature; // 65 bytes
41 ECPoint publicKey; // 64 bytes - uncompressed and no type byte
42 byte[] nonce; // 32 bytes
43 int version = 4; // 4 bytes
44
45 public AuthInitiateMessageV4() {
46 }
47
48 static AuthInitiateMessageV4 decode(byte[] wire) {
49 AuthInitiateMessageV4 message = new AuthInitiateMessageV4();
50
51 RLPList params = (RLPList) RLP.decode2OneItem(wire, 0);
52
53 byte[] signatureBytes = params.get(0).getRLPData();
54 int offset = 0;
55 byte[] r = new byte[32];
56 byte[] s = new byte[32];
57 System.arraycopy(signatureBytes, offset, r, 0, 32);
58 offset += 32;
59 System.arraycopy(signatureBytes, offset, s, 0, 32);
60 offset += 32;
61 int v = signatureBytes[offset] + 27;
62 message.signature = ECDSASignature.fromComponents(r, s, (byte)v);
63
64 byte[] publicKeyBytes = params.get(1).getRLPData();
65 byte[] bytes = new byte[65];
66 System.arraycopy(publicKeyBytes, 0, bytes, 1, 64);
67 bytes[0] = 0x04; // uncompressed
68 message.publicKey = ECKey.CURVE.getCurve().decodePoint(bytes);
69
70 message.nonce = params.get(2).getRLPData();
71
72 byte[] versionBytes = params.get(3).getRLPData();
73 message.version = ByteUtil.byteArrayToInt(versionBytes);
74
75 return message;
76 }
77
78 public byte[] encode() {
79
80 byte[] rsigPad = new byte[32];
81 byte[] rsig = asUnsignedByteArray(signature.getR());
82 System.arraycopy(rsig, 0, rsigPad, rsigPad.length - rsig.length, rsig.length);
83
84 byte[] ssigPad = new byte[32];
85 byte[] ssig = asUnsignedByteArray(signature.getS());
86 System.arraycopy(ssig, 0, ssigPad, ssigPad.length - ssig.length, ssig.length);
87
88 byte[] publicKey = new byte[64];
89 System.arraycopy(this.publicKey.getEncoded(false), 1, publicKey, 0, publicKey.length);
90
91 byte[] sigBytes = RLP.encode(merge(rsigPad, ssigPad, new byte[]{EncryptionHandshake.recIdFromSignatureV(signature.getV())}));
92 byte[] publicBytes = RLP.encode(publicKey);
93 byte[] nonceBytes = RLP.encode(nonce);
94 byte[] versionBytes = RLP.encodeInt(version);
95
96 return RLP.encodeList(sigBytes, publicBytes, nonceBytes, versionBytes);
97 }
98
99 public ECDSASignature getSignature() {
100 return signature;
101 }
102
103 public void setSignature(ECDSASignature signature) {
104 this.signature = signature;
105 }
106
107 @Override
108 public String toString() {
109
110 byte[] sigBytes = merge(asUnsignedByteArray(signature.getR()),
111 asUnsignedByteArray(signature.getS()), new byte[]{EncryptionHandshake.recIdFromSignatureV(signature.getV())});
112
113 return "AuthInitiateMessage{" +
114 "\n sigBytes=" + ByteUtil.toHexString(sigBytes) +
115 "\n publicKey=" + ByteUtil.toHexString(publicKey.getEncoded(false)) +
116 "\n nonce=" + ByteUtil.toHexString(nonce) +
117 "\n version=" + version +
118 "\n}";
119 }
120 }