This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
template_account.go
110 lines (102 loc) · 3.02 KB
/
template_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package spec
import (
"fmt"
"github.com/hyperledger/burrow/acm/balance"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/genesis"
"github.com/hyperledger/burrow/keys"
"github.com/hyperledger/burrow/permission"
)
func (ta TemplateAccount) Validator(keyClient keys.KeyClient, index int, generateNodeKeys bool) (*genesis.Validator, error) {
var err error
gv := new(genesis.Validator)
gv.PublicKey, gv.Address, err = ta.RealisePublicKeyAndAddress(keyClient)
if err != nil {
return nil, err
}
if generateNodeKeys && ta.NodeAddress == nil {
// If neither PublicKey or Address set then generate a new one
address, err := keyClient.Generate("nodekey-"+ta.Name, crypto.CurveTypeEd25519)
if err != nil {
return nil, err
}
ta.NodeAddress = &address
}
gv.Amount = ta.Balances().GetPower(DefaultPower)
if ta.Name == "" {
gv.Name = accountNameFromIndex(index)
} else {
gv.Name = ta.Name
}
gv.UnbondTo = []genesis.BasicAccount{{
Address: gv.Address,
PublicKey: gv.PublicKey,
Amount: gv.Amount,
}}
gv.NodeAddress = ta.NodeAddress
return gv, nil
}
func (ta TemplateAccount) AccountPermissions() (permission.AccountPermissions, error) {
basePerms, err := permission.BasePermissionsFromStringList(ta.Permissions)
if err != nil {
return permission.ZeroAccountPermissions, nil
}
return permission.AccountPermissions{
Base: basePerms,
Roles: ta.Roles,
}, nil
}
func (ta TemplateAccount) GenesisAccount(keyClient keys.KeyClient, index int) (*genesis.Account, error) {
var err error
ga := new(genesis.Account)
ga.PublicKey, ga.Address, err = ta.RealisePublicKeyAndAddress(keyClient)
if err != nil {
return nil, err
}
ga.Amount = ta.Balances().GetNative(DefaultAmount)
if ta.Name == "" {
ga.Name = accountNameFromIndex(index)
} else {
ga.Name = ta.Name
}
if ta.Permissions == nil {
ga.Permissions = permission.DefaultAccountPermissions.Clone()
} else {
ga.Permissions, err = ta.AccountPermissions()
if err != nil {
return nil, err
}
}
return ga, nil
}
// Adds a public key and address to the template. If PublicKey will try to fetch it by Address.
// If both PublicKey and Address are not set will use the keyClient to generate a new keypair
func (ta TemplateAccount) RealisePublicKeyAndAddress(keyClient keys.KeyClient) (pubKey crypto.PublicKey, address crypto.Address, err error) {
if ta.PublicKey == nil {
if ta.Address == nil {
// If neither PublicKey or Address set then generate a new one
address, err = keyClient.Generate(ta.Name, crypto.CurveTypeEd25519)
if err != nil {
return
}
} else {
address = *ta.Address
}
// Get the (possibly existing) key
pubKey, err = keyClient.PublicKey(address)
if err != nil {
return
}
} else {
address = (*ta.PublicKey).Address()
if ta.Address != nil && *ta.Address != address {
err = fmt.Errorf("template address %s does not match public key derived address %s", ta.Address,
ta.PublicKey)
}
pubKey = *ta.PublicKey
}
return
}
func (ta TemplateAccount) Balances() balance.Balances {
return ta.Amounts
}