SDK Reference

Initialize SDK

Wallet Functions

Storage Functions

FIle Operation Functions

Blobber Functions

Smart Contract Functions

init

Initialize the SDK.

Note: Call setWallet method after initializing the SDK. This is mandatory before calling other methods in the SDK.

import {
  init
} from "@zerochain/zus-sdk";

const configJson = {
  chainId: "0afc093ffb509f059c55478bc1a60351cef7b4e9c008a53a6cc8241ca8617dfe",
  signatureScheme: "bls0chain",
  minConfirmation: 50,
  minSubmit: 50,
  confirmationChainLength: 3,
  blockWorker: "https://dev.zus.network/dns",
  zboxHost: "https://0box.dev.zus.network",
  zboxAppType: "vult",
};

const config = [
  configJson.chainId,
  configJson.blockWorker,
  configJson.signatureScheme,
  configJson.minConfirmation,
  configJson.minSubmit,
  configJson.confirmationChainLength,
  configJson.zboxHost,
  configJson.zboxAppType,
];

await init(config);

setWallet

Set the wallet details

Note: Call setWallet method after initializing the SDK. This is mandatory before calling other methods in the SDK.

import {
  setWallet,
} from "@zerochain/zus-sdk";

await setWallet(clientID, privateKey, publicKey, mnemonic);

getBalance

Get wallet balance

import {
  getBalance,
} from "@zerochain/zus-sdk";

const wallet = await getBalance(clientID);

sendTransaction

Send the token from one wallet to another

import {
  sendTransaction,
} from "@zerochain/zus-sdk";

const fromWallet = {
      id: clientID,
      public_key: publicKey,
      secretKey: privateKey,
};

await sendTransaction(fromWallet, sendTo, parseInt(sendAmount), "");

getBalanceWasm

Get wallet balance using wasm

import {
  getBalanceWasm,
} from "@zerochain/zus-sdk";

const wallet = await getBalanceWasm(clientID);

listAllocations

List all the allocations

import {
  listAllocations,
} from "@zerochain/zus-sdk";

const allocations = await listAllocations();

createAllocation

Create an allocation

import {
  createAllocation,
} from "@zerochain/zus-sdk";

const config = {
    name: "newalloc",
    datashards: 2,
    parityshards: 2,
    size: 2 * 1073741824,
    expiry: Math.floor(expiry.getTime() / 1000),
    minReadPrice: 0,
    maxReadPrice: 184467440737095516,
    minWritePrice: 0,
    maxWritePrice: 184467440737095516,
    lock: 5000000000,
};

await createAllocation(config);

createAllocationWithBlobbers

Create an allocation with the given preferred blobbers

import {
  createAllocationWithBlobbers,
} from "@zerochain/zus-sdk";

const config = {
    datashards: 2,
    parityshards: 2,
    size: 2 * 1073741824,
    expiry: Math.floor(expiry.getTime() / 1000),
    minReadPrice: 0,
    maxReadPrice: 184467440737095516,
    minWritePrice: 0,
    maxWritePrice: 184467440737095516,
    lock: 5000000000,
    blobbers: preferredBlobbers,
};

await createAllocationWithBlobbers(config);

getAllocation

Get allocation details

import {
  getAllocation,
} from "@zerochain/zus-sdk";

const allocation = await getAllocation(selectedAllocation);

getAllocationFromAuthTicket

Get allocation details based on authTicket

import {
  getAllocationFromAuthTicket,
} from "@zerochain/zus-sdk";

const allocation = await getAllocationFromAuthTicket(authTicket);

reloadAllocation

Clear the cache and get the allocation details from blockchain

import {
  reloadAllocation,
} from "@zerochain/zus-sdk";

const allocation = await reloadAllocation(selectedAllocation);

freezeAllocation

Freeze the allocation so that the allocation data can no longer be modified

import {
  freezeAllocation,
} from "@zerochain/zus-sdk";

await freezeAllocation(selectedAllocation);

cancelAllocation

Immediately return all the remaining tokens from challenge pool back to the allocation's owner and cancels the allocation. If blobbers already got some tokens, the tokens will not be returned. Remaining min lock payment to the blobber will be funded from the allocation's write pools.

import {
  cancelAllocation,
} from "@zerochain/zus-sdk";

await cancelAllocation(selectedAllocation);

updateAllocation

Update the allocation settings

import {
  updateAllocation,
} from "@zerochain/zus-sdk";

const size = undefined,
    expiry = 2628000,
    lock = 100,
    updateTerms = true,
    addBlobberId = "",
    removeBlobberId = "";

//Call updateAllocation method
await updateAllocation(
  selectedAllocation,
  size,
  expiry,
  lock,
  updateTerms,
  addBlobberId,
  removeBlobberId
);

bulkUpload

Bulk upload files with json options

import {
  bulkUpload,
} from "@zerochain/zus-sdk";

const objects = [];
for (const file of filesForUpload) {
  objects.push({
    allocationId: allocationId,
    remotePath: `/${file.name}`,
    file: file,
    thumbnailBytes: "",
    encrypt: false,
    isUpdate: false,
    isRepair: false,
    numBlocks: 100,
    callback: function (totalBytes, completedBytes, error) {
      console.log(
        file.name +
          " " +
          completedBytes +
          "/" +
          totalBytes +
          " err:" +
          error
      );
    },
  });
}

const results = await bulkUpload(objects);

download

Download your own file or a shared file.

import {
  download,
} from "@zerochain/zus-sdk";

const file = await download(
        allocationId,
        path,
        "",
        "",
        false,
        10,
        "downloadCallback"
      );

getFaucetToken

Get the test tokens for testing

import {
  getFaucetToken,
} from "@zerochain/zus-sdk";

await getFaucetToken();

executeSmartContract

Send the raw SmartContract transaction, and verify the result

import {
  executeSmartContract,
} from "@zerochain/zus-sdk";

await executeSmartContract(address, methodName, input, value);

listObjects

List the files with the allocationID and remotePath

import {
  listObjects,
} from "@zerochain/zus-sdk";

const list = await listObjects(selectedAllocation, "/");

share

Generate an authToken that provides the authorization to the holder to the specified file on the remotepath.

import {
  share,
} from "@zerochain/zus-sdk";

const authTicket = await share(allocationId, path, "", "", 0, false, 0);

showLogs

Enable the toggle to show the logs

import {
  showLogs,
} from "@zerochain/zus-sdk";

await showLogs();

hideLogs

Enable the toggle to hide the logs

import {
  hideLogs,
} from "@zerochain/zus-sdk";

await hideLogs();

deleteObject

Delete the remote file from blobbers

import {
  deleteObject,
} from "@zerochain/zus-sdk";

await deleteObject(selectedAllocation, path);

renameObject

Rename a file existing already on dStorage. Only the allocation's owner can rename a file.

import {
  renameObject,
} from "@zerochain/zus-sdk";

await renameObject(
      selectedAllocation,
      path,
      newPath
    );

copyObject

Copy the file to another folder path on blobbers

import {
  copyObject,
} from "@zerochain/zus-sdk";

await copyObject(selectedAllocation, path, destPath);

moveObject

Move the file to another remote folder path on dStorage. Only the owner of the allocation can copy an object.

import {
  moveObject,
} from "@zerochain/zus-sdk";

await moveObject(selectedAllocation, path, destPath);

play

Play and stream the video files

import { play } from "@zerochain/zus-sdk";

await play(allocationId, remotePath, authTicket, lookupHash, isLive);

stop

Stops the current play

import { stop } from "@zerochain/zus-sdk";

stop();

getNextSegment

Gets the next segment

import { getNextSegment } from "@zerochain/zus-sdk";

const buf = await getNextSegment();

createDir

Create a folder on the blobbers

import {
  createDir,
} from "@zerochain/zus-sdk";

await createDir(selectedAllocation, "/" + dirName);

getFileStats

Gets the file stats

import {
  getFileStats,
} from "@zerochain/zus-sdk";

const fileStats = await getFileStats(selectedAllocation, path);

downloadBlocks

Download the blocks of a file

import {
  downloadBlocks,
} from "@zerochain/zus-sdk";

const output = await downloadBlocks(
      selectedAllocation,
      path,
      "",
      "",
      10,
      0,
      10
    );

getUSDRate

Get the USD rate by token symbol (eg zcn, eth)

import {
  getUSDRate,
} from "@zerochain/zus-sdk";

const rate = await getUSDRate("zcn");

isWalletID

Check whether the wallet id is valid

import {
  isWalletID,
} from "@zerochain/zus-sdk";

const output = await isWalletID(clientId);

getPublicEncryptionKey

Get the public encryption key by mnemonic

import {
  getPublicEncryptionKey,
} from "@zerochain/zus-sdk";

const key = await getPublicEncryptionKey(mnemonic);

getLookupHash

Get the lookup hash by allocation id and path

import {
  getLookupHash,
} from "@zerochain/zus-sdk";

const hash = await getLookupHash(selectedAllocation, path);

getAllocationBlobbers

Get blobbers with filters for creating the allocation

import {
  getAllocationBlobbers,
} from "@zerochain/zus-sdk";

const referredBlobberURLs = [
      "https://dev2.zus.network/blobber02",
      "https://dev1.zus.network/blobber02",
    ],
    dataShards = 2,
    parityShards = 2,
    size = 2 * 1073741824,
    expiry = Math.floor(expiryDate.getTime() / 1000),
    minReadPrice = 0,
    maxReadPrice = 184467440737095516,
    minWritePrice = 0,
    maxWritePrice = 184467440737095516;

const blobberList = await getAllocationBlobbers(
    referredBlobberURLs,
    dataShards,
    parityShards,
    size,
    expiry,
    minReadPrice,
    maxReadPrice,
    minWritePrice,
    maxWritePrice
  );

getBlobberIds

convert blobber urls to blobber ids

import {
  getBlobberIds,
} from "@zerochain/zus-sdk";

const blobberUrls = [
      "https://dev2.zus.network/blobber02",
      "https://dev1.zus.network/blobber02",
    ];

const blobberIds = await getBlobberIds(blobberUrls);

getBlobbers

Get blobbers from the network

import {
  getBlobbers,
} from "@zerochain/zus-sdk";

result = await getBlobbers();

createReadPool

Create the readpool in storage SC if the pool is missing.

import {
  createReadPool,
} from "@zerochain/zus-sdk";

const result = await createReadPool();

getReadPoolInfo

Gets the information about the read pool for the allocation

import {
  getReadPoolInfo,
} from "@zerochain/zus-sdk";

const result = await getReadPoolInfo(clientId);

lockWritePool

Locks given number of tokes for given duration in write pool

import {
  lockWritePool,
} from "@zerochain/zus-sdk";

const result = await lockWritePool(allocationId, 1000, 10);

createWallet

Create the wallet

import {
  createWallet,
} from "@zerochain/zus-sdk";

const wallet = await createWallet();

recoverWallet

Recover the wallet using mnemonic

import {
  recoverWallet,
} from "@zerochain/zus-sdk";

const wallet = await recoverWallet(mnemonic);

decodeAuthTicket

Decodes an authTicket

import {
  decodeAuthTicket,
} from "@zerochain/zus-sdk";

const result = await decodeAuthTicket(authTicket);

initBridge

Initialize the bridge for calling other bridge methods

import {
  initBridge,
} from "@zerochain/zus-sdk";

const ethereumAddress = "0x5B9eb7E72247c45F6c4B8424FB2002151c57c54d",
      bridgeAddress = "0x2405e40161ea6da91AE0e95061e7A8462b4D5eEa",
      authorizersAddress = "0xB132C20A02AD7C38d88805F0e3fFDdfb54224C58",
      wzcnAddress = "0x10140fbca3a468A1c35F132D75659eF0EB5d95DB",
      ethereumNodeURL =
        "https://goerli.infura.io/v3/6141be73a15d47748af0dc14f53d57d7",
      gasLimit = 300000,
      value = 0,
      consensusThreshold = 75.0;

 await initBridge(
      ethereumAddress,
      bridgeAddress,
      authorizersAddress,
      wzcnAddress,
      ethereumNodeURL,
      gasLimit,
      value,
      consensusThreshold
    );

burnZCN

Bridge method to burn the ZCN

import {
  burnZCN,
} from "@zerochain/zus-sdk";

const hash = await burnZCN(amount);

mintZCN

Bridge method to mint the ZCN

import {
  mintZCN,
} from "@zerochain/zus-sdk";

const hash = await mintZCN(burnTrxHash, timeout);

getMintWZCNPayload

Bridge method to get Mint WZCN Payload

import {
  getMintWZCNPayload,
} from "@zerochain/zus-sdk";

const result = await getMintWZCNPayload(burnTrxHash);

Last updated