-
Notifications
You must be signed in to change notification settings - Fork 4
/
consolecmd.go
141 lines (128 loc) · 3.9 KB
/
consolecmd.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 main
import (
"fmt"
"github.com/LemoFoundationLtd/lemochain-core/common/crypto"
"github.com/LemoFoundationLtd/lemochain-core/common/log"
"github.com/LemoFoundationLtd/lemochain-core/main/console"
"github.com/LemoFoundationLtd/lemochain-core/network/rpc"
"gopkg.in/urfave/cli.v1"
)
var (
consoleCommand = cli.Command{
Action: localConsole,
Name: "console",
Usage: "Start an interactive JavaScript environment",
Flags: append(nodeFlags, rpcFlags...),
Category: "CONSOLE COMMANDS",
Description: `
The Glemo console is an interactive shell for the JavaScript runtime environment
which exposes a node admin interface as well as the Dapp JavaScript API.`,
}
attachCommand = cli.Command{
Action: remoteConsole,
Name: "attach",
Usage: "attach",
ArgsUsage: "[endpoint]",
Flags: attachFlags,
Category: "CONSOLE COMMANDS",
Description: `
The Glemo console is an interactive shell for the JavaScript runtime environment
which exposes a node admin interface as well as the Dapp JavaScript API.
This command allows to open a console on a running glemo node.`,
}
createaccountCommand = cli.Command{
Action: createAccount,
Name: "createaccount",
Usage: "createaccount",
ArgsUsage: "[endpoint]",
Flags: createaccountFlags,
Category: "BLOCKCHAIN COMMANDS",
Description: `
Create an account when we execute "./glemo createaccount".`,
}
createanodekeyCommand = cli.Command{
Action: createNodekey,
Name: "createnodekey",
Usage: "createnodekey",
ArgsUsage: "[endpoint]",
Flags: createnodekeyFlags,
Category: "BLOCKCHAIN COMMANDS",
Description: `
Create nodekey and nodeID when we execute "./glemo createnodekey".`,
}
)
// createAccount
func createAccount(ctx *cli.Context) error {
acc, err := crypto.GenerateAddress()
if err == nil {
fmt.Println("Please keep your account safe! \nPlease apply again if the private key is divulged!\n ")
fmt.Printf("Private:\n%s\n", acc.Private)
fmt.Printf("LemoAddress:\n%s", acc.Address.String())
fmt.Println("\n")
return nil
} else {
fmt.Println("Create account error:", err.Error())
fmt.Println("Suggest to retry!!!")
return nil
}
}
// createNodekey create nodekey and nodeID
func createNodekey(ctx *cli.Context) error {
acc, err := crypto.GenerateAddress()
if err == nil {
nodekey := acc.Private[2:]
nodeID := acc.Public[2:]
fmt.Println("Please save the generated nodekey and nodeID !!!")
fmt.Printf("nodekey:\n%s\n", nodekey)
fmt.Printf("nodeID:\n%s\n", nodeID)
fmt.Println("\n")
return nil
} else {
fmt.Println("Create nodekey error:", err.Error())
fmt.Println("Suggest to retry!!!")
return nil
}
}
func localConsole(ctx *cli.Context) error {
n := makeFullNode(ctx)
startNode(ctx, n)
defer n.Stop()
client, err := n.Attach()
if err != nil {
log.Critf("Failed to attach to the inproc glemo: %v", err)
}
startConsole(client, n.ChainID())
return nil
}
func remoteConsole(ctx *cli.Context) error {
// Attach to a remotely running glemo instance and start the JavaScript console
endpoint := ctx.Args().First()
if endpoint == "" {
log.Critf("Unable to attach to remote glemo: no ipc path")
}
client, err := rpc.Dial(endpoint)
if err != nil {
log.Critf("Unable to attach to remote glemo: %v", err)
}
var chainID uint16
if err := client.Call(&chainID, "chain_chainID"); err != nil {
log.Critf("Unable to call remote glemo: %v", err)
}
startConsole(client, chainID)
return nil
}
func startConsole(client *rpc.Client, chainID uint16) {
config := console.Config{
DocRoot: "scripts", // consoleObj.Execute("exec.js") will execute the file "js/exec.js"
Client: client,
ChainID: chainID,
}
consoleObj, err := console.New(config)
if err != nil {
log.Critf("Failed to start the JavaScript console: %v", err)
}
defer consoleObj.Stop(false)
// Otherwise print the welcome screen and enter interactive mode
consoleObj.Welcome()
consoleObj.Interactive()
}