Skip to content

JavaScript/TypeScript bindings for "candid_parser", compiled to WebAssembly

Notifications You must be signed in to change notification settings

justblender/candid-parser-wasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

candid-parser-wasm

This package provides bindings to (and helper functions for) the candid_parser crate.

version downloads

Features

  • Read all actor functions along with their signature (mode, argument and return types);
  • Encode or decode the arguments of a (shared) actor function into Uint8Array or string;
  • Serialize Candid into a JSON string.

Installation

npm install candid-parser-wasm

Usage

Important

Before you can use this library in your Vite app, it is recommended that you first install vite-plugin-wasm.

import { parseCandid } from "candid-parser-wasm";

// TIP: You can use `fetchCandid` from `@dfinity/agent`
// to fetch Candid source for a given canister ID on-demand.

const parser = parseCandid(`
  service : {
    hello : (text) -> (text) query;
  };
`);

const encoded: Uint8Array = parser.encodeIdlArgs("hello", '("world")');
//    ^ `Uint8Array` with encoded bytes

const decoded: string = parser.decodeIdlArgs("hello", encoded);
//    ^ ("world")

const functions: Record<string, any> = parser.getFunctions();
//    ^ { hello: {modes: [{ kind: "query" }], args: ["text"], rets: ["text"]} }

const json: string = parser.toJSON();
//    ^ fully serialized IDL in a JSON string

Contributing

Contributions are welcome. Please submit your pull requests or open issues to propose changes or report bugs.