chequebook

package
Version: v0.0.0-...-4654a8e Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package chequebook package wraps the 'chequebook' Ethereum smart contract.

The functions in this package allow using chequebook for issuing, receiving, verifying cheques in ether; (auto)cashing cheques in ether as well as (auto)depositing ether to the chequebook contract.

Index

Constants

View Source
const Version = "1.0"

Variables

Functions

func ValidateCode

func ValidateCode(ctx context.Context, b Backend, address common.Address) (ok bool, err error)

ValidateCode checks that the on-chain code at address matches the expected chequebook contract code. This is used to detect suicided chequebooks.

Types

type API

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

func NewAPI

func NewAPI(ch func() *Chequebook) *API

func (*API) Balance

func (a *API) Balance() (string, error)

func (*API) Cash

func (a *API) Cash(cheque *Cheque) (txhash string, err error)

func (*API) Deposit

func (a *API) Deposit(amount *big.Int) (txhash string, err error)

func (*API) Issue

func (a *API) Issue(beneficiary common.Address, amount *big.Int) (cheque *Cheque, err error)

type Backend

type Backend interface {
	bind.ContractBackend
	TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
	BalanceAt(ctx context.Context, address common.Address, blockNum *big.Int) (*big.Int, error)
}

Backend wraps all methods required for chequebook operation.

type Cheque

type Cheque struct {
	Contract    common.Address // address of chequebook, needed to avoid cross-contract submission
	Beneficiary common.Address
	Amount      *big.Int // cumulative amount of all funds sent
	Sig         []byte   // signature Sign(Keccak256(contract, beneficiary, amount), prvKey)
}

Cheque represents a payment promise to a single beneficiary.

func (*Cheque) Cash

func (ch *Cheque) Cash(session *contract.ChequebookSession) (string, error)

Cash cashes the cheque by sending an Ethereum transaction.

func (*Cheque) String

func (ch *Cheque) String() string

func (*Cheque) Verify

func (ch *Cheque) Verify(signerKey *ecdsa.PublicKey, contract, beneficiary common.Address, sum *big.Int) (*big.Int, error)

Verify verifies cheque for signer, contract, beneficiary, amount, valid signature.

type Chequebook

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

Chequebook can create and sign cheques from a single contract to multiple beneficiaries. It is the outgoing payment handler for peer to peer micropayments.

func LoadChequebook

func LoadChequebook(path string, prvKey *ecdsa.PrivateKey, backend Backend, checkBalance bool) (self *Chequebook, err error)

LoadChequebook loads a chequebook from disk (file path).

func NewChequebook

func NewChequebook(path string, contractAddr common.Address, prvKey *ecdsa.PrivateKey, backend Backend) (self *Chequebook, err error)

NewChequebook creates a new Chequebook.

func (*Chequebook) Address

func (chbook *Chequebook) Address() common.Address

Address returns the on-chain contract address of the chequebook.

func (*Chequebook) AutoDeposit

func (chbook *Chequebook) AutoDeposit(interval time.Duration, threshold, buffer *big.Int)

AutoDeposit (re)sets interval time and amount which triggers sending funds to the chequebook. Contract backend needs to be set if threshold is not less than buffer, then deposit will be triggered on every new cheque issued.

func (*Chequebook) Balance

func (chbook *Chequebook) Balance() *big.Int

Balance returns the current balance of the chequebook.

func (*Chequebook) Cash

func (chbook *Chequebook) Cash(ch *Cheque) (txhash string, err error)

Cash is a convenience method to cash any cheque.

func (*Chequebook) Deposit

func (chbook *Chequebook) Deposit(amount *big.Int) (string, error)

Deposit deposits money to the chequebook account.

func (*Chequebook) Issue

func (chbook *Chequebook) Issue(beneficiary common.Address, amount *big.Int) (ch *Cheque, err error)

Issue creates a cheque signed by the chequebook owner's private key. The signer commits to a contract (one that they own), a beneficiary and amount.

func (*Chequebook) MarshalJSON

func (chbook *Chequebook) MarshalJSON() ([]byte, error)

MarshalJSON serialises a chequebook.

func (*Chequebook) Owner

func (chbook *Chequebook) Owner() common.Address

Owner returns the owner account of the chequebook.

func (*Chequebook) Save

func (chbook *Chequebook) Save() (err error)

Save persists the chequebook on disk, remembering balance, contract address and cumulative amount of funds sent for each beneficiary.

func (*Chequebook) Stop

func (chbook *Chequebook) Stop()

Stop quits the autodeposit go routine to terminate

func (*Chequebook) String

func (chbook *Chequebook) String() string

func (*Chequebook) UnmarshalJSON

func (chbook *Chequebook) UnmarshalJSON(data []byte) error

UnmarshalJSON deserialises a chequebook.

type Inbox

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

Inbox can deposit, verify and cash cheques from a single contract to a single beneficiary. It is the incoming payment handler for peer to peer micropayments.

func NewInbox

func NewInbox(prvKey *ecdsa.PrivateKey, contractAddr, beneficiary common.Address, signer *ecdsa.PublicKey, abigen bind.ContractBackend) (self *Inbox, err error)

NewInbox creates an Inbox. An Inboxes is not persisted, the cumulative sum is updated from blockchain when first cheque is received.

func (*Inbox) AutoCash

func (i *Inbox) AutoCash(cashInterval time.Duration, maxUncashed *big.Int)

AutoCash (re)sets maximum time and amount which triggers cashing of the last uncashed cheque if maxUncashed is set to 0, then autocash on receipt.

func (*Inbox) Cash

func (i *Inbox) Cash() (txhash string, err error)

Cash attempts to cash the current cheque.

func (*Inbox) Receive

func (i *Inbox) Receive(promise swap.Promise) (*big.Int, error)

Receive is called to deposit the latest cheque to the incoming Inbox. The given promise must be a *Cheque.

func (*Inbox) Stop

func (i *Inbox) Stop()

Stop quits the autocash goroutine.

func (*Inbox) String

func (i *Inbox) String() string

type Outbox

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

Outbox can issue cheques from a single contract to a single beneficiary.

func NewOutbox

func NewOutbox(chbook *Chequebook, beneficiary common.Address) *Outbox

NewOutbox creates an outbox.

func (*Outbox) AutoDeposit

func (o *Outbox) AutoDeposit(interval time.Duration, threshold, buffer *big.Int)

AutoDeposit enables auto-deposits on the underlying chequebook.

func (*Outbox) Issue

func (o *Outbox) Issue(amount *big.Int) (swap.Promise, error)

Issue creates cheque.

func (*Outbox) Stop

func (o *Outbox) Stop()

Stop helps satisfy the swap.OutPayment interface.

func (*Outbox) String

func (o *Outbox) String() string

String implements fmt.Stringer.

type Params

type Params struct {
	ContractCode, ContractAbi string
}

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL