github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_create_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 "encoding/hex" 25 "strings" 26 "time" 27 28 "github.com/hashgraph/hedera-protobufs-go/services" 29 ) 30 31 // AccountCreateTransaction creates a new account. After the account is created, the AccountID for it is in the receipt, 32 // or by asking for a Record of the transaction to be created, and retrieving that. The account can then automatically 33 // generate records for large transfers into it or out of it, which each last for 25 hours. Records are generated for 34 // any transfer that exceeds the thresholds given here. This account is charged hbar for each record generated, so the 35 // thresholds are useful for limiting Record generation to happen only for large transactions. 36 // 37 // The current API ignores shardID, realmID, and newRealmAdminKey, and creates everything in shard 0 and realm 0, 38 // with a null key. Future versions of the API will support multiple realms and multiple shards. 39 type AccountCreateTransaction struct { 40 Transaction 41 proxyAccountID *AccountID 42 key Key 43 initialBalance uint64 44 autoRenewPeriod *time.Duration 45 memo string 46 receiverSignatureRequired bool 47 maxAutomaticTokenAssociations int32 48 stakedAccountID *AccountID 49 stakedNodeID *int64 50 declineReward bool 51 alias []byte 52 } 53 54 // NewAccountCreateTransaction creates an AccountCreateTransaction transaction which can be used to construct and 55 // execute a Crypto Create Transaction. 56 func NewAccountCreateTransaction() *AccountCreateTransaction { 57 tx := AccountCreateTransaction{ 58 Transaction: _NewTransaction(), 59 } 60 61 tx.SetAutoRenewPeriod(7890000 * time.Second) 62 tx._SetDefaultMaxTransactionFee(NewHbar(5)) 63 64 return &tx 65 } 66 67 func _AccountCreateTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *AccountCreateTransaction { 68 key, _ := _KeyFromProtobuf(pb.GetCryptoCreateAccount().GetKey()) 69 renew := _DurationFromProtobuf(pb.GetCryptoCreateAccount().GetAutoRenewPeriod()) 70 71 var stakedNodeID *int64 72 if pb.GetCryptoCreateAccount().GetStakedNodeId() != 0 { 73 nodeId := pb.GetCryptoCreateAccount().GetStakedNodeId() 74 stakedNodeID = &nodeId 75 } 76 var stakeAccountID *AccountID 77 if pb.GetCryptoCreateAccount().GetStakedAccountId() != nil { 78 stakeAccountID = _AccountIDFromProtobuf(pb.GetCryptoCreateAccount().GetStakedAccountId()) 79 } 80 81 body := AccountCreateTransaction{ 82 Transaction: tx, 83 key: key, 84 initialBalance: pb.GetCryptoCreateAccount().InitialBalance, 85 autoRenewPeriod: &renew, 86 memo: pb.GetCryptoCreateAccount().GetMemo(), 87 receiverSignatureRequired: pb.GetCryptoCreateAccount().ReceiverSigRequired, 88 maxAutomaticTokenAssociations: pb.GetCryptoCreateAccount().MaxAutomaticTokenAssociations, 89 stakedAccountID: stakeAccountID, 90 stakedNodeID: stakedNodeID, 91 declineReward: pb.GetCryptoCreateAccount().GetDeclineReward(), 92 } 93 94 if pb.GetCryptoCreateAccount().GetAlias() != nil { 95 body.alias = pb.GetCryptoCreateAccount().GetAlias() 96 } 97 98 return &body 99 } 100 101 // SetKey sets the key that must sign each transfer out of the account. If RecieverSignatureRequired is true, then it 102 // must also sign any transfer into the account. 103 func (tx *AccountCreateTransaction) SetKey(key Key) *AccountCreateTransaction { 104 tx._RequireNotFrozen() 105 tx.key = key 106 return tx 107 } 108 109 // GetKey returns the key that must sign each transfer out of the account. 110 func (tx *AccountCreateTransaction) GetKey() (Key, error) { 111 return tx.key, nil 112 } 113 114 // SetInitialBalance sets the initial number of Hbar to put into the account 115 func (tx *AccountCreateTransaction) SetInitialBalance(initialBalance Hbar) *AccountCreateTransaction { 116 tx._RequireNotFrozen() 117 tx.initialBalance = uint64(initialBalance.AsTinybar()) 118 return tx 119 } 120 121 // GetInitialBalance returns the initial number of Hbar to put into the account 122 func (tx *AccountCreateTransaction) GetInitialBalance() Hbar { 123 return HbarFromTinybar(int64(tx.initialBalance)) 124 } 125 126 // SetMaxAutomaticTokenAssociations 127 // Set the maximum number of tokens that an Account can be implicitly associated with. Defaults to 0 128 // and up to a maximum value of 1000. 129 func (tx *AccountCreateTransaction) SetMaxAutomaticTokenAssociations(max int32) *AccountCreateTransaction { 130 tx._RequireNotFrozen() 131 tx.maxAutomaticTokenAssociations = max 132 return tx 133 } 134 135 // GetMaxAutomaticTokenAssociations returns the maximum number of tokens that an Account can be implicitly associated with. 136 func (tx *AccountCreateTransaction) GetMaxAutomaticTokenAssociations() int32 { 137 return tx.maxAutomaticTokenAssociations 138 } 139 140 // SetAutoRenewPeriod sets the time duration for when account is charged to extend its expiration date. When the account 141 // is created, the payer account is charged enough hbars so that the new account will not expire for the next 142 // auto renew period. When it reaches the expiration time, the new account will then be automatically charged to 143 // renew for another auto renew period. If it does not have enough hbars to renew for that long, then the remaining 144 // hbars are used to extend its expiration as long as possible. If it is has a zero balance when it expires, 145 // then it is deleted. 146 func (tx *AccountCreateTransaction) SetAutoRenewPeriod(autoRenewPeriod time.Duration) *AccountCreateTransaction { 147 tx._RequireNotFrozen() 148 tx.autoRenewPeriod = &autoRenewPeriod 149 return tx 150 } 151 152 // GetAutoRenewPeriod returns the time duration for when account is charged to extend its expiration date. 153 func (tx *AccountCreateTransaction) GetAutoRenewPeriod() time.Duration { 154 if tx.autoRenewPeriod != nil { 155 return *tx.autoRenewPeriod 156 } 157 158 return time.Duration(0) 159 } 160 161 // Deprecated 162 // SetProxyAccountID sets the ID of the account to which this account is proxy staked. If proxyAccountID is not set, 163 // is an invalid account, or is an account that isn't a _Node, then this account is automatically proxy staked to a _Node 164 // chosen by the _Network, but without earning payments. If the proxyAccountID account refuses to accept proxy staking , 165 // or if it is not currently running a _Node, then it will behave as if proxyAccountID was not set. 166 func (tx *AccountCreateTransaction) SetProxyAccountID(id AccountID) *AccountCreateTransaction { 167 tx._RequireNotFrozen() 168 tx.proxyAccountID = &id 169 return tx 170 } 171 172 // Deprecated 173 func (tx *AccountCreateTransaction) GetProxyAccountID() AccountID { 174 if tx.proxyAccountID == nil { 175 return AccountID{} 176 } 177 178 return *tx.proxyAccountID 179 } 180 181 // SetAccountMemo Sets the memo associated with the account (UTF-8 encoding max 100 bytes) 182 func (tx *AccountCreateTransaction) SetAccountMemo(memo string) *AccountCreateTransaction { 183 tx._RequireNotFrozen() 184 tx.memo = memo 185 return tx 186 } 187 188 // GetAccountMemo Gets the memo associated with the account (UTF-8 encoding max 100 bytes) 189 func (tx *AccountCreateTransaction) GetAccountMemo() string { 190 return tx.memo 191 } 192 193 // SetStakedAccountID Set the account to which this account will stake. 194 func (tx *AccountCreateTransaction) SetStakedAccountID(id AccountID) *AccountCreateTransaction { 195 tx._RequireNotFrozen() 196 tx.stakedAccountID = &id 197 tx.stakedNodeID = nil 198 return tx 199 } 200 201 // GetStakedAccountID returns the account to which this account will stake. 202 func (tx *AccountCreateTransaction) GetStakedAccountID() AccountID { 203 if tx.stakedAccountID != nil { 204 return *tx.stakedAccountID 205 } 206 207 return AccountID{} 208 } 209 210 // SetStakedNodeID Set the node to which this account will stake 211 func (tx *AccountCreateTransaction) SetStakedNodeID(id int64) *AccountCreateTransaction { 212 tx._RequireNotFrozen() 213 tx.stakedNodeID = &id 214 tx.stakedAccountID = nil 215 return tx 216 } 217 218 // GetStakedNodeID returns the node to which this account will stake 219 func (tx *AccountCreateTransaction) GetStakedNodeID() int64 { 220 if tx.stakedNodeID != nil { 221 return *tx.stakedNodeID 222 } 223 224 return 0 225 } 226 227 // SetDeclineStakingReward If set to true, the account declines receiving a staking reward. The default value is false. 228 func (tx *AccountCreateTransaction) SetDeclineStakingReward(decline bool) *AccountCreateTransaction { 229 tx._RequireNotFrozen() 230 tx.declineReward = decline 231 return tx 232 } 233 234 // GetDeclineStakingReward returns true if the account declines receiving a staking reward. 235 func (tx *AccountCreateTransaction) GetDeclineStakingReward() bool { 236 return tx.declineReward 237 } 238 239 func (tx *AccountCreateTransaction) SetAlias(evmAddress string) *AccountCreateTransaction { 240 tx._RequireNotFrozen() 241 242 evmAddress = strings.TrimPrefix(evmAddress, "0x") 243 evmAddressBytes, _ := hex.DecodeString(evmAddress) 244 245 tx.alias = evmAddressBytes 246 return tx 247 } 248 249 func (tx *AccountCreateTransaction) GetAlias() []byte { 250 return tx.alias 251 } 252 253 // SetReceiverSignatureRequired sets the receiverSigRequired flag. If the receiverSigRequired flag is set to true, then 254 // all cryptocurrency transfers must be signed by this account's key, both for transfers in and out. If it is false, 255 // then only transfers out have to be signed by it. This transaction must be signed by the 256 // payer account. If receiverSigRequired is false, then the transaction does not have to be signed by the keys in the 257 // keys field. If it is true, then it must be signed by them, in addition to the keys of the payer account. 258 func (tx *AccountCreateTransaction) SetReceiverSignatureRequired(required bool) *AccountCreateTransaction { 259 tx.receiverSignatureRequired = required 260 return tx 261 } 262 263 // GetReceiverSignatureRequired returns the receiverSigRequired flag. 264 func (tx *AccountCreateTransaction) GetReceiverSignatureRequired() bool { 265 return tx.receiverSignatureRequired 266 } 267 268 // ---- Required Interfaces ---- // 269 270 // Sign uses the provided privateKey to sign the transaction. 271 func (tx *AccountCreateTransaction) Sign( 272 privateKey PrivateKey, 273 ) *AccountCreateTransaction { 274 tx.Transaction.Sign(privateKey) 275 return tx 276 } 277 278 // SignWithOperator signs the transaction with client's operator privateKey. 279 func (tx *AccountCreateTransaction) SignWithOperator( 280 client *Client, 281 ) (*AccountCreateTransaction, error) { 282 _, err := tx.Transaction.signWithOperator(client, tx) 283 if err != nil { 284 return nil, err 285 } 286 return tx, nil 287 } 288 289 // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map 290 // with the publicKey as the map key. 291 func (tx *AccountCreateTransaction) SignWith( 292 publicKey PublicKey, 293 signer TransactionSigner, 294 ) *AccountCreateTransaction { 295 tx.Transaction.SignWith(publicKey, signer) 296 return tx 297 } 298 299 // AddSignature adds a signature to the transaction. 300 func (tx *AccountCreateTransaction) AddSignature(publicKey PublicKey, signature []byte) *AccountCreateTransaction { 301 tx.Transaction.AddSignature(publicKey, signature) 302 return tx 303 } 304 305 // When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.) 306 func (tx *AccountCreateTransaction) SetGrpcDeadline(deadline *time.Duration) *AccountCreateTransaction { 307 tx.Transaction.SetGrpcDeadline(deadline) 308 return tx 309 } 310 311 func (tx *AccountCreateTransaction) Freeze() (*AccountCreateTransaction, error) { 312 return tx.FreezeWith(nil) 313 } 314 315 func (tx *AccountCreateTransaction) FreezeWith(client *Client) (*AccountCreateTransaction, error) { 316 _, err := tx.Transaction.freezeWith(client, tx) 317 return tx, err 318 } 319 320 // GetMaxTransactionFee returns the maximum transaction fee the operator (paying account) is willing to pay. 321 func (tx *AccountCreateTransaction) GetMaxTransactionFee() Hbar { 322 return tx.Transaction.GetMaxTransactionFee() 323 } 324 325 // SetMaxTransactionFee sets the maximum transaction fee the operator (paying account) is willing to pay. 326 func (tx *AccountCreateTransaction) SetMaxTransactionFee(fee Hbar) *AccountCreateTransaction { 327 tx._RequireNotFrozen() 328 tx.Transaction.SetMaxTransactionFee(fee) 329 return tx 330 } 331 332 // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received 333 func (tx *AccountCreateTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *AccountCreateTransaction { 334 tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID) 335 return tx 336 } 337 338 // SetTransactionMemo sets the memo for this AccountCreateTransaction. 339 func (tx *AccountCreateTransaction) SetTransactionMemo(memo string) *AccountCreateTransaction { 340 tx.Transaction.SetTransactionMemo(memo) 341 return tx 342 } 343 344 // SetTransactionValidDuration sets the valid duration for this AccountCreateTransaction. 345 func (tx *AccountCreateTransaction) SetTransactionValidDuration(duration time.Duration) *AccountCreateTransaction { 346 tx.Transaction.SetTransactionValidDuration(duration) 347 return tx 348 } 349 350 // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not 351 func (tx *AccountCreateTransaction) ToBytes() ([]byte, error) { 352 bytes, err := tx.Transaction.toBytes(tx) 353 if err != nil { 354 return nil, err 355 } 356 return bytes, nil 357 } 358 359 // SetTransactionID sets the TransactionID for this AccountCreateTransaction. 360 func (tx *AccountCreateTransaction) SetTransactionID(transactionID TransactionID) *AccountCreateTransaction { 361 tx.Transaction.SetTransactionID(transactionID) 362 return tx 363 } 364 365 // SetNodeAccountIDs sets the _Node AccountID for this AccountCreateTransaction. 366 func (tx *AccountCreateTransaction) SetNodeAccountIDs(nodeID []AccountID) *AccountCreateTransaction { 367 tx.Transaction.SetNodeAccountIDs(nodeID) 368 return tx 369 } 370 371 // SetMaxRetry sets the max number of errors before execution will fail. 372 func (tx *AccountCreateTransaction) SetMaxRetry(count int) *AccountCreateTransaction { 373 tx.Transaction.SetMaxRetry(count) 374 return tx 375 } 376 377 // SetMaxBackoff The maximum amount of time to wait between retries. 378 // Every retry attempt will increase the wait time exponentially until it reaches this time. 379 func (tx *AccountCreateTransaction) SetMaxBackoff(max time.Duration) *AccountCreateTransaction { 380 tx.Transaction.SetMaxBackoff(max) 381 return tx 382 } 383 384 // SetMinBackoff sets the minimum amount of time to wait between retries. 385 func (tx *AccountCreateTransaction) SetMinBackoff(min time.Duration) *AccountCreateTransaction { 386 tx.Transaction.SetMinBackoff(min) 387 return tx 388 } 389 390 func (tx *AccountCreateTransaction) SetLogLevel(level LogLevel) *AccountCreateTransaction { 391 tx.Transaction.SetLogLevel(level) 392 return tx 393 } 394 395 func (tx *AccountCreateTransaction) Execute(client *Client) (TransactionResponse, error) { 396 return tx.Transaction.execute(client, tx) 397 } 398 399 func (tx *AccountCreateTransaction) Schedule() (*ScheduleCreateTransaction, error) { 400 return tx.Transaction.schedule(tx) 401 } 402 403 // ----------- Overridden functions ---------------- 404 405 func (tx *AccountCreateTransaction) getName() string { 406 return "AccountCreateTransaction" 407 } 408 409 func (tx *AccountCreateTransaction) validateNetworkOnIDs(client *Client) error { 410 if client == nil || !client.autoValidateChecksums { 411 return nil 412 } 413 414 if tx.proxyAccountID != nil { 415 if tx.proxyAccountID != nil { 416 if err := tx.proxyAccountID.ValidateChecksum(client); err != nil { 417 return err 418 } 419 } 420 } 421 422 return nil 423 } 424 425 func (tx *AccountCreateTransaction) build() *services.TransactionBody { 426 return &services.TransactionBody{ 427 TransactionID: tx.transactionID._ToProtobuf(), 428 TransactionFee: tx.transactionFee, 429 TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()), 430 Memo: tx.Transaction.memo, 431 Data: &services.TransactionBody_CryptoCreateAccount{ 432 CryptoCreateAccount: tx.buildProtoBody(), 433 }, 434 } 435 } 436 func (tx *AccountCreateTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) { 437 return &services.SchedulableTransactionBody{ 438 TransactionFee: tx.transactionFee, 439 Memo: tx.Transaction.memo, 440 Data: &services.SchedulableTransactionBody_CryptoCreateAccount{ 441 CryptoCreateAccount: tx.buildProtoBody(), 442 }, 443 }, nil 444 } 445 446 func (tx *AccountCreateTransaction) buildProtoBody() *services.CryptoCreateTransactionBody { 447 body := &services.CryptoCreateTransactionBody{ 448 InitialBalance: tx.initialBalance, 449 ReceiverSigRequired: tx.receiverSignatureRequired, 450 Memo: tx.memo, 451 MaxAutomaticTokenAssociations: tx.maxAutomaticTokenAssociations, 452 DeclineReward: tx.declineReward, 453 Alias: tx.alias, 454 } 455 456 if tx.key != nil { 457 body.Key = tx.key._ToProtoKey() 458 } 459 460 if tx.autoRenewPeriod != nil { 461 body.AutoRenewPeriod = _DurationToProtobuf(*tx.autoRenewPeriod) 462 } 463 464 if tx.stakedAccountID != nil { 465 body.StakedId = &services.CryptoCreateTransactionBody_StakedAccountId{StakedAccountId: tx.stakedAccountID._ToProtobuf()} 466 } else if tx.stakedNodeID != nil { 467 body.StakedId = &services.CryptoCreateTransactionBody_StakedNodeId{StakedNodeId: *tx.stakedNodeID} 468 } 469 470 return body 471 } 472 473 func (tx *AccountCreateTransaction) getMethod(channel *_Channel) _Method { 474 return _Method{ 475 transaction: channel._GetCrypto().CreateAccount, 476 } 477 } 478 479 func (tx *AccountCreateTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) { 480 return tx.buildScheduled() 481 }