> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Identity

> 介绍 Bluesky 中 Handle 与 DID 的相互解析方法，并汇总通过 Bluesky::identity() 进行的操作。

## 概览

在 Bluesky 中，经常需要在 Handle、DID 之间进行相互解析。

* [https://docs.bsky.app/docs/advanced-guides/resolving-identities](https://docs.bsky.app/docs/advanced-guides/resolving-identities)
* [https://atproto.com/guides/identity](https://atproto.com/guides/identity)

## 从 Handle 解析 DID

### 通过 API

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;

$did = Bluesky::resolveHandle('***.bsky.social')->json('did');
```

### 通过 DNS / well-known

从 DNS TXT 记录或 `/.well-known/atproto-did` 中获取。

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;

$did = Bluesky::identity()->resolveHandle('alice.test');
```

两者看似相似，但行为不同。API 方式会通过网络向 Bluesky 服务查询，而 DNS/well-known 方式则更适合解析自定义域名的 Handle。

## 从 DID 获取 DID Document

`resolveDID` 会返回 DID Document。

[https://plc.directory/did:plc:ewvi7nxzyoun6zhxrhs64oiz](https://plc.directory/did:plc:ewvi7nxzyoun6zhxrhs64oiz)

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;

$didDoc = Bluesky::identity()->resolveDID('did:plc:*** 或 did:web:***')->json();
```

## 从 DID Document 获取 Handle

DID Document 的 `alsoKnownAs` 字段中包含 Handle。

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Support\DidDocument;

$didDoc = DidDocument::make(Bluesky::identity()->resolveDID('did:plc:*** 或 did:web:***')->json());
$handle = $didDoc->handle();
```

## 使用 resolveIdentity 统一解析

`resolveIdentity` 结合了 `resolveHandle` 与 `resolveDID`，无论传入 DID 还是 Handle 都能进行解析，并返回 DID Document。

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Support\DidDocument;

$didDoc = DidDocument::make(Bluesky::identity()->resolveIdentity('did 或 handle')->json());
$didDoc->id();
$didDoc->handle();
```

## 获取公开个人资料

公开个人资料无论从 DID 还是 Handle 都可以解析，响应中会同时包含 DID 和 Handle。

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;

$profile = Bluesky::getProfile('did 或 handle');
$profile->json('did');
$profile->json('handle');
```

## 获取 PDS URL / serviceEndpoint

DID Document 中的 `serviceEndpoint` 就是 PDS 的 URL。虽然直接使用的情况不多，但你可以按需获取。

```
https://***.***.host.bsky.network
```

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Support\DidDocument;

$didDoc = DidDocument::make(Bluesky::identity()->resolveIdentity('did 或 handle')->json());
$didDoc->pdsUrl();
```

## 获取 Authorization Server URL

通过 PDS 解析出 Authorization Server 的 URL。

对于在 Bluesky 注册的账号，一般是 `https://bsky.social`。

```php theme={null}
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Support\DidDocument;

$didDoc = DidDocument::make(Bluesky::identity()->resolveIdentity('did 或 handle')->json());

$pds = Bluesky::pds()->getProtectedResource($didDoc->pdsUrl());
$pds->authServer();
```

<Info>
  Source: [docs/identity.md](https://github.com/invokable/laravel-bluesky/blob/main/docs/identity.md)
</Info>


## Related topics

- [BlueskyManager 与 HasShortHand](/zh/packages/laravel-bluesky/bluesky-manager.md)
- [WebSocket (Jetstream / Firehose)](/zh/packages/laravel-bluesky/websocket.md)
- [Crypto —— AT Protocol 加密](/zh/packages/laravel-bluesky/crypto.md)
