From ae438bea693dbd9430900a18459ae0ff24309972 Mon Sep 17 00:00:00 2001 From: JukLee0ira Date: Thu, 16 Jan 2025 14:50:38 +0800 Subject: [PATCH] accounts/keystore: fix xdc-prefix address bug (#759) --- accounts/keystore/key.go | 3 +-- accounts/keystore/keystore_plain_test.go | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/accounts/keystore/key.go b/accounts/keystore/key.go index 4ff80ea45f..7bb300fb82 100644 --- a/accounts/keystore/key.go +++ b/accounts/keystore/key.go @@ -25,7 +25,6 @@ import ( "io" "os" "path/filepath" - "strings" "time" "github.com/XinFinOrg/XDPoSChain/accounts" @@ -151,7 +150,7 @@ func NewKeyForDirectICAP(rand io.Reader) *Key { panic("key generation: ecdsa.GenerateKey failed: " + err.Error()) } key := newKeyFromECDSA(privateKeyECDSA) - if !strings.HasPrefix(key.Address.Hex(), "0x00") { + if key.Address[0] != 0 { return NewKeyForDirectICAP(rand) } return key diff --git a/accounts/keystore/keystore_plain_test.go b/accounts/keystore/keystore_plain_test.go index 4a977d2eac..69dee3e3dd 100644 --- a/accounts/keystore/keystore_plain_test.go +++ b/accounts/keystore/keystore_plain_test.go @@ -248,8 +248,27 @@ func TestKeyForDirectICAP(t *testing.T) { t.Skip("NewKeyForDirectICAP in this test is invalid, will fall into a infinite loop ") t.Parallel() key := NewKeyForDirectICAP(rand.Reader) - if !strings.HasPrefix(key.Address.Hex(), "0x00") { - t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex()) + if key.Address[0] != 0 { + t.Errorf("Expected first address byte to be zero, have: %x", key.Address[0]) + } +} + +func TestNewKeyForDirectICAP(t *testing.T) { + addr0 := common.HexToAddress("0x00ffffffffffffffffffffffffffffffffffffff") + addr1 := common.HexToAddress("0xffffffffffffffffffffffffffffffffffffffff") + tests := []struct { + name string + result bool + }{ + {"addr0 start with 0", addr0[0] == 0}, + {"addr1 not start with 0", addr1[0] != 0}, + {"addr0 start with 0x00 or xdc00", strings.HasPrefix(addr0.Hex(), "0x00") || strings.HasPrefix(addr0.Hex(), "xdc00")}, + {"addr1 not start with 0x00 nor xdc00", !strings.HasPrefix(addr1.Hex(), "0x00") && !strings.HasPrefix(addr1.Hex(), "xdc00")}, + } + for _, tt := range tests { + if !tt.result { + t.Errorf("test %q failed\n", tt.name) + } } }