-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
78 lines (63 loc) · 2.1 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
package address
import (
"encoding/hex"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/base58"
"github.com/larahfelipe/tronbridge/pkg/common"
"golang.org/x/crypto/sha3"
)
type Address struct {
*btcec.PublicKey
}
// Returns a new address based on the provided public key and elliptic curve.
func New(publicKeyBytes []byte, ellipticCurve *btcec.KoblitzCurve) (*Address, error) {
pk, err := btcec.ParsePubKey(publicKeyBytes, ellipticCurve)
if err != nil {
return nil, fmt.Errorf("failed to parse public key: %s", err)
}
apk, err := btcutil.NewAddressPubKey(pk.SerializeCompressed(), &chaincfg.Params{
PubKeyHashAddrID: common.NullByte,
})
if err != nil {
return nil, fmt.Errorf("failed to get address from public key: %s", err)
}
return &Address{apk.PubKey()}, nil
}
// ToHex converts the address to a hexadecimal string representation.
func (a *Address) ToHex() string {
return hex.EncodeToString(a.SerializeCompressed())
}
// ToBytes converts the address to a byte slice representation.
func (a *Address) ToBytes() []byte {
return []byte(a.ToHex())
}
// ToB58 converts the address to a base58-encoded string representation.
func (a *Address) ToB58() (string, error) {
h := sha3.NewLegacyKeccak256()
ah := a.ToHex()
if _, err := h.Write([]byte(ah[1:])); err != nil {
return "", fmt.Errorf("failed to write hash: %s", err)
}
sh := h.Sum(nil)[12:]
return base58.CheckEncode(sh, common.TronNetworkByte), nil
}
// B58AddressFromPrivateKey returns a base58-encoded string representing the address derived from the provided private key.
func B58AddressFromPrivateKey(privateKeyHex string, ellipticCurve *btcec.KoblitzCurve) (string, error) {
pkb, err := hex.DecodeString(privateKeyHex)
if err != nil {
return "", fmt.Errorf("failed to decode private key to bytes: %s", err)
}
pk, _ := btcec.PrivKeyFromBytes(ellipticCurve, pkb)
a, err := New(pk.PubKey().SerializeUncompressed(), ellipticCurve)
if err != nil {
return "", err
}
ab58, err := a.ToB58()
if err != nil {
return "", err
}
return ab58, nil
}