2016-12-22 23:24:48 +03:00
|
|
|
package lnwire
|
|
|
|
|
2017-04-17 01:19:39 +03:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"math"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2017-04-17 01:19:39 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MaxFundingTxOutputs is the maximum number of allowed outputs on a
|
|
|
|
// funding transaction within the protocol. This is due to the fact
|
|
|
|
// that we use 2-bytes to encode the index within the funding output
|
|
|
|
// during the funding workflow. Funding transaction with more outputs
|
|
|
|
// than this are considered invalid within the protocol.
|
|
|
|
MaxFundingTxOutputs = math.MaxUint16
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChannelID is a series of 32-bytes that uniquely identifies all channels
|
|
|
|
// within the network. The ChannelID is computed using the outpoint of the
|
|
|
|
// funding transaction (the txid, and output index). Given a funding output the
|
|
|
|
// ChannelID can be calculated by XOR'ing the big-endian serialization of the
|
2018-04-05 19:52:35 +03:00
|
|
|
// txid and the big-endian serialization of the output index, truncated to
|
|
|
|
// 2 bytes.
|
2017-04-17 01:19:39 +03:00
|
|
|
type ChannelID [32]byte
|
|
|
|
|
2018-03-11 19:12:23 +03:00
|
|
|
// ConnectionWideID is an all-zero ChannelID, which is used to represent a
|
2018-04-18 05:03:27 +03:00
|
|
|
// message intended for all channels to specific peer.
|
2018-03-11 19:12:23 +03:00
|
|
|
var ConnectionWideID = ChannelID{}
|
|
|
|
|
2017-04-17 01:19:39 +03:00
|
|
|
// String returns the string representation of the ChannelID. This is just the
|
|
|
|
// hex string encoding of the ChannelID itself.
|
|
|
|
func (c ChannelID) String() string {
|
|
|
|
return hex.EncodeToString(c[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChanIDFromOutPoint converts a target OutPoint into a ChannelID that is
|
2017-12-18 05:40:05 +03:00
|
|
|
// usable within the network. In order to convert the OutPoint into a ChannelID,
|
2017-04-17 01:19:39 +03:00
|
|
|
// we XOR the lower 2-bytes of the txid within the OutPoint with the big-endian
|
|
|
|
// serialization of the Index of the OutPoint, truncated to 2-bytes.
|
|
|
|
func NewChanIDFromOutPoint(op *wire.OutPoint) ChannelID {
|
|
|
|
// First we'll copy the txid of the outpoint into our channel ID slice.
|
|
|
|
var cid ChannelID
|
|
|
|
copy(cid[:], op.Hash[:])
|
|
|
|
|
|
|
|
// With the txid copied over, we'll now XOR the lower 2-bytes of the
|
|
|
|
// partial channelID with big-endian serialization of output index.
|
2017-09-18 20:45:03 +03:00
|
|
|
xorTxid(&cid, uint16(op.Index))
|
2017-04-17 01:19:39 +03:00
|
|
|
|
|
|
|
return cid
|
|
|
|
}
|
|
|
|
|
|
|
|
// xorTxid performs the transformation needed to transform an OutPoint into a
|
|
|
|
// ChannelID. To do this, we expect the cid parameter to contain the txid
|
|
|
|
// unaltered and the outputIndex to be the output index
|
2017-09-18 20:45:03 +03:00
|
|
|
func xorTxid(cid *ChannelID, outputIndex uint16) {
|
2019-11-28 16:40:20 +03:00
|
|
|
var buf [2]byte
|
|
|
|
binary.BigEndian.PutUint16(buf[:], outputIndex)
|
2017-04-17 01:19:39 +03:00
|
|
|
|
2019-11-28 16:40:20 +03:00
|
|
|
cid[30] ^= buf[0]
|
|
|
|
cid[31] ^= buf[1]
|
2016-12-22 23:24:48 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 01:19:39 +03:00
|
|
|
// GenPossibleOutPoints generates all the possible outputs given a channel ID.
|
|
|
|
// In order to generate these possible outpoints, we perform a brute-force
|
|
|
|
// search through the candidate output index space, performing a reverse
|
|
|
|
// mapping from channelID back to OutPoint.
|
|
|
|
func (c *ChannelID) GenPossibleOutPoints() [MaxFundingTxOutputs]wire.OutPoint {
|
|
|
|
var possiblePoints [MaxFundingTxOutputs]wire.OutPoint
|
2019-11-28 16:40:20 +03:00
|
|
|
for i := uint16(0); i < MaxFundingTxOutputs; i++ {
|
2017-04-17 01:19:39 +03:00
|
|
|
cidCopy := *c
|
2019-11-28 16:40:20 +03:00
|
|
|
xorTxid(&cidCopy, i)
|
2017-04-17 01:19:39 +03:00
|
|
|
|
|
|
|
possiblePoints[i] = wire.OutPoint{
|
|
|
|
Hash: chainhash.Hash(cidCopy),
|
2019-11-28 16:40:20 +03:00
|
|
|
Index: uint32(i),
|
2017-04-17 01:19:39 +03:00
|
|
|
}
|
2016-12-22 23:24:48 +03:00
|
|
|
}
|
2017-04-17 01:19:39 +03:00
|
|
|
|
|
|
|
return possiblePoints
|
2016-12-22 23:24:48 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 01:19:39 +03:00
|
|
|
// IsChanPoint returns true if the OutPoint passed corresponds to the target
|
|
|
|
// ChannelID.
|
|
|
|
func (c ChannelID) IsChanPoint(op *wire.OutPoint) bool {
|
|
|
|
candidateCid := NewChanIDFromOutPoint(op)
|
|
|
|
|
|
|
|
return candidateCid == c
|
2016-12-22 23:24:48 +03:00
|
|
|
}
|