justblender / candid-parser-wasm

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

Home Page:https://www.npmjs.com/package/candid-parser-wasm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

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

https://www.npmjs.com/package/candid-parser-wasm


Languages

Language:Rust 100.0%