"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockEddsaSigner = void 0;
const crypto = __importStar(require("crypto"));
class MockEddsaSigner {
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    constructor() {
        this.ed25519PrivKeyPrefix = '302e020100300506032b657004220420';
        this.ed25519PubKeyPrefix = '302a300506032b6570032100';
    }
    getPublicKey() {
        return this.publicKey;
    }
    toDER(hex, privateKey = true) {
        return Buffer.concat([
            Buffer.from(privateKey ? this.ed25519PrivKeyPrefix : this.ed25519PubKeyPrefix, 'hex'),
            Buffer.from(hex, 'hex'),
        ]);
    }
    setPrivateKey(privateKey) {
        this.privateKeyString = privateKey;
        this.privateKey = crypto.createPrivateKey('-----BEGIN PRIVATE KEY-----\n' + this.toDER(privateKey).toString('base64') + '\n-----END PRIVATE KEY-----');
        const publicKey = crypto.createPublicKey(this.privateKey).export({ format: 'der', type: 'spki' }).toString('hex');
        this.publicKey = Buffer.from(publicKey.replace(this.ed25519PubKeyPrefix, ''), 'hex');
    }
    static defaultAccount() {
        return this.fromPrivateKey(this.DEFAULT_ACCOUNT);
    }
    static fromPrivateKey(privateKey) {
        const signer = new MockEddsaSigner();
        signer.setPrivateKey(privateKey);
        return signer;
    }
    static newAccount() {
        const { privateKey } = crypto.generateKeyPairSync('ed25519');
        const der = privateKey.export({ format: 'der', type: 'pkcs8' }).toString('hex');
        return this.fromPrivateKey(der.substring(32));
    }
    async getPublicKeyAsync() {
        return Promise.resolve(this.publicKey);
    }
    async sign(hash) {
        const signature = crypto.sign(null, hash, this.privateKey);
        const [r, s] = signature.toString('hex').match(/.{1,64}/g);
        return Promise.resolve({ r: `0x${r}`, s: `0x${s}` });
    }
}
exports.MockEddsaSigner = MockEddsaSigner;
MockEddsaSigner.DEFAULT_ACCOUNT = 'e45e529972ef4ef6f06961339bb6660fc9cd009f0a63c1ad100813d7f430918f';
