堆栈 Auth 方法和验证器
支持对 Stacks 的账户进行认证、授权和验证,作为: pkh 与 SIWE( X) 和 CACAO 的连接.
与 did-session 财务报告和审定财务报表 @didtools/cacao。 。 。 .
安装
npm install --save @didtools/pkh-stacks
授权使用
在基于web 的 Env 中使用任何执行标准接口的被注入的 Web3 提供者 StacksWebAuth。 。 。 .
// Web Auth Usage
import { StacksWebAuth, getAccountIdByNetwork, verifyStacksSignature } from '@didtools/pkh-stacks'
import { AppConfig, UserSession } from '@stacks/connect'
// ...
const stacksProvider = window.StacksProvider
const appConfig = new AppConfig(['store_write'])
const userSession = new UserSession({ appConfig })
const userData = userSession.loadUserData()
const address = user.profile.stxAddress.mainnet
const accountId = await getAccountIdByNetwork('mainnet', address)
const authMethod = await StacksWebAuth.getAuthMethod(stacksProvider, accountId, publicKey)
与多会和参考多会文件一起使用,以获取更多细节.
const session = await DIDSession.authorize(authMethod, { resources: ['ceramic://*'] })
配置
AuthMethod 创建者消耗一个标准 Stacks 提供者和一个 AccountId. 账户ID遵循CAIP10标准. 帮助方法 getAccountIdByNetwork id提供了,但您也可以直接使用 CAIP 库创建 AccountID .
import { AccountId } from 'caip'
import { getAccountIdByNetwork } from '@didtools/pkh-stacks'
// Using network string
const accountId = getAccountIdByNetwork('mainnet', address)
// With CAIP
const stacksMainnetChainId = '1'
const chainNameSpace = 'stacks'
const chainId = `${chainNameSpace}:${stacksMainnetChainId}`
const accountIdCAIP = new AccountId({ address, chainId })
验证使用
需要验证器来验证使用CACAO的已签名的有效载荷。 需要它们的图书馆 输入一个验证符地图,允许您注册您想要支持的验证符.
import { Cacao } from '@didtools/cacao'
import { getStacksVerifier } from '@didtools/pkh-stacks'
import { DID } from 'dids'
const verifiers = {
...getStacksVerifier(),
}
// 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或麻省理工学院