From 2d8bc99d9e653bf41645d755ef9aeca709d171ba Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 19 Feb 2019 17:05:45 -0800 Subject: [PATCH] lntypes/preimage: add MakePreimage initializer --- invoices/invoiceregistry.go | 2 +- lntypes/preimage.go | 28 ++++++++++++++-------------- server.go | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index 0c9d93bf..31c81bc6 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -23,7 +23,7 @@ var ( // All nodes initialized with the flag active will immediately settle // any incoming HTLC whose rHash corresponds with the debug // preimage. - DebugPre, _ = lntypes.NewPreimage(bytes.Repeat([]byte{1}, 32)) + DebugPre, _ = lntypes.MakePreimage(bytes.Repeat([]byte{1}, 32)) // DebugHash is the hash of the default preimage. DebugHash = DebugPre.Hash() diff --git a/lntypes/preimage.go b/lntypes/preimage.go index 4c4289c7..f73d2111 100644 --- a/lntypes/preimage.go +++ b/lntypes/preimage.go @@ -9,8 +9,8 @@ import ( // PreimageSize of array used to store preimagees. const PreimageSize = 32 -// Preimage is used in several of the lightning messages and common structures. It -// represents a payment preimage. +// Preimage is used in several of the lightning messages and common structures. +// It represents a payment preimage. type Preimage [PreimageSize]byte // String returns the Preimage as a hexadecimal string. @@ -18,35 +18,35 @@ func (p Preimage) String() string { return hex.EncodeToString(p[:]) } -// NewPreimage returns a new Preimage from a byte slice. An error is returned if -// the number of bytes passed in is not PreimageSize. -func NewPreimage(newPreimage []byte) (*Preimage, error) { +// MakePreimage returns a new Preimage from a bytes slice. An error is returned +// if the number of bytes passed in is not PreimageSize. +func MakePreimage(newPreimage []byte) (Preimage, error) { nhlen := len(newPreimage) if nhlen != PreimageSize { - return nil, fmt.Errorf("invalid preimage length of %v, want %v", - nhlen, PreimageSize) + return Preimage{}, fmt.Errorf("invalid preimage length of %v, "+ + "want %v", nhlen, PreimageSize) } var preimage Preimage copy(preimage[:], newPreimage) - return &preimage, nil + return preimage, nil } -// NewPreimageFromStr creates a Preimage from a hex preimage string. -func NewPreimageFromStr(newPreimage string) (*Preimage, error) { +// MakePreimageFromStr creates a Preimage from a hex preimage string. +func MakePreimageFromStr(newPreimage string) (Preimage, error) { // Return error if preimage string is of incorrect length. if len(newPreimage) != PreimageSize*2 { - return nil, fmt.Errorf("invalid preimage string length of %v, "+ - "want %v", len(newPreimage), PreimageSize*2) + return Preimage{}, fmt.Errorf("invalid preimage string length "+ + "of %v, want %v", len(newPreimage), PreimageSize*2) } preimage, err := hex.DecodeString(newPreimage) if err != nil { - return nil, err + return Preimage{}, err } - return NewPreimage(preimage) + return MakePreimage(preimage) } // Hash returns the sha256 hash of the preimage. diff --git a/server.go b/server.go index 38df7e34..be886a5f 100644 --- a/server.go +++ b/server.go @@ -318,7 +318,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, cc *chainControl, // HTLCs with the debug R-Hash immediately settled. if cfg.DebugHTLC { kiloCoin := btcutil.Amount(btcutil.SatoshiPerBitcoin * 1000) - s.invoices.AddDebugInvoice(kiloCoin, *invoices.DebugPre) + s.invoices.AddDebugInvoice(kiloCoin, invoices.DebugPre) srvrLog.Debugf("Debug HTLC invoice inserted, preimage=%x, hash=%x", invoices.DebugPre[:], invoices.DebugHash[:]) }