2021-05-12 19:34:14 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2021-05-26 21:29:37 +00:00
|
|
|
"bytes"
|
2021-05-12 19:34:14 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-05-26 20:57:54 +00:00
|
|
|
"github.com/wealdtech/go-merkletree"
|
|
|
|
|
2021-05-12 19:34:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Transaction struct {
|
2021-05-26 20:57:54 +00:00
|
|
|
Hash []byte
|
2021-08-24 19:36:42 +00:00
|
|
|
MerkleProof merkletree.Proof // sets when transaction is added to block
|
2021-05-26 20:57:54 +00:00
|
|
|
Timestamp time.Time
|
|
|
|
Data []byte
|
2021-05-12 19:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateTransaction(data []byte) *Transaction {
|
2021-05-14 19:42:22 +00:00
|
|
|
timestamp := time.Now()
|
2021-05-12 19:34:14 +00:00
|
|
|
encodedData := hex.EncodeToString(data)
|
2021-07-19 18:41:42 +00:00
|
|
|
hash := crypto.Keccak256([]byte(fmt.Sprintf("%s", encodedData)))
|
2021-05-12 19:34:14 +00:00
|
|
|
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)
|
2021-07-19 18:41:42 +00:00
|
|
|
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
|
|
|
}
|