Coverage Summary for Class: EthModuleWalletEnabled (co.rsk.rpc.modules.eth)
Class |
Class, %
|
Method, %
|
Line, %
|
EthModuleWalletEnabled |
100%
(1/1)
|
100%
(5/5)
|
87.5%
(21/24)
|
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.rpc.modules.eth;
20
21 import co.rsk.core.RskAddress;
22 import co.rsk.core.Wallet;
23 import org.ethereum.core.Account;
24 import org.ethereum.crypto.ECKey;
25 import org.ethereum.crypto.HashUtil;
26 import org.ethereum.crypto.signature.ECDSASignature;
27 import org.ethereum.rpc.TypeConverter;
28 import org.ethereum.util.ByteUtil;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.nio.charset.StandardCharsets;
33 import java.util.Arrays;
34
35 import static org.ethereum.rpc.exception.RskJsonRpcRequestException.invalidParamError;
36
37 public class EthModuleWalletEnabled implements EthModuleWallet {
38
39 private static final Logger LOGGER = LoggerFactory.getLogger("web3");
40
41 private final Wallet wallet;
42
43 public EthModuleWalletEnabled(Wallet wallet) {
44 this.wallet = wallet;
45 }
46
47 @Override
48 public String sign(String addr, String data) {
49 String s = null;
50 try {
51 Account account = this.wallet.getAccount(new RskAddress(addr));
52 if (account == null) {
53 throw invalidParamError("Account not found");
54 }
55
56 return s = this.sign(data, account.getEcKey());
57 } finally {
58 LOGGER.debug("eth_sign({}, {}): {}", addr, data, s);
59 }
60 }
61
62 @Override
63 public String[] accounts() {
64 String[] s = null;
65 try {
66 return s = wallet.getAccountAddressesAsHex();
67 } finally {
68 LOGGER.debug("eth_accounts(): {}", Arrays.toString(s));
69 }
70 }
71
72 private String sign(String data, ECKey ecKey) {
73 byte[] dataHash = TypeConverter.stringHexToByteArray(data);
74 // 0x19 = 25, length should be an ascii decimals, message - original
75 String prefix = (char) 25 + "Ethereum Signed Message:\n" + dataHash.length;
76
77 byte[] messageHash = HashUtil.keccak256(ByteUtil.merge(
78 prefix.getBytes(StandardCharsets.UTF_8),
79 dataHash
80 ));
81 ECDSASignature signature = ECDSASignature.fromSignature(ecKey.sign(messageHash));
82
83 return TypeConverter.toJsonHex(ByteUtil.merge(
84 ByteUtil.bigIntegerToBytes(signature.getR()),
85 ByteUtil.bigIntegerToBytes(signature.getS()),
86 new byte[] {signature.getV()}
87 ));
88 }
89 }