@didtools/pkh-ethereum

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

Implements support to authenticate, authorize and verify with Ethereum accounts as a did:pkh with SIWE(X) and CACAO. Primarly used with did-session and @didtools/cacao.

原始使用文档
文档目录6 节
  1. Ethereum AuthMethod and Verifier
  2. Installation
  3. Auth Usage
  4. Configuration
  5. Verifier Usage
  6. License

Ethereum AuthMethod and Verifier

Implements support to authenticate, authorize and verify with Ethereum accounts as a did:pkh with SIWE(X) and CACAO. Primarly used with did-session and @didtools/cacao.

Installation

npm install --save @didtools/pkh-ethereum

Auth Usage

To Auth in a web based env, use any injected Ethereum provider that implements the standard interface with EthereumWebAuth. A standard Ethereum provider should impelement the interface as defined by 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)

To Auth in a Node based env, use any standard web3 provider interface with 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)

To use with did-session and reference did-session docs for more details.

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

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

Configuration

AuthMethod creators consume a standard Ethereum provider and an AccountId. AccountID follows the CAIP10 standard. The helper method getAccountID is provided, but you can also create an AccountID using the CAIP library directly.

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

The EthereumNodeAuth additionally consumes an application name. The 'EthereumWebAuth' method uses your application domain name by default.

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

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

Verifier Usage

Verifiers are needed to verify different did:pkh signed payloads using CACAO. Libraries that need them will consume a verifiers map allowing your to register the verifiers you want to support.

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

License

Apache-2.0 OR MIT

版本与发布记录

@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 是什么?

Implements support to authenticate, authorize and verify with Ethereum accounts as a did:pkh with SIWE(X) and CACAO. Primarly used with did-session and @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 和提交记录判断。