2018-08-06 01:18:54 -07:00
|
|
|
package types
|
|
|
|
|
2018-11-07 04:36:42 -08:00
|
|
|
import (
|
2018-08-06 01:18:54 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-11-07 04:36:42 -08:00
|
|
|
"github.com/hashicorp/golang-lru"
|
2018-11-12 00:16:56 -08:00
|
|
|
"sync"
|
2018-08-06 01:18:54 -07:00
|
|
|
)
|
|
|
|
|
2018-08-12 19:00:35 -07:00
|
|
|
type AccessType uint8
|
|
|
|
|
|
|
|
const (
|
2019-01-28 22:06:24 -08:00
|
|
|
ReadOnly AccessType = iota
|
2018-08-12 19:00:35 -07:00
|
|
|
Transact
|
|
|
|
ContractDeploy
|
2019-02-11 17:54:13 -08:00
|
|
|
FullAccess
|
2018-08-12 19:00:35 -07:00
|
|
|
)
|
|
|
|
|
2018-08-06 01:18:54 -07:00
|
|
|
type PermStruct struct {
|
2018-11-07 04:36:42 -08:00
|
|
|
AcctId common.Address
|
2018-08-12 19:00:35 -07:00
|
|
|
AcctAccess AccessType
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
2018-09-27 04:24:38 -07:00
|
|
|
type OrgStruct struct {
|
|
|
|
OrgId string
|
2018-11-07 04:36:42 -08:00
|
|
|
Keys []string
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
2018-11-14 02:24:28 -08:00
|
|
|
|
2018-11-13 00:15:28 -08:00
|
|
|
var DefaultAccess = FullAccess
|
2018-08-06 01:18:54 -07:00
|
|
|
|
2018-11-12 00:16:56 -08:00
|
|
|
const acctMapLimit = 100
|
|
|
|
const orgKeyMapLimit = 100
|
2018-09-27 04:24:38 -07:00
|
|
|
|
2018-11-12 00:16:56 -08:00
|
|
|
var AcctMap, _ = lru.New(acctMapLimit)
|
|
|
|
|
|
|
|
var OrgKeyMap, _ = lru.New(orgKeyMapLimit)
|
|
|
|
|
|
|
|
var orgKeyLock sync.Mutex
|
2018-08-06 01:18:54 -07:00
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// sets default access to ReadOnly
|
2018-11-13 00:15:28 -08:00
|
|
|
func SetDefaultAccess() {
|
|
|
|
DefaultAccess = ReadOnly
|
|
|
|
}
|
2019-02-12 17:57:11 -08:00
|
|
|
|
|
|
|
// Adds account access to the cache
|
2018-11-07 04:36:42 -08:00
|
|
|
func AddAccountAccess(acctId common.Address, access uint8) {
|
|
|
|
AcctMap.Add(acctId, &PermStruct{AcctId: acctId, AcctAccess: AccessType(access)})
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// Returns the access type for an account. If not found returns
|
|
|
|
// default access
|
2018-08-12 19:00:35 -07:00
|
|
|
func GetAcctAccess(acctId common.Address) AccessType {
|
2018-11-07 04:36:42 -08:00
|
|
|
if AcctMap.Len() != 0 {
|
|
|
|
if val, ok := AcctMap.Get(acctId); ok {
|
|
|
|
vo := val.(*PermStruct)
|
|
|
|
return vo.AcctAccess
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 20:45:29 -08:00
|
|
|
return DefaultAccess
|
2018-08-06 01:18:54 -07:00
|
|
|
}
|
2018-09-27 04:24:38 -07:00
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// Adds org key details to cache
|
2018-11-07 04:36:42 -08:00
|
|
|
func AddOrgKey(orgId string, key string) {
|
|
|
|
if OrgKeyMap.Len() != 0 {
|
|
|
|
if val, ok := OrgKeyMap.Get(orgId); ok {
|
2018-11-12 00:16:56 -08:00
|
|
|
orgKeyLock.Lock()
|
2019-02-12 17:57:11 -08:00
|
|
|
defer orgKeyLock.Unlock()
|
2018-10-02 01:28:55 -07:00
|
|
|
// Org record exists. Append the key only
|
2018-11-07 04:36:42 -08:00
|
|
|
vo := val.(*OrgStruct)
|
|
|
|
vo.Keys = append(vo.Keys, key)
|
2018-10-02 01:28:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 04:36:42 -08:00
|
|
|
OrgKeyMap.Add(orgId, &OrgStruct{OrgId: orgId, Keys: []string{key}})
|
2018-10-02 01:28:55 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// deletes org key details from cache
|
2018-11-07 04:36:42 -08:00
|
|
|
func DeleteOrgKey(orgId string, key string) {
|
|
|
|
if val, ok := OrgKeyMap.Get(orgId); ok {
|
2018-11-12 00:16:56 -08:00
|
|
|
orgKeyLock.Lock()
|
2019-02-12 17:57:11 -08:00
|
|
|
defer orgKeyLock.Unlock()
|
2018-11-07 04:36:42 -08:00
|
|
|
vo := val.(*OrgStruct)
|
|
|
|
for i, keyVal := range vo.Keys {
|
|
|
|
if keyVal == key {
|
|
|
|
vo.Keys = append(vo.Keys[:i], vo.Keys[i+1:]...)
|
|
|
|
break
|
2018-10-02 01:28:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:57:11 -08:00
|
|
|
// Givens a orgid returns the linked keys for the org
|
2018-11-07 04:36:42 -08:00
|
|
|
func ResolvePrivateForKeys(orgId string) []string {
|
2018-09-27 04:24:38 -07:00
|
|
|
var keys []string
|
2018-11-07 04:36:42 -08:00
|
|
|
if val, ok := OrgKeyMap.Get(orgId); ok {
|
|
|
|
vo := val.(*OrgStruct)
|
|
|
|
if len(vo.Keys) > 0 {
|
|
|
|
keys = vo.Keys
|
|
|
|
} else {
|
|
|
|
keys = append(keys, orgId)
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
2018-11-07 04:36:42 -08:00
|
|
|
return keys
|
2018-09-27 04:24:38 -07:00
|
|
|
}
|
|
|
|
keys = append(keys, orgId)
|
|
|
|
return keys
|
|
|
|
}
|