CorvidLabs/swift-algochat
GitHub: CorvidLabs/swift-algochat
该项目是 Algorand 区块链上的端到端加密点对点消息传递 Swift 库,解决去中心化场景下安全、不可篡改通信的问题。
Stars: 1 | Forks: 1
# swift-algochat
[](https://github.com/CorvidLabs/swift-algochat/actions/workflows/macOS.yml)
[](https://github.com/CorvidLabs/swift-algochat/actions/workflows/linux.yml)
[](https://github.com/CorvidLabs/swift-algochat/blob/main/LICENSE)
[](https://github.com/CorvidLabs/swift-algochat/releases)
Algorand 区块链上的加密点对点消息传递。使用 Swift 6 和 async/await 构建。
## 功能
- **端到端加密** - X25519 密钥协商 + ChaCha20-Poly1305
- **前向保密** - 每条消息的临时密钥保护过往消息
- **量子深度防御** - 可选的棘轮 PSK 模式(协议 v1.1)提供混合 X25519 + 预共享密钥加密
- **重放保护** - 针对 PSK 消息的基于计数器的滑动窗口
- **不可篡改消息** - 永久记录在链上
- **去中心化** - 没有中央服务器控制投递
- **双向解密** - 发送者和接收者均可解密消息
- **回复支持** - 带有回复上下文的对话线程
- **生物识别存储** - 使用 Face ID / Touch ID 保护加密密钥
- **跨实现兼容** - 兼容 [ts-algochat](https://github.com/CorvidLabs/ts-algochat)、[rs-algochat](https://github.com/CorvidLabs/rs-algochat)、[py-algochat](https://github.com/CorvidLabs/py-algochat)、[kt-algochat](https://github.com/CorvidLabs/kt-algochat)
- **多平台** - iOS 15+、macOS 12+、tvOS 15+、watchOS 8+、visionOS 1+、Linux
## 安装说明
### Swift Package Manager
将 AlgoChat 添加到您的 `Package.swift` 中:
```
dependencies: [
.package(url: "https://github.com/CorvidLabs/swift-algochat.git", from: "0.1.0")
]
```
然后将依赖项添加到您的 target:
```
.target(
name: "YourApp",
dependencies: [
.product(name: "AlgoChat", package: "swift-algochat")
]
)
```
或者通过 Xcode 添加:
1. File > Add Package Dependencies
2. 输入:`https://github.com/CorvidLabs/swift-algochat.git`
## 快速开始
### 创建聊天客户端
```
import AlgoChat
import Algorand
// Create a new account
let account = try Account()
// Initialize chat client
let chat = try await AlgoChat(network: .testnet, account: account)
```
### 发送消息
```
// Start a conversation
let recipient = try Address(string: "RECIPIENT_ADDRESS_HERE")
let conversation = try await chat.conversation(with: recipient)
// Send a message (wait for confirmation)
try await chat.send("Hello!", to: conversation, options: .confirmed)
// Send with indexer confirmation (guarantees visibility on refresh)
try await chat.send("Hello!", to: conversation, options: .indexed)
// Send a reply
if let lastMessage = conversation.lastReceived {
try await chat.send("Thanks!", to: conversation, options: .replying(to: lastMessage))
}
```
### 获取消息
```
// Get all conversations
let conversations = try await chat.conversations()
// Refresh a specific conversation
let updated = try await chat.refresh(conversation)
// Access messages
for message in updated.messages {
print("\(message.direction): \(message.content)")
}
// Filter by direction
let received = updated.receivedMessages
let sent = updated.sentMessages
```
### 发布您的密钥
允许其他人在您向他们发送消息之前向您发送消息:
```
try await chat.publishKeyAndWait()
```
### PSK 模式(量子深度防御)
PSK 模式在标准 ECDH 加密的基础上增加了一层预共享密钥,为应对未来针对密钥交换的量子攻击提供了深度防御。
```
// Generate and share a PSK exchange URI
let psk = PSKExchangeURI(
address: account.address.description,
psk: Data.random(count: 32),
label: "Alice"
)
let uri = psk.toString()
// Share uri out-of-band: algochat-psk://v1?addr=...&psk=...&label=Alice
// Import a PSK from a received URI
let received = try PSKExchangeURI.parse(uri)
let contact = PSKContact(
address: received.address,
initialPSK: received.psk,
label: received.label
)
// Manage PSK contacts
let pskManager = PSKManager(storage: FilePSKStorage(directory: ".algochat"))
try await pskManager.addContact(contact)
```
## 核心概念
### 加密
消息使用现代密码学原语进行加密:
- **密钥协商**:X25519 椭圆曲线 Diffie-Hellman
- **加密**:ChaCha20-Poly1305 认证加密
- **密钥派生**:带域分离的 HKDF-SHA256
- **前向保密**:每条消息使用全新的临时密钥
### 消息信封格式
**标准模式**(协议 `0x01`):
```
[version: 1][protocol: 1][sender_pubkey: 32][ephemeral_pubkey: 32][nonce: 12][encrypted_sender_key: 48][ciphertext: variable]
```
- **Header 大小**:126 字节
- **最大消息**:882 字节(扣除加密开销后)
**PSK 棘轮模式**(协议 `0x02`):
```
[version: 1][protocol: 2][ratchet_counter: 4][sender_pubkey: 32][ephemeral_pubkey: 32][nonce: 12][encrypted_sender_key: 48][ciphertext: variable]
```
- **Header 大小**:130 字节
- **最大消息**:878 字节(扣除加密开销后)
### 密钥存储
```
// Biometric-protected storage (Apple platforms)
let storage = KeychainKeyStorage()
let chatAccount = try await ChatAccount(account: algorandAccount, storage: storage)
// Password-encrypted file storage (Linux/cross-platform)
let storage = FileKeyStorage(directory: ".algochat", password: "secret")
```
### 发送选项
```
// Fire-and-forget (fastest)
try await chat.send("Hello!", to: conversation)
// Wait for blockchain confirmation
try await chat.send("Hello!", to: conversation, options: .confirmed)
// Wait for indexer (guarantees visibility)
try await chat.send("Hello!", to: conversation, options: .indexed)
```
## CLI 工具
该 package 包含一个交互式命令行界面:
```
swift run algochat
```
## 测试
### 单元测试
```
swift test
```
### 集成测试(LocalNet)
```
# 启动本地 Algorand 网络
algokit localnet start
# 运行集成测试
swift test --filter "LocalnetIntegration"
# 停止 localnet
algokit localnet stop
```
## 环境要求
- Swift 6.0+
- iOS 15.0+ / macOS 12.0+ / tvOS 15.0+ / watchOS 8.0+ / visionOS 1.0+
- Linux(需带有 Swift 6.0+)
- [AlgoKit CLI](https://github.com/algorandfoundation/algokit-cli)(可选,用于 localnet 测试)
## 安全性
请参阅 [SECURITY.md](SECURITY.md) 了解:
- 密码学保证
- 威胁模型
- 密钥管理详情
## License
MIT License - 详见 [LICENSE](LICENSE) 文件。
## 资源
- [swift-algorand](https://github.com/CorvidLabs/swift-algorand) - 适用于 Swift 的 Algorand SDK
- [swift-algokit](https://github.com/CorvidLabs/swift-algokit) - 高级 Algorand 工具包
- [Algorand 开发者门户](https://developer.algorand.org)
标签:Algorand, Swift, 区块链, 即时通讯, 密码学, 手动系统调用, 端到端加密