-
Notifications
You must be signed in to change notification settings - Fork 31
/
hex_to_bech32.go
98 lines (87 loc) · 2.31 KB
/
hex_to_bech32.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/btcsuite/btcutil/bech32"
"github.com/AlayaNetwork/Alaya-Go/common/bech32util"
"github.com/AlayaNetwork/Alaya-Go/cmd/utils"
"github.com/AlayaNetwork/Alaya-Go/common"
"gopkg.in/urfave/cli.v1"
)
var HexAccountFileFlag = cli.StringFlag{
Name: "hexAddressFile",
Usage: "file bech32/hex accounts want to update to bech32 address,file like [hex,hex...]",
}
type addressPair struct {
Address string
OriginAddress string
Hex string
}
var commandAddressHexToBech32 = cli.Command{
Name: "updateaddress",
Usage: "update hex/bech32 address to bech32 address",
ArgsUsage: "[<address> <address>...]",
Description: `
update hex/bech32 address to bech32 address.
`,
Flags: []cli.Flag{
jsonFlag,
HexAccountFileFlag,
utils.AddressHRPFlag,
},
Action: func(ctx *cli.Context) error {
addressHRP := ctx.String(utils.AddressHRPFlag.Name)
if err := common.SetAddressHRP(addressHRP); err != nil {
return err
}
var accounts []string
if ctx.IsSet(HexAccountFileFlag.Name) {
accountPath := ctx.String(HexAccountFileFlag.Name)
accountjson, err := ioutil.ReadFile(accountPath)
if err != nil {
utils.Fatalf("Failed to read the keyfile at '%s': %v", accountPath, err)
}
if err := json.Unmarshal(accountjson, &accounts); err != nil {
utils.Fatalf("Failed to json decode '%s': %v", accountPath, err)
}
} else {
for _, add := range ctx.Args() {
if add == "" {
utils.Fatalf("the account can't be nil")
}
accounts = append(accounts, add)
}
}
for i, account := range accounts {
_, _, err := bech32.Decode(account)
var out addressPair
var address common.Address
if err != nil {
address = common.HexToAddress(account)
} else {
_, converted, err := bech32util.DecodeAndConvert(account)
if err != nil {
return err
}
address.SetBytes(converted)
}
out = addressPair{
Address: address.String(),
OriginAddress: account,
Hex: address.Hex(),
}
if ctx.Bool(jsonFlag.Name) {
mustPrintJSON(out)
} else {
fmt.Printf("origin: %s\n", out.OriginAddress)
fmt.Printf("bech32: %s\n", out.Address)
fmt.Printf("hex: %s\n", out.Hex)
if i != len(accounts)-1 {
fmt.Println("---")
}
}
}
return nil
},
}