"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getIssuerAddress = exports.getBatchMerkleRoot = void 0;

var _openAttestation = require("@govtechsg/open-attestation");

const getBatchMerkleRoot = document => {
  var _document$signature;

  return `0x${((_document$signature = document.signature) === null || _document$signature === void 0 ? void 0 : _document$signature.merkleRoot) || ""}`;
};

exports.getBatchMerkleRoot = getBatchMerkleRoot;

const isWrappedV3Document = document => {
  return document && document.version === "open-attestation/3.0";
};

const isWrappedV2Document = document => {
  return !isWrappedV3Document(document);
};

const getIssuerAddress = document => {
  if (isWrappedV2Document(document)) {
    const data = (0, _openAttestation.getData)(document);
    if (!(data.issuers.length === 1)) throw new Error("Token must have exactly one token registry contract");
    if (!data.issuers[0].tokenRegistry) throw new Error("Token must have token registry in it");
    return data.issuers[0].tokenRegistry;
  }

  const data = (0, _openAttestation.getData)(document);
  return data.proof.value;
};

exports.getIssuerAddress = getIssuerAddress;