UNPKG

@bajetech/digitalbits-wallet-sdk

Version:

A library to make it easier to write wallets that interact with the DigitalBits blockchain

134 lines (133 loc) 4.77 kB
import { Transaction } from "xdb-digitalbits-sdk"; import { Encrypter, Key, KeyMetadata, KeyStore, KeyTypeHandler, UnstoredKey } from "./types"; export interface KeyManagerParams { keyStore: KeyStore; shouldCache?: boolean; } export interface StoreKeyParams { key: Key | UnstoredKey; encrypterName: string; password: string; } export interface SignTransactionParams { transaction: Transaction; id: string; password: string; custom?: { [key: string]: any; }; } export interface ChangePasswordParams { oldPassword: string; newPassword: string; } /** * The `KeyManager` class is your primary gateway API for encrypting and storing * your users' DigitalBits keys. Make an instance of this and use that * instance to create, read, update, and delete secret keys. * * Note that at this time, `KeyManager` does not generate keys, nor does it * provide UI for accepting it from a user. You're app will have to implement * those features and pass the resulting keys to this class. * * `KeyManager` employs a plugin system. You may implement three types of * interfaces and add them to the `KeyManager` (or use our reference * plugins): * * - A `Encrypter` handles encrypting and decrypting a key. * - A `KeyStore` handles storing, updating, loading, and removing your keys * after they've been encrypted. * - (optional) A `KeyTypeHandler` encodes how to handle keytypes. For example, * raw DigitalBits secret seeds may be signed differently than, say, how Ledger * keys sign transactions (Ledger does not currently support DigitalBits * blockchain accounts but it may in the future). * * Normally, you won't have to write `KeyTypeHandler` interfaces; the SDK * currently provides handlers for these key types: * * - AstraX * - Plaintext secrets * * ### Plugin names * * Each plugin you pass to `KeyManager` will have a `name` property, which * should be unique to that particular interface and to the `KeyManager`. So if * you make an `Encrypter` named "YourUniqueEncrypter", we'll save all your * user's keys with that encrypter name, and we'll look for an `Encrypter` of * that name to decrypt those keys until the end of time! */ export declare class KeyManager { private encrypterMap; private keyStore; private keyHandlerMap; private keyCache; private shouldCache; constructor(params: KeyManagerParams); /** * Register a KeyTypeHandler for a given key type. * @param {KeyTypeHandler} keyHandler */ registerKeyHandler(keyHandler: KeyTypeHandler): void; /** * Register a new encrypter. * @param {Encrypter} encrypter */ registerEncrypter(encrypter: Encrypter): void; /** * Stores a key in the keyStore after encrypting it with the encrypterName. * * @async * @param key Key object to store. an `id` field is optional; if you don't * provide one, we'll generate a random number. The id will be used to read, * change, update, and delete keys. * @param password encrypt key with this as the secret * @param encrypterName encryption algorithm to use (must have been * registered) * * @returns The metadata of the key */ storeKey(params: StoreKeyParams): Promise<KeyMetadata>; /** * Load and decrypt one key, given its id. * * @returns Decrypted key */ loadKey(id: string, password: string): Promise<Key>; /** * Get a list of all stored key ids. * * @returns List of ids */ loadAllKeyIds(): Promise<string[]>; /** * Remove the key specified by this key id. * * @async * @param id Specifies which key to remove. * The id is computed as `sha1(private key + public key)`. * @returns Metadata of the removed key */ removeKey(id: string): Promise<KeyMetadata | undefined>; /** * Sign a transaction using the specified key id. Supports both using a * cached key and going out to the keystore to read and decrypt * * @async * @param {Transaction} transaction Transaction object to sign * @param {string} id Key to sign with. The id is computed as * `sha1(private key + public key)`. * @returns Signed transaction */ signTransaction(params: SignTransactionParams): Promise<Transaction>; /** * Update the stored keys to be encrypted with the new password. * * @async * @param oldPassword the user's old password * @param newPassword the user's new password * @returns {Promise<KeyMetadata[]>} */ changePassword(params: ChangePasswordParams): Promise<KeyMetadata[]>; private _readFromCache; private _writeIndexCache; }