/
models_chain_links.go
293 lines (235 loc) · 7.81 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package types
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/gogo/protobuf/proto"
"github.com/mr-tron/base58"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewChainConfig allows to build a new ChainConfig instance
func NewChainConfig(name string) ChainConfig {
return ChainConfig{
Name: name,
}
}
// Validate checks the validity of the ChainConfig
func (c ChainConfig) Validate() error {
if strings.TrimSpace(c.Name) == "" {
return fmt.Errorf("chain name cannot be empty or blank")
}
if c.Name != strings.ToLower(c.Name) {
return fmt.Errorf("chain name must be lowercase")
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// NewProof is a constructor function for Proof
// nolint:interfacer
func NewProof(pubKey cryptotypes.PubKey, signature string, plainText string) Proof {
pubKeyAny, err := codectypes.NewAnyWithValue(pubKey)
if err != nil {
panic("failed to pack public key to any type")
}
return Proof{
PubKey: pubKeyAny,
Signature: signature,
PlainText: plainText,
}
}
// Validate checks the validity of the Proof
func (p Proof) Validate() error {
if p.PubKey == nil {
return fmt.Errorf("public key field cannot be nil")
}
_, err := hex.DecodeString(p.Signature)
if err != nil {
return fmt.Errorf("invalid hex-encoded signature")
}
if strings.TrimSpace(p.PlainText) == "" {
return fmt.Errorf("plain text cannot be empty or blank")
}
_, err = hex.DecodeString(p.PlainText)
if err != nil {
return fmt.Errorf("invalid hex-encoded plain text")
}
return nil
}
// Verify verifies the signature using the given plain text and public key.
// It returns and error if something is invalid.
func (p Proof) Verify(unpacker codectypes.AnyUnpacker, address AddressData) error {
var pubkey cryptotypes.PubKey
err := unpacker.UnpackAny(p.PubKey, &pubkey)
if err != nil {
return fmt.Errorf("failed to unpack the public key")
}
value, _ := hex.DecodeString(p.PlainText)
sig, _ := hex.DecodeString(p.Signature)
if !pubkey.VerifySignature(value, sig) {
return fmt.Errorf("failed to verify the signature")
}
valid, err := address.VerifyPubKey(pubkey)
if err != nil {
return err
}
if !valid {
return fmt.Errorf("invalid address and public key combination provided")
}
return nil
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (p *Proof) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var pubKey cryptotypes.PubKey
return unpacker.UnpackAny(p.PubKey, &pubKey)
}
// --------------------------------------------------------------------------------------------------------------------
// 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{}
// NewBech32Address returns a new Bech32Address instance
func NewBech32Address(value, prefix string) *Bech32Address {
return &Bech32Address{Value: value, Prefix: prefix}
}
// 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{}
// NewBase58Address returns a new Base58Address instance
func NewBase58Address(value string) *Base58Address {
return &Base58Address{Value: value}
}
// 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
}
// --------------------------------------------------------------------------------------------------------------------
// UnpackAddressData deserializes the given any type value as an address data using the provided unpacker
func UnpackAddressData(unpacker codectypes.AnyUnpacker, addressAny *codectypes.Any) (AddressData, error) {
var address AddressData
if err := unpacker.UnpackAny(addressAny, &address); err != nil {
return nil, err
}
return address, nil
}
// --------------------------------------------------------------------------------------------------------------------
// NewChainLink returns a new ChainLink instance
// nolint:interfacer
func NewChainLink(user string, address AddressData, proof Proof, chainConfig ChainConfig, creationTime time.Time) ChainLink {
addressAny, err := codectypes.NewAnyWithValue(address)
if err != nil {
panic("failed to pack address data to any type")
}
return ChainLink{
User: user,
Address: addressAny,
Proof: proof,
ChainConfig: chainConfig,
CreationTime: creationTime,
}
}
// GetAddressData returns the AddressData associated with this chain link
func (link ChainLink) GetAddressData() AddressData {
return link.Address.GetCachedValue().(AddressData)
}
// Validate checks the validity of the ChainLink
func (link ChainLink) Validate() error {
if _, err := sdk.AccAddressFromBech32(link.User); err != nil {
return fmt.Errorf("invalid creator address: %s", link.User)
}
if link.Address == nil {
return fmt.Errorf("address cannot be nil")
}
err := link.Proof.Validate()
if err != nil {
return err
}
err = link.ChainConfig.Validate()
if err != nil {
return err
}
if link.CreationTime.IsZero() {
return fmt.Errorf("creation time cannot be zero")
}
return nil
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (link *ChainLink) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if link.Address != nil {
var address AddressData
err := unpacker.UnpackAny(link.Address, &address)
if err != nil {
return err
}
}
err := link.Proof.UnpackInterfaces(unpacker)
if err != nil {
return err
}
return nil
}
// MustMarshalChainLink serializes the given chain link using the provided BinaryCodec
func MustMarshalChainLink(cdc codec.BinaryCodec, link ChainLink) []byte {
return cdc.MustMarshal(&link)
}
// MustUnmarshalChainLink deserializes the given byte array as a chain link using
// the provided BinaryCodec
func MustUnmarshalChainLink(codec codec.BinaryCodec, bz []byte) ChainLink {
var link ChainLink
codec.MustUnmarshal(bz, &link)
return link
}