[Ethereum] How to return bytes array in Solidity Contract

arraysbytes

I'd like to input & output an array of bytes (of approximately 56 characters each). The input works but the compiler gives a TypeError on bytes[] for the output function:

TypeError: Internal type is not allowed for public or external functions
function iterator() constant returns (address[],uint256[],uint256[],bytes[]){
                                                                    ^_____^

How do I return bytes from a function? Below is the code:

  CustomMap ledger;
  struct CustomMap {
     mapping (address => uint256) maps;
     address[] keys;
     bytes[] newKeys;
  }

  function createTokens(address recipient) public payable {
    ...

    if (contains(recipient)) {
       ledger.maps[recipient] = ledger.maps[recipient].add(tokens);
    } else {
       ledger.maps[recipient] = tokens;
       ledger.keys.push(recipient);
       ledger.newKeys.push("");
    }
    ...
  }

  function contains(address _addr) constant returns (bool) {
      if (ledger.keys.length == 0) {
         return false;
      }
      uint256 len = ledger.keys.length;
      for (uint256 i = 0 ; i < len ; i++) {
          if (ledger.keys[i] == _addr) {
            return true;
          }
      }
      return false;
  }

  function register(bytes key) returns (bool) {
      if (ledger.keys.length == 0) {
         return false;
      }
      uint256 len = ledger.keys.length;
      for (uint256 i = 0 ; i < len ; i++) {
          if (ledger.keys[i] == msg.sender) {
              ledger.newKeys[i] = key;
              return true;
          }
      }
      return false;
  }

  function iterator() constant returns (address[],uint256[],uint256[],bytes[]){
      uint256 len = ledger.keys.length;
      address[] memory keys = new address[](len);
      uint256[] memory values = new uint256[](len);
      uint256[] memory values2 = new uint256[](len);
      bytes[] memory newKeys = new bytes[](len);
      for (uint256 i = 0 ; i <  len ; i++) {
         address key = ledger.keys[i];
         keys[i] = key;
         values[i] = ledger.maps[key];
         values2[i] = token.balanceOf(key);
         newKeys[i] = ledger.newKeys[key];
      }
      return (keys,values,values2,newKeys);
  }

Best Answer

Try of the other types such as bytes1[], bytes2[] ... bytes32[] instead of bytes[].

I had to remove some of the lines from your code to get it compile, but I did solve the issue with the return type in question.

ex: http://remix.ethereum.org/#version=soljson-v0.4.15+commit.bbb8e64f.js

doc: http://solidity.readthedocs.io/en/develop/types.html#arrays

Related Topic