Giter Site home page Giter Site logo

weijiekoh / eip712-signing-demo Goto Github PK

View Code? Open in Web Editor NEW
20.0 20.0 18.0 52 KB

Demonstrates MetaMask's eip_signTypedData_v3 feature

Home Page: https://weijiekoh.github.io/eip712-signing-demo/index.html

License: The Unlicense

HTML 21.71% JavaScript 78.29%

eip712-signing-demo's People

Contributors

christoph2806 avatar danfinlay avatar mesquka avatar weijiekoh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

eip712-signing-demo's Issues

How to check sign in solidity, if I have only one struct?

https://medium.com/metamask/eip712-is-coming-what-to-expect-and-how-to-use-it-bb92fd1a7a26 There is no explanation of what exactly happens in the contract. I created data for my situation. In Javascript, the signature is created correctly and checked correctly, there are no problems with it. But in the contract, I always get FALSE. Can anyone explain me, what wrong in contract?
My JS data structure for tests:
`const domain = [{
name: "name",
type: "string"
}, {
name: "version",
type: "string"
}, {
name: "chainId",
type: "uint256"
}];

const userscore = [{
	name: "user",
	type: "address"
}, {
	name: "score",
	type: "uint256"
}, {
	name: "id",
	type: "uint256"
}, {
	name: "expire",
	type: "uint256"
}];

// Data
const domainData = {
	name: "Simple Dapp",
	version: "1",
	chainId: 1,
	verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
};

var message = {
	user: "0xe0428E45F41ae8a46284A9B64AB27e0B15A2eAF7",
	score: 20,
	id: 1,
	expire: 20
};

let msgParams = JSON.stringify({
	types: {
		EIP712Domain: domain,
		UserScore: userscore
	},
	domain: domainData,
	primaryType: "UserScore",
	message: message
})`

And my contract code, with correct address, r, s, v and another things.
`pragma experimental ABIEncoderV2;
pragma solidity ^0.5.0;

contract Verifier {
uint256 constant chainId = 1;

string private constant EIP712_DOMAIN  = "EIP712Domain(string name,string version,uint256 chainId)";
string private constant USERSCORE_TYPE = "UserScore(address user,uint256 score,uint256 id,uint256 time)";

bytes32 private constant EIP712_DOMAIN_TYPEHASH = keccak256(abi.encodePacked(EIP712_DOMAIN));
bytes32 private constant USERSCORE_TYPEHASH = keccak256(abi.encodePacked(USERSCORE_TYPE));

bytes32 private constant DOMAIN_SEPARATOR = keccak256(abi.encode(
    EIP712_DOMAIN_TYPEHASH,
    keccak256("Simple Dapp"),
    keccak256("1"),
    chainId
));

struct UserScore {
    address user;
    uint256 score;
    uint256 id;
    uint256 time;
}

function hashUserScore(UserScore memory userscore) private pure returns (bytes32){
    return keccak256(abi.encodePacked(
        "\x19\x01",
        DOMAIN_SEPARATOR,
        keccak256(abi.encode(
            USERSCORE_TYPEHASH,
            userscore.user,
            userscore.score,
            userscore.id,
            userscore.time
        ))
    ));
}

function verify() public pure returns (bool) {
    UserScore memory userscore = UserScore({
        user: 0xe0428E45F41ae8a46284A9B64AB27e0B15A2eAF7,
        score: 20,
        id: 1,
        time: 20
    });
        
    bytes32 sigR = 0x1443eafac538e7a767cbc1308e34ef088734b7e59dcd25baedace361a4356be1;
    bytes32 sigS = 0x2fd582b765b7cf85c3137c06dbfbfb58f02a2a891cc8c78bb4f781888d5a2a7e;
    uint8 sigV = 28;
    address signer = 0xe0428E45F41ae8a46284A9B64AB27e0B15A2eAF7;

    return signer == ecrecover(hashUserScore(userscore), sigV, sigR, sigS);
}

}`

Verify signatures in MetaMask

The example is very useful.
It would be very useful to also have a function which verifies the validity of the signature in Javascript.

Update README to version 4.14 or above

The sample is working with 4.14 at least, probably also newer versions, would be nice to have this mentioned in the README, currently it recommends 4.10 still.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.