-
Notifications
You must be signed in to change notification settings - Fork 62
/
setup.go
217 lines (174 loc) · 5.39 KB
/
setup.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
package client
import (
"encoding/base64"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
"ethcontract"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/loomnetwork/go-loom/auth"
"github.com/loomnetwork/go-loom/client"
"github.com/loomnetwork/go-loom/client/plasma_cash"
"github.com/pkg/errors"
"github.com/spf13/viper"
loom "github.com/loomnetwork/go-loom"
loom_ethcontract "github.com/loomnetwork/go-loom/client/plasma_cash/eth/ethcontract"
"crypto/ecdsa"
"crypto/elliptic"
)
type TestContext struct {
Alice *Client
Bob *Client
Eve *Client
Mallory *Client
Charlie *Client
Dan *Client
Trudy *Client
Authority *Client
}
func getDAppchainTxSigner(name string) (auth.Signer, error) {
privFile := name + ".key"
privKeyB64, err := ioutil.ReadFile(privFile)
if err != nil {
return nil, err
}
privKey, err := base64.StdEncoding.DecodeString(string(privKeyB64))
if err != nil {
return nil, err
}
signer := auth.NewEd25519Signer(privKey)
return signer, nil
}
func getTokenContract(cfg *viper.Viper, name string, privKey *ecdsa.PrivateKey) (plasma_cash.TokenContract, error) {
tokenAddr := common.HexToAddress(cfg.GetString("token_contract"))
tokenContract, err := ethcontract.NewCards(tokenAddr, conn)
if err != nil {
return nil, errors.Wrapf(err, "Failed to instantiate a Token contract")
}
return NewTokenContract(name, privKey, tokenContract), nil
}
func getRootChain(cfg *viper.Viper, name string) (plasma_cash.RootChainClient, error) {
contractAddr := common.HexToAddress(cfg.GetString("root_chain"))
privKeyHexStr := cfg.GetString(name)
privKey, err := crypto.HexToECDSA(strings.TrimPrefix(privKeyHexStr, "0x"))
if err != nil {
return nil, errors.Wrapf(err, "failed to load private key for %s", name)
}
plasmaContract, err := loom_ethcontract.NewRootChain(contractAddr, conn)
if err != nil {
return nil, errors.Wrapf(err, "Failed to instantiate a Token contract")
}
return NewRootChainService(name, privKey, plasmaContract), nil
}
// Loads plasma-config.yml or equivalent from the cwd
func parseConfig() (*viper.Viper, error) {
// When running "go test" the cwd is set to the package dir, not the root dir
// where the config is, so gotta do a bit more work to figure out the config dir...
_, filename, _, _ := runtime.Caller(0)
cfgDir := filepath.Join(filepath.Dir(filename), "../..")
v := viper.New()
v.SetConfigName("plasma-config")
v.AddConfigPath(cfgDir)
err := v.ReadInConfig()
if err != nil {
return nil, err
}
return v, nil
}
func deriveAddressFromECPubKey(pubKey *ecdsa.PublicKey) loom.LocalAddress {
pubKeyInBinary := elliptic.Marshal(pubKey.Curve, pubKey.X, pubKey.Y)
return crypto.Keccak256(pubKeyInBinary[1:])[12:]
}
func setupClient(cfg *viper.Viper, addressMapper *AddressMapperClient, hostile bool, entityName, readUri, writeUri string) (*Client, error) {
signer, err := getDAppchainTxSigner(entityName)
if err != nil {
return nil, err
}
contractName := "plasmacash"
if hostile {
contractName = "hostileoperator"
}
chainServiceClient, err := client.NewPlasmaCashClient(contractName, signer, "default", writeUri, readUri)
if err != nil {
return nil, err
}
privKeyHexStr := cfg.GetString(entityName)
privKey, err := crypto.HexToECDSA(strings.TrimPrefix(privKeyHexStr, "0x"))
if err != nil {
return nil, err
}
from := loom.Address{
ChainID: "default",
Local: loom.LocalAddressFromPublicKey(signer.PublicKey()),
}
to := loom.Address{
ChainID: "eth",
Local: deriveAddressFromECPubKey(&privKey.PublicKey),
}
hasMappingResponse, err := addressMapper.HasMapping(from, from)
if err != nil {
return nil, err
}
if !hasMappingResponse.HasMapping {
err = addressMapper.AddIdentityMapping(from, to, signer, privKey)
if err != nil {
return nil, err
}
}
rootChainClient, err := getRootChain(cfg, entityName)
if err != nil {
return nil, err
}
tokenContract, err := getTokenContract(cfg, entityName, privKey)
if err != nil {
return nil, err
}
return NewClient(cfg, chainServiceClient, rootChainClient, tokenContract), nil
}
func SetupTest(hostile bool, readUri, writeUri string) (*TestContext, error) {
var err error
testCtx := TestContext{}
addressMapper, err := NewAddressMapperClient("default", writeUri, readUri)
if err != nil {
return nil, err
}
cfg, err := parseConfig()
if err != nil {
return nil, err
}
testCtx.Alice, err = setupClient(cfg, addressMapper, hostile, "alice", readUri, writeUri)
if err != nil {
return nil, err
}
testCtx.Bob, err = setupClient(cfg, addressMapper, hostile, "bob", readUri, writeUri)
if err != nil {
return nil, err
}
testCtx.Charlie, err = setupClient(cfg, addressMapper, hostile, "charlie", readUri, writeUri)
if err != nil {
return nil, err
}
testCtx.Dan, err = setupClient(cfg, addressMapper, hostile, "dan", readUri, writeUri)
if err != nil {
return nil, err
}
testCtx.Eve, err = setupClient(cfg, addressMapper, hostile, "eve", readUri, writeUri)
if err != nil {
return nil, err
}
testCtx.Mallory, err = setupClient(cfg, addressMapper, hostile, "mallory", readUri, writeUri)
if err != nil {
return nil, err
}
testCtx.Trudy, err = setupClient(cfg, addressMapper, hostile, "trudy", readUri, writeUri)
if err != nil {
return nil, err
}
testCtx.Authority, err = setupClient(cfg, addressMapper, hostile, "authority", readUri, writeUri)
if err != nil {
return nil, err
}
return &testCtx, nil
}