Git Product home page Git Product logo

payment-channel's Introduction

payment-channel

Ethereum Payment Channel in 50 lines of code

Decodes in lines

Channel constructor

  function Channel(address to, uint timeout) payable {
  	channelRecipient = to;
  	channelSender = msg.sender; // set channelSender as sender of the message
  	startDate = now; // set startDate as current timestamp
  	channelTimeout = timeout;
  }

Close channel

  function CloseChannel(bytes32 h, uint8 v, bytes32 r, bytes32 s, uint value){

  	address signer;
  	bytes32 proof;

  	// get signer from signature
  	// ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address): recover address associated with the public key from elliptic curve signature, return zero on error
  	signer = ecrecover(h, v, r, s);

  	// signature is invalid, throw
  	if (signer != channelSender && signer != channelRecipient) throw;

  // keccak256(...) returns (bytes32): compute the Ethereum-SHA-3 (Keccak-256) hash of the (tightly packed) arguments
  // sha3(...) returns (bytes32): an alias to keccak256()
  	proof = sha3(this, value);

  	// signature is valid but doesn't match the data provided
  	if (proof != h) throw;

  	if (signatures[proof] == 0)
  		signatures[proof] = signer;
  	else if (signatures[proof] != signer){
  		// channel completed, both signatures provided
  		if (!channelRecipient.send(value)) throw;
  		selfdestruct(channelSender);
  	}

  }

Chanel timeout

  function ChannelTimeout(){
  	if (startDate + channelTimeout > now) // check timeout
  		throw;

  	selfdestruct(channelSender);
  }

The story is Bob want to pay Alice if she do her work well.

And Bob doesn't want to pay her first, he want to pay when he check her work.

In this situation, he can create a channel to Alice.

After Alice done her work, she can close chanel with her work and value.

With just only one person close channel, Alice cannot withdraw her coin.

She can do that if Bob close channel too.

After Bob checkout her work and close channel with value.

Finally Alice get her coin.

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.