Skip to content

Commit

Permalink
feat: Added getTransactionHash to transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
andrix10 committed Oct 23, 2020
1 parent c77c65c commit 5fb3075
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package hedera

import (
"bytes"
"crypto/sha512"
"encoding/hex"
"time"

protobuf "github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -63,6 +65,27 @@ func TransactionFromBytes(bytes []byte) Transaction {
return tx
}

func (transaction *Transaction) getTransactionHash() map[AccountID][]byte {
hash := sha512.New384()
bytes, err := protobuf.Marshal(transaction.pbBody)
if err != nil {
// This should be unreachable
// From the documentation this appears to only be possible if there are missing proto types
panic(err)
}
hash.Write(bytes)

byteHash := hex.EncodeToString(hash.Sum(nil))

transactionHash := make(map[AccountID][]byte)

for _, node := range transaction.nodeIDs {
transactionHash[node] = []byte(byteHash)
}

return transactionHash
}

func (transaction *Transaction) initFee(client *Client) {
if client != nil && transaction.pbBody.TransactionFee == 0 {
transaction.SetMaxTransactionFee(client.maxTransactionFee)
Expand Down
4 changes: 4 additions & 0 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ func TestTransactionSerializationDeserialization(t *testing.T) {

assert.NoError(t, err)

_ = transaction.getTransactionHash()

assert.NoError(t, err)

txBytes, err := transaction.MarshalBinary()

assert.NoError(t, err)
Expand Down
7 changes: 7 additions & 0 deletions utilities_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ func newMockTransaction() (Transaction, error) {
return Transaction{}, err
}

nodeIDs := make([]AccountID, 1)
nodeIDs[0], err = AccountIDFromString("0.0.4")
if err != nil {
return Transaction{}, err
}

tx, err := NewCryptoTransferTransaction().
AddSender(AccountID{Account: 2}, HbarFromTinybar(100)).
AddRecipient(AccountID{Account: 3}, HbarFromTinybar(100)).
SetMaxTransactionFee(HbarFrom(1, HbarUnits.Hbar)).
SetTransactionID(testTransactionID).
SetNodeAccountIDs(nodeIDs).
FreezeWith(client)

if err != nil {
Expand Down

0 comments on commit 5fb3075

Please sign in to comment.