"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractPubkeyFromDid = extractPubkeyFromDid;
exports.generateSecpDid = generateSecpDid;
exports.generateSecpDidLegacy = generateSecpDidLegacy;
exports.generateWasmDid = generateWasmDid;
var _bs = _interopRequireDefault(require("bs58"));
var _conversions = require("./conversions");
var _constants = require("./constants");
/**
 * @param address bech32 encoded address, eg: ixo1r3us73z564xxxcessqmc4h0dwh8j4z6sxn2730ag09mq87hlrkzqrg8clc
 */
function generateSecpDid(address) {
  return "did:ixo:" + address;
}

// Leaving old code for reference
function generateSecpDidLegacy(pubkey, prefix) {
  var pubKeyBz = typeof pubkey === "string" ? _bs["default"].decode(pubkey) : pubkey;
  var did = (0, _conversions.encodeMbKey)(_constants.MULTICODEC_SECP256K1_PUB_HEADER, pubKeyBz);
  return "did:" + (prefix || "x") + ":" + did;
}

/**
 * Extracts the public key from a DID.
 * @param did - The DID string (e.g., "did:x:<base58-encoded-key>")
 * @returns The base58 encoded public key
 */
function extractPubkeyFromDid(did) {
  // Assuming the DID is formatted as "did:<prefix>:<base58-encoded-key>"
  var didParts = did.split(":");
  if (didParts.length < 3) {
    throw new Error("Invalid DID format");
  }
  var base58Key = didParts[2];

  // Decode the base58 key
  var decodedKey = (0, _conversions.decodeMbKey)(_constants.MULTICODEC_SECP256K1_PUB_HEADER, base58Key);

  // Convert the raw public key back to a base58 string
  return _bs["default"].encode(decodedKey);
}

/**
 * Return a did with ixo method did and wasm namespace, for the purpose
 * to have iid docs onchain for corresponding cosmwasm addresses
 *
 * @param address bech32 encoded address, eg: ixo1r3us73z564xxxcessqmc4h0dwh8j4z6sxn2730ag09mq87hlrkzqrg8clc
 */
function generateWasmDid(address) {
  return "did:ixo:wasm:".concat(address);
}