-
Notifications
You must be signed in to change notification settings - Fork 8
/
msig.go
436 lines (392 loc) · 13.1 KB
/
msig.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package fio
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/fioprotocol/fio-go/eos"
"sort"
"strconv"
"time"
)
// PermissionLevel wraps eos-go's type to add member functions
type PermissionLevel eos.PermissionLevel
func NewPermissionLevel(account eos.AccountName) *PermissionLevel {
return &PermissionLevel{
Actor: account,
Permission: "active",
}
}
// NewPermissionLevelSlice is a convenience function for quickly building a slice of active permissions
func NewPermissionLevelSlice(accounts []string) []*PermissionLevel {
l := make([]*PermissionLevel, 0)
sort.Strings(accounts)
for _, a := range accounts {
l = append(l, NewPermissionLevel(eos.AccountName(a)))
}
return l
}
// ToEos converts from fio.PermissionLevel to eos.PermissionLevel
func (pl PermissionLevel) ToEos() *eos.PermissionLevel {
return &eos.PermissionLevel{
Actor: pl.Actor,
Permission: pl.Permission,
}
}
type MsigAction struct {
Account eos.Name `json:"account"`
Name eos.Name `json:"name"`
Authorization PermissionLevel `json:"authorization"`
Data []byte `json:"data"`
}
type MsigApproval struct {
Level PermissionLevel `json:"level"`
Time eos.JSONTime `json:"time"`
}
type MsigApprovalsInfo struct {
Version uint8 `json:"version"`
ProposalName eos.Name `json:"proposal_name"`
RequestedApprovals []MsigApproval `json:"requested_approvals"`
ProvidedApprovals []MsigApproval `json:"provided_approvals"`
}
// GetApprovals returns a list of approvals for an account
func (api *API) GetApprovals(scope Name, limit int) (more bool, info []*MsigApprovalsInfo, err error) {
name, err := eos.StringToName(string(scope))
if err != nil {
return false, nil, err
}
res, err := api.GetTableRows(eos.GetTableRowsRequest{
JSON: true,
Scope: fmt.Sprintf("%d", name),
Code: "eosio.msig",
Table: "approvals2",
Limit: uint32(limit),
})
if err != nil {
return false, nil, err
}
more = res.More
info = make([]*MsigApprovalsInfo, 0)
err = json.Unmarshal(res.Rows, &info)
return
}
// HasRequested checks if an account is on the list of requested signatures
func (info MsigApprovalsInfo) HasRequested(actor eos.AccountName) bool {
for _, r := range info.RequestedApprovals {
if r.Level.Actor == actor {
return true
}
}
return info.HasApproved(actor)
}
// HasApproved checks if an account has provided a signature
func (info MsigApprovalsInfo) HasApproved(actor eos.AccountName) bool {
for _, p := range info.ProvidedApprovals {
if p.Level.Actor == actor {
return true
}
}
return false
}
// TODO: not sure if this is needed
type MsigExtension struct {
Type uint16 `json:"type"`
Data []byte `json:"data"`
}
type MsigInvalidation struct {
Account eos.Name `json:"account"`
LastInvalidationTime eos.TimePoint `json:"last_invalidation_time"`
}
type MsigOldApprovalsInfo struct {
ProposalName eos.Name `json:"proposal_name"`
RequestedApprovals []PermissionLevel `json:"requested_approvals"`
ProvidedApprovals []PermissionLevel `json:"provided_approvals"`
}
// this also looks potentially incorrect:
type MsigTransaction struct {
ContextFreeActions []*Action `json:"context_free_actions"`
Actions []*Action `json:"actions"`
TransactionExtensions []*MsigExec `json:"transaction_extensions"`
}
// MsigTransactionHeader is an alias for consistent naming
type MsigTransactionHeader eos.TransactionHeader
/*
Actions
*/
// MsigApprove approves a multi-sig proposal
type MsigApprove struct {
Proposer eos.AccountName `json:"proposer"`
ProposalName eos.Name `json:"proposal_name"`
Level PermissionLevel `json:"level"`
MaxFee uint64 `json:"max_fee"`
ProposalHash eos.Checksum256 `json:"proposal_hash"`
}
func NewMsigApprove(proposer eos.AccountName, proposal eos.Name, actor eos.AccountName, proposalHash eos.Checksum256) *Action {
return NewAction("eosio.msig", "approve", actor,
&MsigApprove{
Proposer: proposer,
ProposalName: proposal,
Level: PermissionLevel{
Actor: actor,
Permission: "active",
},
MaxFee: Tokens(GetMaxFee(FeeMsigApprove)),
ProposalHash: proposalHash,
},
)
}
// MsigCancel withdraws a proposal, must be performed by the account that proposed the transaction
type MsigCancel struct {
Proposer eos.AccountName `json:"proposer"`
ProposalName eos.Name `json:"proposal_name"`
Canceler eos.AccountName `json:"canceler"`
MaxFee uint64 `json:"max_fee"`
}
func NewMsigCancel(proposer eos.AccountName, proposal eos.Name, actor eos.AccountName) *Action {
return NewAction("eosio.msig", "cancel", actor,
&MsigCancel{
Proposer: proposer,
ProposalName: proposal,
Canceler: actor,
MaxFee: Tokens(GetMaxFee(FeeMsigCancel)),
},
)
}
// MsigExec will attempt to execute a proposed transaction
type MsigExec struct {
Proposer eos.AccountName `json:"proposer"`
ProposalName eos.Name `json:"proposal_name"`
MaxFee uint64 `json:"max_fee"`
Executer eos.AccountName `json:"executer"`
}
func NewMsigExec(proposer eos.AccountName, proposal eos.Name, fee uint64, actor eos.AccountName) *Action {
return NewAction("eosio.msig", "exec", actor,
&MsigExec{
Proposer: proposer,
ProposalName: proposal,
MaxFee: fee,
Executer: actor,
},
)
}
// MsigInvalidate is used to remove all approvals and proposals for an account
type MsigInvalidate struct {
Name eos.Name `json:"name"`
MaxFee uint64 `json:"max_fee"`
}
// MsigPropose is a new proposal
type MsigPropose struct {
Proposer eos.AccountName `json:"proposer"`
ProposalName eos.Name `json:"proposal_name"`
Requested []*PermissionLevel `json:"requested"`
MaxFee uint64 `json:"max_fee"`
Trx *eos.SignedTransaction `json:"trx"`
}
type MsigWrappedPropose struct {
Proposer eos.AccountName `json:"proposer"`
ProposalName eos.Name `json:"proposal_name"`
Requested []*PermissionLevel `json:"requested"`
MaxFee uint64 `json:"max_fee"`
Trx *eos.Transaction `json:"trx"`
}
// NewMsigPropose is provided for consistency, but it will make more sense to use NewSignedMsigPropose to build *simple*
// multisig proposals since it abstracts several steps.
func NewMsigPropose(proposer eos.AccountName, proposal eos.Name, signers []*PermissionLevel, signedTx *eos.SignedTransaction) *Action {
var feeBytes uint64
packedTx, err := signedTx.Pack(CompressionNone)
if err != nil {
feeBytes = 1
} else {
feeBytes = uint64((len(packedTx.PackedTransaction) / 1000) + 1)
}
return NewAction("eosio.msig", "propose", proposer, MsigPropose{
Proposer: proposer,
ProposalName: proposal,
Requested: signers,
MaxFee: Tokens(GetMaxFee(FeeMsigPropose)) * feeBytes,
Trx: signedTx,
})
}
// NewSignedMsigPropose simplifies the process of building an MsigPropose by packing and signing the slice of Actions provided into a TX
// and then wrapping that into a signed transaction ready to be submitted.
func (api *API) NewSignedMsigPropose(proposalName Name, approvers []string, actions []*Action, expires time.Duration, signer *Account, txOpt *TxOptions) (*eos.PackedTransaction, error) {
if len(actions) == 0 {
return nil, errors.New("no actions provided")
}
if signer == nil || signer.KeyBag == nil || len(signer.KeyBag.Keys) == 0 {
return nil, errors.New("invalid signer, no private key provided")
}
for _, apvr := range approvers {
if len(apvr) > 12 {
return nil, errors.New("invalid approver in list, account name should be < 12 chars")
}
}
propTx := NewTransaction(actions, txOpt)
propTx.Expiration = eos.JSONTime{Time: time.Now().UTC().Add(expires)}
propTxSigned, propTxPacked, err := api.SignTransaction(propTx, txOpt.ChainID, CompressionNone)
if err != nil {
return nil, err
}
feeBytes := uint64((len(propTxPacked.PackedTransaction) / 1000) + 1)
newTx := NewTransaction([]*Action{NewAction(
"eosio.msig", "propose", signer.Actor, MsigPropose{
Proposer: signer.Actor,
ProposalName: proposalName.ToEos(),
Requested: NewPermissionLevelSlice(approvers),
MaxFee: Tokens(GetMaxFee(FeeMsigPropose)) * feeBytes,
Trx: propTxSigned,
},
)}, txOpt)
newTx.Expiration = eos.JSONTime{Time: time.Now().UTC().Add(expires)}
_, packedTx, err := api.SignTransaction(newTx, txOpt.ChainID, CompressionZlib)
if err != nil {
return nil, err
}
return packedTx, nil
}
// MsigUnapprove withdraws an existing approval for an account
type MsigUnapprove struct {
Proposer eos.AccountName `json:"proposer"`
ProposalName eos.Name `json:"proposal_name"`
Level PermissionLevel `json:"level"`
MaxFee uint64 `json:"max_fee"`
}
func NewMsigUnapprove(proposer eos.AccountName, proposal eos.Name, actor eos.AccountName) *Action {
return NewAction("eosio.msig", "unapprove", actor,
&MsigUnapprove{
Proposer: proposer,
ProposalName: proposal,
Level: PermissionLevel{
Actor: actor,
Permission: "active",
},
MaxFee: Tokens(GetMaxFee(FeeMsigUnapprove)),
},
)
}
type UpdateAuth struct {
Account eos.AccountName `json:"account"`
Permission eos.Name `json:"permission"`
Parent eos.Name `json:"parent"`
Auth Authority `json:"auth"`
MaxFee uint64 `json:"max_fee"`
}
// NewUpdateAuthSimple just takes a list of accounts and a threshold. Nothing fancy, most basic EOS msig account.
func NewUpdateAuthSimple(account eos.AccountName, actors []string, threshold uint32) *Action {
acts := make([]eos.PermissionLevelWeight, 0)
sort.Strings(actors) // actors must be sorted in ascending alphabetic order, or will get an invalid {$auth} error.
for _, a := range actors {
acts = append(acts, eos.PermissionLevelWeight{
Weight: 1,
Permission: eos.PermissionLevel{Actor: eos.AccountName(a), Permission: "active"}})
}
return NewAction("eosio", "updateauth", eos.AccountName(account), UpdateAuth{
Account: account,
Permission: "active",
Parent: "owner",
Auth: Authority{
Threshold: threshold,
Accounts: acts,
},
MaxFee: Tokens(GetMaxFee(FeeAuthUpdate)),
})
}
type msigProposalRow struct {
ProposalName eos.Name `json:"proposal_name"`
PackedTransaction string `json:"packed_transaction"`
}
// MsigProposal is a query response for getting details of a proposed transaction
type MsigProposal struct {
ProposalName eos.Name `json:"proposal_name"`
PackedTransaction *eos.Transaction `json:"packed_transaction"`
ProposalHash eos.Checksum256 `json:"proposal_hash"`
}
// GetProposalTransaction will lookup a specific proposal
func (api *API) GetProposalTransaction(proposalAuthor eos.AccountName, proposalName eos.Name) (*MsigProposal, error) {
name, err := eos.StringToName(string(proposalAuthor))
if err != nil {
return nil, err
}
res, err := api.GetTableRows(eos.GetTableRowsRequest{
Code: "eosio.msig",
Scope: fmt.Sprintf("%v", name),
Table: "proposal",
LowerBound: string(proposalName),
UpperBound: string(proposalName),
Index: "1",
KeyType: "name",
Limit: 1,
JSON: true,
})
if err != nil {
return nil, err
}
if len(res.Rows) < 3 {
return nil, errors.New("did not find the proposal")
}
proposal := make([]*msigProposalRow, 0)
err = json.Unmarshal(res.Rows, &proposal)
if err != nil {
return nil, err
}
txBytes, err := hex.DecodeString(proposal[0].PackedTransaction)
decoder := eos.NewDecoder(txBytes)
tx := &eos.Transaction{}
err = decoder.Decode(tx)
if err != nil {
return nil, err
}
h := sha256.New()
_, err = h.Write(txBytes)
if err != nil {
return nil, err
}
sum := h.Sum(nil)
return &MsigProposal{ProposalName: proposal[0].ProposalName, PackedTransaction: tx, ProposalHash: sum}, nil
}
type scopeResp struct {
Scope string `json:"scope"`
Count int `json:"count"`
}
// GetProposals fetches the proposal list from eosio.msig returning a map of scopes, with a count for each
func (api *API) GetProposals(offset int, limit int) (more bool, scopes map[string]int, err error) {
res, err := api.GetTableByScopeMore(eos.GetTableByScopeRequest{
Code: "eosio.msig",
Table: "proposal",
LowerBound: strconv.Itoa(offset),
UpperBound: "",
Limit: uint32(limit),
})
if err != nil {
return false, nil, err
}
more = res.More
resScopes := make([]scopeResp, 0)
err = json.Unmarshal(res.Rows, &resScopes)
if err != nil {
return false, nil, err
}
scopes = make(map[string]int)
for _, s := range resScopes {
scopes[s.Scope] = s.Count
}
return
}
// WrapExecute wraps a transaction to be executed with specific permissions via eosio.wrap
// NOTE: this is not working as expected, use caution.
type WrapExecute struct {
Executor eos.AccountName `json:"executor"`
Trx *eos.Transaction `json:"trx"`
}
func NewWrapExecute(actor eos.AccountName, executor eos.AccountName, trx *eos.Transaction) *Action {
trx.Expiration = eos.JSONTime{Time: time.Unix(0, 0)}
trx.RefBlockPrefix = 0
trx.RefBlockNum = 0
return NewAction("eosio.wrap", "execute", actor,
&WrapExecute{
Executor: executor,
Trx: trx,
},
)
}