# @didtools/pkh-ethereum 中文文档

规范地址：https://npmcn.com/package/%40didtools/pkh-ethereum/

版本：0\.6\.0
npm 元数据更新时间：2026-10-23T10:34:57.532Z
中文译文更新时间：2026-11-12T07:33:45.177Z
软件包许可：\(Apache-2\.0 OR MIT\)

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40didtools/pkh-ethereum
原文对照：https://npmcn.com/package/%40didtools/pkh-ethereum/?lang=original
项目主页：https://github.com/ceramicnetwork/js-did#readme
源码仓库：https://github.com/ceramicnetwork/js-did

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：20.9 kB，10 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=14\.14
- 运行依赖：5 个
- 首次发布：2022-09-22T18:36:06.608Z
- 最新版本发布：2024-07-15T10:13:58.624Z
- 近 30 天下载量：20483（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：12（近 12 个月 0 个）

## 中文简介

支持对Ethereum账户进行认证、授权和核实,如:pkh与SIWE(X)和CACAO。 与 `did-session` 财务报告和审定财务报表 `@didtools/cacao`。 。 。 .

## 安装

```sh
npm install @didtools/pkh-ethereum
```

也可以使用：`pnpm add @didtools/pkh-ethereum`、`yarn add @didtools/pkh-ethereum`、`bun add @didtools/pkh-ethereum`

## README

## Ethereum Auth 方法和验证器

支持对Ethereum账户进行认证、授权和核实,如:pkh与SIWE(X)和CACAO. 与 `did-session` 财务报告和审定财务报表 `@didtools/cacao`。 。 。 .

## 安装

```
npm install --save @didtools/pkh-ethereum
```

## 授权使用

对基于网络的 Env 中的 Auth , 使用任何执行标准接口的注入 Ethereum 提供者 `EthereumWebAuth`。标准的 Ethereum 提供者应当使定义的接口失效 [EIP-1193 (英语)](https://eips.ethereum.org/EIPS/eip-1193)。 。 。 .

```js
// Web Auth Usage
import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum'
// ...

const ethProvider = // import/get your web3 eth provider
const addresses = await ethProvider.request({ method: 'eth_requestAccounts' })
const accountId = await getAccountId(ethProvider, addresses[0])

const authMethod = await EthereumWebAuth.getAuthMethod(ethProvider, accountId)
```

在基于节点的 Env 中为 Auth 使用任何标准的 Web3 提供者接口 `EthereumNodeAuth`

```js
// Node Auth Usage
import { EthereumNodeAuth, getAccountId } from '@didtools/pkh-ethereum'
// ...

const ethProvider = // import/get your web3 eth provider
const addresses = await ethProvider.request({ method: 'eth_requestAccounts' })
const accountId = await getAccountId(ethProvider, addresses[0])
const appName = 'MyNodeApp'

const authMethod = await EthereumNodeAuth.getAuthMethod(ethProvider, accountId, appName)
```

与多会和参考多会文件一起使用,以获取更多细节.

```js
const client = new ComposeClient({ceramic, definition})
const resources = client.resources

const session = await DIDSession.authorize(authMethod, { resources })
client.setDID(session.did)
```

## 配置

AuthMethod 创建者会消费一个标准的 Ethereum 提供者和一个 AccountId. 账号跟随 CAIP10标准. 帮助方法 `getAccountID` ,但也可以创建账户ID 直接使用 CAIP 库.

```js
import { AccountId } from 'caip'
import { getAccountId } from '@didtools/pkh-ethereum'

const accountId = await getAccountId(ethProvider, addresses[0])

const ethMainnetChainId = '1'
const chainNameSpace = 'eip155'
const chainId = `${chainNameSpace}:${ethMainnetChainId}`
const accountIdCAIP = new AccountId({ address, chainId })

// accountId = accountIdCAIP
```

这个 `EthereumNodeAuth` 额外消耗应用程序名称。 "EhereumWebAuth"方法使用您的 默认应用域名.

```js
import { EthereumNodeAuth  } from '@didtools/pkh-ethereum'

const appName = 'MyNodeApp'
const authMethod = EthereumNodeAuth.getAuthMethod(ethProvider, accountId, appName)
```

## 验证使用

需要验证器来验证使用CACAO的已签名的有效载荷。 需要它们的图书馆 输入一个验证符地图,允许您注册您想要支持的验证符.

```js
import { Cacao } from '@didtools/cacao'
import { getEIP191Verifier } from '@didtools/pkh-ethereum'
import { DID } from 'dids'

const verifiers = {
    ...getEIP191Verifier()
}

// Directly with cacao
Cacao.verify(cacao, { verifiers, ...opts})

// With DIDS, reference DIDS for more details
const dids = //configured dids instance
await dids.verifyJWS(jws, { capability, verifiers, ...opts})
```

## 许可证

Apache-2.0或麻省理工学院

## 常见问题

### @didtools/pkh-ethereum 是什么？

支持对Ethereum账户进行认证、授权和核实,如:pkh与SIWE\(X\)和CACAO。 与 did-session 财务报告和审定财务报表 @didtools/cacao。 。 。 \.

### 如何安装 @didtools/pkh-ethereum？

在项目目录运行 npm install @didtools/pkh-ethereum，或使用 pnpm add / yarn add / bun add。

### @didtools/pkh-ethereum 支持 TypeScript 吗？

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

### @didtools/pkh-ethereum 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @didtools/pkh-ethereum 需要什么版本的 Node\.js？

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

### @didtools/pkh-ethereum 可以商用吗？

@didtools/pkh-ethereum 使用 \(Apache-2\.0 OR MIT\) 许可。可在 Apache-2\.0、MIT 中任选其一遵守。选择 Apache-2\.0 时，宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @didtools/pkh-ethereum 的下载量有多少？

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

### @didtools/pkh-ethereum 还在维护吗？

最新版本 0\.6\.0 发布于 2024/07/15（2 年前）；至今共发布 12 个版本，其中过去 12 个月发布了 0 个；首次发布于 2022/09/23。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

