This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
msgs.go
84 lines (70 loc) · 2.94 KB
/
msgs.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
package types
import (
"encoding/json"
"fmt"
"strings"
gethCommon "github.com/ethereum/go-ethereum/common"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// MsgCreateEthBridgeClaim defines a message for creating claims on the ethereum bridge
type MsgCreateEthBridgeClaim EthBridgeClaim
// NewMsgCreateEthBridgeClaim is a constructor function for MsgCreateBridgeClaim
func NewMsgCreateEthBridgeClaim(ethBridgeClaim EthBridgeClaim) MsgCreateEthBridgeClaim {
return MsgCreateEthBridgeClaim(ethBridgeClaim)
}
// Route should return the name of the module
func (msg MsgCreateEthBridgeClaim) Route() string { return RouterKey }
// Type should return the action
func (msg MsgCreateEthBridgeClaim) Type() string { return "create_bridge_claim" }
// ValidateBasic runs stateless checks on the message
func (msg MsgCreateEthBridgeClaim) ValidateBasic() sdk.Error {
if msg.CosmosReceiver.Empty() {
return sdk.ErrInvalidAddress(msg.CosmosReceiver.String())
}
if msg.ValidatorAddress.Empty() {
return sdk.ErrInvalidAddress(msg.ValidatorAddress.String())
}
if msg.Nonce < 0 {
return ErrInvalidEthNonce(DefaultCodespace)
}
if !gethCommon.IsHexAddress(msg.EthereumSender.String()) {
return ErrInvalidEthAddress(DefaultCodespace)
}
if !gethCommon.IsHexAddress(msg.BridgeContractAddress.String()) {
return ErrInvalidEthAddress(DefaultCodespace)
}
if strings.ToLower(msg.Symbol) == "eth" && msg.TokenContractAddress != NewEthereumAddress("0x0000000000000000000000000000000000000000") {
return ErrInvalidEthSymbol(DefaultCodespace)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgCreateEthBridgeClaim) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return sdk.MustSortJSON(b)
}
// GetSigners defines whose signature is required
func (msg MsgCreateEthBridgeClaim) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.ValidatorAddress)}
}
// MapOracleClaimsToEthBridgeClaims maps a set of generic oracle claim data into EthBridgeClaim objects
func MapOracleClaimsToEthBridgeClaims(ethereumChainID int, bridgeContract EthereumAddress, nonce int, symbol string, tokenContract EthereumAddress, ethereumSender EthereumAddress, oracleValidatorClaims map[string]string, f func(int, EthereumAddress, int, string, EthereumAddress, EthereumAddress, sdk.ValAddress, string) (EthBridgeClaim, sdk.Error)) ([]EthBridgeClaim, sdk.Error) {
mappedClaims := make([]EthBridgeClaim, len(oracleValidatorClaims))
i := 0
for validatorBech32, validatorClaim := range oracleValidatorClaims {
validatorAddress, parseErr := sdk.ValAddressFromBech32(validatorBech32)
if parseErr != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse claim: %s", parseErr))
}
mappedClaim, err := f(ethereumChainID, bridgeContract, nonce, symbol, tokenContract, ethereumSender, validatorAddress, validatorClaim)
if err != nil {
return nil, err
}
mappedClaims[i] = mappedClaim
i++
}
return mappedClaims, nil
}