-
Notifications
You must be signed in to change notification settings - Fork 30
/
slip0044.go
47 lines (38 loc) · 1.04 KB
/
slip0044.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
package slip0044
import (
"encoding/json"
"strings"
)
// Return the path component that corresponds to this coin type.
func (t CoinType) PathComponent() uint32 {
return 0x80000000 | uint32(t)
}
// Return the coin symbol, which is preferred over the native String() method.
func (t CoinType) Symbol() string {
return symbolsByType[t]
}
// Parse a coin from its symbol or name into the corresponding coin type.
func ParseCoinType(coin string) (CoinType, error) {
// First try to parse as a symbol.
if coinType, ok := typesBySymbol[strings.ToUpper(coin)]; ok {
return coinType, nil
}
// Otherwise try to parse as a name.
if coinType, ok := typesByName[strings.ToLower(coin)]; ok {
return coinType, nil
}
return 0, ErrUnknownCoin
}
func (t CoinType) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Symbol())
}
func (t *CoinType) UnmarshalJSON(data []byte) (err error) {
var symbol string
if err = json.Unmarshal(data, &symbol); err != nil {
return err
}
if *t, err = ParseCoinType(symbol); err != nil {
return err
}
return nil
}