@1hive/radspec

中文文档
2.0.1发布于 2023/10/28MITCommonJS近 30 天下载 3385

Radspec is a safe alternative to Ethereum's natspec

原始使用文档
文档目录7 节
  1. Features
  2. Introduction & quick start
  3. Installation
  4. Documentation
  5. Contributing
  6. Aside: Why is natspec.js unsafe?
  7. License

radspec 🤘

Travis branch Coveralls github branch

Radspec is a safe interpreter for dynamic expressions in Ethereum's NatSpec.

This allows smart contact developers to show improved function documentation to end users, without the security pitfalls of natspec.js. Radspec defines its own syntax structure and parses its own AST rather than directly evaluating untrusted JavaScript.

Features

  • Expressive: Show relevant details to smart contract end-users at the time they make transactions.
  • External calls: Radspec can query other contracts.
  • Safe: Radspec requires no DOM access or untrusted JavaScript evaluation.
  • Compatible: Most existing NatSpec dynamic expressions are compatible with Radspec.

Introduction & quick start

Radspec supports any contract programming language, such as Solidity or Vyper because radspec works on the compiled JSON ABI. Here is an example using Solidity.

pragma solidity ^0.5.0;

contract Tree {
    /// @notice Set the tree age to `numYears` years
    function setAge(uint256 numYears) external {
        // set the age into storage
    }
}

Notice the dynamic expression documentation for the setAge function. When presented to the end user, this will render based on the inputs provided by the user. For example, if the end user is calling the contract with an input of 10 years, this will be rendered by radspec as:

Set the tree age to 10 years

Use the Solidity compiler to generate user documentation and ABI with:

solc --userdoc --abi tree.sol

This produces the outputs:

{
  "methods" :
  {
    "setAge(uint256)" :
    {
      "notice" : "Set the tree age to `numYears` years"
    }
  }
}

and

[{
  "constant":false,
  "inputs":[{"name":"numYears","type":"uint256"}],
  "name":"setAge",
  "outputs":[],
  "payable":false,
  "stateMutability":"nonpayable",
  "type":"function"
}]

Note you can also use Human-Redable abis. For the above example that would be:

["function setAge(uint256 numYears) public view"]

Write a simple tool using radspec to interpret this:

import radspec from 'radspec'

// Set userDoc and ABI from above
const expression = userDoc.methods["setAge(uint256)"].notice
const call = {
  abi: abi,
  transaction: {
    to: '0x8521742d3f456bd237e312d6e30724960f72517a',
    data: '0xd5dcf127000000000000000000000000000000000000000000000000000000000000000a'
  }
}
radspec.evaluate(expression, call)
  .then(console.log) // => "Set the tree age to 10 years"

Or see more examples here and in the tests.

Please let us know if there's anything else you'd like Radspec to be able to evaluate by filing an issue!

Installation

Simply use your favorite Node.js package manager:

npm i radspec

Documentation

Documentation about radspec and the internals of radspec can be found here.

Contributing

TBD

Aside: Why is natspec.js unsafe?

natspec.js accepts any valid JavaScript. There are multiple reasons this is a bad idea:

  1. You either need to write your own JavaScript VM or use eval (unsafe!) from inside JavaScript
  2. A fully-featured language with classes, functions and much more is absolutely overkill for something that could be solved with a simple DSL.

As dapps become increasingly complex, it is paramount that tools are written in a way that makes phishing near impossible. Evaluating JavaScript directly makes opens your dapp up to cross-site scripting attacks by users merely submitting a transaction(!).

License

MIT

版本与发布记录

@1hive/radspec 共发布 2 个版本,首次发布于 2021/11/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2023/10/283 年前
2.0.02021/11/294 年前

关于 @1hive/radspec 的常见问题

@1hive/radspec 是什么?

Radspec is a safe alternative to Ethereum's natspec

如何安装 @1hive/radspec?

在项目目录运行 npm install @1hive/radspec,或使用 pnpm add / yarn add / bun add。

@1hive/radspec 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/1hive__radspec 是否存在。

@1hive/radspec 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@1hive/radspec 可以商用吗?

@1hive/radspec 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@1hive/radspec 的下载量有多少?

根据 npm 官方下载统计,@1hive/radspec 在 2026/10/05 – 2026/11/03 这 30 天内被下载 3,385 次,日均约 113 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@1hive/radspec 还在维护吗?

最新版本 2.0.1 发布于 2023/10/28(3 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/11/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。