forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (140 loc) · 3.88 KB
/
main.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
// Package participants contains functions to derive a set of "participant"
// addresses for various data structures in the Stellar network's ledger.
package participants
import (
"fmt"
"github.com/stellar/go/xdr"
)
// ForOperation returns all the participating accounts from the
// provided operation.
func ForOperation(
tx *xdr.Transaction,
op *xdr.Operation,
) (result []xdr.AccountId, err error) {
if op.SourceAccount != nil {
result = append(result, *op.SourceAccount)
} else {
result = append(result, tx.SourceAccount)
}
switch op.Body.Type {
case xdr.OperationTypeCreateAccount:
result = append(result, op.Body.MustCreateAccountOp().Destination)
case xdr.OperationTypePayment:
result = append(result, op.Body.MustPaymentOp().Destination)
case xdr.OperationTypePathPayment:
result = append(result, op.Body.MustPathPaymentOp().Destination)
case xdr.OperationTypeManageOffer:
// the only direct participant is the source_account
case xdr.OperationTypeCreatePassiveOffer:
// the only direct participant is the source_account
case xdr.OperationTypeSetOptions:
// the only direct participant is the source_account
case xdr.OperationTypeChangeTrust:
// the only direct participant is the source_account
case xdr.OperationTypeAllowTrust:
result = append(result, op.Body.MustAllowTrustOp().Trustor)
case xdr.OperationTypeAccountMerge:
result = append(result, op.Body.MustDestination())
case xdr.OperationTypeInflation:
// the only direct participant is the source_account
case xdr.OperationTypeManageData:
// the only direct participant is the source_account
default:
err = fmt.Errorf("Unknown operation type: %s", op.Body.Type)
}
result = dedupe(result)
return
}
// ForTransaction returns all the participating accounts from the provided
// transaction.
func ForTransaction(
tx *xdr.Transaction,
meta *xdr.TransactionMeta,
feeMeta *xdr.LedgerEntryChanges,
) (result []xdr.AccountId, err error) {
result = append(result, tx.SourceAccount)
p, err := forMeta(meta)
if err != nil {
return
}
result = append(result, p...)
p, err = forChanges(feeMeta)
if err != nil {
return
}
result = append(result, p...)
for i := range tx.Operations {
p, err = ForOperation(tx, &tx.Operations[i])
if err != nil {
return
}
result = append(result, p...)
}
result = dedupe(result)
return
}
// dedupe remove any duplicate ids from `in`
func dedupe(in []xdr.AccountId) (out []xdr.AccountId) {
set := map[string]xdr.AccountId{}
for _, id := range in {
set[id.Address()] = id
}
for _, id := range set {
out = append(out, id)
}
return
}
func forChanges(
changes *xdr.LedgerEntryChanges,
) (result []xdr.AccountId, err error) {
for _, c := range *changes {
var account *xdr.AccountId
switch c.Type {
case xdr.LedgerEntryChangeTypeLedgerEntryCreated:
account = forLedgerEntry(c.MustCreated())
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved:
account = forLedgerKey(c.MustRemoved())
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
account = forLedgerEntry(c.MustUpdated())
case xdr.LedgerEntryChangeTypeLedgerEntryState:
account = forLedgerEntry(c.MustState())
default:
err = fmt.Errorf("Unknown change type: %s", c.Type)
return
}
if account != nil {
result = append(result, *account)
}
}
return
}
func forLedgerEntry(le xdr.LedgerEntry) *xdr.AccountId {
if le.Data.Type != xdr.LedgerEntryTypeAccount {
return nil
}
aid := le.Data.MustAccount().AccountId
return &aid
}
func forLedgerKey(lk xdr.LedgerKey) *xdr.AccountId {
if lk.Type != xdr.LedgerEntryTypeAccount {
return nil
}
aid := lk.MustAccount().AccountId
return &aid
}
func forMeta(
meta *xdr.TransactionMeta,
) (result []xdr.AccountId, err error) {
if meta.Operations == nil {
return
}
for _, op := range *meta.Operations {
var acc []xdr.AccountId
acc, err = forChanges(&op.Changes)
if err != nil {
return
}
result = append(result, acc...)
}
return
}