Skip to content

Commit

Permalink
add: 每次发送的秘钥都不一样
Browse files Browse the repository at this point in the history
  • Loading branch information
seth-shi authored and seth-shi committed Apr 11, 2024
1 parent 1720555 commit 0c04f1b
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 71 deletions.
5 changes: 0 additions & 5 deletions internal/consts/csv.go

This file was deleted.

9 changes: 2 additions & 7 deletions internal/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion internal/master/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/csv"
"errors"
"fmt"
"github.com/seth-shi/ethereum-wallet-generator-worker/internal/models"
"os"
"strings"
"time"
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions internal/master/var.go
Original file line number Diff line number Diff line change
@@ -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",
}
)
7 changes: 4 additions & 3 deletions internal/models/wallet.go
Original file line number Diff line number Diff line change
@@ -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"`
}
6 changes: 2 additions & 4 deletions internal/models/worker_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 2 additions & 11 deletions internal/worker/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/csv"
"errors"
"fmt"
"github.com/samber/lo"
"os"
"strings"

Expand All @@ -20,8 +19,6 @@ type RunConfig struct {
MasterHost string
// 线程数量
C int

key []byte
}

func newRunConfig(fullUrl string, c uint, name string) (*RunConfig, error) {
Expand All @@ -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(
Expand Down
56 changes: 28 additions & 28 deletions internal/worker/run_status.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
20 changes: 8 additions & 12 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down

0 comments on commit 0c04f1b

Please sign in to comment.