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

     1  //go:build all || unit
     2  // +build all unit
     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  	"time"
    29  
    30  	"github.com/hashgraph/hedera-protobufs-go/services"
    31  	protobuf "google.golang.org/protobuf/proto"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestUnitScheduleCreateTransactionValidate(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	client, err := _NewMockClient()
    42  	client.SetLedgerID(*NewLedgerIDTestnet())
    43  	require.NoError(t, err)
    44  	client.SetAutoValidateChecksums(true)
    45  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    46  	require.NoError(t, err)
    47  
    48  	scheduleCreate := NewScheduleCreateTransaction().
    49  		SetPayerAccountID(accountID)
    50  
    51  	err = scheduleCreate.validateNetworkOnIDs(client)
    52  	require.NoError(t, err)
    53  }
    54  
    55  func TestUnitScheduleCreateTransactionValidateWrong(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	client, err := _NewMockClient()
    59  	client.SetLedgerID(*NewLedgerIDTestnet())
    60  	require.NoError(t, err)
    61  	client.SetAutoValidateChecksums(true)
    62  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    63  	require.NoError(t, err)
    64  
    65  	scheduleCreate := NewScheduleCreateTransaction().
    66  		SetPayerAccountID(accountID)
    67  
    68  	err = scheduleCreate.validateNetworkOnIDs(client)
    69  	assert.Error(t, err)
    70  	if err != nil {
    71  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    72  	}
    73  }
    74  
    75  func TestUnitScheduleSignTransactionValidate(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	client, err := _NewMockClient()
    79  	client.SetLedgerID(*NewLedgerIDTestnet())
    80  	require.NoError(t, err)
    81  	client.SetAutoValidateChecksums(true)
    82  	scheduleID, err := ScheduleIDFromString("0.0.123-esxsf")
    83  	require.NoError(t, err)
    84  	err = scheduleID.Validate(client)
    85  	require.NoError(t, err)
    86  	scheduleSign := NewScheduleSignTransaction().
    87  		SetScheduleID(scheduleID)
    88  
    89  	err = scheduleSign.validateNetworkOnIDs(client)
    90  	require.NoError(t, err)
    91  }
    92  
    93  func TestUnitScheduleSignTransactionValidateWrong(t *testing.T) {
    94  	t.Parallel()
    95  
    96  	client, err := _NewMockClient()
    97  	client.SetLedgerID(*NewLedgerIDTestnet())
    98  	require.NoError(t, err)
    99  	client.SetAutoValidateChecksums(true)
   100  	scheduleID, err := ScheduleIDFromString("0.0.123-rmkykd")
   101  	require.NoError(t, err)
   102  	err = scheduleID.Validate(client)
   103  	require.Error(t, err)
   104  	scheduleSign := NewScheduleSignTransaction().
   105  		SetScheduleID(scheduleID)
   106  
   107  	err = scheduleSign.validateNetworkOnIDs(client)
   108  	assert.Error(t, err)
   109  	if err != nil {
   110  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
   111  	}
   112  }
   113  
   114  func TestUnitScheduleDeleteTransactionValidate(t *testing.T) {
   115  	t.Parallel()
   116  
   117  	client, err := _NewMockClient()
   118  	client.SetLedgerID(*NewLedgerIDTestnet())
   119  	require.NoError(t, err)
   120  	client.SetAutoValidateChecksums(true)
   121  	scheduleID, err := ScheduleIDFromString("0.0.123-esxsf")
   122  	require.NoError(t, err)
   123  
   124  	scheduleDelete := NewScheduleDeleteTransaction().
   125  		SetScheduleID(scheduleID)
   126  
   127  	err = scheduleDelete.validateNetworkOnIDs(client)
   128  	require.NoError(t, err)
   129  }
   130  
   131  func TestUnitScheduleDeleteTransactionValidateWrong(t *testing.T) {
   132  	t.Parallel()
   133  
   134  	client, err := _NewMockClient()
   135  	client.SetLedgerID(*NewLedgerIDTestnet())
   136  	require.NoError(t, err)
   137  	client.SetAutoValidateChecksums(true)
   138  	scheduleID, err := ScheduleIDFromString("0.0.123-rmkykd")
   139  	require.NoError(t, err)
   140  
   141  	scheduleDelete := NewScheduleDeleteTransaction().
   142  		SetScheduleID(scheduleID)
   143  
   144  	err = scheduleDelete.validateNetworkOnIDs(client)
   145  	assert.Error(t, err)
   146  	if err != nil {
   147  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
   148  	}
   149  }
   150  
   151  func TestUnitScheduleCreateTransactionGet(t *testing.T) {
   152  	t.Parallel()
   153  
   154  	accountID := AccountID{Account: 7}
   155  
   156  	newKey, err := PrivateKeyGenerateEd25519()
   157  
   158  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   159  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   160  
   161  	transaction, err := NewScheduleCreateTransaction().
   162  		SetTransactionID(transactionID).
   163  		SetNodeAccountIDs(nodeAccountID).
   164  		SetScheduledTransaction(NewTransferTransaction())
   165  
   166  	transaction, err = transaction.
   167  		SetPayerAccountID(accountID).
   168  		SetAdminKey(newKey).
   169  		SetScheduleMemo("").
   170  		SetMaxTransactionFee(NewHbar(10)).
   171  		SetTransactionMemo("").
   172  		SetTransactionValidDuration(60 * time.Second).
   173  		SetRegenerateTransactionID(false).
   174  		Freeze()
   175  	require.NoError(t, err)
   176  
   177  	transaction.GetTransactionID()
   178  	transaction.GetNodeAccountIDs()
   179  
   180  	_, err = transaction.GetTransactionHash()
   181  	require.NoError(t, err)
   182  
   183  	transaction.GetAdminKey()
   184  	transaction.GetPayerAccountID()
   185  	transaction.GetMaxTransactionFee()
   186  	transaction.GetTransactionMemo()
   187  	transaction.GetRegenerateTransactionID()
   188  	_, err = transaction.GetSignatures()
   189  	require.NoError(t, err)
   190  	transaction.GetRegenerateTransactionID()
   191  	transaction.GetMaxTransactionFee()
   192  	transaction.GetRegenerateTransactionID()
   193  	transaction.GetScheduleMemo()
   194  }
   195  
   196  func TestUnitScheduleCreateTransactionSetNothing(t *testing.T) {
   197  	t.Parallel()
   198  
   199  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   200  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   201  
   202  	transaction, err := NewScheduleCreateTransaction().
   203  		SetTransactionID(transactionID).
   204  		SetNodeAccountIDs(nodeAccountID).
   205  		Freeze()
   206  	require.NoError(t, err)
   207  
   208  	transaction.GetTransactionID()
   209  	transaction.GetNodeAccountIDs()
   210  
   211  	_, err = transaction.GetTransactionHash()
   212  	require.NoError(t, err)
   213  
   214  	transaction.GetAdminKey()
   215  	transaction.GetPayerAccountID()
   216  	transaction.GetMaxTransactionFee()
   217  	transaction.GetTransactionMemo()
   218  	transaction.GetRegenerateTransactionID()
   219  	_, err = transaction.GetSignatures()
   220  	require.NoError(t, err)
   221  	transaction.GetRegenerateTransactionID()
   222  	transaction.GetMaxTransactionFee()
   223  	transaction.GetRegenerateTransactionID()
   224  	transaction.GetScheduleMemo()
   225  }
   226  
   227  func TestUnitScheduleDeleteTransactionGet(t *testing.T) {
   228  	t.Parallel()
   229  
   230  	scheduleID := ScheduleID{Schedule: 7}
   231  
   232  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   233  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   234  
   235  	transaction, err := NewScheduleDeleteTransaction().
   236  		SetTransactionID(transactionID).
   237  		SetNodeAccountIDs(nodeAccountID).
   238  		SetScheduleID(scheduleID).
   239  		SetMaxTransactionFee(NewHbar(10)).
   240  		SetTransactionMemo("").
   241  		SetTransactionValidDuration(60 * time.Second).
   242  		SetRegenerateTransactionID(false).
   243  		Freeze()
   244  	require.NoError(t, err)
   245  
   246  	transaction.GetTransactionID()
   247  	transaction.GetNodeAccountIDs()
   248  
   249  	_, err = transaction.GetTransactionHash()
   250  	require.NoError(t, err)
   251  
   252  	transaction.GetScheduleID()
   253  	transaction.GetMaxTransactionFee()
   254  	transaction.GetTransactionMemo()
   255  	transaction.GetRegenerateTransactionID()
   256  	_, err = transaction.GetSignatures()
   257  	require.NoError(t, err)
   258  	transaction.GetRegenerateTransactionID()
   259  	transaction.GetMaxTransactionFee()
   260  	transaction.GetRegenerateTransactionID()
   261  }
   262  
   263  func TestUnitScheduleDeleteTransactionSetNothing(t *testing.T) {
   264  	t.Parallel()
   265  
   266  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   267  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   268  
   269  	transaction, err := NewScheduleDeleteTransaction().
   270  		SetTransactionID(transactionID).
   271  		SetNodeAccountIDs(nodeAccountID).
   272  		Freeze()
   273  	require.NoError(t, err)
   274  
   275  	transaction.GetTransactionID()
   276  	transaction.GetNodeAccountIDs()
   277  
   278  	_, err = transaction.GetTransactionHash()
   279  	require.NoError(t, err)
   280  
   281  	transaction.GetScheduleID()
   282  	transaction.GetMaxTransactionFee()
   283  	transaction.GetTransactionMemo()
   284  	transaction.GetRegenerateTransactionID()
   285  	_, err = transaction.GetSignatures()
   286  	require.NoError(t, err)
   287  	transaction.GetRegenerateTransactionID()
   288  	transaction.GetMaxTransactionFee()
   289  	transaction.GetRegenerateTransactionID()
   290  }
   291  
   292  func TestUnitScheduleCreateTransactionCoverage(t *testing.T) {
   293  	t.Parallel()
   294  
   295  	checksum := "dmqui"
   296  	grpc := time.Second * 30
   297  	account := AccountID{Account: 3, checksum: &checksum}
   298  	nodeAccountID := []AccountID{{Account: 10}}
   299  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   300  
   301  	newKey, err := PrivateKeyGenerateEd25519()
   302  	require.NoError(t, err)
   303  
   304  	client, err := _NewMockClient()
   305  	client.SetLedgerID(*NewLedgerIDTestnet())
   306  	require.NoError(t, err)
   307  	client.SetAutoValidateChecksums(true)
   308  
   309  	accountCreate, err := NewAccountCreateTransaction().
   310  		SetTransactionID(transactionID).
   311  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   312  		Freeze()
   313  	require.NoError(t, err)
   314  
   315  	transaction, err := NewScheduleCreateTransaction().
   316  		SetTransactionID(transactionID).
   317  		SetNodeAccountIDs(nodeAccountID).
   318  		SetAdminKey(newKey).
   319  		SetScheduleMemo("no").
   320  		SetPayerAccountID(account).
   321  		SetExpirationTime(time.Unix(3, 23)).
   322  		SetWaitForExpiry(true).
   323  		SetScheduledTransaction(accountCreate)
   324  	require.NoError(t, err)
   325  
   326  	transaction, err = transaction.
   327  		SetGrpcDeadline(&grpc).
   328  		SetMaxTransactionFee(NewHbar(3)).
   329  		SetMaxRetry(3).
   330  		SetMaxBackoff(time.Second * 30).
   331  		SetMinBackoff(time.Second * 10).
   332  		SetTransactionMemo("no").
   333  		SetTransactionValidDuration(time.Second * 30).
   334  		SetRegenerateTransactionID(false).
   335  		Freeze()
   336  	require.NoError(t, err)
   337  
   338  	transaction.validateNetworkOnIDs(client)
   339  
   340  	transaction.GetTransactionID()
   341  	transaction.GetNodeAccountIDs()
   342  	transaction.GetMaxRetry()
   343  	transaction.GetMaxTransactionFee()
   344  	transaction.GetMaxBackoff()
   345  	transaction.GetMinBackoff()
   346  	transaction.GetRegenerateTransactionID()
   347  	byt, err := transaction.ToBytes()
   348  	require.NoError(t, err)
   349  	_, err = TransactionFromBytes(byt)
   350  	require.NoError(t, err)
   351  	_, err = newKey.SignTransaction(&transaction.Transaction)
   352  	require.NoError(t, err)
   353  
   354  	_, err = transaction.GetTransactionHash()
   355  	require.NoError(t, err)
   356  	transaction.GetMaxTransactionFee()
   357  	transaction.GetTransactionMemo()
   358  	transaction.GetRegenerateTransactionID()
   359  	transaction.GetAdminKey()
   360  	transaction.GetScheduleMemo()
   361  	transaction.GetPayerAccountID()
   362  	transaction.GetExpirationTime()
   363  	transaction.GetWaitForExpiry()
   364  	_, err = transaction.GetSignatures()
   365  	require.NoError(t, err)
   366  	transaction.getName()
   367  	//switch b := txFromBytes.(type) {
   368  	//case ScheduleCreateTransaction:
   369  	//	b.AddSignature(newKey.PublicKey(), sig)
   370  	//}
   371  }
   372  
   373  func TestUnitScheduleCreateTransactionMock(t *testing.T) {
   374  	t.Parallel()
   375  
   376  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
   377  	require.NoError(t, err)
   378  
   379  	call := func(request *services.Transaction) *services.TransactionResponse {
   380  		require.NotEmpty(t, request.SignedTransactionBytes)
   381  		signedTransaction := services.SignedTransaction{}
   382  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   383  
   384  		require.NotEmpty(t, signedTransaction.BodyBytes)
   385  		transactionBody := services.TransactionBody{}
   386  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   387  
   388  		require.NotNil(t, transactionBody.TransactionID)
   389  		transactionId := transactionBody.TransactionID.String()
   390  		require.NotEqual(t, "", transactionId)
   391  
   392  		sigMap := signedTransaction.GetSigMap()
   393  		require.NotNil(t, sigMap)
   394  
   395  		return &services.TransactionResponse{
   396  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   397  		}
   398  	}
   399  	responses := [][]interface{}{{
   400  		call,
   401  	}}
   402  
   403  	client, server := NewMockClientAndServer(responses)
   404  	defer server.Close()
   405  
   406  	checksum := "dmqui"
   407  	account := AccountID{Account: 3, checksum: &checksum}
   408  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   409  
   410  	accountCreate, err := NewAccountCreateTransaction().
   411  		SetTransactionID(transactionID).
   412  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   413  		Freeze()
   414  	require.NoError(t, err)
   415  
   416  	freez, err := NewScheduleCreateTransaction().
   417  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   418  		SetAdminKey(newKey).
   419  		SetScheduleMemo("no").
   420  		SetPayerAccountID(account).
   421  		SetScheduledTransaction(accountCreate)
   422  	require.NoError(t, err)
   423  
   424  	_, err = freez.Execute(client)
   425  	require.NoError(t, err)
   426  }
   427  
   428  func TestUnitScheduleDeleteTransactionCoverage(t *testing.T) {
   429  	t.Parallel()
   430  
   431  	checksum := "dmqui"
   432  	grpc := time.Second * 30
   433  	schedule := ScheduleID{Schedule: 3, checksum: &checksum}
   434  	nodeAccountID := []AccountID{{Account: 10}}
   435  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   436  
   437  	newKey, err := PrivateKeyGenerateEd25519()
   438  	require.NoError(t, err)
   439  
   440  	client, err := _NewMockClient()
   441  	client.SetLedgerID(*NewLedgerIDTestnet())
   442  	require.NoError(t, err)
   443  	client.SetAutoValidateChecksums(true)
   444  
   445  	transaction, err := NewScheduleDeleteTransaction().
   446  		SetTransactionID(transactionID).
   447  		SetNodeAccountIDs(nodeAccountID).
   448  		SetScheduleID(schedule).
   449  		SetGrpcDeadline(&grpc).
   450  		SetMaxTransactionFee(NewHbar(3)).
   451  		SetMaxRetry(3).
   452  		SetMaxBackoff(time.Second * 30).
   453  		SetMinBackoff(time.Second * 10).
   454  		SetTransactionMemo("no").
   455  		SetTransactionValidDuration(time.Second * 30).
   456  		SetRegenerateTransactionID(false).
   457  		Freeze()
   458  	require.NoError(t, err)
   459  
   460  	transaction.validateNetworkOnIDs(client)
   461  
   462  	_, err = transaction.Schedule()
   463  	require.NoError(t, err)
   464  	transaction.GetTransactionID()
   465  	transaction.GetNodeAccountIDs()
   466  	transaction.GetMaxRetry()
   467  	transaction.GetMaxTransactionFee()
   468  	transaction.GetMaxBackoff()
   469  	transaction.GetMinBackoff()
   470  	transaction.GetRegenerateTransactionID()
   471  	byt, err := transaction.ToBytes()
   472  	require.NoError(t, err)
   473  	_, err = TransactionFromBytes(byt)
   474  	require.NoError(t, err)
   475  	_, err = newKey.SignTransaction(&transaction.Transaction)
   476  	require.NoError(t, err)
   477  
   478  	_, err = transaction.GetTransactionHash()
   479  	require.NoError(t, err)
   480  	transaction.GetMaxTransactionFee()
   481  	transaction.GetTransactionMemo()
   482  	transaction.GetRegenerateTransactionID()
   483  	transaction.GetScheduleID()
   484  	_, err = transaction.GetSignatures()
   485  	require.NoError(t, err)
   486  	transaction.getName()
   487  	//switch b := txFromBytes.(type) {
   488  	//case ScheduleDeleteTransaction:
   489  	//	b.AddSignature(newKey.PublicKey(), sig)
   490  	//}
   491  }
   492  
   493  func TestUnitScheduleDeleteTransactionMock(t *testing.T) {
   494  	t.Parallel()
   495  
   496  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
   497  	require.NoError(t, err)
   498  
   499  	call := func(request *services.Transaction) *services.TransactionResponse {
   500  		require.NotEmpty(t, request.SignedTransactionBytes)
   501  		signedTransaction := services.SignedTransaction{}
   502  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   503  
   504  		require.NotEmpty(t, signedTransaction.BodyBytes)
   505  		transactionBody := services.TransactionBody{}
   506  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   507  
   508  		require.NotNil(t, transactionBody.TransactionID)
   509  		transactionId := transactionBody.TransactionID.String()
   510  		require.NotEqual(t, "", transactionId)
   511  
   512  		sigMap := signedTransaction.GetSigMap()
   513  		require.NotNil(t, sigMap)
   514  
   515  		return &services.TransactionResponse{
   516  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   517  		}
   518  	}
   519  	responses := [][]interface{}{{
   520  		call,
   521  	}}
   522  
   523  	client, server := NewMockClientAndServer(responses)
   524  	defer server.Close()
   525  
   526  	checksum := "dmqui"
   527  	schedule := ScheduleID{Schedule: 3, checksum: &checksum}
   528  
   529  	freez, err := NewScheduleDeleteTransaction().
   530  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   531  		SetScheduleID(schedule).
   532  		FreezeWith(client)
   533  	require.NoError(t, err)
   534  
   535  	_, err = freez.Sign(newKey).Execute(client)
   536  	require.NoError(t, err)
   537  }