Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@scale-codec/definition-compiler

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@scale-codec/definition-compiler

SCALE-codec types namespace definition compiler

  • 4.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@scale-codec/definition-compiler build status version license

Code generation for SCALE namespaces.

Installation

Available on NPM:

npm i @scale-codec/definition-compiler

Features

  • Support all codecs provided by @scale-codec/core
  • Support type aliases
  • Support circular references between types
  • Support type imports from other places
  • Each compiled type is opaque (except aliases)
  • Defaults are configurable, including "std" types and the runtime library itself

Example

Having the following namespace in Rust:

struct Person {
    name: String,
    age: u8,
    document: PersonDocument
}

enum PersonDocument {
    Id(u8),
    Passport(Passport)
}

struct Passport(u32, u32);

type PersonsMap = HashMap<u8, Person>;

type PersonsVec = Vec<Person>;

struct PublicKey {
    // Fixed-len array
    payload: [u8; 32]
}

You can define a schema for it and compile it into TypeScript module:

import { NamespaceDefinition, renderNamespaceDefinition } from '@scale-codec/definition-compiler'

const schema: NamespaceDefinition = {
  Person: {
    t: 'struct',
    fields: [
      {
        name: 'name',
        ref: 'Str',
      },
      {
        name: 'age',
        ref: 'U8',
      },
      {
        name: 'document',
        ref: 'PersonDocument',
      },
    ],
  },
  PersonDocument: {
    t: 'enum',
    variants: [
      {
        name: 'Id',
        discriminant: 0,
        ref: 'U8',
      },
      {
        name: 'Passport',
        discriminant: 1,
        ref: 'Passport',
      },
    ],
  },
  Passport: {
    t: 'tuple',
    items: ['U32', 'U32'],
  },
  PersonsMap: {
    t: 'map',
    key: 'U8',
    value: 'Person',
  },
  PersonsVec: {
    t: 'vec',
    item: 'Person',
  },
  PublicKey: {
    t: 'struct',
    fields: [
      {
        name: 'payload',
        ref: 'Array_u8_32',
      },
    ],
  },
  Array_u8_32: {
    t: 'array',
    item: 'U8',
    len: 32,
  },
}

const code = renderNamespaceDefinition(schema)

code could be written into a module and then used as:

import { Person, PersonDocument } from './compiled-module'

function printPersonName(person: Person) {
    console.log(person.name)
}

const person = Person({
  name: 'James',
  age: 52,
  document: PersonDocument('Id', 911),
})

const bytes: Uint8Array = Person.toBuffer(person)

Note: all compiled codecs are also factories to define values. Values themselves are defined as opaque types, i.e. they could be defined either through its factory (Person({...})), or through explicit type casting ({...} as Person).

API

@scale-codec/definition-compiler API

Keywords

FAQs

Package last updated on 28 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc