地址说明

以太坊虚拟机地址为是20字节,而V网虚拟机解析地址为21字节。

1. 地址转换

在solidity中使用的时候需要对V网地址做如下处理(推荐):

/**
  * @dev convert uint256 (HexString add 0x at beginning) vision address to solidity address type
  * @param  visionAddress uint256 visionAddress, begin with 0x, followed by HexString
  * @return Solidity address type
  */
     
function convertFromVisionInt(uint256 visionAddress) public view returns(address){
      return address(visionAddress);
}

这个和在以太坊中其他类型转换成address类型语法相同。

2. 地址判断

solidity中有地址常量判断,如果写的是21字节地址编译器会报错,只用写20字节地址即可,如:

function compareAddress(address visionAddress) public view returns (uint256){
     // if (visionAddress == 0x46817ffa3d2f2028fcb9b8d2c619448cb3e3934c47) { // compile error
     if (visionAddress == 0x817ffa3d2f2028fcb9b8d2c619448cb3e3934c47) { // right
          return 1;
     } else {
          return 0;
     }
}

visionAddress从wallet-cli传入是0000000000000000000046817ffa3d2f2028fcb9b8d2c619448cb3e3934c47 这个21字节地址,即正常的V网地址时,是会返回1的,判断正确。

3. 地址赋值

solidity中有地址常量的赋值,如果写的是21字节地址编译器会报错,只用写20字节地址即可,solidity中后续操作直接利用这个20位地址,V网虚拟机内部做了补位操作。如:

function assignAddress() public view {
     // address newAddress = 0x46817ffa3d2f2028fcb9b8d2c619448cb3e3934c47; // compile error
     address newAddress = 0x817ffa3d2f2028fcb9b8d2c619448cb3e3934c47;
     // do something
}