-
Notifications
You must be signed in to change notification settings - Fork 0
/
external.go
68 lines (56 loc) · 1.25 KB
/
external.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
package addrutils
import (
"encoding/hex"
"github.com/ofgp/common/defines"
)
func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str)
return h
}
func FromHex(s string) []byte {
if len(s) > 1 {
if s[0:2] == "0x" || s[0:2] == "0X" {
s = s[2:]
}
}
if len(s)%2 == 1 {
s = "0" + s
}
return Hex2Bytes(s)
}
func CheckBytesToStr(checkBytes []byte, chainCode uint8) (string, error) {
switch chainCode {
case defines.CHAIN_CODE_BTC:
addr, err := BtcAddressFromCheckBytes(checkBytes)
if err != nil {
return "", err
}
return addr.String(), nil
case defines.CHAIN_CODE_BCH:
addr, err := BchAddressFromCheckBytes(checkBytes)
if err != nil {
return "", err
}
return addr.String(), nil
case defines.CHAIN_CODE_ETH:
addr, err := EthAddressFromCheckBytes(checkBytes)
if err != nil {
return "", err
}
return addr.Hex(), nil
default:
return "", ErrUnknownChainCode
}
}
func StrToCheckBytes(sAddr string, chainCode uint8) ([]byte, error) {
switch chainCode {
case defines.CHAIN_CODE_BTC:
return StrBtcAddrToCheckBytes(sAddr)
case defines.CHAIN_CODE_BCH:
return StrBchAddrToCheckBytes(sAddr)
case defines.CHAIN_CODE_ETH:
return StrEthAddrToCheckBytes(sAddr)
default:
return []byte{}, ErrUnknownChainCode
}
}