"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.pubKeyToAddress = exports.didToAddressDid = exports.didToAddress = void 0;
var _tendermintRpc = require("@cosmjs/tendermint-rpc");
var _encoding = require("@cosmjs/encoding");
var _bs = _interopRequireDefault(require("bs58"));
var _did = require("./did");
/**
 * Convert a public key to an address.
 * @param type - The type of public key (e.g., "ed25519" or "secp256k1")
 * @param pubKey Uint8Array or base58 encoded string
 * @param prefix - The prefix to use for the address (default: "ixo")
 * @returns The bech32 encoded address
 */
var pubKeyToAddress = exports.pubKeyToAddress = function pubKeyToAddress(type, pubKey) {
  var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "ixo";
  var parsedPubKey = typeof pubKey === "string" ? _bs["default"].decode(pubKey) : pubKey;
  return (0, _encoding.toBech32)(prefix, (0, _tendermintRpc.pubkeyToRawAddress)(type, parsedPubKey));
};

/**
 * Extracts the address from a secp generate did.
 * @param did - The DID string (e.g., "did:x:<base58-encoded-key>")
 * @param prefix - The prefix to use for the address (default: "ixo")
 * @returns The bech32 encoded address
 */
var didToAddress = exports.didToAddress = function didToAddress(did) {
  var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "ixo";
  // if did is did:x:zQ3.. and the total count for : is only 3 we can safely assume it is the old did format from secp pubkey
  // can get the pubkey to get the address to get the ideal new address based did we are working towards
  if (!did.includes("did:x:zQ3")) {
    throw new Error("Invalid DID format, only support did:x:zQ3...");
  }
  var pubkey = (0, _did.extractPubkeyFromDid)(did);
  var address = pubKeyToAddress("secp256k1", pubkey, prefix);
  return address;
};

/**
 * Convert the a secp generate did into an address based did.
 * @param did - The DID string (e.g., "did:x:<base58-encoded-key>")
 * @param prefix - The prefix to use for the address (default: "ixo")
 * @returns The address based did
 */
var didToAddressDid = exports.didToAddressDid = function didToAddressDid(did) {
  var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "ixo";
  var address = didToAddress(did, prefix);
  return "did:ixo".concat(address);
};