5

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);
  }

1 Answer 1

1

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

1
  • Thanks Jason for your reply. However, each element in my array will store 56 characters. How will it work with bytes1[] or bytes32[]? Doesn't bytes1[] store an array of 1 chars? Doesn't bytes32[] store an array of 32 chars?
    – Curt
    Commented Aug 19, 2017 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.