@ tcute/ 身份
类型、计划以及使用AT 协议身份的公用事业.
npm install @atcute/identity
在AT协议中,用户由 已经 (权力下放) (原始内容存档于2012-10-12) (中文(简体) ). 此软件包提供了与 DIDs 和 DID 文档合作的工具 - 文档 描述用户的身份,包括其手柄、PDS位置和密码密钥.
用于解析手柄和手柄的,参见 @atcute/identity-resolver。 。 。 .
使用量
检查已完成的类型
AT 协议支持两种DD方法: did:plc 财务报告和审定财务报表 did:web。使用类型守护检查哪个
dDE 方法的使用 :
import { isPlcDid, isWebDid, isAtprotoDid } from '@atcute/identity';
const did = 'did:plc:z72i7hdynmk6r22z27h6tvur';
if (isPlcDid(did)) {
// did:plc identifier
console.log('PLC DID');
}
if (isWebDid(did)) {
// did:web identifier (general, includes custom paths)
console.log('Web DID');
}
if (isAtprotoDid(did)) {
// either did:plc or atproto-compatible did:web
console.log('AT Protocol DID');
}
isAtprotoDid 检查AT 协议中有效的文件 - 不包括已做的: 网络标识符
带有自定义路径, atproto 不支持.
提取 DDE 方法
import { extractDidMethod } from '@atcute/identity';
const method = extractDidMethod('did:plc:z72i7hdynmk6r22z27h6tvur');
// -> "plc"
与 did: web 合作
将一个已完成的: web 标识符转换到其已执行的文档 URL :
import { webDidToDocumentUrl, normalizeWebDid } from '@atcute/identity';
// simple domain
const url = webDidToDocumentUrl('did:web:example.com');
// -> URL { href: "https://example.com/.well-known/did.json" }
// with path
const url2 = webDidToDocumentUrl('did:web:example.com:users:alice');
// -> URL { href: "https://example.com/users/alice/did.json" }
// normalize for comparison
const normalized = normalizeWebDid('did:web:EXAMPLE.COM');
// -> "did:web:example.com"
读取已读文档
一旦您有 DD 文档( 从解析器或 API) , 请使用工具提取信息 函数:
import {
getPdsEndpoint,
getAtprotoHandle,
getAtprotoVerificationMaterial,
getLabelerEndpoint,
} from '@atcute/identity';
import type { DidDocument } from '@atcute/identity';
const doc: DidDocument = {
'@context': ['https://www.w3.org/ns/did/v1'],
id: 'did:plc:z72i7hdynmk6r22z27h6tvur',
alsoKnownAs: ['at://bsky.app'],
verificationMethod: [
{
id: 'did:plc:z72i7hdynmk6r22z27h6tvur#atproto',
type: 'Multikey',
controller: 'did:plc:z72i7hdynmk6r22z27h6tvur',
publicKeyMultibase: 'zDnaek...',
},
],
service: [
{
id: '#atproto_pds',
type: 'AtprotoPersonalDataServer',
serviceEndpoint: 'https://morel.us-east.host.bsky.network',
},
],
};
// get the user's PDS URL
const pds = getPdsEndpoint(doc);
// -> "https://morel.us-east.host.bsky.network"
// get the user's handle
const handle = getAtprotoHandle(doc);
// -> "bsky.app"
// get the signing key material
const key = getAtprotoVerificationMaterial(doc);
// -> { type: "Multikey", publicKeyMultibase: "zDnaek..." }
服务端点助手
从 DCD 文档中提取特定服务端点 :
import {
getPdsEndpoint,
getLabelerEndpoint,
getBlueskyChatEndpoint,
getBlueskyFeedgenEndpoint,
getBlueskyNotificationEndpoint,
getAtprotoServiceEndpoint,
} from '@atcute/identity';
// standard helpers for common services
const pds = getPdsEndpoint(doc); // #atproto_pds
const labeler = getLabelerEndpoint(doc); // #atproto_labeler
const chat = getBlueskyChatEndpoint(doc); // #bsky_chat
const feedgen = getBlueskyFeedgenEndpoint(doc); // #bsky_fg
const notif = getBlueskyNotificationEndpoint(doc); // #bsky_notif
// or use the generic helper for custom services
const custom = getAtprotoServiceEndpoint(doc, {
id: '#my_service',
type: 'MyServiceType', // optional type filter
});
正在验证已执行的文档
使用验证计划检查一个 DD 文档是否配置良好 :
import { defs } from '@atcute/identity';
import * as v from 'valibot';
const result = v.safeParse(defs.didDocument, unknownData);
if (result.success) {
// result.output is a validated DidDocument
console.log(result.output.id);
} else {
// validation failed
console.error('invalid DID document', result.issues);
}
计划验证:
- 需要的字段(
@context, (中文(简体) ),id(中文(简体) ) - 执行字符串格式
- 核查方法结构和关键格式
- 服务端点 URL
- 数组中没有重复的条目
类型定义
dOD 文档结构的软件包导出类型Script :
import type { DidDocument, VerificationMethod, Service } from '@atcute/identity';
// DidDocument - the full DID document
// VerificationMethod - a verification method entry
// Service - a service endpoint entry