-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
553 lines (491 loc) · 15.6 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package main
import (
"context"
"cpm/generators"
"cpm/generators/csharp"
"cpm/generators/golang"
"cpm/generators/java"
"cpm/generators/python"
"cpm/generators/typescript"
_ "embed"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var (
LANG_GO = "go"
LANG_PYTHON = "python"
LANG_JAVA = "java"
LANG_CSHARP = "csharp"
LANG_TYPESCRIPT = "ts"
LOG_INFO = "INFO"
LOG_DEBUG = "DEBUG"
DEFAULT_CONFIG_FILE = "cpm.yaml"
version = "dev"
)
var GenerateCommandHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}
USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}language [language options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}}
DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}
LANGUAGES:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
`
func main() {
log.SetOutput(os.Stdout)
app := &cli.App{
Usage: "Contract Package Manager",
Flags: []cli.Flag{
&cli.GenericFlag{
Name: "log-level",
Usage: "Log output level",
Required: false,
Value: &EnumValue{
Enum: []string{LOG_INFO, LOG_DEBUG},
},
},
},
Before: beforeAction,
Action: func(cCtx *cli.Context) error {
if cCtx.NArg() == 0 {
cli.ShowAppHelpAndExit(cCtx, 0)
}
return nil
},
Commands: []*cli.Command{
{
Name: "init",
Usage: "Create a new cpm.yaml config file",
Action: handleCliInit,
},
{
Name: "run",
Usage: "Download all contracts from cpm.yaml and generate SDKs where specified",
Action: handleCliRun,
},
{
Name: "download",
Usage: "Download contract or manifest",
Subcommands: []*cli.Command{
{
Name: "contract",
Usage: "Download a single contract",
Flags: []cli.Flag{
&cli.StringFlag{Name: "c", Usage: "Contract script hash", Required: true},
&cli.StringFlag{Name: "n", Usage: "Source network label. Searches cpm.yaml for the network by label to find the host", Required: false},
&cli.StringFlag{Name: "N", Usage: "Source network host", Required: false},
&cli.StringFlag{Name: "i", Usage: "Neo express config file", Required: false, DefaultText: "default.neo-express"},
&cli.BoolFlag{Name: "s", Usage: "Save contract to the 'contracts' section of cpm.yaml", Required: false, Value: false, DisableDefaultText: true},
},
Action: handleCliDownloadContract,
},
{
Name: "manifest",
Usage: "Download the contract manifest",
Flags: []cli.Flag{
&cli.StringFlag{Name: "c", Usage: "Contract script hash", Required: true},
&cli.StringFlag{Name: "n", Usage: "Source network label. Searches cpm.yaml for the network by label to find the host", Required: false},
&cli.StringFlag{Name: "N", Usage: "Source network host", Required: false},
&cli.BoolFlag{Name: "s", Usage: "Save contract to the 'contracts' section of cpm.yaml", Required: false, Value: true, DisableDefaultText: true},
},
Action: handleCliDownloadManifest,
},
},
},
{
Name: "generate",
Usage: "Generate SDK from manifest",
CustomHelpTemplate: GenerateCommandHelpTemplate,
Subcommands: []*cli.Command{
{
Name: LANG_GO,
Usage: "Generate a SDK for use with Golang",
Action: func(c *cli.Context) error {
return handleCliGenerate(c, LANG_GO)
},
Flags: []cli.Flag{
&cli.StringFlag{Name: "m", Usage: "Path to contract manifest.json", Required: true},
&cli.StringFlag{Name: "c", Usage: "Contract script hash if known", Required: false},
&cli.StringFlag{Name: "o", Usage: "Output folder", Required: false},
&cli.GenericFlag{
Name: "t",
Usage: "SDK type",
Required: true,
Value: &EnumValue{
Enum: []string{generators.SDKOffChain, generators.SDKOnChain},
},
},
},
},
{
Name: LANG_PYTHON,
Usage: "Generate a SDK for use with Python",
Action: func(c *cli.Context) error {
return handleCliGenerate(c, LANG_PYTHON)
},
Flags: []cli.Flag{
&cli.StringFlag{Name: "m", Usage: "Path to contract manifest.json", Required: true},
&cli.StringFlag{Name: "c", Usage: "Contract script hash if known", Required: false},
&cli.StringFlag{Name: "o", Usage: "Output folder", Required: false},
&cli.GenericFlag{
Name: "t",
Usage: "SDK type",
Required: true,
Value: &EnumValue{
Enum: []string{generators.SDKOffChain, generators.SDKOnChain},
},
},
},
},
{
Name: LANG_JAVA,
Usage: "Generate a SDK for use with Java",
Action: func(c *cli.Context) error {
return handleCliGenerate(c, LANG_JAVA)
},
Flags: []cli.Flag{
&cli.StringFlag{Name: "m", Usage: "Path to contract manifest.json", Required: true},
&cli.StringFlag{Name: "c", Usage: "Contract script hash if known", Required: false},
&cli.StringFlag{Name: "o", Usage: "Output folder", Required: false},
&cli.GenericFlag{
Name: "t",
Usage: "SDK type",
Required: true,
Value: &EnumValue{
Enum: []string{generators.SDKOffChain, generators.SDKOnChain},
},
},
},
},
{
Name: LANG_CSHARP,
Usage: "Generate an on-chain SDK for use with C#",
Action: func(c *cli.Context) error {
return handleCliGenerate(c, LANG_CSHARP)
},
Flags: []cli.Flag{
&cli.StringFlag{Name: "m", Usage: "Path to contract manifest.json", Required: true},
&cli.StringFlag{Name: "c", Usage: "Contract script hash if known", Required: false},
&cli.StringFlag{Name: "o", Usage: "Output folder", Required: false},
},
},
{
Name: LANG_TYPESCRIPT,
Usage: "Generate an off-chain SDK for use with TypeScript",
Action: func(c *cli.Context) error {
return handleCliGenerate(c, LANG_TYPESCRIPT)
},
Flags: []cli.Flag{
&cli.StringFlag{Name: "m", Usage: "Path to contract manifest.json", Required: true},
&cli.StringFlag{Name: "c", Usage: "Contract script hash if known", Required: false},
&cli.StringFlag{Name: "o", Usage: "Output folder", Required: false},
},
},
},
},
{
Name: "version",
Usage: "Shows CPM version",
Action: handleCliVersion,
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func beforeAction(cCtx *cli.Context) error {
if cCtx.String("log-level") == LOG_DEBUG {
log.SetLevel(log.DebugLevel)
}
return nil
}
func handleCliInit(*cli.Context) error {
CreateDefaultConfig()
return nil
}
func handleCliRun(cCtx *cli.Context) error {
LoadConfig()
var downloader Downloader
// for now we only support NeoExpress
downloader = NewNeoExpressDownloader(cfg.Tools.NeoExpress.ConfigPath)
for _, c := range cfg.Contracts {
log.Infof("Processing contract '%s' (%s)", c.Label, c.ScriptHash.StringLE())
hosts := cfg.getHosts(*c.SourceNetwork)
if *c.Download {
downloadSuccess := false
for _, host := range hosts {
log.Debugf("Attempting to download contract '%s' (%s) using NEOXP from network %s", c.Label, c.ScriptHash.StringLE(), host)
message, err := downloader.downloadContract(c.ScriptHash, host)
if err == nil {
log.Info(message)
downloadSuccess = true
break
}
// just log the error we got from the downloader and try the next host
log.Debug(message)
}
if !downloadSuccess {
log.Fatalf("Failed to download contract '%s' (%s). Use '--log-level DEBUG' for more information", c.Label, c.ScriptHash)
}
} else {
log.Debugf("Skipping contract download")
}
if *c.GenerateSdk {
generateSuccess := false
for _, host := range hosts {
err := fetchManifestAndGenerateSDK(&c, host)
if err == nil {
generateSuccess = true
break
}
log.Debug(err)
}
if !generateSuccess {
log.Fatalf("Failed to generate SDK for contract '%s' (%s). Use '--log-level DEBUG' for more information", c.Label, c.ScriptHash)
}
} else {
log.Debugf("Skipping SDK generation")
}
}
return nil
}
func handleCliDownloadContract(cCtx *cli.Context) error {
networkLabel := cCtx.String("n")
networkHost := cCtx.String("N")
contractHash := cCtx.String("c")
configPath := cCtx.String("i")
saveContract := cCtx.Bool("s")
LoadConfig()
hosts, err := getHosts(networkLabel, networkHost)
if err != nil {
return err
}
// for now, we only support NeoExpress
downloader := NewNeoExpressDownloader(configPath)
return downloadContract(hosts, contractHash, downloader, saveContract, false)
}
func handleCliDownloadManifest(cCtx *cli.Context) error {
networkLabel := cCtx.String("n")
networkHost := cCtx.String("N")
contractHash := cCtx.String("c")
saveContract := cCtx.Bool("s")
LoadConfig()
hosts, err := getHosts(networkLabel, networkHost)
if err != nil {
return err
}
return downloadManifest(hosts, contractHash, saveContract, false)
}
func handleCliGenerate(cCtx *cli.Context, language string) error {
m, _, err := readManifest(cCtx.String("m"))
if err != nil {
log.Fatalf("can't read contract manifest: %s", err)
}
sdkType := cCtx.String("t")
if sdkType == "" {
if language == LANG_TYPESCRIPT {
sdkType = generators.SDKOffChain
} else {
sdkType = generators.SDKOnChain
}
}
dest := cCtx.String("o")
if dest == "" {
dest = cfg.getSdkDestination(language, sdkType)
} else {
dest = EnsureSuffix(dest)
}
scriptHash := util.Uint160{}
scriptHashStr := cCtx.String("c")
if scriptHashStr != "" {
scriptHash, err = util.Uint160DecodeStringLE(strings.TrimPrefix(cCtx.String("c"), "0x"))
if err != nil {
log.Fatalf("failed to convert script hash: %v", err)
}
}
return generateSDK(&generators.GenerateCfg{Manifest: m, ContractHash: scriptHash, SdkDestination: dest}, language, sdkType)
}
func handleCliVersion(cCtx *cli.Context) error {
fmt.Printf("cpm %s\n", version)
return nil
}
// 'cfg' is expected to be initialized
func downloadContract(hosts []string, contractHash string, downloader Downloader, saveContract, testing bool) error {
scriptHash, err := util.Uint160DecodeStringLE(strings.TrimPrefix(contractHash, "0x"))
if err != nil {
return fmt.Errorf("failed to convert script hash: %v", err)
}
if saveContract {
cfg.addContract("unknown", scriptHash)
if !testing {
cfg.saveToDisk()
}
}
success := false
for _, host := range hosts {
message, err := downloader.downloadContract(scriptHash, host)
if err != nil {
// just log the error we got from the downloader and try the next host
log.Debug(message)
} else {
log.Info(message)
success = true
break
}
}
if !success {
return fmt.Errorf("failed to download contract %s. Use '--log-level DEBUG' for more information", scriptHash)
}
return nil
}
func downloadManifest(hosts []string, contractHash string, saveContract, testing bool) error {
scriptHash, err := util.Uint160DecodeStringLE(strings.TrimPrefix(contractHash, "0x"))
if err != nil {
return fmt.Errorf("failed to convert script hash: %v", err)
}
for _, host := range hosts {
m, err := fetchManifest(&scriptHash, host)
if err != nil {
log.Debug(err)
continue
} else {
f, err := os.Create("contract.manifest.json")
if err != nil {
return err
}
out, err := json.MarshalIndent(m, "", " ")
if err != nil {
return err
}
_, err = f.Write(out)
if err != nil {
return err
}
log.Info("Written manifest to contract.manifest.json")
if saveContract {
cfg.addContract(m.Name, scriptHash)
if !testing {
cfg.saveToDisk()
}
}
return nil
}
}
return fmt.Errorf("failed to fetch manifest. Use '--log-level DEBUG' for more information")
}
// must fetch and generate an SDK. Must return an error if generation failed or nothing is generated
func fetchManifestAndGenerateSDK(c *ContractConfig, host string) error {
m, err := fetchManifest(&c.ScriptHash, host)
if err != nil {
return err
}
var onChainLanguages []string = nil
if c.OnChain != nil {
onChainLanguages = c.OnChain.Languages
} else if cfg.Defaults.OnChain != nil {
onChainLanguages = cfg.Defaults.OnChain.Languages
}
if onChainLanguages != nil {
for _, l := range onChainLanguages {
err = generateSDK(&generators.GenerateCfg{Manifest: m, ContractHash: c.ScriptHash, SdkDestination: cfg.getSdkDestination(l, generators.SDKOnChain)}, l, generators.SDKOnChain)
if err != nil {
return err
}
}
}
var offChainLanguages []string = nil
if c.OffChain != nil {
offChainLanguages = c.OffChain.Languages
} else if cfg.Defaults.OffChain != nil {
offChainLanguages = cfg.Defaults.OffChain.Languages
}
if offChainLanguages != nil {
for _, l := range offChainLanguages {
err = generateSDK(&generators.GenerateCfg{Manifest: m, ContractHash: c.ScriptHash, SdkDestination: cfg.getSdkDestination(l, generators.SDKOffChain)}, l, generators.SDKOffChain)
if err != nil {
return err
}
}
}
if onChainLanguages == nil && offChainLanguages == nil {
return fmt.Errorf("nothing to generate. Ensure your 'cpm.yaml' has an 'onchain' or 'offchain' key under " +
"the 'defaults' section or contract specific section with at least one language specified")
}
return nil
}
func fetchManifest(scriptHash *util.Uint160, host string) (*manifest.Manifest, error) {
opts := rpcclient.Options{}
client, err := rpcclient.New(context.TODO(), host, opts)
if err != nil {
return nil, fmt.Errorf("failed to create RPC client: %v", err)
}
err = client.Init()
if err != nil {
return nil, fmt.Errorf("RPCClient init failed with: %v", err)
}
log.Debugf("Attempting to fetch manifest for contract '%s' using %s", scriptHash.StringLE(), host)
state, err := client.GetContractStateByHash(*scriptHash)
if err != nil {
return nil, fmt.Errorf("getcontractstate failed with: %v", err)
}
return &state.Manifest, nil
}
func readManifest(filename string) (*manifest.Manifest, []byte, error) {
if len(filename) == 0 {
return nil, nil, fmt.Errorf("no manifest file was found, specify manifest file with '-m' flag")
}
manifestBytes, err := os.ReadFile(filename)
if err != nil {
return nil, nil, err
}
m := new(manifest.Manifest)
err = json.Unmarshal(manifestBytes, m)
if err != nil {
return nil, nil, err
}
return m, manifestBytes, nil
}
func generateSDK(cfg *generators.GenerateCfg, language, sdkType string) error {
var err error
if language == LANG_PYTHON {
err = python.GenerateSDK(cfg, sdkType)
} else if language == LANG_JAVA {
err = java.GenerateSDK(cfg, sdkType)
} else if language == LANG_CSHARP {
err = csharp.GenerateCsharpSDK(cfg)
} else if language == LANG_GO {
err = golang.GenerateSDK(cfg, sdkType)
} else if language == LANG_TYPESCRIPT {
err = typescript.GenerateTypeScriptSDK(cfg)
} else {
log.Fatalf("language '%s' is unsupported", language)
}
if err != nil {
return err
}
return nil
}
func getHosts(networkLabel, networkHost string) ([]string, error) {
if networkLabel != "" && networkHost != "" {
return nil, fmt.Errorf("-n and -N flags are mutually exclusive")
}
var hosts []string
if len(networkLabel) > 0 {
hosts = cfg.getHosts(networkLabel)
} else if len(networkHost) > 0 {
// TODO: sanity check value
hosts = []string{networkHost}
} else {
return nil, fmt.Errorf("must specify either -n or -N flag")
}
return hosts, nil
}