Skip to content

Commit

Permalink
Merge pull request #249 from onflow/gregor/create-multi-key
Browse files Browse the repository at this point in the history
Multi-key account creation process improvements
  • Loading branch information
devbugging authored May 17, 2024
2 parents 313d201 + b1adab6 commit 6895668
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 68 deletions.
67 changes: 61 additions & 6 deletions bootstrap/create-multi-key-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package bootstrap

import (
"context"
"flag"
"fmt"
"strings"
"time"

"github.com/onflow/cadence"
json2 "github.com/onflow/cadence/encoding/json"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 0 additions & 58 deletions cmd/main/setup-multi-key-account.go

This file was deleted.

5 changes: 3 additions & 2 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ 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"
"github.com/onflow/go-ethereum/crypto"
"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
Expand Down

0 comments on commit 6895668

Please sign in to comment.