verifiablecredentials-crypto-sdk-typescript-keystore
Version:
Package for managing keys in a key store.
115 lines • 4.96 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const verifiablecredentials_crypto_sdk_typescript_keys_1 = require("verifiablecredentials-crypto-sdk-typescript-keys");
const base64url_1 = __importDefault(require("base64url"));
const KeyStoreOptions_1 = __importDefault(require("./KeyStoreOptions"));
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 [options] Options for retrieving.
*/
get(keyReference, options) {
if (this.store.has(keyReference.keyReference)) {
const key = this.store.get(keyReference.keyReference);
if (key.kty === verifiablecredentials_crypto_sdk_typescript_keys_1.KeyType.Oct) {
if (options && options.publicKeyOnly) {
const error = 'A secret does not has a public key';
return Promise.reject(new Error(error));
}
return Promise.resolve(key);
}
if (options && options.publicKeyOnly) {
switch (key.kty.toLowerCase()) {
case 'ec':
case 'okp':
case 'rsa':
return Promise.resolve(this.publicKeysOnly(key));
default:
const error = `A secret does not has a public key`;
return Promise.reject(new Error(error));
}
}
else {
return Promise.resolve(key);
}
}
else {
const error = `${keyReference.keyReference} not found`;
return Promise.reject(new Error(error));
}
}
publicKeysOnly(container) {
const publicKeyContainer = clone(container);
for (let inx = 0; inx < publicKeyContainer.keys.length; inx++) {
const key = publicKeyContainer.keys[inx];
switch (key.kty.toUpperCase()) {
case 'EC':
case 'OKP':
publicKeyContainer.keys[inx] = new verifiablecredentials_crypto_sdk_typescript_keys_1.EcPublicKey(key);
break;
case 'RSA':
publicKeyContainer.keys[inx] = new verifiablecredentials_crypto_sdk_typescript_keys_1.RsaPublicKey(key);
break;
}
}
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 Promise.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. If the key is a string it will be base64url encoded.
*/
save(keyIdentifier, key, _options = new KeyStoreOptions_1.default()) {
//todo serialization of the key needs to happen in here if key is string than create a oct key of it
console.log(`Store ${keyIdentifier.keyReference}`);
if (typeof key === 'string') {
key = new verifiablecredentials_crypto_sdk_typescript_keys_1.OctKey(base64url_1.default.encode(key));
}
if (this.store.get(keyIdentifier.keyReference)) {
const container = this.store.get(keyIdentifier.keyReference);
container.keys.push(key);
this.store.set(keyIdentifier.keyReference, container);
}
else {
// create new container
const container = new verifiablecredentials_crypto_sdk_typescript_keys_1.KeyContainer(key);
this.store.set(keyIdentifier.keyReference, container);
}
return Promise.resolve();
}
}
exports.default = KeyStoreInMemory;
//# sourceMappingURL=KeyStoreInMemory.js.map