wallet

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: May 12, 2022 License: Apache-2.0 Imports: 9 Imported by: 65

Documentation

Overview

Package wallet contains the definition of the wallet backend interfaces, and manages a global wallet backend instance that is accessible from the rest of the project.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeSparseSigs added in v0.2.1

func DecodeSparseSigs(r io.Reader, sigs *[]Sig) (err error)

DecodeSparseSigs decodes a collection of signatures in the form (mask, sig, sig, sig, ...).

func EncodeSparseSigs added in v0.2.1

func EncodeSparseSigs(w io.Writer, sigs []Sig) error

EncodeSparseSigs encodes a collection of signatures in the form ( mask, sig, sig, sig, ...).

func IndexOfAddr

func IndexOfAddr(addrs []Address, addr Address) int

IndexOfAddr returns the index of the given address in the address slice, or -1 if it is not part of the slice.

func SetBackend

func SetBackend(b Backend)

SetBackend sets the global wallet backend. Must not be called directly but through importing the needed backend.

func VerifySignature

func VerifySignature(msg []byte, sign Sig, a Address) (bool, error)

VerifySignature calls VerifySignature of the current backend.

Types

type Account

type Account interface {
	// Address used by this account.
	Address() Address

	// SignData requests a signature from this account.
	// It returns the signature or an error.
	SignData(data []byte) ([]byte, error)
}

Account represents a single account.

type AddrKey added in v0.4.0

type AddrKey string

AddrKey is a non-human readable representation of an `Address`. It can be compared and therefore used as a key in a map.

func Key added in v0.4.0

func Key(a Address) AddrKey

Key returns the `AddrKey` corresponding to the passed `Address`. The `Address` can be retrieved with `FromKey`. Panics when the `Address` can't be encoded.

func (AddrKey) Equal added in v0.9.0

func (k AddrKey) Equal(a Address) bool

Equal Returns whether the passed `Address` has the same key as the receiving `AddrKey`.

type Address

type Address interface {
	// BinaryMarshaler marshals the blockchain specific address to binary
	// format (a byte array).
	encoding.BinaryMarshaler
	// BinaryUnmarshaler unmarshals the blockchain specific address from
	// binary format (a byte array).
	encoding.BinaryUnmarshaler

	// String converts this address to a string.
	fmt.Stringer
	// Equal returns wether the two addresses are equal. The implementation
	// must be equivalent to checking `Address.Cmp(Address) == 0`.
	Equal(Address) bool
	// Cmp compares the byte representation of two addresses. For `a.Cmp(b)`
	// returns -1 if a < b, 0 if a == b, 1 if a > b.
	Cmp(Address) int
}

Address represents a identifier used in a cryptocurrency. It is dependent on the currency and needs to be implemented for every blockchain.

func CloneAddress added in v0.9.1

func CloneAddress(a Address) Address

CloneAddress returns a clone of an Address using its binary marshaling implementation. It panics if an error occurs during binary (un)marshaling.

func CloneAddresses added in v0.9.1

func CloneAddresses(as []Address) []Address

CloneAddresses returns a clone of a slice of Addresses using their binary marshaling implementation. It panics if an error occurs during binary (un)marshaling.

func FromKey added in v0.4.0

func FromKey(k AddrKey) Address

FromKey returns the `Address` corresponding to the passed `AddrKey` created by `Key`. Panics when the `Address` can't be decoded.

func NewAddress added in v0.9.0

func NewAddress() Address

NewAddress returns a variable of type Address, which can be used for unmarshalling an address from its binary representation.

type AddressDec added in v0.3.0

type AddressDec struct {
	Addr *Address
}

AddressDec is a helper type to decode single wallet addresses.

func (AddressDec) Decode added in v0.3.0

func (a AddressDec) Decode(r stdio.Reader) (err error)

Decode decodes a single wallet address.

type AddressPredicate added in v0.5.0

type AddressPredicate = func(Address) bool

AddressPredicate is a function for filtering Addresses.

type Addresses added in v0.3.0

type Addresses []Address

Addresses is a helper type for encoding and decoding address slices in situations where the length of the slice is known.

func (Addresses) Decode added in v0.3.0

func (a Addresses) Decode(r stdio.Reader) (err error)

Decode decodes a wallet address slice of known length. The slice has to be allocated to the correct size already.

func (Addresses) Encode added in v0.3.0

func (a Addresses) Encode(w stdio.Writer) error

Encode encodes a wallet address slice, the length of which is known to the following decode operation.

type AddressesWithLen added in v0.3.0

type AddressesWithLen []Address

AddressesWithLen is a helper type for encoding and decoding address slices of unknown length.

func (*AddressesWithLen) Decode added in v0.3.0

func (a *AddressesWithLen) Decode(r stdio.Reader) (err error)

Decode decodes a wallet address slice of unknown length.

func (AddressesWithLen) Encode added in v0.3.0

func (a AddressesWithLen) Encode(w stdio.Writer) error

Encode encodes a wallet address slice, the length of which is unknown to the following decode operation.

type Backend

type Backend interface {
	// NewAddress returns a variable of type Address, which can be used
	// for unmarshalling an address from its binary representation.
	NewAddress() Address

	// DecodeSig reads a signature from the provided stream. It is needed for
	// decoding of wire messages.
	DecodeSig(io.Reader) (Sig, error)

	// VerifySignature verifies if this signature was signed by this address.
	// It should return an error iff the signature or message are malformed.
	// If the signature does not match the address it should return false, nil
	VerifySignature(msg []byte, sign Sig, a Address) (bool, error)
}

Backend provides useful methods for this blockchain.

type Sig

type Sig = []byte

Sig is a single signature.

func CloneSigs added in v0.2.1

func CloneSigs(sigs []Sig) []Sig

CloneSigs returns a deep copy of a slice of signatures.

func DecodeSig

func DecodeSig(r io.Reader) (Sig, error)

DecodeSig calls DecodeSig of the current backend.

type SigDec added in v0.4.0

type SigDec struct {
	Sig *Sig
}

SigDec is a helper type to decode signatures.

func (SigDec) Decode added in v0.4.0

func (s SigDec) Decode(r io.Reader) (err error)

Decode decodes a single signature.

type Wallet

type Wallet interface {
	// Unlock requests an unlocked Account for an Address from the Wallet.
	// The returned account must be able to sign messages at least until
	// LockAll has been called, or a matching count of IncrementUsage and
	// DecrementUsage calls on the account's address has been made. Unlock may
	// be called multiple times for the same Address by the Perun SDK.
	Unlock(Address) (Account, error)

	// LockAll is called by the framework when a Client shuts down. This should
	// release all temporary resources held by the wallet, and accesses to
	// accounts after this call are no longer expected to succeed by the Perun
	// SDK. Implementing this function with any behavior is not essential.
	LockAll()

	// IncrementUsage is called whenever a new channel is created or restored.
	// The address passed to the function belongs to the Account the Client is
	// using to participate in the channel. Implementing this function with any
	// behavior is not essential.
	IncrementUsage(Address)

	// DecrementUsage is called whenever a channel is settled. The address
	// passed to the function belongs to the Account the Client is using to
	// participate in the channel. It is guaranteed by the Perun SDK that when
	// an account had the same number of DecrementUsage calls as prior
	// IncrementUsage calls made to it, it can be safely deleted permanently by
	// the wallet implementation. In that event, the affected account does not
	// have to be able to sign messages anymore. Implementing this function with
	// any behavior is not essential.
	DecrementUsage(Address)
}

Wallet is a collection of Accounts, i.e., secret keys. The interface defines a method Unlock, which the framework calls to get an Account for an Address. The other methods may, but don't need to, be implemented to gain more efficient resource handling by the Wallet implementation.

Directories

Path Synopsis
Package test contains generic tests and benchmarks for wallet backend implementation.
Package test contains generic tests and benchmarks for wallet backend implementation.

Jump to

Keyboard shortcuts

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