From 51391224cc7d9408b394aeb3f02576dd184ad31c Mon Sep 17 00:00:00 2001 From: loki-cmu Date: Mon, 28 Oct 2024 21:09:48 +0800 Subject: [PATCH 1/2] docs: add supported blockchains --- README.md | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0c7932a..bc86fb3 100644 --- a/README.md +++ b/README.md @@ -36,17 +36,18 @@ public blockchains including Bitcoin, Ethereum, Tron, Filecoin, etc. ## 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 @@ -81,7 +82,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 @@ -91,6 +93,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 @@ -100,4 +103,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 From 39193de4c045cf5125e9727f73bd3f5569e6495c Mon Sep 17 00:00:00 2001 From: loki-cmu Date: Tue, 29 Oct 2024 00:44:45 +0800 Subject: [PATCH 2/2] docs: Design Principles for AnyChain Wallet SDK --- README.md | 4 +- docs/design-cn.md | 146 ++++++++++++++++++++++++++++++++++++++++++++++ docs/design-en.md | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 docs/design-cn.md create mode 100644 docs/design-en.md diff --git a/README.md b/README.md index bc86fb3..2f49bff 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,6 @@ 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 @@ -29,6 +28,9 @@ public blockchains including Bitcoin, Ethereum, Tron, Filecoin, etc. * 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 diff --git a/docs/design-cn.md b/docs/design-cn.md new file mode 100644 index 0000000..c6447f6 --- /dev/null +++ b/docs/design-cn.md @@ -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; +} + +pub trait Address { + fn from_private_key(private_key: &Self::PrivateKey, format: &Self::Format) -> Result; + fn from_public_key(public_key: &Self::PublicKey, format: &Self::Format) -> Result; +} + +pub trait Amount {} + +pub trait Format {} + +pub trait Transaction { + fn new(parameters: &Self::TransactionParameters) -> Result; + 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 { + todo!() + } + + fn from_public_key( + public_key: &Self::PublicKey, + format: &Self::Format + ) -> Result { + 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) + +这些工具函数用于支持各条区块链的钱包生成。 \ No newline at end of file diff --git a/docs/design-en.md b/docs/design-en.md new file mode 100644 index 0000000..2e9bab3 --- /dev/null +++ b/docs/design-en.md @@ -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; +} + +pub trait Address { + fn from_private_key(private_key: &Self::PrivateKey, format: &Self::Format) -> Result; + fn from_public_key(public_key: &Self::PublicKey, format: &Self::Format) -> Result; +} + +pub trait Amount {} + +pub trait Format {} + +pub trait Transaction { + fn new(parameters: &Self::TransactionParameters) -> Result; + 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 { + todo!() + } + + fn from_public_key( + public_key: &Self::PublicKey, + format: &Self::Format + ) -> Result { + 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. \ No newline at end of file