Git Product home page Git Product logo

sigverify's Introduction

SigVerify

API Reference Go Report Card

sigverify

This project is used to verify signatures under various specifications of ethereum. In addition to the standard elliptic curve signature, it also supports the signature of wallets such as ledger and argent.

It supports:

  1. Standard elliptic curve signature verification. (eth_sign).
  2. EIP712 typed data verification. (eth_signTypedData_v*).
  3. ERC1271 Smart contract wallet signature verification (isValidSignature).
  4. Some hardware wallets signature verification such as ledger.

Examples

1. Standard elliptic curve signature verification

package main

import (
	"fmt"

	ethcommon "github.com/ethereum/go-ethereum/common"
	"github.com/storyicon/sigverify"
)

func main() {
	valid, err := sigverify.VerifyEllipticCurveHexSignatureEx(
		ethcommon.HexToAddress("0xb052C02346F80cF6ae4DF52c10FABD3e0aD24d81"),
		[]byte("hello"),
		"0x0498c6564863c78e663848b963fde1ea1d860d5d882d2abdb707d1e9179ff80630a4a71705da534a562c08cb64a546c6132de26eb77a44f086832cbc1dbe01f71b",
	)
	fmt.Println(valid, err) // true <nil>
}

2. EIP-712 Typed data verification

package main

import (
	"encoding/json"
	"fmt"

	ethcommon "github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/signer/core/apitypes"
	"github.com/storyicon/sigverify"
)

const ExampleTypedData = `
{
    "types": {
        "EIP712Domain": [
            {
                "name": "name",
                "type": "string"
            },
            {
                "name": "chainId",
                "type": "uint256"
            }
        ],
        "RandomAmbireTypeStruct": [
            {
                "name": "identity",
                "type": "address"
            },
            {
                "name": "rewards",
                "type": "uint256"
            }
        ]
    },
    "domain": {
        "name": "Ambire Typed test message",
        "chainId": "1"
    },
    "primaryType": "RandomAmbireTypeStruct",
    "message": {
        "identity": "0x0000000000000000000000000000000000000000",
        "rewards": 0
    }
}
`

func main() {
	var typedData apitypes.TypedData
	if err := json.Unmarshal([]byte(ExampleTypedData), &typedData); err != nil {
		panic(err)
	}
	valid, err := sigverify.VerifyTypedDataHexSignatureEx(
		ethcommon.HexToAddress("0xaC39b311DCEb2A4b2f5d8461c1cdaF756F4F7Ae9"),
		typedData,
		"0xee0d9f9e63fa7183bea2ca2e614cf539464a4c120c8dfc1d5ccc367f242a2c5939d7f59ec2ab413b8a9047de5de2f1e5e97da4eba2ef0d6a89136464f992dae11c",
	)
	fmt.Println(valid, err) // true <nil>
}

3. EIP1271 Smart contract wallet signature verification

package main

import (
	"context"
	"fmt"

	ethcommon "github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/storyicon/sigverify"
)

func main() {
	client, err := ethclient.Dial("https://polygon-rpc.com")
	if err != nil {
		panic(err)
	}
	valid, err := sigverify.VerifyERC1271HexSignature(
		context.Background(),
		client,
		ethcommon.HexToAddress("0x4836A472ab1dd406ECb8D0F933A985541ee3921f"),
		[]byte{120, 113, 119},
		"0xc0f8db6019888d87a0afc1299e81ef45d3abce64f63072c8d7a6ef00f5f82c1522958ff110afa98b8c0d23b558376db1d2fbab4944e708f8bf6dc7b977ee07201b00",
	)
	fmt.Println(valid, err) // true <nil>
}

Contribution

Thank you for considering to help out with the source code! Welcome contributions from anyone on the internet, and are grateful for even the smallest of fixes!

If you'd like to contribute to this project, please fork, fix, commit and send a pull request for me to review and merge into the main code base.

Please make sure your contributions adhere to our coding guidelines:

  • Code must adhere to the official Go formatting guidelines (i.e. uses gofmt).
  • Code must be documented adhering to the official Go commentary guidelines.
  • Pull requests need to be based on and opened against the master branch.

sigverify's People

Contributors

storyicon avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

sigverify's Issues

Failing verification for typed messages signed using MetaMask

Hi, I'm trying to verify a message signed using the javascript library wagmi (uses ethers.js) through MetaMask and trying to verify in Go using this project but it fails with the error there is extra data provided in the message (0 < 1). Code to reproduce:

JavaScript code used to create signature:

const domain = {
    name: 'My Domain',
} as const
const types = {
    Action: [
        { name: 'type', type: 'string' },
        { name: 'value', type: 'string' },
    ],
} as const;
const value = {
    type: "Change description",
    value: "New value"
} as const
const signed = await signTypedDataAsync({
    domain,
    types,
    value,
});

Then I signed the data using MetaMask wallet with address 0xd6F5004d3124Aba6A54813EA69627292d96e9c7d and got this signed hash 0x5a9df794941046f20a0f024808f5ebe209b6d46893ecb981eadc084c6c26bc1530e3cb7b8cf7305701acb0f9d2432a0ba409589c6fd2108601d22fdbaae3d2bd1c

Code to verify typed data signature in Go:

const ExampleTypedData = `
{
  "types": {
    "Action": [
      {
        "name": "type",
        "type": "string"
      },
      {
        "name": "value",
        "type": "string"
      }
    ]
  },
  "domain": {
    "name": "My Domain"
  },
  "primaryType": "Action",
  "message": {
    "type": "Change description",
    "value": "New value"
  }
}
`

var typedData apitypes.TypedData
if err := json.Unmarshal([]byte(ExampleTypedData), &typedData); err != nil {
	panic(err)
}

valid, err := sigverify.VerifyTypedDataHexSignatureEx(
	ethcommon.HexToAddress("0xd6F5004d3124Aba6A54813EA69627292d96e9c7d"),
	typedData,
	"0x5a9df794941046f20a0f024808f5ebe209b6d46893ecb981eadc084c6c26bc1530e3cb7b8cf7305701acb0f9d2432a0ba409589c6fd2108601d22fdbaae3d2bd1c",
)
fmt.Println(valid, err)

Expected the verification to succeed but instead got this line: false there is extra data provided in the message (0 < 1).

Is there something obvious that I'm missing here? Verification of normal string messages works fine using using these libraries.

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.