@@ -3,6 +3,8 @@ package eth
3
3
import (
4
4
"errors"
5
5
6
+ "github.com/33cn/chain33/common/crypto/client"
7
+
6
8
"github.com/33cn/chain33/common/address"
7
9
"github.com/ethereum/go-ethereum/common"
8
10
"github.com/ethereum/go-ethereum/crypto"
@@ -41,7 +43,7 @@ func (e *eth) PubKeyToAddr(pubKey []byte) string {
41
43
if value , ok := addrCache .Get (pubStr ); ok {
42
44
return value .(string )
43
45
}
44
- addr := PubKey2EthAddr (pubKey )
46
+ addr := pubKey2EthAddr (pubKey )
45
47
addrCache .Add (pubStr , addr )
46
48
return addr
47
49
}
@@ -61,7 +63,7 @@ func (e *eth) GetName() string {
61
63
62
64
// ToString trans to string format
63
65
func (e * eth ) ToString (addr []byte ) string {
64
- return address . ToLower (common .BytesToAddress (addr ).String ())
66
+ return formatAddr (common .BytesToAddress (addr ).String ())
65
67
}
66
68
67
69
// FromString trans to byte format
@@ -72,16 +74,29 @@ func (e *eth) FromString(addr string) ([]byte, error) {
72
74
return common .HexToAddress (addr ).Bytes (), nil
73
75
}
74
76
75
- // PubKey2EthAddr format eth addr
76
- func PubKey2EthAddr (pubKey []byte ) string {
77
+ func (e * eth ) FormatAddr (addr string ) string {
78
+
79
+ return formatAddr (addr )
80
+ }
81
+
82
+ func formatAddr (addr string ) string {
83
+ ctx := client .GetCryptoContext ()
84
+ if ctx .API == nil || ctx .API .GetConfig ().IsFork (ctx .CurrBlockHeight , address .ForkFormatAddressKey ) {
85
+ return address .ToLower (addr )
86
+ }
87
+ return addr
88
+ }
89
+
90
+ // pubKey2EthAddr format eth addr
91
+ func pubKey2EthAddr (pubKey []byte ) string {
77
92
78
93
pub , err := crypto .DecompressPubkey (pubKey )
79
94
// ecdsa public key, compatible with ethereum, get address from eth api
80
95
if err == nil {
81
- return address . ToLower (crypto .PubkeyToAddress (* pub ).String ())
96
+ return formatAddr (crypto .PubkeyToAddress (* pub ).String ())
82
97
}
83
98
// just format as eth address if pubkey not compatible
84
99
var a common.Address
85
100
a .SetBytes (crypto .Keccak256 (pubKey [1 :])[12 :])
86
- return address . ToLower (a .String ())
101
+ return formatAddr (a .String ())
87
102
}
0 commit comments