Skip to content

Commit 3bf1ac8

Browse files
authored
Import API + decoupled keys import example (#1644)
p2p/evm keys import example
1 parent 9d071c4 commit 3bf1ac8

File tree

8 files changed

+724
-293
lines changed

8 files changed

+724
-293
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
- [Framework](./framework/overview.md)
55
- [Getting Started](./framework/getting_started.md)
66
- [Your First Test](./framework/first_test.md)
7-
- [Connecting Chainlink Node](./framework/connecting_chainlink_node.md)
8-
- [Connecting Chainlink Node (Multiple networks)]()
97
- [NodeSet Environment](./framework/nodeset_environment.md)
8+
- [NodeSet Environment (Import Keys)](./framework/nodeset_environment_import.md)
109
- [NodeSet with Capabilities](./framework/nodeset_capabilities.md)
1110
- [NodeSet (Local Docker builds)](./framework/nodeset_docker_rebuild.md)
1211
- [NodeSet Compat Environment](./framework/nodeset_compatibility.md)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# NodeSet Environment (Import Keys)
2+
3+
If your tests are designed to run not just within a Docker environment but also with external infrastructure, and you want to reuse the test logic across different environments or securely store private keys, refer to the example below.
4+
5+
[Import Keys Example](https://github.com/smartcontractkit/chainlink-testing-framework/blob/main/framework/examples/myproject/smoke_import_keys_test.go)

framework/.changeset/v0.5.5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add CLClient API to import P2P and EVM keys for a node set + example

framework/clclient/client.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package clclient
22

33
import (
4+
"crypto/ecdsa"
5+
"crypto/rand"
46
"crypto/tls"
57
"fmt"
8+
"github.com/ethereum/go-ethereum/accounts/keystore"
9+
"github.com/ethereum/go-ethereum/crypto"
10+
"github.com/pkg/errors"
611
"github.com/smartcontractkit/chainlink-testing-framework/framework"
712
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/clnode"
813
"math/big"
@@ -1233,3 +1238,73 @@ func (c *ChainlinkClient) GetForwarders() (*Forwarders, *http.Response, error) {
12331238
}
12341239
return response, resp.RawResponse, err
12351240
}
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+
}

framework/examples/myproject/go.mod

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/smartcontractkit/chainlink-testing-framework/framework/examples
22

3-
go 1.23.1
4-
5-
toolchain go1.23.3
3+
go 1.23.3
64

75
replace (
86
github.com/smartcontractkit/chainlink-testing-framework/framework => ../../
@@ -22,6 +20,7 @@ require (
2220
github.com/smartcontractkit/chainlink-testing-framework/havoc v1.50.2
2321
github.com/smartcontractkit/chainlink-testing-framework/seth v1.50.10
2422
github.com/smartcontractkit/chainlink-testing-framework/wasp v1.50.2
23+
github.com/smartcontractkit/chainlink/v2 v2.20.0
2524
github.com/stretchr/testify v1.10.0
2625
github.com/testcontainers/testcontainers-go v0.35.0
2726
k8s.io/apimachinery v0.31.2
@@ -31,22 +30,22 @@ require (
3130

3231
require (
3332
dario.cat/mergo v1.0.1 // indirect
34-
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
35-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1 // indirect
36-
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1 // indirect
37-
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 // indirect
33+
filippo.io/edwards25519 v1.1.0 // indirect
34+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.13.0 // indirect
35+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0 // indirect
36+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
3837
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
39-
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1 // indirect
38+
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
4039
github.com/Masterminds/goutils v1.1.1 // indirect
4140
github.com/Masterminds/semver/v3 v3.3.0 // indirect
4241
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
4342
github.com/Microsoft/go-winio v0.6.2 // indirect
44-
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
43+
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect
4544
github.com/armon/go-metrics v0.4.1 // indirect
4645
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
4746
github.com/avast/retry-go v3.0.0+incompatible // indirect
4847
github.com/awalterschulze/gographviz v2.0.3+incompatible // indirect
49-
github.com/aws/aws-sdk-go v1.45.25 // indirect
48+
github.com/aws/aws-sdk-go v1.54.19 // indirect
5049
github.com/aws/aws-sdk-go-v2 v1.32.5 // indirect
5150
github.com/aws/aws-sdk-go-v2/config v1.28.4 // indirect
5251
github.com/aws/aws-sdk-go-v2/credentials v1.17.45 // indirect
@@ -61,10 +60,12 @@ require (
6160
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.4 // indirect
6261
github.com/aws/aws-sdk-go-v2/service/sts v1.33.0 // indirect
6362
github.com/aws/smithy-go v1.22.1 // indirect
63+
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 // indirect
6464
github.com/benbjohnson/clock v1.3.5 // indirect
6565
github.com/beorn7/perks v1.0.1 // indirect
6666
github.com/bits-and-blooms/bitset v1.13.0 // indirect
6767
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
68+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
6869
github.com/buger/jsonparser v1.1.1 // indirect
6970
github.com/bytedance/sonic v1.12.3 // indirect
7071
github.com/bytedance/sonic/loader v0.2.0 // indirect
@@ -112,22 +113,22 @@ require (
112113
github.com/go-logr/logr v1.4.2 // indirect
113114
github.com/go-logr/stdr v1.2.2 // indirect
114115
github.com/go-ole/go-ole v1.3.0 // indirect
115-
github.com/go-openapi/analysis v0.21.4 // indirect
116-
github.com/go-openapi/errors v0.20.4 // indirect
117-
github.com/go-openapi/jsonpointer v0.20.0 // indirect
118-
github.com/go-openapi/jsonreference v0.20.2 // indirect
119-
github.com/go-openapi/loads v0.21.2 // indirect
120-
github.com/go-openapi/spec v0.20.9 // indirect
121-
github.com/go-openapi/strfmt v0.21.7 // indirect
122-
github.com/go-openapi/swag v0.22.4 // indirect
123-
github.com/go-openapi/validate v0.22.1 // indirect
116+
github.com/go-openapi/analysis v0.22.2 // indirect
117+
github.com/go-openapi/errors v0.22.0 // indirect
118+
github.com/go-openapi/jsonpointer v0.20.2 // indirect
119+
github.com/go-openapi/jsonreference v0.20.4 // indirect
120+
github.com/go-openapi/loads v0.21.5 // indirect
121+
github.com/go-openapi/spec v0.20.14 // indirect
122+
github.com/go-openapi/strfmt v0.23.0 // indirect
123+
github.com/go-openapi/swag v0.22.9 // indirect
124+
github.com/go-openapi/validate v0.23.0 // indirect
124125
github.com/go-playground/locales v0.14.1 // indirect
125126
github.com/go-playground/universal-translator v0.18.1 // indirect
126127
github.com/go-playground/validator/v10 v10.22.1 // indirect
127128
github.com/go-redis/redis/v8 v8.11.5 // indirect
128129
github.com/goccy/go-json v0.10.3 // indirect
129130
github.com/gogo/googleapis v1.4.1 // indirect
130-
github.com/gogo/protobuf v1.3.2 // indirect
131+
github.com/gogo/protobuf v1.3.3 // indirect
131132
github.com/gogo/status v1.1.1 // indirect
132133
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
133134
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
@@ -145,29 +146,29 @@ require (
145146
github.com/grafana/grafana-foundation-sdk/go v0.0.0-20240326122733-6f96a993222b // indirect
146147
github.com/grafana/loki v1.6.2-0.20231215164305-b51b7d7b5503 // indirect
147148
github.com/grafana/loki/pkg/push v0.0.0-20231124142027-e52380921608 // indirect
148-
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect
149+
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
149150
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect
150-
github.com/hashicorp/consul/api v1.28.2 // indirect
151+
github.com/hashicorp/consul/api v1.29.2 // indirect
151152
github.com/hashicorp/errwrap v1.1.0 // indirect
152153
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
153-
github.com/hashicorp/go-hclog v1.5.0 // indirect
154+
github.com/hashicorp/go-hclog v1.6.3 // indirect
154155
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
155156
github.com/hashicorp/go-msgpack v0.5.5 // indirect
156157
github.com/hashicorp/go-multierror v1.1.1 // indirect
157158
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
158-
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
159+
github.com/hashicorp/go-sockaddr v1.0.6 // indirect
159160
github.com/hashicorp/golang-lru v0.6.0 // indirect
160161
github.com/hashicorp/memberlist v0.5.0 // indirect
161162
github.com/hashicorp/serf v0.10.1 // indirect
162163
github.com/holiman/uint256 v1.3.1 // indirect
163-
github.com/huandu/xstrings v1.3.3 // indirect
164+
github.com/huandu/xstrings v1.4.0 // indirect
164165
github.com/imdario/mergo v0.3.16 // indirect
165166
github.com/jmespath/go-jmespath v0.4.0 // indirect
166167
github.com/josharian/intern v1.0.0 // indirect
167168
github.com/jpillora/backoff v1.0.0 // indirect
168169
github.com/json-iterator/go v1.1.12 // indirect
169170
github.com/julienschmidt/httprouter v1.3.0 // indirect
170-
github.com/klauspost/compress v1.17.9 // indirect
171+
github.com/klauspost/compress v1.17.11 // indirect
171172
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
172173
github.com/kylelemons/godebug v1.1.0 // indirect
173174
github.com/leodido/go-urn v1.4.0 // indirect
@@ -176,11 +177,11 @@ require (
176177
github.com/mailru/easyjson v0.7.7 // indirect
177178
github.com/mattn/go-colorable v0.1.13 // indirect
178179
github.com/mattn/go-isatty v0.0.20 // indirect
179-
github.com/miekg/dns v1.1.56 // indirect
180-
github.com/mitchellh/copystructure v1.0.0 // indirect
180+
github.com/miekg/dns v1.1.61 // indirect
181+
github.com/mitchellh/copystructure v1.2.0 // indirect
181182
github.com/mitchellh/go-homedir v1.1.0 // indirect
182183
github.com/mitchellh/mapstructure v1.5.0 // indirect
183-
github.com/mitchellh/reflectwalk v1.0.1 // indirect
184+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
184185
github.com/mmcloughlin/addchain v0.4.0 // indirect
185186
github.com/moby/docker-image-spec v1.3.1 // indirect
186187
github.com/moby/patternmatcher v0.6.0 // indirect
@@ -202,38 +203,38 @@ require (
202203
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
203204
github.com/opentracing/opentracing-go v1.2.0 // indirect
204205
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
205-
github.com/pierrec/lz4/v4 v4.1.21 // indirect
206206
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
207207
github.com/pkg/errors v0.9.1 // indirect
208208
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
209209
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
210-
github.com/prometheus/alertmanager v0.26.0 // indirect
210+
github.com/prometheus/alertmanager v0.27.0 // indirect
211211
github.com/prometheus/client_golang v1.20.5 // indirect
212212
github.com/prometheus/client_model v0.6.1 // indirect
213-
github.com/prometheus/common v0.60.0 // indirect
213+
github.com/prometheus/common v0.60.1 // indirect
214214
github.com/prometheus/common/sigv4 v0.1.0 // indirect
215-
github.com/prometheus/exporter-toolkit v0.10.1-0.20230714054209-2f4150c63f97 // indirect
215+
github.com/prometheus/exporter-toolkit v0.11.0 // indirect
216216
github.com/prometheus/procfs v0.15.1 // indirect
217-
github.com/prometheus/prometheus v0.47.2-0.20231010075449-4b9c19fe5510 // indirect
217+
github.com/prometheus/prometheus v0.54.1 // indirect
218218
github.com/robfig/cron/v3 v3.0.1 // indirect
219-
github.com/rogpeppe/go-internal v1.13.1 // indirect
220219
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
221220
github.com/sercand/kuberesolver/v5 v5.1.1 // indirect
222221
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
223-
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
222+
github.com/shirou/gopsutil/v3 v3.24.3 // indirect
224223
github.com/shoenig/go-m1cpu v0.1.6 // indirect
225224
github.com/shopspring/decimal v1.4.0 // indirect
226225
github.com/sirupsen/logrus v1.9.3 // indirect
226+
github.com/smartcontractkit/chainlink-common v0.4.2-0.20250121141917-62443f4b3c30 // indirect
227227
github.com/smartcontractkit/chainlink-testing-framework/lib/grafana v1.50.0 // indirect
228+
github.com/smartcontractkit/libocr v0.0.0-20241223215956-e5b78d8e3919 // indirect
228229
github.com/soheilhy/cmux v0.1.5 // indirect
229230
github.com/sony/gobreaker v0.5.0 // indirect
230231
github.com/spf13/cast v1.6.0 // indirect
231232
github.com/spf13/pflag v1.0.5 // indirect
232233
github.com/stretchr/objx v0.5.2 // indirect
233234
github.com/supranational/blst v0.3.13 // indirect
234-
github.com/tidwall/gjson v1.14.4 // indirect
235+
github.com/tidwall/gjson v1.17.0 // indirect
235236
github.com/tidwall/match v1.1.1 // indirect
236-
github.com/tidwall/pretty v1.2.0 // indirect
237+
github.com/tidwall/pretty v1.2.1 // indirect
237238
github.com/tklauser/go-sysconf v0.3.12 // indirect
238239
github.com/tklauser/numcpus v0.6.1 // indirect
239240
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
@@ -242,14 +243,14 @@ require (
242243
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
243244
github.com/ugorji/go/codec v1.2.12 // indirect
244245
github.com/x448/float16 v0.8.4 // indirect
245-
github.com/yusufpapurcu/wmi v1.2.3 // indirect
246+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
246247
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
247248
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
248249
go.etcd.io/etcd/client/v3 v3.5.14 // indirect
249-
go.mongodb.org/mongo-driver v1.12.0 // indirect
250-
go.opentelemetry.io/collector/pdata v1.0.0-rcv0015 // indirect
251-
go.opentelemetry.io/collector/semconv v0.81.0 // indirect
252-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
250+
go.mongodb.org/mongo-driver v1.15.0 // indirect
251+
go.opentelemetry.io/collector/pdata v1.12.0 // indirect
252+
go.opentelemetry.io/collector/semconv v0.105.0 // indirect
253+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect
253254
go.opentelemetry.io/otel v1.32.0 // indirect
254255
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0 // indirect
255256
go.opentelemetry.io/otel/metric v1.32.0 // indirect
@@ -263,7 +264,7 @@ require (
263264
go4.org/netipx v0.0.0-20230125063823-8449b0a6169f // indirect
264265
golang.org/x/arch v0.11.0 // indirect
265266
golang.org/x/crypto v0.32.0 // indirect
266-
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
267+
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
267268
golang.org/x/mod v0.21.0 // indirect
268269
golang.org/x/net v0.30.0 // indirect
269270
golang.org/x/oauth2 v0.24.0 // indirect
@@ -292,3 +293,6 @@ require (
292293
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
293294
sigs.k8s.io/yaml v1.4.0 // indirect
294295
)
296+
297+
// replicating the replace directive on cosmos SDK
298+
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

0 commit comments

Comments
 (0)