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

     1  //go:build all || e2e
     2  // +build all e2e
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  
    29  	"github.com/hashgraph/hedera-protobufs-go/sdk"
    30  	"github.com/hashgraph/hedera-protobufs-go/services"
    31  	"github.com/stretchr/testify/assert"
    32  
    33  	"github.com/stretchr/testify/require"
    34  	protobuf "google.golang.org/protobuf/proto"
    35  )
    36  
    37  func TestIntegrationTransactionAddSignature(t *testing.T) {
    38  	t.Parallel()
    39  	env := NewIntegrationTestEnv(t)
    40  
    41  	newKey, err := PrivateKeyGenerateEd25519()
    42  	require.NoError(t, err)
    43  
    44  	resp, err := NewAccountCreateTransaction().
    45  		SetKey(newKey.PublicKey()).
    46  		SetNodeAccountIDs(env.NodeAccountIDs).
    47  		Execute(env.Client)
    48  	require.NoError(t, err)
    49  
    50  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    51  	require.NoError(t, err)
    52  
    53  	tx, err := NewAccountDeleteTransaction().
    54  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    55  		SetAccountID(*receipt.AccountID).
    56  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
    57  		FreezeWith(env.Client)
    58  	require.NoError(t, err)
    59  
    60  	updateBytes, err := tx.ToBytes()
    61  	require.NoError(t, err)
    62  
    63  	sig1, err := newKey.SignTransaction(&tx.Transaction)
    64  	require.NoError(t, err)
    65  
    66  	tx2, err := TransactionFromBytes(updateBytes)
    67  	require.NoError(t, err)
    68  
    69  	if newTx, ok := tx2.(AccountDeleteTransaction); ok {
    70  		resp, err = newTx.AddSignature(newKey.PublicKey(), sig1).Execute(env.Client)
    71  		require.NoError(t, err)
    72  	}
    73  
    74  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    75  	require.NoError(t, err)
    76  
    77  	err = CloseIntegrationTestEnv(env, nil)
    78  	require.NoError(t, err)
    79  }
    80  
    81  func TestIntegrationTransactionSignTransaction(t *testing.T) {
    82  	t.Parallel()
    83  	env := NewIntegrationTestEnv(t)
    84  
    85  	newKey, err := PrivateKeyGenerateEd25519()
    86  	require.NoError(t, err)
    87  
    88  	resp, err := NewAccountCreateTransaction().
    89  		SetKey(newKey.PublicKey()).
    90  		SetNodeAccountIDs(env.NodeAccountIDs).
    91  		Execute(env.Client)
    92  	require.NoError(t, err)
    93  
    94  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    95  	require.NoError(t, err)
    96  
    97  	tx, err := NewAccountDeleteTransaction().
    98  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    99  		SetAccountID(*receipt.AccountID).
   100  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   101  		FreezeWith(env.Client)
   102  	require.NoError(t, err)
   103  
   104  	_, err = newKey.SignTransaction(&tx.Transaction)
   105  	require.NoError(t, err)
   106  
   107  	resp, err = tx.Execute(env.Client)
   108  	require.NoError(t, err)
   109  
   110  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   111  	require.NoError(t, err)
   112  
   113  	err = CloseIntegrationTestEnv(env, nil)
   114  	require.NoError(t, err)
   115  }
   116  
   117  func TestIntegrationTransactionGetHash(t *testing.T) {
   118  	t.Parallel()
   119  	env := NewIntegrationTestEnv(t)
   120  
   121  	newKey, err := PrivateKeyGenerateEd25519()
   122  	require.NoError(t, err)
   123  
   124  	tx, err := NewAccountCreateTransaction().
   125  		SetKey(newKey.PublicKey()).
   126  		SetNodeAccountIDs(env.NodeAccountIDs).
   127  		FreezeWith(env.Client)
   128  	require.NoError(t, err)
   129  
   130  	tx, err = tx.SignWithOperator(env.Client)
   131  	require.NoError(t, err)
   132  
   133  	hash, err := tx.GetTransactionHash()
   134  	require.NoError(t, err)
   135  
   136  	resp, err := tx.Execute(env.Client)
   137  	require.NoError(t, err)
   138  
   139  	record, err := resp.GetRecord(env.Client)
   140  	require.NoError(t, err)
   141  
   142  	assert.Equal(t, hash, record.TransactionHash)
   143  
   144  	err = CloseIntegrationTestEnv(env, nil)
   145  	require.NoError(t, err)
   146  }
   147  
   148  func DisabledTestTransactionFromBytes(t *testing.T) { // nolint
   149  	id := TransactionIDGenerate(AccountID{0, 0, 542348, nil, nil, nil})
   150  
   151  	TransactionBody := services.TransactionBody{
   152  		TransactionID: &services.TransactionID{
   153  			AccountID: &services.AccountID{
   154  				Account: &services.AccountID_AccountNum{AccountNum: 542348},
   155  			},
   156  			TransactionValidStart: &services.Timestamp{
   157  				Seconds: id.ValidStart.Unix(),
   158  				Nanos:   int32(id.ValidStart.Nanosecond()),
   159  			},
   160  		},
   161  		NodeAccountID: &services.AccountID{
   162  			Account: &services.AccountID_AccountNum{AccountNum: 3},
   163  		},
   164  		TransactionFee: 200_000_000,
   165  		TransactionValidDuration: &services.Duration{
   166  			Seconds: 120,
   167  		},
   168  		GenerateRecord: false,
   169  		Memo:           "",
   170  		Data: &services.TransactionBody_CryptoTransfer{
   171  			CryptoTransfer: &services.CryptoTransferTransactionBody{
   172  				Transfers: &services.TransferList{
   173  					AccountAmounts: []*services.AccountAmount{
   174  						{
   175  							AccountID: &services.AccountID{
   176  								Account: &services.AccountID_AccountNum{AccountNum: 47439},
   177  							},
   178  							Amount: 10,
   179  						},
   180  						{
   181  							AccountID: &services.AccountID{
   182  								Account: &services.AccountID_AccountNum{AccountNum: 542348},
   183  							},
   184  							Amount: -10,
   185  						},
   186  					},
   187  				},
   188  			},
   189  		},
   190  	}
   191  
   192  	BodyBytes, err := protobuf.Marshal(&TransactionBody)
   193  	require.NoError(t, err)
   194  
   195  	key1, _ := PrivateKeyFromString("302e020100300506032b6570042204203e7fda6dde63c3cdb3cb5ecf5264324c5faad7c9847b6db093c088838b35a110")
   196  	key2, _ := PrivateKeyFromString("302e020100300506032b65700422042032d3d5a32e9d06776976b39c09a31fbda4a4a0208223da761c26a2ae560c1755")
   197  	key3, _ := PrivateKeyFromString("302e020100300506032b657004220420195a919056d1d698f632c228dbf248bbbc3955adf8a80347032076832b8299f9")
   198  	key4, _ := PrivateKeyFromString("302e020100300506032b657004220420b9962f17f94ffce73a23649718a11638cac4b47095a7a6520e88c7563865be62")
   199  	key5, _ := PrivateKeyFromString("302e020100300506032b657004220420fef68591819080cd9d48b0cbaa10f65f919752abb50ffb3e7411ac66ab22692e")
   200  
   201  	publicKey1 := key1.PublicKey()
   202  	publicKey2 := key2.PublicKey()
   203  	publicKey3 := key3.PublicKey()
   204  	publicKey4 := key4.PublicKey()
   205  	publicKey5 := key5.PublicKey()
   206  
   207  	signature1 := key1.Sign(BodyBytes)
   208  	signature2 := key2.Sign(BodyBytes)
   209  	signature3 := key3.Sign(BodyBytes)
   210  	signature4 := key4.Sign(BodyBytes)
   211  	signature5 := key5.Sign(BodyBytes)
   212  
   213  	signed := services.SignedTransaction{
   214  		BodyBytes: BodyBytes,
   215  		SigMap: &services.SignatureMap{
   216  			SigPair: []*services.SignaturePair{
   217  				{
   218  					PubKeyPrefix: key1.PublicKey().Bytes(),
   219  					Signature: &services.SignaturePair_Ed25519{
   220  						Ed25519: signature1,
   221  					},
   222  				},
   223  				{
   224  					PubKeyPrefix: key2.PublicKey().Bytes(),
   225  					Signature: &services.SignaturePair_Ed25519{
   226  						Ed25519: signature2,
   227  					},
   228  				},
   229  				{
   230  					PubKeyPrefix: key3.PublicKey().Bytes(),
   231  					Signature: &services.SignaturePair_Ed25519{
   232  						Ed25519: signature3,
   233  					},
   234  				},
   235  				{
   236  					PubKeyPrefix: key4.PublicKey().Bytes(),
   237  					Signature: &services.SignaturePair_Ed25519{
   238  						Ed25519: signature4,
   239  					},
   240  				},
   241  				{
   242  					PubKeyPrefix: key5.PublicKey().Bytes(),
   243  					Signature: &services.SignaturePair_Ed25519{
   244  						Ed25519: signature5,
   245  					},
   246  				},
   247  			},
   248  		},
   249  	}
   250  
   251  	bytes, err := protobuf.Marshal(&signed)
   252  	require.NoError(t, err)
   253  
   254  	bytes, err = protobuf.Marshal(&sdk.TransactionList{
   255  		TransactionList: []*services.Transaction{{
   256  			SignedTransactionBytes: bytes,
   257  		}},
   258  	})
   259  	require.NoError(t, err)
   260  
   261  	transaction, err := TransactionFromBytes(bytes)
   262  	require.NoError(t, err)
   263  
   264  	t.Parallel()
   265  	env := NewIntegrationTestEnv(t)
   266  
   267  	switch tx := transaction.(type) {
   268  	case TransferTransaction:
   269  		assert.Equal(t, tx.GetHbarTransfers()[AccountID{0, 0, 542348, nil, nil, nil}].AsTinybar(), int64(-10))
   270  		assert.Equal(t, tx.GetHbarTransfers()[AccountID{0, 0, 47439, nil, nil, nil}].AsTinybar(), int64(10))
   271  
   272  		signatures, err := tx.GetSignatures()
   273  		require.NoError(t, err)
   274  		assert.Contains(t, signatures[AccountID{0, 0, 3, nil, nil, nil}], &publicKey1)
   275  		assert.Contains(t, signatures[AccountID{0, 0, 3, nil, nil, nil}], &publicKey2)
   276  		assert.Contains(t, signatures[AccountID{0, 0, 3, nil, nil, nil}], &publicKey3)
   277  		assert.Contains(t, signatures[AccountID{0, 0, 3, nil, nil, nil}], &publicKey4)
   278  		assert.Contains(t, signatures[AccountID{0, 0, 3, nil, nil, nil}], &publicKey5)
   279  
   280  		assert.Equal(t, len(tx.GetNodeAccountIDs()), 1)
   281  		assert.True(t, tx.GetNodeAccountIDs()[0]._Equals(AccountID{0, 0, 3, nil, nil, nil}))
   282  
   283  		resp, err := tx.Execute(env.Client)
   284  		require.NoError(t, err)
   285  
   286  		_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   287  		require.NoError(t, err)
   288  	default:
   289  		panic("Transaction was not a crypto transfer?")
   290  	}
   291  }
   292  
   293  func TestIntegrationTransactionFailsWhenSigningWithoutFreezing(t *testing.T) {
   294  	t.Parallel()
   295  	env := NewIntegrationTestEnv(t)
   296  
   297  	newKey, err := PrivateKeyGenerateEd25519()
   298  	require.NoError(t, err)
   299  
   300  	tx := NewAccountCreateTransaction().
   301  		SetKey(newKey.PublicKey()).
   302  		SetNodeAccountIDs(env.NodeAccountIDs)
   303  
   304  	_, err = tx.Sign(newKey).Execute(env.Client)
   305  	require.ErrorContains(t, err, "transaction is not frozen")
   306  
   307  	err = CloseIntegrationTestEnv(env, nil)
   308  	require.NoError(t, err)
   309  }