/
main.go
133 lines (114 loc) · 2.51 KB
/
main.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
package main
import (
"bufio"
"encoding/hex"
"flag"
"fmt"
"os"
cid "github.com/ipfs/go-cid"
mcp "github.com/multiformats/go-multicodec-packed"
mh "github.com/multiformats/go-multihash"
)
func fatal(i interface{}) {
fmt.Println(i)
os.Exit(1)
}
func main() {
typ := flag.String("type", "", "type to convert to/from cid")
flag.Parse()
if len(flag.Args()) == 0 {
fatal("must specify 'e' or 'd' for encode or decode")
}
var encode bool
switch flag.Arg(0) {
case "e":
encode = true
if *typ == "" {
fatal("please specify type of input for encoding")
}
case "d":
encode = false
default:
fatal("must specify 'e' or 'd' for encode or decode")
}
scan := bufio.NewScanner(os.Stdin)
for scan.Scan() {
l := scan.Text()
if encode {
out, err := encodeToCid(l, *typ)
if err != nil {
fatal(err)
}
fmt.Println(out)
} else {
t, h, v, err := decodeToInfo(l)
if err != nil {
fatal(err)
}
fmt.Printf("%s\t%s\t%s\n", t, h, v)
}
}
}
func encodeToCid(l string, t string) (string, error) {
switch t {
case "zcash-block":
return encodeBtc(l, cid.ZcashBlock), nil
case "zcash-tx":
return encodeBtc(l, cid.ZcashTx), nil
case "bitcoin-block":
return encodeBtc(l, cid.BitcoinBlock), nil
case "bitcoin-tx":
return encodeBtc(l, cid.BitcoinTx), nil
case "eth-block":
return encodeEth(l, cid.EthBlock)
case "eth-tx":
return encodeEth(l, cid.EthTx)
default:
return "", fmt.Errorf("unrecognized input type: %s", t)
}
}
func encodeEth(l string, mcd uint64) (string, error) {
out, err := hex.DecodeString(l)
if err != nil {
return "", err
}
h, err := mh.Encode(out, mh.KECCAK_256)
if err != nil {
return "", err
}
return cid.NewCidV1(mcd, mh.Multihash(h)).String(), nil
}
func encodeBtc(l string, mcd uint64) string {
out, err := hex.DecodeString(l)
if err != nil {
fatal(err)
}
hval := reverse(out)
h, err := mh.Encode(hval, mh.DBL_SHA2_256)
if err != nil {
fatal(err)
}
c := cid.NewCidV1(mcd, h)
return c.String()
}
func decodeToInfo(l string) (string, string, string, error) {
c, err := cid.Parse(l)
if err != nil {
return "", "", "", err
}
dec, _ := mh.Decode(c.Hash())
cname := mcp.CodeToString(mcp.Code(c.Type()))
raw := c.Hash()[len(c.Hash())-dec.Length:]
switch c.Type() {
case cid.ZcashBlock, cid.ZcashTx, cid.BitcoinBlock, cid.BitcoinTx:
raw = reverse(raw)
}
return cname, dec.Name, hex.EncodeToString(raw), nil
}
func reverse(b []byte) []byte {
out := make([]byte, len(b))
for i, v := range b {
out[len(b)-(1+i)] = v
}
return out
}