# PrivateStateProvider

> For the complete documentation index, see [llms.txt](https://docs.midnight.network/llms.txt)

[**Midnight.js API Reference v4.1.0**](https://docs.midnight.network/api-reference/midnight-js.md)

***

[Midnight.js API Reference](https://docs.midnight.network/api-reference/midnight-js/packages.md) / [@midnight-ntwrk/midnight-js-types](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types.md) / PrivateStateProvider

# Interface: PrivateStateProvider\<PSI, PS>

Interface for a typed key-valued store containing contract private states.

## Type Parameters[​](#type-parameters "Direct link to Type Parameters")

### PSI[​](#psi "Direct link to PSI")

`PSI` *extends* [`PrivateStateId`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/type-aliases/PrivateStateId.md) = [`PrivateStateId`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/type-aliases/PrivateStateId.md)

Parameter indicating the private state ID, sometimes a union of string literals.

### PS[​](#ps "Direct link to PS")

`PS` = `any`

Parameter indicating the private state type stored, sometimes a union of private state types.

## Methods[​](#methods "Direct link to Methods")

### clear()[​](#clear "Direct link to clear()")

> **clear**(): `Promise`<`void`>

Remove all contract private states.

#### Returns[​](#returns "Direct link to Returns")

`Promise`<`void`>

***

### clearSigningKeys()[​](#clearsigningkeys "Direct link to clearSigningKeys()")

> **clearSigningKeys**(): `Promise`<`void`>

Remove all contract signing keys.

#### Returns[​](#returns-1 "Direct link to Returns")

`Promise`<`void`>

***

### exportPrivateStates()[​](#exportprivatestates "Direct link to exportPrivateStates()")

> **exportPrivateStates**(`options?`): `Promise`<[`PrivateStateExport`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/PrivateStateExport.md)>

Export all private states as an encrypted JSON-serializable structure.

NOTE: This does NOT export signing keys for security reasons.

#### Parameters[​](#parameters "Direct link to Parameters")

##### options?[​](#options "Direct link to options?")

[`ExportPrivateStatesOptions`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/ExportPrivateStatesOptions.md)

Export options including optional custom password and state limit.

#### Returns[​](#returns-2 "Direct link to Returns")

`Promise`<[`PrivateStateExport`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/PrivateStateExport.md)>

A JSON-serializable export structure that can be saved or transmitted.

#### Throws[​](#throws "Direct link to Throws")

If no states exist to export, the state limit is exceeded, or a caller-supplied `options.password` does not satisfy the minimum-length policy.

#### Throws[​](#throws-1 "Direct link to Throws")

If implementations require a scoped operating context (for example, an account or contract address) and that context is not set.

#### Throws[​](#throws-2 "Direct link to Throws")

If the password returned by the configured password provider does not satisfy the minimum strength policy (validation runs on every invocation).

#### Throws[​](#throws-3 "Direct link to Throws")

If reading existing entries fails for any of the reasons listed on [PrivateStateProvider.get](#get) (decryption failure, rotation lock timeout, store I/O).

***

### exportSigningKeys()[​](#exportsigningkeys "Direct link to exportSigningKeys()")

> **exportSigningKeys**(`options?`): `Promise`<[`SigningKeyExport`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/SigningKeyExport.md)>

Export all signing keys as an encrypted JSON-serializable structure.

#### Parameters[​](#parameters-1 "Direct link to Parameters")

##### options?[​](#options-1 "Direct link to options?")

[`ExportSigningKeysOptions`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/ExportSigningKeysOptions.md)

Export options including optional custom password and key limit.

#### Returns[​](#returns-3 "Direct link to Returns")

`Promise`<[`SigningKeyExport`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/SigningKeyExport.md)>

A JSON-serializable export structure that can be saved or transmitted.

#### Throws[​](#throws-4 "Direct link to Throws")

If no keys exist to export, the key limit is exceeded, or a caller-supplied `options.password` does not satisfy the minimum-length policy.

#### Throws[​](#throws-5 "Direct link to Throws")

If the password returned by the configured password provider does not satisfy the minimum strength policy (validation runs on every invocation).

#### Throws[​](#throws-6 "Direct link to Throws")

If reading existing entries fails for any of the reasons listed on [PrivateStateProvider.getSigningKey](#getsigningkey).

***

### get()[​](#get "Direct link to get()")

> **get**(`privateStateId`): `Promise`<`PS` | `null`>

Retrieve the private state at the given private state ID.

#### Parameters[​](#parameters-2 "Direct link to Parameters")

##### privateStateId[​](#privatestateid "Direct link to privateStateId")

`PSI`

The private state identifier.

#### Returns[​](#returns-4 "Direct link to Returns")

`Promise`<`PS` | `null`>

The stored private state, or `null` if either:

* the key is absent from the underlying store, or
* the stored value deserializes to `undefined`.

Callers should treat both `null` outcomes equivalently as "no usable value". The provider does not distinguish between an absent key and an explicitly-undefined stored value; if the distinction matters for your application, store a sentinel value instead.

#### Throws[​](#throws-7 "Direct link to Throws")

If `setContractAddress` has not been called prior to invocation.

#### Throws[​](#throws-8 "Direct link to Throws")

If the password returned by the configured password provider does not satisfy the minimum strength policy. Validation runs on every invocation against the password returned by the provider. It is not cached.

#### Throws[​](#throws-9 "Direct link to Throws")

If decryption of the stored value fails (wrong password, salt mismatch, unsupported encryption version, or authentication tag mismatch). Decryption errors are propagated to the caller and do **not** collapse to `null`.

#### Throws[​](#throws-10 "Direct link to Throws")

If a concurrent password rotation does not release its lock within the internal default timeout (5 minutes on the read path; the read path does not expose a configuration knob).

#### Throws[​](#throws-11 "Direct link to Throws")

Underlying store I/O errors are propagated; callers should not include them in user-facing messages without redacting paths and OS-level metadata.

#### Remarks[​](#remarks "Direct link to Remarks")

Implementations may lazily migrate legacy or unencrypted entries on read, which means a successful logical read can trigger a write to the underlying store. In read-only environments (mounted-read-only file systems, quota-exhausted backends), `get` may reject with an I/O error even when the value is present and decryptable. The list of decryption failure modes above is illustrative, not exhaustive: payload encoding/parse errors, crypto-backend availability errors, and other corruption modes are also surfaced as throws rather than `null`.

***

### getSigningKey()[​](#getsigningkey "Direct link to getSigningKey()")

> **getSigningKey**(`address`): `Promise`<`string` | `null`>

Retrieve the signing key for a contract.

#### Parameters[​](#parameters-3 "Direct link to Parameters")

##### address[​](#address "Direct link to address")

`string`

The address of the contract for which to get the signing key.

#### Returns[​](#returns-5 "Direct link to Returns")

`Promise`<`string` | `null`>

The stored signing key, or `null` if either:

* no signing key is stored for the given address, or
* the stored value deserializes to `undefined`.

Callers should treat both `null` outcomes equivalently as "no usable value".

#### Throws[​](#throws-12 "Direct link to Throws")

If the password returned by the configured password provider does not satisfy the minimum strength policy. Validation runs on every invocation against the password returned by the provider. It is not cached.

#### Throws[​](#throws-13 "Direct link to Throws")

If decryption of the stored value fails (wrong password, salt mismatch, unsupported encryption version, or authentication tag mismatch). Decryption errors are propagated to the caller and do **not** collapse to `null`.

#### Throws[​](#throws-14 "Direct link to Throws")

If a concurrent password rotation does not release its lock within the internal default timeout (5 minutes on the read path; the read path does not expose a configuration knob).

#### Throws[​](#throws-15 "Direct link to Throws")

Underlying store I/O errors are propagated; callers should not include them in user-facing messages without redacting paths and OS-level metadata.

#### Remarks[​](#remarks-1 "Direct link to Remarks")

Unlike [PrivateStateProvider.get](#get), this method does **not** require [PrivateStateProvider.setContractAddress](#setcontractaddress) to have been called first: the contract address is supplied as an argument.

Implementations may lazily migrate legacy or unencrypted entries on read, which means a successful logical read can trigger a write to the underlying store. In read-only environments, `getSigningKey` may reject with an I/O error even when the value is present and decryptable. The list of decryption failure modes above is illustrative, not exhaustive: payload encoding/parse errors, crypto-backend availability errors, and other corruption modes are also surfaced as throws rather than `null`.

***

### importPrivateStates()[​](#importprivatestates "Direct link to importPrivateStates()")

> **importPrivateStates**(`exportData`, `options?`): `Promise`<[`ImportPrivateStatesResult`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/ImportPrivateStatesResult.md)>

Import private states from a previously exported structure.

#### Parameters[​](#parameters-4 "Direct link to Parameters")

##### exportData[​](#exportdata "Direct link to exportData")

[`PrivateStateExport`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/PrivateStateExport.md)

The export data structure to import.

##### options?[​](#options-2 "Direct link to options?")

[`ImportPrivateStatesOptions`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/ImportPrivateStatesOptions.md)

Import options including password, conflict strategy, and state limit.

#### Returns[​](#returns-6 "Direct link to Returns")

`Promise`<[`ImportPrivateStatesResult`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/ImportPrivateStatesResult.md)>

Result indicating how many states were imported/skipped/overwritten.

#### Throws[​](#throws-16 "Direct link to Throws")

If decryption fails (wrong password or corrupted data).

#### Throws[​](#throws-17 "Direct link to Throws")

If the export format is invalid or unsupported.

#### Throws[​](#throws-18 "Direct link to Throws")

If conflictStrategy is 'error' and conflicts exist.

#### Throws[​](#throws-19 "Direct link to Throws")

If a caller-supplied `options.password` does not satisfy the minimum-length policy.

#### Throws[​](#throws-20 "Direct link to Throws")

If implementations require a scoped operating context (for example, an account or contract address) and that context is not set.

#### Throws[​](#throws-21 "Direct link to Throws")

If reading or writing the underlying store fails for any of the reasons listed on [PrivateStateProvider.get](#get) or [PrivateStateProvider.set](#set).

***

### importSigningKeys()[​](#importsigningkeys "Direct link to importSigningKeys()")

> **importSigningKeys**(`exportData`, `options?`): `Promise`<[`ImportSigningKeysResult`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/ImportSigningKeysResult.md)>

Import signing keys from a previously exported structure.

#### Parameters[​](#parameters-5 "Direct link to Parameters")

##### exportData[​](#exportdata-1 "Direct link to exportData")

[`SigningKeyExport`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/SigningKeyExport.md)

The export data structure to import.

##### options?[​](#options-3 "Direct link to options?")

[`ImportSigningKeysOptions`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/ImportSigningKeysOptions.md)

Import options including password, conflict strategy, and key limit.

#### Returns[​](#returns-7 "Direct link to Returns")

`Promise`<[`ImportSigningKeysResult`](https://docs.midnight.network/api-reference/midnight-js/@midnight-ntwrk/midnight-js-types/interfaces/ImportSigningKeysResult.md)>

Result indicating how many keys were imported/skipped/overwritten.

#### Throws[​](#throws-22 "Direct link to Throws")

If decryption fails (wrong password or corrupted data).

#### Throws[​](#throws-23 "Direct link to Throws")

If the export format is invalid or unsupported.

#### Throws[​](#throws-24 "Direct link to Throws")

If conflictStrategy is 'error' and conflicts exist.

#### Throws[​](#throws-25 "Direct link to Throws")

If a caller-supplied `options.password` does not satisfy the minimum-length policy.

#### Throws[​](#throws-26 "Direct link to Throws")

If reading or writing the underlying store fails for any of the reasons listed on [PrivateStateProvider.getSigningKey](#getsigningkey) or [PrivateStateProvider.setSigningKey](#setsigningkey).

***

### remove()[​](#remove "Direct link to remove()")

> **remove**(`privateStateId`): `Promise`<`void`>

Remove the value at the given private state ID.

#### Parameters[​](#parameters-6 "Direct link to Parameters")

##### privateStateId[​](#privatestateid-1 "Direct link to privateStateId")

`PSI`

The private state identifier.

#### Returns[​](#returns-8 "Direct link to Returns")

`Promise`<`void`>

***

### removeSigningKey()[​](#removesigningkey "Direct link to removeSigningKey()")

> **removeSigningKey**(`address`): `Promise`<`void`>

Remove the signing key for a contract.

#### Parameters[​](#parameters-7 "Direct link to Parameters")

##### address[​](#address-1 "Direct link to address")

`string`

The address of the contract for which to delete the signing key.

#### Returns[​](#returns-9 "Direct link to Returns")

`Promise`<`void`>

***

### set()[​](#set "Direct link to set()")

> **set**(`privateStateId`, `state`): `Promise`<`void`>

Store the given private state at the given private state ID.

#### Parameters[​](#parameters-8 "Direct link to Parameters")

##### privateStateId[​](#privatestateid-2 "Direct link to privateStateId")

`PSI`

The private state identifier.

##### state[​](#state "Direct link to state")

`PS`

The private state to store.

#### Returns[​](#returns-10 "Direct link to Returns")

`Promise`<`void`>

***

### setContractAddress()[​](#setcontractaddress "Direct link to setContractAddress()")

> **setContractAddress**(`address`): `void`

Set the contract address for scoping private state operations. Must be called before any get/set/remove operations on private states. This provides namespace isolation between different contracts.

#### Parameters[​](#parameters-9 "Direct link to Parameters")

##### address[​](#address-2 "Direct link to address")

`string`

The contract address to scope operations to.

#### Returns[​](#returns-11 "Direct link to Returns")

`void`

***

### setSigningKey()[​](#setsigningkey "Direct link to setSigningKey()")

> **setSigningKey**(`address`, `signingKey`): `Promise`<`void`>

Store the given signing key at the given address.

#### Parameters[​](#parameters-10 "Direct link to Parameters")

##### address[​](#address-3 "Direct link to address")

`string`

The address of the contract having the given signing key.

##### signingKey[​](#signingkey "Direct link to signingKey")

`string`

The signing key to store.

#### Returns[​](#returns-12 "Direct link to Returns")

`Promise`<`void`>
