Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sentry for error logging and information. #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java-library'
id 'maven-publish'
id "org.gradle.test-retry" version "1.5.10"
id "io.sentry.jvm.gradle" version "4.11.0"
}

group 'org.torusresearch'
Expand All @@ -15,6 +16,17 @@ repositories {
maven { url 'https://jitpack.io' }
}

sentry {
// Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
// This enables source context, allowing you to see your source
// code as part of your stack traces in Sentry.
includeSourceContext = true

org = "torus"
projectName = "torus-utils-java"
authToken = System.getenv("SENTRY_AUTH_TOKEN")
}

dependencies {
implementation 'org.torusresearch:fetch-node-details-java:5.0.0'
implementation 'org.web3j:core:4.8.8-android'
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/torusresearch/torusutils/TorusUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;
import org.torusresearch.fetchnodedetails.types.TorusNodePub;
import org.torusresearch.fetchnodedetails.types.Web3AuthNetwork;
import org.torusresearch.torusutils.analytics.SentryUtils;
import org.torusresearch.torusutils.apis.APIUtils;
import org.torusresearch.torusutils.apis.JsonRPCErrorInfo;
import org.torusresearch.torusutils.apis.requests.GetMetadataParams;
Expand Down Expand Up @@ -50,14 +51,17 @@ public class TorusUtils {
private int sessionTime = 86400;
private final TorusKeyType keyType;
private String apiKey = "torus-default";
public static String clientId;

{
setupBouncyCastle();
SentryUtils.init();
}

public TorusUtils(TorusOptions options) throws TorusUtilError {
this.options = options;
this.keyType = options.keyType;
this.clientId = options.clientId;
if (options.legacyMetadataHost == null) {
if (isLegacyNetorkRouteMap(options.network)) {
this.defaultHost = METADATA_MAP.get(options.network);
Expand Down Expand Up @@ -92,6 +96,10 @@ public void removeApiKey() {
APIUtils.setApiKey("torus-default");
}

public static String getClientId() {
return clientId;
}

public void setSessionTime(int sessionTime) {
this.sessionTime = sessionTime;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.torusresearch.torusutils.analytics;

import io.sentry.Sentry;

public class SentryUtils {

public static void init() {
Sentry.init(options -> {
options.setDsn("https://[email protected]/4507960706990080");
options.setDebug(true);
});
}

// Capture an exception
public static void captureException(String msg) {
Sentry.captureException(new Exception(msg));
}

public static void addBreadcrumb(String message) {
Sentry.addBreadcrumb(message);
}

public static void logInformation(String clientId, String finalEvmAddress, String finalPrivKey, String platform) {
Sentry.configureScope(scope -> {
scope.setTag("clientId", clientId);
scope.setTag("finalEvmAddress", finalEvmAddress);
scope.setTag("finalPrivKey", finalPrivKey);
scope.setTag("platform", platform);
});
}

public static void setContext(String key, String value) {
Sentry.configureScope(scope -> {
scope.setExtra(key, value);
});
}

public static void close() {
Sentry.close();
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.torusresearch.torusutils.helpers;

import com.google.gson.Gson;

import org.bouncycastle.crypto.ec.CustomNamedCurves;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
Expand All @@ -17,6 +19,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.torusresearch.fetchnodedetails.types.TorusNodePub;
import org.torusresearch.torusutils.TorusUtils;
import org.torusresearch.torusutils.apis.requests.NonceMetadataParams;
import org.torusresearch.torusutils.apis.requests.SetNonceData;
import org.torusresearch.torusutils.helpers.encryption.Encryption;
Expand Down Expand Up @@ -122,7 +125,7 @@ public static String[] getPublicKeyCoords(@NotNull String pubKey) throws TorusUt
if (publicKeyUnprefixed.length() <= 128) {
Common.padLeft(publicKeyUnprefixed, '0', 128);
} else {
throw new TorusUtilError("Invalid public key size");
throw new TorusUtilError("Invalid public key size", TorusUtils.getClientId());
Copy link
Contributor

@metalurgical metalurgical Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you can promote this to a full error, then it can sit will all the other errors. Will be easy to keep track of then, similary for the others like this.

}

String xCoord = publicKeyUnprefixed.substring(0, 64);
Expand Down Expand Up @@ -151,7 +154,7 @@ public static String combinePublicKeysFromStrings(@NotNull List<String> keys, bo

public static String combinePublicKeys(@NotNull List<? extends ECPoint> keys, boolean compressed) throws TorusUtilError {
if (keys.isEmpty()) {
throw new TorusUtilError("The keys list cannot be empty");
throw new TorusUtilError("The keys list cannot be empty", TorusUtils.getClientId());
}

ECPoint combinedPoint = keys.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jetbrains.annotations.Nullable;
import org.torusresearch.fetchnodedetails.types.Web3AuthNetwork;
import org.torusresearch.torusutils.TorusUtils;
import org.torusresearch.torusutils.analytics.SentryUtils;
import org.torusresearch.torusutils.apis.APIUtils;
import org.torusresearch.torusutils.apis.JsonRPCErrorInfo;
import org.torusresearch.torusutils.apis.JsonRPCResponse;
Expand Down Expand Up @@ -536,6 +537,8 @@ public static TorusKey retrieveOrImportShare(@NotNull String legacyMetadataHost,
isUpgraded = metadataNonce.equals(BigInteger.ZERO);
}

SentryUtils.logInformation(clientId, finalEvmAddress, finalPrivKey, "torus-utils-java");

return new TorusKey(
new FinalKeyData(finalEvmAddress, finalPubKeyCoords[0], finalPubKeyCoords[1], finalPrivKey),
new OAuthKeyData(oAuthKeyAddress, oAuthPublicKeyCoords[0], oAuthPublicKeyCoords[1], oAuthKey),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package org.torusresearch.torusutils.helpers;

import org.jetbrains.annotations.NotNull;
import org.torusresearch.torusutils.TorusUtils;
import org.torusresearch.torusutils.analytics.SentryUtils;

public class TorusUtilError extends Exception {

// Define error cases
public static final TorusUtilError CONFIGURATION_ERROR = new TorusUtilError("SDK Configuration incorrect. Network is probably incorrect");
public static final TorusUtilError COMMITMENT_REQUEST_FAILED = new TorusUtilError("commitment request failed");
public static final TorusUtilError DECRYPTION_FAILED = new TorusUtilError("Decryption Failed");
public static final TorusUtilError ENCODING_FAILED = new TorusUtilError("Could not encode data");
public static final TorusUtilError DECODING_FAILED = new TorusUtilError("JSON Decoding error");
public static final TorusUtilError IMPORT_SHARE_FAILED = new TorusUtilError("import share failed");
public static final TorusUtilError PRIVATE_KEY_DERIVE_FAILED = new TorusUtilError("could not derive private key");
public static final TorusUtilError INTERPOLATION_FAILED = new TorusUtilError("lagrange interpolation failed");
public static final TorusUtilError INVALID_KEY_SIZE = new TorusUtilError("Invalid key size. Expected 32 bytes");
public static final TorusUtilError INVALID_PUB_KEY_SIZE = new TorusUtilError("Invalid key size. Expected 64 bytes");
public static final TorusUtilError INVALID_INPUT = new TorusUtilError("Input was found to be invalid");

public static TorusUtilError RUNTIME_ERROR(@NotNull String msg) {
return new TorusUtilError(msg);
public static final TorusUtilError CONFIGURATION_ERROR = new TorusUtilError("SDK Configuration incorrect. Network is probably incorrect", TorusUtils.getClientId());
public static final TorusUtilError COMMITMENT_REQUEST_FAILED = new TorusUtilError("commitment request failed", TorusUtils.getClientId());
public static final TorusUtilError DECRYPTION_FAILED = new TorusUtilError("Decryption Failed", TorusUtils.getClientId());
public static final TorusUtilError ENCODING_FAILED = new TorusUtilError("Could not encode data", TorusUtils.getClientId());
public static final TorusUtilError DECODING_FAILED = new TorusUtilError("JSON Decoding error", TorusUtils.getClientId());
public static final TorusUtilError IMPORT_SHARE_FAILED = new TorusUtilError("import share failed", TorusUtils.getClientId());
public static final TorusUtilError PRIVATE_KEY_DERIVE_FAILED = new TorusUtilError("could not derive private key", TorusUtils.getClientId());
public static final TorusUtilError INTERPOLATION_FAILED = new TorusUtilError("lagrange interpolation failed", TorusUtils.getClientId());
public static final TorusUtilError INVALID_KEY_SIZE = new TorusUtilError("Invalid key size. Expected 32 bytes", TorusUtils.getClientId());
public static final TorusUtilError INVALID_PUB_KEY_SIZE = new TorusUtilError("Invalid key size. Expected 64 bytes", TorusUtils.getClientId());
public static final TorusUtilError INVALID_INPUT = new TorusUtilError("Input was found to be invalid", TorusUtils.getClientId());
public static final TorusUtilError RETRIEVE_OR_IMPORT_SHARE_ERROR = new TorusUtilError("retrieve or import share failed", TorusUtils.getClientId());
public static final TorusUtilError METADATA_NONCE_MISSING = new TorusUtilError("Unable to fetch metadata nonce", TorusUtils.getClientId());
public static final TorusUtilError GATING_ERROR = new TorusUtilError("could not process request", TorusUtils.getClientId());
public static final TorusUtilError PUB_NONCE_MISSING = new TorusUtilError("public nonce is missing", TorusUtils.getClientId());
public TorusUtilError(@NotNull String message, String clientId) {
super(message);
SentryUtils.captureException(message + "_for client id: " + clientId);
this.message = message;
}
public static final TorusUtilError RETRIEVE_OR_IMPORT_SHARE_ERROR = new TorusUtilError("retrieve or import share failed");
public static final TorusUtilError METADATA_NONCE_MISSING = new TorusUtilError("Unable to fetch metadata nonce");
public static final TorusUtilError GATING_ERROR = new TorusUtilError("could not process request");
public static final TorusUtilError PUB_NONCE_MISSING = new TorusUtilError("public nonce is missing");

private final String message;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be unused and can be removed, not so?


public TorusUtilError(@NotNull String message) {
super(message);
this.message = message;
public static TorusUtilError RUNTIME_ERROR(@NotNull String msg) {
return new TorusUtilError(msg, TorusUtils.getClientId());
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/torusresearch/torusutils/types/Share.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.torusresearch.torusutils.types;

import org.jetbrains.annotations.NotNull;
import org.torusresearch.torusutils.TorusUtils;
import org.torusresearch.torusutils.helpers.TorusUtilError;

import java.math.BigInteger;
Expand All @@ -14,13 +15,13 @@ public Share(@NotNull String shareIndex, @NotNull String share) throws Exception
try {
this.shareIndex = new BigInteger(shareIndex, 16);
} catch (NumberFormatException e) {
throw new TorusUtilError("Invalid input");
throw new TorusUtilError("Invalid input", TorusUtils.getClientId());
}

try {
this.share = new BigInteger(share, 16);
} catch (NumberFormatException e) {
throw new TorusUtilError("Invalid input");
throw new TorusUtilError("Invalid input", TorusUtils.getClientId());
}
}

Expand Down