Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for sign typed data with bytes #146

Merged
merged 2 commits into from
Jul 12, 2021

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Jul 12, 2021

This is a rebase of #28. This is the original description:

Added test that contain bytes for signing typed data.

The test values have been calculated with the following smart contract:

pragma solidity 0.4.24;

contract Test {
    
    function personData()
        public
        pure
        returns (bytes) 
    {
        bytes32 personSchemaHash = keccak256("Person(string name,address wallet)");
        return abi.encode(
            personSchemaHash,
            keccak256('Bob'),
            address(0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB)
        );
    }
    
    function signData()
        public
        pure
        returns (bytes) 
    {
        bytes32 personSchemaHash = keccak256("Person(string name,address wallet)");
        return abi.encode(
            mailSchemaHash(),
            keccak256(
                abi.encode(
                    personSchemaHash,
                    keccak256('Cow'),
                    address(0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826)
                )
            ),
            keccak256(personData()),
            keccak256("Hello, Bob!"),
            keccak256(hex"25192142931f380985072cdd991e37f65cf8253ba7a0e675b54163a1d133b8ca")
        );
    }
    
    function signHash()
        public
        pure
        returns (bytes32) 
    {
        return keccak256(signData());
    }
    
    function sign()
        public
        pure
        returns (bytes32) 
    {
        return keccak256(abi.encodePacked(
            byte(0x19), 
            byte(1), 
            0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f, 
            signHash()
        ));
    }
    
    
    function mailSchemaHash() 
        public
        pure
        returns (bytes32) 
    {
        return keccak256("Mail(Person from,Person to,string contents,bytes payload)Person(string name,address wallet)");
    }
}

@Gudahtt Gudahtt requested a review from a team as a code owner July 12, 2021 14:04
@adonesky1
Copy link
Contributor

@Gudahtt my solidity knowledge needs some buffing but just a general question does the bytes type ultimately allow any string?

@rekmarks rekmarks self-assigned this Jul 12, 2021
@Gudahtt
Copy link
Member Author

Gudahtt commented Jul 12, 2021

I'm not entirely sure - I don't know much about solidity either. But I just looked this up, and it looks like "bytes" is expected to be an array of byte values, where each byte is a number from 1-32 inclusive. The intended encoding seems ambiguous to me though 🤔. I'll have to look into that.

@Gudahtt
Copy link
Member Author

Gudahtt commented Jul 12, 2021

It looks like we accept any string at the moment. Presumably each character represents some number of bytes. Not 100% sure how that gets interpreted though, i.e. which string encoding.

Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Gudahtt Gudahtt merged commit efcc5f3 into main Jul 12, 2021
@Gudahtt Gudahtt deleted the add_bytes_test_for_eth_sign_typed_data branch July 12, 2021 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants