Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Generic Deposit CLI command and Generic Resources CLI fixes #283

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions chains/evm/cli/bridge/register-generic-resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ func BindRegisterGenericResourceFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&ResourceID, "resource", "", "Resource ID to query")
cmd.Flags().StringVar(&Bridge, "bridge", "", "Bridge contract address")
cmd.Flags().StringVar(&Target, "target", "", "Contract address or hash storage to be registered")
cmd.Flags().StringVar(&Deposit, "deposit", "0x00000000", "Deposit function signature")
cmd.Flags().StringVar(&Execute, "execute", "0x00000000", "Execute proposal function signature")
cmd.Flags().StringVar(&Deposit, "deposit", "00000000", "Deposit function signature")
cmd.Flags().Uint64Var(&DepositerOffset, "depositerOffset", 0, "Offset to find the bridge tx depositer address inside the metadata sent on a deposit")
cmd.Flags().StringVar(&Execute, "execute", "00000000", "Execute proposal function signature")
cmd.Flags().BoolVar(&Hash, "hash", false, "Treat signature inputs as function prototype strings, hash and take the first 4 bytes")
flags.MarkFlagsAsRequired(cmd, "handler", "resource", "bridge", "target")
}
Expand Down Expand Up @@ -103,25 +104,53 @@ func ProcessRegisterGenericResourceFlags(cmd *cobra.Command, args []string) erro
ResourceIdBytesArr = callsUtil.SliceTo32Bytes(resourceIdBytes)

if Hash {
// We must check whether both a deposit and execute function signature is provide or else
// an invalid hash of 0x00000000 will be taken and set as a function selector in the handler
DepositSigBytes = callsUtil.GetSolidityFunctionSig([]byte(Deposit))
ExecuteSigBytes = callsUtil.GetSolidityFunctionSig([]byte(Execute))
if Deposit == "00000000" {
DepositSigBytes = [4]byte{}
}
if Execute == "00000000" {
ExecuteSigBytes = [4]byte{}
}
} else {
copy(DepositSigBytes[:], []byte(Deposit)[:])
copy(ExecuteSigBytes[:], []byte(Execute)[:])
depositBytes, err := hex.DecodeString(Deposit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably use the DepositSigBytes variable here, and then omit the copy stuff

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean substituting depositBytes with DepositSigBytes from the hex.DecodeString call? Currently DepositSigBytes is type [4]byte whereas DecodeString returns []byte and can be used to assign a value to DepositSigBytes. This could probably still be done but would require slight changes to all other places that use DepositSigBytes

if err != nil {
return err
}
copy(DepositSigBytes[:], depositBytes[:])

executeBytes, err := hex.DecodeString(Execute)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above

if err != nil {
return err
}
copy(ExecuteSigBytes[:], executeBytes[:])
}

log.Debug().Msgf("DepositSigBytes: %x\n", DepositSigBytes[:])
log.Debug().Msgf("ExecuteSigBytes: %x\n", ExecuteSigBytes[:])

return nil
}

func RegisterGenericResource(cmd *cobra.Command, args []string, contract *bridge.BridgeContract) error {
log.Info().Msgf("Registering contract %s with resource ID %s on handler %s", TargetContractAddr, ResourceID, HandlerAddr)

depositerOffsetBigInt := new(big.Int).SetUint64(DepositerOffset)
log.Info().Msgf("handlerAddr: %s, resourceId: %s, targetcontract: %s, depositsigbytes: %s, depositerOffset: %s, executeSigBytes: %s",
HandlerAddr,
ResourceIdBytesArr,
TargetContractAddr,
string(DepositSigBytes[:]),
depositerOffsetBigInt,
string(DepositSigBytes[:]))
h, err := contract.AdminSetGenericResource(
HandlerAddr,
ResourceIdBytesArr,
TargetContractAddr,
DepositSigBytes,
big.NewInt(int64(DepositerOffset)),
depositerOffsetBigInt,
ExecuteSigBytes,
transactor.TransactOptions{GasLimit: gasLimit},
)
Expand Down
1 change: 1 addition & 0 deletions chains/evm/cli/centrifuge/centrifuge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ var CentrifugeCmd = &cobra.Command{
func init() {
CentrifugeCmd.AddCommand(deployCmd)
CentrifugeCmd.AddCommand(getHashCmd)
CentrifugeCmd.AddCommand(depositCmd)
}
118 changes: 118 additions & 0 deletions chains/evm/cli/centrifuge/deposit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package centrifuge

import (
"encoding/hex"
"fmt"

"github.com/ChainSafe/chainbridge-core/chains/evm/calls/contracts/bridge"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/evmtransaction"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor"
"github.com/ChainSafe/chainbridge-core/chains/evm/cli/flags"
"github.com/ChainSafe/chainbridge-core/chains/evm/cli/initialize"
"github.com/ChainSafe/chainbridge-core/chains/evm/cli/logger"
"github.com/ChainSafe/chainbridge-core/util"
"github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var depositCmd = &cobra.Command{
Use: "deposit",
Short: "Deposit a generic data hash",
Long: "The deposit subcommand creates a new generic data deposit on the bridge contract",
PreRun: func(cmd *cobra.Command, args []string) {
logger.LoggerMetadata(cmd.Name(), cmd.Flags())
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return util.CallPersistentPreRun(cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
c, err := initialize.InitializeClient(url, senderKeyPair)
if err != nil {
return err
}
t, err := initialize.InitializeTransactor(gasPrice, evmtransaction.NewTransaction, c, prepare)
if err != nil {
return err
}
return DepositCmd(cmd, args, bridge.NewBridgeContract(c, BridgeAddr, t))
},
Args: func(cmd *cobra.Command, args []string) error {
err := ValidateDepositFlags(cmd, args)
if err != nil {
return err
}

err = ProcessDepositFlags(cmd, args)
if err != nil {
return err
}
return nil
},
}

func init() {
BindDepositFlags(depositCmd)
}

func BindDepositFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&Recipient, "recipient", "", "Address of contract to receive generic data")
cmd.Flags().StringVar(&Bridge, "bridge", "", "Address of bridge contract")
cmd.Flags().StringVar(&Metadata, "metadata", "", "Data (hex bytes) representing params for previously registered functions. Params should be encoded as 32 bytes each")
cmd.Flags().Uint8Var(&DomainID, "domain", 0, "Destination domain ID")
cmd.Flags().StringVar(&ResourceID, "resource", "", "Resource ID for transfer")
cmd.Flags().StringVar(&Priority, "priority", "none", "Transaction priority speed")
flags.MarkFlagsAsRequired(cmd, "recipient", "bridge", "metadata", "domain", "resource")
}

func ValidateDepositFlags(cmd *cobra.Command, args []string) error {
if !common.IsHexAddress(Recipient) {
return fmt.Errorf("invalid recipient address %s", Recipient)
}
if !common.IsHexAddress(Bridge) {
return fmt.Errorf("invalid bridge address %s", Bridge)
}
switch Priority {
case "none", "slow", "medium", "fast":
return nil
default:
return fmt.Errorf("invalid priority value %s, supported priorities: \"slow|medium|fast\"", Priority)
}
}

func ProcessDepositFlags(cmd *cobra.Command, args []string) error {
var err error

RecipientAddr = common.HexToAddress(Recipient)
BridgeAddr = common.HexToAddress(Bridge)
ResourceIdBytesArr, err = flags.ProcessResourceID(ResourceID)
if err != nil {
return err
}

MetadataBytes, err = hex.DecodeString(Metadata)
if err != nil {
return err
}

return err
}

func DepositCmd(cmd *cobra.Command, args []string, bridgeContract *bridge.BridgeContract) error {
txHash, err := bridgeContract.GenericDeposit(
MetadataBytes, ResourceIdBytesArr, uint8(DomainID), transactor.TransactOptions{GasLimit: gasLimit, Priority: transactor.TxPriorities[Priority]},
)
if err != nil {
return err
}

log.Info().Msgf(
`Generic deposit hash: %s
%s metadata was transferred to %s from %s`,
txHash.Hex(),
Metadata,
RecipientAddr.Hex(),
senderKeyPair.CommonAddress().String(),
)
return nil
}
20 changes: 16 additions & 4 deletions chains/evm/cli/centrifuge/flagVars.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,37 @@ import (
"math/big"

"github.com/ChainSafe/chainbridge-core/crypto/secp256k1"
"github.com/ChainSafe/chainbridge-core/types"
"github.com/ethereum/go-ethereum/common"
)

//flag vars
var (
Hash string
Address string
Hash string
Address string
Metadata string
Recipient string
Bridge string
DomainID uint8
ResourceID string
Priority string
)

//processed flag vars
var (
StoreAddr common.Address
ByteHash [32]byte
StoreAddr common.Address
ByteHash [32]byte
MetadataBytes []byte
RecipientAddr common.Address
BridgeAddr common.Address
ResourceIdBytesArr types.ResourceID
)

// global flags
var (
url string
gasPrice *big.Int
gasLimit uint64
senderKeyPair *secp256k1.Keypair
prepare bool
)