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
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
you can implement your own Storage by
import { StorageSpec } from "@tanglelabs/ssimon";
export class MyStorage implements StorageSpec {
...
}
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;
};
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;
}