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

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/pkg/errors"
    27  
    28  	"github.com/hashgraph/hedera-protobufs-go/services"
    29  )
    30  
    31  // ScheduleSignTransaction Adds zero or more signing keys to a schedule.
    32  // If Long Term Scheduled Transactions are enabled and wait for expiry was set to true on the
    33  // ScheduleCreate then the transaction will always wait till it's `expiration_time` to execute.
    34  // Otherwise, if the resulting set of signing keys satisfy the
    35  // scheduled transaction's signing requirements, it will be executed immediately after the
    36  // triggering ScheduleSign.
    37  // Upon SUCCESS, the receipt includes the scheduledTransactionID to use to query
    38  // for the record of the scheduled transaction's execution (if it occurs).
    39  type ScheduleSignTransaction struct {
    40  	Transaction
    41  	scheduleID *ScheduleID
    42  }
    43  
    44  // NewScheduleSignTransaction creates ScheduleSignTransaction which adds zero or more signing keys to a schedule.
    45  // If Long Term Scheduled Transactions are enabled and wait for expiry was set to true on the
    46  // ScheduleCreate then the transaction will always wait till it's `expiration_time` to execute.
    47  // Otherwise, if the resulting set of signing keys satisfy the
    48  // scheduled transaction's signing requirements, it will be executed immediately after the
    49  // triggering ScheduleSign.
    50  // Upon SUCCESS, the receipt includes the scheduledTransactionID to use to query
    51  // for the record of the scheduled transaction's execution (if it occurs).
    52  func NewScheduleSignTransaction() *ScheduleSignTransaction {
    53  	tx := ScheduleSignTransaction{
    54  		Transaction: _NewTransaction(),
    55  	}
    56  	tx._SetDefaultMaxTransactionFee(NewHbar(5))
    57  
    58  	return &tx
    59  }
    60  
    61  func _ScheduleSignTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *ScheduleSignTransaction {
    62  	return &ScheduleSignTransaction{
    63  		Transaction: tx,
    64  		scheduleID:  _ScheduleIDFromProtobuf(pb.GetScheduleSign().GetScheduleID()),
    65  	}
    66  }
    67  
    68  // SetScheduleID Sets the id of the schedule to add signing keys to
    69  func (tx *ScheduleSignTransaction) SetScheduleID(scheduleID ScheduleID) *ScheduleSignTransaction {
    70  	tx._RequireNotFrozen()
    71  	tx.scheduleID = &scheduleID
    72  	return tx
    73  }
    74  
    75  // GetScheduleID returns the id of the schedule to add signing keys to
    76  func (tx *ScheduleSignTransaction) GetScheduleID() ScheduleID {
    77  	if tx.scheduleID == nil {
    78  		return ScheduleID{}
    79  	}
    80  
    81  	return *tx.scheduleID
    82  }
    83  
    84  // ---- Required Interfaces ---- //
    85  
    86  // Sign uses the provided privateKey to sign the transaction.
    87  func (tx *ScheduleSignTransaction) Sign(privateKey PrivateKey) *ScheduleSignTransaction {
    88  	tx.Transaction.Sign(privateKey)
    89  	return tx
    90  }
    91  
    92  // SignWithOperator signs the transaction with client's operator privateKey.
    93  func (tx *ScheduleSignTransaction) SignWithOperator(client *Client) (*ScheduleSignTransaction, error) {
    94  	_, err := tx.Transaction.signWithOperator(client, tx)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	return tx, nil
    99  }
   100  
   101  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   102  // with the publicKey as the map key.
   103  func (tx *ScheduleSignTransaction) SignWith(
   104  	publicKey PublicKey,
   105  	signer TransactionSigner,
   106  ) *ScheduleSignTransaction {
   107  	tx.Transaction.SignWith(publicKey, signer)
   108  	return tx
   109  }
   110  
   111  // AddSignature adds a signature to the transaction.
   112  func (tx *ScheduleSignTransaction) AddSignature(publicKey PublicKey, signature []byte) *ScheduleSignTransaction {
   113  	tx.Transaction.AddSignature(publicKey, signature)
   114  	return tx
   115  }
   116  
   117  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
   118  func (tx *ScheduleSignTransaction) SetGrpcDeadline(deadline *time.Duration) *ScheduleSignTransaction {
   119  	tx.Transaction.SetGrpcDeadline(deadline)
   120  	return tx
   121  }
   122  
   123  func (tx *ScheduleSignTransaction) Freeze() (*ScheduleSignTransaction, error) {
   124  	return tx.FreezeWith(nil)
   125  }
   126  
   127  func (tx *ScheduleSignTransaction) FreezeWith(client *Client) (*ScheduleSignTransaction, error) {
   128  	_, err := tx.Transaction.freezeWith(client, tx)
   129  	return tx, err
   130  }
   131  
   132  // SetMaxTransactionFee sets the max transaction fee for this ScheduleSignTransaction.
   133  func (tx *ScheduleSignTransaction) SetMaxTransactionFee(fee Hbar) *ScheduleSignTransaction {
   134  	tx.Transaction.SetMaxTransactionFee(fee)
   135  	return tx
   136  }
   137  
   138  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   139  func (tx *ScheduleSignTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *ScheduleSignTransaction {
   140  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   141  	return tx
   142  }
   143  
   144  // SetTransactionMemo sets the memo for this ScheduleSignTransaction.
   145  func (tx *ScheduleSignTransaction) SetTransactionMemo(memo string) *ScheduleSignTransaction {
   146  	tx.Transaction.SetTransactionMemo(memo)
   147  	return tx
   148  }
   149  
   150  // SetTransactionValidDuration sets the valid duration for this ScheduleSignTransaction.
   151  func (tx *ScheduleSignTransaction) SetTransactionValidDuration(duration time.Duration) *ScheduleSignTransaction {
   152  	tx.Transaction.SetTransactionValidDuration(duration)
   153  	return tx
   154  }
   155  
   156  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   157  func (tx *ScheduleSignTransaction) ToBytes() ([]byte, error) {
   158  	bytes, err := tx.Transaction.toBytes(tx)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	return bytes, nil
   163  }
   164  
   165  // SetTransactionID sets the TransactionID for this ScheduleSignTransaction.
   166  func (tx *ScheduleSignTransaction) SetTransactionID(transactionID TransactionID) *ScheduleSignTransaction {
   167  	tx.Transaction.SetTransactionID(transactionID)
   168  	return tx
   169  }
   170  
   171  // SetNodeAccountIDs sets the _Node AccountID for this ScheduleSignTransaction.
   172  func (tx *ScheduleSignTransaction) SetNodeAccountIDs(nodeID []AccountID) *ScheduleSignTransaction {
   173  	tx.Transaction.SetNodeAccountIDs(nodeID)
   174  	return tx
   175  }
   176  
   177  // SetMaxRetry sets the max number of errors before execution will fail.
   178  func (tx *ScheduleSignTransaction) SetMaxRetry(count int) *ScheduleSignTransaction {
   179  	tx.Transaction.SetMaxRetry(count)
   180  	return tx
   181  }
   182  
   183  // SetMaxBackoff The maximum amount of time to wait between retries.
   184  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   185  func (tx *ScheduleSignTransaction) SetMaxBackoff(max time.Duration) *ScheduleSignTransaction {
   186  	tx.Transaction.SetMaxBackoff(max)
   187  	return tx
   188  }
   189  
   190  // SetMinBackoff sets the minimum amount of time to wait between retries.
   191  func (tx *ScheduleSignTransaction) SetMinBackoff(min time.Duration) *ScheduleSignTransaction {
   192  	tx.Transaction.SetMinBackoff(min)
   193  	return tx
   194  }
   195  
   196  func (tx *ScheduleSignTransaction) SetLogLevel(level LogLevel) *ScheduleSignTransaction {
   197  	tx.Transaction.SetLogLevel(level)
   198  	return tx
   199  }
   200  
   201  func (tx *ScheduleSignTransaction) Execute(client *Client) (TransactionResponse, error) {
   202  	return tx.Transaction.execute(client, tx)
   203  }
   204  
   205  func (tx *ScheduleSignTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   206  	return tx.Transaction.schedule(tx)
   207  }
   208  
   209  // ----------- Overridden functions ----------------
   210  
   211  func (tx *ScheduleSignTransaction) getName() string {
   212  	return "ScheduleSignTransaction"
   213  }
   214  
   215  func (tx *ScheduleSignTransaction) validateNetworkOnIDs(client *Client) error {
   216  	if client == nil || !client.autoValidateChecksums {
   217  		return nil
   218  	}
   219  
   220  	if tx.scheduleID != nil {
   221  		if err := tx.scheduleID.ValidateChecksum(client); err != nil {
   222  			return err
   223  		}
   224  	}
   225  
   226  	return nil
   227  }
   228  
   229  func (tx *ScheduleSignTransaction) build() *services.TransactionBody {
   230  	return &services.TransactionBody{
   231  		TransactionFee:           tx.transactionFee,
   232  		Memo:                     tx.Transaction.memo,
   233  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   234  		TransactionID:            tx.transactionID._ToProtobuf(),
   235  		Data: &services.TransactionBody_ScheduleSign{
   236  			ScheduleSign: tx.buildProtoBody(),
   237  		},
   238  	}
   239  }
   240  
   241  func (tx *ScheduleSignTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   242  	return nil, errors.New("cannot schedule `ScheduleSignTransaction")
   243  }
   244  
   245  func (tx *ScheduleSignTransaction) buildProtoBody() *services.ScheduleSignTransactionBody {
   246  	body := &services.ScheduleSignTransactionBody{}
   247  	if tx.scheduleID != nil {
   248  		body.ScheduleID = tx.scheduleID._ToProtobuf()
   249  	}
   250  
   251  	return body
   252  }
   253  
   254  func (tx *ScheduleSignTransaction) getMethod(channel *_Channel) _Method {
   255  	return _Method{
   256  		transaction: channel._GetSchedule().SignSchedule,
   257  	}
   258  }
   259  
   260  func (tx *ScheduleSignTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   261  	return tx.buildScheduled()
   262  }