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