From 0c04f1ba3ae5600acbeca31da02de7b095dab3dd Mon Sep 17 00:00:00 2001 From: seth-shi <1033404553@qq.com> Date: Thu, 11 Apr 2024 18:46:57 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=AF=8F=E6=AC=A1=E5=8F=91=E9=80=81?= =?UTF-8?q?=E7=9A=84=E7=A7=98=E9=92=A5=E9=83=BD=E4=B8=8D=E4=B8=80=E6=A0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/consts/csv.go | 5 --- internal/master/master.go | 9 ++--- internal/master/run_config.go | 4 ++- internal/master/var.go | 11 +++++++ internal/models/wallet.go | 7 ++-- internal/models/worker_status.go | 6 ++-- internal/worker/run_config.go | 13 ++------ internal/worker/run_status.go | 56 ++++++++++++++++---------------- internal/worker/worker.go | 20 +++++------- 9 files changed, 60 insertions(+), 71 deletions(-) delete mode 100644 internal/consts/csv.go create mode 100644 internal/master/var.go diff --git a/internal/consts/csv.go b/internal/consts/csv.go deleted file mode 100644 index 649718a..0000000 --- a/internal/consts/csv.go +++ /dev/null @@ -1,5 +0,0 @@ -package consts - -var ( - CsvHeaders = []string{"公钥", "加密Key", "加密后的助记词(请查看readme解密)"} -) diff --git a/internal/master/master.go b/internal/master/master.go index b32c192..04ab718 100644 --- a/internal/master/master.go +++ b/internal/master/master.go @@ -58,7 +58,7 @@ func NewMaster(port int, prefix, suffix string) (*Master, error) { workerStatusManager: models.NewWorkerStatusManager(works), } - if err := master.runConfig.storeWalletData(consts.CsvHeaders); err != nil { + if err := master.runConfig.storeWalletData(CsvHeaders); err != nil { return nil, err } @@ -237,12 +237,7 @@ func (m *Master) StartWebServer() { m.workerStatusManager.Add(&pro) if pro.HasWallet() { // 如果没有秘钥, 那么就是客户端发的 - line := []string{ - pro.Address, - pro.EncryptKey, - pro.EncryptMnemonic, - } - utils.MustError(m.runConfig.storeWalletData(line)) + utils.MustError(m.runConfig.storeWalletData(pro.Wallet)) } c.JSON(http.StatusOK, m.WorkerContent) diff --git a/internal/master/run_config.go b/internal/master/run_config.go index 1561818..378683b 100644 --- a/internal/master/run_config.go +++ b/internal/master/run_config.go @@ -4,6 +4,7 @@ import ( "encoding/csv" "errors" "fmt" + "github.com/seth-shi/ethereum-wallet-generator-worker/internal/models" "os" "strings" "time" @@ -43,9 +44,10 @@ func newRunConfig(port int, startAt time.Time) (*RunConfig, error) { }, nil } -func (rc *RunConfig) storeWalletData(line []string) error { +func (rc *RunConfig) storeWalletData(wa *models.WalletModel) error { // 创建一个csv写入器 + line := []string{wa.Address, wa.Key, wa.EncryptMnemonic} writer := csv.NewWriter(rc.FilePoint) // 循环写入数据 err := writer.Write(line) diff --git a/internal/master/var.go b/internal/master/var.go new file mode 100644 index 0000000..65a36c8 --- /dev/null +++ b/internal/master/var.go @@ -0,0 +1,11 @@ +package master + +import "github.com/seth-shi/ethereum-wallet-generator-worker/internal/models" + +var ( + CsvHeaders = &models.WalletModel{ + Address: "公钥", + EncryptMnemonic: "加密后的助记词(请查看readme解密)", + Key: "加密Key", + } +) diff --git a/internal/models/wallet.go b/internal/models/wallet.go index f21c024..82e446b 100644 --- a/internal/models/wallet.go +++ b/internal/models/wallet.go @@ -1,6 +1,7 @@ package models -type Wallet struct { - Address string `json:"address"` - Mnemonic []byte `json:"mnemonic"` +type WalletModel struct { + Address string `json:"address"` + EncryptMnemonic string `json:"mnemonic"` + Key string `json:"key"` } diff --git a/internal/models/worker_status.go b/internal/models/worker_status.go index 9841ddd..ce62ec6 100644 --- a/internal/models/worker_status.go +++ b/internal/models/worker_status.go @@ -13,11 +13,9 @@ type WorkStatusRequest struct { BuildVersion string `json:"build_version"` // 需要加密的数据 - EncryptKey string `json:"encrypt_key"` - Address string `json:"address"` - EncryptMnemonic string `json:"mnemonic"` + Wallet *WalletModel `json:"wallet"` } func (w *WorkStatusRequest) HasWallet() bool { - return w.Address != "" && w.EncryptMnemonic != "" + return w.Wallet != nil } diff --git a/internal/worker/run_config.go b/internal/worker/run_config.go index f26894e..5be2203 100644 --- a/internal/worker/run_config.go +++ b/internal/worker/run_config.go @@ -4,7 +4,6 @@ import ( "encoding/csv" "errors" "fmt" - "github.com/samber/lo" "os" "strings" @@ -20,8 +19,6 @@ type RunConfig struct { MasterHost string // 线程数量 C int - - key []byte } func newRunConfig(fullUrl string, c uint, name string) (*RunConfig, error) { @@ -31,20 +28,14 @@ func newRunConfig(fullUrl string, c uint, name string) (*RunConfig, error) { Version: utils.GetBuildVersion(), MasterHost: fullUrl, C: int(c), - key: []byte(lo.RandomString(consts.KeyLength, lo.LowerCaseLettersCharset)), }, nil } -func (rc *RunConfig) storeWalletData(wa *models.Wallet) { +func (rc *RunConfig) storeWalletData(wa *models.WalletModel) { // 凡是出错, 直接打印原始出来在标准输出 // 保存钱包的时候, 也需要加密数据 - encryptData, err := utils.AesGcmEncrypt(wa.Mnemonic, rc.key) - if err != nil { - utils.MustError(errors.New(fmt.Sprintf("钱包加密失败:[%s,%s]%s", wa.Address, wa.Mnemonic, err.Error()))) - } - - line := []string{wa.Address, string(rc.key), encryptData} + line := []string{wa.Address, wa.Key, wa.EncryptMnemonic} lineStr := strings.Join(line, ",") // 打开或创建一个csv文件,以追加模式写入 pf, err := os.OpenFile( diff --git a/internal/worker/run_status.go b/internal/worker/run_status.go index 0ca423d..cbd10f0 100644 --- a/internal/worker/run_status.go +++ b/internal/worker/run_status.go @@ -1,7 +1,9 @@ package worker import ( - "fmt" + "github.com/samber/lo" + "github.com/seth-shi/ethereum-wallet-generator-worker/internal/consts" + "github.com/seth-shi/ethereum-wallet-generator-worker/internal/utils" "strings" "sync/atomic" "time" @@ -29,31 +31,6 @@ func newRunStatus() *RunStatus { } } -func (r *RunStatus) matchNewWallet(matchConfig *models.MatchConfig) *models.Wallet { - defer func() { - r.TotalCount.Add(1) - r.RecentCount.Add(1) - }() - - wallet, err := r.newWallet() - if err != nil { - fmt.Println(err) - return nil - } - - if matchConfig.Prefix != "" && !strings.HasPrefix(wallet.Address, matchConfig.Prefix) { - return nil - } - - if matchConfig.Suffix != "" && !strings.HasSuffix(wallet.Address, matchConfig.Suffix) { - return nil - } - - r.FoundCount.Add(1) - - return wallet -} - func (r *RunStatus) Speed() float64 { var speed = 0.0 diff := time.Now().Unix() - r.StartAt @@ -63,7 +40,13 @@ func (r *RunStatus) Speed() float64 { return speed } -func (r *RunStatus) newWallet() (*models.Wallet, error) { +func (r *RunStatus) matchNewWallet(matchConfig *models.MatchConfig) (*models.WalletModel, error) { + + defer func() { + r.TotalCount.Add(1) + r.RecentCount.Add(1) + }() + // 生成随机熵(128位) entropy, err := bip39.NewEntropy(128) if err != nil { @@ -90,5 +73,22 @@ func (r *RunStatus) newWallet() (*models.Wallet, error) { // 获取地址、私钥和公钥 address := account.Address.Hex() - return &models.Wallet{Address: address, Mnemonic: []byte(mnemonic)}, nil + + if matchConfig.Prefix != "" && !strings.HasPrefix(address, matchConfig.Prefix) { + return nil, nil + } + + if matchConfig.Suffix != "" && !strings.HasSuffix(address, matchConfig.Suffix) { + return nil, nil + } + + // 加密 + key := lo.RandomString(consts.KeyLength, lo.LowerCaseLettersCharset) + encryptData, err := utils.AesGcmEncrypt([]byte(mnemonic), []byte(key)) + if err != nil { + return nil, err + } + + r.FoundCount.Add(1) + return &models.WalletModel{Address: address, EncryptMnemonic: encryptData, Key: key}, nil } diff --git a/internal/worker/worker.go b/internal/worker/worker.go index 519bce5..3ec05ad 100644 --- a/internal/worker/worker.go +++ b/internal/worker/worker.go @@ -71,7 +71,12 @@ func (w *Worker) timerReportServer() { func (w *Worker) loopMatchWallets() { for { - newWalletData := w.runStatus.matchNewWallet(w.matchConfig) + newWalletData, err := w.runStatus.matchNewWallet(w.matchConfig) + if err != nil { + fmt.Println(err) + continue + } + if newWalletData != nil { if err := w.reportServer(newWalletData); err != nil { w.runConfig.storeWalletData(newWalletData) @@ -106,7 +111,7 @@ func (w *Worker) timerOutput() { } } -func (w *Worker) reportServer(wa *models.Wallet) (err error) { +func (w *Worker) reportServer(wa *models.WalletModel) (err error) { recentCount := w.runStatus.RecentCount.Swap(0) defer func() { @@ -123,17 +128,8 @@ func (w *Worker) reportServer(wa *models.Wallet) (err error) { Count: int(recentCount), Speed: w.runStatus.Speed(), StartAt: w.runStatus.StartAt, + Wallet: wa, } - if wa != nil { - encryptData, err := utils.AesGcmEncrypt(wa.Mnemonic, w.runConfig.key) - if err != nil { - return err - } - progressReq.Address = wa.Address - progressReq.EncryptMnemonic = encryptData - progressReq.EncryptKey = string(w.runConfig.key) - } - data, err := json.Marshal(progressReq) if err != nil { return err