Giter Club home page Giter Club logo

sign-and-verify's Introduction

Build Status

Signing and Verifying with Ethereum

This project demonstrate a simple example of signing a message locally (off-chain) with the JSON RPC eth_sign. The signature can be verified by through a smart contract which makes uses of the Solidity's ecrecover function.

The whole flow is implemented in the test script. Commandline suite dapp.tools is used.

Starting a testnet

A testnet can be simply started with this command

dapp testnet --accounts 2

2 accounts will be created in the testnet. We can use one to sign a message and then use the other to deploy the contract and interact with it.

Signing a message

Starting with a message

msg="Hello World"

We convert to hex format

hexData=$(seth --from-ascii $msg)

And then get the keccak256 hash of it

hexDataFixedSize=$(seth keccak $hexData)

We can now sign it with seth sign which will make use of the JSON-RPC eth_sign.

export ETH_KEYSTORE=~/.dapp/testnet/8545/keystore/
export ETH_RPC_URL=http://127.0.0.1:8545

export ETH_FROM=<account_address to sign>
signedData=$(seth  sign  $hexDataFixedSize )

The signature should be broken down into 3 components v, r, s (these are part of the ECDSA standard signature).

r=0x${signedData:2:64}
s=0x${signedData:66:64}
v=0x${signedData:130:2}

v, r, s will be passed to a smart contract for verification.

Verifying the signature

A smart contract is implemented in verifier.sol.

The first step is to deploy it

export ETH_FROM=<account_address to deploy>
export ETH_GAS=4500000

verifier=$(dapp create  verifier)

The isSigned function of the contract can be called to verify the signature. It returns true if the signature is signed by the given address.

isSigned=$(seth call $verifier \
	'isSigned(address,bytes32,uint8,bytes32,bytes32)(bool)' \
	<account address that was used to sign> \
	$hexDataFixedSize \
	$(seth --to-word $v) \
	$r \
	$s)

Deploy on testnet

A verifier contract is deployed on the Rinkeby testnet at https://rinkeby.etherscan.io/address/0x14617305e1ffea4af4bdf2c98d177a0efbb698d0

Following are the steps to deploy with dapp.

  • Make a clean build
dapp clean
dapp build
  • Set the environment variables for dapp:
export ETH_FROM=<account to deploy>
export ETH_RPC_URL=https://rinkeby.infura.io
export ETH_GAS=4500000
export ETH_KEYSTORE=~/rinkeby-testnet
  • Create the contract
dapp create verifier

sign-and-verify's People

Contributors

alext234 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.