From 1ef218a73d4c918d70b30fd8137374944e26055d Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 14 Nov 2016 19:03:55 -0800 Subject: [PATCH] lnwire: add the state hint obsfucator to the SingleFundingComplete msg --- lnwire/lnwire.go | 8 +++++++ lnwire/single_funding_complete.go | 30 ++++++++++++++++++-------- lnwire/single_funding_complete_test.go | 6 +++++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lnwire/lnwire.go b/lnwire/lnwire.go index 230897cbc..69d8dc370 100644 --- a/lnwire/lnwire.go +++ b/lnwire/lnwire.go @@ -199,6 +199,10 @@ func writeElement(w io.Writer, element interface{}) error { if _, err := w.Write(b[:]); err != nil { return err } + case [4]byte: + if _, err := w.Write(e[:]); err != nil { + return err + } case []byte: // Enforce the maxmium length of all slices used in the wire // protocol. @@ -450,6 +454,10 @@ func readElement(r io.Reader, element interface{}) error { } *e = wire.BitcoinNet(binary.BigEndian.Uint32(b[:])) return nil + case *[4]byte: + if _, err := io.ReadFull(r, e[:]); err != nil { + return err + } case *[]byte: bytes, err := wire.ReadVarBytes(r, 0, MaxSliceLength, "byte slice") if err != nil { diff --git a/lnwire/single_funding_complete.go b/lnwire/single_funding_complete.go index 2102e8b0b..1a158a617 100644 --- a/lnwire/single_funding_complete.go +++ b/lnwire/single_funding_complete.go @@ -35,18 +35,27 @@ type SingleFundingComplete struct { // will send a pre-image which will allow the initiator to sweep the // initiator's funds if the violate the contract. RevocationKey *btcec.PublicKey + + // StateHintObsfucator is the set of bytes used by the initiator to + // obsfucate the state number encoded within the sequence number for + // the commitment transaction's only input. The initiator generates + // this value by hashing the 0th' state derived from the revocation PRF + // an additional time. + StateHintObsfucator [4]byte } // NewSingleFundingComplete creates, and returns a new empty // SingleFundingResponse. func NewSingleFundingComplete(chanID uint64, fundingPoint *wire.OutPoint, - commitSig *btcec.Signature, revokeKey *btcec.PublicKey) *SingleFundingComplete { + commitSig *btcec.Signature, revokeKey *btcec.PublicKey, + obsfucator [4]byte) *SingleFundingComplete { return &SingleFundingComplete{ - ChannelID: chanID, - FundingOutPoint: fundingPoint, - CommitSignature: commitSig, - RevocationKey: revokeKey, + ChannelID: chanID, + FundingOutPoint: fundingPoint, + CommitSignature: commitSig, + RevocationKey: revokeKey, + StateHintObsfucator: obsfucator, } } @@ -64,7 +73,8 @@ func (s *SingleFundingComplete) Decode(r io.Reader, pver uint32) error { &s.ChannelID, &s.FundingOutPoint, &s.CommitSignature, - &s.RevocationKey) + &s.RevocationKey, + &s.StateHintObsfucator) if err != nil { return err } @@ -86,7 +96,8 @@ func (s *SingleFundingComplete) Encode(w io.Writer, pver uint32) error { s.ChannelID, s.FundingOutPoint, s.CommitSignature, - s.RevocationKey) + s.RevocationKey, + s.StateHintObsfucator) if err != nil { return err } @@ -105,11 +116,11 @@ func (s *SingleFundingComplete) Command() uint32 { // MaxPayloadLength returns the maximum allowed payload length for a // SingleFundingComplete. This is calculated by summing the max length of all // the fields within a SingleFundingResponse. Therefore, the final breakdown -// is: 8 + 36 + 73 = 150 +// is: 8 + 36 + 33 + 73 + 4 = 154 // // This is part of the lnwire.Message interface. func (s *SingleFundingComplete) MaxPayloadLength(uint32) uint32 { - return 150 + return 154 } // Validate examines each populated field within the SingleFundingComplete for @@ -146,5 +157,6 @@ func (s *SingleFundingComplete) String() string { fmt.Sprintf("FundingOutPoint:\t\t\t%x\n", s.FundingOutPoint) + fmt.Sprintf("CommitSignature\t\t\t\t%x\n", s.CommitSignature) + fmt.Sprintf("RevocationKey\t\t\t\t%x\n", rk) + + fmt.Sprintf("StateHintObsfucator\t\t\t%x\n", s.StateHintObsfucator) + fmt.Sprintf("--- End SingleFundingComplete ---\n") } diff --git a/lnwire/single_funding_complete_test.go b/lnwire/single_funding_complete_test.go index 42ace4110..ac052a3a7 100644 --- a/lnwire/single_funding_complete_test.go +++ b/lnwire/single_funding_complete_test.go @@ -7,8 +7,12 @@ import ( ) func TestSingleFundingCompleteWire(t *testing.T) { + var obsfucator [4]byte + copy(obsfucator[:], bytes.Repeat([]byte("k"), 4)) + // First create a new SFC message. - sfc := NewSingleFundingComplete(22, outpoint1, commitSig1, pubKey) + sfc := NewSingleFundingComplete(22, outpoint1, commitSig1, pubKey, + obsfucator) // Next encode the SFC message into an empty bytes buffer. var b bytes.Buffer