Skip to content

Commit

Permalink
feat: implement TransactionFromBytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
janaakhterov committed Nov 11, 2020
1 parent 52327b3 commit e83000b
Show file tree
Hide file tree
Showing 38 changed files with 388 additions and 40 deletions.
7 changes: 7 additions & 0 deletions account_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func NewAccountCreateTransaction() *AccountCreateTransaction {
return &transaction
}

func accountCreateTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) AccountCreateTransaction {
return AccountCreateTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetCryptoCreateAccount(),
}
}

// SetKey sets the key that must sign each transfer out of the account. If RecieverSignatureRequired is true, then it
// must also sign any transfer into the account.
func (transaction *AccountCreateTransaction) SetKey(key Key) *AccountCreateTransaction {
Expand Down
7 changes: 7 additions & 0 deletions account_delete_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type AccountDeleteTransaction struct {
pb *proto.CryptoDeleteTransactionBody
}

func accountDeleteTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) AccountDeleteTransaction {
return AccountDeleteTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetCryptoDelete(),
}
}

func NewAccountDeleteTransaction() *AccountDeleteTransaction {
pb := &proto.CryptoDeleteTransactionBody{}

Expand Down
7 changes: 7 additions & 0 deletions account_update_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func NewAccountUpdateTransaction() *AccountUpdateTransaction {
return &transaction
}

func accountUpdateTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) AccountUpdateTransaction {
return AccountUpdateTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetCryptoUpdateAccount(),
}
}

func (transaction *AccountUpdateTransaction) SetKey(key Key) *AccountUpdateTransaction {
transaction.requireNotFrozen()
transaction.pb.Key = key.toProtoKey()
Expand Down
7 changes: 7 additions & 0 deletions contract_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ func NewContractCreateTransaction() *ContractCreateTransaction {
return &transaction
}

func contractCreateTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) ContractCreateTransaction {
return ContractCreateTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetContractCreateInstance(),
}
}

func (transaction *ContractCreateTransaction) SetBytecodeFileID(bytecodeFileID FileID) *ContractCreateTransaction {
transaction.requireNotFrozen()
transaction.pb.FileID = bytecodeFileID.toProtobuf()
Expand Down
7 changes: 7 additions & 0 deletions contract_delete_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ func NewContractDeleteTransaction() *ContractDeleteTransaction {
return &transaction
}

func contractDeleteTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) ContractDeleteTransaction {
return ContractDeleteTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetContractDeleteInstance(),
}
}

func (transaction *ContractDeleteTransaction) SetContractID(contractID ContractID) *ContractDeleteTransaction {
transaction.requireNotFrozen()
transaction.pb.ContractID = contractID.toProtobuf()
Expand Down
7 changes: 7 additions & 0 deletions contract_execute_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func NewContractExecuteTransaction() *ContractExecuteTransaction {
return &transaction
}

func contractExecuteTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) ContractExecuteTransaction {
return ContractExecuteTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetContractCall(),
}
}

// SetContractID sets the contract instance to call.
func (transaction *ContractExecuteTransaction) SetContractID(ID ContractID) *ContractExecuteTransaction {
transaction.requireNotFrozen()
Expand Down
7 changes: 7 additions & 0 deletions contract_update_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func NewContractUpdateTransaction() *ContractUpdateTransaction {
return &transaction
}

func contractUpdateTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) ContractUpdateTransaction {
return ContractUpdateTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetContractUpdateInstance(),
}
}

// SetContractID sets The Contract ID instance to update (this can't be changed on the contract)
func (transaction *ContractUpdateTransaction) SetContractID(contractID ContractID) *ContractUpdateTransaction {
transaction.pb.ContractID = contractID.toProtobuf()
Expand Down
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var errNoClientOrTransactionIDOrNodeId = errors.New("`client` must be provided o
var errClientOperatorSigning = errors.New("`client` must have an `operator` to sign with the operator")
var errNoClientProvided = errors.New("`client` must be provided and have an operator")
var errTransactionIsNotFrozen = errors.New("transaction is not frozen")
var errFailedToDeserializeBytes = errors.New("Failed to deserialize bytes")
var errNoTransactionInBytes = errors.New("No transaction was found in bytes")

type ErrInvalidNodeAccountIDSet struct {
NodeAccountID AccountID
Expand Down
7 changes: 7 additions & 0 deletions file_append_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func NewFileAppendTransaction() *FileAppendTransaction {
return &transaction
}

func fileAppendTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) FileAppendTransaction {
return FileAppendTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetFileAppend(),
}
}

// SetFileID sets the FileID of the file to which the bytes are appended to.
func (transaction *FileAppendTransaction) SetFileID(ID FileID) *FileAppendTransaction {
transaction.requireNotFrozen()
Expand Down
7 changes: 7 additions & 0 deletions file_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func NewFileCreateTransaction() *FileCreateTransaction {
return &transaction
}

func fileCreateTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) FileCreateTransaction {
return FileCreateTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetFileCreate(),
}
}

// AddKey adds a key to the internal list of keys associated with the file. All of the keys on the list must sign to
// create or modify a file, but only one of them needs to sign in order to delete the file. Each of those "keys" may
// itself be threshold key containing other keys (including other threshold keys). In other words, the behavior is an
Expand Down
7 changes: 7 additions & 0 deletions file_delete_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ func NewFileDeleteTransaction() *FileDeleteTransaction {
return &transaction
}

func fileDeleteTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) FileDeleteTransaction {
return FileDeleteTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetFileDelete(),
}
}

func (transaction *FileDeleteTransaction) SetFileID(fileID FileID) *FileDeleteTransaction {
transaction.requireNotFrozen()
transaction.pb.FileID = fileID.toProtobuf()
Expand Down
7 changes: 7 additions & 0 deletions file_update_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func NewFileUpdateTransaction() *FileUpdateTransaction {
return &transaction
}

func fileUpdateTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) FileUpdateTransaction {
return FileUpdateTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetFileUpdate(),
}
}

func (transaction *FileUpdateTransaction) SetFileID(ID FileID) *FileUpdateTransaction {
transaction.requireNotFrozen()
transaction.pb.FileID = ID.toProtobuf()
Expand Down
7 changes: 7 additions & 0 deletions freeze_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ func NewFreezeTransaction() *FreezeTransaction {
return &transaction
}

func freezeTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) FreezeTransaction {
return FreezeTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetFreeze(),
}
}

func (transaction *FreezeTransaction) SetStartTime(startTime time.Time) *FreezeTransaction {
transaction.requireNotFrozen()
transaction.pb.StartHour = int32(startTime.Hour())
Expand Down
7 changes: 7 additions & 0 deletions live_hash_add_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func NewLiveHashAddTransaction() *LiveHashAddTransaction {
return &transaction
}

func liveHashAddTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) LiveHashAddTransaction {
return LiveHashAddTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetCryptoAddLiveHash(),
}
}

func (transaction *LiveHashAddTransaction) SetHash(hash []byte) *LiveHashAddTransaction {
transaction.requireNotFrozen()
transaction.pb.LiveHash.Hash = hash
Expand Down
7 changes: 7 additions & 0 deletions live_hash_delete_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func NewLiveHashDeleteTransaction() *LiveHashDeleteTransaction {
return &transaction
}

func liveHashDeleteTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) LiveHashDeleteTransaction {
return LiveHashDeleteTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetCryptoDeleteLiveHash(),
}
}

func (transaction *LiveHashDeleteTransaction) SetHash(hash []byte) *LiveHashDeleteTransaction {
transaction.requireNotFrozen()
transaction.pb.LiveHashToDelete = hash
Expand Down
6 changes: 6 additions & 0 deletions single_topic_message_submit_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ func newSingleTopicMessageSubmitTransaction(
pb: pb,
}
}
func singleTopicMessageSubmitTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) singleTopicMessageSubmitTransaction {
return singleTopicMessageSubmitTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetConsensusSubmitMessage(),
}

}
//
// The following methods must be copy-pasted/overriden at the bottom of **every** _transaction.go file
// We override the embedded fluent setter methods to return the outer type
Expand Down
7 changes: 7 additions & 0 deletions system_delete_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func NewSystemDeleteTransaction() *SystemDeleteTransaction {
return &transaction
}

func systemDeleteTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) SystemDeleteTransaction {
return SystemDeleteTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetSystemDelete(),
}
}

func (transaction *SystemDeleteTransaction) SetExpirationTime(expiration time.Time) *SystemDeleteTransaction {
transaction.requireNotFrozen()
transaction.pb.ExpirationTime = &proto.TimestampSeconds{
Expand Down
7 changes: 7 additions & 0 deletions system_undelete_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ func NewSystemUndeleteTransaction() *SystemUndeleteTransaction {
return &transaction
}

func systemUndeleteTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) SystemUndeleteTransaction {
return SystemUndeleteTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetSystemUndelete(),
}
}

func (transaction *SystemUndeleteTransaction) SetContractID(contractID ContractID) *SystemUndeleteTransaction {
transaction.requireNotFrozen()
transaction.pb.Id = &proto.SystemUndeleteTransactionBody_ContractID{ContractID: contractID.toProtobuf()}
Expand Down
7 changes: 7 additions & 0 deletions token_associate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func NewTokenAssociateTransaction() *TokenAssociateTransaction {
return &transaction
}

func tokenAssociateTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenAssociateTransaction {
return TokenAssociateTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenAssociate(),
}
}

// The account to be associated with the provided tokens
func (transaction *TokenAssociateTransaction) SetAccountID(accountID AccountID) *TokenAssociateTransaction {
transaction.requireNotFrozen()
Expand Down
7 changes: 7 additions & 0 deletions token_burn_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func NewTokenBurnTransaction() *TokenBurnTransaction {
return &transaction
}

func tokenBurnTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenBurnTransaction {
return TokenBurnTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenBurn(),
}
}

// The token for which to burn tokens. If token does not exist, transaction results in
// INVALID_TOKEN_ID
func (transaction *TokenBurnTransaction) SetTokenID(tokenID TokenID) *TokenBurnTransaction {
Expand Down
7 changes: 7 additions & 0 deletions token_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func NewTokenCreateTransaction() *TokenCreateTransaction {
return &transaction
}

func tokenCreateTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenCreateTransaction {
return TokenCreateTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenCreation(),
}
}

// The publicly visible name of the token, specified as a string of only ASCII characters
func (transaction *TokenCreateTransaction) SetTokenName(name string) *TokenCreateTransaction {
transaction.requireNotFrozen()
Expand Down
7 changes: 7 additions & 0 deletions token_delete_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func NewTokenDeleteTransaction() *TokenDeleteTransaction {
return &transaction
}

func tokenDeleteTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenDeleteTransaction {
return TokenDeleteTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenDeletion(),
}
}

// The Token to be deleted
func (transaction *TokenDeleteTransaction) SetTokenID(tokenID TokenID) *TokenDeleteTransaction {
transaction.requireNotFrozen()
Expand Down
7 changes: 7 additions & 0 deletions token_dissociate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func NewTokenDissociateTransaction() *TokenDissociateTransaction {
return &transaction
}

func tokenDissociateTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenDissociateTransaction {
return TokenDissociateTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenDissociate(),
}
}

// The account to be dissociated with the provided tokens
func (transaction *TokenDissociateTransaction) SetAccountID(accountID AccountID) *TokenDissociateTransaction {
transaction.requireNotFrozen()
Expand Down
7 changes: 7 additions & 0 deletions token_freeze_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func NewTokenFreezeTransaction() *TokenFreezeTransaction {
return &transaction
}

func tokenFreezeTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenFreezeTransaction {
return TokenFreezeTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenFreeze(),
}
}

// The token for which this account will be frozen. If token does not exist, transaction results
// in INVALID_TOKEN_ID
func (transaction *TokenFreezeTransaction) SetTokenID(tokenID TokenID) *TokenFreezeTransaction {
Expand Down
7 changes: 7 additions & 0 deletions token_grant_kyc_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func NewTokenGrantKycTransaction() *TokenGrantKycTransaction {
return &transaction
}

func tokenGrantKycTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenGrantKycTransaction {
return TokenGrantKycTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenGrantKyc(),
}
}

// The token for which this account will be granted KYC. If token does not exist, transaction results in INVALID_TOKEN_ID
func (transaction *TokenGrantKycTransaction) SetTokenID(tokenID TokenID) *TokenGrantKycTransaction {
transaction.requireNotFrozen()
Expand Down
7 changes: 7 additions & 0 deletions token_mint_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func NewTokenMintTransaction() *TokenMintTransaction {
return &transaction
}

func tokenMintTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenMintTransaction {
return TokenMintTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenMint(),
}
}

// The token for which to mint tokens. If token does not exist, transaction results in
// INVALID_TOKEN_ID
func (transaction *TokenMintTransaction) SetTokenID(tokenID TokenID) *TokenMintTransaction {
Expand Down
7 changes: 7 additions & 0 deletions token_revoke_kcy_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func NewTokenRevokeKycTransaction() *TokenRevokeKycTransaction {
return &transaction
}

func tokenRevokeKycTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenRevokeKycTransaction {
return TokenRevokeKycTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenRevokeKyc(),
}
}

// The token for which this account will get his KYC revoked. If token does not exist, transaction results in INVALID_TOKEN_ID
func (transaction *TokenRevokeKycTransaction) SetTokenID(tokenID TokenID) *TokenRevokeKycTransaction {
transaction.requireNotFrozen()
Expand Down
7 changes: 7 additions & 0 deletions token_unfreeze_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func NewTokenUnfreezeTransaction() *TokenUnfreezeTransaction {
return &transaction
}

func tokenUnfreezeTransactionFromProtobuf(transactions map[TransactionID]map[AccountID]*proto.Transaction, pb *proto.TransactionBody) TokenUnfreezeTransaction {
return TokenUnfreezeTransaction{
Transaction: transactionFromProtobuf(transactions, pb),
pb: pb.GetTokenUnfreeze(),
}
}

// The token for which this account will be unfrozen. If token does not exist, transaction results in INVALID_TOKEN_ID
func (transaction *TokenUnfreezeTransaction) SetTokenID(tokenID TokenID) *TokenUnfreezeTransaction {
transaction.requireNotFrozen()
Expand Down
Loading

0 comments on commit e83000b

Please sign in to comment.