sigverify

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2022 License: Apache-2.0 Imports: 11 Imported by: 9

README

SigVerify

API Reference Go Report Card

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyBytes

func CopyBytes(data []byte) []byte

CopyBytes is used to copy slice

func EcRecover

func EcRecover(data []byte, sig []byte) (ethcommon.Address, error)

EcRecover returns the address for the account that was used to create the signature, Note, this function is compatible with eth_sign and personal_sign. As such it recovers the address of: hash = keccak256("\x19Ethereum Signed Message:\n"${message length}${message}) addr = ecrecover(hash, signature)

Note, the signature must conform to the secp256k1 curve R, S and V values, where the V value must be 27 or 28 for legacy reasons.

https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_ecRecover

func EcRecoverEx

func EcRecoverEx(data []byte, sig []byte) (ethcommon.Address, error)

EcRecoverEx is an extension to EcRecover that supports more signature formats, such as ledger signatures.

func GetERC1271Magic

func GetERC1271Magic() [4]byte

GetERC1271Magic is used to get the magic value defined by ERC1271

func Has0xPrefix

func Has0xPrefix(str string) bool

Has0xPrefix validates str begins with '0x' or '0X'.

func HashTypedData

func HashTypedData(data apitypes.TypedData) ([]byte, []byte, error)

HashTypedData is used to calculate the hash of EIP-712 conformant typed data hash = keccak256("\x19${byteVersion}${domainSeparator}${hashStruct(message)}")

func HexDecode

func HexDecode(s string) ([]byte, error)

HexDecode returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".

func IsErrExecutionReverted

func IsErrExecutionReverted(err error) bool

IsErrExecutionReverted is used to determine whether err is an ExecutionReverted error

func IsErrNoContractCode

func IsErrNoContractCode(err error) bool

IsErrNoContractCode is used to determine whether err is an NoContractCode error

func RecoveryAddress

func RecoveryAddress(data []byte, sig []byte) (ethcommon.Address, error)

RecoveryAddress returns the address for the account that was used to create the signature, this function is almost a fork of EcRecover However, EcRecover in go-ethereum will automatically perform accounts.TextHash for data in EcRecover, which makes EIP712 unable to reuse this function This design makes the function lose versatility, so this behavior is changed here

func RecoveryAddressEx

func RecoveryAddressEx(data []byte, sig []byte) (ethcommon.Address, error)

RecoveryAddressEx is an extension to RecoveryAddress that supports more signature formats, such as ledger signatures.

func RecoveryTypedDataAddressEx

func RecoveryTypedDataAddressEx(data apitypes.TypedData, signature []byte) (ethcommon.Address, error)

RecoveryTypedDataAddressEx is used to recover the signer address of the TypedData signature

func VerifyERC1271HexSignature

func VerifyERC1271HexSignature(ctx context.Context, client *ethclient.Client, address ethcommon.Address, data []byte, signature string) (bool, error)

VerifyERC1271HexSignature is a helper function. look up VerifyERC1271 for more comments.

func VerifyERC1271Signature

func VerifyERC1271Signature(ctx context.Context, client *ethclient.Client, address ethcommon.Address, data []byte, signature []byte) (bool, error)

VerifyERC1271Signature verifies signatures based on the ERC1271 standard 1. When the given address is EOA, "no contract code at given address" will be thrown: 2. When the given address is a contract but does not conform to the erc1271 specification, "execution reverted" will be thrown

func VerifyEllipticCurveHexSignatureEx added in v1.1.0

func VerifyEllipticCurveHexSignatureEx(address ethcommon.Address, data []byte, signature string) (bool, error)

VerifyEllipticCurveHexSignatureEx is used to verify elliptic curve signatures It calls the EcRecoverEx function to verify the signature.

func VerifyEllipticCurveSignature

func VerifyEllipticCurveSignature(address ethcommon.Address, data []byte, signature []byte) (bool, error)

VerifyEllipticCurveSignature is used to verify the elliptic curve signature It calls the native ecrecover function to verify the signature

func VerifyEllipticCurveSignatureEx

func VerifyEllipticCurveSignatureEx(address ethcommon.Address, data []byte, signature []byte) (bool, error)

VerifyEllipticCurveSignatureEx is used to verify elliptic curve signatures It calls the EcRecoverEx function to verify the signature.

func VerifyHexSignatureEx

func VerifyHexSignatureEx(ctx context.Context, client *ethclient.Client, address ethcommon.Address, msg []byte, signature string) (bool, error)

VerifyHexSignatureEx is used to verify text signature

func VerifySignatureEx

func VerifySignatureEx(ctx context.Context, client *ethclient.Client, address ethcommon.Address, msg []byte, signature []byte) (bool, error)

VerifySignatureEx is used to verify text signature

func VerifyTypedDataHexSignatureEx

func VerifyTypedDataHexSignatureEx(address ethcommon.Address, data apitypes.TypedData, signature string) (bool, error)

VerifyTypedDataHexSignatureEx is used to verify the signer address of the TypedData signature

func VerifyTypedDataSignatureEx

func VerifyTypedDataSignatureEx(address ethcommon.Address, data apitypes.TypedData, signature []byte) (bool, error)

VerifyTypedDataSignatureEx is used to verify the signer address of the TypedData signature

Types

This section is empty.

Directories

Path Synopsis
contracts

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL