import { getResolver } from 'key-did-resolver';
import Resolver from '../generic/resolver.js';
import '../../utils/types/array.js';
import 'crypto';
import { encodeAsBase58 } from '../../utils/encoding/base-58.js';
import 'multiformats/bases/base58';
import 'varint';
import '../../utils/encoding/ss-58.js';
import { parseDIDUrl } from '../../utils/ident/parsers.js';
import '../../types/did/index.js';
import '../../vc/crypto/Bls12381BBDT16KeyPairDock2024.js';
import '../../vc/crypto/Bls12381BBDT16MACDock2024.js';
import '../../vc/crypto/Bls12381BBDT16MACProofDock2024.js';
import '../../vc/crypto/Bls12381BBSKeyPairDock2023.js';
import '../../vc/crypto/Bls12381BBSSignatureDock2022.js';
import '../../vc/crypto/Bls12381BBSSignatureDock2023.js';
import '../../vc/crypto/Bls12381BBSSignatureProofDock2022.js';
import '../../vc/crypto/Bls12381BBSSignatureProofDock2023.js';
import '../../vc/crypto/Bls12381BDDT16KeyPairDock2024.js';
import '../../vc/crypto/Bls12381BDDT16MACDock2024.js';
import '../../vc/crypto/Bls12381BDDT16MACProofDock2024.js';
import '../../vc/crypto/Bls12381G2KeyPairDock2022.js';
import '../../vc/crypto/Bls12381PSKeyPairDock2023.js';
import '../../vc/crypto/Bls12381PSSignatureDock2023.js';
import '../../vc/crypto/Bls12381PSSignatureProofDock2023.js';
import { Bls12381BBS23DockVerKeyName, Bls12381BBSDockVerKeyName } from '../../vc/crypto/constants.js';
import 'bs58';
import '@juanelas/base64';
import '../../keypairs/keypair-secp256k1.js';
import '../../vc/crypto/common/CustomLinkedDataSignature.js';
import '@stablelib/ed25519';
import '../../types/public-keys/public-key.js';
import '../../types/public-keys/public-key-sr25519-value.js';
import '../../types/public-keys/public-key-ed25519-value.js';
import '../../types/public-keys/public-key-secp256k1-value.js';
import '../../types/public-keys/public-key-x25519-value.js';
import '../../types/signatures/signature-value.js';
import '../../types/signatures/signature.js';
import '../../keypairs/dock-keypair.js';
import '@transmute/json-web-signature';
import { DidMethodKey } from '../../types/did/onchain/typed-did/index.js';
import '../../utils/types/string.js';
import '../generic/const.js';
import '../../utils/inheritance.js';
import '../../utils/types/object.js';
import '../../utils/types/iterable.js';
import '../../utils/types/set.js';
import '../../utils/types/bytes.js';
import '../../utils/interfaces.js';
import '../../utils/assert.js';
import '../../utils/error.js';
import 'blake2b';
import '../../utils/ident/matchers.js';
import '../../types/generic/typed-enum.js';
import '../../types/generic/with-base.js';
import '../../types/generic/with-catch-null.js';
import '../../types/generic/with-eq.js';
import '../../types/generic/any-of.js';
import '../../types/generic/with-from.js';
import '../../types/generic/with-from-json-bytes.js';
import '../../types/generic/typed-null.js';
import '../../types/generic/create-placeholder.js';
import '../../types/generic/typed-bytes.js';
import '../../types/generic/typed-string.js';
import '../../types/generic/typed-array.js';
import '../../types/generic/typed-tuple.js';
import '../../types/generic/typed-number.js';
import '../../types/generic/typed-set.js';
import '../../types/generic/typed-map.js';
import '../../types/generic/typed-struct.js';
import 'uuid';
import 'js-sha256';
import '../../types/did/offchain/index.js';
import '../../types/generic/sized.js';
import '../../utils/types/number.js';
import '../../types/did/offchain/doc-ref.js';
import '../../types/did/onchain/index.js';
import '../../types/did/onchain/constants.js';
import '../../types/did/onchain/controllers.js';
import '../../types/did/onchain/typed-did/did-method-key/did-method-key-public-key.js';
import '../../utils/encoding/base-58-btc.js';
import '../../types/generic/with-qualifier.js';
import '../../types/did/onchain/typed-did/signature.js';
import '../../types/did/onchain/typed-did/did-method-key/did-method-key-signature.js';
import '../../types/signatures/signature-ed25519-value.js';
import '../../types/signatures/signature-secp256k1-value.js';
import '../../types/signatures/signature-sr25519-value.js';
import '../../types/did/onchain/typed-did/dock-did-value.js';
import '../../types/generic/typed-uuid.js';
import '../../types/generic/utils.js';
import '../../types/generic/typed-bytes-array.js';
import '../../types/did/onchain/typed-did/cheqd-did.js';
import '../../types/did/onchain/verification-relationship.js';
import '../../types/did/onchain/did-key.js';
import '../../types/offchain-signatures/params/index.js';
import '../../types/offchain-signatures/params/value.js';
import '../../types/generic/option.js';
import '../../types/offchain-signatures/curve-type.js';
import '../../types/offchain-signatures/params/id.js';
import '../../types/generic/with-from-dock-id.js';
import '../../types/offchain-signatures/public-keys/index.js';
import '../../types/generic/with-prop.js';
import '../../types/generic/with-props.js';
import '../../types/offchain-signatures/public-keys/value.js';
import '../../types/offchain-signatures/public-keys/ref.js';
import '../../types/offchain-signatures/public-keys/id.js';
import '@docknetwork/crypto-wasm-ts';
import '../../vc/crypto/common/DockCryptoKeyPair.js';
import '@docknetwork/crypto-wasm-ts/lib/index.js';
import '../../vc/crypto/common/DockCryptoSignature.js';
import 'jsonld-signatures';
import 'json-stringify-deterministic';
import 'semver/preload.js';
import 'base64url';
import 'jsonld';
import '../../types/did/document/verification-method-ref.js';
import '../../vc/jws.js';
import '../../vc/crypto/common/DockCryptoSignatureProof.js';
import 'elliptic';
import '../../types/did/onchain/verification-method-signature.js';
import '../../keypairs/did-keypair.js';
import '../../types/did/document/verification-method-ref-or-ident-ref.js';
import '../../types/did/document/ident-ref.js';
import '../../types/did/document/index.js';
import '../../types/did/document/service-endpoint.js';
import '../../types/did/document/verification-method.js';
import '../../types/generic/with-base58.js';
import '../../types/generic/with-base58btc.js';
import '../../types/generic/with-multibase.js';
import '../../utils/encoding/multibase.js';
import '../../types/did/document/verification-method-type.js';
import '../../types/did/document/verification-method-ref-or-cheqd-verification-method.js';
import '../../types/did/document/const.js';

const resolveMethod = getResolver().key;

/**
 * Resolves `DID` keys with identifier `did:key:*`.
 */
class DIDKeyResolver extends Resolver {
  prefix = 'did';

  method = 'key';

  constructor() {
    super(void 0);
  }

  async resolve(did) {
    const parsed = parseDIDUrl(did);
    const canonicalDid = parsed.did;

    try {
      const { didDocument } = await resolveMethod(did, parsed, null, {});
      if (didDocument == null) {
        throw new Error(`Unable to resolve \`${did}\``);
      }

      return {
        '@context': 'https://www.w3.org/ns/did/v1',
        ...didDocument,
      };
    } catch (error) {
      let didMethodKey;
      try {
        didMethodKey = DidMethodKey.from(canonicalDid).didMethodKey;
      } catch {
        throw error;
      }

      if (!didMethodKey.isBbs && !didMethodKey.isBbsPlus) {
        throw error;
      }

      const verificationMethodType = didMethodKey.isBbs
        ? Bls12381BBS23DockVerKeyName
        : Bls12381BBSDockVerKeyName;
      const publicKeyMultibase = didMethodKey.toEncodedString();
      const verificationMethodId = `${canonicalDid}#${publicKeyMultibase}`;
      const publicKeyBase58 = encodeAsBase58(didMethodKey.value.bytes);

      return {
        '@context': 'https://www.w3.org/ns/did/v1',
        id: canonicalDid,
        verificationMethod: [
          {
            id: verificationMethodId,
            type: verificationMethodType,
            controller: canonicalDid,
            publicKeyBase58,
          },
        ],
        authentication: [verificationMethodId],
        assertionMethod: [verificationMethodId],
        capabilityInvocation: [verificationMethodId],
        capabilityDelegation: [verificationMethodId],
      };
    }
  }
}

export { DIDKeyResolver as default };
//# sourceMappingURL=did-key-resolver.js.map
