-
Notifications
You must be signed in to change notification settings - Fork 12
/
eos.go
226 lines (205 loc) · 6.36 KB
/
eos.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package dcrm
import (
"encoding/json"
"errors"
"reflect"
"sort"
"strings"
"time"
"github.com/fsn-dev/dcrm-walletService/crypto/dcrm/cryptocoins"
"github.com/fsn-dev/dcrm-walletService/crypto/dcrm/cryptocoins/config"
"github.com/fsn-dev/dcrm-walletService/crypto/dcrm/cryptocoins/eos"
"github.com/fsn-dev/dcrm-walletService/crypto/dcrm/cryptocoins/rpcutils"
//"github.com/syndtr/goleveldb/leveldb"
"github.com/fsn-dev/dcrm-walletService/ethdb"
"github.com/astaxie/beego/logs"
"fmt"
"bytes"
"sync"
"github.com/fsn-dev/dcrm-walletService/crypto/dcrm/dev"
)
var (
lock sync.Mutex
)
func CreateRealEosAccount(accountName string, ownerkey string, activekey string) error {
fmt.Println("==========create eos account Start!!,account name = %s,ownerkey = %s,activekey = %s, ==============",accountName,ownerkey,activekey)
av := cryptocoins.NewAddressValidator("EOS_NORMAL")
if !av.IsValidAddress(accountName) {
return errors.New("eos account name format error")
}
owner, e1 := eos.HexToPubKey(ownerkey)
active, e2 := eos.HexToPubKey(activekey)
if e1 != nil || e2 != nil {
fmt.Println("==========create eos account ==========,ownerkey error = %+v,active error = %+v",e1,e2)
return errors.New("cannot convert to eos pubkey format.")
}
fmt.Println("==========create eos account,owner = %s,active = %s,==============",owner,active)
///*
ojbk, err := eos.CreateNewAccount(eos.CREATOR_ACCOUNT,eos.CREATOR_PRIVKEY,accountName,owner.String(),active.String(),eos.InitialRam)
if ojbk == false || err != nil {
fmt.Println("create eos account failed,error = %+v",err)
return errors.New("create eos account failed")
}
ojbk2, err := eos.DelegateBW(eos.CREATOR_ACCOUNT,eos.CREATOR_PRIVKEY,accountName,eos.InitialCPU,eos.InitialStakeNet,true)
if ojbk2 == false || err != nil {
fmt.Println("delegate cpu and net failed,error = %+v",err)
return errors.New("delegate cpu and net failed")
}
return err
//*/
// return nil
}
var trytimes = 50
func CheckRealEosAccount(accountName, ownerkey, activekey string) (ok bool) {
ok = false
defer func () {
if r := recover(); r != nil {
logs.Debug("check eos account","error",r)
}
}()
logs.Debug("==========check eos account Start!!==========")
// 1. check if account exists
api := "v1/chain/get_account"
data := `{"account_name":"` + accountName + `"}`
var ret string
info := new(AccountInfo)
var err error
for i := 0; i < trytimes; i++ {
ret = rpcutils.DoCurlRequest(config.ApiGateways.EosGateway.Nodeos, api, data)
logs.Debug("========check eos account========","ret",ret)
err = json.Unmarshal([]byte(ret), info)
if err != nil {
logs.Debug("========check eos account========","decode error",err)
time.Sleep(time.Duration(1)*time.Second)
continue
}
break
}
if err != nil {
logs.Debug("decode account info error")
return false
}
if info.Error != nil || info.AccountName != accountName {
logs.Debug("check real eos account error", "error", info.Error)
return false
}
// 2. check owner key
// 3. check active key
// 4. check no other keys authorized
owner, e1 := eos.HexToPubKey(ownerkey) //EOS8JXJf7nuBEs8dZ8Pc5NpS8BJJLt6bMAmthWHE8CSqzX4VEFKtq
active, e2 := eos.HexToPubKey(activekey)
if e1 != nil || e2 != nil {
logs.Debug("public key error","owner key error", e1, "active key error", e2)
return false
}
perm := info.Perms
objPerm := Permissions([]Permission{
Permission{PermName:"owner",Parent:"",RequiredAuth:Auth{Threshold:1,Keys:[]Key{Key{Key:owner.String(),Weight:1}}}},
Permission{PermName:"active",Parent:"owner",RequiredAuth:Auth{Threshold:1,Keys:[]Key{Key{Key:active.String(),Weight:1}}}},
})
sort.Sort(objPerm)
sort.Sort(perm)
if reflect.DeepEqual(perm, objPerm) == false {
logs.Debug("account permissions not match","have",perm,"required",objPerm)
return false
}
// 5. enough ram cpu net
if info.RamQuato - info.RamUsage < eos.InitialRam / 2 {
logs.Debug("account ram is too low")
return false
}
if int64(info.CpuLimit.Max) < eos.InitialCPU * 5 {
logs.Debug("account cpu is too low")
return false
}
if int64(info.NetLimit.Max) < eos.InitialStakeNet * 5 {
logs.Debug("account net bandwidth is too low")
return false
}
logs.Debug("==========check eos account Success!!==========")
return true
}
type AccountInfo struct {
AccountName string `json:"account_name"`
RamQuato uint32 `json:"ram_quato"`
NetWeight uint32 `json:"net_weight"`
CpuWeight uint32 `json:"cpu_weight"`
NetLimit Limit `json:"net_limit"`
CpuLimit Limit `json:"cpu_limit"`
RamUsage uint32 `json:"ram_usage"`
Perms Permissions `json:"permissions"`
Error map[string]interface{} `json:"error"`
}
type Permissions []Permission
func (p Permissions) Len() int {
return len([]Permission(p))
}
func (p Permissions) Less(i, j int) bool {
if []Permission(p)[i].PermName == "owner" {
return true
}
return false
}
func (p Permissions) Swap(i, j int) {
tmp := []Permission(p)[i]
[]Permission(p)[i] = []Permission(p)[j]
[]Permission(p)[j] = tmp
}
type Permission struct {
PermName string `json:"perm_name"`
Parent string `json:"parent"`
RequiredAuth Auth `json:"required_auth"`
}
type Auth struct {
Threshold int `json:"threshold"`
Keys []Key `json:"keys"`
}
type Key struct {
Key string `json:"key"`
Weight int `json:"weight"`
}
type Limit struct {
Used int64 `used`
Available int64 `available`
Max int64 `max`
}
func GetEosAccount() (acct, owner, active string) {
lock.Lock()
dir := dev.GetEosDbDir()
db,err := ethdb.NewLDBDatabase(dir, 0, 0)
if err != nil {
logs.Debug("==============open db fail.============")
lock.Unlock()
return "", "", ""
}
var data string
var b bytes.Buffer
b.WriteString("")
b.WriteByte(0)
b.WriteString("")
iter := db.NewIterator()
for iter.Next() {
key := string(iter.Key())
value := string(iter.Value())
if strings.EqualFold(key,string([]byte("eossettings"))) {
data = value
break
}
}
iter.Release()
if data == "" {
fmt.Println("===============GetEosAccount,get data fail.==================")
db.Close()
lock.Unlock()
return "","",""
}
datas := strings.Split(string(data),":")
if len(datas) == 5 && datas[0] == "EOS_INITIATE" {
db.Close()
lock.Unlock()
return datas[1], datas[2], datas[3]
}
db.Close()
lock.Unlock()
return "", "", ""
}