Skip to main content

Resources Contract Technical Reference

The Resources contract allows users to manage encrypted data resources on the blockchain. Users can add, update, and remove these resources, control access permissions, and keep track of allowed readers for each resource.

Details:

Inheritance

The Resources contract extends the Recoverable contract.

Structs

Resource: Represents an encrypted data resource with the following fields: id, owner, encryptedData, and encryptedSharedKey.

State Variables

  • resources: Mapping of resource id to the encrypted data.
  • resourceCounter: Total count of resources.
  • resourceAutoIncrement: An auto-increment value for resources.
  • marketplace: Address of the marketplace contract.
  • sharedKeys: Mapping of resource id to mapping of client address to encrypted shared keys.
  • userResourcesIds: Mapping of user address to their resource ids.
  • userResourceIndexes: Mapping from resource id to the user's resource index.
  • resourceOwner: Mapping from resource id to its owner's address.
  • readers: Mapping from resource id to its readers' addresses.
  • readersIndexes: Mapping from resource id to its readers' index.
  • delegationCount: Mapping from resource id to the count of how many times a client was added as a reader.
  • ownerKeys: Mapping of client address to encrypted JSON string containing all decryption keys.

Functions

Events


Write Functions

setMarketplace

Sets the marketplace contract address.

function setMarketplace(address _marketplace) external onlyOwner returns (bool);

Parameters:

NameType
_marketplaceaddressThe address of the marketplace contract.

Return values:

Type
boolWhether the setting was successful or not.

addResource

Adds a new resource.

function addResource(
uint marketplaceId,
string calldata encryptedData,
string calldata encryptedSharedKey,
string calldata _ownerKeys
) external onlyAuthorized nonReentrant returns (uint resourceId);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
encryptedDatastringThe encrypted data of the resource.
encryptedSharedKeystringThe encrypted shared key of the resource.
_ownerKeysstringThe encrypted owner keys of the resource.

Return values:

NameType
resourceIduintThe id of the created resource.

updateResource

Updates a resource's encrypted data.

function updateResource(
uint marketplaceId,
uint resourceId,
string calldata encryptedData
) external onlyAuthorized nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
resourceIduintThe id of the resource to update.
encryptedDatastringThe new encrypted data of the resource.

Return values:

Type
boolWhether the update was successful or not.

removeResource

Removes a resource.

function removeResource(
uint marketplaceId,
uint resourceId
) external onlyAuthorized nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
resourceIduintThe id of the resource to remove.

Return values:

Type
boolWhether the removal was successful or not.

addAllowedAddress

Adds an allowed address to access a resource.

function addAllowedAddress(
uint marketplaceId,
uint resourceId,
address user,
string calldata encryptedSharedKey
) external onlyAuthorized nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
resourceIduintThe id of the resource to add an allowed address.
useraddressThe address of the user to add.
encryptedSharedKeystringThe encrypted shared key of the resource.

Return values:

Type
boolWhether the addition was successful or not.

removeAllowedAddress

Removes an allowed address from accessing a resource.

function removeAllowedAddress(
uint marketplaceId,
uint resourceId,
address user
) external onlyAuthorized nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
resourceIduintThe id of the resource to remove an allowed address.
useraddressThe address of the user to remove.

Return values:

Type
boolWhether the removal was successful or not.

Read Functions

userOwnsResource

Checks if a user owns a particular resource.

function userOwnsResource(
address user,
uint resourceId
) public view returns (bool);

Parameters:

NameType
useraddressThe address of the user.
resourceIduintThe id of the resource to check.

Return values:

Type
boolWhether the user owns the resource or not.

getResource

Retrieves a specific resource based on id and address.

function getResource(
uint marketplaceId,
uint resourceId,
address _addr
) external view returns (Resource memory resource);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
resourceIduintThe id of the resource to retrieve.
_addraddressThe address of the user.

Return values:

NameType
resourceResourceThe resource object.

getResources

Retrieves all resources of a specific address.

function getResources(
uint marketplaceId,
address _addr
) external view returns (Resource[] memory _resources);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
_addraddressThe address of the user.

Return values:

NameType
_resourcesResource[]The array of resources.

getPaginatedResources

Retrieves a paginated list of resources.

function getPaginatedResources(
uint marketplaceId,
address _addr,
uint _start,
uint _count
) external view returns (Resource[] memory _resources, uint _totalResources);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
_addraddressThe address of the user.
_startuintThe start index of the resources to retrieve.
_countuintThe number of resources to retrieve.

Return values:

NameType
_resourcesResource[]The array of resources.
_totalResourcesuintThe total number of resources.

getOwnerKeys

Retrieves encrypted owner keys for a given address.

function getOwnerKeys(
address _addr
) public view returns (string memory _ownerKeys);

Parameters:

NameType
_addraddressThe address of the user.

Return values:

NameType
_ownerKeysstringThe encrypted owner keys.

Events

AddedResource

Emitted when a new resource is added.

event AddedResource(uint _id);

Parameters:

NameType
_iduintThe id of the added resource.

RemovedResource

Emitted when a resource is removed.

event RemovedResource(uint _id);

Parameters:

NameType
_iduintThe id of the removed resource.

UpdatedResource

Emitted when a resource is updated.

event UpdatedResource(uint _id);

Parameters:

NameType
_iduintThe id of the updated resource.

UpdatedOwnerKeys

Emitted when the owner keys are updated.

event UpdatedOwnerKeys(address _addr);

Parameters:

NameType
_addraddressThe address of the user.

AddedAllowedAddress

Emitted when an address is allowed access to a resource.

event AddedAllowedAddress(uint _id, address _addr);

Parameters:

NameType
_iduintThe id of the resource.
_addraddressThe address of the user.

RemovedAllowedAddress

Emitted when an address is removed from accessing a resource.

event RemovedAllowedAddress(uint _id, address _addr);

Parameters:

NameType
_iduintThe id of the resource.
_addraddressThe address of the user.