-
Notifications
You must be signed in to change notification settings - Fork 5
/
address.go
115 lines (89 loc) · 2.45 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Package address provides functionality for parsing and manipulating Tron addresses.
package address
import (
"crypto/ecdsa"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/btcsuite/btcutil/base58"
"github.com/ethereum/go-ethereum/crypto"
"golang.org/x/crypto/sha3"
)
// All addresses are prefixed with 0x41 so that when they are encoded into base 58 they
// start with 'T'.
const prefix byte = 0x41
// Address is a public identifier for an account that exists on the Tron network.
type Address [21]byte
var Zero = Address([21]byte{})
func FromPublicKey(pub *ecdsa.PublicKey) Address {
// TODO(271): Remove dependencies for go-ethereum.
// Compressed ECDSA keys have a byte prefix that we need to trim off to get the coordinates.
xy := crypto.FromECDSAPub(pub)[1:]
h := sha3.NewLegacyKeccak256()
if _, err := h.Write(xy); err != nil {
panic("address: unexpected error encountered while writing key")
}
// All addresses start with 0x41, the last 20 bytes of the hash are the address.
var addr Address
addr[0] = prefix
copy(addr[1:], h.Sum(nil)[12:])
return addr
}
// FromBase16 parses a base 16 (hexadecimal) string into an address.
func FromBase16(str string) (Address, error) {
bs, err := hex.DecodeString(str)
if err != nil {
return Zero, err
}
if len(bs) != 21 {
return Zero, fmt.Errorf("address: hex string is invalid length (%d)", len(bs))
}
var addr Address
copy(addr[:], bs)
return addr, err
}
// FromBase58 parses a base 58 checked string into an address.
func FromBase58(str string) (Address, error) {
bs, check, err := base58.CheckDecode(str)
if err != nil {
return Zero, err
}
if check != prefix {
return Zero, fmt.Errorf("address: invalid prefix (%d)", check)
}
var addr Address
addr[0] = prefix
copy(addr[1:], bs)
return addr, nil
}
// ToBase16 encodes the address into a base 16 string.
func (a Address) ToBase16() string {
return hex.EncodeToString(a[:])
}
// ToBase58 encodes the address into a checked base 58 string.
func (a Address) ToBase58() string {
return base58.CheckEncode(a[1:], prefix)
}
func (a *Address) UnmarshalJSON(b []byte) error {
var str string
if err := json.Unmarshal(b, &str); err != nil {
return err
}
var (
addr Address
err error
)
switch len(str) {
case 42:
addr, err = FromBase16(str)
case 34:
addr, err = FromBase58(str)
default:
return fmt.Errorf("address: unexpected length of json string (%d)", len(str))
}
if err != nil {
return err
}
*a = addr
return nil
}