Skip to content
Open
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
26 changes: 26 additions & 0 deletions src/main/java/org/tron/p2p/dns/tree/Algorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public static byte[] sigData(String msg, String privateKey) {
return data;
}

public static byte[] sigData(byte[] msg, String privateKey) {
ECKeyPair keyPair = generateKeyPair(privateKey);
Sign.SignatureData signature = Sign.signMessage(msg, keyPair, true);
byte[] data = new byte[65];
System.arraycopy(signature.getR(), 0, data, 0, 32);
System.arraycopy(signature.getS(), 0, data, 32, 32);
data[64] = signature.getV()[0];
return data;
}

public static BigInteger recoverPublicKey(String msg, byte[] sig) throws SignatureException {
int recId = sig[64];
if (recId < 27) {
Expand All @@ -78,6 +88,16 @@ public static BigInteger recoverPublicKey(String msg, byte[] sig) throws Signatu
return Sign.signedMessageToKey(msg.getBytes(), signature);
}

public static BigInteger recoverPublicKey(byte[] msg, byte[] sig) throws SignatureException {
int recId = sig[64];
if (recId < 27) {
recId += 27;
}
Sign.SignatureData signature = new SignatureData((byte) recId, ByteArray.subArray(sig, 0, 32),
ByteArray.subArray(sig, 32, 64));
return Sign.signedMessageToKey(msg, signature);
}

/**
* @param publicKey uncompress hex publicKey
* @param msg to be hashed message
Expand All @@ -88,6 +108,12 @@ public static boolean verifySignature(String publicKey, String msg, byte[] sig)
BigInteger pubKeyRecovered = recoverPublicKey(msg, sig);
return pubKey.equals(pubKeyRecovered);
}
public static boolean verifySignature(String publicKey, byte[] msg, byte[] sig)
throws SignatureException {
BigInteger pubKey = new BigInteger(publicKey, 16);
BigInteger pubKeyRecovered = recoverPublicKey(msg, sig);
return pubKey.equals(pubKeyRecovered);
}

//we only use fix width hash
public static boolean isValidHash(String base32Hash) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/protos/Discover.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ message PingMessage {
Endpoint to = 2;
int32 version = 3;
int64 timestamp = 4;
bytes nodePubkey = 5;
bytes nodeSig = 6;
}

message PongMessage {
Endpoint from = 1;
int32 echo = 2;
int64 timestamp = 3;
bytes nodePubkey = 4;
bytes nodeSig = 5;
}

message FindNeighbours {
Endpoint from = 1;
bytes targetId = 2;
int64 timestamp = 3;
bytes nodePubkey = 4;
bytes nodeSig = 5;
}

message Neighbours {
Endpoint from = 1;
repeated Endpoint neighbours = 2;
int64 timestamp = 3;
bytes nodePubkey = 4;
bytes nodeSig = 5;
}

message EndPoints {
Expand Down
Loading