This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdelegate.go
157 lines (134 loc) · 3.42 KB
/
delegate.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
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package auth
import (
"context"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/crypto"
"github.com/ava-labs/indexvm/storage"
)
var _ chain.Auth = (*Delegate)(nil)
const (
actorPaysBit uint8 = 0
)
type Delegate struct {
Actor crypto.PublicKey `json:"actor"`
Signer crypto.PublicKey `json:"signer"`
Signature crypto.Signature `json:"signature"`
ActorPays bool `json:"actorPays"`
}
func (d *Delegate) payer() crypto.PublicKey {
if d.ActorPays {
return d.Actor
}
return d.Signer
}
func (*Delegate) MaxUnits(
chain.Rules,
) uint64 {
return 1 + crypto.PublicKeyLen*2 + crypto.SignatureLen*5 // make signatures more expensive
}
func (*Delegate) ValidRange(chain.Rules) (int64, int64) {
return -1, -1
}
func (d *Delegate) StateKeys() [][]byte {
return [][]byte{
storage.PrefixBalanceKey(d.payer()), // fee payer
storage.PrefixPermissionsKey(d.Actor, d.Signer),
}
}
func (d *Delegate) AsyncVerify(msg []byte) error {
if !crypto.Verify(msg, d.Signer, d.Signature) {
return ErrInvalidSignature
}
return nil
}
// Verify could be used to perform complex ACL rules that require state access
// to check.
func (d *Delegate) Verify(
ctx context.Context,
r chain.Rules,
db chain.Database,
action chain.Action,
) (uint64, error) {
if d.Actor == d.Signer {
return 0, ErrActorEqualsSigner
}
// Allowed will check the existence of actor
//
// Note: actor and signer equivalence does not mean an action is allowed (key
// could have been rotated)
if err := Authorized(ctx, db, action, d.Actor, d.Signer, d.ActorPays); err != nil {
return 0, err
}
return d.MaxUnits(r), nil
}
func (d *Delegate) Payer() []byte {
payer := d.payer()
return payer[:]
}
func (d *Delegate) Marshal(p *codec.Packer) {
p.PackPublicKey(d.Actor)
p.PackPublicKey(d.Signer)
p.PackSignature(d.Signature)
p.PackBool(d.ActorPays)
}
func UnmarshalDelegate(p *codec.Packer) (chain.Auth, error) {
var d Delegate
p.UnpackPublicKey(&d.Actor)
p.UnpackPublicKey(&d.Signer)
p.UnpackSignature(&d.Signature)
d.ActorPays = p.UnpackBool()
return &d, p.Err()
}
func (d *Delegate) CanDeduct(
ctx context.Context,
db chain.Database,
amount uint64,
) error {
// Account must exist if [u] > 0
u, _, err := storage.GetBalance(ctx, db, d.payer())
if err != nil {
return err
}
if u < amount {
return storage.ErrInvalidBalance
}
return nil
}
func (d *Delegate) Deduct(
ctx context.Context,
db chain.Database,
amount uint64,
) error {
return storage.SubUnlockedBalance(ctx, db, d.payer(), amount)
}
func (d *Delegate) Refund(
ctx context.Context,
db chain.Database,
amount uint64,
) error {
// Discard any funds returned if the account doesn't exist
//
// This could occur if the transaction closes out the account
_, err := storage.AddUnlockedBalance(ctx, db, d.payer(), amount, true)
return err
}
var _ chain.AuthFactory = (*DelegateFactory)(nil)
func NewDelegateFactory(
actor crypto.PublicKey,
priv crypto.PrivateKey,
actorPays bool,
) *DelegateFactory {
return &DelegateFactory{actor, priv, actorPays}
}
type DelegateFactory struct {
actor crypto.PublicKey
priv crypto.PrivateKey
actorPays bool
}
func (d *DelegateFactory) Sign(msg []byte, _ chain.Action) (chain.Auth, error) {
sig := crypto.Sign(msg, d.priv)
return &Delegate{d.actor, d.priv.PublicKey(), sig, d.actorPays}, nil
}