0.3.1 • Published 3 years ago

@tallyho/keyring-controller v0.3.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

keyring-controller

A module for managing key material for multiple crypto networks, called "keyrings", useful for building and managing wallets.

Originally forked from the library to manage key material in MetaMask, keyring-controller has the additional goals of strict type safety, strong test coverage, and greater cross-chain flexibility.

A KeyringController has two main responsibilities:

  • Generating keyrings and using them to sign messages and transactions
  • Providing encrypted serialization and deserialization of secret material.

Installation

npm install @tallyho/keyring-controller --save

Usage

import KeyringController from "@tallyho/keyring-controller"
import SimpleKeyring from "eth-simple-keyring"

const persistedVaults: string[] = []

const keyringController = new KeyringController({
  keyringTypes: [SimpleKeyring], // optional array of types to support.
  initState: { ... }, // Last emitted persisted state.
  encryptor: {
    // An optional object for defining encryption schemes:
    // Defaults to Browser-native SubtleCrypto.
    encrypt: async (password: string, data: any) => {
      return "encrypted!"
    },
    decrypt: async (password: string, cipherText: string) => {
      return { foo: "bar" }
    },
  },
  persistVault: async (encryptedVault: string) => {
    // toy example, it's up to the consumer to persist vaults somewhere safe!
    persistedVaults.push(encryptedVault)
  }
})

// The KeyringController is also an event emitter:
this.keyringController.on("newAccount", (address: string) => {
  console.log(`New account created: ${address}`)
})
this.keyringController.on("removedAccount", handleThat)

Extension

To add new account types to a KeyringController, just make sure it follows the Keyring class protocol.

Methods

Currently the methods are heavily commented in the source, so it's the best place to look until we aggregate it here as well.