diff --git a/sdk/account_allowance_approve_transaction_unit_test.go b/sdk/account_allowance_approve_transaction_unit_test.go index 6ae66f2e..53b15975 100644 --- a/sdk/account_allowance_approve_transaction_unit_test.go +++ b/sdk/account_allowance_approve_transaction_unit_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/hiero-ledger/hiero-sdk-go/v2/proto/services" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/wrapperspb" ) @@ -251,3 +252,15 @@ func TestUnitAccountAllowanceApproveTransactionScheduleProtobuf(t *testing.T) { require.NoError(t, err) require.Equal(t, expected.String(), actual.String()) } + +func TestUnitAllowanceApproveTransactionFromToBytes(t *testing.T) { + tx := NewAccountAllowanceApproveTransaction() + + txBytes, err := tx.ToBytes() + require.NoError(t, err) + + txFromBytes, err := TransactionFromBytes(txBytes) + require.NoError(t, err) + + assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountAllowanceApproveTransaction).buildProtoBody()) +} diff --git a/sdk/account_allowance_delete_transaction_unit_test.go b/sdk/account_allowance_delete_transaction_unit_test.go index 3bcf6587..b62e427d 100644 --- a/sdk/account_allowance_delete_transaction_unit_test.go +++ b/sdk/account_allowance_delete_transaction_unit_test.go @@ -9,6 +9,7 @@ import ( "github.com/hiero-ledger/hiero-sdk-go/v2/proto/services" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" protobuf "google.golang.org/protobuf/proto" ) @@ -148,3 +149,15 @@ func TestUnitAccountAllowanceDeleteTransactionSetNothing(t *testing.T) { _, err = transaction.GetSignatures() require.NoError(t, err) } + +func TestUnitAllowanceDeleteTransactionFromToBytes(t *testing.T) { + tx := NewAccountAllowanceDeleteTransaction() + + txBytes, err := tx.ToBytes() + require.NoError(t, err) + + txFromBytes, err := TransactionFromBytes(txBytes) + require.NoError(t, err) + + assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountAllowanceDeleteTransaction).buildProtoBody()) +} diff --git a/sdk/account_create_transaction.go b/sdk/account_create_transaction.go index 78c0a1d0..794045de 100644 --- a/sdk/account_create_transaction.go +++ b/sdk/account_create_transaction.go @@ -46,8 +46,16 @@ func NewAccountCreateTransaction() *AccountCreateTransaction { } func _AccountCreateTransactionFromProtobuf(tx Transaction[*AccountCreateTransaction], pb *services.TransactionBody) AccountCreateTransaction { - key, _ := _KeyFromProtobuf(pb.GetCryptoCreateAccount().GetKey()) - renew := _DurationFromProtobuf(pb.GetCryptoCreateAccount().GetAutoRenewPeriod()) + var key Key = nil + if pb.GetCryptoCreateAccount().GetKey() != nil { + key, _ = _KeyFromProtobuf(pb.GetCryptoCreateAccount().GetKey()) + } + + var autoRenew *time.Duration + if pb.GetCryptoCreateAccount().GetAutoRenewPeriod() != nil { + autoRenewVal := _DurationFromProtobuf(pb.GetCryptoCreateAccount().GetAutoRenewPeriod()) + autoRenew = &autoRenewVal + } var stakedNodeID *int64 if pb.GetCryptoCreateAccount().GetStakedNodeId() != 0 { @@ -62,7 +70,7 @@ func _AccountCreateTransactionFromProtobuf(tx Transaction[*AccountCreateTransact accountCreateTransaction := AccountCreateTransaction{ key: key, initialBalance: pb.GetCryptoCreateAccount().InitialBalance, - autoRenewPeriod: &renew, + autoRenewPeriod: autoRenew, memo: pb.GetCryptoCreateAccount().GetMemo(), receiverSignatureRequired: pb.GetCryptoCreateAccount().ReceiverSigRequired, maxAutomaticTokenAssociations: pb.GetCryptoCreateAccount().MaxAutomaticTokenAssociations, diff --git a/sdk/account_create_transaction_unit_test.go b/sdk/account_create_transaction_unit_test.go index c3ebb063..1c2d8c89 100644 --- a/sdk/account_create_transaction_unit_test.go +++ b/sdk/account_create_transaction_unit_test.go @@ -360,3 +360,15 @@ func TestUnitAccountCreateSetStakedAccountID(t *testing.T) { require.Equal(t, int64(0), tx.GetStakedNodeID()) require.Equal(t, account, tx.GetStakedAccountID()) } + +func TestUnitAccountCreateTransactionFromToBytes(t *testing.T) { + tx := NewAccountCreateTransaction() + + txBytes, err := tx.ToBytes() + require.NoError(t, err) + + txFromBytes, err := TransactionFromBytes(txBytes) + require.NoError(t, err) + + assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountCreateTransaction).buildProtoBody()) +} diff --git a/sdk/account_delete_transaction_unit_test.go b/sdk/account_delete_transaction_unit_test.go index 833a25f0..82659fd5 100644 --- a/sdk/account_delete_transaction_unit_test.go +++ b/sdk/account_delete_transaction_unit_test.go @@ -251,3 +251,15 @@ func TestUnitAccountDeleteTransactionTransactionMock(t *testing.T) { Execute(client) require.NoError(t, err) } + +func TestUnitAccountDeleteTransactionFromToBytes(t *testing.T) { + tx := NewAccountDeleteTransaction() + + txBytes, err := tx.ToBytes() + require.NoError(t, err) + + txFromBytes, err := TransactionFromBytes(txBytes) + require.NoError(t, err) + + assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountDeleteTransaction).buildProtoBody()) +} diff --git a/sdk/account_update_transaction.go b/sdk/account_update_transaction.go index 90b3bfeb..bbe3d803 100644 --- a/sdk/account_update_transaction.go +++ b/sdk/account_update_transaction.go @@ -50,7 +50,11 @@ func NewAccountUpdateTransaction() *AccountUpdateTransaction { } func _AccountUpdateTransactionFromProtobuf(tx Transaction[*AccountUpdateTransaction], pb *services.TransactionBody) AccountUpdateTransaction { - key, _ := _KeyFromProtobuf(pb.GetCryptoUpdateAccount().GetKey()) + var key Key + if pb.GetCryptoUpdateAccount().GetKey() != nil { + key, _ = _KeyFromProtobuf(pb.GetCryptoUpdateAccount().GetKey()) + } + var receiverSignatureRequired bool switch s := pb.GetCryptoUpdateAccount().GetReceiverSigRequiredField().(type) { @@ -60,10 +64,23 @@ func _AccountUpdateTransactionFromProtobuf(tx Transaction[*AccountUpdateTransact receiverSignatureRequired = s.ReceiverSigRequiredWrapper.Value // nolint } - autoRenew := _DurationFromProtobuf(pb.GetCryptoUpdateAccount().AutoRenewPeriod) - expiration := _TimeFromProtobuf(pb.GetCryptoUpdateAccount().ExpirationTime) + var autoRenew *time.Duration + if pb.GetCryptoUpdateAccount().GetAutoRenewPeriod() != nil { + autoRenewVal := _DurationFromProtobuf(pb.GetCryptoUpdateAccount().GetAutoRenewPeriod()) + autoRenew = &autoRenewVal + } + + var expiration *time.Time + if pb.GetCryptoUpdateAccount().GetExpirationTime() != nil { + expirationVal := _TimeFromProtobuf(pb.GetCryptoUpdateAccount().GetExpirationTime()) + expiration = &expirationVal + } - stakedNodeID := pb.GetCryptoUpdateAccount().GetStakedNodeId() + var stakedNodeID *int64 + if pb.GetCryptoUpdateAccount().GetStakedNodeId() != 0 { + stakedNodeIdVal := pb.GetCryptoUpdateAccount().GetStakedNodeId() + stakedNodeID = &stakedNodeIdVal + } var stakeNodeAccountID *AccountID if pb.GetCryptoUpdateAccount().GetStakedAccountId() != nil { @@ -73,13 +90,13 @@ func _AccountUpdateTransactionFromProtobuf(tx Transaction[*AccountUpdateTransact accountUpdateTransaction := AccountUpdateTransaction{ accountID: _AccountIDFromProtobuf(pb.GetCryptoUpdateAccount().GetAccountIDToUpdate()), key: key, - autoRenewPeriod: &autoRenew, + autoRenewPeriod: autoRenew, memo: pb.GetCryptoUpdateAccount().GetMemo().Value, receiverSignatureRequired: receiverSignatureRequired, - expirationTime: &expiration, + expirationTime: expiration, maxAutomaticTokenAssociations: pb.GetCryptoUpdateAccount().MaxAutomaticTokenAssociations.GetValue(), stakedAccountID: stakeNodeAccountID, - stakedNodeID: &stakedNodeID, + stakedNodeID: stakedNodeID, declineReward: pb.GetCryptoUpdateAccount().GetDeclineReward().GetValue(), } diff --git a/sdk/account_update_transaction_unit_test.go b/sdk/account_update_transaction_unit_test.go index 1f36e96c..95c877fd 100644 --- a/sdk/account_update_transaction_unit_test.go +++ b/sdk/account_update_transaction_unit_test.go @@ -328,3 +328,15 @@ func TestUnitAccountUpdateTransactionCoverage(t *testing.T) { b.AddSignature(key.PublicKey(), sig) } } + +func TestUnitAccountUpdateTransactionFromToBytes(t *testing.T) { + tx := NewAccountUpdateTransaction() + + txBytes, err := tx.ToBytes() + require.NoError(t, err) + + txFromBytes, err := TransactionFromBytes(txBytes) + require.NoError(t, err) + + assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountUpdateTransaction).buildProtoBody()) +} diff --git a/sdk/contract_create_transaction.go b/sdk/contract_create_transaction.go index f52b11a5..c7474d37 100644 --- a/sdk/contract_create_transaction.go +++ b/sdk/contract_create_transaction.go @@ -45,9 +45,21 @@ func NewContractCreateTransaction() *ContractCreateTransaction { } func _ContractCreateTransactionFromProtobuf(tx Transaction[*ContractCreateTransaction], pb *services.TransactionBody) ContractCreateTransaction { - key, _ := _KeyFromProtobuf(pb.GetContractCreateInstance().GetAdminKey()) - autoRenew := _DurationFromProtobuf(pb.GetContractCreateInstance().GetAutoRenewPeriod()) - stakedNodeID := pb.GetContractCreateInstance().GetStakedNodeId() + var key Key = nil + if pb.GetContractCreateInstance().GetAdminKey() != nil { + key, _ = _KeyFromProtobuf(pb.GetContractCreateInstance().GetAdminKey()) + } + var autoRenew *time.Duration + if pb.GetContractCreateInstance().GetAutoRenewPeriod() != nil { + autoRenewVal := _DurationFromProtobuf(pb.GetContractCreateInstance().GetAutoRenewPeriod()) + autoRenew = &autoRenewVal + } + + var stakedNodeID *int64 + if pb.GetContractCreateInstance().GetStakedNodeId() != 0 { + stakedNodeIdVal := pb.GetContractCreateInstance().GetStakedNodeId() + stakedNodeID = &stakedNodeIdVal + } var stakeNodeAccountID *AccountID if pb.GetContractCreateInstance().GetStakedAccountId() != nil { @@ -64,14 +76,14 @@ func _ContractCreateTransactionFromProtobuf(tx Transaction[*ContractCreateTransa adminKey: key, gas: pb.GetContractCreateInstance().Gas, initialBalance: pb.GetContractCreateInstance().InitialBalance, - autoRenewPeriod: &autoRenew, + autoRenewPeriod: autoRenew, parameters: pb.GetContractCreateInstance().ConstructorParameters, memo: pb.GetContractCreateInstance().GetMemo(), initcode: pb.GetContractCreateInstance().GetInitcode(), autoRenewAccountID: autoRenewAccountID, maxAutomaticTokenAssociations: pb.GetContractCreateInstance().MaxAutomaticTokenAssociations, stakedAccountID: stakeNodeAccountID, - stakedNodeID: &stakedNodeID, + stakedNodeID: stakedNodeID, declineReward: pb.GetContractCreateInstance().GetDeclineReward(), } tx.childTransaction = &contractCreateTransaction diff --git a/sdk/contract_create_transaction_unit_test.go b/sdk/contract_create_transaction_unit_test.go index f5d94c49..32a0ced2 100644 --- a/sdk/contract_create_transaction_unit_test.go +++ b/sdk/contract_create_transaction_unit_test.go @@ -336,3 +336,15 @@ func TestUnitContractCreateTransactionCoverage(t *testing.T) { b.AddSignature(newKey.PublicKey(), sig) } } + +func TestUnitContractCreateTransactionFromToBytes(t *testing.T) { + tx := NewContractCreateTransaction() + + txBytes, err := tx.ToBytes() + require.NoError(t, err) + + txFromBytes, err := TransactionFromBytes(txBytes) + require.NoError(t, err) + + assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ContractCreateTransaction).buildProtoBody()) +} diff --git a/sdk/contract_delete_transaction_unit_test.go b/sdk/contract_delete_transaction_unit_test.go index a2f8d2fb..c3589f95 100644 --- a/sdk/contract_delete_transaction_unit_test.go +++ b/sdk/contract_delete_transaction_unit_test.go @@ -274,3 +274,15 @@ func TestUnitContractDeleteTransactionMock(t *testing.T) { Execute(client) require.NoError(t, err) } + +func TestUnitContractDeleteTransactionFromToBytes(t *testing.T) { + tx := NewContractDeleteTransaction() + + txBytes, err := tx.ToBytes() + require.NoError(t, err) + + txFromBytes, err := TransactionFromBytes(txBytes) + require.NoError(t, err) + + assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ContractDeleteTransaction).buildProtoBody()) +} diff --git a/sdk/contract_execute_transaction_unit_test.go b/sdk/contract_execute_transaction_unit_test.go index f006e5ec..c39e2a0f 100644 --- a/sdk/contract_execute_transaction_unit_test.go +++ b/sdk/contract_execute_transaction_unit_test.go @@ -290,3 +290,15 @@ func TestUnitContractExecuteTransactionCoverage(t *testing.T) { b.AddSignature(newKey.PublicKey(), sig) } } + +func TestUnitContractExecuteTransactionFromToBytes(t *testing.T) { + tx := NewContractExecuteTransaction() + + txBytes, err := tx.ToBytes() + require.NoError(t, err) + + txFromBytes, err := TransactionFromBytes(txBytes) + require.NoError(t, err) + + assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ContractExecuteTransaction).buildProtoBody()) +} diff --git a/sdk/contract_update_transaction.go b/sdk/contract_update_transaction.go index 4597d0fa..52ae2901 100644 --- a/sdk/contract_update_transaction.go +++ b/sdk/contract_update_transaction.go @@ -63,9 +63,20 @@ func NewContractUpdateTransaction() *ContractUpdateTransaction { } func _ContractUpdateTransactionFromProtobuf(tx Transaction[*ContractUpdateTransaction], pb *services.TransactionBody) ContractUpdateTransaction { - key, _ := _KeyFromProtobuf(pb.GetContractUpdateInstance().AdminKey) - autoRenew := _DurationFromProtobuf(pb.GetContractUpdateInstance().GetAutoRenewPeriod()) - expiration := _TimeFromProtobuf(pb.GetContractUpdateInstance().GetExpirationTime()) + var key Key = nil + if pb.GetContractUpdateInstance().GetAdminKey() != nil { + key, _ = _KeyFromProtobuf(pb.GetContractUpdateInstance().GetAdminKey()) + } + var autoRenew *time.Duration + if pb.GetContractUpdateInstance().GetAutoRenewPeriod() != nil { + autoRenewVal := _DurationFromProtobuf(pb.GetContractUpdateInstance().GetAutoRenewPeriod()) + autoRenew = &autoRenewVal + } + var expiration *time.Time + if pb.GetContractUpdateInstance().GetExpirationTime() != nil { + expirationVal := _TimeFromProtobuf(pb.GetContractUpdateInstance().GetExpirationTime()) + expiration = &expirationVal + } var memo string switch m := pb.GetContractUpdateInstance().GetMemoField().(type) { @@ -75,7 +86,11 @@ func _ContractUpdateTransactionFromProtobuf(tx Transaction[*ContractUpdateTransa memo = m.MemoWrapper.Value } - stakedNodeID := pb.GetContractUpdateInstance().GetStakedNodeId() + var stakedNodeID *int64 + if pb.GetContractUpdateInstance().GetStakedNodeId() != 0 { + stakedNodeIdVal := pb.GetContractUpdateInstance().GetStakedNodeId() + stakedNodeID = &stakedNodeIdVal + } var stakeNodeAccountID *AccountID if pb.GetContractUpdateInstance().GetStakedAccountId() != nil { @@ -90,13 +105,13 @@ func _ContractUpdateTransactionFromProtobuf(tx Transaction[*ContractUpdateTransa contractUpdateTransaction := ContractUpdateTransaction{ contractID: _ContractIDFromProtobuf(pb.GetContractUpdateInstance().GetContractID()), adminKey: key, - autoRenewPeriod: &autoRenew, - expirationTime: &expiration, + autoRenewPeriod: autoRenew, + expirationTime: expiration, memo: memo, autoRenewAccountID: autoRenewAccountID, maxAutomaticTokenAssociations: pb.GetContractUpdateInstance().MaxAutomaticTokenAssociations.GetValue(), stakedAccountID: stakeNodeAccountID, - stakedNodeID: &stakedNodeID, + stakedNodeID: stakedNodeID, declineReward: pb.GetContractUpdateInstance().GetDeclineReward().GetValue(), } tx.childTransaction = &contractUpdateTransaction @@ -384,14 +399,6 @@ func (tx ContractUpdateTransaction) buildProtoBody() *services.ContractUpdateTra } } - if tx.adminKey != nil { - body.AdminKey = tx.adminKey._ToProtoKey() - } - - if tx.contractID != nil { - body.ContractID = tx.contractID._ToProtobuf() - } - if tx.stakedAccountID != nil { body.StakedId = &services.ContractUpdateTransactionBody_StakedAccountId{StakedAccountId: tx.stakedAccountID._ToProtobuf()} } else if tx.stakedNodeID != nil { diff --git a/sdk/contract_update_transaction_unit_test.go b/sdk/contract_update_transaction_unit_test.go index 91ed34d0..8186480f 100644 --- a/sdk/contract_update_transaction_unit_test.go +++ b/sdk/contract_update_transaction_unit_test.go @@ -334,3 +334,15 @@ func TestUnitContractUpdateTransactionCoverage(t *testing.T) { b.AddSignature(newKey.PublicKey(), sig) } } + +func TestUnitContractUpdateTransactionFromToBytes(t *testing.T) { + tx := NewContractUpdateTransaction() + + txBytes, err := tx.ToBytes() + require.NoError(t, err) + + txFromBytes, err := TransactionFromBytes(txBytes) + require.NoError(t, err) + + assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ContractUpdateTransaction).buildProtoBody()) +}