Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: anychain design doc #282

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,35 @@ public blockchains including Bitcoin, Ethereum, Tron, Filecoin, etc.
* hex
* rand


### Functions

* Build raw unsigned transactions for different blockchains according to parameters taken from the user of this library

* Build signed transactions for different blockchains by merging the raw transaction and the corresponding signature
taken from the user of this library

### Design Doc

[Design Principles for AnyChain Wallet SDK](docs/design-en.md)

### Build the source

cargo build --release

## Crates

| Name | Description | Crates.io | Documentation |
|--------------------|------------------------|--------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| [`anychain-core`] | Core types and traits. | [![Crates.io](https://img.shields.io/crates/v/anychain-core)][anychain-core] | [![Documentation](https://shields.io/docsrs/anychain-core)][anychain-core-docs] |
| [`anychain-kms`] | BIP32、BIP39. | [![Crates.io](https://img.shields.io/crates/v/anychain-kms)][anychain-kms] | [![Documentation](https://shields.io/docsrs/anychain-kms)][anychain-kms-docs] |
| [`anychain-bitcoin`] | Bitcoin impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-bitcoin)][anychain-bitcoin] | [![Documentation](https://shields.io/docsrs/anychain-bitcoin)][anychain-bitcoin-docs] |
| Name | Description | Crates.io | Documentation |
|-----------------------|------------------------|--------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| [`anychain-core`] | Core types and traits. | [![Crates.io](https://img.shields.io/crates/v/anychain-core)][anychain-core] | [![Documentation](https://shields.io/docsrs/anychain-core)][anychain-core-docs] |
| [`anychain-kms`] | BIP32、BIP39. | [![Crates.io](https://img.shields.io/crates/v/anychain-kms)][anychain-kms] | [![Documentation](https://shields.io/docsrs/anychain-kms)][anychain-kms-docs] |
| [`anychain-bitcoin`] | Bitcoin impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-bitcoin)][anychain-bitcoin] | [![Documentation](https://shields.io/docsrs/anychain-bitcoin)][anychain-bitcoin-docs] |
| [`anychain-ethereum`] | Ethereum impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-ethereum)][anychain-ethereum] | [![Documentation](https://shields.io/docsrs/anychain-ethereum)][anychain-ethereum-docs] |
| [`anychain-filecoin`] | Filecoin impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-filecoin)][anychain-filecoin] | [![Documentation](https://shields.io/docsrs/anychain-filecoin)][anychain-filecoin-docs] |
| [`anychain-tron`] | Tron impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-tron)][anychain-tron] | [![Documentation](https://shields.io/docsrs/anychain-tron)][anychain-tron-docs] |
| [`anychain-ripple`] | Ripple impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-ripple)][anychain-ripple] | [![Documentation](https://shields.io/docsrs/anychain-ripple)][anychain-ripple-docs] |
| [`anychain-tron`] | Tron impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-tron)][anychain-tron] | [![Documentation](https://shields.io/docsrs/anychain-tron)][anychain-tron-docs] |
| [`anychain-ripple`] | Ripple impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-ripple)][anychain-ripple] | [![Documentation](https://shields.io/docsrs/anychain-ripple)][anychain-ripple-docs] |
| [`anychain-polkadot`] | Polkadot impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-polkadot)][anychain-polkadot] | [![Documentation](https://shields.io/docsrs/anychain-polkadot)][anychain-polkadot-docs] |
| [`anychain-solana`] | Solana impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-solana)][anychain-solana] | [![Documentation](https://shields.io/docsrs/anychain-solana)][anychain-solana-docs] |
| [`anychain-solana`] | Solana impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-solana)][anychain-solana] | [![Documentation](https://shields.io/docsrs/anychain-solana)][anychain-solana-docs] |
| [`anychain-ton`] | Ton impl. | [![Crates.io](https://img.shields.io/crates/v/anychain-ton)][anychain-ton] | [![Documentation](https://shields.io/docsrs/anychain-solana)][anychain-solana-docs] |

## Supported Chains
- Bitcoin
Expand Down Expand Up @@ -81,7 +84,8 @@ Feel free to join anychain sdk [Telegram](https://t.me/anychain_sdk) for discuss
[`anychain-tron`]: https://github.com/0xcregis/anychain/tree/main/anychain-tron
[`anychain-ripple`]: https://github.com/0xcregis/anychain/tree/main/anychain-ripple
[`anychain-polkadot`]: https://github.com/0xcregis/anychain/tree/main/anychain-polkadot
[`anychain-solana`]:https://github.com/0xcregis/anychain-solana
[`anychain-solana`]: https://github.com/0xcregis/anychain-solana
[`anychain-ton`]: https://github.com/0xcregis/anychain-ton
[anychain-core]: https://crates.io/crates/anychain-core
[anychain-kms]: https://crates.io/crates/anychain-kms
[anychain-bitcoin]: https://crates.io/crates/anychain-bitcoin
Expand All @@ -91,6 +95,7 @@ Feel free to join anychain sdk [Telegram](https://t.me/anychain_sdk) for discuss
[anychain-ripple]: https://crates.io/crates/anychain-ripple
[anychain-polkadot]: https://crates.io/crates/anychain-polkadot
[anychain-solana]: https://crates.io/crates/anychain-solana
[anychain-ton]: https://crates.io/crates/anychain-ton
[anychain-core-docs]: https://docs.rs/anychain-core
[anychain-kms-docs]: https://docs.rs/anychain-kms
[anychain-bitcoin-docs]: https://docs.rs/anychain-bitcoin
Expand All @@ -100,4 +105,5 @@ Feel free to join anychain sdk [Telegram](https://t.me/anychain_sdk) for discuss
[anychain-ripple-docs]: https://docs.rs/anychain-ripple
[anychain-polkadot-docs]: https://docs.rs/anychain-polkadot
[anychain-solana-docs]: https://docs.rs/anychain-solana
[anychain-ton-docs]: https://docs.rs/anychain-ton
[license]: https://github.com/0xcregis/anychain/blob/main/LICENSE
146 changes: 146 additions & 0 deletions docs/design-cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# 设计 AnyChain Wallet SDK 的出发点

1. 支持市值 TVL Top100 Token 的转账交易,包括:BTC、ETH、USDT、BNB、SOL、USDC、TON 等。相应地,需要接入的区块链包括:比特币、以太坊、索拉纳,以及各条链上的 L2 网络。
2. 实现跨平台编译,确保跨平台兼容性。考虑到源代码兼容性和工具链兼容性,编程语言选择限定为 C/C++/Rust。

## 公链的特性抽象

### 大多数公链需要支持的核心特性

- PublicKey(公钥)
- PrivateKey(私钥)
- Address(地址)
- Amount(金额)
- Transaction(交易)
- Network(网络)
- Format(格式)

anychain-core 作为一个全面的抽象 Trait,定义了以下通用方法:

```rust
pub trait PublicKey {
fn from_private_key(private_key: &Self::PrivateKey) -> Self;
fn to_address(&self, format: &Self::Format) -> Result<Self::Address, AddressError>;
}

pub trait Address {
fn from_private_key(private_key: &Self::PrivateKey, format: &Self::Format) -> Result<Self, AddressError>;
fn from_public_key(public_key: &Self::PublicKey, format: &Self::Format) -> Result<Self, AddressError>;
}

pub trait Amount {}

pub trait Format {}

pub trait Transaction {
fn new(parameters: &Self::TransactionParameters) -> Result<Self, TransactionError>;
fn sign();
fn from_bytes();
fn to_bytes(&self);
fn to_transaction_id(&self);
}
```

### 各条链实现 Trait 的具体方法

以 anychain-tron 的 TronAddress 为例:

```rust
pub struct TronAddress([u8; 21]);

impl Address for TronAddress {
type Format = TronFormat;
type PrivateKey = TronPrivateKey;
type PublicKey = TronPublicKey;

fn from_private_key(
private_key: &Self::PrivateKey,
format: &Self::Format
) -> Result<Self, AddressError> {
todo!()
}

fn from_public_key(
public_key: &Self::PublicKey,
format: &Self::Format
) -> Result<Self, AddressError> {
todo!()
}
}
```

通过引入 anychain-core 的抽象层和 anychain-bitcoin、anychain-tron 等链的具体实现,上层应用可以使用统一的代码和接口来调用 anychain SDK。

## 跨平台编译和技术栈

| 平台 | 目标文件格式 |
| --- | --- |
| iOS | .a(静态库) |
| Android | .so(共享对象) |
| Web/Wasm | .wasm(WebAssembly) |
| Windows | .dll(动态链接库) |
| macOS | .dylib(动态库) |
| 嵌入式设备 | ELF(可执行与可链接格式) |
| 可信执行环境(TEE) | .eif(加密镜像文件) |

### 以 iOS 平台为例,其调用层级结构

```
+-------------------+
| iOS Application |
+-------------------+
|
| Link
v
+-------------------+
| C Library (.dylib)|
+-------------------+
|
| FFI
v
+-------------------+
| Rust Library |
+-------------------+
|
| Compile
v
+-------------------+
| Rust Source Code |
+-------------------+
```

### 以 iOS 平台编译为例,其实现步骤包括:

1. 创建 Rust 库
2. 在 Rust 中使用 FFI(外部函数接口)
3. 定义 C-ABI 函数
4. 编译库文件
5. 将库文件链接到 iOS 应用
6. 处理数据类型
7. 测试和调试

### 最终编译目标的存储空间占用

| **平台/格式** | **文件名** | **大小** |
| --- | --- | --- |
| Docker 镜像 | enclave-server | < 17M |
| WebAssembly 包 | anychain_wasm_bg.wasm | 81K |
| STM32 | ROM | < 10M |
| iOS | anychain-ethereum-cli | 7.4M |

相比之下,web3.js/node_modules 需要引入高达 29M 的第三方包

## Anychain-KMS

### Anychain 基于 BIP32/BIP39 标准提供的工具函数集合

- 私钥(Private Key)
- 公钥(Public Key)
- 助记词(Mnemonic)
- 种子(Seed)
- 扩展密钥(Extended Key)
- 扩展私钥(Extended Private Key)
- 扩展公钥(Extended Public Key)
- 派生路径(Derivation Path)

这些工具函数用于支持各条区块链的钱包生成。
146 changes: 146 additions & 0 deletions docs/design-en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Design Principles for AnyChain Wallet SDK

1. Support transfer transactions for the top 100 tokens by market cap and TVL, including: BTC, ETH, USDT, BNB, SOL, USDC, TON, etc. Correspondingly, the blockchains to be integrated include: Bitcoin, Ethereum, Solana, as well as L2 networks on various chains.
2. Implement cross-platform compilation for wide compatibility. Language choice limited to C/C++/Rust due to source code and toolchain considerations.

## Abstraction of Public Chain Features

### Core Features Most Public Chains Need to Support

- PublicKey
- PrivateKey
- Address
- Amount
- Transaction
- Network
- Format

anychain-core, as a comprehensive abstract Trait, defines the following common methods:

```rust
pub trait PublicKey {
fn from_private_key(private_key: &Self::PrivateKey) -> Self;
fn to_address(&self, format: &Self::Format) -> Result<Self::Address, AddressError>;
}

pub trait Address {
fn from_private_key(private_key: &Self::PrivateKey, format: &Self::Format) -> Result<Self, AddressError>;
fn from_public_key(public_key: &Self::PublicKey, format: &Self::Format) -> Result<Self, AddressError>;
}

pub trait Amount {}

pub trait Format {}

pub trait Transaction {
fn new(parameters: &Self::TransactionParameters) -> Result<Self, TransactionError>;
fn sign();
fn from_bytes();
fn to_bytes(&self);
fn to_transaction_id(&self);
}
```

### Trait Implementation for Various Chains

Let's take TronAddress from anychain-tron as an example:

```rust
pub struct TronAddress([u8; 21]);

impl Address for TronAddress {
type Format = TronFormat;
type PrivateKey = TronPrivateKey;
type PublicKey = TronPublicKey;

fn from_private_key(
private_key: &Self::PrivateKey,
format: &Self::Format
) -> Result<Self, AddressError> {
todo!()
}

fn from_public_key(
public_key: &Self::PublicKey,
format: &Self::Format
) -> Result<Self, AddressError> {
todo!()
}
}
```

By introducing the abstraction layer of anychain-core and the specific implementations of chains such as anychain-bitcoin and anychain-tron, upper-level applications can use unified code and interfaces to call the anychain SDK.

## Cross-Platform Compilation

| Platform | Target File Format |
| --- | --- |
| iOS | .a (Static Library) |
| Android | .so (Shared Object) |
| Web/Wasm | .wasm (WebAssembly) |
| Windows | .dll (Dynamic-Link Library) |
| macOS | .dylib (Dynamic Library) |
| Embedded Devices | ELF (Executable and Linkable Format) |
| Trusted Execution Environment (TEE) | .eif (Encrypted Image File) |

### iOS platform calling hierarchy

```
+-------------------+
| iOS Application |
+-------------------+
|
| Link
v
+-------------------+
| C Library (.dylib)|
+-------------------+
|
| FFI
v
+-------------------+
| Rust Library |
+-------------------+
|
| Compile
v
+-------------------+
| Rust Source Code |
+-------------------+
```

### iOS Platform Compilation Steps:

1. Creating a Rust library
2. Using FFI (Foreign Function Interface) in Rust
3. Defining C-ABI functions
4. Compiling the library file
5. Linking the library file to the iOS application
6. Handling data types
7. Testing and debugging

### Compiled Target Sizes

| **Platform/Format** | **File Name** | **Size** |
| --- | --- | --- |
| Docker image | enclave-server | < 17M |
| WebAssembly package | anychain_wasm_bg.wasm | 81K |
| STM32 | ROM | < 10M |
| iOS | anychain-ethereum-cli | 7.4M |

In comparison, web3.js/node_modules requires the inclusion of third-party packages up to 29M

## Anychain-KMS

### Anychain's BIP32/BIP39-based utility functions

- Private Key
- Public Key
- Mnemonic
- Seed
- Extended Key
- Extended Private Key
- Extended Public Key
- Derivation Path

These utility functions are used to support wallet generation for various blockchains.
Loading