• Stars
    star
    72
  • Rank 422,910 (Top 9 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated 24 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Info about SCALE encodable Rust types

scale-info Β· build Latest Version

A library to describe Rust types, geared towards providing info about the structure of SCALE encodable types.

The definitions provide third party tools (e.g. a UI client) with information about how they are able to decode types agnostic of language.

At its core is the TypeInfo trait:

pub trait TypeInfo {
    type Identity: ?Sized + 'static;
    fn type_info() -> Type;
}

Types implementing this trait build up and return a Type struct:

pub struct Type<T: Form = MetaForm> {
    /// The unique path to the type. Can be empty for built-in types
    path: Path<T>,
    /// The generic type parameters of the type in use. Empty for non generic types
    type_params: Vec<T::Type>,
    /// The actual type definition
    type_def: TypeDef<T>,
}

Types are defined as one of the following variants:

pub enum TypeDef<T: Form = MetaForm> {
    /// A composite type (e.g. a struct or a tuple)
    Composite(TypeDefComposite<T>),
    /// A variant type (e.g. an enum)
    Variant(TypeDefVariant<T>),
    /// A sequence type with runtime known length.
    Sequence(TypeDefSequence<T>),
    /// An array type with compile-time known length.
    Array(TypeDefArray<T>),
    /// A tuple type.
    Tuple(TypeDefTuple<T>),
    /// A Rust primitive type.
    Primitive(TypeDefPrimitive),
}

Built-in Type Definitions

The following "built-in" types have predefined TypeInfo definitions:

  • Primitives: bool, char, str, u8, u16, u32, u64, u128, i8, i16, i32, i64, i128.

  • Sequence: Variable size sequence of elements of T, where T implements TypeInfo. e.g. [T], &[T], &mut [T], Vec<T>

  • Array: Fixed size [T: $n] for any T which implements TypeInfo, where $n is one of the predefined sizes.

  • Tuple: Tuples consisting of up to 10 fields with types implementing TypeInfo.

User-defined Types

There are two kinds of user-defined types: Composite and Variant.

Both make use of the Path and Field types in their definition:

Fields

A fundamental building block to represent user defined types is the Field struct which defines the Type of a field together with its optional name. Builders for the user defined types enforce the invariant that either all fields have a name (e.g. structs) or all fields are unnamed (e.g. tuples).

Path

The path of a type is a unique sequence of identifiers. Rust types typically construct a path from the namespace and the identifier e.g. foo::bar::Baz is converted to the path ["foo", "bar", "Baz"].

Composite

Composite data types are composed of a set of Fields.

Structs are represented by a set of named fields, enforced during construction:

struct Foo<T> {
    bar: T,
    data: u64,
}

impl<T> TypeInfo for Foo<T>
where
    T: TypeInfo + 'static,
{
    type Identity = Self;

    fn type_info() -> Type {
        Type::builder()
            .path(Path::new("Foo", module_path!()))
            .type_params(vec![MetaType::new::<T>()])
            .composite(Fields::named()
                .field(|f| f.ty::<T>().name("bar").type_name("T"))
                .field(|f| f.ty::<u64>().name("data").type_name("u64"))
            )
    }
}

Tuples are represented by a set of unnamed fields, enforced during construction:

struct Foo(u32, bool);

impl TypeInfo for Foo {
    type Identity = Self;

    fn type_info() -> Type {
        Type::builder()
            .path(Path::new("Foo", module_path!()))
            .composite(Fields::unnamed()
                .field(|f| f.ty::<u32>().type_name("u32"))
                .field(|f| f.ty::<bool>().type_name("bool"))
            )
    }
}

Variant

Variant types aka enums or tagged unions are composed of a set of variants. Variants can have unnamed fields, named fields or no fields at all:

enum Foo<T>{
    A(T),
    B { f: u32 },
    C,
}

impl<T> TypeInfo for Foo<T>
where
    T: TypeInfo + 'static,
{
    type Identity = Self;

    fn type_info() -> Type {
        Type::builder()
            .path(Path::new("Foo", module_path!()))
            .type_params(vec![MetaType::new::<T>()])
            .variant(
                Variants::new()
                   .variant("A", |v| v.fields(Fields::unnamed().field(|f| f.ty::<T>())))
                   .variant("B", |v| v.fields(Fields::named().field(|f| f.ty::<u32>().name("f").type_name("u32"))))
                   .variant_unit("C")
            )
    }
}

If no variants contain fields then the discriminant can be set explicitly, enforced by the builder during construction:

enum Foo {
    A,
    B,
    C = 33,
}

impl TypeInfo for Foo {
    type Identity = Self;

    fn type_info() -> Type {
        Type::builder()
            .path(Path::new("Foo", module_path!()))
            .variant(
                Variants::new()
                    .variant("A", |v| v.index(1))
                    .variant("B", |v| v.index(2))
                    .variant("C", |v| v.index(33))
            )
    }
}

The Registry

Information about types is provided within the so-called type registry (Registry). Type definitions are registered there and are associated with unique IDs that the outside can refer to, providing a lightweight way to decrease overhead instead of using type identifiers.

All concrete TypeInfo structures have two forms:

  • One meta form (MetaType) that acts as a bridge to other forms
  • A portable form suitable for serialization.

The IntoPortable trait must also be implemented in order prepare a type definition for serialization using an instance of the type registry.

After transformation all type definitions are stored in the type registry. Note that the type registry should be serialized as part of the metadata structure where the registered types are utilized to allow consumers to resolve the types.

Encoding

The type registry can be encoded as:

  • JSON (with the "serde" feature enabled).
  • SCALE itself (using parity-scale-codec).

Features

The following optional cargo features are available:

  • serde includes support for json serialization/deserialization of the type registry. See example here.
  • derive reexports the scale-info-derive crate.

Known issues

When deriving TypeInfo for a type with generic compact fields e.g.

#[derive(Encode, TypeInfo)]
struct Foo<S> { #[codec(compact)] a: S }

You may experience the following error when using this generic type without the correct bounds:

error[E0275]: overflow evaluating the requirement `_::_parity_scale_codec::Compact<_>: Decode`

See #65 for more information.

Resources

More Repositories

1

substrate

Substrate: The platform for blockchain innovators
Rust
8,212
star
2

polkadot

Polkadot Node Implementation
Rust
6,865
star
3

ink

Parity's ink! to write smart contracts.
Rust
1,316
star
4

wasmi

WebAssembly (Wasm) interpreter.
Rust
1,269
star
5

polkadot-sdk

The Parity Polkadot Blockchain SDK
Rust
979
star
6

jsonrpc

Rust JSON-RPC implementation
Rust
752
star
7

parity-bitcoin

The Parity Bitcoin client
Rust
725
star
8

cumulus

Write Parachains on Substrate
Rust
618
star
9

parity-signer

Air-gapped crypto wallet.
Rust
539
star
10

frontier

Ethereum compatibility layer for Substrate.
Rust
496
star
11

polkadot-launch

Simple CLI tool to launch a local Polkadot test network
TypeScript
458
star
12

jsonrpsee

Rust JSON-RPC library on top of async/await
Rust
457
star
13

parity-wasm

WebAssembly serialization/deserialization in rust
Rust
395
star
14

subxt

Submit extrinsics (transactions) to a substrate node via RPC
Rust
317
star
15

parity-bridge

Rust
317
star
16

smoldot

Alternative client for Substrate-based chains.
Rust
286
star
17

parity-common

Collection of crates used in Parity projects
Rust
274
star
18

substrate-telemetry

Polkadot Telemetry service
Rust
268
star
19

parity-bridges-common

Collection of Useful Bridge Building Tools πŸ—οΈ
Rust
265
star
20

banana_split

Shamir's Secret Sharing for people with friends
TypeScript
259
star
21

parity-db

Experimental blockchain database
Rust
245
star
22

parity-scale-codec

Lightweight, efficient, binary serialization and deserialization codec
Rust
238
star
23

cargo-contract

Setup and deployment tool for developing Wasm based smart contracts via ink!
Rust
226
star
24

substrate-api-sidecar

REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.
TypeScript
226
star
25

substrate-connect

Run Wasm Light Clients of any Substrate based chain directly in your browser.
TypeScript
217
star
26

trie

Base-16 Modified Patricia Merkle Tree (aka Trie)
Rust
201
star
27

substrate-archive

Blockchain Indexing Engine
Rust
197
star
28

shasper

Parity Shasper beacon chain implementation using the Substrate framework.
Rust
196
star
29

parity-zcash

Rust implementation of Zcash protocol
Rust
183
star
30

cachepot

cachepot is `sccache` with extra sec, which in turn is `ccache` with cloud storage
Rust
174
star
31

libsecp256k1

Pure Rust Implementation of secp256k1.
Rust
166
star
32

homebrew-paritytech

Homebrew tap for ethcore
Ruby
160
star
33

zombienet

A cli tool to easily spawn ephemeral Polkadot/Substrate networks and perform tests against them.
TypeScript
147
star
34

polkadot-staking-dashboard

A dashboard for Polkadot staking, nomination pools, and everything around it.
TypeScript
146
star
35

xcm-format

Polkadot Cross Consensus-system Message format.
143
star
36

finality-grandpa

finality gadget for blockchains using common prefix agreement
Rust
139
star
37

substrate-contracts-node

Minimal Substrate node configured for smart contracts via pallet-contracts.
Rust
116
star
38

capi

[WIP] A framework for crafting interactions with Substrate chains
TypeScript
105
star
39

grandpa-bridge-gadget

A Bridge Gadget to Grandpa Finality.
Rust
99
star
40

substrate-debug-kit

A collection of debug tools, scripts and libraries on top of substrate.
Rust
95
star
41

ink-examples

A set of examples for ink! smart contract language. Happy hacking!
Rust
89
star
42

wasm-utils

Rust
82
star
43

subport

Parity Substrate(-based) chains usage and development support
Rust
81
star
44

trappist

Rust
80
star
45

substrate-playground

Start hacking your substrate runtime in a web based VSCode like IDE
TypeScript
74
star
46

substrate-light-ui

Minimal, WASM-light-client-bundled Substrate wallet for balance transfers
TypeScript
72
star
47

substrate-ui

Bondy Polkadot UI
JavaScript
72
star
48

statemint

Statemint Node Implementation
Rust
72
star
49

parity-tokio-ipc

Parity tokio-ipc
Rust
71
star
50

txwrapper-core

Tools for FRAME chain builders to publish chain specific offline transaction generation libraries.
TypeScript
71
star
51

canvas

Node implementation for Canvas β€’ a Substrate parachain for smart contracts.
Rust
69
star
52

substrate-up

Scripts for working with new Substrate projects
Shell
66
star
53

useink

A React hooks library for ink!
TypeScript
62
star
54

stateless-blockchain

Stateless Blockchain on Substrate using RSA Accumulators
Rust
60
star
55

txwrapper

Helper funtions for offline transaction generation.
TypeScript
59
star
56

contracts-ui

Web application for deploying wasm smart contracts on Substrate chains that include the FRAME contracts pallet
TypeScript
58
star
57

awesome-ink

A curated list of awesome projects related to Parity's ink!.
56
star
58

srtool

A fork of chevdor's srtool
Shell
56
star
59

cargo-unleash

cargo release automatisation tooling for massiv mono-repos
Rust
55
star
60

ss58-registry

Registry for SS58 account types
Rust
54
star
61

oo7

The Bonds framework along with associated modules.
JavaScript
53
star
62

diener

Diener - dependency diener is a tool for easily changing https://github.com/paritytech/substrate or https://github.com/paritytech/polkadot dependency versions
Rust
52
star
63

lunarity

Lunarity - a Solidity parser in Rust
Rust
50
star
64

nohash-hasher

An implementation of `std :: hash :: Hasher` which does not hash at all.
Rust
49
star
65

arkworks-extensions

Library to integrate arkworks-rs/algebra into Substrate
Rust
44
star
66

wasm-instrument

Instrument and transform wasm modules.
Rust
42
star
67

fleetwood

Testbed repo for trying out ideas of what a smart contract API in Rust would look like
Rust
41
star
68

scripts

Collection of Dockerfiles, scripts and related resources
Dockerfile
41
star
69

trappist-extra

Dart
38
star
70

parity-extension

Parity Chrome Extension
JavaScript
37
star
71

rhododendron

Asynchronously safe BFT consensus, implementation in Rust
Rust
36
star
72

desub

Decode Substrate with Backwards-Compatible Metadata
Rust
36
star
73

ethkey

Ethereum key generator
Rust
35
star
74

asset-transfer-api

Typescript API aiming to provide clear, and simple to use tools for transferring assets across common good parachains.
TypeScript
35
star
75

substrate-bip39

Rust
34
star
76

primitives

Rust
34
star
77

parity-config-generator

Parity Config Generator
JavaScript
34
star
78

polkadot-introspector

A collection of tools focused on debugging and monitoring the relay chain and parachain progress from a 🐦-view
Rust
34
star
79

substrate-open-working-groups

The Susbstrate Open Working Groups (SOWG) are community-based mechanisms to develop standards, specifications, implementations, guidelines or general initiatives in regards to the Substrate framework. It could, but not restricted to, lead to new Polkadot Standards Proposals. SOWG is meant as a place to find and track ongoing efforts and enable everybody with similar interests to join and contribute.
34
star
80

Nomidot

Staking Portal for Polkadot and Kusama
TypeScript
32
star
81

subdb

Experimental domain-specific database for Substrate
Rust
32
star
82

polkadot-api

TypeScript
31
star
83

polkassembly

Polkassembly now has a new home:
TypeScript
30
star
84

pallet-contracts-waterfall

Collection of simple Substrate smart contract examples written in Rust, AssemblyScript, Solang and the smart contract language ink! to test substrates pallet-contracts module
TypeScript
30
star
85

vscode-substrate

Substrate Marketplace VSCode Plugin
TypeScript
28
star
86

oo7-bare

The Bond API.
JavaScript
27
star
87

metadata-portal

Metadata portal for Parity Signer
TypeScript
27
star
88

bigint

Rust
27
star
89

sub-flood

Flooding substrate node with transactions
TypeScript
26
star
90

polkadot-scripts

Collection of random scripts and utilities written for Polkadot
TypeScript
26
star
91

polkadot-cloud

[BETA] A library and automated platform for developing and publishing assets for Polkadot dapps.
TypeScript
25
star
92

ink-playground

Browser Based Playground for editing, sharing & compiling ink! Smart Contracts
Rust
25
star
93

sms-verification

SMS verification for Parity.
JavaScript
25
star
94

link

Unstoppable URL shortener built with the ink! smart contract language.
TypeScript
25
star
95

parachains-integration-tests

Tool to make Parachains integrations tests development easier and faster
TypeScript
25
star
96

extended-parachain-template

Node template to build parachains with all the required pallets. Slightly opinionated based on what majority of parachain teams are using.
Rust
25
star
97

orchestra

A partial actor pattern with a global orchestrator.
Rust
25
star
98

zombienet-sdk

ZombieNet SDK
Rust
25
star
99

polkadot-onboard

A wallet integration SDK to facilitate adding different type of wallets (extension, WC, Ledger) to Dapps.
TypeScript
24
star
100

frame-metadata

A set of tools to parse FRAME metadata retrieved from Substrate-based nodes.
Rust
24
star