Skip to content

Commit

Permalink
Merge pull request #814 from JukLee0ira/fix-perfix
Browse files Browse the repository at this point in the history
accounts/keystore: fix xdc-prefix address bug
  • Loading branch information
gzliudan authored Jan 23, 2025
2 parents c3badb4 + ae438be commit ff923bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 1 addition & 2 deletions accounts/keystore/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/XinFinOrg/XDPoSChain/accounts"
Expand Down Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions accounts/keystore/keystore_plain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit ff923bd

Please sign in to comment.