lntypes/preimage: add MakePreimage initializer

This commit is contained in:
Conner Fromknecht 2019-02-19 17:05:45 -08:00
parent e8b7f1fca3
commit 2d8bc99d9e
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
3 changed files with 16 additions and 16 deletions

@ -23,7 +23,7 @@ var (
// All nodes initialized with the flag active will immediately settle // All nodes initialized with the flag active will immediately settle
// any incoming HTLC whose rHash corresponds with the debug // any incoming HTLC whose rHash corresponds with the debug
// preimage. // 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 is the hash of the default preimage.
DebugHash = DebugPre.Hash() DebugHash = DebugPre.Hash()

@ -9,8 +9,8 @@ import (
// PreimageSize of array used to store preimagees. // PreimageSize of array used to store preimagees.
const PreimageSize = 32 const PreimageSize = 32
// Preimage is used in several of the lightning messages and common structures. It // Preimage is used in several of the lightning messages and common structures.
// represents a payment preimage. // It represents a payment preimage.
type Preimage [PreimageSize]byte type Preimage [PreimageSize]byte
// String returns the Preimage as a hexadecimal string. // String returns the Preimage as a hexadecimal string.
@ -18,35 +18,35 @@ func (p Preimage) String() string {
return hex.EncodeToString(p[:]) return hex.EncodeToString(p[:])
} }
// NewPreimage returns a new Preimage from a byte slice. An error is returned if // MakePreimage returns a new Preimage from a bytes slice. An error is returned
// the number of bytes passed in is not PreimageSize. // if the number of bytes passed in is not PreimageSize.
func NewPreimage(newPreimage []byte) (*Preimage, error) { func MakePreimage(newPreimage []byte) (Preimage, error) {
nhlen := len(newPreimage) nhlen := len(newPreimage)
if nhlen != PreimageSize { if nhlen != PreimageSize {
return nil, fmt.Errorf("invalid preimage length of %v, want %v", return Preimage{}, fmt.Errorf("invalid preimage length of %v, "+
nhlen, PreimageSize) "want %v", nhlen, PreimageSize)
} }
var preimage Preimage var preimage Preimage
copy(preimage[:], newPreimage) copy(preimage[:], newPreimage)
return &preimage, nil return preimage, nil
} }
// NewPreimageFromStr creates a Preimage from a hex preimage string. // MakePreimageFromStr creates a Preimage from a hex preimage string.
func NewPreimageFromStr(newPreimage string) (*Preimage, error) { func MakePreimageFromStr(newPreimage string) (Preimage, error) {
// Return error if preimage string is of incorrect length. // Return error if preimage string is of incorrect length.
if len(newPreimage) != PreimageSize*2 { if len(newPreimage) != PreimageSize*2 {
return nil, fmt.Errorf("invalid preimage string length of %v, "+ return Preimage{}, fmt.Errorf("invalid preimage string length "+
"want %v", len(newPreimage), PreimageSize*2) "of %v, want %v", len(newPreimage), PreimageSize*2)
} }
preimage, err := hex.DecodeString(newPreimage) preimage, err := hex.DecodeString(newPreimage)
if err != nil { if err != nil {
return nil, err return Preimage{}, err
} }
return NewPreimage(preimage) return MakePreimage(preimage)
} }
// Hash returns the sha256 hash of the preimage. // Hash returns the sha256 hash of the preimage.

@ -318,7 +318,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, cc *chainControl,
// HTLCs with the debug R-Hash immediately settled. // HTLCs with the debug R-Hash immediately settled.
if cfg.DebugHTLC { if cfg.DebugHTLC {
kiloCoin := btcutil.Amount(btcutil.SatoshiPerBitcoin * 1000) 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", srvrLog.Debugf("Debug HTLC invoice inserted, preimage=%x, hash=%x",
invoices.DebugPre[:], invoices.DebugHash[:]) invoices.DebugPre[:], invoices.DebugHash[:])
} }