From 5fb307526aaabbcbaee4bfc0f5388b0020832592 Mon Sep 17 00:00:00 2001 From: Andrei Kuzmiankov Date: Thu, 22 Oct 2020 17:42:47 -0700 Subject: [PATCH] feat: Added getTransactionHash to transaction --- transaction.go | 23 +++++++++++++++++++++++ transaction_test.go | 4 ++++ utilities_for_test.go | 7 +++++++ 3 files changed, 34 insertions(+) diff --git a/transaction.go b/transaction.go index e5d80f56d..79cfb38cb 100644 --- a/transaction.go +++ b/transaction.go @@ -2,6 +2,8 @@ package hedera import ( "bytes" + "crypto/sha512" + "encoding/hex" "time" protobuf "github.com/golang/protobuf/proto" @@ -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) diff --git a/transaction_test.go b/transaction_test.go index 9167e9259..fc9913427 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -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) diff --git a/utilities_for_test.go b/utilities_for_test.go index 0bcec408b..7ea3494fa 100644 --- a/utilities_for_test.go +++ b/utilities_for_test.go @@ -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 {