Solidity

關鍵字“salt”在solidity中是什麼意思?

  • April 7, 2022

“其他合約可以使用 new 關鍵字創建合約。從 0.8.0 開始,new 關鍵字通過指定 salt 選項支持 create2 功能。”

pragma solidity ^0.8.10;

contract Car {
address public owner;
string public model;
address public carAddr;

constructor(address _owner, string memory _model) payable {
   owner = _owner;
   model = _model;
   carAddr = address(this);
 }
}

contract CarFactory {
Car[] public cars;

function create(address _owner, string memory _model) public {
   Car car = new Car(_owner, _model);
   cars.push(car);
}

function createAndSendEther(address _owner, string memory _model) public payable {
   Car car = (new Car){value: msg.value}(_owner, _model);
   cars.push(car);
}

function create2(
   address _owner,
   string memory _model,
   bytes32 _salt
) public {
   Car car = (new Car){salt: _salt}(_owner, _model);
   cars.push(car);
}

function create2AndSendEther(
   address _owner,
   string memory _model,
   bytes32 _salt
) public payable {
   Car car = (new Car){value: msg.value, salt: _salt}(_owner, _model);
   cars.push(car);
}

function getCar(uint _index)
   public
   view
   returns (
       address owner,
       string memory model,
       address carAddr,
       uint balance
   )
{
   Car car = cars[_index];

   return (car.owner(), car.model(), car.carAddr(), address(car).balance);
   }
}

鹽是添加到交易(通常是散列函式的輸入)中的一點隨機性,以便更難發現秘密(密鑰)。

https://en.wikipedia.org/wiki/Salt_(cryptography)

salt 參數是發送者在部署合約時發送的值

摘自:使用 create2() 創建智能合約有什麼好處?

引用自:https://ethereum.stackexchange.com/questions/125555