dione/blockchain/types/transaction.go

37 lines
773 B
Go
Raw Normal View History

package types
import (
2021-05-26 21:29:37 +00:00
"bytes"
"encoding/hex"
"fmt"
"time"
"github.com/wealdtech/go-merkletree"
"github.com/ethereum/go-ethereum/crypto"
)
type Transaction struct {
Hash []byte
MerkleProof merkletree.Proof // sets when transaction is added to block
Timestamp time.Time
Data []byte
}
func CreateTransaction(data []byte) *Transaction {
timestamp := time.Now()
encodedData := hex.EncodeToString(data)
hash := crypto.Keccak256([]byte(fmt.Sprintf("%s", encodedData)))
return &Transaction{
Hash: hash,
Timestamp: timestamp,
Data: data,
}
}
2021-05-26 21:29:37 +00:00
func (tx *Transaction) ValidateHash() bool {
2021-07-11 23:23:00 +00:00
encodedData := hex.EncodeToString(tx.Data)
h := crypto.Keccak256([]byte(fmt.Sprintf("%s", encodedData)))
2021-07-10 22:07:04 +00:00
return bytes.Equal(h, tx.Hash)
2021-05-26 21:29:37 +00:00
}