Options
All
  • Public
  • Public/Protected
  • All
Menu

Class KeyRing

The KeyRing provides an interface that allows you to generate and store a pair of ed25519 keys, as well as sign and verify ed25519 signatures.

It takes a directory as the only argument of the constructor. It will create a new keyring in this directory or load the keyring from the directory if it is already present.

It also takes the special string :memory: as a parameter to only store the keys in memory, so this module an run in a browser. Make sure you provide a way for the client to export the keys and cert if you do this.

Keyrings can only be used with one coordinator. You can not connect to two different coordinators with one keyring, you must generate a new keyring for each coordinator.

Example Usage:

const keyring = new KeyRing("./keyring");

// If you want to perform operations with the keyring, wait for the ready event.
  keyring.on("ready", () => {
  const signed = keyring.sign(keyring.getPub());
  const verified = keyring.verify(keyring.getPub(), signed, keyring.getPub());

  if (verified) {
    console.log("The signature is verified!");
  }
});

  keyring.on("error", (error: Error) => {
    // do something with the error
  });

Note that the sign() and verify() functions take uint8 arrays. If you need to convert hex strings into Uint8 arrays, use the helper functions in the Utils class.

Hierarchy

  • EventEmitter
    • KeyRing

Index

Constructors

constructor

  • new KeyRing(keyFolder: string, secretKey?: string | null, cert?: string | null): KeyRing
  • Parameters

    • keyFolder: string

      The folder where you want the keys to be saved. If the folder does not exist, it will be created. Keys are saved as utf8 encoded hex strings on the disk.

    • Default value secretKey: string | null = null

      If you are initializing a secret key you already have, input it here.

    • Default value cert: string | null = null

      If you already have a cert, you may input it here. You must supply the cert if you have connected with this keypair before.

    Returns KeyRing

Events

on

  • on(event: "ready", callback: () => void): this
  • on(event: "error", callback: (error: Error) => void): this
  • This is emitted whenever the keyring is done initializing. In the initialization process, it will either load or create the key folder and key files at the specified path.

    Example:

      keyring.on("ready", () => {
      const signed = keyring.sign(keyring.getPub());
      const verified = keyring.verify(keyring.getPub(), signed, keyring.getPub());
    
      if (verified) {
        console.log("The signature is verified!");
      }
    });
    
      keyring.on("error", (error: Error) => {
        // do something with the error
      });

    Parameters

    • event: "ready"
    • callback: () => void
        • (): void
        • Returns void

    Returns this

  • This is emitted whenever the keyring experiences an error initializing.

    Example:

    
      keyring.on("error", (error: Error) => {
        // do something with the error
      });

    Parameters

    • event: "error"
    • callback: (error: Error) => void
        • (error: Error): void
        • Parameters

          • error: Error

          Returns void

    Returns this

Methods

getCert

  • getCert(): Uint8Array | null
  • Get the current cert, if there is one. There will not be a cert if the keyring has never been used to connect to a cordinator before.

    Returns Uint8Array | null

    The cert, or null.

getKeyFolder

  • getKeyFolder(): string
  • Get the keyring directory path.

    Returns string

    The key folder path.

getPriv

  • getPriv(): Uint8Array

getPub

  • getPub(): Uint8Array

init

  • init(): void
  • Re-initializes the keyring. This may be useful if you've made changes to the key files on disk and want them to update.

    Returns void

setCert

  • setCert(cert: Uint8Array): void
  • Sets the current cert. This is used internally by the Channel class.

    Parameters

    • cert: Uint8Array

    Returns void

sign

  • sign(message: Uint8Array): Uint8Array
  • Signs a message with the keyring private key.

    Parameters

    • message: Uint8Array

      The message to sign.

    Returns Uint8Array

    The resulting signature.

verify

  • verify(message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean
  • Verifies a message signature is valid for a given public key.

    Parameters

    • message: Uint8Array

      The message to sign.

    • signature: Uint8Array

      The signature to verify.

    • publicKey: Uint8Array

      The public key to verify against.

    Returns boolean

    true if the signature verifies, false if it doesn't.

Generated using TypeDoc