@ainblockchain/ain-util

中文文档
1.3.0发布于 2024/07/23MPL-2.0TypeScriptCommonJS近 30 天下载 2953

A collection of utility functions for AI Network (AIN).

原始使用文档
文档目录36 节
  1. Installation
  2. Examples
  3. API
  4. addHexPrefix(str: string) -> string
  5. bufferToHex(buf: Buffer) -> string
  6. ecSignMessage(message: any, privateKey: Buffer, chainId?: n…
  7. ecRecoverPub(msgHash: Buffer, r: Buffer, s: Buffer, v: numb…
  8. ecSplitSig(signature: Buffer) -> ECDSASignature
  9. ecVerifySig(data: any, signature: string, address: string, …
  10. ecSignTransaction(txData: TransactionBody, privateKey: Buff…
  11. hashTransaction(transaction: TransactionBody | string) -> B…
  12. hashMessage(message: any) -> Buffer
  13. isHexPrefixed(str: string) -> boolean
  14. isValidPrivate(privateKey: Buffer) -> boolean
  15. isValidPublic(publicKey: Buffer, isSEC1: boolean = false) -…
  16. isValidAddress(address: string) -> boolean
  17. areSameAddresses(address1: string, address2: string) -> boo…
  18. keccak(input: any, bits: number = 256) -> Buffer
  19. privateToPublic(privateKey: Buffer) -> Buffer
  20. pubToAddress(publicKey: Buffer, isSEC1: boolean = false) ->…
  21. privateToAccount(privateKey: Buffer) -> Account
  22. serialize(data: any, _fields?: Array) -> Buffer (will depre…
  23. setLength(message: any, length: number, right: boolean = fa…
  24. stripHexPrefix(str: string) -> string
  25. toBuffer(v: any) -> Buffer
  26. toChecksumAddress(address: string) -> string
  27. encryptWithPublicKey(publicKey: string, message: string) ->…
  28. decryptWithPrivateKey(privateKey: string, encrypted: Encryp…
  29. createAccount(entropy?: string) -> Account
  30. privateToV3Keystore(privateKey: Buffer, password: string, o…
  31. v3KeystoreToPrivate(v3Keystore: V3Keystore | string, passwo…
  32. encode(key: string) -> string
  33. decode(key: string) -> string
  34. Environments
  35. node
  36. LICENSE

ain-util

A collection of utility functions for AI Network (AIN). The methods and tests have been modified from ethereumjs-util and ethjs-util.

Installation

npm install @ainblockchain/ain-util

Examples

const ainUtil = require('@ainblockchain/ain-util');

const message = { foo: 'bar' };
const privateKey = YOUR_OWN_PRIVATE_KEY;

// Alice generates signature using private key.
const signature = ainUtil.ecSignMessage(JSON.stringify(message), Buffer.from(privateKey, 'hex'));

// Bob verifies the message
const hash = ainUtil.hashMessage(JSON.stringify(message));
const sigBuffer = ainUtil.toBuffer(signature);
const len = sigBuffer.length;
const lenHash = len - 65;
const { r, s, v } = ainUtil.ecSplitSig(sigBuffer.slice(lenHash, len));
const publicKey = ainUtil.ecRecoverPub(Buffer.from(hash, 'hex'), r, s, v);
const address = ainUtil.toChecksumAddress(ainUtil.bufferToHex(ainUtil.pubToAddress(publicKey, publicKey.length === 65)));
const isVerified = ainUtil.ecVerifySig(JSON.stringify(message), signature, address);
// console.log(isVerified);
// will return
// ADDRESS
// true

API

addHexPrefix(str: string) -> string

Adds "0x" to a given string if it does not already start with "0x".

bufferToHex(buf: Buffer) -> string

Converts a Buffer into a hex string.

ecSignMessage(message: any, privateKey: Buffer, chainId?: number) -> string

Signs a message with a private key and returns a string signature.

ecRecoverPub(msgHash: Buffer, r: Buffer, s: Buffer, v: number, chainId?: number) -> Buffer

ECDSA public key recovery from signature.

ecSplitSig(signature: Buffer) -> ECDSASignature

Converts signature format of the eth_sign RPC method to signature parameters.

ecVerifySig(data: any, signature: string, address: string, chainId?: number) -> boolean

Checks if the signature is valid.

ecSignTransaction(txData: TransactionBody, privateKey: Buffer, chainId?: number) -> string

Signs a transaction body with a private key and returns a string signature.

hashTransaction(transaction: TransactionBody | string) -> Buffer

Generates keccak hash using a transaction body.

hashMessage(message: any) -> Buffer

Returns the bitcoin's varint encoding of keccak-256 hash of message, prefixed with the header 'AINetwork Signed Message:\n'.

isHexPrefixed(str: string) -> boolean

Checks whether the str is prefixed with "0x".

isValidPrivate(privateKey: Buffer) -> boolean

Checks whether the privateKey is a valid private key (follows the rules of the curve secp256k1).

isValidPublic(publicKey: Buffer, isSEC1: boolean = false) -> boolean

Checks whether the publicKey is a valid public key (follows the rules of the curve secp256k1 and meets the AIN requirements).

isValidAddress(address: string) -> boolean

Checks if the address is valid.

areSameAddresses(address1: string, address2: string) -> boolean

Checks if the two addresses are the same.

keccak(input: any, bits: number = 256) -> Buffer

Creates Keccak hash of the input.

privateToPublic(privateKey: Buffer) -> Buffer

Returns the public key of a given private key.

pubToAddress(publicKey: Buffer, isSEC1: boolean = false) -> Buffer

Returns the AI Network address of a given public key.

privateToAccount(privateKey: Buffer) -> Account

Returns an Account with the given private key.

serialize(data: any, _fields?: Array) -> Buffer (will deprecated)

Serializes an object (e.g. tx data) using rlp encoding.

setLength(message: any, length: number, right: boolean = false) -> Buffer

Pads and message with leading zeros till it has length bytes. Truncates from the beginning if message is longer than length.

stripHexPrefix(str: string) -> string

Removes '0x' from a given String is present.

toBuffer(v: any) -> Buffer

Attempts to turn a value into a Buffer. As input it supports Buffer, String, Number, null/undefined, BN and other objects with a toArray() method.

toChecksumAddress(address: string) -> string

Returns a checksummed address.

encryptWithPublicKey(publicKey: string, message: string) -> Promise

Encrypts message with publicKey.

decryptWithPrivateKey(privateKey: string, encrypted: Encrypted | string) -> Promise

Decrypts encrypted data with privateKey.

createAccount(entropy?: string) -> Account

Creates an account with a given entropy.

privateToV3Keystore(privateKey: Buffer, password: string, options: V3KeystoreOptions = {}) -> V3Keystore

Converts an account into a V3 Keystore and encrypts it with a password.

v3KeystoreToPrivate(v3Keystore: V3Keystore | string, password: string) -> Buffer

Returns a private key from a V3 Keystore.

encode(key: string) -> string

decode(key: string) -> string

Environments

Tested on the below environments.

node

  • v12.x.x
  • v10.x.x

  • v6.x.x
  • v7.x.x

LICENSE

MPL-2.0

版本与发布记录

@ainblockchain/ain-util 共发布 23 个版本,首次发布于 2019/02/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.0latest2024/07/232 年前
1.2.12024/07/052 年前
1.2.02024/07/052 年前
1.1.92021/12/304 年前
1.1.82021/11/094 年前
1.1.72021/08/235 年前

关于 @ainblockchain/ain-util 的常见问题

@ainblockchain/ain-util 是什么?

A collection of utility functions for AI Network (AIN).

如何安装 @ainblockchain/ain-util?

在项目目录运行 npm install @ainblockchain/ain-util,或使用 pnpm add / yarn add / bun add。

@ainblockchain/ain-util 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ainblockchain/ain-util 支持 ESM 还是 CommonJS?

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

@ainblockchain/ain-util 可以商用吗?

@ainblockchain/ain-util 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@ainblockchain/ain-util 的下载量有多少?

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

@ainblockchain/ain-util 还在维护吗?

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