"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");
const signingUtil = new utils_1.EcdsaSecp256k1Evm();
/**
 * 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 {
    assertValidKeyPair() {
        signingUtil.assertValidKeyPair(this.publicKey, this.privateKey);
    }
    // eslint-disable-next-line class-methods-use-this
    getSignatureType() {
        return trackerless_network_1.SignatureType.ECDSA_SECP256K1_EVM;
    }
    async createMessageSignature(payload) {
        return signingUtil.createSignature(payload, this.privateKey);
    }
    async getTransactionSigner(rpcProviderSource) {
        const primaryProvider = rpcProviderSource.getProvider();
        return new ethers_1.Wallet((0, utils_1.binaryToHex)(this.privateKey), primaryProvider);
    }
    /** @internal */
    static fromConfig(config) {
        const privateKey = config.auth.privateKey;
        const address = config.auth.publicKey;
        return EthereumKeyPairIdentity.fromPrivateKey(privateKey, address);
    }
    static fromPrivateKey(privateKey, address) {
        return new EthereumKeyPairIdentity((0, utils_1.hexToBinary)(address ?? new ethers_1.Wallet(privateKey).address), (0, utils_1.hexToBinary)(privateKey));
    }
    static generate() {
        const keyPair = signingUtil.generateKeyPair();
        return new EthereumKeyPairIdentity(keyPair.publicKey, keyPair.privateKey);
    }
}
exports.EthereumKeyPairIdentity = EthereumKeyPairIdentity;
//# sourceMappingURL=EthereumKeyPairIdentity.js.map