@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
71 lines • 3.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const KeyTypeFactory_1 = require("./KeyTypeFactory");
const W3cCryptoApiConstants_1 = require("../utilities/W3cCryptoApiConstants");
const EcPairwiseKey_1 = require("./ec/EcPairwiseKey");
const CryptoError_1 = require("../CryptoError");
const RsaPairwiseKey_1 = require("./rsa/RsaPairwiseKey");
const JoseConstants_1 = require("../protocols/jose/JoseConstants");
/**
* Class to model pairwise keys
*/
class PairwiseKey {
/**
* Create an instance of @class PairwiseKey.
* @param cryptoFactory The crypto factory object.
*/
constructor(cryptoFactory) {
// Set of master keys for the different persona's
this.masterKeys = new Map();
this.cryptoFactory = cryptoFactory;
}
/**
* Generate a pairwise key for the specified algorithms
* @param algorithm for the key
* @param seedReference Reference to the seed
* @param personaId Id for the persona
* @param peerId Id for the peer
*/
async generatePairwiseKey(algorithm, seedReference, personaId, peerId) {
const personaMasterKey = await this.generatePersonaMasterKey(seedReference, personaId);
const keyType = KeyTypeFactory_1.default.createViaWebCrypto(algorithm);
switch (keyType) {
case KeyTypeFactory_1.KeyType.EC:
case KeyTypeFactory_1.KeyType.OKP:
return EcPairwiseKey_1.default.generate(this.cryptoFactory, personaMasterKey, algorithm, peerId);
case KeyTypeFactory_1.KeyType.RSA:
return RsaPairwiseKey_1.default.generate(this.cryptoFactory, personaMasterKey, algorithm, peerId);
default:
throw new CryptoError_1.default(algorithm, `Pairwise key for type '${keyType}' is not supported.`);
}
}
/**
* Generate a pairwise master key.
* @param seedReference The master seed for generating pairwise keys
* @param personaId The owner DID
*/
async generatePersonaMasterKey(seedReference, personaId) {
let mk = this.masterKeys.get(personaId);
if (mk) {
return mk;
}
// Get the seed
const jwk = (await this.cryptoFactory.keyStore.get(seedReference, false)).getKey();
// Get the subtle crypto
const crypto = this.cryptoFactory.getMessageAuthenticationCodeSigners(W3cCryptoApiConstants_1.default.Hmac);
// Generate the master key
const alg = { name: W3cCryptoApiConstants_1.default.Hmac, hash: W3cCryptoApiConstants_1.default.Sha512 };
const masterJwk = {
kty: 'oct',
alg: JoseConstants_1.default.Hs512,
k: jwk.k
};
let key = await crypto.importKey('jwk', masterJwk, alg, false, ['sign']);
const masterKey = await crypto.sign(alg, key, Buffer.from(personaId));
mk = Buffer.from(masterKey);
this.masterKeys.set(personaId, mk);
return mk;
}
}
exports.default = PairwiseKey;
//# sourceMappingURL=PairwiseKey.js.map