2017-05-01 19:58:08 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// htlcPacket is a wrapper around htlc lnwire update, which adds additional
|
|
|
|
// information which is needed by this package.
|
|
|
|
type htlcPacket struct {
|
2017-06-17 00:49:38 +03:00
|
|
|
// destNode is the first-hop destination of a local created HTLC add
|
|
|
|
// message.
|
|
|
|
destNode [33]byte
|
|
|
|
|
|
|
|
// payHash is the payment hash of the HTLC which was modified by either
|
|
|
|
// a settle or fail action.
|
|
|
|
//
|
2017-05-01 19:58:08 +03:00
|
|
|
// NOTE: This fields is initialized only in settle and fail packets.
|
|
|
|
payHash [sha256.Size]byte
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// dest is the destination of this packet identified by the short
|
|
|
|
// channel ID of the target link.
|
|
|
|
dest lnwire.ShortChannelID
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// src is the source of this packet identified by the short channel ID
|
|
|
|
// of the target link.
|
|
|
|
src lnwire.ShortChannelID
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// amount is the value of the HTLC that is being created or modified.
|
2017-08-22 09:36:43 +03:00
|
|
|
amount lnwire.MilliSatoshi
|
2017-06-17 00:49:38 +03:00
|
|
|
|
|
|
|
// htlc lnwire message type of which depends on switch request type.
|
|
|
|
htlc lnwire.Message
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2017-07-15 06:08:29 +03:00
|
|
|
// obfuscator contains the necessary state to allow the switch to wrap
|
|
|
|
// any forwarded errors in an additional layer of encryption.
|
2017-10-11 05:36:52 +03:00
|
|
|
obfuscator ErrorEncrypter
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2017-07-15 06:08:29 +03:00
|
|
|
// isObfuscated is set to true if an error occurs as soon as the switch
|
|
|
|
// forwards a packet to the link. If so, and this is an error packet,
|
|
|
|
// then this allows the switch to avoid doubly encrypting the error.
|
|
|
|
//
|
|
|
|
// TODO(andrew.shvv) revisit after refactoring the way of returning
|
|
|
|
// errors inside the htlcswitch packet.
|
2017-06-29 16:40:45 +03:00
|
|
|
isObfuscated bool
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// newInitPacket creates htlc switch add packet which encapsulates the add htlc
|
|
|
|
// request and additional information for proper forwarding over htlc switch.
|
|
|
|
func newInitPacket(destNode [33]byte, htlc *lnwire.UpdateAddHTLC) *htlcPacket {
|
2017-05-01 19:58:08 +03:00
|
|
|
return &htlcPacket{
|
2017-06-17 00:49:38 +03:00
|
|
|
destNode: destNode,
|
|
|
|
htlc: htlc,
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// newAddPacket creates htlc switch add packet which encapsulates the add htlc
|
|
|
|
// request and additional information for proper forwarding over htlc switch.
|
|
|
|
func newAddPacket(src, dest lnwire.ShortChannelID,
|
2017-10-11 05:36:52 +03:00
|
|
|
htlc *lnwire.UpdateAddHTLC, e ErrorEncrypter) *htlcPacket {
|
2017-06-17 00:49:38 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return &htlcPacket{
|
2017-06-29 16:40:45 +03:00
|
|
|
dest: dest,
|
|
|
|
src: src,
|
|
|
|
htlc: htlc,
|
2017-10-11 05:36:52 +03:00
|
|
|
obfuscator: e,
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSettlePacket creates htlc switch ack/settle packet which encapsulates the
|
|
|
|
// settle htlc request which should be created and sent back by last hope in
|
|
|
|
// htlc path.
|
2017-06-17 00:49:38 +03:00
|
|
|
func newSettlePacket(src lnwire.ShortChannelID, htlc *lnwire.UpdateFufillHTLC,
|
2017-08-22 09:36:43 +03:00
|
|
|
payHash [sha256.Size]byte, amount lnwire.MilliSatoshi) *htlcPacket {
|
2017-06-17 00:49:38 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return &htlcPacket{
|
|
|
|
src: src,
|
|
|
|
payHash: payHash,
|
|
|
|
htlc: htlc,
|
|
|
|
amount: amount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newFailPacket creates htlc switch fail packet which encapsulates the fail
|
|
|
|
// htlc request which propagated back to the original hope who sent the htlc
|
2017-06-17 00:49:38 +03:00
|
|
|
// add request if something wrong happened on the path to the final
|
|
|
|
// destination.
|
|
|
|
func newFailPacket(src lnwire.ShortChannelID, htlc *lnwire.UpdateFailHTLC,
|
2017-08-22 09:36:43 +03:00
|
|
|
payHash [sha256.Size]byte, amount lnwire.MilliSatoshi,
|
|
|
|
isObfuscated bool) *htlcPacket {
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return &htlcPacket{
|
2017-06-29 16:40:45 +03:00
|
|
|
src: src,
|
|
|
|
payHash: payHash,
|
|
|
|
htlc: htlc,
|
|
|
|
amount: amount,
|
|
|
|
isObfuscated: isObfuscated,
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
}
|