UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

93 lines 3.56 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); const KeyTypeFactory_1 = require("../keys/KeyTypeFactory"); const clone = require('clone'); /** * Class defining methods and properties for a light KeyStore */ class KeyStoreInMemory { constructor() { this.store = new Map(); } /** * Returns the key container associated with the specified * key identifier. * @param keyReference for which to return the key. * @param [publicKeyOnly] True if only the public key is needed. */ get(keyReference, publicKeyOnly = true) { return new Promise((resolve, reject) => { if (this.store.has(keyReference)) { const key = this.store.get(keyReference); if (key.kty === KeyTypeFactory_1.KeyType.Oct) { if (publicKeyOnly) { throw new Error('A secret does not has a public key'); } return resolve(key); } if (publicKeyOnly) { switch (key.kty.toLowerCase()) { case 'ec': case 'okp': case 'rsa': return resolve(this.publicKeysOnly(key)); default: throw new Error(`A secret does not has a public key`); } } else { resolve(key); } } else { reject(`${keyReference} not found`); } }); } publicKeysOnly(container) { const publicKeyContainer = clone(container); for (let inx = 0; inx < publicKeyContainer.keys.length; inx++) { if (publicKeyContainer.keys[inx].getPublicKey) { publicKeyContainer.keys[inx] = publicKeyContainer.keys[inx].getPublicKey(); } } return publicKeyContainer; } /** * Lists all keys with their corresponding key ids */ list() { const dictionary = {}; for (let [key, container] of this.store) { if (container) { const keyListInContainer = { kty: container.kty, kids: [] }; dictionary[key] = keyListInContainer; for (let keyInContainer of container.keys) { keyListInContainer.kids.push(keyInContainer.kid); } } } return new Promise((resolve) => { resolve(dictionary); }); } /** * Saves the specified key to the key store using * the key identifier. * @param keyIdentifier for the key being saved. * @param key being saved to the key store. */ save(keyIdentifier, key) { console.log(this.store.toString() + keyIdentifier + key.toString()); this.store.set(keyIdentifier, key); return new Promise((resolve) => { resolve(); }); } } exports.default = KeyStoreInMemory; //# sourceMappingURL=KeyStoreInMemory.js.map