This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
/
util.go
242 lines (207 loc) · 6.29 KB
/
util.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package model
import (
"bytes"
"crypto/ed25519"
"encoding/binary"
"math"
"strconv"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stellar/go/network"
"github.com/stellar/go/xdr"
"github.com/kinecosystem/agora-common/solana"
"github.com/kinecosystem/agora-common/solana/token"
"github.com/kinecosystem/go/strkey"
)
type SortableEntries []*Entry
// Len is the number of elements in the collection.
func (s SortableEntries) Len() int {
return len(s)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (s SortableEntries) Less(i int, j int) bool {
iKey, err := s[i].GetOrderingKey()
if err != nil {
iKey = []byte{}
}
jKey, err := s[j].GetOrderingKey()
if err != nil {
jKey = []byte{}
}
return bytes.Compare(iKey, jKey) < 0
}
// Swap swaps the elements with indexes i and j.
func (s SortableEntries) Swap(i int, j int) {
s[i], s[j] = s[j], s[i]
}
func (m *Entry) GetTxID() ([]byte, error) {
switch v := m.Kind.(type) {
case *Entry_Stellar:
var env xdr.TransactionEnvelope
if _, err := xdr.Unmarshal(bytes.NewReader(v.Stellar.EnvelopeXdr), &env); err != nil {
return nil, errors.Wrap(err, "failed to parse envelope xdr")
}
hash, err := network.HashTransaction(&env.Tx, v.Stellar.NetworkPassphrase)
return hash[:], err
case *Entry_Solana:
var txn solana.Transaction
if err := txn.Unmarshal(v.Solana.Transaction); err != nil {
return nil, errors.Wrap(err, "failed to parse solana transaction")
}
return txn.Signature(), nil
default:
return nil, errors.Errorf("unsupported entry version: %d", m.Version)
}
}
func (m *Entry) GetAccounts() ([]string, error) {
switch v := m.Kind.(type) {
case *Entry_Stellar:
var env xdr.TransactionEnvelope
if _, err := xdr.Unmarshal(bytes.NewReader(v.Stellar.EnvelopeXdr), &env); err != nil {
return nil, errors.Wrap(err, "failed to parse envelope xdr")
}
accounts, err := GetAccountsFromEnvelope(env)
if err != nil {
return nil, err
}
accountIDs := make([]string, 0, len(accounts))
for k := range accounts {
accountIDs = append(accountIDs, k)
}
return accountIDs, nil
case *Entry_Solana:
var txn solana.Transaction
if err := txn.Unmarshal(v.Solana.Transaction); err != nil {
return nil, errors.Wrap(err, "failed to parse solana transaction")
}
accounts, err := GetAccountsFromTransaction(txn)
if err != nil {
return nil, err
}
accountIDs := make([]string, 0, len(accounts))
for k := range accounts {
accountIDs = append(accountIDs, k)
}
return accountIDs, nil
default:
return nil, errors.Errorf("unsupported entry version: %d", m.Version)
}
}
func (m *Entry) GetOrderingKey() ([]byte, error) {
switch v := m.Kind.(type) {
case *Entry_Stellar:
var b [9]byte
b[0] = byte(m.Version)
binary.BigEndian.PutUint64(b[1:], v.Stellar.PagingToken)
return b[:], nil
case *Entry_Solana:
h, err := m.GetTxID()
if err != nil {
return nil, err
}
var b [1 + 8 + 8]byte
b[0] = byte(m.Version)
binary.BigEndian.PutUint64(b[1:], v.Solana.Slot)
copy(b[9:], h[:8])
return b[:], nil
default:
return nil, errors.Errorf("unsupported entry version: %d", m.Version)
}
}
func OrderingKeyFromCursor(v KinVersion, cursor string) ([]byte, error) {
pt, err := strconv.ParseUint(cursor, 10, 64)
if err != nil {
return nil, errors.Wrap(err, "cursor is not in toid format")
}
var b [9]byte
b[0] = byte(v)
binary.BigEndian.PutUint64(b[1:], pt)
return b[:], nil
}
func OrderingKeyFromBlock(block uint64, max bool) []byte {
var b [1 + 8 + 8]byte
b[0] = byte(KinVersion_KIN4)
binary.BigEndian.PutUint64(b[1:], block)
if max {
binary.BigEndian.PutUint64(b[9:], math.MaxUint64)
}
return b[:]
}
func BlockFromOrderingKey(k []byte) (uint64, error) {
if len(k) != 1+8+8 {
return 0, errors.Errorf("invalid ordering key size: %d", len(k))
}
return binary.BigEndian.Uint64(k[1:]), nil
}
// GetAccountsFromEnvelope returns the set of accounts involved in a transaction
// contained within a transaction envelope.
func GetAccountsFromEnvelope(env xdr.TransactionEnvelope) (map[string]struct{}, error) {
// Gather the list of all 'associated' accounts in the transaction.
idSet := make(map[string]struct{})
addr, err := env.Tx.SourceAccount.GetAddress()
if err != nil {
return nil, errors.Wrap(err, "failed to get source addr")
}
idSet[addr] = struct{}{}
for _, op := range env.Tx.Operations {
if op.SourceAccount != nil {
addr, err := op.SourceAccount.GetAddress()
if err != nil {
return nil, errors.Wrap(err, "failed to get op source addr")
}
idSet[addr] = struct{}{}
}
switch op.Body.Type {
case xdr.OperationTypePayment:
if p, ok := op.Body.GetPaymentOp(); ok {
addr, err := p.Destination.GetAddress()
if err != nil {
return nil, errors.Wrap(err, "failed to get source addr")
}
idSet[addr] = struct{}{}
}
case xdr.OperationTypeCreateAccount:
if c, ok := op.Body.GetCreateAccountOp(); ok {
addr, err := c.Destination.GetAddress()
if err != nil {
return nil, errors.Wrap(err, "failed to get source addr")
}
idSet[addr] = struct{}{}
}
case xdr.OperationTypeAccountMerge:
if d, ok := op.Body.GetDestination(); ok {
addr, err := d.GetAddress()
if err != nil {
return nil, errors.Wrap(err, "failed to get source addr")
}
idSet[addr] = struct{}{}
}
case xdr.OperationTypeChangeTrust:
// No action; op has no account
default:
logrus.StandardLogger().WithFields(logrus.Fields{
"type": "transaction/history/ingestion/stellar",
"method": "GetAccountsFromEnvelope",
}).Warn("Unsupported transaction type, unable to get relevant accounts")
}
}
return idSet, nil
}
func GetAccountsFromTransaction(txn solana.Transaction) (map[string]struct{}, error) {
idSet := make(map[string]struct{})
for i := range txn.Message.Instructions {
transfer, err := token.DecompileTransferAccount(txn.Message, i)
if err == solana.ErrIncorrectProgram || err == solana.ErrIncorrectInstruction {
continue
} else if err != nil {
return nil, errors.Wrap(err, "failed to decompile transfer instruction")
}
idSet[accountFromRaw(transfer.Source)] = struct{}{}
idSet[accountFromRaw(transfer.Destination)] = struct{}{}
}
return idSet, nil
}
func accountFromRaw(raw ed25519.PublicKey) string {
return strkey.MustEncode(strkey.VersionByteAccountID, raw)
}