Documentation ¶
Overview ¶
Package chainhash provides abstracted hash functionality.
This package provides a generic hash type and associated functions that allows the specific hash algorithm to be abstracted.
Index ¶
- Constants
- Variables
- func Decode(dst *Hash, src string) error
- func DoubleHashB(b []byte) []byte
- func HashB(b []byte) []byte
- func NextPowerOfTwo(n int) int
- func ValidateCoinbaseMerkleTreeProof(txHash Hash, proof []Hash, expectedRoot Hash) bool
- func ValidateMerkleTreeRoot(txHashes []Hash, expectedRoot Hash) bool
- type Hash
- func BuildCoinbaseMerkleTreeProof(txHashes []Hash) []Hash
- func BuildMerkleTreeStore(txHashes []Hash) []*Hash
- func CoinbaseMerkleTreeProofRoot(txHash Hash, proof []Hash) Hash
- func DoubleHashH(b []byte) Hash
- func HashH(b []byte) Hash
- func HashMerkleBranches(left *Hash, right *Hash) *Hash
- func MerkleTreeRoot(txHashes []Hash) Hash
- func NewHash(newHash []byte) (*Hash, error)
- func NewHashFromStr(hash string) (*Hash, error)
Constants ¶
const HashSize = 32
HashSize of array used to store hashes. See Hash.
const MaxHashStringSize = HashSize * 2
MaxHashStringSize is the maximum length of a Hash hash string.
Variables ¶
var ErrHashStrSize = fmt.Errorf("max hash string length is %v bytes", MaxHashStringSize)
ErrHashStrSize describes an error that indicates the caller specified a hash string that has too many characters.
Functions ¶
func Decode ¶
Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a destination.
func DoubleHashB ¶
DoubleHashB calculates hash(hash(b)) and returns the resulting bytes.
func NextPowerOfTwo ¶
NextPowerOfTwo returns the next highest power of two from a given number if it is not already a power of two. This is a helper function used during the calculation of a merkle tree.
func ValidateCoinbaseMerkleTreeProof ¶ added in v0.4.0
func ValidateMerkleTreeRoot ¶ added in v0.4.0
Types ¶
type Hash ¶
Hash is used in several of the bitcoin messages and common structures. It typically represents the double sha256 of data.
var ZeroHash Hash
ZeroHash is the zero value for a chainhash.Hash and is defined as a package level variable to avoid the need to create a new instance every time a check is needed.
func BuildCoinbaseMerkleTreeProof ¶ added in v0.4.0
nolint: gocritic
func BuildMerkleTreeStore ¶
BuildMerkleTreeStore creates a merkle tree from a slice of transactions, stores it using a linear array, and returns a slice of the backing array. A linear array was chosen as opposed to an actual tree structure since it uses about half as much memory. The following describes a merkle tree and how it is stored in a linear array.
A merkle tree is a tree in which every non-leaf node is the Hash of its children nodes. A diagram depicting how this works for bitcoin transactions where h(x) is a double sha256 follows:
root = h1234 = h(h12 + h34) / \ h12 = h(h1 + h2) h34 = h(h3 + h4) / \ / \ h1 = h(tx1) h2 = h(tx2) h3 = h(tx3) h4 = h(tx4)
The above stored as a linear array is as follows:
[h1 h2 h3 h4 h12 h34 root]
As the above shows, the merkle root is always the last element in the array.
The number of inputs is not always a power of two which results in a balanced tree structure as above. In that case, parent nodes with no children are also zero and parent nodes with only a single left node are calculated by concatenating the left node with itself before hashing. Since this function uses nodes that are pointers to the hashes, empty nodes will be nil.
The additional bool parameter indicates if we are generating the merkle tree using witness transaction id's rather than regular transaction id's. This also presents an additional case wherein the wtxid of the coinbase transaction is the zeroHash.
func CoinbaseMerkleTreeProofRoot ¶ added in v0.4.0
func DoubleHashH ¶
DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a Hash.
func HashMerkleBranches ¶
HashMerkleBranches takes two hashes, treated as the left and right tree nodes, and returns the Hash of their concatenation. This is a helper function used to aid in the generation of a merkle tree.
func MerkleTreeRoot ¶ added in v0.4.0
func NewHash ¶
NewHash returns a new Hash from a byte slice. An error is returned if the number of bytes passed in is not HashSize.
func NewHashFromStr ¶
NewHashFromStr creates a Hash from a hash string. The string should be the hexadecimal string of a byte-reversed hash, but any missing characters result in zero padding at the end of the Hash.
func (*Hash) CloneBytes ¶
CloneBytes returns a copy of the bytes which represent the hash as a byte slice.
NOTE: It is generally cheaper to just slice the hash directly thereby reusing the same bytes rather than calling this method.