github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/serialize_deserialize_test.go (about)

     1  //go:build all || e2e
     2  // +build all e2e
     3  
     4  package hedera
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestIntegrationSerializeTransactionDeserializeAndAgainSerializeHasTheSameBytesFreezeBeforeSer(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	newKey, err := PrivateKeyGenerateEd25519()
    17  	require.NoError(t, err)
    18  	env := NewIntegrationTestEnv(t)
    19  
    20  	newBalance := NewHbar(2)
    21  
    22  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
    23  
    24  	transactionOriginal, _ := NewAccountCreateTransaction().
    25  		SetKey(newKey.PublicKey()).
    26  		SetInitialBalance(newBalance).SignWithOperator(env.Client)
    27  	transactionOriginal, _ = transactionOriginal.FreezeWith(env.Client)
    28  
    29  	require.NoError(t, err)
    30  	firstBytes, _ := transactionOriginal.ToBytes()
    31  
    32  	txFromBytes, err := TransactionFromBytes(firstBytes)
    33  	require.NoError(t, err)
    34  
    35  	transaction := txFromBytes.(AccountCreateTransaction)
    36  	secondBytes, err := transaction.ToBytes()
    37  	require.NoError(t, err)
    38  
    39  	assert.Equal(t, firstBytes, secondBytes)
    40  }
    41  
    42  func TestIntegrationSerializeTransactionDeserializeAndAgainSerializeHasTheSameBytesDontFreeze(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	newKey, err := PrivateKeyGenerateEd25519()
    46  	require.NoError(t, err)
    47  
    48  	newBalance := NewHbar(2)
    49  
    50  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
    51  	originalTransaction := NewAccountCreateTransaction().
    52  		SetKey(newKey.PublicKey()).
    53  		SetInitialBalance(newBalance)
    54  	firstBytes, err := originalTransaction.ToBytes()
    55  	require.NoError(t, err)
    56  
    57  	txFromBytes, err := TransactionFromBytes(firstBytes)
    58  	require.NoError(t, err)
    59  	transaction := txFromBytes.(AccountCreateTransaction)
    60  
    61  	secondBytes, err := transaction.ToBytes()
    62  	require.NoError(t, err)
    63  
    64  	assert.Equal(t, firstBytes, secondBytes)
    65  }
    66  
    67  func TestIntegrationAddSignatureSerializeDeserializeAddAnotherSignatureExecute(t *testing.T) {
    68  	t.Parallel()
    69  	env := NewIntegrationTestEnv(t)
    70  
    71  	// Generate new key to use with new account
    72  	newKey, err := GeneratePrivateKey()
    73  	if err != nil {
    74  		panic(err)
    75  	}
    76  	resp, err := NewAccountCreateTransaction().SetKey(newKey).Execute(env.Client)
    77  	receipt, err := resp.GetReceipt(env.Client)
    78  	newAccountId := *receipt.AccountID
    79  
    80  	// Prepare and sign the tx and send it to be signed by another actor
    81  	txBefore := NewTransferTransaction().SetTransactionMemo("Serialize/Deserialize transaction test").AddHbarTransfer(env.OperatorID, NewHbar(-1)).AddHbarTransfer(newAccountId, NewHbar(1)).
    82  		Sign(env.OperatorKey)
    83  
    84  	bytes, err := txBefore.ToBytes()
    85  
    86  	FromBytes, err := TransactionFromBytes(bytes)
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	txFromBytes := FromBytes.(TransferTransaction)
    91  	// Assert the fields are the same:
    92  	assert.Equal(t, txFromBytes.signedTransactions._Length(), txBefore.signedTransactions._Length())
    93  	assert.Equal(t, txFromBytes.memo, txBefore.memo)
    94  
    95  	frozenTx, err := txFromBytes.FreezeWith(env.Client)
    96  	require.NoError(t, err)
    97  
    98  	executed, err := frozenTx.Sign(newKey).Execute(env.Client)
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  	receipt, err = executed.GetReceipt(env.Client)
   103  	assert.Equal(t, receipt.Status, StatusSuccess)
   104  	if err != nil {
   105  		panic(err)
   106  	}
   107  }
   108  
   109  func TestIntegrationTransactionShouldReturnFailedReceiptWhenFieldsAreNotSet(t *testing.T) {
   110  	t.Parallel()
   111  	env := NewIntegrationTestEnv(t)
   112  
   113  	// Prepare and sign the tx and send it to be signed by another actor
   114  	txBefore := NewTransferTransaction().SetTransactionMemo("Serialize/Deserialize transaction test").AddHbarTransfer(env.OperatorID, NewHbar(-1)).
   115  		Sign(env.OperatorKey)
   116  
   117  	bytes, err := txBefore.ToBytes()
   118  
   119  	FromBytes, err := TransactionFromBytes(bytes)
   120  	if err != nil {
   121  		panic(err)
   122  	}
   123  	txFromBytes := FromBytes.(TransferTransaction)
   124  	// Assert the fields are the same:
   125  	assert.Equal(t, txFromBytes.signedTransactions._Length(), txBefore.signedTransactions._Length())
   126  	assert.Equal(t, txFromBytes.memo, txBefore.memo)
   127  
   128  	_, err = txFromBytes.Execute(env.Client)
   129  	assert.Error(t, err)
   130  }
   131  
   132  func TestIntegrationAddSignatureSerializeDeserialiseExecute(t *testing.T) {
   133  	t.Parallel()
   134  	env := NewIntegrationTestEnv(t)
   135  
   136  	newKey, err := PrivateKeyGenerateEd25519()
   137  	require.NoError(t, err)
   138  
   139  	resp, err := NewAccountCreateTransaction().
   140  		SetKey(newKey.PublicKey()).
   141  		SetNodeAccountIDs(env.NodeAccountIDs).
   142  		Execute(env.Client)
   143  	require.NoError(t, err)
   144  
   145  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   146  	require.NoError(t, err)
   147  
   148  	tx, err := NewAccountDeleteTransaction().
   149  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   150  		SetAccountID(*receipt.AccountID).
   151  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   152  		FreezeWith(env.Client)
   153  	require.NoError(t, err)
   154  	updateBytes, err := tx.ToBytes()
   155  	require.NoError(t, err)
   156  
   157  	sig1, err := newKey.SignTransaction(&tx.Transaction)
   158  	require.NoError(t, err)
   159  
   160  	tx2, err := TransactionFromBytes(updateBytes)
   161  	require.NoError(t, err)
   162  
   163  	if newTx, ok := tx2.(AccountDeleteTransaction); ok {
   164  		assert.True(t, newTx.IsFrozen())
   165  		resp, err = newTx.AddSignature(newKey.PublicKey(), sig1).Execute(env.Client)
   166  		require.NoError(t, err)
   167  	}
   168  
   169  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   170  	require.NoError(t, err)
   171  
   172  	err = CloseIntegrationTestEnv(env, nil)
   173  	require.NoError(t, err)
   174  
   175  }
   176  
   177  func TestIntegrationTopicCreateTransactionAfterSerialization(t *testing.T) {
   178  	t.Parallel()
   179  	env := NewIntegrationTestEnv(t)
   180  
   181  	tx := NewTopicCreateTransaction().
   182  		SetAdminKey(env.Client.GetOperatorPublicKey()).
   183  		SetSubmitKey(env.Client.GetOperatorPublicKey()).
   184  		SetTopicMemo(topicMemo)
   185  
   186  	// Serialize unfinished transaction
   187  	bytes, err := tx.ToBytes()
   188  
   189  	fromBytes, err := TransactionFromBytes(bytes)
   190  	require.NoError(t, err)
   191  	// Deserialize and add node accounts transaction
   192  	transaction := fromBytes.(TopicCreateTransaction)
   193  	resp, err := transaction.SetNodeAccountIDs(env.NodeAccountIDs).Execute(env.Client)
   194  	require.NoError(t, err)
   195  
   196  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   197  	require.NoError(t, err)
   198  
   199  	topicID := *receipt.TopicID
   200  	assert.NotNil(t, topicID)
   201  
   202  	info, err := NewTopicInfoQuery().
   203  		SetTopicID(topicID).
   204  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   205  		SetQueryPayment(NewHbar(1)).
   206  		Execute(env.Client)
   207  	require.NoError(t, err)
   208  	assert.NotNil(t, info)
   209  
   210  	assert.Equal(t, topicMemo, info.TopicMemo)
   211  	assert.Equal(t, uint64(0), info.SequenceNumber)
   212  	assert.Equal(t, env.Client.GetOperatorPublicKey().String(), info.AdminKey.String())
   213  
   214  	resp, err = NewTopicDeleteTransaction().
   215  		SetTopicID(topicID).
   216  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   217  		Execute(env.Client)
   218  	require.NoError(t, err)
   219  
   220  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   221  	require.NoError(t, err)
   222  
   223  	err = CloseIntegrationTestEnv(env, nil)
   224  	require.NoError(t, err)
   225  }
   226  
   227  func TestIntegrationTopicSubmitTransactionSerializationDeserialization(t *testing.T) {
   228  	const bigContents2 = `
   229  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur aliquam augue sem, ut mattis dui laoreet a. Curabitur consequat est euismod, scelerisque metus et, tristique dui. Nulla commodo mauris ut faucibus ultricies. Quisque venenatis nisl nec augue tempus, at efficitur elit eleifend. Duis pharetra felis metus, sed dapibus urna vehicula id. Duis non venenatis turpis, sit amet ornare orci. Donec non interdum quam. Sed finibus nunc et risus finibus, non sagittis lorem cursus. Proin pellentesque tempor aliquam. Sed congue nisl in enim bibendum, condimentum vehicula nisi feugiat.
   230  
   231  Suspendisse non sodales arcu. Suspendisse sodales, lorem ac mollis blandit, ipsum neque porttitor nulla, et sodales arcu ante fermentum tellus. Integer sagittis dolor sed augue fringilla accumsan. Cras vitae finibus arcu, sit amet varius dolor. Etiam id finibus dolor, vitae luctus velit. Proin efficitur augue nec pharetra accumsan. Aliquam lobortis nisl diam, vel fermentum purus finibus id. Etiam at finibus orci, et tincidunt turpis. Aliquam imperdiet congue lacus vel facilisis. Phasellus id magna vitae enim dapibus vestibulum vitae quis augue. Morbi eu consequat enim. Maecenas neque nulla, pulvinar sit amet consequat sed, tempor sed magna. Mauris lacinia sem feugiat faucibus aliquet. Etiam congue non turpis at commodo. Nulla facilisi.
   232  
   233  Nunc velit turpis, cursus ornare fringilla eu, lacinia posuere leo. Mauris rutrum ultricies dui et suscipit. Curabitur in euismod ligula. Curabitur vitae faucibus orci. Phasellus volutpat vestibulum diam sit amet vestibulum. In vel purus leo. Nulla condimentum lectus vestibulum lectus faucibus, id lobortis eros consequat. Proin mollis libero elit, vel aliquet nisi imperdiet et. Morbi ornare est velit, in vehicula nunc malesuada quis. Donec vehicula convallis interdum.
   234  `
   235  
   236  	t.Parallel()
   237  	env := NewIntegrationTestEnv(t)
   238  
   239  	tx := NewTopicCreateTransaction().
   240  		SetAdminKey(env.Client.GetOperatorPublicKey()).
   241  		SetSubmitKey(env.Client.GetOperatorPublicKey()).
   242  		SetTopicMemo(topicMemo)
   243  
   244  	// Serialize unfinished transaction
   245  	bytes, err := tx.ToBytes()
   246  
   247  	fromBytes, err := TransactionFromBytes(bytes)
   248  	require.NoError(t, err)
   249  	// Deserialize and add node accounts transaction
   250  	transaction := fromBytes.(TopicCreateTransaction)
   251  	resp, err := transaction.SetNodeAccountIDs(env.NodeAccountIDs).Execute(env.Client)
   252  	require.NoError(t, err)
   253  
   254  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   255  	require.NoError(t, err)
   256  
   257  	topicID := *receipt.TopicID
   258  	assert.NotNil(t, topicID)
   259  
   260  	submitBytes, err := NewTopicMessageSubmitTransaction().
   261  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   262  		SetMessage([]byte(bigContents2)).
   263  		SetTopicID(topicID).ToBytes()
   264  	require.NoError(t, err)
   265  
   266  	fromBytes, err = TransactionFromBytes(submitBytes)
   267  	require.NoError(t, err)
   268  
   269  	topicSubmitTx := fromBytes.(TopicMessageSubmitTransaction)
   270  	_, err = topicSubmitTx.Execute(env.Client)
   271  	require.NoError(t, err)
   272  
   273  	err = CloseIntegrationTestEnv(env, nil)
   274  	require.NoError(t, err)
   275  }