UNPKG

@bsv/sdk

Version:

BSV Blockchain Software Development Kit

102 lines 5.57 kB
import { PrivateKey, PublicKey, SymmetricKey } from '../primitives/index.js'; import { Counterparty, KeyDeriverApi } from './KeyDeriver.js'; import { WalletProtocol } from './Wallet.interfaces.js'; /** * A cached version of KeyDeriver that caches the results of key derivation methods. * This is useful for optimizing performance when the same keys are derived multiple times. * It supports configurable cache size with sane defaults and maintains cache entries using LRU (Least Recently Used) eviction policy. */ export default class CachedKeyDeriver implements KeyDeriverApi { private readonly keyDeriver; private readonly cache; private readonly maxCacheSize; /** * The root key from which all other keys are derived. */ rootKey: PrivateKey; /** * The identity of this key deriver which is normally the public key associated with the `rootKey` */ identityKey: string; /** * Initializes the CachedKeyDeriver instance with a root private key and optional cache settings. * @param {PrivateKey | 'anyone'} rootKey - The root private key or the string 'anyone'. * @param {Object} [options] - Optional settings for the cache. * @param {number} [options.maxCacheSize=1000] - The maximum number of entries to store in the cache. */ constructor(rootKey: PrivateKey | 'anyone', options?: { maxCacheSize?: number; }); /** * Derives a public key based on protocol ID, key ID, and counterparty. * Caches the result for future calls with the same parameters. * @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name. * @param {string} keyID - The key identifier. * @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone'). * @param {boolean} [forSelf=false] - Whether deriving for self. * @returns {PublicKey} - The derived public key. */ derivePublicKey(protocolID: WalletProtocol, keyID: string, counterparty: Counterparty, forSelf?: boolean): PublicKey; /** * Derives a private key based on protocol ID, key ID, and counterparty. * Caches the result for future calls with the same parameters. * @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name. * @param {string} keyID - The key identifier. * @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone'). * @returns {PrivateKey} - The derived private key. */ derivePrivateKey(protocolID: WalletProtocol, keyID: string, counterparty: Counterparty): PrivateKey; /** * Derives a symmetric key based on protocol ID, key ID, and counterparty. * Caches the result for future calls with the same parameters. * @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name. * @param {string} keyID - The key identifier. * @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone'). * @returns {SymmetricKey} - The derived symmetric key. * @throws {Error} - Throws an error if attempting to derive a symmetric key for 'anyone'. */ deriveSymmetricKey(protocolID: WalletProtocol, keyID: string, counterparty: Counterparty): SymmetricKey; /** * Reveals the shared secret between the root key and the counterparty. * Caches the result for future calls with the same parameters. * @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone'). * @returns {number[]} - The shared secret as a number array. * @throws {Error} - Throws an error if attempting to reveal a shared secret for 'self'. */ revealCounterpartySecret(counterparty: Counterparty): number[]; /** * Reveals the specific key association for a given protocol ID, key ID, and counterparty. * Caches the result for future calls with the same parameters. * @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone'). * @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name. * @param {string} keyID - The key identifier. * @returns {number[]} - The specific key association as a number array. */ revealSpecificSecret(counterparty: Counterparty, protocolID: WalletProtocol, keyID: string): number[]; /** * Generates a unique cache key based on the method name and input parameters. * @param {string} methodName - The name of the method. * @param {...any} args - The arguments passed to the method. * @returns {string} - The generated cache key. */ private generateCacheKey; /** * Serializes an argument to a string for use in a cache key. * @param {any} arg - The argument to serialize. * @returns {string} - The serialized argument. */ private serializeArgument; /** * Retrieves an item from the cache and updates its position to reflect recent use. * @param {string} cacheKey - The key of the cached item. * @returns {any} - The cached value. */ private cacheGet; /** * Adds an item to the cache and evicts the least recently used item if necessary. * @param {string} cacheKey - The key of the item to cache. * @param {any} value - The value to cache. */ private cacheSet; } //# sourceMappingURL=CachedKeyDeriver.d.ts.map