Skip to content

Commit

Permalink
- Fix test
Browse files Browse the repository at this point in the history
- Track origin correctly
- Improve error logging
  • Loading branch information
chaitanyapotti committed Jan 14, 2021
1 parent 84a631c commit 84c99a7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
28 changes: 24 additions & 4 deletions src/main/java/org/torusresearch/torusutils/TorusUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import java8.util.concurrent.CompletableFuture;
import okhttp3.internal.http2.Header;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
Expand Down Expand Up @@ -35,6 +36,7 @@ public class TorusUtils {
private final BigInteger secp256k1N = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
private final String metadataHost;
private final String allowHost;
private final String origin;
ConcurrentHashMap<String, BigInteger> metadataCache = new ConcurrentHashMap<>();
ConcurrentHashMap<String, ReentrantReadWriteLock> locks = new ConcurrentHashMap<>();

Expand All @@ -46,9 +48,18 @@ public TorusUtils() {
this("https://metadata.tor.us", "https://signer.tor.us/api/allow");
}

public TorusUtils(String origin) {
this("https://metadata.tor.us", "https://signer.tor.us/api/allow", origin);
}

public TorusUtils(String metadataHost, String allowHost) {
this(metadataHost, allowHost, "Custom");
}

public TorusUtils(String metadataHost, String allowHost, String origin) {
this.metadataHost = metadataHost;
this.allowHost = allowHost;
this.origin = origin;
}

public static void setAPIKey(String apiKey) {
Expand Down Expand Up @@ -128,9 +139,10 @@ private void setupBouncyCastle() {
Security.insertProviderAt(new BouncyCastleProvider(), 1);
}

public CompletableFuture<RetrieveSharesResponse> retrieveShares(String[] endpoints, BigInteger[] indexes, String verifier, HashMap<String, Object> verifierParams, String idToken) throws TorusException {
public CompletableFuture<RetrieveSharesResponse> retrieveShares(String[] endpoints, BigInteger[] indexes, String verifier,
HashMap<String, Object> verifierParams, String idToken, HashMap<String, Object> extraParams) throws TorusException {
try {
APIUtils.get(this.allowHost, true).join();
APIUtils.get(this.allowHost, new Header[]{new Header("Origin", this.origin)}, true).join();
List<CompletableFuture<String>> promiseArr = new ArrayList<>();
// generate temporary private and public key that is used to secure receive shares
ECKeyPair tmpKey = Keys.createEcKeyPair();
Expand All @@ -156,7 +168,7 @@ public CompletableFuture<RetrieveSharesResponse> retrieveShares(String[] endpoin
completableFuture.complete(completedRequests);
return completableFuture;
} else {
throw new PredicateFailedException("insufficient responses");
throw new PredicateFailedException("insufficient responses for commitments");
}
})
.getCompletableFuture()
Expand All @@ -180,6 +192,9 @@ public CompletableFuture<RetrieveSharesResponse> retrieveShares(String[] endpoin
verifierParams.put("idtoken", idToken);
verifierParams.put("nodesignatures", nodeSignatures);
verifierParams.put("verifieridentifier", verifier);
if (extraParams != null) {
verifierParams.putAll(extraParams);
}
List<HashMap<String, Object>> shareRequestItems = new ArrayList<HashMap<String, Object>>() {{
add(verifierParams);
}};
Expand Down Expand Up @@ -258,7 +273,7 @@ public CompletableFuture<RetrieveSharesResponse> retrieveShares(String[] endpoin
new BigInteger(derivedPubKeyY, 16).compareTo(new BigInteger(thresholdPubKey.getY(), 16)) == 0
) {
privateKey = derivedPrivateKey;
// ethAddress = "0x" + Hash.sha3(derivedECKeyPair.getPublicKey().toString(16)).substring(64 - 38);
// ethAddress = "0x" + Hash.sha3(derivedECKeyPair.getPublicKey().toString(16)).substring(64 - 38);
break;
}
}
Expand Down Expand Up @@ -289,6 +304,11 @@ public CompletableFuture<RetrieveSharesResponse> retrieveShares(String[] endpoin
}
}

public CompletableFuture<RetrieveSharesResponse> retrieveShares(String[] endpoints, BigInteger[] indexes, String verifier,
HashMap<String, Object> verifierParams, String idToken) throws TorusException {
return this.retrieveShares(endpoints, indexes, verifier, verifierParams, idToken, null);
}

public CompletableFuture<BigInteger> getMetadata(MetadataPubKey data) {
Gson gson = new Gson();
String metadata = gson.toJson(data, MetadataPubKey.class);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/torusresearch/torusutils/helpers/Some.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package org.torusresearch.torusutils.helpers;

import com.google.gson.Gson;
import java8.util.concurrent.CompletableFuture;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class Some<T> {
private final AtomicInteger finishedCount = new AtomicInteger(0);
private final String[] resultArr;
private final String[] errorArr;
private final CompletableFuture<T> completableFuture;
private final AtomicBoolean resolved = new AtomicBoolean(false);
private final AtomicReference<String> predicateError = new AtomicReference<>("");
public Some(List<CompletableFuture<String>> promises, Predicate<T> predicate) {
resultArr = new String[promises.size()];
errorArr = new String[promises.size()];
completableFuture = new CompletableFuture<>();
for (int i = 0; i < promises.size(); i++) {
int index = i;
Expand All @@ -26,14 +31,18 @@ public Some(List<CompletableFuture<String>> promises, Predicate<T> predicate) {
resolved.set(true);
completableFuture.complete(intermediateResult);
} catch (Exception e) {
predicateError.set(e.getMessage());
// swallow exceptions due to threshold assumptions
}
return null;
}).exceptionally(e -> {
errorArr[index] = e.getMessage();
// swallow exceptions due to threshold assumptions
int count = finishedCount.incrementAndGet();
if (count == promises.size()) {
completableFuture.completeExceptionally(new Exception("Unable to resolve enough promises"));
Gson gson = new Gson();
completableFuture.completeExceptionally(new Exception("Unable to resolve enough promises errors: " + gson.toJson(resultArr) + ", responses: " +
gson.toJson(errorArr) + ", predicate: " + predicateError.get()));
}
return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TorusUtilsTest {
static Algorithm algorithmRs;

static String TORUS_TEST_VERIFIER = "torus-test-health";
static String TORUS_TEST_AGGREGATE_VERIFIER = "torus-test-aggregate-health";
static String TORUS_TEST_AGGREGATE_VERIFIER = "torus-test-health-aggregate";

static String TORUS_TEST_EMAIL = "[email protected]";

Expand Down Expand Up @@ -107,6 +107,6 @@ public void shouldAggregateLogin() throws ExecutionException, InterruptedExcepti
}},
hashedIdToken).get();
System.out.println(retrieveSharesResponse.getEthAddress());
assertNotNull(retrieveSharesResponse.getEthAddress());
assertEquals("0x5a165d2Ed4976BD104caDE1b2948a93B72FA91D2", retrieveSharesResponse.getEthAddress());
}
}

0 comments on commit 84c99a7

Please sign in to comment.