-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator.go
78 lines (66 loc) · 1.76 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package walleter
import (
"crypto/md5"
"encoding/json"
"fmt"
"gorm.io/gorm"
)
// walletValidator will check if the sign code of wallet is correct.
type walletValidator struct{}
func newWalletValidator() *walletValidator {
return &walletValidator{}
}
func (receiver walletValidator) validateWallet(wallet Wallet) (bool, error) {
checkSign := wallet.CheckSign
md5Value, err := receiver.generateNewSignHash(wallet)
if err != nil {
return false, err
}
if md5Value != checkSign {
return false, ErrIncorrectCheckSign
}
return true, nil
}
func (receiver walletValidator) generateNewSignHash(w Wallet) (string, error) {
var newERC20TokenData []ERC20TokenWallet
for _, token := range w.ERC20TokenData {
erc20Data := ERC20TokenWallet{
Model: gorm.Model{},
AccountId: token.AccountId,
Token: token.Token,
Balance: token.Balance,
Decimal: token.Decimal,
TotalIncome: token.TotalIncome,
TotalSpend: token.TotalSpend,
TotalDeposit: token.TotalDeposit,
TotalWithdraw: token.TotalWithdraw,
TotalFee: token.TotalFee,
}
newERC20TokenData = append(newERC20TokenData, erc20Data)
}
erc1155Data := ERC1155TokenWallet{
Model: gorm.Model{},
AccountId: w.ERC1155TokenData.AccountId,
Ids: w.ERC1155TokenData.Ids,
Values: w.ERC1155TokenData.Values,
}
tempWallet := Wallet{
Model: gorm.Model{},
AccountId: w.AccountId,
ERC20TokenData: newERC20TokenData,
ERC1155TokenData: erc1155Data,
CheckSign: "",
}
b, err := json.Marshal(tempWallet)
if err != nil {
return "", err
}
md5Value := md5Value(string(b))
return md5Value, nil
}
func md5Value(str string) string {
data := []byte(str)
has := md5.Sum(data)
md5str := fmt.Sprintf("%x", has)
return md5str
}