From e3717485f54613ac713569fa800dd4f2400cd0c4 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 18 Sep 2017 19:45:03 +0200 Subject: [PATCH] lnwire: fix bug in wire.OutPoint -> lnwire.ChannelID conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes an existing, unnoticed bug within the lnwire. NewChanIDFromOutPoint function. Two lingering issues cause the function to not do anything at all, meaning that the channel ID, would be the exact same as the actual txid passed in. The first issue was that the xorTxid function wasn’t actually XOR’ing the last two bytes. This was due to the fact that the function wasn’t taking a pointer to the target ChannelID, meaning that the mutation wouldn’t be seen outside of the scope of the function. Second, we had our slicing reversed, rather than buf[30:], we were using buf[:30], meaning that we were weren’t properly filling the buffer with the lower 2-bytes of the passed index. --- lnwire/channel_id.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lnwire/channel_id.go b/lnwire/channel_id.go index cfcc481e..62326c9c 100644 --- a/lnwire/channel_id.go +++ b/lnwire/channel_id.go @@ -41,7 +41,7 @@ func NewChanIDFromOutPoint(op *wire.OutPoint) ChannelID { // With the txid copied over, we'll now XOR the lower 2-bytes of the // partial channelID with big-endian serialization of output index. - xorTxid(cid, uint16(op.Index)) + xorTxid(&cid, uint16(op.Index)) return cid } @@ -49,12 +49,12 @@ func NewChanIDFromOutPoint(op *wire.OutPoint) ChannelID { // 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 -func xorTxid(cid ChannelID, outputIndex uint16) { +func xorTxid(cid *ChannelID, outputIndex uint16) { var buf [32]byte - binary.BigEndian.PutUint16(buf[:30], outputIndex) + binary.BigEndian.PutUint16(buf[30:], outputIndex) - buf[30] = cid[30] ^ buf[30] - buf[31] = cid[31] ^ buf[31] + cid[30] = cid[30] ^ buf[30] + cid[31] = cid[31] ^ buf[31] } // GenPossibleOutPoints generates all the possible outputs given a channel ID. @@ -65,7 +65,7 @@ func (c *ChannelID) GenPossibleOutPoints() [MaxFundingTxOutputs]wire.OutPoint { var possiblePoints [MaxFundingTxOutputs]wire.OutPoint for i := uint32(0); i < MaxFundingTxOutputs; i++ { cidCopy := *c - xorTxid(cidCopy, uint16(i)) + xorTxid(&cidCopy, uint16(i)) possiblePoints[i] = wire.OutPoint{ Hash: chainhash.Hash(cidCopy),