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 ¶
- Variables
- func HashBytes(preimage []byte) []byte
- func NewBucket() orm.ModelBucket
- func RegisterQuery(qr weave.QueryRouter)
- func RegisterRoutes(r weave.Registry, auth x.Authenticator, cashctrl cash.Controller)
- type CreateMsg
- func (*CreateMsg) Descriptor() ([]byte, []int)
- func (m *CreateMsg) GetAmount() []*coin.Coin
- func (m *CreateMsg) GetDestination() github_com_iov_one_weave.Address
- func (m *CreateMsg) GetMemo() string
- func (m *CreateMsg) GetMetadata() *weave.Metadata
- func (m *CreateMsg) GetPreimageHash() []byte
- func (m *CreateMsg) GetSource() github_com_iov_one_weave.Address
- func (m *CreateMsg) GetTimeout() github_com_iov_one_weave.UnixTime
- func (m *CreateMsg) Marshal() (dAtA []byte, err error)
- func (m *CreateMsg) MarshalTo(dAtA []byte) (int, error)
- func (CreateMsg) Path() string
- func (*CreateMsg) ProtoMessage()
- func (m *CreateMsg) Reset()
- func (m *CreateMsg) Size() (n int)
- func (m *CreateMsg) String() string
- func (m *CreateMsg) Unmarshal(dAtA []byte) error
- func (m *CreateMsg) Validate() error
- func (m *CreateMsg) XXX_DiscardUnknown()
- func (m *CreateMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *CreateMsg) XXX_Merge(src proto.Message)
- func (m *CreateMsg) XXX_Size() int
- func (m *CreateMsg) XXX_Unmarshal(b []byte) error
- type CreateSwapHandler
- type ReleaseMsg
- func (*ReleaseMsg) Descriptor() ([]byte, []int)
- func (m *ReleaseMsg) GetMetadata() *weave.Metadata
- func (m *ReleaseMsg) GetPreimage() []byte
- func (m *ReleaseMsg) GetSwapID() []byte
- func (m *ReleaseMsg) Marshal() (dAtA []byte, err error)
- func (m *ReleaseMsg) MarshalTo(dAtA []byte) (int, error)
- func (ReleaseMsg) Path() string
- func (*ReleaseMsg) ProtoMessage()
- func (m *ReleaseMsg) Reset()
- func (m *ReleaseMsg) Size() (n int)
- func (m *ReleaseMsg) String() string
- func (m *ReleaseMsg) Unmarshal(dAtA []byte) error
- func (m *ReleaseMsg) Validate() error
- func (m *ReleaseMsg) XXX_DiscardUnknown()
- func (m *ReleaseMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *ReleaseMsg) XXX_Merge(src proto.Message)
- func (m *ReleaseMsg) XXX_Size() int
- func (m *ReleaseMsg) XXX_Unmarshal(b []byte) error
- type ReleaseSwapHandler
- type ReturnMsg
- func (*ReturnMsg) Descriptor() ([]byte, []int)
- func (m *ReturnMsg) GetMetadata() *weave.Metadata
- func (m *ReturnMsg) GetSwapID() []byte
- func (m *ReturnMsg) Marshal() (dAtA []byte, err error)
- func (m *ReturnMsg) MarshalTo(dAtA []byte) (int, error)
- func (ReturnMsg) Path() string
- func (*ReturnMsg) ProtoMessage()
- func (m *ReturnMsg) Reset()
- func (m *ReturnMsg) Size() (n int)
- func (m *ReturnMsg) String() string
- func (m *ReturnMsg) Unmarshal(dAtA []byte) error
- func (m *ReturnMsg) Validate() error
- func (m *ReturnMsg) XXX_DiscardUnknown()
- func (m *ReturnMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *ReturnMsg) XXX_Merge(src proto.Message)
- func (m *ReturnMsg) XXX_Size() int
- func (m *ReturnMsg) XXX_Unmarshal(b []byte) error
- type ReturnSwapHandler
- type Swap
- func (*Swap) Descriptor() ([]byte, []int)
- func (m *Swap) GetAddress() github_com_iov_one_weave.Address
- func (m *Swap) GetDestination() github_com_iov_one_weave.Address
- func (m *Swap) GetMemo() string
- func (m *Swap) GetMetadata() *weave.Metadata
- func (m *Swap) GetPreimageHash() []byte
- func (m *Swap) GetSource() github_com_iov_one_weave.Address
- func (m *Swap) GetTimeout() github_com_iov_one_weave.UnixTime
- func (m *Swap) Marshal() (dAtA []byte, err error)
- func (m *Swap) MarshalTo(dAtA []byte) (int, error)
- func (*Swap) ProtoMessage()
- func (m *Swap) Reset()
- func (m *Swap) Size() (n int)
- func (m *Swap) String() string
- func (m *Swap) Unmarshal(dAtA []byte) error
- func (s *Swap) Validate() error
- func (m *Swap) XXX_DiscardUnknown()
- func (m *Swap) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *Swap) XXX_Merge(src proto.Message)
- func (m *Swap) XXX_Size() int
- func (m *Swap) XXX_Unmarshal(b []byte) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflowCodec = fmt.Errorf("proto: integer overflow") )
Functions ¶
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) GetDestination ¶ added in v0.18.0
func (m *CreateMsg) GetDestination() github_com_iov_one_weave.Address
func (*CreateMsg) GetMetadata ¶ added in v0.17.0
func (*CreateMsg) GetPreimageHash ¶ added in v0.17.0
func (*CreateMsg) GetSource ¶ added in v0.18.0
func (m *CreateMsg) GetSource() github_com_iov_one_weave.Address
func (*CreateMsg) GetTimeout ¶ added in v0.17.0
func (m *CreateMsg) GetTimeout() github_com_iov_one_weave.UnixTime
func (*CreateMsg) ProtoMessage ¶ added in v0.17.0
func (*CreateMsg) ProtoMessage()
func (*CreateMsg) XXX_DiscardUnknown ¶ added in v0.17.0
func (m *CreateMsg) XXX_DiscardUnknown()
func (*CreateMsg) XXX_Marshal ¶ added in v0.17.0
func (*CreateMsg) XXX_Unmarshal ¶ added in v0.17.0
type CreateSwapHandler ¶
type CreateSwapHandler struct {
// contains filtered or unexported fields
}
CreateSwapHandler creates a swap
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.
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) GetMetadata ¶ added in v0.19.0
func (*ReturnMsg) ProtoMessage ¶ added in v0.19.0
func (*ReturnMsg) ProtoMessage()
func (*ReturnMsg) XXX_DiscardUnknown ¶ added in v0.19.0
func (m *ReturnMsg) XXX_DiscardUnknown()
func (*ReturnMsg) XXX_Marshal ¶ added in v0.19.0
func (*ReturnMsg) XXX_Unmarshal ¶ added in v0.19.0
type ReturnSwapHandler ¶
type ReturnSwapHandler struct {
// contains filtered or unexported fields
}
ReturnSwapHandler returns funds to the sender when swap timed out.
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 ¶
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) 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) GetMetadata ¶
func (*Swap) GetPreimageHash ¶
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) ProtoMessage ¶
func (*Swap) ProtoMessage()
func (*Swap) XXX_DiscardUnknown ¶
func (m *Swap) XXX_DiscardUnknown()