Skip to content

Latest commit

 

History

History
103 lines (80 loc) · 2.64 KB

1.md

File metadata and controls

103 lines (80 loc) · 2.64 KB

Getting Started

To instantiate an instance of SSIMON, you need to pass it an array of supported adapters, and a storage.

  • adapters: used to figure out which methods are supported and how to interact with them
  • storage: used to store secrets, and configuration information about that identity data

Storage

The storage which you need to pass to SSIMON, needs to be compatible with the StorageSpec, either you can implement a storage of your own (example use case would be wanting to interact with a custom network) or use a generic store

Implementing Own Interface

you can implement your own Storage by

import { StorageSpec } from "@tanglelabs/ssimon";

export class MyStorage implements StorageSpec {
    ...
}

Using the GenericStore

generic store is a basic implementation of the storage driver which can take a reader and a writer function and then create a StorageSpec compatible instance

Example implementation for a filesystem store

import { GenericStore, encryptWithAES } from "@tanglelabs/ssimon";
import { readFile, writeFile } from "fs/promises";

const constructFileStore = ({
  path,
  password,
}: {
  path: string;
  password: string;
}) => {
  /**
   * FS writer
   */
  const writer = async (body: string) => {
    await writeFile(path, body);
  };

  /**
   * FS Reader
   */
  const reader = async () => {
    const data = await readFile(path).catch(async (e) => {
      if (e.code === "ENOENT") {
        const encryptedEmptyArray = encryptWithAES("[]", password);
        await writer(encryptedEmptyArray);
        return encryptedEmptyArray;
      }
      throw new Error();
    });

    return data.toString();
  };

  /**
   * Construct a new FS Store and return
   */

  const store = new GenericStore({ path, password, reader, writer });
  return store;
};

Instantiating The Manager

to instantiate identity manager we can consume any adapter, the example below consumes the did:key adapter

import {
  GenericStore,
  IdentityManager,
  encryptWithAES,
} from "@tanglelabs/ssimon";
import path from "path";
import { DidKeyAdapter } from "@tanglelabs/key-identity-adapter";
import { DidWebAdapter } from "@tanglelabs/web-identity-adapter";

export async function getIdentityManager() {
  // create a new storage using the FS Storage Builder
  // the path specified here, would be where all the encrypted identity data is stored
  const storage = constructFileStore({
    path: path.resolve(__dirname, `./identity`),
    password: "asdf",
  });

  // Create a new IdentityManger Instance
  const manager = await IdentityManager.build({
    adapters: [DidKeyAdapter, DidWebAdapter],
    storage,
  });

  return manager;
}