-
Notifications
You must be signed in to change notification settings - Fork 82
/
addr_converter.go
117 lines (98 loc) · 2.65 KB
/
addr_converter.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
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"github.com/btcsuite/btcutil/bech32"
"github.com/spf13/cobra"
)
func AddressConverter() *cobra.Command {
cmd := &cobra.Command{
Use: "addr-converter [input-file] [output-file] [prefix]",
Short: "Converting the bech32 address into prefix account address.",
Args: cobra.ExactArgs(3),
Example: `
go run . addr-converter input.csv output_address.csv pasg
`,
RunE: func(cmd *cobra.Command, args []string) error {
input_file := args[0]
output_file := args[1]
prefix := args[2]
fmt.Println(input_file, output_file, prefix)
if err := AddressConvert(input_file, output_file, prefix); err != nil {
return err
}
fmt.Println(fmt.Sprintf("Successfully prefix %s address conversion is done.", prefix))
return nil
},
}
return cmd
}
func AddressConvert(input_file, output_file, prefix string) error {
var data [][]string
data, err := readCsvFile(input_file)
if err != nil {
panic(err)
}
airdropRecords, err := parseRecords(prefix, data[1:])
if err != nil {
panic(err)
}
var airdropAccountRecords []Passage3DAirdropClaimRecord
for _, airdropRecord := range airdropRecords {
re := Passage3DAirdropClaimRecord{
ClaimAmount: airdropRecord.AirdropAmount,
Passage3DAddress: airdropRecord.NewPrefixAddress,
}
airdropAccountRecords = append(airdropAccountRecords, re)
}
writeToCsvFile(output_file, airdropAccountRecords)
return nil
}
type AirdropRecord struct {
OldAddress string
NewPrefixAddress string
AirdropAmount string
}
type Passage3DAirdropClaimRecord struct {
Passage3DAddress string
ClaimAmount string
}
func writeToCsvFile(output_file string, claimRecords []Passage3DAirdropClaimRecord) {
csvFile, err := os.Create(output_file)
if err != nil {
log.Fatalf("failed creating file: %s", err)
}
csvwriter := csv.NewWriter(csvFile)
for _, empRow := range claimRecords {
_ = csvwriter.Write([]string{empRow.Passage3DAddress, empRow.ClaimAmount})
}
csvwriter.Flush()
csvFile.Close()
}
func parseRecords(prefix string, records [][]string) ([]AirdropRecord, error) {
var airdropAccounts []AirdropRecord
var count = 0
for _, record := range records {
// bech32 decode
_, hrf, err := bech32.Decode(record[0])
if err != nil {
return nil, err
}
aidropAmount := record[1]
newAccAddr, err := bech32.Encode(prefix, hrf)
if err != nil {
return nil, err
}
// encode with prefix
airdropAccounts = append(airdropAccounts, AirdropRecord{
OldAddress: record[0],
NewPrefixAddress: newAccAddr,
AirdropAmount: aidropAmount,
})
count++
}
fmt.Println("total converted", count)
return airdropAccounts, nil
}