

const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.js');
const require_PublicJwk = require('../../modules/kms/jwk/PublicJwk.js');
const require_KeyManagementApi = require('../../modules/kms/KeyManagementApi.js');
require('../../modules/kms/index.js');
const require_CredoWebCryptoKey = require('./CredoWebCryptoKey.js');
const require_CredoWebCryptoError = require('./CredoWebCryptoError.js');
const require_types = require('./types.js');
const require_keyAlgorithmConversion = require('./utils/keyAlgorithmConversion.js');
require('./utils/index.js');
let __noble_hashes_sha2 = require("@noble/hashes/sha2");
__noble_hashes_sha2 = require_rolldown_runtime.__toESM(__noble_hashes_sha2);
let __peculiar_asn1_schema = require("@peculiar/asn1-schema");
__peculiar_asn1_schema = require_rolldown_runtime.__toESM(__peculiar_asn1_schema);
let __noble_curves_p384 = require("@noble/curves/p384");
__noble_curves_p384 = require_rolldown_runtime.__toESM(__noble_curves_p384);
let __peculiar_asn1_x509 = require("@peculiar/asn1-x509");
__peculiar_asn1_x509 = require_rolldown_runtime.__toESM(__peculiar_asn1_x509);
let __noble_curves_p256 = require("@noble/curves/p256");
__noble_curves_p256 = require_rolldown_runtime.__toESM(__noble_curves_p256);

//#region src/crypto/webcrypto/CredoWalletWebCrypto.ts
var CredoWalletWebCrypto = class {
	constructor(agentContext) {
		this.agentContext = agentContext;
		this.kms = agentContext.resolve(require_KeyManagementApi.KeyManagementApi);
	}
	generateRandomValues(array) {
		if (!array) return array;
		return this.kms.randomBytes({ length: array.byteLength });
	}
	async sign(key, message, algorithm) {
		const jwaAlgorithm = require_types.keyParamsToJwaAlgorithm(algorithm, key);
		const keyId = key.publicJwk.keyId;
		const { signature } = await this.kms.sign({
			keyId,
			data: message,
			algorithm: jwaAlgorithm
		});
		return signature;
	}
	async verify(key, algorithm, message, signature) {
		const publicKey = key.publicJwk.publicKey;
		if (algorithm.name === "ECDSA") {
			const hashAlg = typeof algorithm.hash === "string" ? algorithm.hash : algorithm.hash.name;
			if (publicKey.kty === "EC" && publicKey.crv === "P-256" && hashAlg !== "SHA-256") {
				if (hashAlg !== "SHA-384") throw new require_CredoWebCryptoError.CredoWebCryptoError(`Hash Alg: ${hashAlg} is not supported with key type ${publicKey.crv} currently`);
				return __noble_curves_p256.p256.verify(signature, (0, __noble_hashes_sha2.sha384)(message), publicKey.publicKey);
			}
			if (publicKey.kty === "EC" && publicKey.crv === "P-384" && hashAlg !== "SHA-384") {
				if (hashAlg !== "SHA-256") throw new require_CredoWebCryptoError.CredoWebCryptoError(`Hash Alg: ${hashAlg} is not supported with key type ${publicKey.crv} currently`);
				return __noble_curves_p384.p384.verify(signature, (0, __noble_hashes_sha2.sha256)(message), publicKey.publicKey);
			}
		}
		const jwaAlgorithm = require_types.keyParamsToJwaAlgorithm(algorithm, key);
		const { verified } = await this.kms.verify({
			key: { publicJwk: key.publicJwk.toJson() },
			algorithm: jwaAlgorithm,
			signature,
			data: message
		});
		return verified;
	}
	async generate(algorithm) {
		return await this.kms.createKey({ type: require_keyAlgorithmConversion.cryptoKeyAlgorithmToCreateKeyOptions(algorithm) });
	}
	async importKey(format, keyData, algorithm, extractable, keyUsages) {
		if (format === "jwk" && keyData instanceof Uint8Array) throw new Error("JWK format is only allowed with a jwk as key data");
		if (format !== "jwk" && !(keyData instanceof Uint8Array)) throw new Error("non-jwk formats are only allowed with a uint8array as key data");
		switch (format.toLowerCase()) {
			case "jwk": return new require_CredoWebCryptoKey.CredoWebCryptoKey(require_PublicJwk.PublicJwk.fromUnknown(keyData), algorithm, extractable, "public", keyUsages);
			case "spki": return new require_CredoWebCryptoKey.CredoWebCryptoKey(require_keyAlgorithmConversion.spkiToPublicJwk(__peculiar_asn1_schema.AsnParser.parse(keyData, __peculiar_asn1_x509.SubjectPublicKeyInfo)), algorithm, extractable, "public", keyUsages);
			default: throw new Error(`Unsupported export format: ${format}`);
		}
	}
	async exportKey(format, key) {
		switch (format.toLowerCase()) {
			case "jwk": return key.publicJwk.toJson();
			case "spki": {
				const publicKeyInfo = require_keyAlgorithmConversion.publicJwkToSpki(key.publicJwk);
				const derEncoded = __peculiar_asn1_schema.AsnConvert.serialize(publicKeyInfo);
				return new Uint8Array(derEncoded);
			}
			default: throw new Error(`Unsupported export format: ${format}`);
		}
	}
};

//#endregion
exports.CredoWalletWebCrypto = CredoWalletWebCrypto;