-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount.go
More file actions
89 lines (74 loc) · 2.58 KB
/
account.go
File metadata and controls
89 lines (74 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package splash
import (
"context"
"fmt"
"log"
"sort"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flowkit/v2"
"github.com/onflow/flowkit/v2/accounts"
)
func (c *Connector) CreateAccounts(ctx context.Context, saAccountName string) *Connector {
conn, err := c.CreateAccountsE(ctx, saAccountName)
if err != nil {
log.Fatal(err)
}
return conn
}
// CreateAccountsE ensures that all accounts present in the deployment block for the given network is present
func (c *Connector) CreateAccountsE(ctx context.Context, saAccountName string) (*Connector, error) {
p := c.State
signerAccount, err := p.Accounts().ByName(saAccountName)
if err != nil {
return nil, err
}
accountList := *p.AccountsForNetwork(c.Services.Network())
accountNames := accountList.Names()
sort.Strings(accountNames)
c.Logger.Info(fmt.Sprintf("%v\n", accountNames))
for _, accountName := range accountNames {
c.Logger.Debug(fmt.Sprintf("Ensuring account with name '%s' is present", accountName))
// this error can never happen here, there is a test for it.
account, _ := p.Accounts().ByName(accountName)
if _, err := c.Services.GetAccount(ctx, account.Address); err == nil {
c.Logger.Debug("Account is present")
continue
}
a, _, err := c.Services.CreateAccount(
ctx,
signerAccount,
[]accounts.PublicKey{{
Public: account.Key.ToConfig().PrivateKey.PublicKey(),
Weight: flow.AccountKeyWeightThreshold,
SigAlgo: account.Key.SigAlgo(),
HashAlgo: account.Key.HashAlgo(),
}})
if err != nil {
return nil, err
}
c.Logger.Info("Account created " + a.Address.String())
if a.Address.String() != account.Address.String() {
// this condition happens when we create accounts defined in flow.json
// after some other accounts were created manually.
// In this case, account addresses may not match the expected values
c.Logger.Error("Account address mismatch. Expected " + account.Address.String() + ", got " + a.Address.String())
}
}
return c, nil
}
// InitializeContracts installs all contracts in the deployment block for the configured network
func (c *Connector) InitializeContracts(ctx context.Context) *Connector {
if err := c.InitializeContractsE(ctx); err != nil {
log.Fatal(err)
}
return c
}
// InitializeContractsE installs all contracts in the deployment block for the configured network
// and returns an error if it fails.
func (c *Connector) InitializeContractsE(ctx context.Context) error {
c.Logger.Info("Deploying contracts")
if _, err := c.Services.DeployProject(ctx, flowkit.UpdateExistingContract(true)); err != nil {
return err
}
return nil
}