-
Notifications
You must be signed in to change notification settings - Fork 20
/
npm_module.go
231 lines (201 loc) · 6.59 KB
/
npm_module.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
package overflow
import (
"fmt"
"strings"
"github.com/onflow/cadence/ast"
"github.com/onflow/cadence/parser"
"github.com/onflow/cadence/sema"
)
// NPM Module
//
// Overflow has support for generating an NPM module from a set of interactions
// a type representing the raw solutions that contains all transactions, scripts, networks and warnings of any
type OverflowSolution struct {
// all transactions with name and what paremters they have
Transactions map[string]*OverflowDeclarationInfo `json:"transactions"`
// all scripts with name and parameter they have
Scripts map[string]*OverflowDeclarationInfo `json:"scripts"`
// all networks with associated scripts/tranasctions/contracts preresolved
Networks map[string]*OverflowSolutionNetwork `json:"networks"`
// warnings accumulated during parsing
Warnings []string `json:"warnings"`
}
type OverflowAuthorizers [][]string
// a type containing information about parameter types and orders
type OverflowDeclarationInfo struct {
Parameters map[string]string `json:"parameters"`
Authorizers OverflowAuthorizers `json:"-"`
ParameterOrder []string `json:"order"`
}
// a type representing one network in a solution, so mainnet/testnet/emulator
type OverflowSolutionNetwork struct {
Scripts map[string]string `json:"scripts"`
Transactions map[string]string `json:"transactions,omitempty"`
Contracts *map[string]string `json:"contracts,omitempty"`
}
// a type representing a merged solution that will be serialized as the json file for the npm module
type OverflowSolutionMerged struct {
Networks map[string]OverflowSolutionMergedNetwork `json:"networks"`
}
// a network in the merged solution
type OverflowSolutionMergedNetwork struct {
Scripts map[string]OverflowCodeWithSpec `json:"scripts"`
Transactions map[string]OverflowCodeWithSpec `json:"transactions,omitempty"`
Contracts *map[string]string `json:"contracts,omitempty"`
}
// representing code with specification if parameters
type OverflowCodeWithSpec struct {
Spec *OverflowDeclarationInfo `json:"spec"`
Code string `json:"code"`
}
// merge the given Solution into a MergedSolution that is suited for exposing as an NPM module
func (s *OverflowSolution) MergeSpecAndCode() *OverflowSolutionMerged {
networks := map[string]OverflowSolutionMergedNetwork{}
networkNames := []string{}
for name := range s.Networks {
networkNames = append(networkNames, name)
}
for name, network := range s.Networks {
scripts := map[string]OverflowCodeWithSpec{}
for rawScriptName, code := range network.Scripts {
scriptName := rawScriptName
valid := true
for _, networkName := range networkNames {
overwriteNetworkScriptName := fmt.Sprintf("%s%s", networkName, scriptName)
_, ok := s.Scripts[overwriteNetworkScriptName]
if ok {
if networkName == name {
valid = false
break
}
}
if strings.HasPrefix(scriptName, networkName) {
if networkName == name {
scriptName = strings.TrimPrefix(scriptName, networkName)
valid = true
break
} else {
valid = false
break
}
}
}
if valid {
scripts[scriptName] = OverflowCodeWithSpec{
Code: formatCode(code),
Spec: s.Scripts[rawScriptName],
}
}
}
transactions := map[string]OverflowCodeWithSpec{}
for rawTxName, code := range network.Transactions {
txName := rawTxName
txValid := true
for _, networkName := range networkNames {
overwriteNetworkTxName := fmt.Sprintf("%s%s", networkName, txName)
_, ok := s.Transactions[overwriteNetworkTxName]
if ok {
if networkName == name {
txValid = false
break
}
}
if strings.HasPrefix(txName, networkName) {
if networkName == name {
txName = strings.TrimPrefix(txName, networkName)
txValid = true
break
} else {
txValid = false
break
}
}
}
if txValid {
transactions[txName] = OverflowCodeWithSpec{
Code: formatCode(code),
Spec: s.Transactions[rawTxName],
}
}
}
networks[name] = OverflowSolutionMergedNetwork{
Contracts: network.Contracts,
Scripts: scripts,
Transactions: transactions,
}
}
return &OverflowSolutionMerged{Networks: networks}
}
func declarationInfo(code []byte) *OverflowDeclarationInfo {
params, authorizerTypes := paramsAndAuthorizers(code)
if params == nil {
return &OverflowDeclarationInfo{
ParameterOrder: []string{},
Parameters: map[string]string{},
Authorizers: authorizerTypes,
}
}
parametersMap := make(map[string]string, len(params.Parameters))
var parameterList []string
for _, parameter := range params.Parameters {
parametersMap[parameter.Identifier.Identifier] = parameter.TypeAnnotation.Type.String()
parameterList = append(parameterList, parameter.Identifier.Identifier)
}
if len(parameterList) == 0 {
return &OverflowDeclarationInfo{
ParameterOrder: []string{},
Parameters: map[string]string{},
Authorizers: authorizerTypes,
}
}
return &OverflowDeclarationInfo{
ParameterOrder: parameterList,
Parameters: parametersMap,
Authorizers: authorizerTypes,
}
}
func paramsAndAuthorizers(code []byte) (*ast.ParameterList, OverflowAuthorizers) {
program, err := parser.ParseProgram(nil, code, parser.Config{})
if err != nil {
return nil, nil
}
authorizers := OverflowAuthorizers{}
// if we have any transtion declaration then return it
for _, txd := range program.TransactionDeclarations() {
if txd.Prepare != nil {
prepareParams := txd.Prepare.FunctionDeclaration.ParameterList
if prepareParams != nil {
for _, parg := range txd.Prepare.FunctionDeclaration.ParameterList.ParametersByIdentifier() {
// name := parg.Identifier.Identifier
ta := parg.TypeAnnotation
if ta != nil {
rt, ok := ta.Type.(*ast.ReferenceType)
if ok {
entitlements := []string{}
switch authorization := rt.Authorization.(type) {
case ast.EntitlementSet:
for _, entitlement := range authorization.Entitlements() {
entitlements = append(entitlements, entitlement.Identifier.Identifier)
}
}
authorizers = append(authorizers, entitlements)
} else {
authorizers = append(authorizers, []string{})
}
}
}
}
}
return txd.ParameterList, authorizers
}
functionDeclaration := sema.FunctionEntryPointDeclaration(program)
if functionDeclaration != nil {
if functionDeclaration.ParameterList != nil {
return functionDeclaration.ParameterList, nil
}
}
return nil, nil
}
func formatCode(input string) string {
return strings.ReplaceAll(strings.TrimSpace(input), "\t", " ")
}