2

Assume we a smart contract that accepts a number of cryptographically signed attributes. The smart contract will validate each signature and check that all the addresses of those signed signatures match. The smart contract should be able to take a minimum of two signature and a maximum of say ten signatures. How do we create a smart contract that allows the user to input a dynamic number of attributes? Is that even possible?

The following code works for a single signature and returns the address of the signer:

pragma solidity ^0.4.8;

contract test {

    function recoverAddr(bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) constant returns (address) {
        bytes memory prefix = "\x19Ethereum Signed Message:\n32";
        bytes32 prefixedHash = sha3(prefix, msgHash);
        address signer = ecrecover(prefixedHash, v, r, s);
        return signer;
    }
}

I want to add to this so that it can accept the n signed msgHashes (considering sometimes a user might enter at least 2 and other times the user might enter more attributes, up to the maximum number allowed, 10 in this case). Then if all addresses match, we return the address.

| improve this question | | | | |
2

You can pass dynamically-sized arrays to a function and loop. E.g.:

pragma solidity ^0.4.21;

contract Test {
    ...

    function check(bytes32[] msgHash, uint8[] v, bytes32[] r, bytes32[] s) public view returns (bool) {
        for (uint256 i = 0; i < msgHash.length; i++) {
            address signer = recoverAddr(msgHash[i], v[i], r[i], s[i]);
            ...
        }
    }
}
| improve this answer | | | | |
  • I am testing this contract on remix. How would I input the attributes to this contract? Assuming I have two attributes, I am trying ["H1","H2"],[1,1],["v1","v2"],["r1","r2"],["s1","s2"] but it keeps giving me Unexpected token " in JSON at position 2 error. Of course I am trying with real values. – Fawzy3881 Mar 29 '18 at 18:17
  • That looks about right to me. Here are some dummy values I just tested: ["0x12345667890123456678901234566789012", "0x12345667890123456678901234566789012"],[1,1],["0x12345667890123456678901234566789012","0x12345667890123456678901234566789012"],["0x12345667890123456678901234566789012","0x12345667890123456678901234566789012"]. – user19510 Mar 29 '18 at 18:20
  • Yeah those dummy values worked for me too, however, I tried these values and it didn't work [“0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8”,”0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8”],[28,28],[“0x9242685bf161793cc25603c231bc2f568eb630ea16aa137d2664ac8038825608”,”0x9242685bf161793cc25603c231bc2f568eb630ea16aa137d2664ac8038825608”],[“0x4f8ae3bd7535248d0bd448298cc2e2071e56992d0774dc340c368ae950852ada”,”0x4f8ae3bd7535248d0bd448298cc2e2071e56992d0774dc340c368ae950852ada"] – Fawzy3881 Mar 29 '18 at 18:22
  • Not sure if it's just a copy/paste issue, but most of the quotation marks in what you pasted here aren't regular quotation marks. You have and instead of ". – user19510 Mar 29 '18 at 18:24
  • When I fix the quotation marks, the values you shared work fine for me. – user19510 Mar 29 '18 at 18:24
0

You can do it using overloading or use only one function and check the value if it has been passed in the function.

    function callMe(uint one) public pure returns(uint sum) {
        return one;
    }

    function callMe(uint one, uint two) public pure returns(uint sum) {
        return callMe(one) + two;
    }

in this case you overload the function to define an additional parameter. Another way can be using one function and conditionally check the parameters

    function callMe(uint one, uint two) public pure returns(uint sum) {
        // default value for parameters is zero and if so that means that has not been passed 
        if(two > 0) {
            return one + (two * 2);
        }

        return one;
    }

if you call this function without the second parameter inside the contract will arrive as zero, and you can check it against the default value to define whether it has been arrived.

Calling this function without one parameter or even parameterless is legal (using remix). And I think it would be the same through javascript/web3 (which is used by remix). It might be an issue with other implementation of web3 though. And definitely you can't call a function without a parameter in Solidity.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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