Skip to content

Commit

Permalink
fix: v2 superscript (#11146)
Browse files Browse the repository at this point in the history
* dereference only after parsing args
* fix sending key parsing
* fix public key parsing
  • Loading branch information
makramkd authored Nov 2, 2023
1 parent 627d698 commit f7e868e
Showing 1 changed file with 60 additions and 41 deletions.
101 changes: 60 additions & 41 deletions core/scripts/vrfv2/testnet/v2scripts/super_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"encoding/hex"
"flag"
"fmt"
"github.com/smartcontractkit/chainlink/core/scripts/common/vrf/constants"
"github.com/smartcontractkit/chainlink/core/scripts/common/vrf/jobs"
"github.com/smartcontractkit/chainlink/core/scripts/common/vrf/model"
"github.com/smartcontractkit/chainlink/core/scripts/common/vrf/util"
"math/big"
"os"
"strings"
Expand All @@ -18,6 +14,11 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/shopspring/decimal"

"github.com/smartcontractkit/chainlink/core/scripts/common/vrf/constants"
"github.com/smartcontractkit/chainlink/core/scripts/common/vrf/jobs"
"github.com/smartcontractkit/chainlink/core/scripts/common/vrf/model"
"github.com/smartcontractkit/chainlink/core/scripts/common/vrf/util"

helpers "github.com/smartcontractkit/chainlink/core/scripts/common"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/link_token_interface"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/vrf_coordinator_v2"
Expand All @@ -37,12 +38,12 @@ func DeployUniverseViaCLI(e helpers.Environment) {
deployCmd := flag.NewFlagSet("deploy-universe", flag.ExitOnError)

// required flags
linkAddress := *deployCmd.String("link-address", "", "address of link token")
linkEthAddress := *deployCmd.String("link-eth-feed", "", "address of link eth feed")
bhsContractAddressString := *deployCmd.String("bhs-address", "", "address of BHS contract")
batchBHSAddressString := *deployCmd.String("batch-bhs-address", "", "address of Batch BHS contract")
coordinatorAddressString := *deployCmd.String("coordinator-address", "", "address of VRF Coordinator contract")
batchCoordinatorAddressString := *deployCmd.String("batch-coordinator-address", "", "address Batch VRF Coordinator contract")
linkAddress := deployCmd.String("link-address", "", "address of link token")
linkEthAddress := deployCmd.String("link-eth-feed", "", "address of link eth feed")
bhsContractAddressString := deployCmd.String("bhs-address", "", "address of BHS contract")
batchBHSAddressString := deployCmd.String("batch-bhs-address", "", "address of Batch BHS contract")
coordinatorAddressString := deployCmd.String("coordinator-address", "", "address of VRF Coordinator contract")
batchCoordinatorAddressString := deployCmd.String("batch-coordinator-address", "", "address Batch VRF Coordinator contract")

subscriptionBalanceJuelsString := deployCmd.String("subscription-balance", constants.SubscriptionBalanceJuels, "amount to fund subscription")
nodeSendingKeyFundingAmount := deployCmd.String("sending-key-funding-amount", constants.NodeSendingKeyFundingAmount, "CL node sending key funding amount")
Expand Down Expand Up @@ -87,7 +88,10 @@ func DeployUniverseViaCLI(e helpers.Environment) {
ReqsForTier5: big.NewInt(*reqsForTier5),
}

vrfPrimaryNodeSendingKeys := strings.Split(*vrfPrimaryNodeSendingKeysString, ",")
var vrfPrimaryNodeSendingKeys []string
if len(*vrfPrimaryNodeSendingKeysString) > 0 {
vrfPrimaryNodeSendingKeys = strings.Split(*vrfPrimaryNodeSendingKeysString, ",")
}

nodesMap := make(map[string]model.Node)

Expand All @@ -100,14 +104,14 @@ func DeployUniverseViaCLI(e helpers.Environment) {
SendingKeyFundingAmount: fundingAmount,
}

bhsContractAddress := common.HexToAddress(bhsContractAddressString)
batchBHSAddress := common.HexToAddress(batchBHSAddressString)
coordinatorAddress := common.HexToAddress(coordinatorAddressString)
batchCoordinatorAddress := common.HexToAddress(batchCoordinatorAddressString)
bhsContractAddress := common.HexToAddress(*bhsContractAddressString)
batchBHSAddress := common.HexToAddress(*batchBHSAddressString)
coordinatorAddress := common.HexToAddress(*coordinatorAddressString)
batchCoordinatorAddress := common.HexToAddress(*batchCoordinatorAddressString)

contractAddresses := model.ContractAddresses{
LinkAddress: linkAddress,
LinkEthAddress: linkEthAddress,
LinkAddress: *linkAddress,
LinkEthAddress: *linkEthAddress,
BhsContractAddress: bhsContractAddress,
BatchBHSAddress: batchBHSAddress,
CoordinatorAddress: coordinatorAddress,
Expand Down Expand Up @@ -149,29 +153,32 @@ func VRFV2DeployUniverse(
batchFulfillmentEnabled bool,
nodesMap map[string]model.Node,
) model.JobSpecs {

// Put key in ECDSA format
if strings.HasPrefix(*registerKeyUncompressedPubKey, "0x") {
*registerKeyUncompressedPubKey = strings.Replace(*registerKeyUncompressedPubKey, "0x", "04", 1)
}

// Generate compressed public key and key hash
pubBytes, err := hex.DecodeString(*registerKeyUncompressedPubKey)
helpers.PanicErr(err)
pk, err := crypto.UnmarshalPubkey(pubBytes)
helpers.PanicErr(err)
var pkBytes []byte
if big.NewInt(0).Mod(pk.Y, big.NewInt(2)).Uint64() != 0 {
pkBytes = append(pk.X.Bytes(), 1)
} else {
pkBytes = append(pk.X.Bytes(), 0)
var compressedPkHex string
var keyHash common.Hash
if len(*registerKeyUncompressedPubKey) > 0 {
// Put key in ECDSA format
if strings.HasPrefix(*registerKeyUncompressedPubKey, "0x") {
*registerKeyUncompressedPubKey = strings.Replace(*registerKeyUncompressedPubKey, "0x", "04", 1)
}

// Generate compressed public key and key hash
pubBytes, err := hex.DecodeString(*registerKeyUncompressedPubKey)
helpers.PanicErr(err)
pk, err := crypto.UnmarshalPubkey(pubBytes)
helpers.PanicErr(err)
var pkBytes []byte
if big.NewInt(0).Mod(pk.Y, big.NewInt(2)).Uint64() != 0 {
pkBytes = append(pk.X.Bytes(), 1)
} else {
pkBytes = append(pk.X.Bytes(), 0)
}
var newPK secp256k1.PublicKey
copy(newPK[:], pkBytes)

compressedPkHex = hexutil.Encode(pkBytes)
keyHash, err = newPK.Hash()
helpers.PanicErr(err)
}
var newPK secp256k1.PublicKey
copy(newPK[:], pkBytes)

compressedPkHex := hexutil.Encode(pkBytes)
keyHash, err := newPK.Hash()
helpers.PanicErr(err)

if len(contractAddresses.LinkAddress) == 0 {
fmt.Println("\nDeploying LINK Token...")
Expand Down Expand Up @@ -268,7 +275,13 @@ func VRFV2DeployUniverse(
e.ChainID, //evmChainID
strings.Join(util.MapToAddressArr(nodesMap[model.VRFPrimaryNodeName].SendingKeys), "\",\""), //fromAddresses
contractAddresses.CoordinatorAddress,
nodesMap[model.VRFPrimaryNodeName].SendingKeys[0].Address,
func() string {
if keys := nodesMap[model.VRFPrimaryNodeName].SendingKeys; len(keys) > 0 {
return keys[0].Address
} else {
return common.HexToAddress("0x0").String()
}
}(),
contractAddresses.CoordinatorAddress,
contractAddresses.CoordinatorAddress,
)
Expand All @@ -283,7 +296,13 @@ func VRFV2DeployUniverse(
e.ChainID, //evmChainID
strings.Join(util.MapToAddressArr(nodesMap[model.VRFBackupNodeName].SendingKeys), "\",\""), //fromAddresses
contractAddresses.CoordinatorAddress,
nodesMap[model.VRFPrimaryNodeName].SendingKeys[0],
func() string {
if keys := nodesMap[model.VRFPrimaryNodeName].SendingKeys; len(keys) > 0 {
return keys[0].Address
} else {
return common.HexToAddress("0x0").String()
}
}(),
contractAddresses.CoordinatorAddress,
contractAddresses.CoordinatorAddress,
)
Expand Down

0 comments on commit f7e868e

Please sign in to comment.