Skip to content

Repository files navigation

Ethernity Cloud SDK JS

This project provides a set of tools and scripts to work with the Ethernity Cloud SDK in a JavaScript environment.

Table of Contents

Installation

To install the package and its dependencies, run:

npm install ethernity-cloud-sdk-js

Usage

After installation, you can use the provided scripts to build, publish, and initialize your project.

Pre-requisites

The sdk requires the following to be installed on your system:

  • Node.js v20.04 or higher
  • npm
  • docker (daemon running in the background for build and publish scripts)

Operating System compatibility

The sdk has been tested on the following operating systems:

  • MacOS
  • Ubuntu 20.04
  • Windows 10

Blockchain compatibility

  • Bloxberg:
    • Testnet - tested and working
    • Mainnet - to be provided during the following updates
  • Polyhon:
    • Amoy Testnet - to be provided during the following updates
    • Mainnet - to be provided during the following updates

Scripts

  • Initialize: To initialize the project, run:

    npm run ecld-init

    at this step, all the initial configurations will be set up and the project will be ready to be built, published and run.

  • Build: To build the project, run:

    npm run ecld-build

    the project will be built and the docker repository output will be stored in the registry/ directory. This is the stage where the backend functions are added to the secure images.

  • Publish: To publish the project, run:

    npm run ecld-publish

    Required after build, to build and integrate the secure certificates that will be used during executions and to register the project to the Ethernity Cloud Image Register.

  • Test (local, no chain): To run your backend locally with the enclave's own executor — no SGX, no gas, instant — run:

    npx ecld-test 'hello("World")'
    npx ecld-test --file payload.js
    npx ecld-test --input data.json 'processData(___etny_data_set___)'

    This validates the call (function names, arguments, result wiring) before a single on-chain request is paid for. If your dApp uses ESR (Enclave State Registry), state is emulated locally and on by default: require('../ecld_state').StateRegistry get/commit, ownership/ACL, and taskCaller() all work in-process against an in-memory registry, and persist between runs in .ecld-esr-local.*.json. The task caller defaults to your developer address; override it with --caller. npx ecld-test serve starts a local API so the runner's LOCAL mode drives your real integration end to end. Exit code 0 on SUCCESS, 1 otherwise.

  • Run (on the network): To submit a payload to the network and print the decrypted result, run:

    npx ecld-run 'hello("World")'
    npx ecld-run --file payload.js
    npx ecld-run --input data.json 'processData(___etny_data_set___)'
    npx ecld-run --json 'esrIncrement()'

    This is the network-side sibling of ecld-test: instead of executing locally it drives the runner end to end — encrypt → IPFS → on-chain request → wait for a node → download and decrypt the result. It costs gas and needs a funded key. Network and enclaves come from .env (BLOCKCHAIN_NETWORK, PROJECT_NAME, TRUSTED_ZONE_IMAGE), overridable with --network/--securelock/--trustedzone; the signing key is PRIVATE_KEY (or ECLD_PRIVATE_KEY) — a funded 0x key that pays for the order. Resources are tunable with --task-price/--cpu/--memory/--storage/--bandwidth/--duration/--validators. Exit code 0 on a SUCCESS task result, 1 otherwise.

  • Inspect (read-only): To read enclave and on-chain diagnostics — network, trustedzone/securelock registration, and ESR state — without spending gas, run:

    npx ecld-info
    npx ecld-info esr state <key>

Project Structure

.gitignore
build.js
build.sh
demo/
init.js
nodenithy/
package.json
postinstall.js
publish.js
pynithy/

Notable Directories and Files

  • build.js: Script to build the project.
  • init.js: Script to initialize the project.
  • publish.js: Script to publish the project.
  • postinstall.js: Script that runs after the package is installed.
  • nodenithy/: Contains various scripts and modules for the project.
  • pynithy/: Contains Python-related scripts and configurations.

Usage

To use the SDK:

  • after installation, run npm run ecld-init to initialize the project
  • in you workspace, you will find the scr/serverless directory, this contains a backend.js file. This file will be imported in the dApp images to provide the backend functions for calling from the frontend of your application, eg.:
function hello(msg='World') {
    return "Hello "+msg;
}

module.exports = { hello };

From your frontend application, using the ethernity cloud runner library, you will be calling the function as seen in the below example, where we pass hello("World"); to be executed on the backend which will run in the Blockchain:

const AppCss = require('./App.css');
import EthernityCloudRunner from "@ethernity-cloud/runner";
import {ECEvent, ECRunner, ECStatus} from "@ethernity-cloud/runner/enums";
import Web3 from 'web3';

const PROJECT_NAME = "";
const IPFS_ENDPOINT = "";

const code = `hello("World");`;

function App() {
    const executeTask = async () => {
        const runner = new EthernityCloudRunner();
        // this is a server provided by Ethernity CLOUD, please bear in mind that you can use your own Decentralized Storage server
        const ipfsAddress = IPFS_ENDPOINT;
        runner.initializeStorage(ipfsAddress);
        console.log(PROJECT_NAME)
        const onTaskProgress = (e) => {
            if (e.detail.status === ECStatus.ERROR) {
                console.error(e.detail.message);
            } else {
                console.log(e.detail.message);
            }
        };

        const onTaskCompleted = (e) => {
            console.log(`Task Result: ${e.detail.message.result}`);
            // display the result in page below the buttons
            const result = document.createElement("p");
            result.innerHTML = `Task Result: ${e.detail.message.result}`;
            document.body.appendChild(result);
        }

        runner.addEventListener(ECEvent.TASK_PROGRESS, onTaskProgress);
        runner.addEventListener(ECEvent.TASK_COMPLETED, onTaskCompleted);

        await runner.run(PROJECT_NAME,
                        code,
                         '',
                         { taskPrice: 10, cpu: 1, memory: 1, storage: 10, bandwidth: 1, duration: 1, validators: 1 });
    };
    const connectWallet = async () => {
      if (window.ethereum) {
          window.web3 = new Web3(window.ethereum);
          try {
              // Request account access
              await window.ethereum.request({ method: 'eth_requestAccounts' });
              console.log("Wallet connected");
          } catch (error) {
              console.error("User denied account access");
          }
      } else {
          console.log('Please install MetaMask!');
      }
  };

    return (
        <div className="container">
            <button className="centeredButton" onClick={executeTask}>Execute Task</button>
            <button className="centeredButton" onClick={connectWallet}>Connect Wallet</button>
        </div>
    );
}
export default App;
  • you are able to define the functions needed to be used in the backend, while making sure that the function that is script is compilable and that it exports the function that will be called from the frontend, in the above example, the hello function.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the AGPL-3.0 License. See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages