Skip to content

Commit 2a3b01d

Browse files
committed
Initial Commit
1 parent 245668a commit 2a3b01d

28 files changed

+2736
-3
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dist/
2+
myenv/
3+
*/__pycache__/
4+
*.pyc
5+
*.pyo
6+
*.venv
7+
examples/*.json
8+
*.egg-info/
9+
build/

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 HeiYa
3+
Copyright (c) 2024 GaoWei Chang
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.en.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Cross-Platform Identity Authentication and End-to-End Encrypted Communication Technology Based on DID
2+
3+
**Author**: Chang Gaowei
4+
**Email**: chgaowei@gmail.com
5+
**Website**: [pi-unlimited.com](http://pi-unlimited.com)
6+
7+
## Abstract
8+
9+
This project is an open-source SDK implementation based on Decentralized Identifier (DID) and end-to-end encrypted communication technology. For technical details, refer to the [Technical White Paper: A Cross-Platform Identity Authentication and End-to-End Encrypted Communication Technology Based on DID](https://egp0uc2jnx.feishu.cn/wiki/JyaIwTwngiWi9qkJjjycI4XcnXe?from=from_copylink). With the didall open-source project, any intelligent agent or server can connect to the DID server, register its DID, accept connections from other users, connect to other users, and engage in end-to-end encrypted communication.
10+
11+
## Features
12+
13+
- **Cross-Platform Identity Authentication**: Achieves identity interoperability across different platforms using DID.
14+
- **End-to-End Encrypted Communication**: Uses ECDHE for short-term key agreement to ensure communication security.
15+
- **Efficient and Secure**: Simplifies the identity verification process, ensuring data confidentiality and integrity.
16+
17+
### Installation
18+
19+
The latest version has been removed from PyPI, so install directly:
20+
21+
```bash
22+
pip install didall
23+
```
24+
25+
### Usage
26+
27+
After installing the didall library, you can run the sample code in the examples directory to generate DID files for Alice and Bob, save Alice's DID file to the DID server, and then have Bob connect to Alice's DID for end-to-end encrypted communication.
28+
29+
1. Generate two DID documents, alice.json and bob.json, save them to the specified files, and register them with the DID server:
30+
```bash
31+
python sample_did.py alice.json
32+
python sample_did.py bob.json
33+
```
34+
35+
2. Start Alice's demo:
36+
```bash
37+
python sample_alice.py alice.json
38+
```
39+
40+
3. Start Bob's demo:
41+
```bash
42+
python sample_bob.py bob.json
43+
```
44+
45+
By checking the logs, you can see that Alice and Bob have successfully connected and engaged in end-to-end encrypted communication.
46+
47+
## Contributing
48+
49+
Contributions to this project are welcome. Please read the contribution guidelines before submitting a pull request.
50+
51+
## License
52+
53+
This project is open-sourced under the MIT License. For more details, please refer to the LICENSE file.
54+
55+
## Packaging and Uploading (update the version number in setup.py first)
56+
57+
```bash
58+
python setup.py sdist bdist_wheel
59+
twine upload dist/*
60+
```

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
1-
# didall
2-
Technical White Paper: A Cross-Platform Identity Authentication and End-to-End Encrypted Communication Technology Based on DID
1+
# 基于DID的跨平台身份认证和端到端加密通信技术
2+
3+
**作者**: 常高伟
4+
**邮箱**: chgaowei@gmail.com
5+
**官网**: [pi-unlimited.com](http://pi-unlimited.com)
6+
7+
## 摘要
8+
9+
本项目是基于去中心化标识符(DID)和端到端加密通信技术的开源SDK实现,技术细节详见[技术白皮书:一种基于DID的跨平台身份认证和端到端加密通信技术](https://egp0uc2jnx.feishu.cn/wiki/JyaIwTwngiWi9qkJjjycI4XcnXe?from=from_copylink)
10+
借助didall开源项目,任意一个智能体或者服务端都可以连接到did server,注册自己的did,接收其他用户连接,也可以连接其他用户,并且相互之间进行端到端的加密通信。
11+
12+
## 特点
13+
14+
- **跨平台身份认证**:通过DID实现不同平台间的身份互操作性。
15+
- **端到端加密通信**:使用ECDHE进行短期密钥协商,保证通信的安全性。
16+
- **高效和安全**:简化身份验证过程,确保数据的保密性和完整性。
17+
18+
### 安装
19+
20+
最新版本已删除pypi,直接安装即可:
21+
22+
```bash
23+
pip install didall
24+
```
25+
26+
### 运行
27+
28+
在安装完didall库后,可以运行examples目录下的sample代码,可以生成alice和bob的did文件,并且将alice的did文件保存到did server,然后bob可以连接alice的did,进行端到端的加密通信。
29+
30+
1. 生成两个did文档alice.json和bob.json,保存到指定文件中,并注册到did server
31+
```bash
32+
python sample_did.py alice.json
33+
python sample_did.py bob.json
34+
```
35+
36+
2. 启动alice的demo
37+
```bash
38+
python sample_alice.py alice.json
39+
```
40+
41+
3. 启动bob的demo
42+
```bash
43+
python sample_bob.py bob.json
44+
```
45+
46+
可以通过日志看到,alice和bob成功连接,并且进行端到端的加密通信。
47+
48+
## 贡献
49+
50+
欢迎对本项目进行贡献。请在提交Pull Request之前阅读贡献指南。
51+
52+
## 许可证
53+
54+
本项目基于MIT许可证开源。详细信息请参阅LICENSE文件。
55+
56+
57+
## 打包上传(先更改setup.py中版本号)
58+
59+
```bash
60+
python setup.py sdist bdist_wheel
61+
twine upload dist/*
62+
```

didall/__init__.py

Whitespace-only changes.

didall/didallclient.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Cross-Platform Identity Authentication and End-to-End Encrypted Communication Technology Based on DID
2+
# Author: GaoWei Chang
3+
# Email: chgaowei@gmail.com
4+
# Website: http://pi-unlimited.com
5+
#
6+
# This project is open-sourced under the MIT License. For details, please see the LICENSE file.
7+
8+
9+
import logging
10+
from typing import Tuple
11+
import aiohttp
12+
from didall.utils.crypto_tool import get_pem_from_private_key
13+
from didall.utils.did_generate import did_generate
14+
import requests
15+
16+
class DIDAllClient:
17+
def __init__(self, did_service_url: str, api_key: str):
18+
self.api_key = api_key
19+
self.did_service_url = did_service_url
20+
21+
def generate_did_document(self, communication_service_endpoint: str, router_did: str = "") -> Tuple[str, str, str]:
22+
"""
23+
Generate DID document without registering to DID service
24+
25+
Args:
26+
communication_service_endpoint (str): Communication service endpoint for DID document.
27+
router (str, optional): Router's DID, default is empty string.
28+
29+
Returns:
30+
Tuple[str, str, str]: A tuple containing private key in PEM format, DID string, and DID document JSON string.
31+
"""
32+
private_key, _, did, did_document_json = did_generate(communication_service_endpoint, router_did)
33+
34+
# Convert private key to PEM format
35+
private_key_pem = get_pem_from_private_key(private_key)
36+
37+
return private_key_pem, did, did_document_json
38+
39+
async def generate_register_did_document(self, communication_service_endpoint: str, router_did: str = "") -> Tuple[str, str, str]:
40+
"""
41+
Register DID document to DID service.
42+
43+
This function asynchronously generates DID and corresponding DID document, and registers it to the configured DID service.
44+
It uses the aiohttp library to send asynchronous HTTP requests.
45+
46+
Args:
47+
communication_service_endpoint (str): Communication service endpoint for DID document.
48+
router_did (str, optional): Router's DID, default is empty string.
49+
50+
Returns:
51+
Tuple[str, str, str]: A tuple containing private key in PEM format, DID string, and DID document JSON string.
52+
If registration fails, it will return three None values.
53+
"""
54+
55+
# Generate private key, public key, DID and DID document
56+
private_key, _, did, did_document_json = did_generate(communication_service_endpoint, router_did)
57+
58+
# Convert private key to PEM format
59+
private_key_pem = get_pem_from_private_key(private_key)
60+
61+
# Prepare request headers
62+
headers = {
63+
"Content-Type": "application/text",
64+
"Authorization": f"Bearer {self.api_key}"
65+
}
66+
67+
# Ensure correct request URL
68+
request_url = f"{self.did_service_url}/v1/did" # Make sure the URL is correct
69+
70+
# Use aiohttp to send asynchronous POST request
71+
async with aiohttp.ClientSession() as session:
72+
async with session.post(request_url, headers=headers, data=did_document_json) as response:
73+
if response.status == 200:
74+
return private_key_pem, did, did_document_json
75+
else:
76+
response_text = await response.text()
77+
logging.error(f"Failed to create DID document: {response.status} {response_text}")
78+
return None, None, None
79+
80+
async def get_did_document(self, did: str):
81+
# Prepare request headers
82+
headers = {
83+
"Accept": "application/text",
84+
"Authorization": f"Bearer {self.api_key}"
85+
}
86+
87+
# Construct complete request URL
88+
request_url = f"{self.did_service_url}/v1/did/{did}"
89+
90+
# Use aiohttp to send asynchronous GET request
91+
async with aiohttp.ClientSession() as session:
92+
async with session.get(request_url, headers=headers) as response:
93+
if response.status == 200:
94+
return await response.text()
95+
else:
96+
response_text = await response.text()
97+
logging.error(f"Failed to retrieve DID document: {response.status} {response_text}")
98+
return None
99+
100+
def register_did_document_sync(self, communication_service_endpoint: str, router: str = ""):
101+
# Generate private key, public key, DID and DID document
102+
private_key, _, did, did_document_json = did_generate(communication_service_endpoint, router)
103+
104+
# Convert private key to PEM format
105+
private_key_pem = get_pem_from_private_key(private_key)
106+
107+
# Prepare request headers
108+
headers = {
109+
"Content-Type": "application/text",
110+
"Authorization": f"Bearer {self.api_key}"
111+
}
112+
113+
# Use requests to send synchronous POST request
114+
response = requests.post(f"{self.did_service_url}/v1/did", headers=headers, data=did_document_json)
115+
if response.status_code == 200:
116+
return private_key_pem, did, did_document_json
117+
else:
118+
logging.error(f"Failed to create DID document: {response.status_code} {response.text}")
119+
return None, None, None
120+
121+
def get_did_document_sync(self, did: str):
122+
# Prepare request headers
123+
headers = {
124+
"Accept": "application/text",
125+
"Authorization": f"Bearer {self.api_key}"
126+
}
127+
128+
# Send synchronous GET request
129+
response = requests.get(f"{self.did_service_url}/v1/did/{did}", headers=headers)
130+
if response.status_code == 200:
131+
return response.text
132+
else:
133+
logging.error(f"Failed to retrieve DID document: {response.status_code} {response.text}")
134+
return None

0 commit comments

Comments
 (0)