github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/rpc/convert/transactions.go (about)

     1  package convert
     2  
     3  import (
     4  	"github.com/onflow/flow/protobuf/go/flow/entities"
     5  
     6  	"github.com/onflow/flow-go/model/flow"
     7  )
     8  
     9  // TransactionToMessage converts a flow.TransactionBody to a protobuf message
    10  func TransactionToMessage(tb flow.TransactionBody) *entities.Transaction {
    11  	proposalKeyMessage := &entities.Transaction_ProposalKey{
    12  		Address:        tb.ProposalKey.Address.Bytes(),
    13  		KeyId:          uint32(tb.ProposalKey.KeyIndex),
    14  		SequenceNumber: tb.ProposalKey.SequenceNumber,
    15  	}
    16  
    17  	authMessages := make([][]byte, len(tb.Authorizers))
    18  	for i, auth := range tb.Authorizers {
    19  		authMessages[i] = auth.Bytes()
    20  	}
    21  
    22  	payloadSigMessages := make([]*entities.Transaction_Signature, len(tb.PayloadSignatures))
    23  
    24  	for i, sig := range tb.PayloadSignatures {
    25  		payloadSigMessages[i] = &entities.Transaction_Signature{
    26  			Address:   sig.Address.Bytes(),
    27  			KeyId:     uint32(sig.KeyIndex),
    28  			Signature: sig.Signature,
    29  		}
    30  	}
    31  
    32  	envelopeSigMessages := make([]*entities.Transaction_Signature, len(tb.EnvelopeSignatures))
    33  
    34  	for i, sig := range tb.EnvelopeSignatures {
    35  		envelopeSigMessages[i] = &entities.Transaction_Signature{
    36  			Address:   sig.Address.Bytes(),
    37  			KeyId:     uint32(sig.KeyIndex),
    38  			Signature: sig.Signature,
    39  		}
    40  	}
    41  
    42  	return &entities.Transaction{
    43  		Script:             tb.Script,
    44  		Arguments:          tb.Arguments,
    45  		ReferenceBlockId:   tb.ReferenceBlockID[:],
    46  		GasLimit:           tb.GasLimit,
    47  		ProposalKey:        proposalKeyMessage,
    48  		Payer:              tb.Payer.Bytes(),
    49  		Authorizers:        authMessages,
    50  		PayloadSignatures:  payloadSigMessages,
    51  		EnvelopeSignatures: envelopeSigMessages,
    52  	}
    53  }
    54  
    55  // MessageToTransaction converts a protobuf message to a flow.TransactionBody
    56  func MessageToTransaction(
    57  	m *entities.Transaction,
    58  	chain flow.Chain,
    59  ) (flow.TransactionBody, error) {
    60  	if m == nil {
    61  		return flow.TransactionBody{}, ErrEmptyMessage
    62  	}
    63  
    64  	t := flow.NewTransactionBody()
    65  
    66  	proposalKey := m.GetProposalKey()
    67  	if proposalKey != nil {
    68  		proposalAddress, err := Address(proposalKey.GetAddress(), chain)
    69  		if err != nil {
    70  			return *t, err
    71  		}
    72  		t.SetProposalKey(proposalAddress, uint64(proposalKey.GetKeyId()), proposalKey.GetSequenceNumber())
    73  	}
    74  
    75  	payer := m.GetPayer()
    76  	if payer != nil {
    77  		payerAddress, err := Address(payer, chain)
    78  		if err != nil {
    79  			return *t, err
    80  		}
    81  		t.SetPayer(payerAddress)
    82  	}
    83  
    84  	for _, authorizer := range m.GetAuthorizers() {
    85  		authorizerAddress, err := Address(authorizer, chain)
    86  		if err != nil {
    87  			return *t, err
    88  		}
    89  		t.AddAuthorizer(authorizerAddress)
    90  	}
    91  
    92  	for _, sig := range m.GetPayloadSignatures() {
    93  		addr, err := Address(sig.GetAddress(), chain)
    94  		if err != nil {
    95  			return *t, err
    96  		}
    97  		t.AddPayloadSignature(addr, uint64(sig.GetKeyId()), sig.GetSignature())
    98  	}
    99  
   100  	for _, sig := range m.GetEnvelopeSignatures() {
   101  		addr, err := Address(sig.GetAddress(), chain)
   102  		if err != nil {
   103  			return *t, err
   104  		}
   105  		t.AddEnvelopeSignature(addr, uint64(sig.GetKeyId()), sig.GetSignature())
   106  	}
   107  
   108  	t.SetScript(m.GetScript())
   109  	t.SetArguments(m.GetArguments())
   110  	t.SetReferenceBlockID(flow.HashToID(m.GetReferenceBlockId()))
   111  	t.SetComputeLimit(m.GetGasLimit())
   112  
   113  	return *t, nil
   114  }
   115  
   116  // TransactionsToMessages converts a slice of flow.TransactionBody to a slice of protobuf messages
   117  func TransactionsToMessages(transactions []*flow.TransactionBody) []*entities.Transaction {
   118  	transactionMessages := make([]*entities.Transaction, len(transactions))
   119  	for i, t := range transactions {
   120  		transactionMessages[i] = TransactionToMessage(*t)
   121  	}
   122  	return transactionMessages
   123  }