Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inner transactions on main transaction #260

Merged
merged 15 commits into from
Oct 11, 2023
1 change: 1 addition & 0 deletions data/transaction/apiTransactionResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type ApiTransactionResult struct {
Options uint32 `json:"options"`
GuardianAddr string `json:"guardian,omitempty"`
GuardianSignature string `json:"guardianSignature,omitempty"`
InnerTransaction data.TransactionHandler `json:"innerTransaction,omitempty"`
}

// ApiSmartContractResult represents a smart contract result with changed fields' types in order to make it friendly for API's json
Expand Down
32 changes: 17 additions & 15 deletions data/transaction/frontendTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package transaction

// FrontendTransaction represents the DTO used in transaction signing/validation.
type FrontendTransaction struct {
Nonce uint64 `json:"nonce"`
Value string `json:"value"`
Receiver string `json:"receiver"`
Sender string `json:"sender"`
SenderUsername []byte `json:"senderUsername,omitempty"`
ReceiverUsername []byte `json:"receiverUsername,omitempty"`
GasPrice uint64 `json:"gasPrice"`
GasLimit uint64 `json:"gasLimit"`
Data []byte `json:"data,omitempty"`
Signature string `json:"signature,omitempty"`
ChainID string `json:"chainID"`
Version uint32 `json:"version"`
Options uint32 `json:"options,omitempty"`
GuardianAddr string `json:"guardian,omitempty"`
GuardianSignature string `json:"guardianSignature,omitempty"`
Nonce uint64 `json:"nonce"`
Value string `json:"value"`
Receiver string `json:"receiver"`
Sender string `json:"sender"`
SenderUsername []byte `json:"senderUsername,omitempty"`
ReceiverUsername []byte `json:"receiverUsername,omitempty"`
GasPrice uint64 `json:"gasPrice"`
GasLimit uint64 `json:"gasLimit"`
Data []byte `json:"data,omitempty"`
Signature string `json:"signature,omitempty"`
ChainID string `json:"chainID"`
Version uint32 `json:"version"`
Options uint32 `json:"options,omitempty"`
GuardianAddr string `json:"guardian,omitempty"`
GuardianSignature string `json:"guardianSignature,omitempty"`
Relayer string `json:"relayer,omitempty"`
InnerTransaction *FrontendTransaction `json:"innerTransaction,omitempty"`
}
81 changes: 55 additions & 26 deletions data/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package transaction

import (
"encoding/hex"
"math/big"

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -68,38 +69,19 @@ func (tx *Transaction) GetDataForSigning(encoder data.Encoder, marshaller data.M
return nil, ErrNilHasher
}

receiverAddr, err := encoder.Encode(tx.RcvAddr)
ftx, err := tx.prepareTx(encoder)
if err != nil {
return nil, err
}

senderAddr, err := encoder.Encode(tx.SndAddr)
if err != nil {
return nil, err
}

ftx := &FrontendTransaction{
Nonce: tx.Nonce,
Value: tx.Value.String(),
Receiver: receiverAddr,
Sender: senderAddr,
GasPrice: tx.GasPrice,
GasLimit: tx.GasLimit,
SenderUsername: tx.SndUserName,
ReceiverUsername: tx.RcvUserName,
Data: tx.Data,
ChainID: string(tx.ChainID),
Version: tx.Version,
Options: tx.Options,
}

if len(tx.GuardianAddr) > 0 {
guardianAddr, errGuardian := encoder.Encode(tx.GuardianAddr)
if errGuardian != nil {
return nil, errGuardian
if tx.InnerTransaction != nil {
ftx.InnerTransaction, err = tx.InnerTransaction.prepareTx(encoder)
if err != nil {
return nil, err
}

ftx.GuardianAddr = guardianAddr
ftx.InnerTransaction.Signature = hex.EncodeToString(tx.InnerTransaction.Signature)
ftx.InnerTransaction.GuardianSignature = hex.EncodeToString(tx.InnerTransaction.GuardianSignature)
}

ftxBytes, err := marshaller.Marshal(ftx)
Expand Down Expand Up @@ -147,3 +129,50 @@ func (tx *Transaction) CheckIntegrity() error {

return nil
}

func (tx *Transaction) prepareTx(encoder data.Encoder) (*FrontendTransaction, error) {
receiverAddr, err := encoder.Encode(tx.RcvAddr)
if err != nil {
return nil, err
}

senderAddr, err := encoder.Encode(tx.SndAddr)
if err != nil {
return nil, err
}

ftx := &FrontendTransaction{
Nonce: tx.Nonce,
Value: tx.Value.String(),
Receiver: receiverAddr,
Sender: senderAddr,
GasPrice: tx.GasPrice,
GasLimit: tx.GasLimit,
SenderUsername: tx.SndUserName,
ReceiverUsername: tx.RcvUserName,
Data: tx.Data,
ChainID: string(tx.ChainID),
Version: tx.Version,
Options: tx.Options,
}

if len(tx.RelayerAddr) > 0 {
relayerAddr, errRelayer := encoder.Encode(tx.RelayerAddr)
if errRelayer != nil {
return nil, errRelayer
}

ftx.Relayer = relayerAddr
}

if len(tx.GuardianAddr) > 0 {
guardianAddr, errGuardian := encoder.Encode(tx.GuardianAddr)
if errGuardian != nil {
return nil, errGuardian
}

ftx.GuardianAddr = guardianAddr
}

return ftx, nil
}
Loading