-
Notifications
You must be signed in to change notification settings - Fork 45
/
models_chain_links.go
146 lines (113 loc) · 3.79 KB
/
models_chain_links.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package types
// DONTCOVER
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gogo/protobuf/proto"
"github.com/mr-tron/base58"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// --------------------------------------------------------------------------------------------------------------------
// AddressData is an interface representing a generic external chain address
type AddressData interface {
proto.Message
// Validate checks the validity of the AddressData
Validate() error
// GetValue returns the address value
GetValue() string
// VerifyPubKey verifies that the given public key is associated with this address data
VerifyPubKey(key cryptotypes.PubKey) (bool, error)
}
// --------------------------------------------------------------------------------------------------------------------
var _ AddressData = &Bech32Address{}
// Validate implements AddressData
func (b Bech32Address) Validate() error {
if strings.TrimSpace(b.Value) == "" {
return fmt.Errorf("value cannot be empty or blank")
}
if strings.TrimSpace(b.Prefix) == "" {
return fmt.Errorf("prefix cannot be empty or blank")
}
_, err := sdk.GetFromBech32(b.Value, b.Prefix)
if err != nil {
return fmt.Errorf("invalid Bech32 value or wrong prefix")
}
return nil
}
// GetValue implements AddressData
func (b Bech32Address) GetValue() string {
return b.Value
}
// VerifyPubKey implements AddressData
func (b Bech32Address) VerifyPubKey(key cryptotypes.PubKey) (bool, error) {
_, bz, err := bech32.DecodeAndConvert(b.Value)
if err != nil {
return false, err
}
return bytes.Equal(bz, key.Address().Bytes()), nil
}
// --------------------------------------------------------------------------------------------------------------------
var _ AddressData = &Base58Address{}
// Validate implements AddressData
func (b Base58Address) Validate() error {
if strings.TrimSpace(b.Value) == "" {
return fmt.Errorf("address cannot be empty or blank")
}
if _, err := base58.Decode(b.Value); err != nil {
return fmt.Errorf("invalid Base58 address")
}
return nil
}
// GetValue implements AddressData
func (b Base58Address) GetValue() string {
return b.Value
}
// VerifyPubKey implements AddressData
func (b Base58Address) VerifyPubKey(key cryptotypes.PubKey) (bool, error) {
bz, err := base58.Decode(b.Value)
return bytes.Equal(tmhash.SumTruncated(bz), key.Address().Bytes()), err
}
// --------------------------------------------------------------------------------------------------------------------
var _ AddressData = &HexAddress{}
// Validate implements AddressData
func (h HexAddress) Validate() error {
if strings.TrimSpace(h.Value) == "" {
return fmt.Errorf("value cannot be empty or blank")
}
if len(h.Value) <= len(h.Prefix) {
return fmt.Errorf("address cannot be smaller than prefix")
}
prefix, addrWithoutPrefix := h.Value[:len(h.Prefix)], h.Value[len(h.Prefix):]
if prefix != h.Prefix {
return fmt.Errorf("prefix does not match")
}
if _, err := hex.DecodeString(addrWithoutPrefix); err != nil {
return fmt.Errorf("invalid hex address")
}
return nil
}
// GetValue implements AddressData
func (h HexAddress) GetValue() string {
return h.Value
}
// VerifyPubKey implements AddressData
func (h HexAddress) VerifyPubKey(key cryptotypes.PubKey) (bool, error) {
addr := h.Value[len(h.Prefix):]
bz, err := hex.DecodeString(addr)
if err != nil {
return false, err
}
pubKey, err := btcec.ParsePubKey(key.Bytes())
if err != nil {
return false, err
}
uncompressedPubKey := pubKey.SerializeUncompressed()
return bytes.Equal(crypto.Keccak256(uncompressedPubKey[1:])[12:], bz), err
}