Skip to content

Commit 1ae743a

Browse files
committed
accounts: avoid duplicate regex compilation (ethereum#29943)
* fix: Optimize regular initialization * modify var name * variable change to private types
1 parent 8578ff2 commit 1ae743a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

accounts/abi/type.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type Type struct {
6464
var (
6565
// typeRegex parses the abi sub types
6666
typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?")
67+
68+
// sliceSizeRegex grab the slice size
69+
sliceSizeRegex = regexp.MustCompile("[0-9]+")
6770
)
6871

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

9799
if len(intz) == 0 {
98100
// is a slice

accounts/scwallet/wallet.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ var (
7373
DerivationSignatureHash = sha256.Sum256(common.Hash{}.Bytes())
7474
)
7575

76+
var (
77+
// PinRegexp is the regular expression used to validate PIN codes.
78+
pinRegexp = regexp.MustCompile(`^[0-9]{6,}$`)
79+
80+
// PukRegexp is the regular expression used to validate PUK codes.
81+
pukRegexp = regexp.MustCompile(`^[0-9]{12,}$`)
82+
)
83+
7684
// List of APDU command-related constants
7785
const (
7886
claISO7816 = 0
@@ -380,15 +388,15 @@ func (w *Wallet) Open(passphrase string) error {
380388
case passphrase == "":
381389
return ErrPINUnblockNeeded
382390
case status.PinRetryCount > 0:
383-
if !regexp.MustCompile(`^[0-9]{6,}$`).MatchString(passphrase) {
391+
if !pinRegexp.MatchString(passphrase) {
384392
w.log.Error("PIN needs to be at least 6 digits")
385393
return ErrPINNeeded
386394
}
387395
if err := w.session.verifyPin([]byte(passphrase)); err != nil {
388396
return err
389397
}
390398
default:
391-
if !regexp.MustCompile(`^[0-9]{12,}$`).MatchString(passphrase) {
399+
if !pukRegexp.MatchString(passphrase) {
392400
w.log.Error("PUK needs to be at least 12 digits")
393401
return ErrPINUnblockNeeded
394402
}

0 commit comments

Comments
 (0)