قراردادهای چندامضایی (Multi-Signature Contract) به شما این امکان را میدهند که انجام یک عمل خاص (مانند برداشت وجه) تنها با تایید چندین شخص (امضا) ممکن باشد. این نوع قرارداد میتواند در مواردی مانند تجمع داراییها یا مدیریت مشترک داراییها مفید باشد. در زیر، مراحل توسعه قراردادهای هوشمند چند امضایی را با سالیدیتی بررسی میکنیم.
سرفصل های مقاله
1. تعریف ساختار قرارداد
ابتدا یک قرارداد جدید ایجاد کرده و نیازمندیهای پایه را تعریف میکنیم. این قرارداد باید شامل آدرسهای امضا کنندگان و حداقل تعداد امضای لازم برای تأیید هر اقدام باشد.
2. کد مثال قرارداد چندامضایی
در اینجا یک مثال ساده از قرارداد چندامضایی آمده است:
// SPDX-License-Identifier: MIT
pragma solidity ^.8.;
contract MultiSigWallet {
uint256 public requiredSignatures;
address[] public owners;
mapping(address => bool) public isOwner;
struct Transaction {
address to;
uint256 value;
bool executed;
uint256 confirmations;
mapping(address => bool) isConfirmed;
}
Transaction[] public transactions;
event Deposit(address indexed sender, uint256 amount);
event SubmitTransaction(address indexed owner, uint indexed txIndex);
event ConfirmTransaction(address indexed owner, uint indexed txIndex);
event ExecuteTransaction(address indexed owner, uint indexed txIndex);
constructor(address[] memory _owners, uint256 _requiredSignatures) {
require(_owners.length > , "Owners required");
require(_requiredSignatures <= _owners.length && _requiredSignatures > , "Invalid number of required signatures");
for (uint256 i = ; i < _owners.length; i++) {
address owner = _owners[i];
require(owner != address(), "Invalid owner");
require(!isOwner[owner], "Owner not unique");
isOwner[owner] = true;
owners.push(owner);
}
requiredSignatures = _requiredSignatures;
}
receive() external payable {
emit Deposit(msg.sender, msg.value);
}
function submitTransaction(address _to, uint256 _value) public {
require(isOwner[msg.sender], "Only owners can submit transactions");
uint txIndex = transactions.length;
transactions.push();
Transaction storage newTransaction = transactions[txIndex];
newTransaction.to = _to;
newTransaction.value = _value;
newTransaction.executed = false;
newTransaction.confirmations = ;
emit SubmitTransaction(msg.sender, txIndex);
}
function confirmTransaction(uint256 _txIndex) public {
require(isOwner[msg.sender], "Only owners can confirm transactions");
Transaction storage transaction = transactions[_txIndex];
require(!transaction.isConfirmed[msg.sender], "Transaction already confirmed");
transaction.isConfirmed[msg.sender] = true;
transaction.confirmations++;
emit ConfirmTransaction(msg.sender, _txIndex);
}
function executeTransaction(uint256 _txIndex) public {
Transaction storage transaction = transactions[_txIndex];
require(transaction.confirmations >= requiredSignatures, "Not enough confirmations");
require(!transaction.executed, "Transaction already executed");
transaction.executed = true;
(bool success, ) = transaction.to.call{value: transaction.value}("");
require(success, "Transaction failed");
emit ExecuteTransaction(msg.sender, _txIndex);
}
}
3. توضیحات کد
- متغیرها و ساختارها:
- owners: آرایهای از آدرسهای صاحبان (امضاکنندگان).
- requiredSignatures: تعداد حداقل امضاهای لازم برای اجرای یک تراکنش.
- Transaction: ساختاری که اطلاعات تراکنشها را ذخیره میکند.
- تابعها:
- submitTransaction: این تابع به یک مالک اجازه میدهد تا تراکنشی جدید ثبت کند.
- confirmTransaction: این تابع به یک مالک اجازه میدهد تا بر روی تراکنش تأیید بگذارد.
- executeTransaction: زمانی که تعداد تأییدها به حد نصاب رسید، این تابع تراکنش را اجرا میکند.
4. تست و پیادهسازی قرارداد
میتوانید از ابزارهایی مانند Truffle یا Hardhat برای تست و پیادهسازی این قرارداد استفاده کنید. مجموعهای از تستها کمک میکند تا مطمئن شوید که قرارداد به درستی عمل میکند و از نظر امنیتی در برابر حملات متداول مقاوم است.
نتیجه گیری
قراردادهای هوشمند چند امضایی یکی از ابزارهای قدرتمند در بلاکچین هستند که کنترل و مدیریت منابع را میان چندین کاربر تسهیل میکنند. با استفاده از قراردادهای چندامضایی، میتوانید امنیت و شفافیت بیشتری را در فعالیتهای مالی و مدیریتی خود ایجاد کنید.