2016-01-05 19:19:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-06-21 07:55:07 +03:00
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
2016-01-05 19:19:22 +03:00
|
|
|
)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// HTLCAddRequest is the message sent by Alice to Bob when she wishes to add an
|
|
|
|
// HTLC to his remote commitment transaction. In addition to information
|
|
|
|
// detailing the value, and contract type of the HTLC, and onion blob is also
|
|
|
|
// included which allows Bob to derive the next hop in the route. The HTLC
|
|
|
|
// added by this message is to be added to the remote node's "pending" HTLC's.
|
|
|
|
// A subsequent CommitSignature message will move the pending HTLC to the newly
|
|
|
|
// created commitment transaction, marking them as "staged".
|
2016-01-05 19:19:22 +03:00
|
|
|
type HTLCAddRequest struct {
|
2016-06-21 07:55:07 +03:00
|
|
|
// ChannelPoint is the particular active channel that this HTLCAddRequest
|
2016-05-23 23:54:34 +03:00
|
|
|
// is binded to.
|
2016-06-21 07:55:07 +03:00
|
|
|
ChannelPoint *wire.OutPoint
|
2016-01-05 19:19:22 +03:00
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Expiry is the number of blocks after which this HTLC should expire.
|
|
|
|
// It is the receiver's duty to ensure that the outgoing HTLC has a
|
|
|
|
// sufficient expiry value to allow her to redeem the incmoing HTLC.
|
2016-01-05 19:19:22 +03:00
|
|
|
Expiry uint32
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// Amount to pay in the hop
|
|
|
|
// Difference between hop and first item in blob is the fee to complete
|
2016-05-23 23:54:34 +03:00
|
|
|
|
|
|
|
// Amount is the number of credits this HTLC is worth.
|
2016-01-05 19:19:22 +03:00
|
|
|
Amount CreditsAmount
|
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// RefundContext is for payment cancellation
|
2016-05-23 23:54:34 +03:00
|
|
|
// TODO(j): not currently in use, add later
|
2016-01-15 04:02:23 +03:00
|
|
|
RefundContext HTLCKey
|
2016-01-05 19:19:22 +03:00
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// ContractType defines the particular output script to be used for
|
|
|
|
// this HTLC. This value defaults to zero for regular HTLCs. For
|
|
|
|
// multi-sig HTLCs, then first 4 bit represents N, while the second 4
|
|
|
|
// bits are M, within the N-of-M multi-sig.
|
2016-01-05 19:19:22 +03:00
|
|
|
ContractType uint8
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// RedemptionHashes are the hashes to be used within the HTLC script.
|
|
|
|
// An HTLC is only fufilled once Bob is provided with the required
|
|
|
|
// number of pre-images for each of the listed hashes. For regular HTLC's
|
|
|
|
// this slice only has one hash. However, for "multi-sig" HTLC's, the
|
|
|
|
// length of this slice should be N.
|
|
|
|
RedemptionHashes [][20]byte
|
2016-01-05 19:19:22 +03:00
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// OnionBlob is the raw serialized mix header used to route an HTLC in
|
|
|
|
// a privacy-preserving manner. The mix header is defined currently to
|
|
|
|
// be parsed as a 4-tuple: (groupElement, routingInfo, headerMAC, body).
|
|
|
|
// First the receiving node should use the groupElement, and its current
|
|
|
|
// onion key to derive a shared secret with the source. Once the shared
|
|
|
|
// secret has been derived, the headerMAC should be checked FIRST. Note
|
|
|
|
// that the MAC only covers the routingInfo field. If the MAC matches,
|
|
|
|
// and the shared secret is fresh, then the node should stip off a layer
|
|
|
|
// of encryption, exposing the next hop to be used in the subsequent
|
|
|
|
// HTLCAddRequest message.
|
|
|
|
OnionBlob []byte
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// NewHTLCAddRequest returns a new empty HTLCAddRequest message.
|
|
|
|
func NewHTLCAddRequest() *HTLCAddRequest {
|
|
|
|
return &HTLCAddRequest{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure HTLCAddRequest implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*HTLCAddRequest)(nil)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Decode deserializes a serialized HTLCAddRequest message stored in the passed
|
|
|
|
// io.Reader observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *HTLCAddRequest) Decode(r io.Reader, pver uint32) error {
|
2016-06-21 07:55:07 +03:00
|
|
|
// ChannelPoint(8)
|
2016-01-17 04:14:35 +03:00
|
|
|
// Expiry(4)
|
|
|
|
// Amount(4)
|
|
|
|
// ContractType(1)
|
|
|
|
// RedemptionHashes (numOfHashes * 20 + numOfHashes)
|
2016-05-23 23:54:34 +03:00
|
|
|
// OnionBlog
|
2016-01-05 19:19:22 +03:00
|
|
|
err := readElements(r,
|
2016-06-21 07:55:07 +03:00
|
|
|
&c.ChannelPoint,
|
2016-01-05 19:19:22 +03:00
|
|
|
&c.Expiry,
|
|
|
|
&c.Amount,
|
|
|
|
&c.ContractType,
|
|
|
|
&c.RedemptionHashes,
|
2016-05-23 23:54:34 +03:00
|
|
|
&c.OnionBlob,
|
2016-01-05 19:19:22 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Encode serializes the target HTLCAddRequest into the passed io.Writer observing
|
|
|
|
// the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *HTLCAddRequest) Encode(w io.Writer, pver uint32) error {
|
|
|
|
err := writeElements(w,
|
2016-06-21 07:55:07 +03:00
|
|
|
c.ChannelPoint,
|
2016-01-05 19:19:22 +03:00
|
|
|
c.Expiry,
|
|
|
|
c.Amount,
|
|
|
|
c.ContractType,
|
|
|
|
c.RedemptionHashes,
|
2016-05-23 23:54:34 +03:00
|
|
|
c.OnionBlob,
|
2016-01-05 19:19:22 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Command returns the integer uniquely identifying this message type on the
|
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *HTLCAddRequest) Command() uint32 {
|
|
|
|
return CmdHTLCAddRequest
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for a HTLCAddRequest
|
|
|
|
// complete message observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *HTLCAddRequest) MaxPayloadLength(uint32) uint32 {
|
2016-01-17 04:14:35 +03:00
|
|
|
// base size ~110, but blob can be variable.
|
|
|
|
// shouldn't be bigger than 8K though...
|
2016-01-05 19:19:22 +03:00
|
|
|
return 8192
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Validate performs any necessary sanity checks to ensure all fields present
|
|
|
|
// on the HTLCAddRequest are valid.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *HTLCAddRequest) Validate() error {
|
|
|
|
if c.Amount < 0 {
|
2016-01-17 04:14:35 +03:00
|
|
|
// While fees can be negative, it's too confusing to allow
|
|
|
|
// negative payments. Maybe for some wallets, but not this one!
|
2016-01-05 19:19:22 +03:00
|
|
|
return fmt.Errorf("Amount paid cannot be negative.")
|
|
|
|
}
|
2016-01-17 04:14:35 +03:00
|
|
|
// We're good!
|
2016-01-05 19:19:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// String returns the string representation of the target HTLCAddRequest.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:19:22 +03:00
|
|
|
func (c *HTLCAddRequest) String() string {
|
|
|
|
var redemptionHashes string
|
|
|
|
for i, rh := range c.RedemptionHashes {
|
|
|
|
redemptionHashes += fmt.Sprintf("\n\tSlice\t%d\n", i)
|
2016-05-23 23:54:34 +03:00
|
|
|
redemptionHashes += fmt.Sprintf("\t\tRedemption Hash: %x\n", rh)
|
2016-01-05 19:19:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("\n--- Begin HTLCAddRequest ---\n") +
|
2016-06-21 07:55:07 +03:00
|
|
|
fmt.Sprintf("ChannelPoint:\t%d\n", c.ChannelPoint) +
|
2016-01-05 19:19:22 +03:00
|
|
|
fmt.Sprintf("Expiry:\t\t%d\n", c.Expiry) +
|
|
|
|
fmt.Sprintf("Amount\t\t%d\n", c.Amount) +
|
|
|
|
fmt.Sprintf("ContractType:\t%d (%b)\n", c.ContractType, c.ContractType) +
|
|
|
|
fmt.Sprintf("RedemptionHashes:") +
|
|
|
|
redemptionHashes +
|
2016-05-23 23:54:34 +03:00
|
|
|
fmt.Sprintf("OnionBlob:\t\t\t\t%x\n", c.OnionBlob) +
|
2016-01-05 19:19:22 +03:00
|
|
|
fmt.Sprintf("--- End HTLCAddRequest ---\n")
|
|
|
|
}
|