2021-06-11 11:40:32 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/blockchain/types"
|
|
|
|
"github.com/wealdtech/go-merkletree"
|
|
|
|
"github.com/wealdtech/go-merkletree/keccak256"
|
|
|
|
)
|
|
|
|
|
|
|
|
func VerifyTx(blockHeader *types.BlockHeader, tx *types.Transaction) error {
|
2021-08-24 19:36:42 +00:00
|
|
|
if tx.MerkleProof.Hashes == nil {
|
2021-07-11 23:23:00 +00:00
|
|
|
return fmt.Errorf("block transaction doesn't have merkle proof")
|
2021-06-11 11:40:32 +00:00
|
|
|
}
|
2021-08-24 19:36:42 +00:00
|
|
|
txProofVerified, err := merkletree.VerifyProofUsing(tx.Hash, true, &tx.MerkleProof, [][]byte{blockHeader.Hash}, keccak256.New())
|
2021-06-11 11:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to verify tx hash merkle proof: %s", err.Error())
|
|
|
|
}
|
|
|
|
if !txProofVerified {
|
|
|
|
return fmt.Errorf("transaction doesn't present in block hash merkle tree")
|
|
|
|
}
|
|
|
|
if !tx.ValidateHash() {
|
|
|
|
return fmt.Errorf("transaction hash is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|