Skip to content

Working with identities

luiziov42 edited this page Aug 13, 2020 · 3 revisions

Working with identities

Identities are used in the iov42 platform to represent entities that interact with the system (e.g. people or smart devices).

In order to interact with the platform, the identity must provide proof that it possess the key pair that matches the one used during its creation.

Keypair generation:

The helper method generateKeypairWithProtocolId creates a key pair using either ECDSA or RSA algorithms.

import { IKeyPairData, PlatformUtils } from "../iov42/core-sdk";

const platformUtils = new PlatformUtils ();
const protocolId = "SHA256WithECDSA"; // Specifies ECDSA algorithm for generating the key pair
const keyPair = platformUtils .generateKeypairWithProtocolId(protocolId);

Creation

The method createIdentity creates an identity in the iov42 platform.

import { v4 as uuidv4 } from "uuid";
import { IKeyPairData, PlatformClient, PlatformUtils } from "../iov42/core-sdk";

const rpcUrl = "https://api.sandbox.iov42.dev"; //Url of the iov42 API server
const platformClient = new PlatformClient(rpcUrl);
const platformUtils = new PlatformUtils();

const protocolId: string = "SHA256WithECDSA"; // Specifies ECDSA algorithm for generating the key pair
const identityId: string = uuidv4();
const keyPair: IKeyPairData = platformUtils.generateKeypairWithProtocolId(protocolId, identityId);
const requestId: string = uuidv4();
const request = {
    identityId,
    publicCredentials : {
        key: keyPair.pubKeyBase64,
        protocolId: input.protocolId,
    },
    requestId,
};
platformClient.createIdentity(request, keyPair)
.then( (response) => {
    // Check response
})
.catch( (error) => {
    console.error(error);
});

Reading

The method getIdentity reads an identity in the iov42 platform.

import { IKeyPairData, PlatformClient } from "../iov42/core-sdk";

const rpcUrl = "https://api.sandbox.iov42.dev"; //Url of the iov42 API server
const platformClient = new PlatformClient(rpcUrl);

const keyPair: IKeyPairData = "<previously generated key pair>";
const identityId: string = "<existing identityId>";
platformClient.getIdentity(identityId, keyPair);
.then( (response) => {
    // Check response
})
.catch( (error) => {
    console.error(error);
});

The method getPublicKey retrieves an identity's public key.

import { IKeyPairData, PlatformClient } from "../iov42/core-sdk";

const rpcUrl = "https://api.sandbox.iov42.dev"; //Url of the iov42 API server
const platformClient = new PlatformClient(rpcUrl);

const keyPair: IKeyPairData = "<previously generated key pair>";
const identityId: string = "<existing identityId>";
platformClient.getPublicKey(identityId, keyPair);
.then( (response) => {
    // Check response
})
.catch( (error) => {
    console.error(error);
});