-
Notifications
You must be signed in to change notification settings - Fork 13
/
filecoin.go
219 lines (200 loc) · 4.84 KB
/
filecoin.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package coincodec
import (
"bytes"
"encoding/base32"
"errors"
"strconv"
"github.com/wealdtech/go-slip44"
"golang.org/x/crypto/blake2b"
)
const (
PREFIX byte = 'f'
checksumSize byte = 4
BASE32_ALPHABET_FILECOIN string = "abcdefghijklmnopqrstuvwxyz234567"
)
var (
base32Encoding = base32.NewEncoding(BASE32_ALPHABET_FILECOIN).WithPadding(base32.NoPadding)
)
type Type byte
const (
TypeID = iota
TypeSecp256k1
TypeActor
TypeBls
TypeInvalid
)
func init() {
toBytesMap[slip44.FILECOIN] = FilecoinDecodeToBytes
toStringMap[slip44.FILECOIN] = FilecoinEncodeToString
}
// Attempts to get the type by ASCII.
func parseType(ascii byte) Type {
if ascii >= '0' && ascii <= '3' {
return Type(ascii - '0')
}
return TypeInvalid
}
func typeAscii(typ Type) string {
return string(byte('0') + byte(typ))
}
func getType(raw byte) Type {
switch raw {
case 0:
return TypeID
case 1:
return TypeSecp256k1
case 2:
return TypeActor
case 3:
return TypeBls
default:
return TypeInvalid
}
}
// Returns the payload size (excluding any prefixes) of an address type.
// If the payload size is undefined/variable (e.g. ID)
// or the type is unknown, it returns zero.
func payloadSize(t Type) byte {
switch t {
case TypeSecp256k1:
return 20
case TypeActor:
return 20
case TypeBls:
return 48
}
return 0
}
func isValidID(id string) bool {
if len(id) > 22 {
return false
}
for i := 2; i < len(id); i++ {
if id[i] < '0' || id[i] > '9' {
return false
}
}
_, err := strconv.ParseUint(id[2:], 10, 0)
return err == nil
}
func filecoinComputeChecksum(address []byte) []byte {
hash, _ := blake2b.New(int(checksumSize), nil)
hash.Write(address)
sum := hash.Sum(nil)
return sum
}
func isValidBase32(input string, t Type) error {
// Check if valid Base32.
size := payloadSize(t)
var decoded []byte = make([]byte, size+checksumSize)
_, err := base32Encoding.Decode(decoded, []byte(input[2:]))
if err != nil {
return errors.New("decoding base32 failed")
}
// Check size
if len(decoded) != int(size+checksumSize) {
return errors.New("Invalid size")
}
// Extract raw address
var address []byte = make([]byte, 0)
address = append(address, byte(t))
address = append(address, decoded[:size]...)
// Verify checksum
shouldSum := filecoinComputeChecksum(address)
if bytes.Compare(shouldSum[:], decoded[size:]) != 0 {
return errors.New("Wrong checksum")
}
return nil
}
func isValidString(input string) error {
if len(input) < 3 {
return errors.New("Too short")
}
// Only main net addresses supported.
if input[0] != PREFIX {
return errors.New("Invalid network")
}
// Get address type.
typ := parseType(input[1])
if typ == TypeInvalid {
return errors.New("Invalid type")
}
// ID addresses are special, they are just numbers.
if typ == TypeID {
if !isValidID(input) {
return errors.New("Invalid ID")
}
return nil
}
return isValidBase32(input, typ)
}
// FilecoinDecodeToBytes converts the input string to a byte array
func FilecoinDecodeToBytes(input string) ([]byte, error) {
if err := isValidString(input); err != nil {
return nil, err
}
typ := parseType(input[1])
var bytes []byte = make([]byte, 0)
// First byte is type
bytes = append(bytes, byte(typ))
if typ == TypeID {
id, _ := strconv.ParseUint(input[2:], 10, 0) // error checked in isValid
for id >= 0x80 {
bytes = append(bytes, byte(id)|byte(0x80))
id >>= 7
}
bytes = append(bytes, byte(id))
return bytes, nil
}
payloadSize := payloadSize(typ)
var decoded []byte = make([]byte, payloadSize+checksumSize)
_, err := base32Encoding.Decode(decoded, []byte(input[2:]))
if err != nil {
return nil, errors.New("Invalid address, base32 decoding failed")
}
bytes = append(bytes, decoded[:payloadSize]...)
return bytes, nil
}
// FilecoinEncodeToString converts the input byte array to a string representation of the Solana address.
func FilecoinEncodeToString(data []byte) (string, error) {
if len(data) < 2 {
return "", errors.New("Data too short")
}
var t Type = getType(data[0])
if t == TypeInvalid {
return "", errors.New("Invalid type")
}
var s string
// Main net address prefix
s += string(PREFIX)
// Address type prefix
s += typeAscii(t)
if t == TypeID {
var id uint = 0
shift := 0
for i := 1; i < len(data); i++ {
if data[i] < 0x80 {
id |= uint(data[i]) << shift
break
} else {
id |= (uint(data[i] & 0x7F)) << shift
shift += 7
}
}
s += strconv.FormatUint(uint64(id), 10)
return s, nil
}
psize := payloadSize(t)
if len(data) != 1+int(psize) {
return "", errors.New("Invalid length")
}
// Base32 encoded body
var toEncode []byte = make([]byte, 0)
// Copy address payload without prefix
toEncode = append(toEncode, data[1:psize+1]...)
// Append checksum
sum := filecoinComputeChecksum(data)
toEncode = append(toEncode, sum...)
s += base32Encoding.EncodeToString(toEncode)
return s, nil
}