/
main.go
135 lines (109 loc) · 3.28 KB
/
main.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
package main
// -------------------------------------------------------------
// Main (ebrelayer)
//
// Implements CLI commands for the Relayer service, such as
// initalization and event relay.
// -------------------------------------------------------------
import (
"encoding/hex"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client/rpc"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"
// "golang.org/x/crypto"
app "github.com/swishlabsco/cosmos-ethereum-bridge"
relayer "github.com/swishlabsco/cosmos-ethereum-bridge/cmd/ebrelayer/relayer"
)
const (
storeAcc = "acc"
routeEthbridge = "ethbridge"
)
var defaultCLIHome = os.ExpandEnv("$HOME/.ebcli")
var appCodec *amino.Codec
func init() {
// Read in the configuration file for the sdk
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub)
config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
config.Seal()
cdc := app.MakeCodec()
appCodec = cdc
// Construct Root Command
rootCmd.AddCommand(
rpc.StatusCommand(),
initRelayerCmd(),
)
executor := cli.PrepareMainCmd(rootCmd, "EBRELAYER", defaultCLIHome)
err := executor.Execute()
if err != nil {
panic(err)
}
}
var rootCmd = &cobra.Command{
Use: "ebrelayer",
Short: "Relayer service which listens for and relays ethereum smart contract events",
SilenceUsage: true,
}
func initRelayerCmd() *cobra.Command {
initRelayerCmd := &cobra.Command{
Use: "init chain-id web3-provider contract-address event-signature validatorFromName",
Short: "Initalizes a web socket which streams live events from a smart contract",
RunE: RunRelayerCmd,
}
return initRelayerCmd
}
func RunRelayerCmd(cmd *cobra.Command, args []string) error {
if len(args) != 5 {
return fmt.Errorf("Expected 5 arguments, got %v", len(args))
}
// Parse chain's ID
chainId := args[0]
if chainId == "" {
return fmt.Errorf("Invalid chain-id: %v", chainId)
}
// Parse ethereum provider
ethereumProvider := args[1]
if !relayer.IsWebsocketURL(ethereumProvider) {
return fmt.Errorf("Invalid web3-provider: %v", ethereumProvider)
}
// Parse the address of the deployed contract
bytesContractAddress, err := hex.DecodeString(args[2])
if err != nil {
return fmt.Errorf("Invalid contract-address: %v", bytesContractAddress)
}
contractAddress := common.BytesToAddress(bytesContractAddress)
// Parse the event signature for the subscription
eventSig := "0xe154a56f2d306d5bbe4ac2379cb0cfc906b23685047a2bd2f5f0a0e810888f72"
// eventSig := crypto.Keccak256Hash([]byte(args[3]))
if eventSig == "" {
return fmt.Errorf("Invalid event-signature: %v", eventSig)
}
// Parse the validator running the relayer service
validatorFrom := args[4]
// Initialize the relayer
initErr := relayer.InitRelayer(
appCodec,
chainId,
ethereumProvider,
contractAddress,
eventSig,
validatorFrom)
if initErr != nil {
fmt.Printf("%v", initErr)
return initErr
}
return nil
}
func main() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
os.Exit(0)
}