Skip to content

Commit

Permalink
Merge pull request #48 from onflow/chasefleming/init-path
Browse files Browse the repository at this point in the history
Allow for private key to be created in a target directory
  • Loading branch information
chasefleming authored May 14, 2024
2 parents 6afebc0 + 32f9589 commit 2f09f4a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 26 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 @@ -75,11 +75,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 @@ -265,11 +268,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
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,6 @@ github.com/onflow/atree v0.1.0-beta1.0.20211027184039-559ee654ece9/go.mod h1:+6x
github.com/onflow/atree v0.6.0 h1:j7nQ2r8npznx4NX39zPpBYHmdy45f4xwoi+dm37Jk7c=
github.com/onflow/atree v0.6.0/go.mod h1:gBHU0M05qCbv9NN0kijLWMgC47gHVNBIp4KmsVFi0tc=
github.com/onflow/cadence v0.20.1/go.mod h1:7mzUvPZUIJztIbr9eTvs+fQjWWHTF8veC+yk4ihcNIA=
github.com/onflow/cadence v0.42.9 h1:EX+eak/Jjy9PyKcAEmOViGOHMyP/nCOwJO+deodZlJE=
github.com/onflow/cadence v0.42.9/go.mod h1:1wFd+LiNiN6qoZXof3MBdpM6d8BsxbVIxOA77LbIYmE=
github.com/onflow/cadence v0.42.10 h1:3oC5ceeXhdCrhHcf9H0yYXQKW3Tw/vkSXLe+PUZa4i0=
github.com/onflow/cadence v0.42.10/go.mod h1:1wFd+LiNiN6qoZXof3MBdpM6d8BsxbVIxOA77LbIYmE=
github.com/onflow/crypto v0.25.1 h1:0txy2PKPMM873JbpxQNbJmuOJtD56bfs48RQfm0ts5A=
Expand All @@ -714,19 +712,13 @@ github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20231016154253-a
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20231016154253-a00dbf7c061f/go.mod h1:jM6GMAL+m0hjusUgiYDNrixPQ6b9s8xjoJQoEu5bHQI=
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.4-0.20231016154253-a00dbf7c061f h1:Ep+Mpo2miWMe4pjPGIaEvEzshRep30dvNgxqk+//FrQ=
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.4-0.20231016154253-a00dbf7c061f/go.mod h1:ZeLxwaBkzuSInESGjL8/IPZWezF+YOYsYbMrZlhN+q4=
github.com/onflow/flow-emulator v0.62.0 h1:3b3NtE6LL8CVaQFjdkaoDWfLMDjvmLvT30sBsOLJyg0=
github.com/onflow/flow-emulator v0.62.0/go.mod h1:Mn2cszC79c+GIM3b/UdD80RF1k/mzuvKQ5ATuAZQ7vo=
github.com/onflow/flow-emulator v0.62.1 h1:VivECYftZbaIo9oZBVsmeE/LUcxdx7hJJ1ET2rGY8p8=
github.com/onflow/flow-emulator v0.62.1/go.mod h1:JbvxBZ39Q8hOQ5fSwuV0ZJwfaiQeaYTiIY04ws8LcNE=
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20230711213910-baad011d2b13 h1:B4ll7e3j+MqTJv2122Enq3RtDNzmIGRu9xjV7fo7un0=
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20230711213910-baad011d2b13/go.mod h1:kTMFIySzEJJeupk+7EmXs0EJ6CBWY/MV9fv9iYQk+RU=
github.com/onflow/flow-go v0.33.2-0.20240411115514-7b9997bb6c14 h1:aoFvroi2SKQTdHrfcotcLkBj2fON37RZyzw9AbyFzVI=
github.com/onflow/flow-go v0.33.2-0.20240411115514-7b9997bb6c14/go.mod h1:D1n4GAHosso05bqGSreYZa/Cr4ywKjBIQbjTmRMhE7E=
github.com/onflow/flow-go v0.33.2-0.20240412174857-015156b297b5 h1:2785d9unR8CpzJzRQcMz+yjKKRmqN+Kbvu0gKdy6iT4=
github.com/onflow/flow-go v0.33.2-0.20240412174857-015156b297b5/go.mod h1:FkonSWZ60bOtc5MdH+CRHScza34k+uNpa6GwXnRpUvg=
github.com/onflow/flow-go-sdk v0.24.0/go.mod h1:IoptMLPyFXWvyd9yYA6/4EmSeeozl6nJoIv4FaEMg74=
github.com/onflow/flow-go-sdk v0.46.0 h1:mrIQziCDe6Oi5HH/aPFvYluh1XUwO6lYpoXLWrBZc2s=
github.com/onflow/flow-go-sdk v0.46.0/go.mod h1:azVWF0yHI8wT1erF0vuYGqQZybl6Frbc+0Zu3rIPeHc=
github.com/onflow/flow-go-sdk v0.46.2 h1:ypVGBeH9m5XpBOTU/CYVC0y/+z42e8mhUlq5aLiD24A=
github.com/onflow/flow-go-sdk v0.46.2/go.mod h1:tfLjB9FZmwqtT5gaacjvpIhz7KCd67YPm6v+iqYAjEA=
github.com/onflow/flow-go/crypto v0.21.3/go.mod h1:vI6V4CY3R6c4JKBxdcRiR/AnjBfL8OSD97bJc60cLuQ=
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 2f09f4a

Please sign in to comment.