难点:多链钱包大文件拆分
项目难点:expo-wallet-core iOS Services 层
问题
早期把多链钱包能力(创建/导入、签名、各链交易构建与编译等)都堆在 一个 Swift 文件 里,带来三类问题:
- AI 协作成本高 — 改任意一条链的逻辑,往往要把整份大文件塞进上下文;token 占用大、响应慢,还容易漏改或误改其他链代码。
- 维护与定位慢 — 多链逻辑交织,找 bug、做 review、理解调用关系都要在大文件里翻。
- 扩展性差 — 新增一条链(如 Sui、Aptos)只能继续往同一个文件里加,冲突多、边界不清。
当前 Services 目录合计约 7500+ 行、27 个 Swift 文件;若仍保持单文件,体量已经很难维护。
解决:按职责 + 按链拆分
重构后的结构大致如下:
Services/
├── WalletService.swift # 钱包基础:助记词、私钥、地址、账户生成
├── SignService.swift # 通用曲线签名
├── TransactionSignatureUtils.swift # 跨链签名编译工具
├── BitcoinAddressService.swift # Bitcoin 地址专项
│
├── EthereumTransactionService.swift + EthereumTransactionTypes.swift
├── BitcoinTransactionService.swift + BitcoinTransactionType.swift
├── SolanaTransactionService.swift + SolanaTransactionTypes.swift
├── CosmosTransactionService.swift + CosmosTransactionType.swift
├── Tron / Sui / Aptos / Cardano / Ripple / Polkadot ...(同样 Service + Types 配对)
│
└── *Test.swift # 各链测试/调试
分层职责:
| 层级 | 职责 | 示例 |
|---|---|---|
| Module 入口 | JS/TS ↔ Native 路由,不含业务 | ExpoWalletCoreModule.swift |
| WalletService | 与链无关的钱包能力 | 创建钱包、解析私钥、地址校验 |
| XxxTransactionService | 单链交易:sign / preImageHash / compile | EthereumTransactionService |
| XxxTransactionTypes | 单链数据结构、协议、枚举 | EthereumTransactionTypes |
| Utils | 可复用的签名/地址逻辑 | TransactionSignatureUtils |
ExpoWalletCoreModule 只做薄路由,例如 Ethereum 相关调用直接转到对应 Service:
let key = try WalletService.parsePrivateKey(privateKey)
return try EthereumTransactionService.signEthereumTransaction(
key: key, transactionData: transactionData)
带来的收益
- AI 友好 — 改 Ethereum 只需加载
EthereumTransactionService.swift(~416 行)+EthereumTransactionTypes.swift(~411 行),不必整包 7500 行进上下文。 - 扩展清晰 — 新链接口模式固定:
XxxTransactionTypes+XxxTransactionService+ 在 Module 里注册 AsyncFunction。 - 并行开发 — 不同链可由不同人/不同 AI 会话独立改,合并冲突少。
- 类型与业务分离 — Types 管 Codable/协议,Service 管 WalletCore 调用,职责边界明确。
可写进文档的一句话总结
难点:多链钱包 Native 层最初采用单文件实现,导致上下文过大、AI 协作效率低、扩展困难。
方案:按「钱包基础 / 通用签名 / 单链 Service + Types / Module 路由」拆分 Services 目录,使每条链成为独立、可增量维护的模块。
如果你需要,我可以再帮你整理成:
- 对内技术分享用的 1 页 slide 大纲
- 对外/汇报用的 中英双语版本
- 或 新链接入 checklist(Types → Service → Module 注册三步模板)