-
Notifications
You must be signed in to change notification settings - Fork 127
/
validation.go
97 lines (87 loc) 路 2.47 KB
/
validation.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package etc
import (
"encoding/hex"
"fmt"
"strings"
"github.com/MixinNetwork/mixin/crypto"
"github.com/ethereum/go-ethereum/common"
)
var (
EthereumClassicChainBase string
EthereumClassicChainId crypto.Hash
)
func init() {
EthereumClassicChainBase = "2204c1ee-0ea2-4add-bb9a-b3719cfff93a"
EthereumClassicChainId = crypto.NewHash([]byte(EthereumClassicChainBase))
}
func VerifyAssetKey(assetKey string) error {
if assetKey == "0x0000000000000000000000000000000000000000" {
return nil
}
return fmt.Errorf("invalid ethereum classic asset key %s", assetKey)
}
func VerifyAddress(address string) error {
if strings.TrimSpace(address) != address {
return fmt.Errorf("invalid ethereum classic address %s", address)
}
if len(address) != 42 {
return fmt.Errorf("invalid ethereum classic address %s", address)
}
if !strings.HasPrefix(address, "0x") {
return fmt.Errorf("invalid ethereum classic address %s", address)
}
form, err := formatAddress(address)
if err != nil {
return fmt.Errorf("invalid ethereum classic address %s", address)
}
if form != address {
return fmt.Errorf("invalid ethereum classic address %s", address)
}
a, err := hex.DecodeString(address[2:])
if err != nil {
return fmt.Errorf("invalid ethereum classic address %s %s", address, err.Error())
}
if len(a) != 20 {
return fmt.Errorf("invalid ethereum classic address %s", address)
}
return nil
}
func VerifyTransactionHash(hash string) error {
if len(hash) != 66 {
return fmt.Errorf("invalid ethereum classic transaction hash %s", hash)
}
if !strings.HasPrefix(hash, "0x") {
return fmt.Errorf("invalid ethereum classic transaction hash %s", hash)
}
if strings.ToLower(hash) != hash {
return fmt.Errorf("invalid ethereum classic transaction hash %s", hash)
}
h, err := hex.DecodeString(hash[2:])
if err != nil {
return fmt.Errorf("invalid ethereum classic transaction hash %s %s", hash, err.Error())
}
if len(h) != 32 {
return fmt.Errorf("invalid ethereum classic transaction hash %s", hash)
}
return nil
}
func GenerateAssetId(assetKey string) crypto.Hash {
err := VerifyAssetKey(assetKey)
if err != nil {
panic(assetKey)
}
if assetKey == "0x0000000000000000000000000000000000000000" {
return EthereumClassicChainId
}
panic(assetKey)
}
func formatAddress(to string) (string, error) {
var bytesto [20]byte
_bytesto, err := hex.DecodeString(to[2:])
if err != nil {
return "", err
}
copy(bytesto[:], _bytesto)
address := common.Address(bytesto)
return address.Hex(), nil
}