0.1.4 • Published 5 years ago

@blobaa/baa-did-method-handler-ts v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

baa-did-method-handler-ts

A handler for the baa DID method written in TypeScript.

Table of Contents

Background

This library implements a handler for the baa DID method to enable the Ardor Blockchain to act as a DPKI (Public Utility) within the Trust over IP Stack for Self-Sovereign Identity (SSI).

Install

npm install @blobaa/baa-did-method-handler-ts

Usage

Create and Register DID and DID Document

import { DIDDocKey, DIDDocRelationship, DIDDocRelationshipType, DIDDocument } from "@blobaa/did-document-ts";
import { CreateDIDParams, baaMethodHandler } from "@blobaa/baa-did-method-handler-ts";


const createDID = async(): Promise<void> => {

    /* Even though not necessary, it is recommended to create a DID Document template with
      the @blobaa/did-document-ts package.
    */

    /* create or import DID Document public keys  */
    const key = new DIDDocKey();
    await key.generate();
    const publicKey = key.publish();

    /* create verification relationships */
    const authentication = new DIDDocRelationship({
        relationshipType: DIDDocRelationshipType.AUTHENTICATION,
        publicKeys: [ publicKey ]
    });

    const document = new DIDDocument({
        relationships: [ authentication ]
    });

    const didDocTsTemplate = document.publish();


    /* You can also use an otherwise created template */
    const didDocTemplate = {
        "@context": [ "https://www.w3.org/ns/did/v1", "https://w3id.org/security/v1" ],
        id: "", // already existing DIDs will be overwritten in the resolution process
        authentication: [
            {
                id: "#z6Mkq9uAju2ezpgoT8q88pqMDLcZ4wJQXZiNKpT4SyJ8xCDQ",
                type: "Ed25519VerificationKey2018",
                publicKeyBase58: "Bhe89enDfHCLLdzRTFsWNF4ZFN2Z7gU1doY8chL82yS2"
            }
        ]
    };


    /* set parameters */
    const params: CreateDIDParams = {
        didDocumentTemplate: didDocTemplate,
        passphrase: "<controller account passphrase>",
        isTestnetDid: true
    };

    try {

        /* create and register DID and DID Document */
        const response = await baaMethodHandler.createDID("https://testardor.jelurida.com", params);

        console.log("DID:", response.did); // did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe
        console.log("DID Document:", JSON.stringify(response.didDocument, undefined, 4));
        /*
        {
            "@context": [
                "https://www.w3.org/ns/did/v1",
                "https://w3id.org/security/v1"
            ],
            "id": "did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe",
            "authentication": [
                {
                    "id": "did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe#z6Mkq9uAju2ezpgoT8q88pqMDLcZ4wJQXZiNKpT4SyJ8xCDQ",
                    "type": "Ed25519VerificationKey2018",
                    "publicKeyBase58": "Bhe89enDfHCLLdzRTFsWNF4ZFN2Z7gU1doY8chL82yS2"
                }
            ]
        }
        */

    } catch (e) { /* see error handling */ }
};

createDID();

Resolve DID

import { ResolveDIDParams, baaMethodHandler } from "@blobaa/baa-did-method-handler-ts";


const resolveDID = async(): Promise<void> => {

    /* set parameters */
    const params: ResolveDIDParams = {
        did:"did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe"
    };

    try {

        /* resolve DID */
        const response = await baaMethodHandler.resolveDID("https://testardor.jelurida.com", params);

        console.log("DID:", response.did); // did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe
        console.log("DID Document", JSON.stringify(response.didDocument, undefined, 4));
        /*
        {
            "@context": [
                "https://www.w3.org/ns/did/v1",
                "https://w3id.org/security/v1"
            ],
            "id": "did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe",
            "authentication": [
                {
                    "id": "did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe#z6Mkq9uAju2ezpgoT8q88pqMDLcZ4wJQXZiNKpT4SyJ8xCDQ",
                    "type": "Ed25519VerificationKey2018",
                    "publicKeyBase58": "Bhe89enDfHCLLdzRTFsWNF4ZFN2Z7gU1doY8chL82yS2"
                }
            ]
        }
        */

    } catch (e) { /* see error handling */ }
};

resolveDID();

Update DID Document

import { DIDDocKey, DIDDocRelationship, DIDDocRelationshipType, DIDDocument } from "@blobaa/did-document-ts";
import { baaMethodHandler, UpdateDIDDocumentParams } from "@blobaa/baa-did-method-handler-ts";


const updateDIDDocument = async(): Promise<void> => {

    /* create new DID Document template */
    const key = new DIDDocKey();
    await key.generate();
    const publicKey = key.publish();

    const authentication = new DIDDocRelationship({
        relationshipType: DIDDocRelationshipType.AUTHENTICATION,
        publicKeysAsRef: [ publicKey ]
    });

    const assertion = new DIDDocRelationship({
        relationshipType: DIDDocRelationshipType.ASSERTION_METHOD,
        publicKeysAsRef: [ publicKey ]
    });

    const document = new DIDDocument({
        relationships: [ authentication, assertion ],
        publicKeys: [ publicKey ]
    });

    const didDocTsTemplate = document.publish();


    /* set parameters */
    const params: UpdateDIDDocumentParams = {
        did:"did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe",
        newDidDocumentTemplate: didDocTsTemplate,
        passphrase: "<controller account passphrase"
    };

    try {

        /* update DID Document */
        const response = await baaMethodHandler.updateDIDDocument("https://testardor.jelurida.com", params);

        console.log("DID:", response.did); // did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe
        console.log("new DID Document", JSON.stringify(response.newDidDocument, undefined, 4));
        /*
        {
            "@context": [
                "https://www.w3.org/ns/did/v1",
                "https://w3id.org/security/v1"
            ],
            "id": "did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe",
            "publicKey": [
                {
                    "id": "did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe#z6MkqSkgHX9yBBSsmpABk2wgyjE2SpDJTYtWKfo2HSgY4Ymm",
                    "type": "Ed25519VerificationKey2018",
                    "publicKeyBase58": "BzVdhGuXqdxQfKKV4Tyr8dg2dEwT3fe9det6TAiX9KzP"
                }
            ],
            "authentication": [
                "did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe#z6MkqSkgHX9yBBSsmpABk2wgyjE2SpDJTYtWKfo2HSgY4Ymm"
            ],
            "assertionMethod": [
                "did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe#z6MkqSkgHX9yBBSsmpABk2wgyjE2SpDJTYtWKfo2HSgY4Ymm"
            ]
        }
        */

    } catch (e) { /* see error handling */ }
};

updateDIDDocument();

Update DID Controller Account

import { baaMethodHandler, UpdateDIDControllerParams } from "@blobaa/baa-did-method-handler-ts";


const updateDIDController = async(): Promise<void> => {

    /* set parameters */
    const params: UpdateDIDControllerParams = {
        did:"did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe",
        passphrase: "<old controller account passphrase>",
        newPassphrase: "<new controller account passphrase>"
    };

    try {

        /* update DID Controller Account */
        const response = await baaMethodHandler.updateDIDController("https://testardor.jelurida.com", params);

        console.log("DID:", response.did); // did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe
        console.log("old Account:", response.oldControllerAccount); // "ARDOR-S27P-EHWT-8D2L-937R7"
        console.log("new Account:", response.newControllerAccount); // "ARDOR-YQ26-W5RK-6ATW-G9HRT"

    } catch (e) { /* see error handling */ }
};

updateDIDController();

Deactivate DID

import { DeactivateDIDParams, baaMethodHandler } from "@blobaa/baa-did-method-handler-ts";


const deactivateDID = async(): Promise<void> => {

    /* set parameters */
    const params: DeactivateDIDParams = {
        did:"did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe",
        passphrase: account.bob.secret,
    };

    try {

        /* deactivate DID */
        const response = await baaMethodHandler.deactivateDID("https://testardor.jelurida.com", params);

        console.log("deactivated DID:", response.deactivatedDid); // did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe

    } catch (e) { /* see error handling */ }
};

deactivateDID();

Error Handling

There is an unified error handling for all APIs. Every API throws an error in case of any failures or unmet conditions. Every error implements the 'Error' interface of this library. The interface consist of two data fields. The code field contains a value of the 'ErrorCode' enum to indicate the error reason. The description field contains a human readable description of the error reason.

import { baaMethodHandler, Error, ErrorCode, ResolveDIDParams } from "@blobaa/baa-did-method-handler-ts";


const errorHandlingExample = async(): Promise<void> => {

    const params: ResolveDIDParams = {
        did:"did:baa:t:0cfe0be67dc0d4e1162fa2e9ccec798d83f8fd5d78a8a36ccd71e194abc60efe"
    };

    try {

        /* resolve DID */
        await baaMethodHandler.resolveDID("https://testardor.jelurida.com", params);

    } catch (e) {

        /* all errors implement the library's Error interface */
        const error = e as Error;

        /* every error has an error code that corresponds to the ErrorCode enum */
        if (error.code === ErrorCode.DID_DEACTIVATED) {
            //  handle did deactivated error here
        }

        console.log(error.code);
        console.log(error.description);
    }
};

errorHandlingExample();

Module Instantiation

The handler module is pre instantiated and importable via the lower case module name. If you need the class definition, import it via the upper case name.

import { baaMethodHandler, BaaMethodHandler, ResolveDIDParams } from "@blobaa/baa-did-method-handler-ts";


const moduleInstantiationExample = async (): Promise<void> => {

    const params: ResolveDIDParams = {
        did:"did:baa:t:0b1c85a9759eb90a576aeb3be80f76b21d95a0251396b147caa9a5f941a87d82#z6MkqSkgHX9yBBSsmpABk2wgyjE2SpDJTYtWKfo2HSgY4Ymm"
    };

    try {

        /* use the default instance */
        const response = await baaMethodHandler.resolveDID("https://testardor.jelurida.com", params);
        console.log(response);

        /* use your own instance */
        const myBaaMethodHandler = new BaaMethodHandler();
        const response = await myBaaMethodHandler.resolveDID("https://testardor.jelurida.com", params);
        console.log(response);

    } catch (e) { /* error handling */}
};

moduleInstantiationExample();

API

TBD

Contributing

PRs accepted.

If editing the Readme, please conform to the standard-readme specification.

License

MIT © Attila Aldemir

0.1.4

5 years ago

0.1.3

5 years ago