Skip to main content

Storage

addItem(itemType, item, seed)

Parameters:

  • itemType REQUIRED <string>: The type of the item being stored.

  • item REQUIRED <string>: The actual item being stored.

  • seed <string>: seed phrase for deriving a keypair.

NOTE

If the user has already passed the seed at the time of creating the SDK instance, then it is not necessary to pass it again at the time of creating the DID. .

NOTE

Must add tokens to your wallet address in order to execute write functions. See Get $AGUNG tokens for more details.

The addItem function allows developers to store key-value pairs directly on the peaq blockchain through the SDK, abstracting the complexities of interacting with the underlying Substrate-based blockchain. peaq storage supports key-value pairs defined as {itemType: item}, where itemType is the key (e.g., CID) and item is the value (e.g., hashed data).

This function supports storing small amounts of data, with a limit of 64 bytes for itemType and 256 bytes for item, which is suitable for hashed data, unique identifiers, or asset metadata. Storing CIDs (Content Identifiers) and their corresponding hashed data on-chain ensures data integrity and provenance. Whether it's metadata for assets, machine identifiers, or cryptographic proofs, having this data on-chain enhances the trustworthiness of applications.

Code example

// Use previously stated imports as referenced in SDK Quickstart

const peaqAddStorage = async (sdk, itemType, item) => {
const result = await sdk.storage.addItem({
itemType: itemType,
item: item
});
return result;
};

// Example usage
const main = async () => {
const itemType = 'cid';
const item = 'hashed_data';

// see SDK Quickstart for createSdkInstance() function
const sdk = await createSdkInstance();
await sdk.connect();

try {
const result = await peaqAddStorage(sdk, itemType, item);
console.log(result);
}
catch(error) {
console.error(`Failed to create SDK instance: ${error}`);
throw error;
}
finally {
await sdk.disconnect();
}
};

main();

Response object

{
"message": "Successfully added the storage item type cid with item hashed_data for the address 5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12Pg",
"block_hash": "0x258218b653d80702cc3e53b3d687864576867a3c6f50b86638085b35f37d44e9",
"unsubscribe": [Function (anonymous)]
}

INFORMATION
  • message lets the user know what item type and item is stored on-chain, along with the wallet address that performed the operation.

  • block_hash is the hash of the block the transaction was executed on. For more information about that particular block you can go on our Subscan Explorer (for the proper chain) and search for the block hash.

  • unsubscribe object is a function that you can call to stop listening to further status updates for the transaction. This is useful when you want to manage resources and avoid receiving updates once you've obtained the necessary information or if the operation is completed.

getItem(itemType, address)

Parameters:

  • itemType REQUIRED <string>: The key to retrieve the corresponding value stored.

  • address <string>: The address where the item was stored. If not provided, it defaults to the initialized keypair address.

The getItem function enables developers to retrieve data stored on the peaq blockchain by fetching the value (item) associated with a given itemType key for a specific address. If no address is provided, the function defaults to the keypair's address. On the backend, it generates a hashed_key based on the itemType and address to query the blockchain for the stored value. The stored value is then returned to the user.

NOTE

If the stored value is in hexadecimal format and you wish to convert it into a readable string, use the hexToString() package from @polkadot/util. This conversion is typically needed for string data, but not necessary when storing hash values.

Code example

import { hexToString } from '@polkadot/util';

// Use previously stated imports as referenced in SDK Quickstart in addition to the one above

const peaqGetStorage = async (sdk, itemType) => {
const result = await sdk.storage.getItem({
itemType: itemType
});
return result;
};

// Example usage
const main = async () => {
const itemType = 'cid';

// see SDK Quickstart for createSdkInstance() function
const sdk = await createSdkInstance();
await sdk.connect();

try {
const result = await peaqGetStorage(sdk, itemType);
console.log(result);
console.log(hexToString(result.data));
}
catch(error) {
console.error(`Failed to create SDK instance: ${error}`);
throw error;
}
finally {
await sdk.disconnect();
}
};

main();

Response object

{
"data": "0x6861736865645f64617461"
}

updateItem(itemType, item, seed)

Parameters:

  • itemType REQUIRED <string>: The key representing the item to be updated.

  • item REQUIRED <string>: The new value to replace the previous stored data. The limit is 256 bytes.

  • seed <string>: The seed phrase for deriving a keypair if not already provided.

The updateItem function allows developers to update previously stored key-value pairs on the peaq blockchain using the SDK. It updates the value (item) associated with a given itemType key for a specific address. Developers must pass both the itemType and the new item value, ensuring it is within the 256-byte size limit. If no seed is provided, the function defaults to the keypair's address. The function then constructs an extrinsic to submit the update request to the blockchain.

Code example


// Use previously stated imports as referenced in SDK Quickstart.

const peaqUpdateStorage = async (sdk, itemType, item) => {
const result = await sdk.storage.updateItem({
itemType: itemType,
item: item
});
return result;
};

// Example usage
const main = async () => {
const itemType = 'cid';
const item = 'hashed_data2';

// see SDK Quickstart for createSdkInstance() function
const sdk = await createSdkInstance();
await sdk.connect();

try {
// can perform reads before and after to guarantee proper update
const result = await peaqUpdateStorage(sdk, itemType, item);
console.log(result);
}
catch(error) {
console.error(`Failed to create SDK instance: ${error}`);
throw error;
}
finally {
await sdk.disconnect();
}
};

main();

Response object

{
"message": "Successfully updated the storage item type cid to the new item hashed_data2 for the address 5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12Pg",
"block_hash": "0x6e757db83b752b0501e428c60b1aa8bb029ac9dc1e4ad1c51484d0602cdc99b5",
"unsubscribe": [Function (anonymous)]
}

removeItem(itemType, seed)

Parameters:

  • itemType REQUIRED <string>: The key representing the item to be removed.

  • seed <string>: The seed phrase for deriving a keypair if not already initialized.

The removeItem function allows developers to delete previously stored key-value pairs from the peaq blockchain using the SDK. It removes the value (item) associated with a given itemType key for a specific address, abstracting the complexities of interacting with the Substrate-based blockchain. Developers only need to pass the itemType to be removed, and if no seed is provided, the function uses the default keypair. The function constructs an extrinsic to submit the removal request to the blockchain and returns back a message and the block the transaction was executed at.

Code example


// Use previously stated imports as referenced in SDK Quickstart.

const peaqRemoveStorage = async (sdk, itemType) => {
const result = await sdk.storage.removeItem({
itemType: itemType
});
return result;
};

// Example usage
const main = async () => {
const itemType = 'cid';

// see SDK Quickstart for createSdkInstance() function
const sdk = await createSdkInstance();
await sdk.connect();

try {
const result = await peaqRemoveStorage(sdk, itemType);
console.log(result);
}
catch(error) {
console.error(`Failed to create SDK instance: ${error}`);
throw error;
}
finally {
await sdk.disconnect();
}
};

main();

Response object

{
"message": "Successfully removed the storage item type cid from address 5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12Pg",
"block_hash": "0xa7034411caee90ceea19d67a480d829a6ae44aca254508e7292c939a34e8dbd7",
"unsubscribe": [Function (anonymous)]
}