-
Notifications
You must be signed in to change notification settings - Fork 13
/
stellar.go
66 lines (58 loc) · 1.74 KB
/
stellar.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
package coincodec
import (
"bytes"
"encoding/base32"
"github.com/pkg/errors"
"github.com/wealdtech/go-slip44"
)
const (
versionByteAccountId byte = 0x30
)
func init() {
toBytesMap[slip44.STELLAR_LUMENS] = StellarDecodeToBytes
toStringMap[slip44.STELLAR_LUMENS] = StellarEncodeToString
toBytesMap[slip44.KIN] = StellarDecodeToBytes
toStringMap[slip44.KIN] = StellarEncodeToString
}
// StellarDecodeToBytes converts the input string to a byte array
func StellarDecodeToBytes(input string) ([]byte, error) {
decoded, err := base32.StdEncoding.DecodeString(input)
if err != nil {
return nil, errors.Wrap(err, "base32 decode error")
}
if decoded[0] != versionByteAccountId {
return nil, errors.New("invalid version byte")
}
checksum := crc16(decoded[:len(decoded)-2])
if !bytes.Equal(checksum, decoded[len(decoded)-2:]) {
return nil, errors.New("wrong checksum")
}
// strip version byte and checksum
return decoded[1 : len(decoded)-2], nil
}
// StellarEncodeToString converts the input byte array to a string representation of the Cosmos address.
func StellarEncodeToString(bytes []byte) (string, error) {
data := []byte{versionByteAccountId}
data = append(data, bytes...)
checksum := crc16(data)
data = append(data, checksum...)
return base32.StdEncoding.EncodeToString(data), nil
}
func crc16(bytes []byte) []byte {
// https://godoc.org/github.com/stellar/go/crc16
crc := uint16(0x0000)
polynomial := uint16(0x1021)
for _, b := range bytes {
for bitidx := byte(0); bitidx < 8; bitidx++ {
bit := (b >> (7 - bitidx) & 1) == 1
c15 := (crc >> 15 & 1) == 1
crc <<= 1
if c15 != bit {
crc ^= polynomial
}
}
}
crc = crc & uint16(0xffff)
checksum := []byte{byte(crc & 0x00ff), byte((crc >> 8) & 0x00ff)}
return checksum
}