-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
task.vrfv2plus.go
157 lines (144 loc) · 5.49 KB
/
task.vrfv2plus.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
package pipeline
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"go.uber.org/multierr"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/vrf_coordinator_v2plus_interface"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/signatures/secp256k1"
"github.com/smartcontractkit/chainlink/v2/core/services/vrf/proof"
)
var (
vrfCoordinatorV2PlusABI = evmtypes.MustGetABI(vrf_coordinator_v2plus_interface.IVRFCoordinatorV2PlusInternalABI)
)
// VRFTaskV2Plus is identical to VRFTaskV2 except that it uses the V2Plus VRF
// request commitment, which includes a boolean indicating whether native or
// link payment was used.
type VRFTaskV2Plus struct {
BaseTask `mapstructure:",squash"`
PublicKey string `json:"publicKey"`
RequestBlockHash string `json:"requestBlockHash"`
RequestBlockNumber string `json:"requestBlockNumber"`
Topics string `json:"topics"`
keyStore VRFKeyStore
}
var _ Task = (*VRFTaskV2Plus)(nil)
func (t *VRFTaskV2Plus) Type() TaskType {
return TaskTypeVRFV2Plus
}
func (t *VRFTaskV2Plus) Run(_ context.Context, lggr logger.Logger, vars Vars, inputs []Result) (result Result, runInfo RunInfo) {
if len(inputs) != 1 {
return Result{Error: ErrWrongInputCardinality}, runInfo
}
if inputs[0].Error != nil {
return Result{Error: ErrInputTaskErrored}, runInfo
}
logValues, ok := inputs[0].Value.(map[string]interface{})
if !ok {
return Result{Error: errors.Wrap(ErrBadInput, "expected map input")}, runInfo
}
var (
pubKey BytesParam
requestBlockHash BytesParam
requestBlockNumber Uint64Param
topics HashSliceParam
)
err := multierr.Combine(
errors.Wrap(ResolveParam(&pubKey, From(VarExpr(t.PublicKey, vars))), "publicKey"),
errors.Wrap(ResolveParam(&requestBlockHash, From(VarExpr(t.RequestBlockHash, vars))), "requestBlockHash"),
errors.Wrap(ResolveParam(&requestBlockNumber, From(VarExpr(t.RequestBlockNumber, vars))), "requestBlockNumber"),
errors.Wrap(ResolveParam(&topics, From(VarExpr(t.Topics, vars))), "topics"),
)
if err != nil {
return Result{Error: err}, runInfo
}
requestKeyHash, ok := logValues["keyHash"].([32]byte)
if !ok {
return Result{Error: errors.Wrapf(ErrBadInput, "invalid keyHash")}, runInfo
}
requestPreSeed, ok := logValues["preSeed"].(*big.Int)
if !ok {
return Result{Error: errors.Wrapf(ErrBadInput, "invalid preSeed")}, runInfo
}
requestId, ok := logValues["requestId"].(*big.Int)
if !ok {
return Result{Error: errors.Wrapf(ErrBadInput, "invalid requestId")}, runInfo
}
subID, ok := logValues["subId"].(*big.Int)
if !ok {
return Result{Error: errors.Wrapf(ErrBadInput, "invalid subId")}, runInfo
}
callbackGasLimit, ok := logValues["callbackGasLimit"].(uint32)
if !ok {
return Result{Error: errors.Wrapf(ErrBadInput, "invalid callbackGasLimit")}, runInfo
}
numWords, ok := logValues["numWords"].(uint32)
if !ok {
return Result{Error: errors.Wrapf(ErrBadInput, "invalid numWords")}, runInfo
}
sender, ok := logValues["sender"].(common.Address)
if !ok {
return Result{Error: errors.Wrapf(ErrBadInput, "invalid sender")}, runInfo
}
extraArgs, ok := logValues["extraArgs"].([]byte)
if !ok {
return Result{Error: errors.Wrapf(ErrBadInput, "invalid extraArgs")}, runInfo
}
pk, err := secp256k1.NewPublicKeyFromBytes(pubKey)
if err != nil {
return Result{Error: fmt.Errorf("failed to create PublicKey from bytes %v", err)}, runInfo
}
pkh := pk.MustHash()
// Validate the key against the spec
if !bytes.Equal(requestKeyHash[:], pkh[:]) {
return Result{Error: fmt.Errorf("invalid key hash %v expected %v", hex.EncodeToString(requestKeyHash[:]), hex.EncodeToString(pkh[:]))}, runInfo
}
preSeed, err := proof.BigToSeed(requestPreSeed)
if err != nil {
return Result{Error: fmt.Errorf("unable to parse preseed %v", preSeed)}, runInfo
}
if len(requestBlockHash) != common.HashLength {
return Result{Error: fmt.Errorf("invalid BlockHash length %d expected %d", len(requestBlockHash), common.HashLength)}, runInfo
}
preSeedData := proof.PreSeedDataV2Plus{
PreSeed: preSeed,
BlockHash: common.BytesToHash(requestBlockHash),
BlockNum: uint64(requestBlockNumber),
SubId: subID,
CallbackGasLimit: callbackGasLimit,
NumWords: numWords,
Sender: sender,
ExtraArgs: extraArgs,
}
finalSeed := proof.FinalSeedV2Plus(preSeedData)
id := hexutil.Encode(pk[:])
p, err := t.keyStore.GenerateProof(id, finalSeed)
if err != nil {
return Result{Error: err}, retryableRunInfo()
}
onChainProof, rc, err := proof.GenerateProofResponseFromProofV2Plus(p, preSeedData)
if err != nil {
return Result{Error: err}, retryableRunInfo()
}
b, err := vrfCoordinatorV2PlusABI.Pack("fulfillRandomWords", onChainProof, rc)
if err != nil {
return Result{Error: err}, runInfo
}
results := make(map[string]interface{})
output := hexutil.Encode(b)
results["output"] = output
// RequestID needs to be a [32]byte for EvmTxMeta.
results["requestID"] = hexutil.Encode(requestId.Bytes())
// store vrf proof and request commitment separately so they can be used in a batch fashion
results["proof"] = onChainProof
results["requestCommitment"] = rc
lggr.Debugw("Completed VRF V2 task run", "reqID", requestId.String(), "output", output)
return Result{Value: results}, runInfo
}