Skip to content

Commit

Permalink
Merge branch 'main' into cf/merge-main-5-14
Browse files Browse the repository at this point in the history
  • Loading branch information
chasefleming committed May 14, 2024
2 parents 89ec9a0 + 2f09f4a commit 5ca04c8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
14 changes: 8 additions & 6 deletions accounts/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package accounts
import (
"crypto/rand"
"fmt"
"path/filepath"
"strings"

"github.com/onflow/flow-go-sdk"
Expand All @@ -37,8 +38,8 @@ type Account struct {
}

// PrivateKeyFile returns the private key file name for an account.
func PrivateKeyFile(name string) string {
return fmt.Sprintf("%s.pkey", name)
func PrivateKeyFile(name, targetDirectory string) string {
return filepath.Join(targetDirectory, fmt.Sprintf("%s.pkey", name))
}

func FromConfig(conf *config.Config) (Accounts, error) {
Expand Down Expand Up @@ -91,14 +92,15 @@ func toConfig(account Account) config.Account {
}

// defaultEmulatorPrivateKeyFile returns the default emulator private key file name.
func defaultEmulatorPrivateKeyFile() string {
return PrivateKeyFile("emulator-account")
func defaultEmulatorPrivateKeyFile(targetDirectory string) string {
return PrivateKeyFile("emulator-account", targetDirectory)
}

func NewEmulatorAccount(
rw config.ReaderWriter,
sigAlgo crypto.SignatureAlgorithm,
hashAlgo crypto.HashAlgorithm,
targetDirectory string,
) (*Account, error) {
seed := make([]byte, crypto.MinSeedLength)
_, err := rand.Read(seed)
Expand All @@ -111,14 +113,14 @@ func NewEmulatorAccount(
return nil, fmt.Errorf("failed to generate emulator service key: %w", err)
}

if err := rw.WriteFile(defaultEmulatorPrivateKeyFile(), []byte(privateKey.String()), 0644); err != nil {
if err := rw.WriteFile(defaultEmulatorPrivateKeyFile(targetDirectory), []byte(privateKey.String()), 0644); err != nil {
return nil, fmt.Errorf("failed to write private key file: %w", err)
}

return &Account{
Name: config.DefaultEmulator.ServiceAccount,
Address: flow.ServiceAddress(flow.Emulator),
Key: NewFileKey(defaultEmulatorPrivateKeyFile(), 0, sigAlgo, hashAlgo, rw),
Key: NewFileKey(defaultEmulatorPrivateKeyFile(""), 0, sigAlgo, hashAlgo, rw),
}, nil
}

Expand Down
10 changes: 8 additions & 2 deletions flowkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ func newAccount(name string, address string, seed string) *accounts.Account {

func setup() (*State, Flowkit, *mocks.TestGateway) {
readerWriter, _ := tests.ReaderWriter()
state, err := Init(readerWriter, crypto.ECDSA_P256, crypto.SHA3_256)
state, err := Init(readerWriter)
if err != nil {
panic(err)
}

emulatorServiceAccount, _ := accounts.NewEmulatorAccount(readerWriter, crypto.ECDSA_P256, crypto.SHA3_256, "")
state.Accounts().AddOrUpdate(emulatorServiceAccount)

gw := mocks.DefaultMockGateway()
flowkit := Flowkit{
state: state,
Expand Down Expand Up @@ -264,11 +267,14 @@ func TestAccounts(t *testing.T) {

func setupIntegration() (*State, Flowkit) {
readerWriter, _ := tests.ReaderWriter()
state, err := Init(readerWriter, crypto.ECDSA_P256, crypto.SHA3_256)
state, err := Init(readerWriter)
if err != nil {
panic(err)
}

emulatorAccount, _ := accounts.NewEmulatorAccount(readerWriter, crypto.ECDSA_P256, crypto.SHA3_256, "")
state.Accounts().AddOrUpdate(emulatorAccount)

acc, _ := state.EmulatorServiceAccount()
pk, _ := acc.Key.PrivateKey()
gw := gateway.NewEmulatorGatewayWithOpts(&gateway.EmulatorKey{
Expand Down
9 changes: 1 addition & 8 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,22 +314,15 @@ func Load(configFilePaths []string, readerWriter ReaderWriter) (*State, error) {
// Init initializes a new Flow project.
func Init(
rw ReaderWriter,
sigAlgo crypto.SignatureAlgorithm,
hashAlgo crypto.HashAlgorithm,
) (*State, error) {
emulatorServiceAccount, err := accounts.NewEmulatorAccount(rw, sigAlgo, hashAlgo)
if err != nil {
return nil, err
}

loader := config.NewLoader(rw)
loader.AddConfigParser(json.NewParser())

return &State{
confLoader: loader,
readerWriter: rw,
conf: config.Default(),
accounts: &accounts.Accounts{*emulatorServiceAccount},
accounts: &accounts.Accounts{},
}, nil
}

Expand Down
6 changes: 5 additions & 1 deletion tests/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ const testAccountName = "test-account"
func initTestnet(t *testing.T) (gateway.Gateway, *flowkit.State, flowkit.Services, flowkit.ReaderWriter, afero.Fs) {
readerWriter, mockFs := ReaderWriter()

state, err := flowkit.Init(readerWriter, crypto.ECDSA_P256, crypto.SHA3_256)
state, err := flowkit.Init(readerWriter)
require.NoError(t, err)

emulatorServiceAccount, err := accounts2.NewEmulatorAccount(readerWriter, crypto.ECDSA_P256, crypto.SHA3_256, "")
require.NoError(t, err)
state.Accounts().AddOrUpdate(emulatorServiceAccount)

gw, err := gateway.NewGrpcGateway(config.TestnetNetwork)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion transactions/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestNew(t *testing.T) {
assert.Equal(t, proposer.Keys[index].Index, tx.FlowTransaction().ProposalKey.KeyIndex)

rw, _ := tests.ReaderWriter()
sig, _ := accounts.NewEmulatorAccount(rw, crypto.ECDSA_P256, crypto.SHA3_256)
sig, _ := accounts.NewEmulatorAccount(rw, crypto.ECDSA_P256, crypto.SHA3_256, "")
sig.Address = flow.HexToAddress("0x01")
err = tx.SetSigner(sig)
assert.NoError(t, err)
Expand Down

0 comments on commit 5ca04c8

Please sign in to comment.