Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7386b04
Added sapphire end-points
grvgoel81 May 23, 2023
1c8d855
Added sapphire torus networks and Utils.java
grvgoel81 May 25, 2023
a15380a
Added sapphire config json and test-cases for sapphire networks
grvgoel81 May 26, 2023
ac6f1cb
code refactoring
grvgoel81 May 26, 2023
67f9c81
code refactoring
grvgoel81 May 26, 2023
d667a0f
code refactoring
grvgoel81 May 26, 2023
f55c81a
code refactoring
grvgoel81 May 29, 2023
2d8d450
code refactoring
grvgoel81 May 30, 2023
1abeaba
Updated sapphire networks configuration, modified test cases and adde…
grvgoel81 Jul 3, 2023
b6f3e30
code refactoring
grvgoel81 Jul 3, 2023
6ea7768
code refactoring
grvgoel81 Jul 3, 2023
be65b95
Added legacy support in FetchNodeDetails.java
grvgoel81 Jul 3, 2023
073d696
code refactored
grvgoel81 Jul 4, 2023
5c6c3fe
code refactored
grvgoel81 Jul 4, 2023
ad219e8
code refactored
grvgoel81 Jul 5, 2023
7dcf847
code refactored
grvgoel81 Jul 12, 2023
a8782d4
refactor: removing sapphire-devnet network
grvgoel81 Aug 22, 2023
a476951
refactor: Mainnet migration changes added.
grvgoel81 Aug 28, 2023
26f5bd4
refactor: remove unused code in APiUtils and remove org.json:json lib…
grvgoel81 Sep 5, 2023
2551a1e
refactor: remove unused code in APiUtils and remove org.json:json lib…
grvgoel81 Sep 5, 2023
2696997
refactor: remove old code
grvgoel81 Sep 5, 2023
05b6d2d
updated test cases for aqua and cyan after migration
grvgoel81 Oct 24, 2023
0c95030
fully working
chaitanyapotti Nov 1, 2023
fdb07bc
DEFAULT NETWORK SHOULD BE SAPPHIRE MAINNET
chaitanyapotti Nov 1, 2023
6fc4940
fix test
chaitanyapotti Nov 1, 2023
21d3a41
NETWORK_MAP and SIGNER_MAP added in Utils.java
grvgoel81 Nov 1, 2023
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
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ repositories {
}

dependencies {
implementation 'org.web3j:core:4.8.8-android'
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.google.code.gson:gson:2.9.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
testImplementation 'org.slf4j:slf4j-simple:1.7.36'
Expand Down
119 changes: 40 additions & 79 deletions src/main/java/org/torusresearch/fetchnodedetails/FetchNodeDetails.java
Original file line number Diff line number Diff line change
@@ -1,106 +1,67 @@
package org.torusresearch.fetchnodedetails;

import com.google.gson.GsonBuilder;

import org.torusresearch.fetchnodedetails.types.APIUtils;
import org.torusresearch.fetchnodedetails.types.FNDResponse;
import org.torusresearch.fetchnodedetails.types.NodeDetails;
import org.torusresearch.fetchnodedetails.types.NodeInfo;
import org.torusresearch.fetchnodedetails.types.TorusNetwork;
import org.torusresearch.fetchnodedetails.types.TorusNodePub;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.Hash;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.DefaultGasProvider;
import org.torusresearch.fetchnodedetails.types.Utils;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class FetchNodeDetails {

public static String PROXY_ADDRESS_MAINNET = "0xf20336e16B5182637f09821c27BDe29b0AFcfe80";
public static String PROXY_ADDRESS_TESTNET = "0xd084604e5FA387FbC2Da8bAab07fDD6aDED4614A";
public static String PROXY_ADDRESS_CYAN = "0x9f072ba19b3370e512aa1b4bfcdaf97283168005";
public static String PROXY_ADDRESS_AQUA = "0x29Dea82a0509153b91040ee13cDBba0f03efb625";
public static String PROXY_ADDRESS_CELESTE = "0x6Bffb4e89453069E7487f0fa5c9f4a2D771cce6c";
private List<String> multi_cluster_networks = Arrays.asList("celeste");


public static HashMap<TorusNetwork, String> NETWORK_MAP = new HashMap<TorusNetwork, String>() {
{
put(TorusNetwork.MAINNET, "mainnet");
put(TorusNetwork.TESTNET, "goerli");
put(TorusNetwork.CYAN, "polygon-mainnet");
put(TorusNetwork.AQUA, "polygon-mainnet");
put(TorusNetwork.CELESTE, "polygon-mainnet");
}
};

public static NodeDetails NODE_DETAILS_MAINNET = new NodeDetails("19", "0xf20336e16B5182637f09821c27BDe29b0AFcfe80", new String[]{"https://torus-19.torusnode.com/jrpc", "https://torus-node.ens.domains/jrpc", "https://torus-node.matic.network/jrpc", "https://torus.zilliqa.network/jrpc", "https://torus-mainnet.cosmos.network/jrpc", "https://torus2.etherscan.com/jrpc", "https://torus-node-v2.skalelabs.com/jrpc", "https://torus-node.binancex.dev/jrpc", "https://torusnode.ont.io/jrpc"}, new BigInteger[]{new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4"), new BigInteger("5"), new BigInteger("6"), new BigInteger("7"), new BigInteger("8"), new BigInteger("9"),}, new TorusNodePub[]{new TorusNodePub("bbe83c64177c3775550e6ba6ac2bc059f6847d644c9e4894e42c60d7974d8c2b", "82b49a7caf70def38cdad2740af45c1e4f969650105c5019a29bb18b21a9acb5"), new TorusNodePub("c208cac4ef9a47d386097a9c915b28e9cb89213abee8d26a17198ee261201b0d", "c7db2fe4631109f40833de9dc78d07e35706549ee48fa557b33e4e75e1047873"), new TorusNodePub("ca1766bb426d4ca5582818a0c5439d560ea64f5baa060793ab29dd3d0ceacfe", "d46c1d08c40e1306e1bca328c2287b8268166b11a1ba4b8442ea2ad0c5e32152"), new TorusNodePub("c3934dd2f6f4b3d2e1e398cc501e143c1e1a381b52feb6d1525af34d16253768", "71f5141a5035799099f5ea3e241e66946bc55dc857ac3bd7d6fcdb8dcd3eeeef"), new TorusNodePub("22e66f1929631d00bf026227581597f085fd94fd952fc0dca9f0833398b5c064", "6088b3912e10a1e9d50355a609c10db7d188f16a2e2fd7357e51bf4f6a74f0a1"), new TorusNodePub("9dc9fa410f3ce9eb70df70cdea00a49f2c4cc7a31c08c0dab5f863ed35ff5139", "627a291cb87a75c61da3f65d6818e1e05e360217179817ed27e8c73bca7ec122"), new TorusNodePub("118b9fc07e97b096d899b9f6658463ce6a8caa64038e37fc969df4e6023dd8c6", "baf9fa4e51770f4796ea165dd03a769b8606681a38954a0a92c4cbffd6609ce9"), new TorusNodePub("8a6d8b925da15a273dec3d8f8395ec35cd6878f274b2b180e4e106999db64043", "96f67f870c157743da0b1eb84d89bf30500d74dc84c11f501ee1cb013acc8c46"), new TorusNodePub("39cecb62e863729f572f7dfc46c24867981bf04bb405fed0df39e33984bfade5", "61c2364434012e68a2be2e9952805037e52629d7762fafc8e10e9fb5bad8f790")}, false);

private final String proxyAddress;
private final NodeDetails nodeDetails = new NodeDetails();
private final String providerUrl;
private TorusLookup torusLookup;
private TorusNetwork torusNetwork = TorusNetwork.SAPPHIRE_MAINNET;

public FetchNodeDetails() {
this(TorusNetwork.MAINNET, "0xf20336e16B5182637f09821c27BDe29b0AFcfe80");
}

public FetchNodeDetails(TorusNetwork network, String proxyAddress) {
this.proxyAddress = proxyAddress;
this.providerUrl = "https://" + NETWORK_MAP.get(network) + ".infura.io/v3/" + "b8cdb0e4cff24599a286bf8e87ff1c96";
this.setupWeb3();
this(TorusNetwork.SAPPHIRE_MAINNET);
}

public FetchNodeDetails(String providerUrl, String proxyAddress) {
this.proxyAddress = proxyAddress;
this.providerUrl = providerUrl;
this.setupWeb3();
public FetchNodeDetails(TorusNetwork network) {
this.torusNetwork = network;
}

public CompletableFuture<NodeDetails> getNodeDetails(String verifier, String verifierId) {
// For mainnet & ropsten, verifierId combination doesn't change the network details
if (this.nodeDetails.getUpdated() && (this.proxyAddress.equals(PROXY_ADDRESS_MAINNET) || this.proxyAddress.equals(PROXY_ADDRESS_TESTNET)))
if (this.nodeDetails.getUpdated() && !(multi_cluster_networks.contains(this.torusNetwork.name())))
return CompletableFuture.supplyAsync(() -> this.nodeDetails);
byte[] hashedVerifierId = Hash.sha3(verifierId.getBytes(StandardCharsets.UTF_8));
CompletableFuture<NodeDetails> cf = new CompletableFuture<>();
this.torusLookup.getNodeSet(verifier, hashedVerifierId).sendAsync().whenCompleteAsync((nodeEndPoints, err) -> {
if (err != null) {
if (this.proxyAddress.equals(FetchNodeDetails.PROXY_ADDRESS_MAINNET)) {
cf.complete(FetchNodeDetails.NODE_DETAILS_MAINNET);
} else {
cf.completeExceptionally(err);
}
return;
}
try {
String[] updatedEndpoints = new String[nodeEndPoints.component3().size()];
TorusNodePub[] updatedNodePub = new TorusNodePub[nodeEndPoints.component3().size()];

for (int i = 0; i < nodeEndPoints.component3().size(); i++) {
NodeInfo endPointElement = new NodeInfo(String.valueOf(nodeEndPoints.component2().get(i)), new BigInteger(String.valueOf(nodeEndPoints.component3().get(i)), 10).toString(16).replace("0x", ""), new BigInteger(String.valueOf(nodeEndPoints.component4().get(i)), 10).toString(16).replace("0x", ""));

String endpoint = "https://" + endPointElement.getDeclaredIp().split(":")[0] + "/jrpc";
updatedEndpoints[i] = endpoint;
updatedNodePub[i] = new TorusNodePub(endPointElement.getPubKx(), endPointElement.getPubKy());
}
this.nodeDetails.setNodeListAddress(this.proxyAddress);
this.nodeDetails.setCurrentEpoch(nodeEndPoints.component1().toString());
this.nodeDetails.setTorusNodeEndpoints(updatedEndpoints);
this.nodeDetails.setTorusNodePub(updatedNodePub);
this.nodeDetails.setTorusIndexes(nodeEndPoints.component5().toArray(new BigInteger[0]));
this.nodeDetails.setUpdated(true);
cf.complete(this.nodeDetails);
} catch (Exception err2) {
cf.completeExceptionally(err2);
}
});
CompletableFuture<NodeDetails> cf = new CompletableFuture<>();
try {
String fndServerEndpoint = "https://fnd.tor.us/node-details";
String url = fndServerEndpoint + "?network=" + this.torusNetwork + "&verifier=" + verifier + "&verifierId=" + verifierId;
CompletableFuture<String> response = APIUtils.get(url);
FNDResponse fndResponse =
new GsonBuilder().disableHtmlEscaping().create().fromJson(response.get(), FNDResponse.class);
this.setNodeDetails(fndResponse.getNodeDetails(), true);
cf.complete(this.nodeDetails);
return cf;
} catch (Exception ex) {
ex.printStackTrace();
}
NodeDetails nodeDetails = Utils.fetchLocalConfig(this.torusNetwork);
if (nodeDetails == null) cf.completeExceptionally(new Exception("Failed to fetch node details"));
else {
this.setNodeDetails(nodeDetails, false);
cf.complete(this.nodeDetails);
}
return cf;
}

private void setupWeb3() {
Web3j web3j = Web3j.build(new HttpService(this.providerUrl));
Credentials credentials = Credentials.create("0x5bbbef76458bf30511c9ee6ed56783644eb339258d02656755c68098c4809130");
this.torusLookup = new TorusLookup(this.proxyAddress, web3j, credentials, new DefaultGasProvider());
private void setNodeDetails(NodeDetails nodeDetails, boolean updated) {
this.nodeDetails.setTorusNodeEndpoints(nodeDetails.getTorusNodeEndpoints());
this.nodeDetails.setTorusNodePub(nodeDetails.getTorusNodePub());
this.nodeDetails.setCurrentEpoch(nodeDetails.getCurrentEpoch());
this.nodeDetails.setUpdated(updated);
this.nodeDetails.setTorusIndexes(nodeDetails.getTorusIndexes());
this.nodeDetails.setTorusNodeRSSEndpoints(nodeDetails.getTorusNodeRSSEndpoints());
this.nodeDetails.setTorusNodeSSSEndpoints(nodeDetails.getTorusNodeSSSEndpoints());
this.nodeDetails.setTorusNodeTSSEndpoints(nodeDetails.getTorusNodeTSSEndpoints());
}
}
Loading