"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ECDSAKeyPairIdentity = void 0;
const utils_1 = require("@streamr/utils");
const KeyPairIdentity_1 = require("./KeyPairIdentity");
const trackerless_network_1 = require("@streamr/trackerless-network");
const signingUtil = new utils_1.EcdsaSecp256r1();
/**
 * An identity that uses ECDSA on the SECP256R1 curve
 */
class ECDSAKeyPairIdentity extends KeyPairIdentity_1.KeyPairIdentity {
    cachedJWK;
    assertValidKeyPair() {
        signingUtil.assertValidKeyPair(this.publicKey, this.privateKey);
    }
    // eslint-disable-next-line class-methods-use-this
    getSignatureType() {
        return trackerless_network_1.SignatureType.ECDSA_SECP256R1;
    }
    async createMessageSignature(payload) {
        // Cache the privateKey in JWK format for a performance optimization
        this.cachedJWK ??= signingUtil.privateKeyToJWK(this.privateKey);
        return signingUtil.createSignature(payload, this.cachedJWK);
    }
    /** @internal */
    static fromConfig(config) {
        const keyPairConfig = KeyPairIdentity_1.KeyPairIdentity.getKeyPairFromConfig(config);
        const privateKey = (0, utils_1.hexToBinary)(keyPairConfig.privateKey);
        const publicKey = keyPairConfig.publicKey ? (0, utils_1.hexToBinary)(keyPairConfig.publicKey) : signingUtil.getPublicKeyFromPrivateKey(privateKey);
        return new ECDSAKeyPairIdentity(publicKey, privateKey);
    }
    static generate() {
        const keyPair = signingUtil.generateKeyPair();
        return new ECDSAKeyPairIdentity(keyPair.publicKey, keyPair.privateKey);
    }
}
exports.ECDSAKeyPairIdentity = ECDSAKeyPairIdentity;
//# sourceMappingURL=ECDSAKeyPairIdentity.js.map