This section describes how to run a data app in the confidential space to execute data processing tasks.
The confidential space is an isolated computing environment that focuses on the computing of sensitive personal data, and provides dedicated APIs for loading and running data apps.
A data app (DA) is an independent app running in the confidential space. It can use the data provided by an external app and the user data provided by the data management service in the confidential space to complete specific computing tasks, and then send the final computing result back to the external app. Data apps are loaded to the confidential space as dynamic libraries (.so files) through the confidential space APIs.
An app can call the runApp API for up to 20 times a day.
A data app can send a maximum of 4 bits of data during a single run, that is, an array with a length of 1. The value range of this element is [0, 15].
The running duration of a data app cannot exceed 5 seconds each time. Instances that time out will be forcibly terminated.
To help you understand the complete running process of the confidential space, this section describes the internal running principles of the confidential space.
System service processes related to the confidential space include the confidential space management service and data management service.
The confidential space management service is a system service that manages the lifecycle of the confidential space and provides APIs for the confidential space. The confidential space is not always resident. It is dynamically created by the confidential space management service when it is called and destroyed when all running data apps are terminated.
The data management service is a system service process within the confidential space. It configures the runtime environment for each data app, forwards communication content between external apps and data apps, manages and signs data sent from the confidential space to the outside, and provides services such as internal message forwarding and database.
The service process for an app to run a DA in the confidential space is as follows:

An app calls runApp to asynchronously initiate a request to run a DA.
The app calls onReceiveData to register a callback for receiving messages.
The app calls sendData to asynchronously send data.
The app calls stop to stop the DA. The confidential space service then terminates the DA process.
For details about the APIs and their usage, see API Reference.
| API Name | Description |
|---|---|
| runApp(appPath: string, argv: string[]): Promise<DataAppHandle> | Starts a data app with a given path. |
| sendData(data: Uint8Array): Promise<void> | Sends data to a data app. |
| onReceiveData(callback: Callback<Uint8Array>): void | Registers a callback function for receiving data from a data app. |
| offReceiveData(callback?: Callback<Uint8Array>): void | Unregisters the callback function for receiving data from a data app. |
| onReceiveDataError(callback: ErrorCallback<BusinessError<DataAppErrorInfo>>): void | Registers a callback function for handling errors that occur when receiving data. |
| offReceiveDataError(callback?: ErrorCallback<BusinessError<DataAppErrorInfo>>): void | Unregisters a callback function for handling errors that occur when receiving data. |
| stop(): void | Ends a data app. |
Import the confidential space module.
import { confidentialSpace } from '@kit.ConfidentialSpaceKit';
// Import the log module and text-to-binary conversion capability required by the sample code.
import { buffer } from '@kit.ArkTS';
import { hilog } from '@kit.PerformanceAnalysisKit'; Call runApp to start a data app in the confidential space.
const TAG = 'ConfidentialSpace';
let handle: confidentialSpace.DataAppHandle;
try {
// Run a data app in the confidential space.
handle = await confidentialSpace.runApp(`/data/storage/el1/bundle/libs/arm64/libdemo_da.so`, []);
} catch (e) {
hilog.error(0x0000, TAG, `Failed to run app. code=${e.code} message=${e.message}`);
// ...
} Call the onReceiveData and onReceiveDataError methods to register the callback function when data is received (or an error occurs during data receiving).
// Register the message receiving callback and error callback.
handle.onReceiveData((data: Uint8Array) => {
hilog.info(0x0000, TAG, `Received data, length=${data.length}`);
handle.stop();
handle.offReceiveData();
handle.offReceiveDataError();
// ...
});
handle.onReceiveDataError((e: BusinessError<confidentialSpace.DataAppErrorInfo>) => {
hilog.error(0x0000, TAG, `Failed to receive data. code=${e.code} message=${e.message}`);
if (e.code === 1028700014) { // An error is generated by the data app.
hilog.error(0x0000, TAG, `Error is from DA; raw code=${e.data?.dataAppErrorCode}`);
}
handle.stop();
handle.offReceiveData();
handle.offReceiveDataError();
// ...
}); Call sendData to send data to the data app.
// Define the message format as "ECHO:${msg}" and convert it into a character array.
let payload = new Uint8Array(buffer.from(`ECHO:${msg}`, 'utf-8').buffer);
// Send data.
try {
await handle.sendData(payload);
} catch (e) {
hilog.error(0x0000, TAG, `Failed to send data. code=${e.code} message=${e.message}`);
handle.stop();
// ...
}