从零开发一个支持以太坊的云笔记:Electron +区块链全栈实战指南

·

关键词:区块链项目实战、去中心化云笔记、Electron桌面开发、以太坊智能合约、Web3.js、Node.js全栈、Solidity教程

项目亮点与核心技术栈

👉 点击深入了解如何在不写一行 C++ 的前提下完成跨平台桌面应用

目录速览

  1. 前期准备:安装 Node.js、MetaMask、Hardhat
  2. 快速起航:15 分钟跑通示例仓库
  3. 去中心化云笔记核心模块拆解
  4. 以太坊智能合约设计与部署
  5. Electron 绑定区块链:Web3.js 调用最佳实践
  6. 本地加密 & 链上脱敏:保护隐私的两种思路
  7. 打包与发布:GitHub Release + Auto Update
  8. FAQ:常见坑与官方解答

前期准备

  1. 系统依赖

    • Node.js ≥ 18
    • Git ≥ 2.31
    • pnpm 或 npm(推荐 pnpm 节省磁盘)
  2. 区块链工具

    • MetaMask(浏览器钱包)
    • Ganache CLI 或 Hardhat 本地测试链
  3. IDE & 调试

    • VSCode + Solidity、ESLint、Prettier 三巨头插件
    • Chrome DevTools(对接 Electron)

💡 首次运行提示 eth_chainId 报错?大概率是测试链端口冲突,关闭占用 8545 端口的其他进程即可。


快速起航

下文所有命令均以 macOS bash 为例,Windows PowerShell 将 export 改为 set 即可。
# 克隆官方模板
git clone https://github.com/devarena/eth-note-demo.git
cd eth-note-demo
pnpm install

# 启动 Hardhat 测试链
npx hardhat node

# 另开终端,部署合约
npx hardhat run scripts/deploy.ts --network localhost

# 运行 Electron 本地开发环境
pnpm electron:dev

浏览器里可爱卡通猫头鹰即是「区块链链接成功」的标志。当看到 Chain ID: 0x7a69 说明你已成功对接本地链。


云笔记核心功能拆解

1. 数据模型

2. 新建、编辑、恢复流程

  1. 用户点击“新建笔记”,触发 Electron renderer 将内容加密后写入本地。
  2. 通过 Node.js IPC 主进程把 content + metadata 打包上传至 IPFS Pinning 服务,返回 CID。
  3. renderer 再次调用 Solidity 方法把 CID 摘要存入 ETH。
  4. 恢复账本时,先通过 getPastEvents("NoteCreated") 拿到 CID,再拉取 IPFS 数据。

3. 前端组件示例(伪代码)

// services/notes.ts
export async function createNote(title: string, body: string) {
  const cid = await ipfsAdd({ title, body })
  const tx = await noteContract.createNote(title, cid)
  await tx.wait(1)
}

以太坊智能合约设计

pragma solidity ^0.8.23;

contract EthNote {
  event NoteCreated(uint32 indexed id, address owner, string title, string ipfsCid);
  event NoteUpdated(uint32 indexed id, string ipfsCid);

  struct Note {
    address owner;
    string title;
    string ipfsCid;
    uint32 timestamp;
  }

  Note[] public notes;

  mapping(address => uint32[]) public ownerToIds;

  function createNote(string calldata title, string calldata cid) external {
    uint32 id = uint32(notes.length);
    notes.push(Note(msg.sender, title, cid, uint32(block.timestamp)));
    ownerToIds[msg.sender].push(id);
    emit NoteCreated(id, msg.sender, title, cid);
  }

  function updateNote(uint32 id, string calldata cid) external {
    require(notes[id].owner == msg.sender);
    notes[id].ipfsCid = cid;
    emit NoteUpdated(id, cid);
  }
}

安全点


Electron 调用 Web3.js 的最佳姿势

规避暴露私钥、避免渲染进程直接操作 Web3。

Scene 1:预加载脚本隔离

// preload.js
const { ipcRenderer } = require('electron')
window.electronAPI = {
  invokeWeb3: (method, params) => ipcRenderer.invoke('web3-action', { method, params })
}

Scene 2:主进程管道

// main.js
const { ipcMain } = require('electron')
const { ethers } = require('ethers')

ipcMain.handle('web3-action', async (_, { method, params }) => {
  const provider = new ethers.JsonRpcProvider('http://localhost:8545')
  const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider)
  switch (method) {
    case 'createNote':
      const contract = new ethers.Contract(contractAddress, abi, signer)
      return await contract.createNote(...params)
  }
})

优点:


本地加密 + 链上脱敏

你可能担忧:

  1. 敏感笔记被 IPFS 永久保存怎么办?
  2. 合约公开可见怎么办?

解决方案:

代码片段(伪):

import * as crypto from 'crypto'
const secretKey = Buffer.from(env.LOCAL_KEY_HEX, 'hex')

export function encrypt(data: string): string {
  const iv = crypto.randomBytes(16)
  const cipher = crypto.createCipheriv('aes-256-cbc', secretKey, iv)
  return iv.toString('hex') + ':' + cipher.update(data, 'utf8', 'hex') + cipher.final('hex')
}

该方式确保即使 IPFS 被爬取,也无法还原笔记原文。

👉 查看实测 Gas Cost 如何减少 60% ,只需优化参数顺序


打包 & 发布

利用 electron-builder 一条命令搞定三种系统:

pnpm electron:build

生成物位于 ./dist 目录,文件名包含 mac-x64win-x64linux-x64 三类。
结合 GitHub Actions 设置 Release,还能自动化标签,节省困苦的打包夜。


FAQ

Q1:为何选择 Electron,而不用纯 Web?

A:桌面端可直接调用 Node.js 底层能力,文件系统读写无需额外权限弹窗,加密缓存更顺滑。

Q2:Gas 费太高怎么办?

A:采用 Sepolia 测试链 + 官方水龙头做开发,等到用户量上去再迁移 Layer2 Rollup(如 Optimism),单笔 gas <0.001 美元。

Q3:智能合约升级会带来兼容性问题吗?

A:通过 Proxy Pattern(OpenZeppelin Upgrades)实现合约逻辑的无缝迭代,前端只需替换新的 ABI 即可。

Q4:如何让普通用户免去安装 Chrome Pendant 插件?

A:方案一:发布预置私钥的 托管钱包(教育场景区有限);方案二:提供 In-App 钱包扫码签名,结合 WalletConnect 扫码即可。

Q5:IPFS 被 HashCloak 等爬取导致数据泄露?

A:走 专用私有网关 并启用 身份鉴权,或在 CID 前加 onion 地址代理,确保只能被你指定节点服务抓取。


结语

凭借 Electron + 以太坊智能合约 的组合,你可以在一周之内打造出一款真正属于用户、永不宕机、真正可验证的 去中心化云笔记.
持续提升 gas 效率、优化 UI/UX、考虑多链拓展,你的下一个产品雏形已在酝酿。祝编码愉快!