-
Notifications
You must be signed in to change notification settings - Fork 45
/
tablet_auth_link.go
107 lines (82 loc) · 3.05 KB
/
tablet_auth_link.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
package statedb
import (
"fmt"
pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
pbstatedb "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/statedb/v1"
"github.com/eoscanada/eos-go"
"github.com/eoscanada/eos-go/system"
"github.com/golang/protobuf/proto"
"github.com/streamingfast/fluxdb"
)
const alCollection = 0xB100
const alPrefix = "al"
func init() {
fluxdb.RegisterTabletFactory(alCollection, alPrefix, func(identifier []byte) (fluxdb.Tablet, error) {
if len(identifier) < 8 {
return nil, fluxdb.ErrInvalidKeyLengthAtLeast("auth link tablet identifier", 8, len(identifier))
}
return AuthLinkTablet(identifier[0:8]), nil
})
}
func NewAuthLinkTablet(account string) AuthLinkTablet {
return AuthLinkTablet(standardNameToBytes(account))
}
// AuthLinkTablet tablet is composed
type AuthLinkTablet []byte
func (t AuthLinkTablet) Collection() uint16 {
return alCollection
}
func (t AuthLinkTablet) Identifier() []byte {
return t
}
func (t AuthLinkTablet) Row(height uint64, primaryKey []byte, data []byte) (fluxdb.TabletRow, error) {
if len(primaryKey) != 16 {
return nil, fluxdb.ErrInvalidKeyLength("auth link primary key", 16, len(primaryKey))
}
return &AuthLinkRow{baseRow(t, height, primaryKey, data)}, nil
}
func (t AuthLinkTablet) String() string {
return alPrefix + ":" + bytesToName(t)
}
type AuthLinkRow struct {
fluxdb.BaseTabletRow
}
func NewInsertAuthLinkRow(blockNum uint64, actionTrace *pbcodec.ActionTrace) (*AuthLinkRow, error) {
var linkAuth *system.LinkAuth
if err := actionTrace.Action.UnmarshalData(&linkAuth); err != nil {
return nil, err
}
return newAuthLinkRow(blockNum, string(linkAuth.Account), string(linkAuth.Code), string(linkAuth.Type), string(linkAuth.Requirement))
}
func NewDeleteAuthLinkRow(blockNum uint64, actionTrace *pbcodec.ActionTrace) (*AuthLinkRow, error) {
var unlinkAuth *system.UnlinkAuth
if err := actionTrace.Action.UnmarshalData(&unlinkAuth); err != nil {
return nil, err
}
return newAuthLinkRow(blockNum, string(unlinkAuth.Account), string(unlinkAuth.Code), string(unlinkAuth.Type), "")
}
func newAuthLinkRow(blockNum uint64, account, contract, action string, permission string) (row *AuthLinkRow, err error) {
tablet := NewAuthLinkTablet(account)
primaryKey := standardNameToBytes(string(contract), string(action))
var value []byte
if permission != "" {
pb := pbstatedb.AuthLinkValue{Permission: eos.MustStringToName(permission)}
if value, err = proto.Marshal(&pb); err != nil {
return nil, fmt.Errorf("marshal proto: %w", err)
}
}
return &AuthLinkRow{baseRow(tablet, blockNum, primaryKey, value)}, nil
}
func (r *AuthLinkRow) Explode() (contract, action string) {
return bytesToName2(r.PrimaryKey())
}
func (r *AuthLinkRow) Permission() (out eos.PermissionName, err error) {
pb := pbstatedb.AuthLinkValue{}
if err := proto.Unmarshal(r.Value(), &pb); err != nil {
return out, fmt.Errorf("marshal proto: %w", err)
}
return eos.PermissionName(eos.NameToString(pb.Permission)), nil
}
func (r *AuthLinkRow) String() string {
return r.Stringify(bytesToJoinedName2(r.PrimaryKey()))
}