Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Add parameter guards to all public methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
srottem committed Oct 10, 2017
1 parent b6f21d0 commit 1830c1d
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.hyperledger.indy.sdk;

public class ParamGuard {

public static void notNull(Object param, String paramName) {
if(param == null)
throw new IllegalArgumentException("A value must be provided for the '" + paramName + "' parameter.");
}

public static void notNullOrWhiteSpace(String param, String paramName) {
if(StringUtils.isNullOrWhiteSpace(param))
throw new IllegalArgumentException("A non-empty string must be provided for the '" + paramName + "' parameter.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.hyperledger.indy.sdk;

public class StringUtils {

public static boolean isNullOrWhiteSpace(String s) {
if (s == null)
return true;

for (int i = 0; i < s.length(); i++) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}

return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.hyperledger.indy.sdk.IndyException;
import org.hyperledger.indy.sdk.IndyJava;
import org.hyperledger.indy.sdk.LibIndy;
import org.hyperledger.indy.sdk.ParamGuard;
import org.hyperledger.indy.sdk.pool.Pool;
import org.hyperledger.indy.sdk.wallet.Wallet;

Expand Down Expand Up @@ -289,6 +290,12 @@ public static CompletableFuture<Connection> agentConnect(
String senderDid,
String receiverDid,
AgentObservers.MessageObserver messageObserver) throws IndyException {

ParamGuard.notNull(pool, "pool");
ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(receiverDid, "senderDid");
ParamGuard.notNullOrWhiteSpace(receiverDid, "senderDid");
ParamGuard.notNull(messageObserver, "messageObserver");

CompletableFuture<Connection> future = new CompletableFuture<>();
int commandHandle = addFuture(future);
Expand Down Expand Up @@ -323,6 +330,9 @@ public static CompletableFuture<Listener> agentListen(
String endpoint,
AgentObservers.ConnectionObserver connectionObserver) throws IndyException {

ParamGuard.notNullOrWhiteSpace(endpoint, "endpoint");
ParamGuard.notNull(connectionObserver, "connectionObserver");

CompletableFuture<Listener> future = new CompletableFuture<>();
int commandHandle = addFuture(future);
addConnectionObserver(commandHandle, connectionObserver);
Expand Down Expand Up @@ -354,6 +364,11 @@ public static CompletableFuture<Void> agentAddIdentity(
Pool pool,
Wallet wallet,
String did) throws IndyException {

ParamGuard.notNull(listener, "listener");
ParamGuard.notNull(pool, "pool");
ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(did, "did");

CompletableFuture<Void> future = new CompletableFuture<Void>();
int commandHandle = addFuture(future);
Expand Down Expand Up @@ -389,6 +404,10 @@ public static CompletableFuture<Void> agentRemoveIdentity(
Wallet wallet,
String did) throws IndyException {

ParamGuard.notNull(listener, "listener");
ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(did, "did");

CompletableFuture<Void> future = new CompletableFuture<Void>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -418,6 +437,9 @@ public static CompletableFuture<Void> agentRemoveIdentity(
public static CompletableFuture<Void> agentSend(
Agent.Connection connection,
String message) throws IndyException {

ParamGuard.notNull(connection, "connection");
ParamGuard.notNull(message, "message");

CompletableFuture<Void> future = new CompletableFuture<Void>();
int commandHandle = addFuture(future);
Expand Down Expand Up @@ -445,6 +467,8 @@ public static CompletableFuture<Void> agentSend(
public static CompletableFuture<Void> agentCloseConnection(
Agent.Connection connection) throws IndyException {

ParamGuard.notNull(connection, "connection");

CompletableFuture<Void> future = new CompletableFuture<Void>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -472,6 +496,8 @@ public static CompletableFuture<Void> agentCloseConnection(
public static CompletableFuture<Void> agentCloseListener(
Agent.Listener listener) throws IndyException {

ParamGuard.notNull(listener, "listener");

CompletableFuture<Void> future = new CompletableFuture<Void>();
int commandHandle = addFuture(future);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.hyperledger.indy.sdk.IndyException;
import org.hyperledger.indy.sdk.IndyJava;
import org.hyperledger.indy.sdk.LibIndy;
import org.hyperledger.indy.sdk.ParamGuard;
import org.hyperledger.indy.sdk.anoncreds.AnoncredsResults.IssuerCreateClaimResult;
import org.hyperledger.indy.sdk.wallet.Wallet;

Expand Down Expand Up @@ -259,6 +260,10 @@ public static CompletableFuture<String> issuerCreateAndStoreClaimDef(
String schemaJson,
String signatureType,
boolean createNonRevoc) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(issuerDid, "issuerDid");
ParamGuard.notNullOrWhiteSpace(schemaJson, "schemaJson");

CompletableFuture<String> future = new CompletableFuture<String>();
int commandHandle = addFuture(future);
Expand Down Expand Up @@ -295,6 +300,9 @@ public static CompletableFuture<String> issuerCreateAndStoreRevocReg(
int schemaSeqNo,
int maxClaimNum) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(issuerDid, "issuerDid");

CompletableFuture<String> future = new CompletableFuture<String>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -329,6 +337,10 @@ public static CompletableFuture<IssuerCreateClaimResult> issuerCreateClaim(
String claimJson,
int userRevocIndex) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(claimReqJson, "claimReqJson");
ParamGuard.notNullOrWhiteSpace(claimJson, "claimJson");

CompletableFuture<IssuerCreateClaimResult> future = new CompletableFuture<IssuerCreateClaimResult>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -363,6 +375,9 @@ public static CompletableFuture<String> issuerRevokeClaim(
int schemaSeqNo,
int userRevocIndex) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(issuerDid, "issuerDid");

CompletableFuture<String> future = new CompletableFuture<String>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -393,6 +408,9 @@ public static CompletableFuture<Void> proverStoreClaimOffer(
Wallet wallet,
String claimOfferJson) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(claimOfferJson, "claimOfferJson");

CompletableFuture<Void> future = new CompletableFuture<Void>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -421,6 +439,9 @@ public static CompletableFuture<String> proverGetClaimOffers(
Wallet wallet,
String filterJson) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(filterJson, "filterJson");

CompletableFuture<String> future = new CompletableFuture<String>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -449,6 +470,9 @@ public static CompletableFuture<Void> proverCreateMasterSecret(
Wallet wallet,
String masterSecretName) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(masterSecretName, "masterSecretName");

CompletableFuture<Void> future = new CompletableFuture<Void>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -483,6 +507,12 @@ public static CompletableFuture<String> proverCreateAndStoreClaimReq(
String claimDefJson,
String masterSecretName) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(proverDid, "proverDid");
ParamGuard.notNullOrWhiteSpace(claimOfferJson, "claimOfferJson");
ParamGuard.notNullOrWhiteSpace(claimDefJson, "claimDefJson");
ParamGuard.notNullOrWhiteSpace(masterSecretName, "masterSecretName");

CompletableFuture<String> future = new CompletableFuture<String>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -514,6 +544,9 @@ public static CompletableFuture<Void> proverStoreClaim(
Wallet wallet,
String claim) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(claim, "claim");

CompletableFuture<Void> future = new CompletableFuture<Void>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -542,6 +575,9 @@ public static CompletableFuture<String> proverGetClaims(
Wallet wallet,
String filter) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(filter, "filter");

CompletableFuture<String> future = new CompletableFuture<String>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -570,6 +606,9 @@ public static CompletableFuture<String> proverGetClaimsForProofReq(
Wallet wallet,
String proofRequest) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(proofRequest, "proofRequest");

CompletableFuture<String> future = new CompletableFuture<String>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -608,6 +647,14 @@ public static CompletableFuture<String> proverCreateProof(
String claimDefs,
String revocRegs) throws IndyException {

ParamGuard.notNull(wallet, "wallet");
ParamGuard.notNullOrWhiteSpace(proofRequest, "proofRequest");
ParamGuard.notNullOrWhiteSpace(requestedClaims, "requestedClaims");
ParamGuard.notNullOrWhiteSpace(schemas, "schemas");
ParamGuard.notNullOrWhiteSpace(masterSecret, "masterSecret");
ParamGuard.notNullOrWhiteSpace(claimDefs, "claimDefs");
ParamGuard.notNullOrWhiteSpace(revocRegs, "revocRegs");

CompletableFuture<String> future = new CompletableFuture<String>();
int commandHandle = addFuture(future);

Expand Down Expand Up @@ -647,6 +694,12 @@ public static CompletableFuture<Boolean> verifierVerifyProof(
String claimDefs,
String revocRegs) throws IndyException {

ParamGuard.notNullOrWhiteSpace(proofRequest, "proofRequest");
ParamGuard.notNullOrWhiteSpace(proof, "proof");
ParamGuard.notNullOrWhiteSpace(schemas, "schemas");
ParamGuard.notNullOrWhiteSpace(claimDefs, "claimDefs");
ParamGuard.notNullOrWhiteSpace(revocRegs, "revocRegs");

CompletableFuture<Boolean> future = new CompletableFuture<Boolean>();
int commandHandle = addFuture(future);

Expand Down
Loading

0 comments on commit 1830c1d

Please sign in to comment.