Skip to content

社交API

社交API提供去中心化身份(DID)、P2P加密消息、联系人管理和社交互动功能。

接口列表

接口方法路径说明
创建DID身份POST/api/did/create创建去中心化身份
DID列表GET/api/did/list查询DID身份列表
DID签名POST/api/did/sign使用DID签名消息
DID验证POST/api/did/verify验证DID签名
添加联系人POST/api/social/contacts添加联系人
联系人列表GET/api/social/contacts查询联系人列表
发送消息POST/api/p2p/send发送P2P加密消息
好友请求POST/api/social/friend/add发送好友请求
发布动态POST/api/social/posts发布社交动态
社交统计GET/api/social/stats获取社交统计数据

创建DID身份

创建基于Ed25519的去中心化身份。

请求

http
POST /api/did/create
Authorization: Bearer <token>
Content-Type: application/json

请求体:

json
{
  "alias": "我的主身份",
  "method": "key"
}

参数说明:

参数类型必填说明
aliasstring身份别名
methodstringDID方法,默认key(支持key、web)

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "DID创建成功",
  "data": {
    "did": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
    "alias": "我的主身份",
    "method": "key",
    "publicKey": "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
    "createdAt": "2024-12-02T10:30:00Z"
  }
}

DID列表

查询当前用户的所有DID身份。

请求

http
GET /api/did/list
Authorization: Bearer <token>

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "success",
  "data": {
    "items": [
      {
        "did": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
        "alias": "我的主身份",
        "method": "key",
        "isDefault": true,
        "createdAt": "2024-12-02T10:30:00Z"
      }
    ],
    "total": 1
  }
}

DID签名

使用默认DID对消息进行数字签名。

请求

http
POST /api/did/sign
Authorization: Bearer <token>
Content-Type: application/json

请求体:

json
{
  "message": "需要签名的消息内容",
  "did": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
}

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "签名成功",
  "data": {
    "signature": "3045022100...",
    "did": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
    "algorithm": "Ed25519",
    "timestamp": "2024-12-02T10:30:00Z"
  }
}

DID验证

验证DID签名的有效性。

请求

http
POST /api/did/verify
Authorization: Bearer <token>
Content-Type: application/json

请求体:

json
{
  "message": "需要验证的消息内容",
  "signature": "3045022100...",
  "did": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
}

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "验证完成",
  "data": {
    "valid": true,
    "did": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
    "algorithm": "Ed25519"
  }
}

添加联系人

添加一个新联系人。

请求

http
POST /api/social/contacts
Authorization: Bearer <token>
Content-Type: application/json

请求体:

json
{
  "name": "Alice",
  "did": "did:key:z6MkpTHR8VNs5xhqeLfTkb...",
  "note": "技术社区认识的朋友"
}

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "联系人添加成功",
  "data": {
    "contactId": "contact-1701589200123",
    "name": "Alice",
    "did": "did:key:z6MkpTHR8VNs5xhqeLfTkb...",
    "createdAt": "2024-12-02T10:30:00Z"
  }
}

联系人列表

分页查询联系人列表。

请求

http
GET /api/social/contacts?page=1&pageSize=20
Authorization: Bearer <token>

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "success",
  "data": {
    "items": [
      {
        "contactId": "contact-1701589200123",
        "name": "Alice",
        "did": "did:key:z6MkpTHR8VNs5xhqeLfTkb...",
        "status": "active",
        "createdAt": "2024-12-02T10:30:00Z"
      }
    ],
    "total": 5,
    "page": 1,
    "pageSize": 20
  }
}

发送消息

通过P2P网络发送端到端加密消息。

请求

http
POST /api/p2p/send
Authorization: Bearer <token>
Content-Type: application/json

请求体:

json
{
  "peerId": "peer-abc123",
  "message": "你好,这是一条加密消息",
  "encrypt": true
}

参数说明:

参数类型必填说明
peerIdstring目标节点ID
messagestring消息内容
encryptboolean是否加密,默认true

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "消息发送成功",
  "data": {
    "messageId": "msg-1701589200456",
    "peerId": "peer-abc123",
    "encrypted": true,
    "sentAt": "2024-12-02T10:30:00Z"
  }
}

好友请求

向指定联系人发送好友请求。

请求

http
POST /api/social/friend/add
Authorization: Bearer <token>
Content-Type: application/json

请求体:

json
{
  "contactId": "contact-1701589200123",
  "message": "你好,我是Bob,希望加你为好友"
}

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "好友请求已发送",
  "data": {
    "requestId": "req-1701589200789",
    "contactId": "contact-1701589200123",
    "status": "pending",
    "sentAt": "2024-12-02T10:30:00Z"
  }
}

发布动态

发布一条社交动态。

请求

http
POST /api/social/posts
Authorization: Bearer <token>
Content-Type: application/json

请求体:

json
{
  "content": "今天学习了ChainlessChain的DID功能,非常强大!",
  "visibility": "public"
}

参数说明:

参数类型必填说明
contentstring动态内容
visibilitystring可见性:public/friends/private,默认public

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "动态发布成功",
  "data": {
    "postId": "post-1701589200123",
    "content": "今天学习了ChainlessChain的DID功能,非常强大!",
    "visibility": "public",
    "createdAt": "2024-12-02T10:30:00Z"
  }
}

社交统计

获取当前用户的社交统计数据。

请求

http
GET /api/social/stats
Authorization: Bearer <token>

响应

成功响应 (200):

json
{
  "code": 200,
  "message": "success",
  "data": {
    "contacts": 15,
    "friends": 8,
    "posts": 42,
    "messages": 256,
    "peers": 3
  }
}

错误码

错误码说明
5001DID创建失败
5002DID不存在
5003签名验证失败
5004联系人不存在
5005好友请求已存在
5006P2P节点不可达
5007消息加密失败

CLI对应命令

bash
# DID管理
chainlesschain did create
chainlesschain did list
chainlesschain did sign "message"

# P2P消息
chainlesschain p2p peers
chainlesschain p2p send <peer> "msg"

# 社交功能
chainlesschain social contact add "Alice"
chainlesschain social contact list
chainlesschain social friend add <contact-id>
chainlesschain social post publish "Hello"
chainlesschain social chat send <user> "msg"
chainlesschain social stats

附录:规范章节补全(v5.0.3.108)

为对齐项目用户文档标准结构,下列章节补齐若干未在正文中单独列出的视角。已在正文覆盖的章节在此段仅作简述并标注 见上文 指引。

1. 概述

见正文「接口列表」。社交 API 提供去中心化身份(DID)、P2P 加密消息、联系人管理与社交互动,是 ChainlessChain 去中心化社交的核心接口。

2. 核心特性

  • DID 身份(创建 / 列表 / 签名 / 验证)
  • P2P 加密消息(libp2p + Signal Protocol)
  • 联系人 / 好友请求管理
  • 社交动态发布 + 统计

3. 系统架构

客户端 / CLI ──Bearer JWT──► REST /api/did|social|p2p
                                (ChainlessChain 系统 http://localhost:3000/api)

                  DID(Ed25519)+ libp2p/WebRTC mesh + Signal Protocol E2E

4. 系统定位

ChainlessChain 去中心化社交的 API 侧,对应 CLI chainlesschain did/p2p/social(见正文「CLI对应命令」)。

5. 核心功能

见正文「接口列表」:POST /api/did/createGET /api/did/listPOST /api/did/sign|verifyPOST/GET /api/social/contactsPOST /api/p2p/sendPOST /api/social/friend/addPOST /api/social/postsGET /api/social/stats

6. 技术架构

REST + JWT;DID(W3C + Ed25519);P2P libp2p + WebRTC;消息 Signal Protocol 端到端加密;统一响应 {code, message, data}

7. 系统特点

  • 去中心化身份,自主可控
  • P2P 无中心消息,E2E 加密
  • DID 签名 / 验签可用于可验证凭证

8. 应用场景

去中心化社交集成、DID 身份签名验证、P2P 加密通讯、社区动态发布。

9. 竞品对比

维度本 API中心化社交
去中心化身份✅ DID
P2P E2E 消息✅ Signal⚠️
无中心服务器✅ libp2p

10. 配置参考

Base URL:http://localhost:3000/api(ChainlessChain 系统);Authorization: Bearer <token>;P2P 需本地节点在线(见 CLI chainlesschain p2p peers)。

11. 性能指标

DID 签名 / 验签为本地密码学运算;P2P 消息延迟取决于 mesh / 中继;联系人 / 动态查询分页。

12. 测试覆盖

端点契约 + CLI 对应命令;DID 签名验签、P2P 投递、好友请求由后端测试覆盖。

13. 安全考虑

  • 接口需 JWT;DID 私钥仅本地 / 硬件密钥内
  • P2P 消息 Signal Protocol 端到端加密
  • 签名验证防伪造
  • 错误码 5001–5007

14. 故障排除

现象错误码处理
DID 创建失败5001重试 / 检查密钥环境
DID 不存在5002核对 DID
签名验证失败5003检查消息 / 公钥
联系人不存在5004先添加联系人
好友请求已存在5005勿重复发送
P2P 节点不可达5006确认对端在线(p2p peers
消息加密失败5007检查会话密钥

15. 关键文件

资源说明
/api/did* /api/social* /api/p2p/send社交 REST API
CLI chainlesschain did/p2p/social对应命令
libp2p + Signal ProtocolP2P / E2E

16. 使用示例

见正文各端点请求示例与「CLI对应命令」(chainlesschain did create/signp2p sendsocial contact/friend/post)。

17. 相关文档

基于 MIT 许可发布