gopay

package module
v0.0.0-...-bda469e Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 11 Imported by: 0

README

SSPC (Stupid Simple Payment Channels)

This repo contains an implementation for very simple payment channels. This implementation aims to provide a building block for developers to integrate Payment Channels into their own application.

Rationale

Over the years I came to the conclusion that Rollups are still not good enough for microtransactions. There have been multiple companies building Payment and State Channel Networks. These Channel networks allow to route transactions over multiple people. However this increases the complexity of the protocol as well as hugely increasing the complexity of the node software. In my opinion 95% of use cases where a Payment Channel Network is useful can also be solved by simple bidirectional Payment Channels. As long as the opening and closing of the Payment Channel is cheap (e.g. by opening and closing via an L2). SPCC should be a building block enabling developers to quickly integrate it into their own application.

Wrappers

The project currently supplies a golang API that can be used by golang application to start using Payment Channels. The API is designed to be as simple as possible. With NewChannel a channel object is instantiated, with Open you can open a channel etc. An example on how to use the golang wrapper is provided in cmd/gopay. I hope to implement JS and Android/IOS wrappers soon, so the contracts can be used from multiple different languages.

Contracts

There are two different types of contracts. The L2 contracts in package l2/contracts are designed to be optimal on a rollup (where state is less expensive than calldata). The mainnet contracts in package mainnet/contracts are designed to be optimal on mainnet (where state is costly but calldata is cheap). Neither of the contracts are audited, use at your own risk

Sample project

The folder cmd/deploy contains a small program to deploy the contracts on a blockchain. The folder cmd/gopay contains a small sample project that should show the usage of the golang API. It showcases a full node implementation that can be used between two peers to send funds between each other.

API

SPCC provides the following API:

  • NewChannel() to initialize the channel object
  • MetaData() returns the metadata of the channel
  • CurrentState() returns the most recent state of the channel
  • Open() proposes the channel opening
  • Accept() accepts the channel opening
  • CreateCoopClose() creates a cooperative close that needs to be signed
  • CoopClose() send a cooperative closing message
  • StartForceClose() starts the force closing procedure
  • DisputeForceClose() disputes a force close
  • FinishForceClose() finishes the force close
  • SendMoney() send money to a peer
  • ReceivedMoney() update the state with a received money notice

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ChannelAddr = common.HexToAddress("0x01")

Functions

This section is empty.

Types

type Backend

type Backend interface {
	bind.DeployBackend
	bind.ContractBackend
}

type ChallengeError

type ChallengeError struct {
	// contains filtered or unexported fields
}

func (*ChallengeError) Error

func (e *ChallengeError) Error() string

type Channel

type Channel struct {
	// contains filtered or unexported fields
}

Channel wraps an SSPC payment channel.

func NewChannel

func NewChannel(backend Backend, addrA, addrB common.Address, valueA, valueB *big.Int) (*Channel, error)

NewChannel initializes the channel object.

func (*Channel) Accept

func (c *Channel) Accept(auth *bind.TransactOpts, id [32]byte) error

Accept accepts the proposed channel opening with ID id.

func (*Channel) CoopClose

func (c *Channel) CoopClose(auth *bind.TransactOpts) error

CoopClose sends a cooperative close with the latest channel state. The latest channel state must be a valid cooperative close message.

func (*Channel) CreateCoopClose

func (c *Channel) CreateCoopClose() ([32]byte, error)

CreateCoopClose creates the hash of a coop close which needs to be signed and send to a peer. Once a coop close is signed, the channel must not be used to send transactions anymore.

func (*Channel) CurrentState

func (c *Channel) CurrentState() State

Current state returns the state of a channel.

func (*Channel) DisputeForceClose

func (c *Channel) DisputeForceClose(auth *bind.TransactOpts, startBlock uint64) (time.Time, error)

DisputeForceClose disputes a force close procedure. It returns the time when the force close is finished. `startBlock` denotes a block reasonably long before the force close happened.

func (*Channel) FinishForceClose

func (c *Channel) FinishForceClose(auth *bind.TransactOpts) error

FinishForceClose distributes the money after a force close is concluded. This can happen both after a StartForceClose or a DisputeForceClose.

func (*Channel) MetaData

func (c *Channel) MetaData() MetaData

Current state returns the metadata of a channel.

func (*Channel) Open

func (c *Channel) Open(auth *bind.TransactOpts) ([32]byte, error)

Open proposes to open a channel with the initially provided parameters. Open returns the resulting channel ID.

func (*Channel) ReceivedMoney

func (c *Channel) ReceivedMoney(valueA, valueB, round *big.Int, sig []byte) error

ReceivedMoney updates the local channel state with the new values received from our peer.

func (*Channel) SendMoney

func (c *Channel) SendMoney(value *big.Int) ([32]byte, error)

SendMoney returns the hash of the state after the value is transferred. This hash needs to be signed and send to the peer.

func (*Channel) StartForceClose

func (c *Channel) StartForceClose(auth *bind.TransactOpts) (time.Time, error)

StartForceClose starts the forceful closure procedure with the latest peer signature. It returns the time when the force close is successful.

type MetaData

type MetaData struct {
	ID [32]byte
	A  common.Address
	B  common.Address
}

MetaData contains metadata concerning the channel.

type State

type State struct {
	ValueA *big.Int
	ValueB *big.Int
	Round  *big.Int
}

State contains the state of a channel.

Directories

Path Synopsis
cmd
mainnet

Jump to

Keyboard shortcuts

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