0.1.0-unstable.2 • Published 3 years ago

@siriusid-next/siriusid-wallet v0.1.0-unstable.2

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
3 years ago

@siriusid-next/siriusid-wallet

Getting Started

Installation

npm i @siriusid-next/siriusid-wallet --save

Usage

Use the wallet

import { wallet } from '@siriusid-next/siriusid-wallet';

Generate mnemonic

const mnemonic = await wallet.generateMnemonic();

Create an individual identity and submit it to the trusted identity registry. The identity DID Document and its metadata is anchored by batching using the Sirius trusted registry network, due to the lag incurred in registering the DID Document, which may take between 5-10 minutes, the registrationStatus will remain as PROCESSING until the DID Document can be retrieved from the public nodes.

const trustedIdentityRegistry = TrustedRegistry.SiriusIdTestNet;

const identity = await wallet.createIdentity(
    {
        name: 'My individual identity',
        type:IdentityTypes.Individual,
        trustedRegistryUrl: trustedIdentityRegistry,
    }
)

console.log(JSON.stringify(identity));

Create an individual identity using mnemonic phrase

const mnemonic = await wallet.generateMnemonic();

const trustedIdentityRegistry = TrustedRegistry.SiriusIdTestNet;

const identity = await wallet.createIdentity(
    {
        name: 'My individual identity',
        type:IdentityTypes.Individual,
        trustedRegistryUrl: trustedIdentityRegistry,
        fromMnemonic: mnemonic
    }
)

console.log(JSON.stringify(identity));

Resolve or retrieve the identity

const mnemonic = await wallet.generateMnemonic();

const trustedIdentityRegistry = TrustedRegistry.SiriusIdTestNet;

const identity = await wallet.getIdentity(
    {
        did: 'did:sirius:test:EiBTn3k2BNPaTFCL7BzVoz3Pbq4r-EiK9g01zIOcgJDtTQ',
        trustedRegistryUrl: trustedIdentityRegistry,
    }
)

console.log(JSON.stringify(identity));

Issue a new credential.

import {
  JsonWebKey,
  JsonWebSignature,
  JsonWebKey2020
} from "@transmute/json-web-signature";

// create the json web key based on ED25519 alg
const key = {
    id: "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr#z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
    type: "JsonWebKey2020",
    controller: "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
    publicKeyJwk: {
        kty: "OKP",
        crv: "Ed25519",
        x: "ijtvFnowiumYMcYVbaz6p64Oz6bXwe2V_9IlCgDR_38",
    },
    privateKeyJwk: {
        kty: "OKP",
        crv: "Ed25519",
        x: "ijtvFnowiumYMcYVbaz6p64Oz6bXwe2V_9IlCgDR_38",
        d: "ZrHpIW1JBb-sK2-wzKV0mQjbxpnxjUCu151QZ9_F_Vs",
    },
};

// construct the credential
const credential = {
    "@context": [
        "https://www.w3.org/2018/credentials/v1",
        "https://w3id.org/security/suites/jws-2020/v1",
    ],
    id: "http://example.edu/credentials/3732",
    type: ["VerifiableCredential"],
    issuer: {
        id: "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
    },
    issuanceDate: "2010-01-01T19:23:24Z",
    credentialSubject: {
      id: "did:example:ebfeb1f712ebc6f1c276e12ec21",
    },
};

// construct the options
const options = {
    format: ['vc', 'vc-jwt'], // results as the verifiable credential and its jwt format
    documentLoader: documentLoader,
    suite: new JsonWebSignature({
      key: await JsonWebKey.from(key as JsonWebKey2020),
    }),
};

// issue the credential
const issuedCredential = await wallet.createCredential({
    credential: credential,
    options: options,
});

console.log(JSON.stringify(issuedCredential));

Verify the issued credentials

const issuedCredential =   {
    "@context": [
    "https://www.w3.org/2018/credentials/v1",
    "https://w3id.org/security/suites/jws-2020/v1"
    ],
    "id": "http://example.edu/credentials/3732",
    "type": ["VerifiableCredential"],
    "issuer": {
    "id": "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr"
    },
    "issuanceDate": "2010-01-01T19:23:24Z",
    "credentialSubject": { "id": "did:example:ebfeb1f712ebc6f1c276e12ec21" },
    "proof": {
    "type": "JsonWebSignature2020",
    "created": "2010-01-01T19:23:24Z",
    "verificationMethod": "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr#z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
    "proofPurpose": "assertionMethod",
    "jws": "eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..k_7t6h5IGSWFAqIlqru3zyZ0FDPQGo88p9jDeKC1yw8oxd7xj6B70tZNSaspWkMyWbXFmZ5yCO8dlZZ9_kKbAQ"
    }
};


const options = {
    format: ['vc', 'vc-jwt'],
    documentLoader: documentLoader,
    suite: [new JsonWebSignature()],
};

const result = await wallet.verifyCredential({
    credential: issuedCredential,
    options: options,
});

console.log(JSON.stringify(result));