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或麻省理工学院