356 lines
11 KiB
Plaintext
356 lines
11 KiB
Plaintext
// NAC跨链桥接智能合约
|
||
// 使用Charter语言编写(NAC原生智能合约语言)
|
||
// 版本: 1.0.0
|
||
|
||
contract NACBridge {
|
||
// ========== 状态变量 ==========
|
||
|
||
// 合约所有者(32字节地址)
|
||
owner: Address;
|
||
|
||
// 桥接是否暂停
|
||
is_paused: bool;
|
||
|
||
// 最小验证器签名数
|
||
min_validator_signatures: u64;
|
||
|
||
// 验证器映射(地址 -> 验证器信息)
|
||
validators: mapping(Address => ValidatorInfo);
|
||
|
||
// 活跃验证器列表
|
||
active_validators: Address[];
|
||
|
||
// 锁定资产映射(锁定ID -> 锁定信息)
|
||
locks: mapping(Hash => LockInfo);
|
||
|
||
// 解锁资产映射(解锁ID -> 解锁信息)
|
||
unlocks: mapping(Hash => UnlockInfo);
|
||
|
||
// 已处理的消息ID(防止重放攻击)
|
||
processed_messages: mapping(Hash => bool);
|
||
|
||
// 支持的链ID映射
|
||
supported_chains: mapping(u64 => bool);
|
||
|
||
// ========== 数据结构 ==========
|
||
|
||
struct ValidatorInfo {
|
||
address: Address, // 验证器地址(32字节)
|
||
stake_amount: u128, // 质押金额
|
||
reputation: u64, // 声誉值
|
||
is_active: bool, // 是否活跃
|
||
}
|
||
|
||
struct LockInfo {
|
||
lock_id: Hash, // 锁定ID(48字节SHA3-384)
|
||
asset_id: Hash, // 资产ID(48字节)
|
||
amount: u128, // 锁定数量
|
||
locker: Address, // 锁定者地址(32字节)
|
||
target_chain: u64, // 目标链ID
|
||
receiver: Address, // 接收者地址(32字节)
|
||
timestamp: u64, // 锁定时间戳
|
||
status: LockStatus, // 锁定状态
|
||
}
|
||
|
||
struct UnlockInfo {
|
||
unlock_id: Hash, // 解锁ID(48字节)
|
||
lock_id: Hash, // 对应的锁定ID
|
||
asset_id: Hash, // 资产ID
|
||
amount: u128, // 解锁数量
|
||
receiver: Address, // 接收者地址
|
||
timestamp: u64, // 解锁时间戳
|
||
status: UnlockStatus, // 解锁状态
|
||
}
|
||
|
||
enum LockStatus {
|
||
Pending, // 待处理
|
||
Confirmed, // 已确认
|
||
Minted, // 已铸造
|
||
Failed, // 失败
|
||
}
|
||
|
||
enum UnlockStatus {
|
||
Pending, // 待处理
|
||
Verified, // 已验证
|
||
Unlocked, // 已解锁
|
||
Failed, // 失败
|
||
}
|
||
|
||
// ========== 事件 ==========
|
||
|
||
event AssetLocked(
|
||
lock_id: Hash,
|
||
asset_id: Hash,
|
||
amount: u128,
|
||
locker: Address,
|
||
target_chain: u64,
|
||
receiver: Address,
|
||
timestamp: u64
|
||
);
|
||
|
||
event AssetUnlocked(
|
||
unlock_id: Hash,
|
||
lock_id: Hash,
|
||
asset_id: Hash,
|
||
amount: u128,
|
||
receiver: Address,
|
||
timestamp: u64
|
||
);
|
||
|
||
event ValidatorRegistered(
|
||
validator: Address,
|
||
stake_amount: u128,
|
||
timestamp: u64
|
||
);
|
||
|
||
event ValidatorUnregistered(
|
||
validator: Address,
|
||
timestamp: u64
|
||
);
|
||
|
||
event BridgePaused(timestamp: u64);
|
||
event BridgeResumed(timestamp: u64);
|
||
|
||
// ========== 修饰符 ==========
|
||
|
||
modifier only_owner() {
|
||
require(msg.sender == owner, "Only owner can call this function");
|
||
_;
|
||
}
|
||
|
||
modifier when_not_paused() {
|
||
require(!is_paused, "Bridge is paused");
|
||
_;
|
||
}
|
||
|
||
modifier when_paused() {
|
||
require(is_paused, "Bridge is not paused");
|
||
_;
|
||
}
|
||
|
||
modifier only_validator() {
|
||
require(validators[msg.sender].is_active, "Not an active validator");
|
||
_;
|
||
}
|
||
|
||
// ========== 构造函数 ==========
|
||
|
||
constructor(min_signatures: u64) {
|
||
owner = msg.sender;
|
||
is_paused = false;
|
||
min_validator_signatures = min_signatures;
|
||
|
||
// 添加NAC和以太坊为支持的链
|
||
supported_chains[1] = true; // NAC
|
||
supported_chains[2] = true; // Ethereum
|
||
}
|
||
|
||
// ========== 验证器管理函数 ==========
|
||
|
||
/// 注册验证器
|
||
function register_validator(stake_amount: u128) public payable {
|
||
require(stake_amount >= 10000 * 10**18, "Insufficient stake amount");
|
||
require(!validators[msg.sender].is_active, "Validator already registered");
|
||
require(msg.value == stake_amount, "Stake amount mismatch");
|
||
|
||
let validator = ValidatorInfo {
|
||
address: msg.sender,
|
||
stake_amount: stake_amount,
|
||
reputation: 100,
|
||
is_active: true,
|
||
};
|
||
|
||
validators[msg.sender] = validator;
|
||
active_validators.push(msg.sender);
|
||
|
||
emit ValidatorRegistered(msg.sender, stake_amount, block.timestamp);
|
||
}
|
||
|
||
/// 注销验证器
|
||
function unregister_validator() public only_validator {
|
||
let validator = validators[msg.sender];
|
||
require(validator.is_active, "Validator not active");
|
||
|
||
// 退还质押金额
|
||
msg.sender.transfer(validator.stake_amount);
|
||
|
||
// 从活跃列表中移除
|
||
for (let i = 0; i < active_validators.length; i++) {
|
||
if (active_validators[i] == msg.sender) {
|
||
active_validators.remove(i);
|
||
break;
|
||
}
|
||
}
|
||
|
||
validators[msg.sender].is_active = false;
|
||
|
||
emit ValidatorUnregistered(msg.sender, block.timestamp);
|
||
}
|
||
|
||
/// 获取活跃验证器数量
|
||
function get_active_validator_count() public view returns (u64) {
|
||
return active_validators.length;
|
||
}
|
||
|
||
// ========== 资产锁定函数 ==========
|
||
|
||
/// 锁定资产
|
||
function lock_asset(
|
||
asset_id: Hash,
|
||
amount: u128,
|
||
target_chain: u64,
|
||
receiver: Address
|
||
) public payable when_not_paused returns (Hash) {
|
||
require(supported_chains[target_chain], "Target chain not supported");
|
||
require(amount > 0, "Amount must be greater than 0");
|
||
require(msg.value == amount, "Amount mismatch");
|
||
|
||
// 生成锁定ID(使用SHA3-384)
|
||
let lock_id = sha3_384(asset_id, msg.sender, block.timestamp);
|
||
require(!locks[lock_id].lock_id, "Lock ID already exists");
|
||
|
||
// 创建锁定信息
|
||
let lock_info = LockInfo {
|
||
lock_id: lock_id,
|
||
asset_id: asset_id,
|
||
amount: amount,
|
||
locker: msg.sender,
|
||
target_chain: target_chain,
|
||
receiver: receiver,
|
||
timestamp: block.timestamp,
|
||
status: LockStatus::Pending,
|
||
};
|
||
|
||
locks[lock_id] = lock_info;
|
||
|
||
emit AssetLocked(
|
||
lock_id,
|
||
asset_id,
|
||
amount,
|
||
msg.sender,
|
||
target_chain,
|
||
receiver,
|
||
block.timestamp
|
||
);
|
||
|
||
return lock_id;
|
||
}
|
||
|
||
/// 更新锁定状态(仅验证器可调用)
|
||
function update_lock_status(
|
||
lock_id: Hash,
|
||
new_status: LockStatus
|
||
) public only_validator {
|
||
require(locks[lock_id].lock_id, "Lock not found");
|
||
locks[lock_id].status = new_status;
|
||
}
|
||
|
||
/// 获取锁定信息
|
||
function get_lock_info(lock_id: Hash) public view returns (LockInfo) {
|
||
require(locks[lock_id].lock_id, "Lock not found");
|
||
return locks[lock_id];
|
||
}
|
||
|
||
// ========== 资产解锁函数 ==========
|
||
|
||
/// 解锁资产(需要验证器签名)
|
||
function unlock_asset(
|
||
lock_id: Hash,
|
||
burn_tx_hash: Hash,
|
||
signatures: Signature[]
|
||
) public when_not_paused returns (Hash) {
|
||
require(locks[lock_id].lock_id, "Lock not found");
|
||
require(locks[lock_id].status == LockStatus::Minted, "Lock not minted");
|
||
require(signatures.length >= min_validator_signatures, "Insufficient signatures");
|
||
|
||
// 验证签名
|
||
let message_hash = sha3_384(lock_id, burn_tx_hash, block.timestamp);
|
||
require(!processed_messages[message_hash], "Message already processed");
|
||
|
||
let valid_signatures = 0;
|
||
for (let i = 0; i < signatures.length; i++) {
|
||
let signer = recover_signer(message_hash, signatures[i]);
|
||
if (validators[signer].is_active) {
|
||
valid_signatures += 1;
|
||
}
|
||
}
|
||
|
||
require(valid_signatures >= min_validator_signatures, "Invalid signatures");
|
||
|
||
// 生成解锁ID
|
||
let unlock_id = sha3_384(lock_id, burn_tx_hash, block.timestamp);
|
||
|
||
// 创建解锁信息
|
||
let unlock_info = UnlockInfo {
|
||
unlock_id: unlock_id,
|
||
lock_id: lock_id,
|
||
asset_id: locks[lock_id].asset_id,
|
||
amount: locks[lock_id].amount,
|
||
receiver: locks[lock_id].receiver,
|
||
timestamp: block.timestamp,
|
||
status: UnlockStatus::Pending,
|
||
};
|
||
|
||
unlocks[unlock_id] = unlock_info;
|
||
processed_messages[message_hash] = true;
|
||
|
||
// 转账给接收者
|
||
locks[lock_id].receiver.transfer(locks[lock_id].amount);
|
||
|
||
// 更新解锁状态
|
||
unlocks[unlock_id].status = UnlockStatus::Unlocked;
|
||
|
||
emit AssetUnlocked(
|
||
unlock_id,
|
||
lock_id,
|
||
locks[lock_id].asset_id,
|
||
locks[lock_id].amount,
|
||
locks[lock_id].receiver,
|
||
block.timestamp
|
||
);
|
||
|
||
return unlock_id;
|
||
}
|
||
|
||
/// 获取解锁信息
|
||
function get_unlock_info(unlock_id: Hash) public view returns (UnlockInfo) {
|
||
require(unlocks[unlock_id].unlock_id, "Unlock not found");
|
||
return unlocks[unlock_id];
|
||
}
|
||
|
||
// ========== 管理函数 ==========
|
||
|
||
/// 暂停桥接
|
||
function pause() public only_owner when_not_paused {
|
||
is_paused = true;
|
||
emit BridgePaused(block.timestamp);
|
||
}
|
||
|
||
/// 恢复桥接
|
||
function resume() public only_owner when_paused {
|
||
is_paused = false;
|
||
emit BridgeResumed(block.timestamp);
|
||
}
|
||
|
||
/// 添加支持的链
|
||
function add_supported_chain(chain_id: u64) public only_owner {
|
||
supported_chains[chain_id] = true;
|
||
}
|
||
|
||
/// 移除支持的链
|
||
function remove_supported_chain(chain_id: u64) public only_owner {
|
||
supported_chains[chain_id] = false;
|
||
}
|
||
|
||
/// 更新最小验证器签名数
|
||
function update_min_signatures(new_min: u64) public only_owner {
|
||
require(new_min > 0, "Min signatures must be greater than 0");
|
||
min_validator_signatures = new_min;
|
||
}
|
||
|
||
/// 获取桥接状态
|
||
function get_bridge_status() public view returns (bool, u64, u64) {
|
||
return (is_paused, min_validator_signatures, active_validators.length);
|
||
}
|
||
}
|