@didtools/pkh-ethereum

中文文档
0.6.0发布于 2024/07/15(Apache-2.0 OR MIT)TypeScriptES Module近 30 天下载 2万

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

中文使用文档
文档目录6 节
  1. Ethereum Auth 方法和验证器
  2. 安装
  3. 授权使用
  4. 配置
  5. 验证使用
  6. 许可证

Ethereum Auth 方法和验证器

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

安装

npm install --save @didtools/pkh-ethereum

授权使用

对基于网络的 Env 中的 Auth , 使用任何执行标准接口的注入 Ethereum 提供者 EthereumWebAuth。标准的 Ethereum 提供者应当使定义的接口失效 EIP-1193 (英语)。 。 。 .

// 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

// 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)

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

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 库.

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"方法使用您的 默认应用域名.

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

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

验证使用

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

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 共发布 12 个版本,首次发布于 2022/09/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.6.0latest2024/07/152 年前
0.5.02024/01/042 年前
0.4.2-next.0next2023/10/283 年前
0.4.12023/08/143 年前
0.4.02023/06/203 年前
0.3.02023/03/093 年前

关于 @didtools/pkh-ethereum 的常见问题

@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 和提交记录判断。