Skip to content

Commit

Permalink
accounts: avoid duplicate regex compilation (ethereum#29943)
Browse files Browse the repository at this point in the history
* fix: Optimize regular initialization

* modify var name

* variable change to private types
  • Loading branch information
yincongcyincong authored and gzliudan committed Jan 11, 2025
1 parent 9c1e23b commit 5435e67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 4 additions & 2 deletions accounts/abi/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ type Type struct {
var (
// typeRegex parses the abi sub types
typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?")

// sliceSizeRegex grab the slice size
sliceSizeRegex = regexp.MustCompile("[0-9]+")
)

// NewType creates a new reflection type of abi type given in t.
Expand Down Expand Up @@ -91,8 +94,7 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
// grab the last cell and create a type from there
sliced := t[i:]
// grab the slice size with regexp
re := regexp.MustCompile("[0-9]+")
intz := re.FindAllString(sliced, -1)
intz := sliceSizeRegex.FindAllString(sliced, -1)

if len(intz) == 0 {
// is a slice
Expand Down
12 changes: 10 additions & 2 deletions accounts/scwallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ var (
DerivationSignatureHash = sha256.Sum256(common.Hash{}.Bytes())
)

var (
// PinRegexp is the regular expression used to validate PIN codes.
pinRegexp = regexp.MustCompile(`^[0-9]{6,}$`)

// PukRegexp is the regular expression used to validate PUK codes.
pukRegexp = regexp.MustCompile(`^[0-9]{12,}$`)
)

// List of APDU command-related constants
const (
claISO7816 = 0
Expand Down Expand Up @@ -380,15 +388,15 @@ func (w *Wallet) Open(passphrase string) error {
case passphrase == "":
return ErrPINUnblockNeeded
case status.PinRetryCount > 0:
if !regexp.MustCompile(`^[0-9]{6,}$`).MatchString(passphrase) {
if !pinRegexp.MatchString(passphrase) {
w.log.Error("PIN needs to be at least 6 digits")
return ErrPINNeeded
}
if err := w.session.verifyPin([]byte(passphrase)); err != nil {
return err
}
default:
if !regexp.MustCompile(`^[0-9]{12,}$`).MatchString(passphrase) {
if !pukRegexp.MatchString(passphrase) {
w.log.Error("PUK needs to be at least 12 digits")
return ErrPINUnblockNeeded
}
Expand Down

0 comments on commit 5435e67

Please sign in to comment.