From 18698663c5c353f647c5bb3be096f773066fbabe Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 15 Jan 2019 10:42:25 +0100 Subject: [PATCH] lnhash: create Hash and Preimage types This commit adds new hash and preimage types. These types are similar to chainhash.Hash, except for that string representations are not reversed. The reason for adding dedicated types and not use [32]byte, is to facilitate logging (%v displays as hex string) and have standard methods to convert from byte slice and string with a length check. --- lntypes/hash.go | 49 ++++++++++++++++++++++++++++++++++++++++ lntypes/preimage.go | 55 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 lntypes/hash.go create mode 100644 lntypes/preimage.go diff --git a/lntypes/hash.go b/lntypes/hash.go new file mode 100644 index 00000000..32214489 --- /dev/null +++ b/lntypes/hash.go @@ -0,0 +1,49 @@ +package lntypes + +import ( + "encoding/hex" + "fmt" +) + +// HashSize of array used to store hashes. +const HashSize = 32 + +// Hash is used in several of the lightning messages and common structures. It +// typically represents a payment hash. +type Hash [HashSize]byte + +// String returns the Hash as a hexadecimal string. +func (hash Hash) String() string { + return hex.EncodeToString(hash[:]) +} + +// NewHash returns a new Hash from a byte slice. An error is returned if +// the number of bytes passed in is not HashSize. +func NewHash(newHash []byte) (*Hash, error) { + nhlen := len(newHash) + if nhlen != HashSize { + return nil, fmt.Errorf("invalid hash length of %v, want %v", + nhlen, HashSize) + } + + var hash Hash + copy(hash[:], newHash) + + return &hash, nil +} + +// NewHashFromStr creates a Hash from a hex hash string. +func NewHashFromStr(newHash string) (*Hash, error) { + // Return error if hash string is of incorrect length. + if len(newHash) != HashSize*2 { + return nil, fmt.Errorf("invalid hash string length of %v, "+ + "want %v", len(newHash), HashSize*2) + } + + hash, err := hex.DecodeString(newHash) + if err != nil { + return nil, err + } + + return NewHash(hash) +} diff --git a/lntypes/preimage.go b/lntypes/preimage.go new file mode 100644 index 00000000..4c4289c7 --- /dev/null +++ b/lntypes/preimage.go @@ -0,0 +1,55 @@ +package lntypes + +import ( + "crypto/sha256" + "encoding/hex" + "fmt" +) + +// 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. +type Preimage [PreimageSize]byte + +// String returns the Preimage as a hexadecimal string. +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) { + nhlen := len(newPreimage) + if nhlen != PreimageSize { + return nil, fmt.Errorf("invalid preimage length of %v, want %v", + nhlen, PreimageSize) + } + + var preimage Preimage + copy(preimage[:], newPreimage) + + return &preimage, nil +} + +// NewPreimageFromStr creates a Preimage from a hex preimage string. +func NewPreimageFromStr(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) + } + + preimage, err := hex.DecodeString(newPreimage) + if err != nil { + return nil, err + } + + return NewPreimage(preimage) +} + +// Hash returns the sha256 hash of the preimage. +func (p *Preimage) Hash() Hash { + return Hash(sha256.Sum256(p[:])) +}