diff --git a/wire/leaf_test.go b/wire/leaf_test.go index c093dc45..250b9a9e 100644 --- a/wire/leaf_test.go +++ b/wire/leaf_test.go @@ -6,6 +6,7 @@ package wire import ( "bytes" + "crypto/sha512" "encoding/hex" "encoding/json" "fmt" @@ -630,3 +631,67 @@ func TestIsCompact(t *testing.T) { } } } + +func TestLeafHash(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + ld LeafData + }{ + { + name: "Testnet3 tx 061bb0bf... from block 1600000", + ld: LeafData{ + BlockHash: *newHashFromStr("00000000000172ff8a4e14441512072bacaf8d38b995a3fcd2f8435efc61717d"), + OutPoint: OutPoint{ + Hash: *newHashFromStr("061bb0bf3a1b9df13773da06bf92920394887a9c2b8b8772ac06be4e077df5eb"), + Index: 10, + }, + Amount: 200000, + PkScript: hexToBytes("a914e8d74935cfa223f9750a32b18d609cba17a5c3fe87"), + Height: 1599255, + IsCoinBase: false, + }, + }, + { + name: "Mainnet coinbase tx fa201b65... from block 573123", + ld: LeafData{ + BlockHash: *newHashFromStr("000000000000000000278eb9386b4e70b850a4ec21907af3a27f50330b7325aa"), + OutPoint: OutPoint{ + Hash: *newHashFromStr("fa201b650eef761f5701afbb610e4a211b86985da4745aec3ac0f4b7a8e2c8d2"), + Index: 0, + }, + Amount: 1315080370, + PkScript: hexToBytes("76a9142cc2b87a28c8a097f48fcc1d468ced6e7d39958d88ac"), + Height: 573123, + IsCoinBase: true, + }, + }, + } + + // Just a hash func for sanity checking here. + hashFunc := func(ld LeafData) [32]byte { + shaTag := sha512.Sum512(chainhash.TagUtreexoV1) + preimage := make([]byte, 0, (64*2)+ld.SerializeSize()) + preimage = append(preimage, shaTag[:]...) + preimage = append(preimage, shaTag[:]...) + var buf bytes.Buffer + err := ld.Serialize(&buf) + if err != nil { + t.Fatal(err) + } + preimage = append(preimage, buf.Bytes()...) + + return sha512.Sum512_256(preimage) + } + + for _, test := range tests { + got := test.ld.LeafHash() + expect := hashFunc(test.ld) + if got != expect { + t.Fatalf("expect %s but got %s", + hex.EncodeToString(expect[:]), + hex.EncodeToString(got[:])) + } + } +}