{"version":3,"file":"IssueNodeNoC.cjs","sources":["../../../../src/methods/ESPRMFabric/IssueNodeNoC.ts"],"sourcesContent":["/*\n * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport {\n  ESPRMAPIManager,\n  APIEndpoints,\n  HTTPMethods,\n} from \"@espressif/rainmaker-base-sdk\";\nimport { ESPRMFabric } from \"../../ESPRMFabric\";\nimport { ESPRMIssueNodeNoCRequest } from \"../../types/io/input\";\nimport { ESPRMMatterCommissioningRequest } from \"../../ESPRMMatterCommissioningRequest\";\nimport {\n  MatterAPIOperations,\n  MatterCSRTypes,\n  MatterPostMessageTypes,\n  MatterValidationErrorCodes,\n} from \"../../utils/constants\";\nimport { ESPRMMatterBase } from \"../../ESPRMMatterBase\";\nimport { ESPRMMatterValidationError } from \"../../utils/error/ESPRMMatterValidationError\";\nimport { ESPRMFabricDetails } from \"../../types/fabric\";\n\n/**\n * Augments the ESPRMFabric class with the `issueNodeNoC` method to request Matter node certificates.\n * This module extension adds the ability to issue Node Operational Certificates (NoCs) for Matter devices\n * using Certificate Signing Requests (CSR) obtained from app-level adapters during commissioning.\n */\ndeclare module \"../../ESPRMFabric\" {\n  interface ESPRMFabric {\n    /**\n     * Issues a Matter Node Operational Certificate (NoC) for this fabric using the configured adapter.\n     * This method coordinates with the Matter commissioning adapter to:\n     * 1. Generate CSR using native secure storage (if CSR not provided)\n     * 2. Call the Matter SDK API to issue the NoC\n     * 3. Send the response data back to native platform\n     *\n     * @param request - The request containing CSR and optional metadata for the Matter node\n     * @param request.csr - Base64-encoded CSR string (if empty, will be generated via adapter)\n     * @param request.tags - Optional array of strings in key:value format for device categorization\n     * @param request.metadata - Optional key-value object for node-specific metadata\n     * @returns A promise that resolves to an ESPRMMatterCommissioningRequest instance\n     * @throws Error if adapter is not configured or CSR generation fails\n     * @throws ESPRMError if the NoC issuance fails\n     */\n    issueNodeNoC(\n      request: ESPRMIssueNodeNoCRequest\n    ): Promise<ESPRMMatterCommissioningRequest>;\n  }\n}\n\nESPRMFabric.prototype.issueNodeNoC = async function (\n  request: ESPRMIssueNodeNoCRequest\n): Promise<ESPRMMatterCommissioningRequest> {\n  // Validate CSR\n  if (!request.csr || request.csr.trim() === \"\") {\n    throw new ESPRMMatterValidationError(\n      MatterValidationErrorCodes.MISSING_CSR\n    );\n  }\n\n  const requestData = createRequestData(request, this.id);\n  const requestConfig = {\n    url: APIEndpoints.USER_GROUP,\n    method: HTTPMethods.PUT,\n    data: requestData,\n    headers: {\n      \"Content-Type\": \"application/json\",\n    },\n  };\n\n  const response = await ESPRMAPIManager.authorizeRequest(requestConfig);\n\n  const adapter = ESPRMMatterBase.ESPMatterCommissioningAdaptor;\n  if (!adapter) {\n    throw new ESPRMMatterValidationError(\n      MatterValidationErrorCodes.MISSING_MATTER_COMMISSIONING_ADAPTOR\n    );\n  }\n\n  let fabricDetails = this.fabricDetails;\n  if (!fabricDetails || !fabricDetails.rootCa) {\n    fabricDetails = await this.getFabricDetails();\n  }\n  const nodeNoCResponse = createNodeNoCResponse(\n    response,\n    request,\n    fabricDetails\n  );\n  await adapter.postMessage({\n    type: MatterPostMessageTypes.ISSUE_NODE_NOC_RESPONSE,\n    data: nodeNoCResponse,\n  });\n\n  const commissioningRequest = new ESPRMMatterCommissioningRequest({\n    requestId: response.request_id,\n    groupId: this.id,\n    status: response.status,\n    description: response.description,\n    certificates: response.certificates,\n  });\n\n  return commissioningRequest;\n};\n\n/**\n * Utility function to create the request data for the issue node NoC API.\n * This function transforms the ESPRMIssueNodeNoCRequest parameters into the format expected\n * by the ESP Rainmaker API, including Matter-specific parameters and proper field mapping.\n * It handles all optional parameters and ensures they are only included when provided.\n *\n * @param request - The node NoC request parameters\n * @param request.csr - A base64-encoded Certificate Signing Request string\n * @param request.tags - Optional array of strings in key:value format for device categorization\n * @param request.metadata - Optional key-value object for node-specific metadata\n * @param groupId - The ID of the Matter fabric group to add the node to\n * @returns A record containing the API request parameters with proper field mapping\n */\nfunction createRequestData(\n  request: ESPRMIssueNodeNoCRequest,\n  groupId: string\n): Record<string, any> {\n  const requestData: Record<string, any> = {\n    operation: MatterAPIOperations.ADD,\n    csr_requests: [\n      {\n        csr: request.csr,\n        group_id: groupId,\n      },\n    ],\n    csr_type: MatterCSRTypes.NODE,\n  };\n\n  if (request.tags && request.tags.length > 0) {\n    requestData.tags = request.tags;\n  }\n\n  if (request.metadata && Object.keys(request.metadata).length > 0) {\n    requestData.metadata = request.metadata;\n  }\n\n  return requestData;\n}\n\n/**\n * Utility function to create the node NoC response data.\n * This function transforms the API response into the format expected\n * by the native platform, including Matter-specific parameters and proper field mapping.\n * It handles all optional parameters and ensures they are only included when provided.\n *\n * @param cloudResponse - The API response\n * @param request - The node NoC request parameters\n * @param fabricDetails - The fabric details\n * @returns A record containing the node NoC response data with proper field mapping\n */\nfunction createNodeNoCResponse(\n  cloudResponse: Record<string, any>,\n  request: ESPRMIssueNodeNoCRequest,\n  fabricDetails: ESPRMFabricDetails\n): Record<string, any> {\n  const certificate = cloudResponse.certificates?.[0];\n  return {\n    status: cloudResponse.status,\n    requestId: cloudResponse.request_id ?? \"\",\n    deviceId: request.deviceId ?? \"\",\n    rootCert: fabricDetails.rootCa ?? \"\",\n    operationalCert: certificate?.node_noc ?? \"\",\n    ipk: fabricDetails.ipk ?? \"\",\n    vendorId: ESPRMMatterBase.matterVendorId,\n    matterNodeId: certificate?.matter_node_id ?? \"\",\n  };\n}\n"],"names":["ESPRMFabric","ESPRMMatterValidationError","MatterValidationErrorCodes","APIEndpoints","HTTPMethods","ESPRMAPIManager","ESPRMMatterBase","MatterPostMessageTypes","ESPRMMatterCommissioningRequest","MatterAPIOperations","MatterCSRTypes"],"mappings":";;;;;;;;;AAAA;;;;AAIG;AAgDHA,uBAAW,CAAC,SAAS,CAAC,YAAY,GAAG,gBACnC,OAAiC,EAAA;;AAGjC,IAAA,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,OAAO,CAA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