-
Notifications
You must be signed in to change notification settings - Fork 28
/
bridge_owner.go
139 lines (114 loc) · 4.13 KB
/
bridge_owner.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
package zcnbridge
import (
"context"
"fmt"
eth "github.com/ethereum/go-ethereum"
"time"
"github.com/0chain/gosdk/zcnbridge/ethereum/authorizers"
hdw "github.com/0chain/gosdk/zcncore/ethhdwallet"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
func (b *BridgeOwner) prepareAuthorizers(ctx context.Context, method string, params ...interface{}) (*authorizers.Authorizers, *bind.TransactOpts, error) {
etherClient, err := b.CreateEthClient()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create etherClient")
}
// To (contract)
contractAddress := common.HexToAddress(b.AuthorizersAddress)
// Get ABI of the contract
abi, err := authorizers.AuthorizersMetaData.GetAbi()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get ABI")
}
// Pack the method argument
pack, err := abi.Pack(method, params...)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to pack arguments")
}
from := common.HexToAddress(b.EthereumAddress)
// Gas limits in units
gasLimitUnits, err := etherClient.EstimateGas(ctx, eth.CallMsg{
To: &contractAddress,
From: from,
Data: pack,
})
if err != nil {
return nil, nil, errors.Wrap(err, "failed to estimate gas")
}
// Update gas limits + 10%
gasLimitUnits = addPercents(gasLimitUnits, 10).Uint64()
transactOpts := b.CreateSignedTransactionFromKeyStore(etherClient, gasLimitUnits)
// Authorizers instance
authorizersInstance, err := authorizers.NewAuthorizers(contractAddress, etherClient)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create authorizers instance")
}
return authorizersInstance, transactOpts, nil
}
// AddEthereumAuthorizer Adds authorizer to Ethereum bridge. Only contract deployer can call this method
func (b *BridgeOwner) AddEthereumAuthorizer(ctx context.Context, address common.Address) (*types.Transaction, error) {
instance, transactOpts, err := b.prepareAuthorizers(ctx, "addAuthorizers", address)
if err != nil {
return nil, errors.Wrap(err, "failed to prepare bridge")
}
tran, err := instance.AddAuthorizers(transactOpts, address)
if err != nil {
msg := "failed to execute AddAuthorizers transaction to ClientID = %s with amount = %s"
return nil, errors.Wrapf(err, msg, b.ClientID(), address.String())
}
return tran, err
}
// RemoveEthereumAuthorizer Removes authorizer from Ethereum bridge. Only contract deployer can call this method
func (b *BridgeOwner) RemoveEthereumAuthorizer(ctx context.Context, address common.Address) (*types.Transaction, error) {
instance, transactOpts, err := b.prepareAuthorizers(ctx, "removeAuthorizers", address)
if err != nil {
return nil, errors.Wrap(err, "failed to prepare bridge")
}
tran, err := instance.RemoveAuthorizers(transactOpts, address)
if err != nil {
msg := "failed to execute RemoveAuthorizers transaction to ClientID = %s with amount = %s"
return nil, errors.Wrapf(err, msg, b.ClientID(), address.String())
}
return tran, err
}
func (b *BridgeOwner) AddEthereumAuthorizers(configDir string) {
cfg := viper.New()
cfg.AddConfigPath(configDir)
cfg.SetConfigName("authorizers")
if err := cfg.ReadInConfig(); err != nil {
fmt.Println(err)
return
}
mnemonics := cfg.GetStringSlice("authorizers")
for _, mnemonic := range mnemonics {
wallet, err := hdw.NewFromMnemonic(mnemonic)
if err != nil {
fmt.Printf("failed to read mnemonic: %v", err)
continue
}
pathD := hdw.MustParseDerivationPath("m/44'/60'/0'/0/0")
account, err := wallet.Derive(pathD, true)
if err != nil {
fmt.Println(err)
continue
}
transaction, err := b.AddEthereumAuthorizer(context.TODO(), account.Address)
if err != nil || transaction == nil {
fmt.Printf("AddAuthorizer error: %v, Address: %s", err, account.Address.Hex())
continue
}
status, err := ConfirmEthereumTransaction(transaction.Hash().String(), 100, time.Second*10)
if err != nil {
fmt.Println(err)
}
if status == 1 {
fmt.Printf("Authorizer has been added: %s\n", mnemonic)
} else {
fmt.Printf("Authorizer has failed to be added: %s\n", mnemonic)
}
}
}