/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { DerCodec, DerError, DerRawUint } from "#codec/DerCodec.js";
import { Bytes } from "#util/Bytes.js";
import { SignatureEncodingError } from "./CryptoError.js";
class EcdsaSignature {
  #bytes;
  constructor(bytes, encoding = "ieee-p1363") {
    if (encoding === "der") {
      try {
        const decoded = DerCodec.decode(bytes);
        const r = DerCodec.decodeBigUint(decoded?._elements?.[0], 32);
        const s = DerCodec.decodeBigUint(decoded?._elements?.[1], 32);
        this.#bytes = Bytes.concat(r, s);
      } catch (cause) {
        DerError.accept(cause);
        throw new SignatureEncodingError("Could not decode DER signature", { cause });
      }
    } else {
      if (bytes.byteLength !== 64) {
        throw new SignatureEncodingError("Invalid IEEE P1364 signature length");
      }
      this.#bytes = bytes;
    }
  }
  /**
   * Access signature in IEEE P1363 format.
   */
  get bytes() {
    return this.#bytes;
  }
  /**
   * Access signature in DER format.
   */
  get der() {
    const bytes = Bytes.of(this.#bytes);
    const bytesPerComponent = bytes.length / 2;
    return DerCodec.encode({
      r: DerRawUint(bytes.slice(0, bytesPerComponent)),
      s: DerRawUint(bytes.slice(bytesPerComponent))
    });
  }
}
((EcdsaSignature2) => {
  EcdsaSignature2.IEEE_P1363 = "ieee-p1363";
  EcdsaSignature2.DER = "der";
})(EcdsaSignature || (EcdsaSignature = {}));
export {
  EcdsaSignature
};
//# sourceMappingURL=EcdsaSignature.js.map
