Skip to content

Commit

Permalink
Merge pull request #31 from torusresearch/feat/check-is-new-key
Browse files Browse the repository at this point in the history
fix: Additional check for isNewKey
  • Loading branch information
metalurgical authored Sep 13, 2024
2 parents 6bf5423 + b261754 commit b5ead50
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.torusresearch.torusutils.helpers;

import org.jetbrains.annotations.NotNull;

public class IsNewKeyResponse {

public boolean isNewKey;
public String publicKeyX;

public IsNewKeyResponse(@NotNull boolean isNewKey, @NotNull String publicKeyX) {
this.isNewKey = isNewKey;
this.publicKeyX = publicKeyX;
}
}
16 changes: 11 additions & 5 deletions src/main/java/org/torusresearch/torusutils/helpers/NodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,12 @@ public static TorusKey retrieveOrImportShare(@NotNull String legacyMetadataHost,
ArrayList<String> sessionTokens = new ArrayList<>();
ArrayList<Integer> nodeIndexes = new ArrayList<>();
ArrayList<SessionToken> sessionTokenDatas = new ArrayList<>();
ArrayList<String> isNewKeys = new ArrayList<>();
ArrayList<IsNewKeyResponse> isNewKeys = new ArrayList<>();

for (ShareRequestResult item : shareResponses) {
isNewKeys.add(item.is_new_key.toString());
if (item.keys.length > 0 && item.keys[0] != null) {
isNewKeys.add(new IsNewKeyResponse(item.is_new_key, item.keys[0].public_key.getX()));
}

if (item.session_token_sigs != null && item.session_token_sigs.length > 0) {
if (item.session_token_sig_metadata != null && item.session_token_sig_metadata.length > 0) {
Expand Down Expand Up @@ -443,7 +445,12 @@ public static TorusKey retrieveOrImportShare(@NotNull String legacyMetadataHost,
throw TorusUtilError.PRIVATE_KEY_DERIVE_FAILED;
}

String thesholdIsNewKey = thresholdSame(isNewKeys.toArray(new String[0]), threshold);
boolean isNewKey = false;
for (IsNewKeyResponse item : isNewKeys) {
if (item.isNewKey && item.publicKeyX.equalsIgnoreCase(thresholdPublicKey.getX())) {
isNewKey = true;
}
}

String oAuthKey = Common.padLeft(privateKey.toString(16), '0', 64);
String oAuthPublicKey = KeyUtils.privateToPublic(privateKey);
Expand All @@ -458,8 +465,7 @@ public static TorusKey retrieveOrImportShare(@NotNull String legacyMetadataHost,
finalPublicKey = oAuthPublicKey;
} else if (TorusUtils.isLegacyNetorkRouteMap(network)) {
if (enableOneKey) {
Boolean isNewKey = (!(thesholdIsNewKey != null && thesholdIsNewKey.equalsIgnoreCase("true")));
GetOrSetNonceResult nonce = MetadataUtils.getOrSetNonce(legacyMetadataHost, thresholdPublicKey.getX(), thresholdPublicKey.getY(), serverOffsetResponse, oAuthKey, isNewKey, null);
GetOrSetNonceResult nonce = MetadataUtils.getOrSetNonce(legacyMetadataHost, thresholdPublicKey.getX(), thresholdPublicKey.getY(), serverOffsetResponse, oAuthKey, !isNewKey, null);
metadataNonce = (nonce.nonce != null) ? new BigInteger(Common.padLeft(nonce.nonce, '0', 64), 16) : BigInteger.ZERO;
typeOfUser = (nonce.typeOfUser != null) ? nonce.typeOfUser : TypeOfUser.v1;

Expand Down

0 comments on commit b5ead50

Please sign in to comment.