|
1 | 1 | package clclient |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/ecdsa" |
| 5 | + "crypto/rand" |
4 | 6 | "crypto/tls" |
5 | 7 | "fmt" |
| 8 | + "github.com/ethereum/go-ethereum/accounts/keystore" |
| 9 | + "github.com/ethereum/go-ethereum/crypto" |
| 10 | + "github.com/pkg/errors" |
6 | 11 | "github.com/smartcontractkit/chainlink-testing-framework/framework" |
7 | 12 | "github.com/smartcontractkit/chainlink-testing-framework/framework/components/clnode" |
8 | 13 | "math/big" |
@@ -1233,3 +1238,73 @@ func (c *ChainlinkClient) GetForwarders() (*Forwarders, *http.Response, error) { |
1233 | 1238 | } |
1234 | 1239 | return response, resp.RawResponse, err |
1235 | 1240 | } |
| 1241 | + |
| 1242 | +func NewETHKey(password string) ([]byte, error) { |
| 1243 | + privateKey, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader) |
| 1244 | + if err != nil { |
| 1245 | + return nil, errors.Wrap(err, "failed to generate private key") |
| 1246 | + } |
| 1247 | + jsonKey, err := keystore.EncryptKey(&keystore.Key{ |
| 1248 | + PrivateKey: privateKey, |
| 1249 | + Address: crypto.PubkeyToAddress(privateKey.PublicKey), |
| 1250 | + }, password, keystore.StandardScryptN, keystore.StandardScryptP) |
| 1251 | + if err != nil { |
| 1252 | + return nil, errors.Wrap(err, "failed to encrypt the keystore") |
| 1253 | + } |
| 1254 | + return jsonKey, nil |
| 1255 | +} |
| 1256 | + |
| 1257 | +// ImportEVMKey imports EVM key to the node (encrypted go-ethereum JSON wallet format) |
| 1258 | +func (c *ChainlinkClient) ImportEVMKey(key []byte, chainID string) (*http.Response, error) { |
| 1259 | + framework.L.Info().Str(NodeURL, c.Config.URL).Str("Key", string(key)).Msg("Importing EVM key") |
| 1260 | + // empty response, nothing to marshal |
| 1261 | + resp, err := c.APIClient.R().SetBody(key).Post(fmt.Sprintf("/v2/keys/eth/import?evmChainID=%s", chainID)) |
| 1262 | + if err != nil { |
| 1263 | + return nil, err |
| 1264 | + } |
| 1265 | + return resp.RawResponse, err |
| 1266 | +} |
| 1267 | + |
| 1268 | +// ImportEVMKeys imports an array of EVM keys to the nodes |
| 1269 | +func ImportEVMKeys(cl []*ChainlinkClient, keys [][]byte, chainID string) error { |
| 1270 | + eg := &errgroup.Group{} |
| 1271 | + for i, c := range cl { |
| 1272 | + eg.Go(func() error { |
| 1273 | + _, err := c.ImportEVMKey(keys[i], chainID) |
| 1274 | + if err != nil { |
| 1275 | + return err |
| 1276 | + } |
| 1277 | + return nil |
| 1278 | + }) |
| 1279 | + } |
| 1280 | + return eg.Wait() |
| 1281 | +} |
| 1282 | + |
| 1283 | +// ImportP2PKey import P2P keys to the node (encrypted go-ethereum JSON wallet format, keystore.EncryptDataV3 + prefix) |
| 1284 | +func (c *ChainlinkClient) ImportP2PKey(encryptedJSONKey []byte) (*P2PKey, *http.Response, error) { |
| 1285 | + p2pKey := &P2PKey{} |
| 1286 | + framework.L.Info().Str(NodeURL, c.Config.URL).Msg("Importing P2P Key") |
| 1287 | + resp, err := c.APIClient.R(). |
| 1288 | + SetBody(encryptedJSONKey). |
| 1289 | + SetResult(p2pKey). |
| 1290 | + Post("/v2/keys/p2p/import") |
| 1291 | + if err != nil { |
| 1292 | + return nil, nil, err |
| 1293 | + } |
| 1294 | + return p2pKey, resp.RawResponse, err |
| 1295 | +} |
| 1296 | + |
| 1297 | +// ImportP2PKeys imports an array of P2P keys to the nodes |
| 1298 | +func ImportP2PKeys(cl []*ChainlinkClient, keys [][]byte) error { |
| 1299 | + eg := &errgroup.Group{} |
| 1300 | + for i, c := range cl { |
| 1301 | + eg.Go(func() error { |
| 1302 | + _, _, err := c.ImportP2PKey(keys[i]) |
| 1303 | + if err != nil { |
| 1304 | + return err |
| 1305 | + } |
| 1306 | + return nil |
| 1307 | + }) |
| 1308 | + } |
| 1309 | + return eg.Wait() |
| 1310 | +} |
0 commit comments