2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-16 00:12:52 +00:00
loop/loopdb/loopout.go

266 lines
6.3 KiB
Go
Raw Normal View History

package loopdb
2019-03-06 20:13:50 +00:00
import (
"bytes"
"encoding/binary"
"fmt"
"time"
"github.com/btcsuite/btcd/chaincfg"
2019-03-06 20:13:50 +00:00
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
)
// LoopOutContract contains the data that is serialized to persistent storage
// for pending swaps.
type LoopOutContract struct {
// SwapContract contains basic information pertaining to this swap.
// Each swap type has a base contract, then swap specific information
// on top of it.
SwapContract
2019-03-06 20:13:50 +00:00
// DestAddr is the destination address of the loop out swap.
DestAddr btcutil.Address
2019-03-06 20:13:50 +00:00
// SwapInvoice is the invoice that is to be paid by the client to
// initiate the loop out swap.
SwapInvoice string
2019-03-06 20:13:50 +00:00
// MaxSwapRoutingFee is the maximum off-chain fee in msat that may be
// paid for the swap payment to the server.
MaxSwapRoutingFee btcutil.Amount
2019-03-06 20:13:50 +00:00
// SweepConfTarget specifies the targeted confirmation target for the
// client sweep tx.
SweepConfTarget int32
2019-03-06 20:13:50 +00:00
// TargetChannel is the channel to loop out. If zero, any channel may
// be used.
UnchargeChannel *uint64
// PrepayInvoice is the invoice that the client should pay to the
// server that will be returned if the swap is complete.
PrepayInvoice string
// MaxPrepayRoutingFee is the maximum off-chain fee in msat that may be
// paid for the prepayment to the server.
MaxPrepayRoutingFee btcutil.Amount
// SwapPublicationDeadline is a timestamp that the server commits to
// have the on-chain swap published by. It is set by the client to
// allow the server to delay the publication in exchange for possibly
// lower fees.
SwapPublicationDeadline time.Time
2019-03-06 20:13:50 +00:00
}
// LoopOut is a combination of the contract and the updates.
type LoopOut struct {
Loop
2019-03-06 20:13:50 +00:00
// Contract is the active contract for this swap. It describes the
// precise details of the swap including the final fee, CLTV value,
// etc.
Contract *LoopOutContract
2019-03-06 20:13:50 +00:00
}
// LastUpdateTime returns the last update time of this swap.
func (s *LoopOut) LastUpdateTime() time.Time {
lastUpdate := s.LastUpdate()
if lastUpdate == nil {
return s.Contract.InitiationTime
}
2019-03-06 20:13:50 +00:00
return lastUpdate.Time
2019-03-06 20:13:50 +00:00
}
func deserializeLoopOutContract(value []byte, chainParams *chaincfg.Params) (
*LoopOutContract, error) {
2019-03-06 20:13:50 +00:00
r := bytes.NewReader(value)
contract := LoopOutContract{}
var err error
var unixNano int64
if err := binary.Read(r, byteOrder, &unixNano); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
contract.InitiationTime = time.Unix(0, unixNano)
2019-03-06 20:13:50 +00:00
if err := binary.Read(r, byteOrder, &contract.Preimage); err != nil {
return nil, err
2019-03-06 20:13:50 +00:00
}
2019-10-07 15:29:24 +00:00
err = binary.Read(r, byteOrder, &contract.AmountRequested)
if err != nil {
return nil, err
}
contract.PrepayInvoice, err = wire.ReadVarString(r, 0)
2019-03-06 20:13:50 +00:00
if err != nil {
return nil, err
}
n, err := r.Read(contract.SenderKey[:])
2019-03-06 20:13:50 +00:00
if err != nil {
return nil, err
}
if n != keyLength {
return nil, fmt.Errorf("sender key has invalid length")
}
2019-03-06 20:13:50 +00:00
n, err = r.Read(contract.ReceiverKey[:])
2019-03-06 20:13:50 +00:00
if err != nil {
return nil, err
}
if n != keyLength {
return nil, fmt.Errorf("receiver key has invalid length")
}
2019-03-06 20:13:50 +00:00
if err := binary.Read(r, byteOrder, &contract.CltvExpiry); err != nil {
return nil, err
}
if err := binary.Read(r, byteOrder, &contract.MaxMinerFee); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Read(r, byteOrder, &contract.MaxSwapFee); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Read(r, byteOrder, &contract.MaxPrepayRoutingFee); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Read(r, byteOrder, &contract.InitiationHeight); err != nil {
return nil, err
2019-03-06 20:13:50 +00:00
}
addr, err := wire.ReadVarString(r, 0)
if err != nil {
return nil, err
}
contract.DestAddr, err = btcutil.DecodeAddress(addr, chainParams)
if err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
contract.SwapInvoice, err = wire.ReadVarString(r, 0)
if err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Read(r, byteOrder, &contract.SweepConfTarget); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Read(r, byteOrder, &contract.MaxSwapRoutingFee); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
var unchargeChannel uint64
if err := binary.Read(r, byteOrder, &unchargeChannel); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if unchargeChannel != 0 {
contract.UnchargeChannel = &unchargeChannel
}
2019-03-06 20:13:50 +00:00
var deadlineNano int64
err = binary.Read(r, byteOrder, &deadlineNano)
if err != nil {
return nil, err
}
contract.SwapPublicationDeadline = time.Unix(0, deadlineNano)
return &contract, nil
2019-03-06 20:13:50 +00:00
}
func serializeLoopOutContract(swap *LoopOutContract) (
[]byte, error) {
var b bytes.Buffer
if err := binary.Write(&b, byteOrder, swap.InitiationTime.UnixNano()); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Write(&b, byteOrder, swap.Preimage); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Write(&b, byteOrder, swap.AmountRequested); err != nil {
return nil, err
}
2019-03-06 20:13:50 +00:00
if err := wire.WriteVarString(&b, 0, swap.PrepayInvoice); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
n, err := b.Write(swap.SenderKey[:])
2019-03-06 20:13:50 +00:00
if err != nil {
return nil, err
}
if n != keyLength {
return nil, fmt.Errorf("sender key has invalid length")
}
n, err = b.Write(swap.ReceiverKey[:])
2019-03-06 20:13:50 +00:00
if err != nil {
return nil, err
}
if n != keyLength {
return nil, fmt.Errorf("receiver key has invalid length")
}
if err := binary.Write(&b, byteOrder, swap.CltvExpiry); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Write(&b, byteOrder, swap.MaxMinerFee); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Write(&b, byteOrder, swap.MaxSwapFee); err != nil {
2019-03-06 20:13:50 +00:00
return nil, err
}
if err := binary.Write(&b, byteOrder, swap.MaxPrepayRoutingFee); err != nil {
return nil, err
2019-03-06 20:13:50 +00:00
}
if err := binary.Write(&b, byteOrder, swap.InitiationHeight); err != nil {
return nil, err
2019-03-06 20:13:50 +00:00
}
addr := swap.DestAddr.String()
if err := wire.WriteVarString(&b, 0, addr); err != nil {
return nil, err
2019-03-06 20:13:50 +00:00
}
if err := wire.WriteVarString(&b, 0, swap.SwapInvoice); err != nil {
return nil, err
2019-03-06 20:13:50 +00:00
}
if err := binary.Write(&b, byteOrder, swap.SweepConfTarget); err != nil {
return nil, err
2019-03-06 20:13:50 +00:00
}
if err := binary.Write(&b, byteOrder, swap.MaxSwapRoutingFee); err != nil {
return nil, err
2019-03-06 20:13:50 +00:00
}
var unchargeChannel uint64
if swap.UnchargeChannel != nil {
unchargeChannel = *swap.UnchargeChannel
2019-03-06 20:13:50 +00:00
}
if err := binary.Write(&b, byteOrder, unchargeChannel); err != nil {
return nil, err
2019-03-06 20:13:50 +00:00
}
err = binary.Write(&b, byteOrder, swap.SwapPublicationDeadline.UnixNano())
if err != nil {
return nil, err
}
return b.Bytes(), nil
2019-03-06 20:13:50 +00:00
}