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
// 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()

@ -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.

@ -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[:])
}