-
Notifications
You must be signed in to change notification settings - Fork 12
/
tron_address.go
37 lines (31 loc) · 964 Bytes
/
tron_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
package types
import (
"errors"
"fmt"
tronaddress "github.com/fbsobreira/gotron-sdk/pkg/address"
"github.com/fbsobreira/gotron-sdk/pkg/common"
)
// TronContractAddressLen is the length of contract address strings
const TronContractAddressLen = 34
// ValidateTronAddress validates the ethereum address strings
func ValidateTronAddress(address string) error {
if address == "" {
return errors.New("empty")
}
if len(address) != TronContractAddressLen {
return errors.New("wrong length")
}
tronAddr, err := common.DecodeCheck(address)
if err != nil {
return errors.New("doesn't pass format validation")
}
expectAddress := common.EncodeCheck(tronAddr)
if expectAddress != address {
return fmt.Errorf("mismatch expected: %s, got: %s", expectAddress, address)
}
return nil
}
func AddressFromHex(str string) string {
bytes, _ := common.FromHex(str)
return tronaddress.Address(append([]byte{tronaddress.TronBytePrefix}, bytes...)).String()
}