/
create_account.go
90 lines (80 loc) · 2.61 KB
/
create_account.go
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
90
package replayer
import (
"fmt"
"os"
"strings"
"github.com/aurora-is-near/near-api-go"
"github.com/aurora-is-near/near-api-go/keystore"
"github.com/aurora-is-near/near-api-go/utils"
)
// TLAMinLength defines the minimum length for top-level accounts.
const TLAMinLength = 32
// CreateAccount allow to create accounts.
type CreateAccount struct {
Config *near.Config
InitialBalance string
MasterAccount string
}
// Create account with accountID.
func (ca *CreateAccount) Create(accountID string) error {
splitAccount := strings.Split(accountID, ".")
if len(splitAccount) == 1 {
// TLA (bob-with-at-least-maximum-characters)
if len(splitAccount[0]) < TLAMinLength {
return fmt.Errorf("top-level accounts must be at least %d characters.\n"+
"Note: this is for advanced usage only. Typical account names are of the form:\n"+
"app.alice.test, where the masterAccount shares the top-level account (.test).",
TLAMinLength)
}
}
// Subaccounts (short.alice.near, even.more.bob.test, and eventually peter.potato)
// Check that master account TLA matches
if !strings.HasSuffix(accountID, ca.MasterAccount) {
return fmt.Errorf("new account doesn't share the same top-level account. Expecting account name to end in %s",
ca.MasterAccount)
}
c := near.NewConnection(ca.Config.NodeURL)
// generate key
kp, err := keystore.GenerateEd25519KeyPair(accountID)
if err != nil {
return err
}
// check to see if account already exists
_, err = c.GetAccountState(accountID)
if err == nil {
return fmt.Errorf("sorry, account '%s' already exists", accountID)
} else if !strings.Contains(err.Error(), "does not exist while viewing") {
return err
}
amnt, err := utils.ParseNearAmountAsBigInt(ca.InitialBalance)
if err != nil {
return err
}
a, err := near.LoadAccount(c, ca.Config, ca.MasterAccount)
if err != nil {
fmt.Println("error")
return err
}
// save key
filename, err := kp.Write(ca.Config.NetworkID)
if err != nil {
return err
}
fmt.Printf("saving key to '%s'\n", filename)
// create account
txResult, err := a.CreateAccount(accountID, utils.PublicKeyFromEd25519(kp.Ed25519PubKey), *amnt)
if err != nil {
if err == utils.ErrRetriesExceeded {
fmt.Fprintf(os.Stderr, "Received a timeout when creating account, please run:\n")
fmt.Fprintf(os.Stderr, "`evm state %s\n", accountID)
fmt.Fprintf(os.Stderr, "to confirm creation. Keyfile for this account has been saved.\n")
} else {
// remove keyfile
os.Remove(filename)
}
return err
}
utils.PrettyPrintResponse(txResult)
fmt.Printf("account %s for network \"%s\" was created\n", accountID, ca.Config.NetworkID)
return nil
}