-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
script for manually fulfilling old VRF V2 requests #11731
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,15 @@ package main | |
import ( | ||
"bytes" | ||
"context" | ||
"crypto/rand" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"math/big" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
|
@@ -23,6 +27,8 @@ import ( | |
|
||
"github.com/jmoiron/sqlx" | ||
|
||
vrfutil "github.com/smartcontractkit/chainlink/core/scripts/common/vrf/util" | ||
|
||
helpers "github.com/smartcontractkit/chainlink/core/scripts/common" | ||
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets" | ||
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" | ||
|
@@ -43,6 +49,7 @@ import ( | |
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/blockhashstore" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/keystore" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/vrfkey" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/pg" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/vrf/proof" | ||
"github.com/smartcontractkit/chainlink/v2/core/utils" | ||
|
@@ -56,6 +63,121 @@ func main() { | |
e := helpers.SetupEnv(false) | ||
|
||
switch os.Args[1] { | ||
// "generate-proof" generates a proof for a given request ID and block number | ||
// using they VRF key from the given VRF node. It can optionally submit the | ||
// transaction on-chain. This scripts is useful when you want to manually fulfill | ||
// a request ID but do not already have a proof | ||
// this script exports key from the given node and decrypts it in memory | ||
// the exported key is not persisted locally | ||
case "generate-proof": | ||
cmd := flag.NewFlagSet("generate-proof", flag.ExitOnError) | ||
vrfNodeURL := cmd.String("vrf-node-url", "", "remote node URL") | ||
pubKey := cmd.String("compressed-pub-key", "", "compressed public key") | ||
requestBlock := cmd.Uint64("request-block", 0, "request block") | ||
requestID := cmd.String("request-id", "", "request ID") | ||
coordinatorAddress := cmd.String("coordinator-address", "", "request ID") | ||
executeTx := cmd.Bool("execute-tx", false, "if true, posts fulfillment transaction on-chain") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the use case for this script if you don't want to force fulfill the request? Just to generate the proof? Maybe we can call the script |
||
helpers.ParseArgs(cmd, os.Args[2:], "vrf-node-url", "compressed-pub-key", "request-block", "request-id", "coordinator-address") | ||
|
||
output := &bytes.Buffer{} | ||
client, _ := vrfutil.ConnectToNode(vrfNodeURL, output, nil) | ||
|
||
// generate password for encrypting the key | ||
randomBytes := make([]byte, 32) | ||
_, err := rand.Read(randomBytes) | ||
helpers.PanicErr(err) | ||
pw := base64.URLEncoding.EncodeToString(randomBytes) | ||
|
||
fmt.Println("Exporting vrf key...") | ||
resp, err := client.HTTP.Post("/v2/keys/vrf/export/"+*pubKey+"?newpassword="+pw, nil) | ||
helpers.PanicErr(err) | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
panic(fmt.Sprintf("invalid status code: %d", resp.StatusCode)) | ||
} | ||
|
||
keyJson, err := io.ReadAll(resp.Body) | ||
helpers.PanicErr(err) | ||
|
||
fmt.Println("Received vrf key") | ||
|
||
key, err := vrfkey.FromEncryptedJSON(keyJson, pw) | ||
helpers.PanicErr(err) | ||
|
||
logs, err := e.Ec.FilterLogs(context.Background(), ethereum.FilterQuery{ | ||
FromBlock: big.NewInt(0).SetUint64(*requestBlock), | ||
ToBlock: big.NewInt(0).SetUint64(*requestBlock), | ||
Addresses: []common.Address{ | ||
common.HexToAddress(*coordinatorAddress), | ||
}, | ||
Topics: [][]common.Hash{ | ||
{ | ||
vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested{}.Topic(), | ||
}, | ||
}, | ||
}) | ||
helpers.PanicErr(err) | ||
|
||
fmt.Println("Searching for request log...") | ||
|
||
coordinator, err := vrf_coordinator_v2.NewVRFCoordinatorV2( | ||
common.HexToAddress(*coordinatorAddress), | ||
e.Ec) | ||
helpers.PanicErr(err) | ||
|
||
var request *vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested | ||
for _, log := range logs { | ||
unpacked, err2 := coordinator.ParseLog(log) | ||
helpers.PanicErr(err2) | ||
rwr, ok := unpacked.(*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested) | ||
if !ok { | ||
// should never happen | ||
panic("cast to *vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested") | ||
} | ||
if rwr.RequestId.String() == *requestID { | ||
fmt.Printf("found request ID: %s\n", *requestID) | ||
request = rwr | ||
break | ||
} | ||
} | ||
|
||
if request == nil { | ||
panic(fmt.Sprintf("couldn't find request ID: %s in block: %d", *requestID, *requestBlock)) | ||
} | ||
|
||
ps, err := proof.BigToSeed(request.PreSeed) | ||
helpers.PanicErr(err) | ||
preSeedData := proof.PreSeedDataV2{ | ||
PreSeed: ps, | ||
BlockHash: request.Raw.BlockHash, | ||
BlockNum: *requestBlock, | ||
SubId: request.SubId, | ||
CallbackGasLimit: request.CallbackGasLimit, | ||
NumWords: request.NumWords, | ||
Sender: request.Sender, | ||
} | ||
|
||
fmt.Printf("preseed data : %+v\n", preSeedData) | ||
finalSeed := proof.FinalSeedV2(preSeedData) | ||
|
||
p, err := key.GenerateProof(finalSeed) | ||
helpers.PanicErr(err) | ||
|
||
proof, rc, err := proof.GenerateProofResponseFromProofV2(p, preSeedData) | ||
helpers.PanicErr(err) | ||
|
||
fmt.Printf("Proof: %+v, commitment: %+v\n", proof, rc) | ||
|
||
if *executeTx { | ||
fmt.Println("Sending fulfillment!") | ||
tx, err := coordinator.FulfillRandomWords(e.Owner, proof, rc) | ||
helpers.PanicErr(err) | ||
|
||
helpers.ConfirmTXMined(context.Background(), e.Ec, tx, e.ChainID, fmt.Sprintf("manual fulfillment of request ID: %s", *requestID)) | ||
} | ||
fmt.Println("done!") | ||
|
||
case "manual-fulfill": | ||
cmd := flag.NewFlagSet("manual-fulfill", flag.ExitOnError) | ||
// In order to get the tx data for a fulfillment transaction, you can grep the | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this script only be executed somewhere in a safe environment, e.g. on the CLL infrastructure with direct access to the CL node? Just a thought of precaution, if you can execute this from your laptop, then the private key for proofs might be exposed to in-memory attacks (AVTs or advanced volatile threats). BTW what is the recovery scenario in case the private key for generating proofs ever gets hijacked from the CL node, can we replace it with another one?