@exodus/bintoken

中文文档
0.1.2发布于 2023/03/15UNLICENSEDCommonJS零依赖近 30 天下载 3.1万

Binary encoding of named fixed-length buffers.

原始使用文档

Bintoken

A bintoken is an encoded binary message of a fixed length, meant to serve as a message which can be signed. Depending on the type byte, and who signed the bintoken, the message may hold different meanings.

For example, if the type indicates the message is an authentication token, and the bintoken is signed by a server's private key, this might provide a guarantee that the server verified some information at the time indicated in the bintoken.

The encoding format of the token is specified in-advance by instantiating the BintokenEncoding class exported by this package. The encoding is specified with property names and lengths for each property. The key names are sorted alphabetically when encoding and decoding bintokens.

The deterministic binary format simplifies signature verification. Its fixed length simplifies decoding & validation, and thus reduces the chance for malicious exploitation. Unlike with other formats such as JSON, there is only one way to encode a message within a bintoken encoding scheme.

Here is the generic serialization format of any bintoken encoding:

  • type (1 byte) : a version number which can be used to identify the context of the message.
  • [...payload properties]
  • time (4 bytes) : the timestamp (in unix seconds) when the message was created, encoded as a big-endian uint32.

Example

Here is an example of a bintoken encoding which encodes only a 32-byte public key as the payload:

const BintokenEncoding = require('@exodus/bintoken')

const tokenEncoding = new BintokenEncoding({ publicKey: 32, hash: 20 })

const token = tokenEncoding.toBuffer({
  type: 1,
  publicKey: randomBytes(32),
  hash: randomBytes(20),
})

/*
  // Erroneous use
  const token = tokenEncoding.toBuffer({ type: 1, publicKey: randomBytes(32), hash: randomBytes(20), otherProp: Buffer.alloc(0) })
  const token = tokenEncoding.toBuffer({ type: 1, publicKey: randomBytes(31), hash: randomBytes(20) })
  const token = tokenEncoding.toBuffer({ type: 1, publicKey: 'foo', hash: randomBytes(20) })
  const token = tokenEncoding.toBuffer({ type: 1 })
*/

const { type, time, publicKey, hash } = tokenEncoding.fromBuffer(token)

/*
  // Error, wrong length
  const { type, time, publicKey } = tokenEncoding.fromBuffer(
    Buffer.concat([token, Buffer.alloc(1)])
  )
*/

In this example, the serialization format is:

  • type (1 byte) : a version number which can be used to identify the context of the message.
  • hash (20) bytes : a hash over which the message holds meaning (as implied by the type).
  • public key (32 bytes) : the public key over which the message holds meaning (as implied by the type).
  • time (4 bytes) : the timestamp (in seconds) when the message was created, encoded as a big-endian uint32.

Notice that hash is written before publicKey in the payload, as the properties are sorted alphabetically before being written to, or read from, a bintoken. This means the keys you choose for your message must be the same for different applications using the same bintokens.

版本与发布记录

@exodus/bintoken 共发布 3 个版本,首次发布于 2021/05/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2latest2023/03/153 年前
0.1.12021/05/265 年前
0.1.02021/05/265 年前

关于 @exodus/bintoken 的常见问题

@exodus/bintoken 是什么?

Binary encoding of named fixed-length buffers.

如何安装 @exodus/bintoken?

在项目目录运行 npm install @exodus/bintoken,或使用 pnpm add / yarn add / bun add。

@exodus/bintoken 支持 TypeScript 吗?

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

@exodus/bintoken 支持 ESM 还是 CommonJS?

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

@exodus/bintoken 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.18.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@exodus/bintoken 可以商用吗?

@exodus/bintoken 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@exodus/bintoken 的下载量有多少?

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

@exodus/bintoken 还在维护吗?

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