[ethers.js] bytecode(=creationCode) 얻기

contract 컴파일 후 bytecode(creationCode)를 얻으려면:

(await ethers.getContractFactory("DasomOLIContract")).bytecode

이걸 keccak256()으로 돌리면(ethersV5 기준)

> ethers.utils.keccak256((await ethers.getContractFactory("DasomOLIContract")).bytecode)

위는 solidity에서 다음을 하는 것과 동일하다.

keccak256(abi.encodePacked(type(DasomOLIContract).creationCode));

[ethers.js] Contract 시그니처와 hash 확인

const ca = await ethers.getContractFactory("DasomToken");
const cabi = ca.interface;
cabi.format() // Fragments 확인
cabi.forEachFunction((func, index) => { console.log(func.format()); }); // 각 함수의 시그니처 확인
cabi.forEachFunction((func, index) => { console.log(func.format(), ethers.keccak256(ethers.toUtf8Bytes(func.format()))); }); // 시그니처와 해시 값 확인.

[Mythril] myth analyze 시 import 에러

Mythril은 ConsenSys의 smart contract의 보함 결함을 점검해주는 툴이다. analyze를 통해 분석을 하려고 할 때 openzeppelin 등을 사용한 컨트랙트의 경우 solc의 import 에러를 겪을 수 있다.

$ myth analyze contracts/dasomoli.sol 
mythril.interfaces.cli [ERROR]: Solc experienced a fatal error.

ParserError: Source "@openzeppelin/contracts/token/ERC20/IERC20.sol" not found: File not found. Searched the following locations: "".
 --> contracts/dasomoli.sol:6:1:
  |
6 | import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

solc를 command line에서 사용할 때는 아래와 같이 remap을 해주면 된다.

solc @openzeppelin/=$(pwd)/node_modules/@openzeppelin/ contracts/dasomoli.sol
Compiler run successful, no output requested.

myth 사용 시에는 이를 json 파일을 만들어 --solc-json 옵션을 통해 줄 수 있다.

  • solc.json
{
   "remappings": [ "@openzeppelin/=/Users/dasomoli/src/smartcontract/node_modules/@openzeppelin/" ]
}
$ myth analyze --solc-json solc.json contracts/dasomoli.sol 
The analysis was completed successfully. No issues were detected.

참고