@atcute/identity

中文文档
2.0.2发布于 2026/07/160BSDES Module近 30 天下载 40.4万

syntax, type definitions and schemas for atproto handles, DIDs and DID documents

原始使用文档
文档目录8 节
  1. usage
  2. checking DID types
  3. extracting the DID method
  4. working with did:web
  5. reading DID documents
  6. service endpoint helpers
  7. validating DID documents
  8. type definitions

@atcute/identity

types, schemas, and utilities for working with AT Protocol identities.

npm install @atcute/identity

in AT Protocol, users are identified by DIDs (decentralized identifiers). this package provides tools for working with DIDs and DID documents - the documents that describe a user's identity, including their handle, PDS location, and cryptographic keys.

for resolving handles and DIDs (fetching their documents), see @atcute/identity-resolver.

usage

checking DID types

AT Protocol supports two DID methods: did:plc and did:web. use the type guards to check which method a DID uses:

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 checks for DIDs that are valid in AT Protocol - this excludes did:web identifiers with custom paths, which atproto doesn't support.

extracting the DID method

import { extractDidMethod } from '@atcute/identity';

const method = extractDidMethod('did:plc:z72i7hdynmk6r22z27h6tvur');
// -> "plc"

working with did:web

convert a did:web identifier to its DID document 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"

reading DID documents

once you have a DID document (from a resolver or API), extract information using the utility functions:

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..." }

service endpoint helpers

extract specific service endpoints from DID documents:

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

validating DID documents

use the validation schemas to check if a DID document is well-formed:

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

the schema validates:

  • required fields (@context, id)
  • DID string format
  • verification method structure and key formats
  • service endpoint URLs
  • no duplicate entries in arrays

type definitions

the package exports TypeScript types for DID document structures:

import type { DidDocument, VerificationMethod, Service } from '@atcute/identity';

// DidDocument - the full DID document
// VerificationMethod - a verification method entry
// Service - a service endpoint entry

版本与发布记录

@atcute/identity 共发布 17 个版本,首次发布于 2025/03/13;过去 12 个月发布了 7 个版本。

版本发布日期距今
2.0.2latest2026/07/163 个月前
2.0.12026/06/294 个月前
2.0.02026/05/095 个月前
1.1.52026/05/086 个月前
1.1.42026/03/217 个月前
1.1.32025/11/2311 个月前

关于 @atcute/identity 的常见问题

@atcute/identity 是什么?

syntax, type definitions and schemas for atproto handles, DIDs and DID documents

如何安装 @atcute/identity?

在项目目录运行 npm install @atcute/identity,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@atcute/lexicons(^2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@atcute/identity 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/atcute__identity 是否存在。

@atcute/identity 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@atcute/identity 可以商用吗?

@atcute/identity 使用 0BSD 许可。几乎没有限制:可自由使用、修改和分发,连保留版权声明也不要求。以上为概括,不构成法律意见,请以许可原文为准。

@atcute/identity 的下载量有多少?

根据 npm 官方下载统计,@atcute/identity 在 2026/10/07 – 2026/11/05 这 30 天内被下载 404,427 次,日均约 13,481 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@atcute/identity 还在维护吗?

最新版本 2.0.2 发布于 2026/07/16(3 个月前);至今共发布 17 个版本,其中过去 12 个月发布了 7 个;首次发布于 2025/03/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。