diff --git a/bootstrap/create-multi-key-account.go b/bootstrap/create-multi-key-account.go index 3e02ba848..405dd67df 100644 --- a/bootstrap/create-multi-key-account.go +++ b/bootstrap/create-multi-key-account.go @@ -2,8 +2,10 @@ package bootstrap import ( "context" + "flag" "fmt" "strings" + "time" "github.com/onflow/cadence" json2 "github.com/onflow/cadence/encoding/json" @@ -14,6 +16,50 @@ import ( "golang.org/x/exp/rand" ) +// RunCreateMultiKeyAccount command creates a new account with multiple keys, which are saved to keys.json for later +// use with running the gateway in a key-rotation mode (used with --coa-key-file flag). +func RunCreateMultiKeyAccount() { + var ( + keyCount int + keyFlag, addressFlag, hostFlag, ftFlag, flowFlag string + ) + + flag.IntVar(&keyCount, "key-count", 20, "how many keys you want to create and assign to account") + flag.StringVar(&keyFlag, "signer-key", "", "signer key used to create the new account") + flag.StringVar(&addressFlag, "signer-address", "", "signer address used to create new account") + flag.StringVar(&ftFlag, "ft-address", "0xee82856bf20e2aa6", "address of fungible token contract") + flag.StringVar(&flowFlag, "flow-token-address", "0x0ae53cb6e3f42a79", "address of flow token contract") + flag.StringVar(&hostFlag, "access-node-grpc-host", "localhost:3569", "host to the flow access node gRPC API") + + flag.Parse() + + key, err := crypto.DecodePrivateKeyHex(crypto.ECDSA_P256, keyFlag) + if err != nil { + panic(err) + } + + payer := flow.HexToAddress(addressFlag) + if payer == flow.EmptyAddress { + panic("invalid address") + } + + client, err := grpc.NewClient(hostFlag) + if err != nil { + panic(err) + } + + address, keys, err := CreateMultiKeyAccount(client, keyCount, payer, ftFlag, flowFlag, key) + if err != nil { + panic(err) + } + + fmt.Println("Address: ", address.Hex()) + fmt.Println("Keys:") + for _, pk := range keys { + fmt.Println(pk.String()) + } +} + /* CreateMultiKeyAccount is used to setup an account that can be used with key-rotation mechanism // todo parts of this are copied from flowkit and go-sdk/templates and should be refactored out once the package are migrated to Cadence 1.0 @@ -131,12 +177,21 @@ func CreateMultiKeyAccount( return nil, nil, err } - res, err := client.GetTransactionResult(context.Background(), tx.ID()) - if err != nil { - return nil, nil, err - } - if res.Error != nil { - return nil, nil, res.Error + var res *flow.TransactionResult + for { + res, err = client.GetTransactionResult(context.Background(), tx.ID()) + if err != nil { + return nil, nil, err + } + if res.Error != nil { + return nil, nil, res.Error + } + + if res.Status != flow.TransactionStatusPending { + break + } + + time.Sleep(2 * time.Second) } events := eventsFromTx(res) diff --git a/cmd/main/main.go b/cmd/main/main.go index e2beec256..0c3d4ad0d 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -3,14 +3,21 @@ package main import ( "context" "fmt" - "github.com/onflow/flow-evm-gateway/bootstrap" - "github.com/onflow/flow-evm-gateway/config" "os" "os/signal" "syscall" + + "github.com/onflow/flow-evm-gateway/bootstrap" + "github.com/onflow/flow-evm-gateway/config" ) func main() { + // create multi-key account + if _, exists := os.LookupEnv("MULTIKEY_MODE"); exists { + bootstrap.RunCreateMultiKeyAccount() + return + } + cfg, err := config.FromFlags() if err != nil { panic(err) diff --git a/cmd/main/setup-multi-key-account.go b/cmd/main/setup-multi-key-account.go deleted file mode 100644 index 430bf3b60..000000000 --- a/cmd/main/setup-multi-key-account.go +++ /dev/null @@ -1,58 +0,0 @@ -package main - -import ( - "flag" - "fmt" - - "github.com/onflow/flow-evm-gateway/bootstrap" - "github.com/onflow/flow-go-sdk" - "github.com/onflow/flow-go-sdk/access/grpc" - "github.com/onflow/flow-go-sdk/crypto" -) - -/* -* -This command creates a new account with multiple keys, which are saved to keys.json for later -use with running the gateway in a key-rotation mode (used with --coa-key-file flag). -*/ -func SetupKey() { - var ( - keyCount int - keyFlag, addressFlag, hostFlag, ftFlag, flowFlag string - ) - - flag.IntVar(&keyCount, "key-count", 20, "how many keys you want to create and assign to account") - flag.StringVar(&keyFlag, "signer-key", "", "signer key used to create the new account") - flag.StringVar(&addressFlag, "signer-address", "", "signer address used to create new account") - flag.StringVar(&ftFlag, "ft-address", "0xee82856bf20e2aa6", "address of fungible token contract") - flag.StringVar(&flowFlag, "flow-token-address", "0x0ae53cb6e3f42a79", "address of flow token contract") - flag.StringVar(&hostFlag, "access-node-grpc-host", "localhost:3569", "host to the flow access node gRPC API") - - flag.Parse() - - key, err := crypto.DecodePrivateKeyHex(crypto.ECDSA_P256, keyFlag) - if err != nil { - panic(key) - } - - payer := flow.HexToAddress(addressFlag) - if payer == flow.EmptyAddress { - panic("invalid address") - } - - client, err := grpc.NewClient(hostFlag) - if err != nil { - panic(err) - } - - address, keys, err := bootstrap.CreateMultiKeyAccount(client, keyCount, payer, flowFlag, ftFlag, key) - if err != nil { - panic(err) - } - - fmt.Println("Address: ", address.Hex()) - fmt.Println("Keys:") - for _, pk := range keys { - fmt.Println(pk.String()) - } -} diff --git a/tests/integration_test.go b/tests/integration_test.go index 1f25047e5..1607e3b26 100644 --- a/tests/integration_test.go +++ b/tests/integration_test.go @@ -9,8 +9,6 @@ import ( "testing" "time" - "github.com/onflow/flow-evm-gateway/bootstrap" - "github.com/onflow/flow-evm-gateway/config" "github.com/onflow/flow-go-sdk/access/grpc" "github.com/onflow/flow-go/fvm/evm/types" "github.com/onflow/go-ethereum/common" @@ -18,6 +16,9 @@ import ( "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/onflow/flow-evm-gateway/bootstrap" + "github.com/onflow/flow-evm-gateway/config" ) // Test_ConcurrentTransactionSubmission test submits multiple transactions concurrently