#wit #generate-json #wasm-interface-types

bin+lib witme

生成和转换 wit 格式的工具

13 个不稳定版本 (3 个破坏性更新)

0.3.1 2022 年 5 月 18 日
0.3.0 2022 年 5 月 13 日
0.2.6 2022 年 5 月 12 日
0.2.5 2022 年 4 月 21 日
0.0.2 2022 年 2 月 14 日

905WebAssembly 中排名

Download history 1/week @ 2024-03-13 5/week @ 2024-03-27 10/week @ 2024-04-03 1/week @ 2024-05-29

每月 56 次下载
用于 raen

MIT 许可证

175KB
2.5K SLoC

Rust 2K SLoC // 0.0% comments TypeScript 343 SLoC // 0.1% comments JavaScript 250 SLoC // 0.0% comments

witme

生成和转换 wit 格式的工具。

选项 1

cargo install witme

如果需要安装用于生成 JSON 架构的 nodejs。一个建议是使用 nvm

选项 2

npm i -g witme

功能

目前这个仓库主要针对 NEAR 智能合约(目前仅限 Rust),但目标是成为一个通用的工具,用于处理 .wit 格式

  • 从用 Rust 编写的 NEAR 智能合约生成 .wit
  • .wit 生成 TS 以与合约交互
  • 从 TS 生成 JSON Schema(未来将直接从 wit 生成)

CLI

目前有一个 near 子命令,用于处理与 NEAR 相关的转换。

  • witme near wit
    • 在 rust 项目的根目录中生成一个 index.wit 文件(注意:它不能是工作空间)。这基于 witgen
  • witme near ts
    • .wit 文件生成 ts 文件(默认 index.wit -> ./ts/*)。这基于 wit-bindgen
  • witme near json
    • 从 TypeScript 生成一个用于输入的 json schema(默认 ./ts/index.ts --> index.schema.json),它使用 ts-json-schema-generator
  • witme near inject
    • 将数据写入 Wasm 二进制的自定义部分。

扩展

生成的 json schema 可以用于自动生成一个 react 表单,该表单可以验证合约调用的参数。由于 .wit 有文档注释的概念,Rust 源代码提供的文档将在生成的 TS 和 Json schema 中可用。这也允许添加特殊注释,可以在合约中使用的类型上添加更多条件。

例如,

///  @minLength 2
///  @maxLength 64
///  @pattern ^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$
type AccountId = String

生成以下wit

///  @minLength 2
///  @maxLength 64
///  @pattern ^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$
type account-id = string

该wit生成以下typescript

/**
* @minLength 2
* @maxLength 64
* @pattern ^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$
*/
export declare type AccountId = string;

该wit生成以下json模式

{
  "$schema": "https://json-schema.fullstack.org.cn/draft-07/schema#",
  "definitions": {
    "AccountId": {
      "maxLength": 64,
      "minLength": 2,
      "pattern": "^(([a-z\\d]+[-_])*[a-z\\d]+\\.)*([a-z\\d]+[-_])*[a-z\\d]+$",
      "type": "string"
    },

请参考本仓库中的rust-status-message示例

/// Retreive a message for a given account id
pub fn get_status(&self, account_id: AccountId) -> Option<String> {
    self.records.get(&account_id)
}

为它的参数生成以下模式

{
  "GetStatus": {
      "additionalProperties": false,
      "contractMethod": "view",
      "description": "Retreive a message for a given account id",
      "properties": {
        "account_id": {
          "$ref": "#/definitions/AccountId"
        }
      },
      "required": [
        "account_id"
      ],
      "type": "object"
    },
}

并在生成的Contract类上生成以下TS方法

    /**
    * Retreive a message for a given account id
    */
    get_status(args: {
        account_id: AccountId;
    }, options?: ViewFunctionOptions): Promise<string | null>;

close-up获取从模式生成表单的示例。

注意

目前.wit没有规定如何为一种语言实现变体类型。 witme目前支持JSON编码的参数和返回值。因此,变体目前以与serde_json提供的默认值相同的方式编码。然而,未来的borsh支持将取消这一限制,并允许更有效地编码变体类型。

路线图

  • 提供一个Rust生成器,允许跨合约调用和客户端/测试实现。
  • 直接从wit生成json模式
  • 在Rust中生成验证并将其编译为Wasm以简化验证,并允许在后台处理诸如编码到borsh之类的转换。

依赖关系

~10MB
~223K SLoC