Skip to content

Commit ae1d90e

Browse files
authored
internal/ethapi: make NewAccount return EIP-55 format (#26973)
This change implements returning the address as EIP-55 encoded when creating a new account.
1 parent 2f2959d commit ae1d90e

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

common/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,16 @@ func (ma *MixedcaseAddress) ValidChecksum() bool {
429429
func (ma *MixedcaseAddress) Original() string {
430430
return ma.original
431431
}
432+
433+
// AddressEIP55 is an alias of Address with a customized json marshaller
434+
type AddressEIP55 Address
435+
436+
// String returns the hex representation of the address in the manner of EIP55.
437+
func (addr AddressEIP55) String() string {
438+
return Address(addr).Hex()
439+
}
440+
441+
// MarshalJSON marshals the address in the manner of EIP55.
442+
func (addr AddressEIP55) MarshalJSON() ([]byte, error) {
443+
return json.Marshal(addr.String())
444+
}

common/types_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,27 @@ func TestHash_Format(t *testing.T) {
559559
})
560560
}
561561
}
562+
563+
func TestAddressEIP55(t *testing.T) {
564+
addr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
565+
addrEIP55 := AddressEIP55(addr)
566+
567+
if addr.Hex() != addrEIP55.String() {
568+
t.Fatal("AddressEIP55 should match original address hex")
569+
}
570+
571+
blob, err := addrEIP55.MarshalJSON()
572+
if err != nil {
573+
t.Fatal("Failed to marshal AddressEIP55", err)
574+
}
575+
if strings.Trim(string(blob), "\"") != addr.Hex() {
576+
t.Fatal("Address with checksum is expected")
577+
}
578+
var dec Address
579+
if err := json.Unmarshal(blob, &dec); err != nil {
580+
t.Fatal("Failed to unmarshal AddressEIP55", err)
581+
}
582+
if addr != dec {
583+
t.Fatal("Unexpected address after unmarshal")
584+
}
585+
}

internal/ethapi/api.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,20 @@ func (s *PersonalAccountAPI) DeriveAccount(url string, path string, pin *bool) (
354354
}
355355

356356
// NewAccount will create a new account and returns the address for the new account.
357-
func (s *PersonalAccountAPI) NewAccount(password string) (common.Address, error) {
357+
func (s *PersonalAccountAPI) NewAccount(password string) (common.AddressEIP55, error) {
358358
ks, err := fetchKeystore(s.am)
359359
if err != nil {
360-
return common.Address{}, err
360+
return common.AddressEIP55{}, err
361361
}
362362
acc, err := ks.NewAccount(password)
363363
if err == nil {
364-
log.Info("Your new key was generated", "address", acc.Address)
364+
addrEIP55 := common.AddressEIP55(acc.Address)
365+
log.Info("Your new key was generated", "address", addrEIP55.String())
365366
log.Warn("Please backup your key file!", "path", acc.URL.Path)
366367
log.Warn("Please remember your password!")
367-
return acc.Address, nil
368+
return addrEIP55, nil
368369
}
369-
return common.Address{}, err
370+
return common.AddressEIP55{}, err
370371
}
371372

372373
// fetchKeystore retrieves the encrypted keystore from the account manager.

0 commit comments

Comments
 (0)