-
Notifications
You must be signed in to change notification settings - Fork 126
/
validation.go
103 lines (92 loc) · 2.32 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
98
99
100
101
102
103
package kusama
import (
"encoding/hex"
"fmt"
"strings"
"github.com/MixinNetwork/mixin/crypto"
"github.com/btcsuite/btcutil/base58"
"golang.org/x/crypto/blake2b"
)
const (
addressPrefix = 2
)
var (
KusamaChainBase string
KusamaChainId crypto.Hash
)
func init() {
KusamaChainBase = "9d29e4f6-d67c-4c4b-9525-604b04afbe9f"
KusamaChainId = crypto.NewHash([]byte(KusamaChainBase))
}
func VerifyAssetKey(assetKey string) error {
if assetKey == KusamaChainBase {
return nil
}
return fmt.Errorf("invalid kusama asset key %s", assetKey)
}
func VerifyAddress(address string) error {
if strings.TrimSpace(address) != address {
return fmt.Errorf("invalid kusama address %s", address)
}
b := base58.Decode(address)
if len(b) < 8 {
return fmt.Errorf("invalid kusama address %s", address)
}
if b[0] != byte(addressPrefix) {
return fmt.Errorf("invalid kusama address %s", address)
}
addr := base58.Encode(b)
if addr != address {
return fmt.Errorf("invalid kusama address %s", address)
}
public := b[1 : len(b)-2]
addr, err := PublicKeyToAddress(public)
if err != nil {
return fmt.Errorf("invalid kusama address %s", address)
}
if addr != address {
return fmt.Errorf("invalid kusama address %s", address)
}
return nil
}
func VerifyTransactionHash(hash string) error {
if len(hash) != 66 {
return fmt.Errorf("invalid kusama transaction hash %s", hash)
}
if !strings.HasPrefix(hash, "0x") {
return fmt.Errorf("invalid kusama transaction hash %s", hash)
}
if strings.ToLower(hash) != hash {
return fmt.Errorf("invalid kusama transaction hash %s", hash)
}
h, err := hex.DecodeString(hash[2:])
if err != nil {
return fmt.Errorf("invalid kusama transaction hash %s %s", hash, err.Error())
}
if len(h) != 32 {
return fmt.Errorf("invalid kusama transaction hash %s", hash)
}
return nil
}
func GenerateAssetId(assetKey string) crypto.Hash {
switch assetKey {
case KusamaChainBase:
return KusamaChainId
default:
panic(assetKey)
}
}
var ss58Prefix = []byte("SS58PRE")
func PublicKeyToAddress(pub []byte) (string, error) {
enc := append([]byte{addressPrefix}, pub...)
hasher, err := blake2b.New(64, nil)
if err != nil {
return "", err
}
_, err = hasher.Write(append(ss58Prefix, enc...))
if err != nil {
return "", err
}
checksum := hasher.Sum(nil)
return base58.Encode(append(enc, checksum[:2]...)), nil
}