Skip to content

refactor(crypto,api): improve code quality #6417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.commons.lang3.tuple.Triple;
import org.tron.common.crypto.Blake2bfMessageDigest;
import org.tron.common.crypto.Hash;
import org.tron.common.crypto.Rsv;
import org.tron.common.crypto.SignUtils;
import org.tron.common.crypto.SignatureInterface;
import org.tron.common.crypto.zksnark.BN128;
Expand Down Expand Up @@ -352,22 +353,13 @@ private static byte[] encodeMultiRes(byte[]... words) {
}

private static byte[] recoverAddrBySign(byte[] sign, byte[] hash) {
byte v;
byte[] r;
byte[] s;
byte[] out = null;
if (ArrayUtils.isEmpty(sign) || sign.length < 65) {
return new byte[0];
}
try {
r = Arrays.copyOfRange(sign, 0, 32);
s = Arrays.copyOfRange(sign, 32, 64);
v = sign[64];
if (v < 27) {
v += 27;
}

SignatureInterface signature = SignUtils.fromComponents(r, s, v,
Rsv rsv = Rsv.fromSignature(sign);
SignatureInterface signature = SignUtils.fromComponents(rsv.getR(), rsv.getS(), rsv.getV(),
CommonParameter.getInstance().isECKeyCryptoEngine());
if (signature.validateComponents()) {
out = SignUtils.signatureToAddress(hash, signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.tron.common.crypto.ECKey.ECDSASignature;
import org.tron.common.crypto.Rsv;
import org.tron.common.crypto.SignInterface;
import org.tron.common.crypto.SignUtils;
import org.tron.common.es.ExecutorServiceManager;
Expand Down Expand Up @@ -456,14 +457,8 @@ public static long getCallValue(Transaction.Contract contract) {
}

public static String getBase64FromByteString(ByteString sign) {
byte[] r = sign.substring(0, 32).toByteArray();
byte[] s = sign.substring(32, 64).toByteArray();
byte v = sign.byteAt(64);
if (v < 27) {
v += 27; //revId -> v
}
ECDSASignature signature = ECDSASignature.fromComponents(r, s, v);
return signature.toBase64();
Rsv rsv = Rsv.fromSignature(sign.toByteArray());
return ECDSASignature.fromComponents(rsv.getR(), rsv.getS(), rsv.getV()).toBase64();
}

public static boolean validateSignature(Transaction transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private long computeReward(long cycle, List<Pair<byte[], Long>> votes) {
}
long userVote = vote.getValue();
double voteRate = (double) userVote / totalVote;
reward += voteRate * totalReward;
reward += (long) (voteRate * totalReward);
}
return reward;
}
Expand Down
26 changes: 26 additions & 0 deletions crypto/src/main/java/org/tron/common/crypto/Rsv.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.tron.common.crypto;


import java.util.Arrays;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class Rsv {

private final byte[] r;
private final byte[] s;
private final byte v;


public static Rsv fromSignature(byte[] sign) {
byte[] r = Arrays.copyOfRange(sign, 0, 32);
byte[] s = Arrays.copyOfRange(sign, 32, 64);
byte v = sign[64];
if (v < 27) {
v += (byte) 27; //revId -> v
}
return new Rsv(r, s, v);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static short decodeShort(byte[] data, int index) {
byte pow = (byte) (length - 1);
for (int i = 1; i <= length; ++i) {
// << (8 * pow) == bit shift to 0 (*1), 8 (*256) , 16 (*65..)
value += (data[index + i] & 0xFF) << (8 * pow);
value += (short) ((data[index + i] & 0xFF) << (8 * pow));
pow--;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
response.getWriter().println("{\"brokerage\": " + value + "}");
} catch (DecoderException | IllegalArgumentException e) {
try {
response.getWriter()
.println("{\"Error\": " + "\"INVALID address, " + e.getMessage() + "\"}");
response.getWriter().println("{\"Error\": " + "\"INVALID address\"}");
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
response.getWriter().println("{\"reward\": " + value + "}");
} catch (DecoderException | IllegalArgumentException e) {
try {
response.getWriter()
.println("{\"Error\": " + "\"INVALID address, " + e.getMessage() + "\"}");
response.getWriter().println("{\"Error\": " + "\"INVALID address\"}");
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
String input = request.getParameter("value");
fillResponse(ByteString.copyFrom(ByteArray.fromHexString(input)), visible, response);
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(e.getMessage());
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand All @@ -46,12 +41,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
JsonFormat.merge(params.getParams(), build, params.isVisible());
fillResponse(build.build().getValue(), params.isVisible(), response);
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(e.getMessage());
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.tron.core.services.http.solidity;

import com.google.protobuf.ByteString;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -37,12 +36,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
response.getWriter().println(JsonFormat.printToString(transInfo, visible));
}
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(e.getMessage());
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand All @@ -60,12 +54,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
response.getWriter().println(JsonFormat.printToString(transInfo, params.isVisible()));
}
} catch (Exception e) {
logger.debug("Exception: {}", e.getMessage());
try {
response.getWriter().println(e.getMessage());
} catch (IOException ioe) {
logger.debug("IOException: {}", ioe.getMessage());
}
Util.processError(e, response);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.google.protobuf.ByteString;
import java.util.Arrays;
import lombok.Getter;
import lombok.ToString;
import org.tron.common.crypto.Rsv;
import org.tron.common.utils.ByteArray;
import org.tron.core.Wallet;
import org.tron.core.capsule.BlockCapsule;
Expand Down Expand Up @@ -65,16 +65,10 @@ private void parseSignature(Transaction tx) {
}

ByteString signature = tx.getSignature(0); // r[32] + s[32] + v[1]
byte[] signData = signature.toByteArray();
byte[] rByte = Arrays.copyOfRange(signData, 0, 32);
byte[] sByte = Arrays.copyOfRange(signData, 32, 64);
byte vByte = signData[64];
if (vByte < 27) {
vByte += 27;
}
v = ByteArray.toJsonHex(vByte);
r = ByteArray.toJsonHex(rByte);
s = ByteArray.toJsonHex(sByte);
Rsv rsv = Rsv.fromSignature(signature.toByteArray());
r = ByteArray.toJsonHex(rsv.getR());
s = ByteArray.toJsonHex(rsv.getS());
v = ByteArray.toJsonHex(rsv.getV());
}

private String parseInput(Transaction tx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public DiversifierT defaultDiversifier() throws BadItemException, ZksnarkExcepti
throw new BadItemException(
"librustzcash_check_diversifier does not return valid diversifier");
}
blob[33] += 1;
blob[33] += (byte) 1;
} finally {
JLibsodium.freeState(state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import static org.tron.core.capsule.TransactionCapsule.getBase64FromByteString;

import com.google.protobuf.ByteString;
import java.security.SignatureException;
Expand Down Expand Up @@ -116,20 +117,6 @@ public static byte[] getOwner(Transaction.Contract contract) {
}
}

/**
* constructor.
*/

public static String getBase64FromByteString(ByteString sign) {
byte[] r = sign.substring(0, 32).toByteArray();
byte[] s = sign.substring(32, 64).toByteArray();
byte v = sign.byteAt(64);
if (v < 27) {
v += 27; //revId -> v
}
ECDSASignature signature = ECDSASignature.fromComponents(r, s, v);
return signature.toBase64();
}

/*
* 1. check hash
Expand Down