Skip to content

Commit

Permalink
Merge pull request #18 from torusresearch/feat/add-get-user
Browse files Browse the repository at this point in the history
Feat/add get user
  • Loading branch information
chaitanyapotti authored Dec 7, 2022
2 parents 70da12d + 5eb4e77 commit 2e6c496
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 84 deletions.
146 changes: 121 additions & 25 deletions src/main/java/org/torusresearch/torusutils/TorusUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,24 +352,22 @@ CompletableFuture<TorusPublicKey> _getPublicAddress(String[] endpoints, TorusNod
lookupCf.completeExceptionally(new Exception("Verifier not supported. Check if you: \\n\n" + " 1. Are on the right network (Torus testnet/mainnet) \\n\n" + " 2. Have setup a verifier on dashboard.web3auth.io?"));
return lookupCf;
} else if (keyLookupResult.getErrResult() != null && keyLookupResult.getErrResult().contains("Verifier + VerifierID has not yet been assigned")) {
return Utils.keyAssign(endpoints, torusNodePubs, null, null, verifierArgs.getVerifier(), verifierArgs.getVerifierId(), this.options.getSignerHost(), this.options.getNetwork())
.thenComposeAsync(k -> Utils.waitKeyLookup(endpoints, verifierArgs.getVerifier(), verifierArgs.getVerifierId(), 1000))
.thenComposeAsync(res -> {
CompletableFuture<VerifierLookupItem> lookupCf = new CompletableFuture<>();
if (res == null || res.getKeyResult() == null) {
lookupCf.completeExceptionally(new Exception("could not get lookup, no results"));
return lookupCf;
}
VerifierLookupRequestResult verifierLookupRequestResult = gson.fromJson(res.getKeyResult(), VerifierLookupRequestResult.class);
if (verifierLookupRequestResult == null || verifierLookupRequestResult.getKeys() == null || verifierLookupRequestResult.getKeys().length == 0) {
lookupCf.completeExceptionally(new Exception("could not get lookup, no keys" + res.getKeyResult() + res.getErrResult()));
return lookupCf;
}
VerifierLookupItem verifierLookupItem = verifierLookupRequestResult.getKeys()[0];
lookupCf.complete(verifierLookupItem);
isNewKey.set(true);
return lookupCf;
});
return Utils.keyAssign(endpoints, torusNodePubs, null, null, verifierArgs.getVerifier(), verifierArgs.getVerifierId(), this.options.getSignerHost(), this.options.getNetwork()).thenComposeAsync(k -> Utils.waitKeyLookup(endpoints, verifierArgs.getVerifier(), verifierArgs.getVerifierId(), 1000)).thenComposeAsync(res -> {
CompletableFuture<VerifierLookupItem> lookupCf = new CompletableFuture<>();
if (res == null || res.getKeyResult() == null) {
lookupCf.completeExceptionally(new Exception("could not get lookup, no results"));
return lookupCf;
}
VerifierLookupRequestResult verifierLookupRequestResult = gson.fromJson(res.getKeyResult(), VerifierLookupRequestResult.class);
if (verifierLookupRequestResult == null || verifierLookupRequestResult.getKeys() == null || verifierLookupRequestResult.getKeys().length == 0) {
lookupCf.completeExceptionally(new Exception("could not get lookup, no keys" + res.getKeyResult() + res.getErrResult()));
return lookupCf;
}
VerifierLookupItem verifierLookupItem = verifierLookupRequestResult.getKeys()[0];
lookupCf.complete(verifierLookupItem);
isNewKey.set(true);
return lookupCf;
});
}
CompletableFuture<VerifierLookupItem> lookupCf = new CompletableFuture<>();
if (keyLookupResult.getKeyResult() != null) {
Expand All @@ -387,7 +385,7 @@ CompletableFuture<TorusPublicKey> _getPublicAddress(String[] endpoints, TorusNod
}).thenComposeAsync(verifierLookupItem -> {
CompletableFuture<TorusPublicKey> keyCf = new CompletableFuture<>();
try {
GetOrSetNonceResult nonceResult;
GetOrSetNonceResult nonceResult = null;
BigInteger nonce;
ECPoint modifiedPubKey;
TypeOfUser typeOfUser;
Expand All @@ -406,9 +404,7 @@ CompletableFuture<TorusPublicKey> _getPublicAddress(String[] endpoints, TorusNod

modifiedPubKey = curve.getCurve().createPoint(new BigInteger(verifierLookupItem.getPub_key_X(), 16), new BigInteger(verifierLookupItem.getPub_key_Y(), 16));
if (nonceResult.getTypeOfUser() == TypeOfUser.v1) {
modifiedPubKey = modifiedPubKey
.add(curve.getG().multiply(nonce))
.normalize();
modifiedPubKey = modifiedPubKey.add(curve.getG().multiply(nonce)).normalize();
} else if (nonceResult.getTypeOfUser() == TypeOfUser.v2) {
if (!nonceResult.isUpgraded()) {
assert nonceResult.getPubNonce() != null;
Expand All @@ -424,9 +420,7 @@ CompletableFuture<TorusPublicKey> _getPublicAddress(String[] endpoints, TorusNod
typeOfUser = TypeOfUser.v1;
nonce = this.getMetadata(new MetadataPubKey(verifierLookupItem.getPub_key_X(), verifierLookupItem.getPub_key_Y())).get();
modifiedPubKey = curve.getCurve().createPoint(new BigInteger(verifierLookupItem.getPub_key_X(), 16), new BigInteger(verifierLookupItem.getPub_key_Y(), 16));
modifiedPubKey = modifiedPubKey
.add(curve.getG().multiply(nonce))
.normalize();
modifiedPubKey = modifiedPubKey.add(curve.getG().multiply(nonce)).normalize();
}
String finalPubKey = Utils.padLeft(modifiedPubKey.getAffineXCoord().toString(), '0', 64) + Utils.padLeft(modifiedPubKey.getAffineYCoord().toString(), '0', 64);
String address = Keys.toChecksumAddress(Hash.sha3(finalPubKey).substring(64 - 38));
Expand All @@ -437,6 +431,7 @@ CompletableFuture<TorusPublicKey> _getPublicAddress(String[] endpoints, TorusNod
key.setTypeOfUser(typeOfUser);
key.setMetadataNonce(nonce);
key.setPubNonce(pubNonce);
key.setUpgraded(nonceResult != null && nonceResult.isUpgraded());
keyCf.complete(key);
}
return keyCf;
Expand Down Expand Up @@ -493,4 +488,105 @@ public CompletableFuture<GetOrSetNonceResult> getNonce(String X, String Y) {
public CompletableFuture<GetOrSetNonceResult> getNonce(BigInteger privKey) {
return this.getOrSetNonce(privKey, true);
}

public CompletableFuture<TorusPublicKey> getUserTypeAndAddress(String[] endpoints, TorusNodePub[] torusNodePubs, VerifierArgs verifierArgs) {
return this.getUserTypeAndAddress(endpoints, torusNodePubs, verifierArgs, false);
}

public CompletableFuture<TorusPublicKey> getUserTypeAndAddress(String[] endpoints, TorusNodePub[] torusNodePubs, VerifierArgs verifierArgs, boolean doesKeyAssign) {
AtomicBoolean isNewKey = new AtomicBoolean(false);
Gson gson = new Gson();
return Utils.keyLookup(endpoints, verifierArgs.getVerifier(), verifierArgs.getVerifierId()).thenComposeAsync(keyLookupResult -> {
if (keyLookupResult.getErrResult() != null && keyLookupResult.getErrResult().contains("Verifier not supported")) {
CompletableFuture<VerifierLookupItem> lookupCf = new CompletableFuture<>();
lookupCf.completeExceptionally(new Exception("Verifier not supported. Check if you: \\n\n" + " 1. Are on the right network (Torus testnet/mainnet) \\n\n" + " 2. Have setup a verifier on dashboard.web3auth.io?"));
return lookupCf;
} else if (keyLookupResult.getErrResult() != null && keyLookupResult.getErrResult().contains("Verifier + VerifierID has not yet been assigned")) {
if (!doesKeyAssign) {
CompletableFuture<VerifierLookupItem> lookupCf = new CompletableFuture<>();
lookupCf.completeExceptionally(new Exception("Verifier + VerifierID has not yet been assigned"));
return lookupCf;
}
return Utils.keyAssign(endpoints, torusNodePubs, null, null, verifierArgs.getVerifier(), verifierArgs.getVerifierId(), this.options.getSignerHost(), this.options.getNetwork()).thenComposeAsync(k -> Utils.waitKeyLookup(endpoints, verifierArgs.getVerifier(), verifierArgs.getVerifierId(), 1000)).thenComposeAsync(res -> {
CompletableFuture<VerifierLookupItem> lookupCf = new CompletableFuture<>();
if (res == null || res.getKeyResult() == null) {
lookupCf.completeExceptionally(new Exception("could not get lookup, no results"));
return lookupCf;
}
VerifierLookupRequestResult verifierLookupRequestResult = gson.fromJson(res.getKeyResult(), VerifierLookupRequestResult.class);
if (verifierLookupRequestResult == null || verifierLookupRequestResult.getKeys() == null || verifierLookupRequestResult.getKeys().length == 0) {
lookupCf.completeExceptionally(new Exception("could not get lookup, no keys" + res.getKeyResult() + res.getErrResult()));
return lookupCf;
}
VerifierLookupItem verifierLookupItem = verifierLookupRequestResult.getKeys()[0];
lookupCf.complete(verifierLookupItem);
isNewKey.set(true);
return lookupCf;
});
}
CompletableFuture<VerifierLookupItem> lookupCf = new CompletableFuture<>();
if (keyLookupResult.getKeyResult() != null) {
VerifierLookupRequestResult verifierLookupRequestResult = gson.fromJson(keyLookupResult.getKeyResult(), VerifierLookupRequestResult.class);
if (verifierLookupRequestResult == null || verifierLookupRequestResult.getKeys() == null || verifierLookupRequestResult.getKeys().length == 0) {
lookupCf.completeExceptionally(new Exception("could not get lookup, no keys" + keyLookupResult.getKeyResult() + keyLookupResult.getErrResult()));
return lookupCf;
}
VerifierLookupItem verifierLookupItem = verifierLookupRequestResult.getKeys()[0];
lookupCf.complete(verifierLookupItem);
return lookupCf;
}
lookupCf.completeExceptionally(new Exception("could not get lookup, no valid key result or error result"));
return lookupCf;
}).thenComposeAsync(verifierLookupItem -> {
CompletableFuture<TorusPublicKey> keyCf = new CompletableFuture<>();
try {
GetOrSetNonceResult nonceResult;
BigInteger nonce;
ECPoint modifiedPubKey;
TypeOfUser typeOfUser;
GetOrSetNonceResult.PubNonce pubNonce = null;
ECNamedCurveParameterSpec curve = ECNamedCurveTable.getParameterSpec("secp256k1");

try {
nonceResult = this.getOrSetNonce(verifierLookupItem.getPub_key_X(), verifierLookupItem.getPub_key_Y(), !isNewKey.get()).get();
nonce = new BigInteger(Utils.isEmpty(nonceResult.getNonce()) ? "0" : nonceResult.getNonce(), 16);
typeOfUser = nonceResult.getTypeOfUser();
} catch (Exception e) {
// Sometimes we take special action if `get or set nonce` api is not available
keyCf.completeExceptionally(new GetOrSetNonceError(e));
return keyCf;
}

modifiedPubKey = curve.getCurve().createPoint(new BigInteger(verifierLookupItem.getPub_key_X(), 16), new BigInteger(verifierLookupItem.getPub_key_Y(), 16));
if (nonceResult.getTypeOfUser() == TypeOfUser.v1) {
modifiedPubKey = modifiedPubKey.add(curve.getG().multiply(nonce)).normalize();
} else if (nonceResult.getTypeOfUser() == TypeOfUser.v2) {
// pubNonce is never deleted, so we can use it to always get the tkey
assert nonceResult.getPubNonce() != null;
ECPoint oneKeyMetadataPoint = curve.getCurve().createPoint(new BigInteger(nonceResult.getPubNonce().getX(), 16), new BigInteger(nonceResult.getPubNonce().getY(), 16));
modifiedPubKey = modifiedPubKey.add(oneKeyMetadataPoint).normalize();
pubNonce = nonceResult.getPubNonce();
} else {
keyCf.completeExceptionally(new Exception("getOrSetNonce should always return typeOfUser."));
return keyCf;
}

String finalPubKey = Utils.padLeft(modifiedPubKey.getAffineXCoord().toString(), '0', 64) + Utils.padLeft(modifiedPubKey.getAffineYCoord().toString(), '0', 64);
String address = Keys.toChecksumAddress(Hash.sha3(finalPubKey).substring(64 - 38));

TorusPublicKey key = new TorusPublicKey(finalPubKey.substring(0, finalPubKey.length() / 2), finalPubKey.substring(finalPubKey.length() / 2), address);
key.setTypeOfUser(typeOfUser);
key.setMetadataNonce(nonce);
key.setPubNonce(pubNonce);
key.setUpgraded(nonceResult.isUpgraded());
keyCf.complete(key);

return keyCf;
} catch (Exception ex) {
keyCf.completeExceptionally(ex);
return keyCf;
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class TorusPublicKey extends TorusNodePub {
private BigInteger metadataNonce;
@Nullable
private GetOrSetNonceResult.PubNonce pubNonce;
private boolean upgraded;

public TorusPublicKey(String _X, String _Y, String _address) {
super(_X, _Y);
Expand Down Expand Up @@ -49,4 +50,12 @@ public GetOrSetNonceResult.PubNonce getPubNonce() {
public void setPubNonce(GetOrSetNonceResult.PubNonce pubNonce) {
this.pubNonce = pubNonce;
}

public boolean getUpgraded() {
return upgraded;
}

public void setUpgraded(boolean upgraded) {
this.upgraded = upgraded;
}
}
15 changes: 4 additions & 11 deletions src/test/java/org/torusresearch/torusutilstest/AquaTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
package org.torusresearch.torusutilstest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.auth0.jwt.algorithms.Algorithm;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.torusresearch.fetchnodedetails.FetchNodeDetails;
import org.torusresearch.fetchnodedetails.types.NodeDetails;
import org.torusresearch.fetchnodedetails.types.TorusNetwork;
import org.torusresearch.torusutils.TorusUtils;
import org.torusresearch.torusutils.types.RetrieveSharesResponse;
import org.torusresearch.torusutils.types.TorusCtorOptions;
import org.torusresearch.torusutils.types.TorusException;
import org.torusresearch.torusutils.types.TorusPublicKey;
import org.torusresearch.torusutils.types.VerifierArgs;
import org.torusresearch.torusutils.types.*;
import org.torusresearch.torusutilstest.utils.JwtUtils;
import org.torusresearch.torusutilstest.utils.PemUtils;
import org.torusresearch.torusutilstest.utils.VerifyParams;
Expand All @@ -34,6 +25,8 @@
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import static org.junit.jupiter.api.Assertions.*;

public class AquaTest {

static FetchNodeDetails fetchNodeDetails;
Expand Down Expand Up @@ -79,7 +72,7 @@ public void shouldKeyAssign() throws ExecutionException, InterruptedException {
nodeDetails.getTorusNodePub(), new VerifierArgs("tkey-google-aqua", email)).get();
System.out.println(email + " -> " + publicAddress.getAddress());
assertNotNull(publicAddress.getAddress());
assertNotEquals(publicAddress.getAddress(),"");
assertNotEquals(publicAddress.getAddress(), "");
}

@DisplayName("Login test")
Expand Down
Loading

0 comments on commit 2e6c496

Please sign in to comment.