import { isHexString, hexZeroPad } from "@ethersproject/bytes";
import { BigNumber } from "@ethersproject/bignumber";
export async function prepareDataSignatureParameters(args) {
    const domainType = [
        { name: "name", type: "string" },
        { name: "version", type: "string" },
        { name: "verifyingContract", type: "address" },
        { name: "salt", type: "bytes32" }
    ];
    const domainData = {
        name: "Boson Protocol",
        version: "V2",
        verifyingContract: args.verifyingContractAddress,
        salt: hexZeroPad(BigNumber.from(args.chainId).toHexString(), 32),
        ...args.customDomainData
    };
    Object.keys(domainData).forEach((key) => {
        if (domainData[key] === undefined) {
            delete domainData[key];
        }
    });
    const signatureTypes = {
        EIP712Domain: domainType,
        ...args.customSignatureType
    };
    const structuredDataToSign = {
        types: signatureTypes,
        domain: domainData,
        primaryType: args.primaryType,
        message: args.message
    };
    if (args.returnTypedDataToSign) {
        return structuredDataToSign; // Return the raw EIP-712 data
    }
    const dataToSign = JSON.stringify(structuredDataToSign);
    const signerAddress = await args.web3Lib.getSignerAddress();
    const signature = await args.web3Lib.send("eth_signTypedData_v4", [
        signerAddress,
        dataToSign
    ]);
    return getSignatureParameters(signature);
}
export function getSignatureParameters(signature) {
    if (!isHexString(signature)) {
        throw new Error(`Value "${signature}" is not a valid hex string`);
    }
    const _signature = signature.substring(2);
    const r = "0x" + _signature.substring(0, 64);
    const s = "0x" + _signature.substring(64, 128);
    let v = parseInt(_signature.substring(128, 130), 16);
    if (!isNaN(v) && v < 2) {
        // support Ledger signature
        v = v + 27;
    }
    return {
        r,
        s,
        v,
        signature
    };
}
export function rebuildSignature({ r, s, v }) {
    if (!isHexString(r) || !isHexString(s)) {
        throw new Error("r and s must be valid hex strings");
    }
    // Remove '0x' prefix if present
    const rValue = r.startsWith("0x") ? r.substring(2) : r;
    const sValue = s.startsWith("0x") ? s.substring(2) : s;
    // Convert v to two digit hex, handle Ledger signature conversion
    const vValue = v.toString(16).padStart(2, "0");
    // Concatenate the signature parts
    return "0x" + rValue + sValue + vValue;
}
//# sourceMappingURL=signature.js.map