From 95f90216afe539ba08b06e51cb5c9d787b7ef5c3 Mon Sep 17 00:00:00 2001 From: yincong Date: Thu, 6 Jun 2024 11:45:42 +0800 Subject: [PATCH 1/3] fix: Optimize regular initialization --- accounts/abi/type.go | 6 ++++-- accounts/scwallet/wallet.go | 12 ++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/accounts/abi/type.go b/accounts/abi/type.go index 383982663331..d57fa3d4e667 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -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. @@ -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 diff --git a/accounts/scwallet/wallet.go b/accounts/scwallet/wallet.go index f0ca9085b680..66c216dbf732 100644 --- a/accounts/scwallet/wallet.go +++ b/accounts/scwallet/wallet.go @@ -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,}$`) + + // PunRegexp is the regular expression used to validate PUN codes. + PunRegexp = regexp.MustCompile(`^[0-9]{12,}$`) +) + // List of APDU command-related constants const ( claISO7816 = 0 @@ -380,7 +388,7 @@ 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 } @@ -388,7 +396,7 @@ func (w *Wallet) Open(passphrase string) error { return err } default: - if !regexp.MustCompile(`^[0-9]{12,}$`).MatchString(passphrase) { + if !PunRegexp.MatchString(passphrase) { w.log.Error("PUK needs to be at least 12 digits") return ErrPINUnblockNeeded } From b6a923fef8fcfb8bdcda07fe68faca215e7493c6 Mon Sep 17 00:00:00 2001 From: yincong Date: Fri, 7 Jun 2024 17:27:45 +0800 Subject: [PATCH 2/3] modify var name --- accounts/scwallet/wallet.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/accounts/scwallet/wallet.go b/accounts/scwallet/wallet.go index 66c216dbf732..0f3311169186 100644 --- a/accounts/scwallet/wallet.go +++ b/accounts/scwallet/wallet.go @@ -77,8 +77,8 @@ var ( // PinRegexp is the regular expression used to validate PIN codes. PinRegexp = regexp.MustCompile(`^[0-9]{6,}$`) - // PunRegexp is the regular expression used to validate PUN codes. - PunRegexp = regexp.MustCompile(`^[0-9]{12,}$`) + // PukRegexp is the regular expression used to validate PUK codes. + PukRegexp = regexp.MustCompile(`^[0-9]{12,}$`) ) // List of APDU command-related constants @@ -396,7 +396,7 @@ func (w *Wallet) Open(passphrase string) error { return err } default: - if !PunRegexp.MatchString(passphrase) { + if !PukRegexp.MatchString(passphrase) { w.log.Error("PUK needs to be at least 12 digits") return ErrPINUnblockNeeded } From 176f0ab9c94c812cef6a2d785d66be994f6a4d29 Mon Sep 17 00:00:00 2001 From: yincong Date: Tue, 11 Jun 2024 18:47:30 +0800 Subject: [PATCH 3/3] variable change to private types --- accounts/scwallet/wallet.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/accounts/scwallet/wallet.go b/accounts/scwallet/wallet.go index 0f3311169186..58cfc8830163 100644 --- a/accounts/scwallet/wallet.go +++ b/accounts/scwallet/wallet.go @@ -75,10 +75,10 @@ var ( var ( // PinRegexp is the regular expression used to validate PIN codes. - PinRegexp = regexp.MustCompile(`^[0-9]{6,}$`) + pinRegexp = regexp.MustCompile(`^[0-9]{6,}$`) // PukRegexp is the regular expression used to validate PUK codes. - PukRegexp = regexp.MustCompile(`^[0-9]{12,}$`) + pukRegexp = regexp.MustCompile(`^[0-9]{12,}$`) ) // List of APDU command-related constants @@ -388,7 +388,7 @@ func (w *Wallet) Open(passphrase string) error { case passphrase == "": return ErrPINUnblockNeeded case status.PinRetryCount > 0: - if !PinRegexp.MatchString(passphrase) { + if !pinRegexp.MatchString(passphrase) { w.log.Error("PIN needs to be at least 6 digits") return ErrPINNeeded } @@ -396,7 +396,7 @@ func (w *Wallet) Open(passphrase string) error { return err } default: - if !PukRegexp.MatchString(passphrase) { + if !pukRegexp.MatchString(passphrase) { w.log.Error("PUK needs to be at least 12 digits") return ErrPINUnblockNeeded }