-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbase58_address.go
48 lines (42 loc) · 1.17 KB
/
base58_address.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
package coincodec
import (
"bytes"
"github.com/pkg/errors"
)
func hasPrefix(data []byte, prefix []byte) bool {
res := false
if len(data) >= len(prefix) {
res = bytes.Equal(data[:len(prefix)], prefix)
}
return res
}
func base58AddressIsValidDataPrefix(data []byte, decodedSize int, validPrefixes [][]byte) error {
if len(data) != decodedSize {
return errors.New("Invalid decoded length")
}
for _, prefix := range validPrefixes {
if hasPrefix(data, prefix) {
return nil
}
}
return errors.New("Invalid prefix")
}
func Base58AddressDecodeToBytesPrefix(input string, decodedSize int, validPrefixes [][]byte) ([]byte, error) {
decoded, err := Base58ChecksumDecode(input, Base58DefaultAlphabet)
if err != nil {
return nil, err
}
err = base58AddressIsValidDataPrefix(decoded, decodedSize, validPrefixes)
if err != nil {
return nil, err
}
return decoded, nil
}
func Base58AddressEncodeToStringPrefix(data []byte, decodedSize int, validPrefixes [][]byte) (string, error) {
err := base58AddressIsValidDataPrefix(data, decodedSize, validPrefixes)
if err != nil {
return "", err
}
encoded := Base58ChecksumEncode(data, Base58DefaultAlphabet)
return encoded, nil
}