@ atcute/did-plc
验证、 签名和与 do:plc 操作交互 .
npm install @atcute/did-plc
id:plc是一种自我认证的DID方法,其中审计日志是真相的来源。 这个 软件包验证操作有适当的签名和链条,并为 创建和提交新的操作.
使用量
使用客户端
import { PlcClient } from '@atcute/did-plc';
const client = new PlcClient();
// fetch DID document
const doc = await client.getDocument('did:plc:ragtjsm2j2vknwkz3zp4oxrd');
// fetch current identity state
const state = await client.getState('did:plc:ragtjsm2j2vknwkz3zp4oxrd');
// fetch operation log
const log = await client.getOperationLog('did:plc:ragtjsm2j2vknwkz3zp4oxrd');
// fetch last operation
const lastOp = await client.getLastOperation('did:plc:ragtjsm2j2vknwkz3zp4oxrd');
// submit a signed operation
await client.submitOperation('did:plc:ragtjsm2j2vknwkz3zp4oxrd', signedOperation);
签名操作
import { signOperation, signTombstone, deriveDidFromGenesisOp } from '@atcute/did-plc';
// sign an unsigned operation
const signedOp = await signOperation(unsignedOp, rotationKey);
// sign a tombstone
const signedTombstone = await signTombstone(unsignedTombstone, rotationKey);
// derive did:plc from genesis operation
const did = await deriveDidFromGenesisOp(signedGenesisOp);
验证审计日志
import { defs, processIndexedEntryLog } from '@atcute/did-plc';
import * as v from 'valibot';
const did = 'did:plc:ragtjsm2j2vknwkz3zp4oxrd';
const response = await fetch(`https://plc.directory/${did}/log/audit`);
const json = await response.json();
const logs = v.parse(defs.indexedEntryLog, json);
const { canonical, nullified } = await processIndexedEntryLog(did, logs);
正在验证新操作
在向plc.directory提交新操作之前:
import { validateIncomingOp } from '@atcute/did-plc';
// throws if operation exceeds size limits or has invalid structure
validateIncomingOp(operation);
检查争议窗口
import { isDisputePeriodActive, getDisputeCandidates } from '@atcute/did-plc';
// assuming `canonical` from processIndexedEntryLog result above
// find operations that a rotation key can dispute
const candidates = getDisputeCandidates(canonical, 'did:key:z...');
// check if a specific operation can still be disputed (72-hour window)
const entry = canonical.at(-1);
if (entry && isDisputePeriodActive(entry)) {
// entry is still within the dispute window
}