"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EthereumKeyPairIdentity = void 0;
const utils_1 = require("@streamr/utils");
const ethers_1 = require("ethers");
const trackerless_network_1 = require("@streamr/trackerless-network");
const KeyPairIdentity_1 = require("./KeyPairIdentity");
/**
 * An Identity that derives an Ethereum address from a secp256k1 private key
 * and uses that as the UserID instead of the actual public key.
 */
class EthereumKeyPairIdentity extends KeyPairIdentity_1.KeyPairIdentity {
    constructor(privateKey, address) {
        const impliedAddress = new ethers_1.Wallet(privateKey).address.toLowerCase();
        super((0, utils_1.hexToBinary)(impliedAddress), (0, utils_1.hexToBinary)(privateKey));
        if (address && address.toLowerCase() !== impliedAddress) {
            throw new Error(`The given publicKey does not match the privateKey! The privateKey implies address: ${impliedAddress}`);
        }
    }
    // eslint-disable-next-line class-methods-use-this
    assertKeyPairIsValid() {
        // Ensured by constructor
    }
    // eslint-disable-next-line class-methods-use-this
    getSignatureType() {
        return trackerless_network_1.SignatureType.ECDSA_SECP256K1_EVM;
    }
    // eslint-disable-next-line class-methods-use-this
    getExpectedPublicKeyLength() {
        // Address, not actual public key
        return 20;
    }
    // eslint-disable-next-line class-methods-use-this
    getExpectedPrivateKeyLength() {
        return 32;
    }
    async createMessageSignature(payload) {
        return utils_1.ECDSA_SECP256K1_EVM.createSignature(payload, this.privateKey);
    }
    async getTransactionSigner(rpcProviderSource) {
        const primaryProvider = rpcProviderSource.getProvider();
        return new ethers_1.Wallet((0, utils_1.binaryToHex)(this.privateKey), primaryProvider);
    }
    static fromConfig(config) {
        const privateKey = config.auth.privateKey;
        const address = config.auth.publicKey;
        return new EthereumKeyPairIdentity(privateKey, address);
    }
    static generate() {
        const wallet = ethers_1.Wallet.createRandom();
        return new EthereumKeyPairIdentity(wallet.privateKey);
    }
}
exports.EthereumKeyPairIdentity = EthereumKeyPairIdentity;
//# sourceMappingURL=EthereumKeyPairIdentity.js.map