Skip to content

Commit

Permalink
feat: Added ToBytes and FromBytes into *ID, *Info, TransactionRecord,…
Browse files Browse the repository at this point in the history
… and TransactionReceipt
  • Loading branch information
andrix10 authored and janaakhterov committed Jan 26, 2021
1 parent 93bd0eb commit bf614fa
Show file tree
Hide file tree
Showing 20 changed files with 460 additions and 12 deletions.
20 changes: 20 additions & 0 deletions account_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hedera

import (
"fmt"
protobuf "github.com/golang/protobuf/proto"
"strings"

"github.com/hashgraph/hedera-sdk-go/v2/proto"
Expand Down Expand Up @@ -92,3 +93,22 @@ func (id AccountID) isZero() bool {
func (id AccountID) equals(other AccountID) bool {
return id.Shard == other.Shard && id.Realm == other.Realm && id.Account == other.Account
}

func (id AccountID) ToBytes() []byte {
data, err := protobuf.Marshal(id.toProtobuf())
if err != nil {
return make([]byte, 0)
}

return data
}

func AccountIDFromBytes(data []byte) (AccountID, error) {
pb := proto.AccountID{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return AccountID{}, err
}

return accountIDFromProtobuf(&pb), nil
}
31 changes: 24 additions & 7 deletions account_info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hedera

import (
"github.com/pkg/errors"
"time"

protobuf "github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -56,7 +55,7 @@ func accountInfoFromProtobuf(pb *proto.CryptoGetInfoResponse_AccountInfo) (Accou
}, nil
}

func (info AccountInfo) toProtobuf() ([]byte, error) {
func (info AccountInfo) toProtobuf() *proto.CryptoGetInfoResponse_AccountInfo {

tokenRelationship := make([]*proto.TokenRelationship, len(info.TokenRelationships))

Expand All @@ -65,7 +64,7 @@ func (info AccountInfo) toProtobuf() ([]byte, error) {
tokenRelationship[i] = singleRelationship
}

pb, err := protobuf.Marshal(&proto.CryptoGetInfoResponse_AccountInfo{
return &proto.CryptoGetInfoResponse_AccountInfo{
AccountID: info.AccountID.toProtobuf(),
ContractAccountID: info.ContractAccountID,
Deleted: info.IsDeleted,
Expand All @@ -78,11 +77,29 @@ func (info AccountInfo) toProtobuf() ([]byte, error) {
ReceiverSigRequired: info.ReceiverSigRequired,
TokenRelationships: tokenRelationship,
ExpirationTime: timeToProtobuf(info.ExpirationTime),
})
}
}

func (info AccountInfo) ToBytes() []byte {
data, err := protobuf.Marshal(info.toProtobuf())
if err != nil {
return pb, errors.Wrap(err, "error serializing account info")
} else {
return pb, nil
return make([]byte, 0)
}

return data
}

func AccountInfoFromBytes(data []byte) (AccountInfo, error) {
pb := proto.CryptoGetInfoResponse_AccountInfo{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return AccountInfo{}, err
}

info, err := accountInfoFromProtobuf(&pb)
if err != nil {
return AccountInfo{}, err
}

return info, nil
}
2 changes: 1 addition & 1 deletion account_records_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (query *AccountRecordsQuery) Execute(client *Client) ([]TransactionRecord,
}

for _, element := range resp.query.GetCryptoGetAccountRecords().Records {
records = append(records, TransactionRecordFromProtobuf(element))
records = append(records, transactionRecordFromProtobuf(element))
}

return records, err
Expand Down
20 changes: 20 additions & 0 deletions contract_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hedera

import (
"fmt"
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
)

Expand Down Expand Up @@ -69,3 +70,22 @@ func contractIDFromProtobuf(pb *proto.ContractID) ContractID {
func (id ContractID) toProtoKey() *proto.Key {
return &proto.Key{Key: &proto.Key_ContractID{ContractID: id.toProtobuf()}}
}

func (id ContractID) ToBytes() []byte {
data, err := protobuf.Marshal(id.toProtobuf())
if err != nil {
return make([]byte, 0)
}

return data
}

func ContractIDFromBytes(data []byte) (ContractID, error) {
pb := proto.ContractID{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return ContractID{}, err
}

return contractIDFromProtobuf(&pb), nil
}
25 changes: 25 additions & 0 deletions contract_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
"time"
)
Expand Down Expand Up @@ -63,3 +64,27 @@ func (contractInfo *ContractInfo) toProtobuf() *proto.ContractGetInfoResponse_Co
Balance: contractInfo.Balance,
}
}

func (contractInfo ContractInfo) ToBytes() []byte {
data, err := protobuf.Marshal(contractInfo.toProtobuf())
if err != nil {
return make([]byte, 0)
}

return data
}

func ContractInfoFromBytes(data []byte) (ContractInfo, error) {
pb := proto.ContractGetInfoResponse_ContractInfo{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return ContractInfo{}, err
}

info, err := contractInfoFromProtobuf(&pb)
if err != nil {
return ContractInfo{}, err
}

return info, nil
}
20 changes: 20 additions & 0 deletions file_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hedera

import (
"fmt"
protobuf "github.com/golang/protobuf/proto"

"github.com/hashgraph/hedera-sdk-go/v2/proto"
)
Expand Down Expand Up @@ -79,3 +80,22 @@ func fileIDFromProtobuf(pb *proto.FileID) FileID {
File: uint64(pb.FileNum),
}
}

func (id FileID) ToBytes() []byte {
data, err := protobuf.Marshal(id.toProtobuf())
if err != nil {
return make([]byte, 0)
}

return data
}

func FileIDFromBytes(data []byte) (FileID, error) {
pb := proto.FileID{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return FileID{}, err
}

return fileIDFromProtobuf(&pb), nil
}
25 changes: 25 additions & 0 deletions file_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
"time"
)
Expand Down Expand Up @@ -54,3 +55,27 @@ func (fileInfo *FileInfo) toProtobuf() *proto.FileGetInfoResponse_FileInfo {
Keys: fileInfo.Keys.toProtoKeyList(),
}
}

func (fileInfo FileInfo) ToBytes() []byte {
data, err := protobuf.Marshal(fileInfo.toProtobuf())
if err != nil {
return make([]byte, 0)
}

return data
}

func FileInfoFromBytes(data []byte) (FileInfo, error) {
pb := proto.FileGetInfoResponse_FileInfo{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return FileInfo{}, err
}

info, err := fileInfoFromProtobuf(&pb)
if err != nil {
return FileInfo{}, err
}

return info, nil
}
25 changes: 25 additions & 0 deletions live_hash.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/v2/proto"

"time"
Expand Down Expand Up @@ -48,3 +49,27 @@ func liveHashFromProtobuf(hash *proto.LiveHash) (LiveHash, error) {
int(hash.Duration.Seconds), time.Now().Nanosecond(), time.Now().Location()),
}, nil
}

func (liveHash LiveHash) ToBytes() []byte {
data, err := protobuf.Marshal(liveHash.toProtobuf())
if err != nil {
return make([]byte, 0)
}

return data
}

func LiveHashFromBytes(data []byte) (LiveHash, error) {
pb := proto.LiveHash{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return LiveHash{}, err
}

liveHash, err := liveHashFromProtobuf(&pb)
if err != nil {
return LiveHash{}, err
}

return liveHash, nil
}
1 change: 1 addition & 0 deletions token_create_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func Test_TokenCreate_NoKeys(t *testing.T) {
SetTokenName("ffff").
SetTokenSymbol("F").
SetTreasuryAccountID(client.GetOperatorAccountID()).
SetAdminKey(client.GetOperatorPublicKey()).
Execute(client)
assert.NoError(t, err)

Expand Down
102 changes: 102 additions & 0 deletions token_grant_kyc_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,105 @@ func Test_TokenGrantKyc_NoAccountID(t *testing.T) {
_, err = resp.GetReceipt(client)
assert.NoError(t, err)
}

func TestTokenGrantKycTransaction_NoKycSet_Execute(t *testing.T) {
client := newTestClient(t)

newKey, err := GeneratePrivateKey()
assert.NoError(t, err)

newBalance := NewHbar(2)

assert.Equal(t, 2*HbarUnits.Hbar.numberOfTinybar(), newBalance.tinybar)

resp, err := NewAccountCreateTransaction().
SetKey(newKey.PublicKey()).
SetInitialBalance(newBalance).
Execute(client)
assert.NoError(t, err)

receipt, err := resp.GetReceipt(client)
assert.NoError(t, err)

accountID := *receipt.AccountID

resp, err = NewTokenCreateTransaction().
SetTokenName("ffff").
SetTokenSymbol("F").
SetDecimals(3).
SetInitialSupply(1000000).
SetTreasuryAccountID(client.GetOperatorAccountID()).
SetAdminKey(client.GetOperatorPublicKey()).
SetFreezeDefault(false).
Execute(client)
assert.NoError(t, err)

receipt, err = resp.GetReceipt(client)
assert.NoError(t, err)

tokenID := *receipt.TokenID

transaction, err := NewTokenAssociateTransaction().
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetAccountID(accountID).
SetTokenIDs(tokenID).
FreezeWith(client)
assert.NoError(t, err)

resp, err = transaction.
Sign(newKey).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)

resp, err = NewTokenGrantKycTransaction().
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetAccountID(accountID).
SetTokenID(tokenID).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)

info, err := NewAccountInfoQuery().
SetAccountID(accountID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
Execute(client)
assert.NoError(t, err)

check := false
for _, relation := range info.TokenRelationships {
if relation.KycStatus != nil {
if *relation.KycStatus {
check = true
}
}
}
assert.Truef(t, check, fmt.Sprintf("token grant kyc transaction failed"))

resp, err = NewTokenDeleteTransaction().
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetTokenID(tokenID).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)

tx, err := NewAccountDeleteTransaction().
SetAccountID(accountID).
SetTransferAccountID(client.GetOperatorAccountID()).
FreezeWith(client)
assert.NoError(t, err)

resp, err = tx.
Sign(newKey).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)
}
Loading

0 comments on commit bf614fa

Please sign in to comment.