aswap

package
v0.21.3 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2019 License: Apache-2.0 Imports: 14 Imported by: 2

Documentation

Overview

Package aswap implements an atomic swap.

Basically atomic swap is a case of escrow, but since we wanted a clean escrow implementation and a very specific atomic swap operation - we have this package now.

What happens here is funds are being held in an escrow aka swap and locked by a preimage_hash. These funds can either be released to the recipient by the sender via supplying a valid preimage, or returned back to the sender when the swap times out. Note, that when swap timed out it is no longer possible for the recipient to retrieve the funds.

The algorithm is as follows: 1. Sender generates a preimage, stores it in a secure place. 2. Sender makes a sha256 hash out of the preimage. 3. With this hash sender creates a Swap. 4. Sender can release the funds to the recipient by supplying a valid preimage, if the swap didn't time out. 5. If the swap timed out sender will be able to retrieve the funds from it just by sending a valid swapID. 6. Swap is deleted on successful retrieval for either step 4 or step 5.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowCodec   = fmt.Errorf("proto: integer overflow")
)

Functions

func HashBytes

func HashBytes(preimage []byte) []byte

func NewBucket

func NewBucket() orm.ModelBucket

func RegisterQuery

func RegisterQuery(qr weave.QueryRouter)

RegisterQuery will register this bucket as "/aswaps"

func RegisterRoutes

func RegisterRoutes(r weave.Registry, auth x.Authenticator, cashctrl cash.Controller)

RegisterRoutes will instantiate and register all handlers in this package

Types

type CreateMsg added in v0.17.0

type CreateMsg struct {
	Metadata *weave.Metadata                  `protobuf:"bytes,1,opt,name=metadata,proto3" json:"metadata,omitempty"`
	Source   github_com_iov_one_weave.Address `protobuf:"bytes,2,opt,name=source,proto3,casttype=github.com/iov-one/weave.Address" json:"source,omitempty"`
	// sha256 hash of preimage, 32 bytes long
	PreimageHash []byte                           `protobuf:"bytes,3,opt,name=preimage_hash,json=preimageHash,proto3" json:"preimage_hash,omitempty"`
	Destination  github_com_iov_one_weave.Address `protobuf:"bytes,4,opt,name=destination,proto3,casttype=github.com/iov-one/weave.Address" json:"destination,omitempty"`
	// amount may contain multiple token types
	Amount []*coin.Coin `protobuf:"bytes,5,rep,name=amount,proto3" json:"amount,omitempty"`
	// Timeout represents wall clock time.
	Timeout github_com_iov_one_weave.UnixTime `protobuf:"varint,6,opt,name=timeout,proto3,casttype=github.com/iov-one/weave.UnixTime" json:"timeout,omitempty"`
	// max length 128 character
	Memo string `protobuf:"bytes,7,opt,name=memo,proto3" json:"memo,omitempty"`
}

CreateMsg creates a Swap with some coins.

func (*CreateMsg) Descriptor added in v0.17.0

func (*CreateMsg) Descriptor() ([]byte, []int)

func (*CreateMsg) GetAmount added in v0.17.0

func (m *CreateMsg) GetAmount() []*coin.Coin

func (*CreateMsg) GetDestination added in v0.18.0

func (m *CreateMsg) GetDestination() github_com_iov_one_weave.Address

func (*CreateMsg) GetMemo added in v0.17.0

func (m *CreateMsg) GetMemo() string

func (*CreateMsg) GetMetadata added in v0.17.0

func (m *CreateMsg) GetMetadata() *weave.Metadata

func (*CreateMsg) GetPreimageHash added in v0.17.0

func (m *CreateMsg) GetPreimageHash() []byte

func (*CreateMsg) GetSource added in v0.18.0

func (*CreateMsg) GetTimeout added in v0.17.0

func (*CreateMsg) Marshal added in v0.17.0

func (m *CreateMsg) Marshal() (dAtA []byte, err error)

func (*CreateMsg) MarshalTo added in v0.17.0

func (m *CreateMsg) MarshalTo(dAtA []byte) (int, error)

func (CreateMsg) Path added in v0.17.0

func (CreateMsg) Path() string

func (*CreateMsg) ProtoMessage added in v0.17.0

func (*CreateMsg) ProtoMessage()

func (*CreateMsg) Reset added in v0.17.0

func (m *CreateMsg) Reset()

func (*CreateMsg) Size added in v0.17.0

func (m *CreateMsg) Size() (n int)

func (*CreateMsg) String added in v0.17.0

func (m *CreateMsg) String() string

func (*CreateMsg) Unmarshal added in v0.17.0

func (m *CreateMsg) Unmarshal(dAtA []byte) error

func (*CreateMsg) Validate added in v0.17.0

func (m *CreateMsg) Validate() error

func (*CreateMsg) XXX_DiscardUnknown added in v0.17.0

func (m *CreateMsg) XXX_DiscardUnknown()

func (*CreateMsg) XXX_Marshal added in v0.17.0

func (m *CreateMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*CreateMsg) XXX_Merge added in v0.17.0

func (m *CreateMsg) XXX_Merge(src proto.Message)

func (*CreateMsg) XXX_Size added in v0.17.0

func (m *CreateMsg) XXX_Size() int

func (*CreateMsg) XXX_Unmarshal added in v0.17.0

func (m *CreateMsg) XXX_Unmarshal(b []byte) error

type CreateSwapHandler

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

CreateSwapHandler creates a swap

func (CreateSwapHandler) Check

Check does the validation and sets the cost of the transaction

func (CreateSwapHandler) Deliver

Deliver moves the tokens from sender to the swap account if all conditions are met.

type ReleaseMsg added in v0.17.0

type ReleaseMsg struct {
	Metadata *weave.Metadata `protobuf:"bytes,1,opt,name=metadata,proto3" json:"metadata,omitempty"`
	// swap_id to release
	SwapID []byte `protobuf:"bytes,2,opt,name=swap_id,json=swapId,proto3" json:"swap_id,omitempty"`
	// raw preimage to unlock swap, also helpful to retrieve the swap by it's hashed version
	// must be exactly 32 bytes long
	Preimage []byte `protobuf:"bytes,3,opt,name=preimage,proto3" json:"preimage,omitempty"`
}

ReleaseMsg releases the tokens to the destination. This operation is authorized by preimage, which is sent raw and then hashed on the backend.

func (*ReleaseMsg) Descriptor added in v0.17.0

func (*ReleaseMsg) Descriptor() ([]byte, []int)

func (*ReleaseMsg) GetMetadata added in v0.17.0

func (m *ReleaseMsg) GetMetadata() *weave.Metadata

func (*ReleaseMsg) GetPreimage added in v0.17.0

func (m *ReleaseMsg) GetPreimage() []byte

func (*ReleaseMsg) GetSwapID added in v0.17.0

func (m *ReleaseMsg) GetSwapID() []byte

func (*ReleaseMsg) Marshal added in v0.17.0

func (m *ReleaseMsg) Marshal() (dAtA []byte, err error)

func (*ReleaseMsg) MarshalTo added in v0.17.0

func (m *ReleaseMsg) MarshalTo(dAtA []byte) (int, error)

func (ReleaseMsg) Path added in v0.17.0

func (ReleaseMsg) Path() string

func (*ReleaseMsg) ProtoMessage added in v0.17.0

func (*ReleaseMsg) ProtoMessage()

func (*ReleaseMsg) Reset added in v0.17.0

func (m *ReleaseMsg) Reset()

func (*ReleaseMsg) Size added in v0.17.0

func (m *ReleaseMsg) Size() (n int)

func (*ReleaseMsg) String added in v0.17.0

func (m *ReleaseMsg) String() string

func (*ReleaseMsg) Unmarshal added in v0.17.0

func (m *ReleaseMsg) Unmarshal(dAtA []byte) error

func (*ReleaseMsg) Validate added in v0.17.0

func (m *ReleaseMsg) Validate() error

func (*ReleaseMsg) XXX_DiscardUnknown added in v0.17.0

func (m *ReleaseMsg) XXX_DiscardUnknown()

func (*ReleaseMsg) XXX_Marshal added in v0.17.0

func (m *ReleaseMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ReleaseMsg) XXX_Merge added in v0.17.0

func (m *ReleaseMsg) XXX_Merge(src proto.Message)

func (*ReleaseMsg) XXX_Size added in v0.17.0

func (m *ReleaseMsg) XXX_Size() int

func (*ReleaseMsg) XXX_Unmarshal added in v0.17.0

func (m *ReleaseMsg) XXX_Unmarshal(b []byte) error

type ReleaseSwapHandler

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

ReleaseSwapHandler releases the amount to destination.

func (ReleaseSwapHandler) Check

Check just verifies it is properly formed and returns the cost of executing it

func (ReleaseSwapHandler) Deliver

Deliver moves the tokens from swap account to the receiver if all preconditions are met. When the swap account is empty it is deleted.

type ReturnMsg added in v0.19.0

type ReturnMsg struct {
	Metadata *weave.Metadata `protobuf:"bytes,1,opt,name=metadata,proto3" json:"metadata,omitempty"`
	// swap_id to return
	SwapID []byte `protobuf:"bytes,2,opt,name=swap_id,json=swapId,proto3" json:"swap_id,omitempty"`
}

ReturnMsg releases the tokens to the source. This operation only works if the Swap is expired.

func (*ReturnMsg) Descriptor added in v0.19.0

func (*ReturnMsg) Descriptor() ([]byte, []int)

func (*ReturnMsg) GetMetadata added in v0.19.0

func (m *ReturnMsg) GetMetadata() *weave.Metadata

func (*ReturnMsg) GetSwapID added in v0.19.0

func (m *ReturnMsg) GetSwapID() []byte

func (*ReturnMsg) Marshal added in v0.19.0

func (m *ReturnMsg) Marshal() (dAtA []byte, err error)

func (*ReturnMsg) MarshalTo added in v0.19.0

func (m *ReturnMsg) MarshalTo(dAtA []byte) (int, error)

func (ReturnMsg) Path added in v0.19.0

func (ReturnMsg) Path() string

func (*ReturnMsg) ProtoMessage added in v0.19.0

func (*ReturnMsg) ProtoMessage()

func (*ReturnMsg) Reset added in v0.19.0

func (m *ReturnMsg) Reset()

func (*ReturnMsg) Size added in v0.19.0

func (m *ReturnMsg) Size() (n int)

func (*ReturnMsg) String added in v0.19.0

func (m *ReturnMsg) String() string

func (*ReturnMsg) Unmarshal added in v0.19.0

func (m *ReturnMsg) Unmarshal(dAtA []byte) error

func (*ReturnMsg) Validate added in v0.19.0

func (m *ReturnMsg) Validate() error

func (*ReturnMsg) XXX_DiscardUnknown added in v0.19.0

func (m *ReturnMsg) XXX_DiscardUnknown()

func (*ReturnMsg) XXX_Marshal added in v0.19.0

func (m *ReturnMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ReturnMsg) XXX_Merge added in v0.19.0

func (m *ReturnMsg) XXX_Merge(src proto.Message)

func (*ReturnMsg) XXX_Size added in v0.19.0

func (m *ReturnMsg) XXX_Size() int

func (*ReturnMsg) XXX_Unmarshal added in v0.19.0

func (m *ReturnMsg) XXX_Unmarshal(b []byte) error

type ReturnSwapHandler

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

ReturnSwapHandler returns funds to the sender when swap timed out.

func (ReturnSwapHandler) Check

Check just verifies it is properly formed and returns the cost of executing it.

func (ReturnSwapHandler) Deliver

Deliver moves all the tokens from the swap to the defined sender if all preconditions are met. The swap is deleted afterwards.

type Swap

type Swap struct {
	// metadata is used for schema versioning support
	Metadata *weave.Metadata `protobuf:"bytes,1,opt,name=metadata,proto3" json:"metadata,omitempty"`
	// sha256 hash of preimage, 32 bytes long
	PreimageHash []byte `protobuf:"bytes,2,opt,name=preimage_hash,json=preimageHash,proto3" json:"preimage_hash,omitempty"`
	// source is a sender address
	Source github_com_iov_one_weave.Address `protobuf:"bytes,3,opt,name=source,proto3,casttype=github.com/iov-one/weave.Address" json:"source,omitempty"`
	// destination is an address of destination
	Destination github_com_iov_one_weave.Address `protobuf:"bytes,5,opt,name=destination,proto3,casttype=github.com/iov-one/weave.Address" json:"destination,omitempty"`
	// If unreleased before timeout, swap will return coins to source.
	// Timeout represents wall clock time as read from the block header. Timeout
	// is represented using POSIX time format.
	// Expiration time is inclusive meaning that the swap expires as soon as
	// the current time is equal or greater than timeout value.
	// nonexpired: [created, timeout)
	// expired: [timeout, infinity)
	Timeout github_com_iov_one_weave.UnixTime `protobuf:"varint,6,opt,name=timeout,proto3,casttype=github.com/iov-one/weave.UnixTime" json:"timeout,omitempty"`
	// max length 128 characters
	Memo string `protobuf:"bytes,7,opt,name=memo,proto3" json:"memo,omitempty"`
	// Address of this entity. Set during creation and does not change.
	Address github_com_iov_one_weave.Address `protobuf:"bytes,8,opt,name=address,proto3,casttype=github.com/iov-one/weave.Address" json:"address,omitempty"`
}

Swap is designed to hold some coins for atomic swap, locked by preimage_hash

func AsSwap

func AsSwap(obj orm.Object) *Swap

AsSwap extracts a *Swap value or nil from the object Must be called on a Bucket result that is an *Swap, will panic on bad type.

func (*Swap) Descriptor

func (*Swap) Descriptor() ([]byte, []int)

func (*Swap) GetAddress added in v0.19.0

func (m *Swap) GetAddress() github_com_iov_one_weave.Address

func (*Swap) GetDestination added in v0.18.0

func (m *Swap) GetDestination() github_com_iov_one_weave.Address

func (*Swap) GetMemo

func (m *Swap) GetMemo() string

func (*Swap) GetMetadata

func (m *Swap) GetMetadata() *weave.Metadata

func (*Swap) GetPreimageHash

func (m *Swap) GetPreimageHash() []byte

func (*Swap) GetSource added in v0.18.0

func (m *Swap) GetSource() github_com_iov_one_weave.Address

func (*Swap) GetTimeout

func (m *Swap) GetTimeout() github_com_iov_one_weave.UnixTime

func (*Swap) Marshal

func (m *Swap) Marshal() (dAtA []byte, err error)

func (*Swap) MarshalTo

func (m *Swap) MarshalTo(dAtA []byte) (int, error)

func (*Swap) ProtoMessage

func (*Swap) ProtoMessage()

func (*Swap) Reset

func (m *Swap) Reset()

func (*Swap) Size

func (m *Swap) Size() (n int)

func (*Swap) String

func (m *Swap) String() string

func (*Swap) Unmarshal

func (m *Swap) Unmarshal(dAtA []byte) error

func (*Swap) Validate

func (s *Swap) Validate() error

Validate ensures the Swap is valid

func (*Swap) XXX_DiscardUnknown

func (m *Swap) XXX_DiscardUnknown()

func (*Swap) XXX_Marshal

func (m *Swap) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Swap) XXX_Merge

func (m *Swap) XXX_Merge(src proto.Message)

func (*Swap) XXX_Size

func (m *Swap) XXX_Size() int

func (*Swap) XXX_Unmarshal

func (m *Swap) XXX_Unmarshal(b []byte) error

Jump to

Keyboard shortcuts

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