什么是以太坊智能合约
以太坊智能合约(Smart Contract)是一种运行在以太坊区块链上的自动执行程序,它无需第三方中介,根据预设的规则和条件,在满足条件时自动完成约定的操作(如转账、数据存储、资产交换等),智能合约是“代码即法律”——一旦部署上链,其逻辑就无法篡改,交易结果由全网共识验证,具有透明、不可篡改、自动执行的核心特性。
以太坊作为全球最大的智能合约平台,通过其虚拟机(EVM)支持开发者使用Solidity、Vyper等编程语言编写合约,为去中心化应用(DApp)提供了底层技术支撑,广泛应用于DeFi(去中心化金融)、NFT、数字身份、供应链管理等领域。
智能合约的核心特点
在举例之前,需先理解智能合约的四大核心特点,这些特点也是其与传统程序的本质区别:
- 自动执行:基于“就…”(If-This-Then-That)的逻辑,无需人工干预;
- 不可篡改:合约部署后,代码和状态记录在区块链上,任何人都无法修改;
- 透明公开:合约代码和交易记录对全网可见,可被审计;
- 去中心化:运行于分布式网络,无单点故障风险,抗审查。
实战举例:一个简单的“数字存证”智能合约
为了更直观地理解智能合约,我们以“数字作品存证合约”为例,展示其从设计到代码实现的全过程,该合约的功能是:允许用户将数字作品(如图片、文章)的哈希值(唯一标识)存储在区块链上,实现版权存证,并支持查询和验证。
合约需求设计
- 功能需求:
- 用户可提交数字作品的哈希值进行存证;
- 用户可查询自己存证的记录;
- 所有人可验证某个哈希值是否已被存证。
- 技术要点:
- 使用
mapping(映射)结构存储哈希值与存证信息的关联; - 使用
struct(结构体)封装存证数据(如存证者地址、时间戳); - 通过
event(事件)记录存证操作,方便前端监听。
- 使用
Solidity代码实现
以下是完整的合约代码(基于Solidity 0.8.x版本,添加了安全注释):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**DigitalCertificateContract
* @dev 数字作品存证合约:允许用户存证作品哈希值,并支持查询与验证
*/
contract DigitalCertificateContract {
// 定义存证信息结构体
struct Certificate {
address owner; // 存证者地址
uint256 timestamp; // 存证时间戳
}
// mapping:作品哈希值 -> 存证信息
// bytes32:作品的哈希值(如SHA-256计算后的32字节值)
// address => 存证者地址,uint256 => 时间戳
mapping(bytes32 => Certificate) public certificates;
// 事件:存证成功时触发,方便前端监听
event CertificateStored(
bytes32 indexed hashValue,
address indexed owner,
uint256 timestamp
);
/**
* @dev 存证数字作品哈希值
* @param _hashValue 作品的哈希值(需由用户自行计算,如通过SHA-256)
*/
function storeCertificate(bytes32 _hashValue) public {
// 检查该哈希值是否已被存证(避免重复存证)
require(certificates[_hashValue].owner == address(0), "Hash already exists");
// 存储存证信息:调用者地址为存证者,当前时间戳
certificates[_hashValue] = Certificate({
owner: msg.sender,
timestamp: block.timestamp
});
// 触发事件
emit CertificateStored(_hashValue, msg.sender, block.timestamp);
}
/**
* @dev 查询哈希值对应的存证信息
* @param _hashValue 作品哈希值
* @return owner 存证者地址,timestamp 存证时间戳
*/
function getCertificate(bytes32 _hashValue) public view returns (address owner, uint256 timestamp) {
Certificate storage cert = certificates[_hashValue];
require(cert.owner != address(0), "Hash not found");
return (cert.owner, cert.timestamp);
}
/**
* @dev 验证哈希值是否已被存证
* @param _hashValue 作品哈希值
* @return bool 是否已存证
*/
function isHashStored(bytes32 _hashValue) public view returns (bool) {
return certificates[_hashValue].owner != address(0);
}
}
代码解析
-
状态变量:
certificates:核心数据结构,通过mapping将作品哈希值(bytes32)与存证信息(Certificate结构体)关联,实现快速查询。Certificate结构体:包含存证者地址(owner)和时间戳(timestamp),记录存证的核心信息。
-
函数:
storeCertificate()