/
wallet_trx.go
110 lines (85 loc) · 1.87 KB
/
wallet_trx.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
package hdwallet
import (
"crypto/ecdsa"
"crypto/sha256"
"github.com/dig-coins/hd-wallet/helpers/ethhelper"
"math/big"
"github.com/shengdoushi/base58"
)
func init() {
coins[TRX] = newTRX
}
type trx struct {
name string
symbol string
key *Key
// trc20 token
contract string
}
func newTRX(key *Key) Wallet {
return &trx{
name: "Tron",
symbol: "TRX",
key: key,
}
}
func (c *trx) GetType() uint32 {
return c.key.Opt.CoinType
}
func (c *trx) GetName() string {
return c.name
}
func (c *trx) GetSymbol() string {
return c.symbol
}
func (c *trx) GetKey() *Key {
return c.key
}
func (c *trx) GetAddress() (string, error) {
a := PubkeyToAddress(*c.key.PublicECDSA)
return a.String(), nil
}
func (c *trx) GetPrivateKey() (string, error) {
return c.key.PrivateHex(), nil
}
//
//
//
const ( // TronBytePrefix is the hex prefix to address
TronBytePrefix = byte(0x41)
)
// Address represents the 21 byte address of an Tron account.
type Address []byte
// Bytes get bytes from address
func (a Address) Bytes() []byte {
return a[:]
}
func Encode(input []byte) string {
return base58.Encode(input, base58.BitcoinAlphabet)
}
func EncodeCheck(input []byte) string {
h256h0 := sha256.New()
h256h0.Write(input)
h0 := h256h0.Sum(nil)
h256h1 := sha256.New()
h256h1.Write(h0)
h1 := h256h1.Sum(nil)
inputCheck := input
inputCheck = append(inputCheck, h1[:4]...)
return Encode(inputCheck)
}
// String implements fmt.Stringer.
func (a Address) String() string {
if a[0] == 0 {
return new(big.Int).SetBytes(a.Bytes()).String()
}
return EncodeCheck(a.Bytes())
}
// PubkeyToAddress returns address from ecdsa public key
func PubkeyToAddress(p ecdsa.PublicKey) Address {
address := ethhelper.PubkeyToAddress(p)
addressTron := make([]byte, 0)
addressTron = append(addressTron, TronBytePrefix)
addressTron = append(addressTron, address.Bytes()...)
return addressTron
}