forked from eoscanada/eosc
/
toolsNames.go
148 lines (122 loc) · 3.55 KB
/
toolsNames.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
package cmd
import (
"encoding/binary"
"encoding/hex"
"fmt"
"math"
"os"
"regexp"
"strconv"
"strings"
"github.com/ryanuber/columnize"
eos "github.com/sebastianmontero/eos-go"
"github.com/spf13/cobra"
)
var toolsNamesCmd = &cobra.Command{
Use: "names value [value ...]",
Short: "Convert value(s) to and from name-encoded strings",
Long: `EOS name encoding creates strings or up to 12 characters out of uint64 values.
This command auto-detects encoding and converts it to different encodings.
`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
showHeader := len(args) > 1
if showHeader {
// Add a starting blank line just like the command with 1 argument
fmt.Println()
}
for _, input := range args {
if showHeader {
fmt.Printf(" # %s", input)
}
printName(input)
}
},
}
func printName(input string) {
showFrom := map[string]uint64{}
baseHex, err := hex.DecodeString(input)
if err == nil {
if len(baseHex) == 8 {
showFrom["hex"] = binary.LittleEndian.Uint64(baseHex)
showFrom["hex_be"] = binary.BigEndian.Uint64(baseHex)
} else if len(baseHex) == 4 {
showFrom["hex"] = uint64(binary.LittleEndian.Uint32(baseHex))
showFrom["hex_be"] = uint64(binary.BigEndian.Uint32(baseHex))
}
}
fromSymbol, err := eos.StringToSymbol(input)
if err == nil {
symbolUint, err := fromSymbol.ToUint64()
if err == nil {
showFrom["symbol"] = symbolUint
}
}
fromSymbolCode, err := eos.StringToSymbolCode(input)
if err == nil {
showFrom["symbol_code"] = uint64(fromSymbolCode)
}
fromName, err := eos.StringToName(input)
if err == nil {
showFrom["name"] = fromName
}
fromUint64, err := strconv.ParseUint(input, 10, 64)
if err == nil {
showFrom["uint64"] = fromUint64
}
someFound := false
rows := []string{"| from \\ to | hex | hex_be | hex_rev_u32 | name | uint64 | symbol | symbol_code", "| --------- | --- | ------ | ----------- | ---- | ------ | ------ | ----------- |"}
for _, from := range []string{"hex", "hex_be", "name", "uint64", "symbol", "symbol_code"} {
val, found := showFrom[from]
if !found {
continue
}
someFound = true
row := []string{from}
for _, to := range []string{"hex", "hex_be", "hex_rev_u32", "name", "uint64", "symbol", "symbol_code"} {
cnt := make([]byte, 8)
switch to {
case "hex":
binary.LittleEndian.PutUint64(cnt, val)
row = append(row, hex.EncodeToString(cnt))
case "hex_be":
binary.BigEndian.PutUint64(cnt, val)
row = append(row, hex.EncodeToString(cnt))
case "hex_rev_u32":
if val > math.MaxUint32 {
row = append(row, "-")
} else {
cnt4 := make([]byte, 4)
binary.BigEndian.PutUint32(cnt4, math.MaxUint32-uint32(val))
row = append(row, hex.EncodeToString(cnt4))
}
case "name":
row = append(row, eos.NameToString(val))
case "uint64":
row = append(row, strconv.FormatUint(val, 10))
case "symbol":
row = append(row, symbOrDash(fmt.Sprintf("%d,%s", uint8(val&0xFF), eos.SymbolCode(val>>8).String())))
case "symbol_code":
row = append(row, symbOrDash(eos.SymbolCode(val).String()))
}
}
rows = append(rows, "| "+strings.Join(row, " | ")+" |")
}
if !someFound {
fmt.Printf("Couldn't decode %q with any of these methods: hex, hex_be, name, uint64\n", input)
os.Exit(1)
}
fmt.Println("")
fmt.Println(columnize.SimpleFormat(rows))
fmt.Println("")
}
var symbOrDashRE = regexp.MustCompile(`^[0-9A-Z,]+$`)
func symbOrDash(input string) string {
if !symbOrDashRE.MatchString(input) {
return "-"
}
return input
}
func init() {
toolsCmd.AddCommand(toolsNamesCmd)
}