Truebit Verify
  • Getting Started
    • 📄What is Truebit Verify?
      • How does Truebit work?
      • System Actors
    • ⏱️Get Started
    • ⚙️Architecture
      • Truebit Verify Hub
      • Truebit Verify Nodes
      • What is a transcript?
        • Transcript example
        • API Transcript example
    • Key Concepts
      • What is verification?
      • Determinism
      • WebAssembly
      • Task instrumentation
  • Developing Truebit Tasks
    • Introduction
    • Writing Function Tasks
      • Supported languages
        • Javascript
        • Python
        • Rust
    • Writing API Tasks
    • Task examples
      • Function Task examples
        • Fibonacci
        • Reverse Alphabet
        • Encrypt sha256 using bun.js
        • Encrypt sha256 using Deno
        • ECDSA signature verification
        • NumPy example
      • API Task examples
        • Pet Store CRUD
        • API-Auth examples
    • Namespace management
    • Task Authorization
    • Truebit CLI
      • secret.json
  • Executing Truebit tasks
    • Introduction
    • Task Execution API
      • Execute Function Tasks
      • Execute API Tasks
      • Get Task Status
    • Transcript API
      • Get Transcript
      • Get Invoice
  • Community
    • Truebit Unchained Protocol
    • Support
Powered by GitBook
On this page
  • Limitations
  • Task Preparation
  • Setup rollup
  • Rollup config file
  • Built-in Libraries
  • Crypto Libraries

Was this helpful?

  1. Developing Truebit Tasks
  2. Writing Function Tasks
  3. Supported languages

Javascript

PreviousSupported languagesNextPython

Last updated 2 months ago

Was this helpful?

Limitations

  • The "os" module (files, setTimeout, etc.) support is not available yet.

  • We support ES6 modules but use rollup to bundle dependencies in a single file.

  • The use of sub-directories is not available yet.

  • Node libraries are not available (alternative libraries with no Node dependencies should be used instead).

  • Currently, only calling synchronous functions is permitted within the context of the .

Task Preparation

To execute Function tasks, you may need to perform additional steps before creating them. Please refer to the following instructions to create a Function task in Javascript.

Setup rollup

We need to roll up your JS code with all required dependencies into a single file to be able to execute it. Please follow the following steps to merge all the files into a single one.

  1. Install the Rollup Tool with the following command

npm i rollup
npm i
  1. Once the task is developed and working correctly, use the following command to bundle dependencies in a single file. After running this command you will get the following file dist/main.js

npx rollup -c
  1. The rollup command generates a single JS file that includes your code and all its dependencies. Now you can test your task in Truebit (run the 'Build' and 'Start' commands in the command-line interface)

If you encounter dependency issues, please ensure you are not relying on native Node.js libraries. If you are, try to find an alternative library to resolve the problem.

Rollup config file

The rollup.config.mjs file is where you provide instructions to Rollup on how to bundle your JavaScript code, making it a fundamental part of the Rollup setup.

Rollup config example
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default [
  {
    input: 'src/main.js',
    output: {
      file: 'dist/main.js',
      format: 'es',
      name: 'fibonacci',
    },
    plugins: [nodeResolve(), commonjs()],
  },
];

Variables

  • Input: Set up the file that Rollup will use as the starting point for bundling everything together

  • Output: Define how the output file will be generated.

    • file: Output rollup file

    • name: This is the name of the bundle

  • plugins: Set up the Rollup plugins that perform additional tasks during bundling. This could include things like converting code, making it smaller, or dealing with outside modules.

  • Optional sections: You can add additional sections to configure specific aspects of your project, such as module resolution or custom settings.

Built-in Libraries

Crypto Libraries

Truebit provides built-in Cryptographic library functions within our interpreter to make it easier to use and to boost execution speeds. At the moment, we support Bun.js and Deno.

Comparison

Library
API
Performance
Adoption

Older. It's Node.js Propietary

Slower. It's implemented in JS

Widely adopted. The API is easy to understand for the large community of Node.js developers.

Newer. WebCrypto is a web standard

Faster. It's implemented in Rust, then compiled as WebAssembly code.

Limited adopted. The API is familiar to web developers and those using Deno (the next generation of Node.js).

Performance

When running the examples below, Deno's compiled WebAssembly performs much better than Bun.js, surpassing it by a huge margin.

Bun.js

To use Bun.js, import the library into the function task using the following ES6 module syntax

import crypto from 'crypto';

Example using Bun.js (Node.js crypto API):

import crypto from 'crypto';

function main() {
    const message = 'truebit'.repeat(5000);
    
    let i = 100;
    while (i--) {
    	const hash = crypto.createHash('sha256');
    	const result = hash.update(message).digest('hex');
        console.log(result);
    }
}

main();

Deno

To use Deno, import the library into the function task using the following ES6 module syntax

import crypto from 'webcrypto';

Example using Deno (WebCrypto API):

import crypto from 'webcrypto';
import TextEncoder from 'encoding';

function buf2hex(buffer) {
return [...new Uint8Array(buffer)]
    .map(x => x.toString(16).padStart(2, '0'))
    .join('');
}

async function main() {
    const message = 'truebit'.repeat(5000);
    const encoder = new TextEncoder('ascii');
    const data = encoder.encode(message).buffer;
    let i = 100;
    while (i--) {
        const hash = await crypto.subtle.digest('sha-256', data);
        const result = buf2hex(hash);
        console.log(result);
    }
}
main();

As you continue your development, you might need to update multiple times your to keep testing

For more information, visit the

format: Specifies the format of the generated bundle.

Click to check the node.js crypto API documentation

Click to check the WebCrypto API documentation

fs module
rollup official website
Available format
Here
Here
rollup config file
Bun.js
Deno