Unverified Commit ae1d90e7 authored by Alex Mylonas's avatar Alex Mylonas Committed by GitHub

internal/ethapi: make NewAccount return EIP-55 format (#26973)

This change implements returning the address as EIP-55 encoded when creating a new account.
parent 2f2959d0
...@@ -429,3 +429,16 @@ func (ma *MixedcaseAddress) ValidChecksum() bool { ...@@ -429,3 +429,16 @@ func (ma *MixedcaseAddress) ValidChecksum() bool {
func (ma *MixedcaseAddress) Original() string { func (ma *MixedcaseAddress) Original() string {
return ma.original return ma.original
} }
// AddressEIP55 is an alias of Address with a customized json marshaller
type AddressEIP55 Address
// String returns the hex representation of the address in the manner of EIP55.
func (addr AddressEIP55) String() string {
return Address(addr).Hex()
}
// MarshalJSON marshals the address in the manner of EIP55.
func (addr AddressEIP55) MarshalJSON() ([]byte, error) {
return json.Marshal(addr.String())
}
...@@ -559,3 +559,27 @@ func TestHash_Format(t *testing.T) { ...@@ -559,3 +559,27 @@ func TestHash_Format(t *testing.T) {
}) })
} }
} }
func TestAddressEIP55(t *testing.T) {
addr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
addrEIP55 := AddressEIP55(addr)
if addr.Hex() != addrEIP55.String() {
t.Fatal("AddressEIP55 should match original address hex")
}
blob, err := addrEIP55.MarshalJSON()
if err != nil {
t.Fatal("Failed to marshal AddressEIP55", err)
}
if strings.Trim(string(blob), "\"") != addr.Hex() {
t.Fatal("Address with checksum is expected")
}
var dec Address
if err := json.Unmarshal(blob, &dec); err != nil {
t.Fatal("Failed to unmarshal AddressEIP55", err)
}
if addr != dec {
t.Fatal("Unexpected address after unmarshal")
}
}
...@@ -354,19 +354,20 @@ func (s *PersonalAccountAPI) DeriveAccount(url string, path string, pin *bool) ( ...@@ -354,19 +354,20 @@ func (s *PersonalAccountAPI) DeriveAccount(url string, path string, pin *bool) (
} }
// NewAccount will create a new account and returns the address for the new account. // NewAccount will create a new account and returns the address for the new account.
func (s *PersonalAccountAPI) NewAccount(password string) (common.Address, error) { func (s *PersonalAccountAPI) NewAccount(password string) (common.AddressEIP55, error) {
ks, err := fetchKeystore(s.am) ks, err := fetchKeystore(s.am)
if err != nil { if err != nil {
return common.Address{}, err return common.AddressEIP55{}, err
} }
acc, err := ks.NewAccount(password) acc, err := ks.NewAccount(password)
if err == nil { if err == nil {
log.Info("Your new key was generated", "address", acc.Address) addrEIP55 := common.AddressEIP55(acc.Address)
log.Info("Your new key was generated", "address", addrEIP55.String())
log.Warn("Please backup your key file!", "path", acc.URL.Path) log.Warn("Please backup your key file!", "path", acc.URL.Path)
log.Warn("Please remember your password!") log.Warn("Please remember your password!")
return acc.Address, nil return addrEIP55, nil
} }
return common.Address{}, err return common.AddressEIP55{}, err
} }
// fetchKeystore retrieves the encrypted keystore from the account manager. // fetchKeystore retrieves the encrypted keystore from the account manager.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment