-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.go
113 lines (98 loc) · 2.15 KB
/
cli.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
package main
import (
"./lib"
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/rpc/client"
)
var (
cli = client.NewHTTP("http://localhost:26657", "/websocket")
)
func main() {
rootCmd := &cobra.Command{
Use: "cli",
}
walletCmd := &cobra.Command{
Use: "init-wallet",
Run: func(cmd *cobra.Command, args []string) { initWallet() },
}
issueCmd := &cobra.Command{
Use: "issue-tx",
Run: func(cmd *cobra.Command, args []string) { issue() },
}
transferCmd := &cobra.Command{
Use: "transfer-tx",
Run: func(cmd *cobra.Command, args []string) { transfer() },
}
queryCmd := &cobra.Command{
Use: "query",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("query who?")
}
label := args[0]
query(label)
return nil
},
}
rootCmd.AddCommand(walletCmd)
rootCmd.AddCommand(issueCmd)
rootCmd.AddCommand(transferCmd)
rootCmd.AddCommand(queryCmd)
rootCmd.Execute()
//initWallet()
//issue()
//transfer()
//query("michael")
//query("britney")
}
func issue() {
wallet := lib.LoadWallet("./wallet")
tx := lib.NewTx(lib.NewIssuePayload(
wallet.GetAddress("issuer"),
wallet.GetAddress("michael"),
1000))
tx.Sign(wallet.GetPrivKey("issuer"))
bz, err := lib.MarshalBinary(tx)
if err != nil {
panic(err)
}
ret, err := cli.BroadcastTxCommit(bz)
if err != nil {
panic(err)
}
fmt.Printf("issue ret => %+v\n", ret)
}
func transfer() {
wallet := lib.LoadWallet("./wallet")
tx := lib.NewTx(lib.NewTransferPayload(
wallet.GetAddress("michael"),
wallet.GetAddress("britney"),
100))
tx.Sign(wallet.GetPrivKey("michael"))
bz, err := lib.MarshalBinary(tx)
if err != nil {
panic(err)
}
ret, err := cli.BroadcastTxCommit(bz)
if err != nil {
panic(err)
}
fmt.Printf("issue ret => %+v\n", ret)
}
func query(label string) {
wallet := lib.LoadWallet("./wallet")
ret, err := cli.ABCIQuery("", wallet.GetAddress(label))
if err != nil {
panic(err)
}
fmt.Printf("ret => %+v\n", ret.Response)
}
func initWallet() {
wallet := lib.NewWallet()
wallet.GenPrivKey("issuer")
wallet.GenPrivKey("michael")
wallet.GenPrivKey("britney")
wallet.Save("./wallet")
}