UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

65 lines 3.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ const W3cCryptoApiConstants_1 = require("../../utilities/W3cCryptoApiConstants"); const base64url_1 = require("base64url"); const KeyTypeFactory_1 = require("../KeyTypeFactory"); const EcPrivateKey_1 = require("./EcPrivateKey"); const CryptoError_1 = require("../../CryptoError"); const JoseConstants_1 = require("../../protocols/jose/JoseConstants"); // Create and initialize EC context const BN = require('bn.js'); const elliptic = require('elliptic').ec; const secp256k1 = new elliptic('secp256k1'); const SUPPORTED_CURVES = ['K-256', 'P-256K', 'ed25519']; /** * Class to model EC pairwise keys */ class EcPairwiseKey { /** * Generate a pairwise key for the specified algorithms * @param cryptoFactory defining the key store and the used crypto api * @param personaMasterKey Master key for the current selected persona * @param algorithm for the key * @param peerId Id for the peer * @param extractable True if key is exportable */ static async generate(cryptoFactory, personaMasterKey, algorithm, peerId) { // This method is currently breaking the subtle crypto pattern and needs to be fixed to be platform independent // Get the subtle crypto const crypto = cryptoFactory.getMessageAuthenticationCodeSigners(W3cCryptoApiConstants_1.default.Hmac); // Generate the master key const alg = { name: W3cCryptoApiConstants_1.default.Hmac, hash: W3cCryptoApiConstants_1.default.Sha256 }; const signingKey = { kty: 'oct', alg: JoseConstants_1.default.Hs256, k: base64url_1.default.encode(personaMasterKey) }; const key = await crypto.importKey('jwk', signingKey, alg, false, ['sign']); const pairwiseKeySeed = await crypto.sign(alg, key, Buffer.from(peerId)); if (SUPPORTED_CURVES.indexOf(algorithm.namedCurve) === -1) { throw new CryptoError_1.default(algorithm, `Curve ${algorithm.namedCurve} is not supported`); } const privateKey = new BN(Buffer.from(pairwiseKeySeed)); const pair = secp256k1.keyPair({ priv: privateKey }); const pubKey = pair.getPublic(); const d = privateKey.toArrayLike(Buffer, 'be', 32); const x = pubKey.x.toArrayLike(Buffer, 'be', 32); const y = pubKey.y.toArrayLike(Buffer, 'be', 32); const pairwise = { crv: algorithm.namedCurve, d: base64url_1.default.encode(d), x: base64url_1.default.encode(x), y: base64url_1.default.encode(y), kty: KeyTypeFactory_1.default.createViaWebCrypto(algorithm), // Need an algorithm for kid generation - todo kid: '#key1' }; return new EcPrivateKey_1.default(pairwise); } } exports.default = EcPairwiseKey; //# sourceMappingURL=EcPairwiseKey.js.map