Skip to main content

Create Identity and Credentials

The example-1 script authenticates an Integration Service client to manage Identities using the Admin identity created in example-0 and then performs the following tasks:

  1. Creates a new Identity (userIdentity).
  2. Creates a credential for that identity (userCredential).
  3. Verifies the generated credential (await identity.checkCredential(userCredential)).

You can run the example with the following command:

mvn exec:_java -Dexec.mainClass=net.gradbase.how_tos.CreateIdentityAndCredential

Example source code: Example-1

Authentication

You can use the following script to authenticate the client using the admin identity and get the BasicIdentityCredential that the admin owns: this is a verifiable credential which is required for an issuer to sign verifiable credentials for other Identities.

// Recover the admin identity
final String didId = AddAsRootIdentity.authenticateRootIdentity(client);
IdentityInternal admin = client.find(didId);

if (admin == null) {
throw new Exception("admin identity is null");
}

// Get admin identity's VC
ArrayList<VerifiableCredential> vcs = (ArrayList<VerifiableCredential>) admin.getVerifiableCredentials();

if (vcs.size() > 0) {
VerifiableCredential firstCredential = vcs.get(0);
System.out.println(firstCredential);
} else {
throw new Exception("admin identity has no credentials");
}

Create an Identity

The method createCredential requires a valid verifiable credential of type BasicIdentity, the destination DID the type and the claim, which can be created using the following script.

// Create identity for user
JSONObject jsonClaim = new JSONObject().put("type", "Person").put("name", "randomName");

Claim claim = new Claim(jsonClaim);

final String randomUsername = Utils.getRandomUsernameOfLength(5);
System.out.println("username: " + randomUsername);
JSONObject newUserIdentity = client.create(randomUsername, claim);
System.out.println("created new user " + newUserIdentity);

VerifiableCredential assignedCredential = client.createCredential(vcs.get(0),
newUserIdentity.getJSONObject("doc").getString("id"), CredentialType.BASIC_IDENTITY,
new Claim(new JSONObject().put("type", "Person").put("position", "Professor")));

System.out.println("Created credential for new user " + assignedCredential.toString());

Verify a Credential

You can verify a credential using the following script:

boolean verified = client.checkCredential(assignedCredential);
System.out.println("Verification result: " + verified);