github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/live_hash_add_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  	"github.com/hashgraph/hedera-protobufs-go/services"
    25  	"github.com/pkg/errors"
    26  
    27  	"time"
    28  )
    29  
    30  // LiveHashAddTransaction At consensus, attaches the given livehash to the given account.  The hash can be deleted by the
    31  // key controlling the account, or by any of the keys associated to the livehash.  Hence livehashes
    32  // provide a revocation service for their implied credentials; for example, when an authority grants
    33  // a credential to the account, the account owner will cosign with the authority (or authorities) to
    34  // attach a hash of the credential to the account---hence proving the grant. If the credential is
    35  // revoked, then any of the authorities may delete it (or the account owner). In this way, the
    36  // livehash mechanism acts as a revocation service.  An account cannot have two identical livehashes
    37  // associated. To modify the list of keys in a livehash, the livehash should first be deleted, then
    38  // recreated with a new list of keys.
    39  type LiveHashAddTransaction struct {
    40  	Transaction
    41  	accountID *AccountID
    42  	hash      []byte
    43  	keys      *KeyList
    44  	duration  *time.Duration
    45  }
    46  
    47  // NewLiveHashAddTransaction creates LiveHashAddTransaction which at consensus, attaches the given livehash to the given account.
    48  // The hash can be deleted by the key controlling the account, or by any of the keys associated to the livehash.  Hence livehashes
    49  // provide a revocation service for their implied credentials; for example, when an authority grants
    50  // a credential to the account, the account owner will cosign with the authority (or authorities) to
    51  // attach a hash of the credential to the account---hence proving the grant. If the credential is
    52  // revoked, then any of the authorities may delete it (or the account owner). In this way, the
    53  // livehash mechanism acts as a revocation service.  An account cannot have two identical livehashes
    54  // associated. To modify the list of keys in a livehash, the livehash should first be deleted, then
    55  // recreated with a new list of keys.
    56  func NewLiveHashAddTransaction() *LiveHashAddTransaction {
    57  	tx := LiveHashAddTransaction{
    58  		Transaction: _NewTransaction(),
    59  	}
    60  	tx._SetDefaultMaxTransactionFee(NewHbar(2))
    61  	return &tx
    62  }
    63  
    64  func _LiveHashAddTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *LiveHashAddTransaction {
    65  	keys, _ := _KeyListFromProtobuf(pb.GetCryptoAddLiveHash().LiveHash.GetKeys())
    66  	duration := _DurationFromProtobuf(pb.GetCryptoAddLiveHash().LiveHash.Duration)
    67  
    68  	return &LiveHashAddTransaction{
    69  		Transaction: tx,
    70  		accountID:   _AccountIDFromProtobuf(pb.GetCryptoAddLiveHash().GetLiveHash().GetAccountId()),
    71  		hash:        pb.GetCryptoAddLiveHash().LiveHash.Hash,
    72  		keys:        &keys,
    73  		duration:    &duration,
    74  	}
    75  }
    76  
    77  // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
    78  func (tx *LiveHashAddTransaction) SetGrpcDeadline(deadline *time.Duration) *LiveHashAddTransaction {
    79  	tx.Transaction.SetGrpcDeadline(deadline)
    80  	return tx
    81  }
    82  
    83  // SetHash Sets the SHA-384 hash of a credential or certificate
    84  func (tx *LiveHashAddTransaction) SetHash(hash []byte) *LiveHashAddTransaction {
    85  	tx._RequireNotFrozen()
    86  	tx.hash = hash
    87  	return tx
    88  }
    89  
    90  func (tx *LiveHashAddTransaction) GetHash() []byte {
    91  	return tx.hash
    92  }
    93  
    94  // SetKeys Sets a list of keys (primitive or threshold), all of which must sign to attach the livehash to an account.
    95  // Any one of which can later delete it.
    96  func (tx *LiveHashAddTransaction) SetKeys(keys ...Key) *LiveHashAddTransaction {
    97  	tx._RequireNotFrozen()
    98  	if tx.keys == nil {
    99  		tx.keys = &KeyList{keys: []Key{}}
   100  	}
   101  	keyList := NewKeyList()
   102  	keyList.AddAll(keys)
   103  
   104  	tx.keys = keyList
   105  
   106  	return tx
   107  }
   108  
   109  func (tx *LiveHashAddTransaction) GetKeys() KeyList {
   110  	if tx.keys != nil {
   111  		return *tx.keys
   112  	}
   113  
   114  	return KeyList{}
   115  }
   116  
   117  // SetDuration Set the duration for which the livehash will remain valid
   118  func (tx *LiveHashAddTransaction) SetDuration(duration time.Duration) *LiveHashAddTransaction {
   119  	tx._RequireNotFrozen()
   120  	tx.duration = &duration
   121  	return tx
   122  }
   123  
   124  // GetDuration returns the duration for which the livehash will remain valid
   125  func (tx *LiveHashAddTransaction) GetDuration() time.Duration {
   126  	if tx.duration != nil {
   127  		return *tx.duration
   128  	}
   129  
   130  	return time.Duration(0)
   131  }
   132  
   133  // SetAccountID Sets the account to which the livehash is attached
   134  func (tx *LiveHashAddTransaction) SetAccountID(accountID AccountID) *LiveHashAddTransaction {
   135  	tx._RequireNotFrozen()
   136  	tx.accountID = &accountID
   137  	return tx
   138  }
   139  
   140  // GetAccountID returns the account to which the livehash is attached
   141  func (tx *LiveHashAddTransaction) GetAccountID() AccountID {
   142  	if tx.accountID == nil {
   143  		return AccountID{}
   144  	}
   145  
   146  	return *tx.accountID
   147  }
   148  
   149  // ---- Required Interfaces ---- //
   150  
   151  // Sign uses the provided privateKey to sign the transaction.
   152  func (tx *LiveHashAddTransaction) Sign(
   153  	privateKey PrivateKey,
   154  ) *LiveHashAddTransaction {
   155  	tx.Transaction.Sign(privateKey)
   156  	return tx
   157  }
   158  
   159  // SignWithOperator signs the transaction with client's operator privateKey.
   160  func (tx *LiveHashAddTransaction) SignWithOperator(
   161  	client *Client,
   162  ) (*LiveHashAddTransaction, error) {
   163  	// If the transaction is not signed by the _Operator, we need
   164  	// to sign the transaction with the _Operator
   165  	_, err := tx.Transaction.signWithOperator(client, tx)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	return tx, nil
   170  }
   171  
   172  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   173  // with the publicKey as the map key.
   174  func (tx *LiveHashAddTransaction) SignWith(
   175  	publicKey PublicKey,
   176  	signer TransactionSigner,
   177  ) *LiveHashAddTransaction {
   178  	tx.Transaction.SignWith(publicKey, signer)
   179  	return tx
   180  }
   181  
   182  // AddSignature adds a signature to the transaction.
   183  func (tx *LiveHashAddTransaction) AddSignature(publicKey PublicKey, signature []byte) *LiveHashAddTransaction {
   184  	tx.Transaction.AddSignature(publicKey, signature)
   185  	return tx
   186  }
   187  
   188  func (tx *LiveHashAddTransaction) Freeze() (*LiveHashAddTransaction, error) {
   189  	return tx.FreezeWith(nil)
   190  }
   191  
   192  func (tx *LiveHashAddTransaction) FreezeWith(client *Client) (*LiveHashAddTransaction, error) {
   193  	_, err := tx.Transaction.freezeWith(client, tx)
   194  	return tx, err
   195  }
   196  
   197  // GetMaxTransactionFee returns the maximum transaction fee the operator (paying account) is willing to pay.
   198  func (tx *LiveHashAddTransaction) GetMaxTransactionFee() Hbar {
   199  	return tx.Transaction.GetMaxTransactionFee()
   200  }
   201  
   202  // SetMaxTransactionFee sets the maximum transaction fee the operator (paying account) is willing to pay.
   203  func (tx *LiveHashAddTransaction) SetMaxTransactionFee(fee Hbar) *LiveHashAddTransaction {
   204  	tx._RequireNotFrozen()
   205  	tx.Transaction.SetMaxTransactionFee(fee)
   206  	return tx
   207  }
   208  
   209  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   210  func (tx *LiveHashAddTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *LiveHashAddTransaction {
   211  	tx._RequireNotFrozen()
   212  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   213  	return tx
   214  }
   215  
   216  // SetTransactionMemo sets the memo for this LiveHashAddTransaction.
   217  func (tx *LiveHashAddTransaction) SetTransactionMemo(memo string) *LiveHashAddTransaction {
   218  	tx._RequireNotFrozen()
   219  	tx.Transaction.SetTransactionMemo(memo)
   220  	return tx
   221  }
   222  
   223  // SetTransactionValidDuration sets the valid duration for this LiveHashAddTransaction.
   224  func (tx *LiveHashAddTransaction) SetTransactionValidDuration(duration time.Duration) *LiveHashAddTransaction {
   225  	tx.Transaction.SetTransactionValidDuration(duration)
   226  	return tx
   227  }
   228  
   229  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   230  func (tx *LiveHashAddTransaction) ToBytes() ([]byte, error) {
   231  	bytes, err := tx.Transaction.toBytes(tx)
   232  	if err != nil {
   233  		return nil, err
   234  	}
   235  	return bytes, nil
   236  }
   237  
   238  // GetTransactionID gets the TransactionID for this	 LiveHashAddTransaction.
   239  func (tx *LiveHashAddTransaction) GetTransactionID() TransactionID {
   240  	return tx.Transaction.GetTransactionID()
   241  }
   242  
   243  // SetTransactionID sets the TransactionID for this LiveHashAddTransaction.
   244  func (tx *LiveHashAddTransaction) SetTransactionID(transactionID TransactionID) *LiveHashAddTransaction {
   245  	tx._RequireNotFrozen()
   246  
   247  	tx.Transaction.SetTransactionID(transactionID)
   248  	return tx
   249  }
   250  
   251  // SetNodeAccountID sets the _Node AccountID for this LiveHashAddTransaction.
   252  func (tx *LiveHashAddTransaction) SetNodeAccountIDs(nodeID []AccountID) *LiveHashAddTransaction {
   253  	tx._RequireNotFrozen()
   254  	tx.Transaction.SetNodeAccountIDs(nodeID)
   255  	return tx
   256  }
   257  
   258  // SetMaxRetry sets the max number of errors before execution will fail.
   259  func (tx *LiveHashAddTransaction) SetMaxRetry(count int) *LiveHashAddTransaction {
   260  	tx.Transaction.SetMaxRetry(count)
   261  	return tx
   262  }
   263  
   264  // SetMaxBackoff The maximum amount of time to wait between retries.
   265  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   266  func (tx *LiveHashAddTransaction) SetMaxBackoff(max time.Duration) *LiveHashAddTransaction {
   267  	tx.Transaction.SetMaxBackoff(max)
   268  	return tx
   269  }
   270  
   271  // SetMinBackoff sets the minimum amount of time to wait between retries.
   272  func (tx *LiveHashAddTransaction) SetMinBackoff(min time.Duration) *LiveHashAddTransaction {
   273  	tx.Transaction.SetMinBackoff(min)
   274  	return tx
   275  }
   276  
   277  func (tx *LiveHashAddTransaction) SetLogLevel(level LogLevel) *LiveHashAddTransaction {
   278  	tx.Transaction.SetLogLevel(level)
   279  	return tx
   280  }
   281  
   282  func (tx *LiveHashAddTransaction) Execute(client *Client) (TransactionResponse, error) {
   283  	return tx.Transaction.execute(client, tx)
   284  }
   285  
   286  func (tx *LiveHashAddTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   287  	return tx.Transaction.schedule(tx)
   288  }
   289  
   290  // ----------- Overridden functions ----------------
   291  
   292  func (tx *LiveHashAddTransaction) getName() string {
   293  	return "LiveHashAddTransaction"
   294  }
   295  func (tx *LiveHashAddTransaction) validateNetworkOnIDs(client *Client) error {
   296  	if client == nil || !client.autoValidateChecksums {
   297  		return nil
   298  	}
   299  
   300  	if tx.accountID != nil {
   301  		if err := tx.accountID.ValidateChecksum(client); err != nil {
   302  			return err
   303  		}
   304  	}
   305  
   306  	return nil
   307  }
   308  
   309  func (tx *LiveHashAddTransaction) build() *services.TransactionBody {
   310  	return &services.TransactionBody{
   311  		TransactionFee:           tx.transactionFee,
   312  		Memo:                     tx.Transaction.memo,
   313  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   314  		TransactionID:            tx.transactionID._ToProtobuf(),
   315  		Data: &services.TransactionBody_CryptoAddLiveHash{
   316  			CryptoAddLiveHash: tx.buildProtoBody(),
   317  		},
   318  	}
   319  }
   320  
   321  func (tx *LiveHashAddTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   322  	return nil, errors.New("cannot schedule `LiveHashAddTransaction`")
   323  }
   324  
   325  func (tx *LiveHashAddTransaction) buildProtoBody() *services.CryptoAddLiveHashTransactionBody {
   326  	body := &services.CryptoAddLiveHashTransactionBody{
   327  		LiveHash: &services.LiveHash{},
   328  	}
   329  
   330  	if tx.accountID != nil {
   331  		body.LiveHash.AccountId = tx.accountID._ToProtobuf()
   332  	}
   333  
   334  	if tx.duration != nil {
   335  		body.LiveHash.Duration = _DurationToProtobuf(*tx.duration)
   336  	}
   337  
   338  	if tx.keys != nil {
   339  		body.LiveHash.Keys = tx.keys._ToProtoKeyList()
   340  	}
   341  
   342  	if tx.hash != nil {
   343  		body.LiveHash.Hash = tx.hash
   344  	}
   345  
   346  	return body
   347  }
   348  
   349  func (tx *LiveHashAddTransaction) getMethod(channel *_Channel) _Method {
   350  	return _Method{
   351  		transaction: channel._GetCrypto().AddLiveHash,
   352  	}
   353  }
   354  func (tx *LiveHashAddTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   355  	return tx.buildScheduled()
   356  }