accounts/keystore: fix xdc-prefix address bug (#759)

This commit is contained in:
JukLee0ira 2025-01-16 14:50:38 +08:00
parent 12488b86ed
commit ae438bea69
2 changed files with 22 additions and 4 deletions

View file

@ -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

View file

@ -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)
}
}
}