github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/schedule_sign_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  	"github.com/stretchr/testify/require"
    32  	protobuf "google.golang.org/protobuf/proto"
    33  )
    34  
    35  func TestUnitScheduleSignTransactionCoverage(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	checksum := "dmqui"
    39  	grpc := time.Second * 30
    40  	schedule := ScheduleID{Schedule: 3, checksum: &checksum}
    41  	nodeAccountID := []AccountID{{Account: 10}}
    42  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    43  
    44  	newKey, err := PrivateKeyGenerateEd25519()
    45  	require.NoError(t, err)
    46  
    47  	client, err := _NewMockClient()
    48  	client.SetLedgerID(*NewLedgerIDTestnet())
    49  	require.NoError(t, err)
    50  	client.SetAutoValidateChecksums(true)
    51  
    52  	transaction, err := NewScheduleSignTransaction().
    53  		SetTransactionID(transactionID).
    54  		SetNodeAccountIDs(nodeAccountID).
    55  		SetScheduleID(schedule).
    56  		SetGrpcDeadline(&grpc).
    57  		SetMaxTransactionFee(NewHbar(3)).
    58  		SetMaxRetry(3).
    59  		SetMaxBackoff(time.Second * 30).
    60  		SetMinBackoff(time.Second * 10).
    61  		SetTransactionMemo("no").
    62  		SetTransactionValidDuration(time.Second * 30).
    63  		SetRegenerateTransactionID(false).
    64  		Freeze()
    65  	require.NoError(t, err)
    66  
    67  	transaction.Sign(newKey)
    68  
    69  	transaction.validateNetworkOnIDs(client)
    70  
    71  	transaction.GetTransactionID()
    72  	transaction.GetNodeAccountIDs()
    73  	transaction.GetMaxRetry()
    74  	transaction.GetMaxTransactionFee()
    75  	transaction.GetMaxBackoff()
    76  	transaction.GetMinBackoff()
    77  	transaction.GetRegenerateTransactionID()
    78  	byt, err := transaction.ToBytes()
    79  	require.NoError(t, err)
    80  	_, err = TransactionFromBytes(byt)
    81  	require.NoError(t, err)
    82  	_, err = newKey.SignTransaction(&transaction.Transaction)
    83  	require.NoError(t, err)
    84  
    85  	_, err = transaction.GetTransactionHash()
    86  	require.NoError(t, err)
    87  	transaction.GetMaxTransactionFee()
    88  	transaction.GetTransactionMemo()
    89  	transaction.GetRegenerateTransactionID()
    90  	transaction.GetScheduleID()
    91  	_, err = transaction.GetSignatures()
    92  	require.NoError(t, err)
    93  	transaction.getName()
    94  	//switch b := txFromBytes.(type) {
    95  	//case ScheduleSignTransaction:
    96  	//	b.AddSignature(newKey.PublicKey(), sig)
    97  	//}
    98  }
    99  
   100  func TestUnitScheduleSignTransactionMock(t *testing.T) {
   101  	t.Parallel()
   102  
   103  	call := func(request *services.Transaction) *services.TransactionResponse {
   104  		require.NotEmpty(t, request.SignedTransactionBytes)
   105  		signedTransaction := services.SignedTransaction{}
   106  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   107  
   108  		require.NotEmpty(t, signedTransaction.BodyBytes)
   109  		transactionBody := services.TransactionBody{}
   110  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   111  
   112  		require.NotNil(t, transactionBody.TransactionID)
   113  		transactionId := transactionBody.TransactionID.String()
   114  		require.NotEqual(t, "", transactionId)
   115  
   116  		sigMap := signedTransaction.GetSigMap()
   117  		require.NotNil(t, sigMap)
   118  
   119  		return &services.TransactionResponse{
   120  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   121  		}
   122  	}
   123  	responses := [][]interface{}{{
   124  		call,
   125  	}}
   126  
   127  	client, server := NewMockClientAndServer(responses)
   128  	defer server.Close()
   129  
   130  	checksum := "dmqui"
   131  	schedule := ScheduleID{Schedule: 3, checksum: &checksum}
   132  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   133  
   134  	transaction, err := NewScheduleSignTransaction().
   135  		SetTransactionID(transactionID).
   136  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   137  		SetScheduleID(schedule).
   138  		Freeze()
   139  	require.NoError(t, err)
   140  
   141  	transaction, err = transaction.SignWithOperator(client)
   142  	require.NoError(t, err)
   143  
   144  	_, err = transaction.Execute(client)
   145  	require.NoError(t, err)
   146  }