-
Notifications
You must be signed in to change notification settings - Fork 2
/
operation.go
743 lines (609 loc) · 22.4 KB
/
operation.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package bvm
import (
"encoding/hex"
"encoding/json"
"github.com/meshplus/gosdk/common/bvmcom"
"strconv"
)
// ContractMethod bvm contract method name
type ContractMethod string
const (
// ContractDeployContract
ContractDeployContract ContractMethod = "DeployContract"
// ContractUpgradeContract
ContractUpgradeContract ContractMethod = "UpgradeContract"
// ContractMaintainContract
ContractMaintainContract ContractMethod = "MaintainContract"
// CNSSetCName cns contract method `SetCName`
CNSSetCName ContractMethod = "SetCName"
// NodeAddNode node contract method `AddNode`
NodeAddNode ContractMethod = "AddNode"
// NodeAddVP node contract method `AddVP`
NodeAddVP ContractMethod = "AddVP"
// NodeRemoveVP node contract method `RemoveVP`
NodeRemoveVP ContractMethod = "RemoveVP"
// PermissionCreateRole permission contract method `CreateRole`
PermissionCreateRole ContractMethod = "CreateRole"
// PermissionDeleteRole permission contract method `DeleteRole`
PermissionDeleteRole ContractMethod = "DeleteRole"
// PermissionGrant permission contract method `Grant`
PermissionGrant ContractMethod = "Grant"
// PermissionRevoke permission contract method `Revoke`
PermissionRevoke ContractMethod = "Revoke"
// CASetCAMode ca contract method `SetCAMode`
CASetCAMode ContractMethod = "SetCAMode"
// CAGetCAMode ca contract method `GetCAMode`
CAGetCAMode ContractMethod = "GetCAMode"
// ProposalCreate proposal contract method `Create`
ProposalCreate ContractMethod = "Create"
// ProposalDirect proposal contract method `Direct`
ProposalDirect ContractMethod = "Direct"
// ProposalVote proposal contract method `Vote`
ProposalVote ContractMethod = "Vote"
// ProposalGrant proposal contract method `Cancel`
ProposalCancel ContractMethod = "Cancel"
// ProposalExecute proposal contract method `Execute`
ProposalExecute ContractMethod = "Execute"
// HashSet hash contract method `Set`
HashSet ContractMethod = "Set"
// HashGet hash contract method `Get`
HashGet ContractMethod = "Get"
// AccountRegister account contract method `Register`
AccountRegister ContractMethod = "Register"
// AccountAbandon account contract method `Logout`
AccountAbandon ContractMethod = "Abandon"
SetchainID ContractMethod = "SetChainID"
//CertRevoke cert contract method `CertRevoke`
CertRevoke ContractMethod = "CertRevoke"
//CertCheck cert contract method `CheckRevoke`
CertCheck ContractMethod = "CertCheck"
//CertFreeze cert contract method `CertFreeze`
CertFreeze ContractMethod = "CertFreeze"
//CertUnfreeze cert contract method `CertUnfreeze`
CertUnfreeze ContractMethod = "CertUnfreeze"
SRSInfo ContractMethod = "GetSRSInfo"
SRSBeacon ContractMethod = "Beacon"
SRSHistory ContractMethod = "GetHistory"
AddRootCA ContractMethod = "AddRootCA"
GetRootCAs ContractMethod = "GetRootCAs"
ChangeHashAlgo ContractMethod = "ChangeHashAlgo"
GetHashAlgo ContractMethod = "GetHashAlgo"
GetSupportHashAlgo ContractMethod = "GetSupportHashAlgo"
RegisterAnchor ContractMethod = "RegisterAnchorNode"
UnRegisterAnchor ContractMethod = "UnregisterAnchorNode"
ReplaceAnchor ContractMethod = "ReplaceAnchorNode"
ReadCrossChain ContractMethod = "ReadCrossChainTransaction"
ReadAnchor ContractMethod = "ReadAnchorStatus"
Timeout ContractMethod = "Timeout"
accountAddress = "0x0000000000000000000000000000000000ffff04"
proposalAddress = "0x0000000000000000000000000000000000ffff02"
hashAddress = "0x0000000000000000000000000000000000ffff01"
certAddress = "0x0000000000000000000000000000000000ffff05"
didAddress = "0x0000000000000000000000000000000000ffff06"
NormalAnchorAddress = "0x0000000000000000000000000000000000ffff08"
mpcAddress = "0x0000000000000000000000000000000000ffff09"
SystemAnchorAddress = "0x0000000000000000000000000000000000ffff0a"
rootCAAddress = "0x0000000000000000000000000000000000ffff0b"
hashAlgoAddress = "0x0000000000000000000000000000000000ffff0d"
// GenesisInfoKey the key for store genesis info in state db.
genesisInfoKey = "the_key_for_genesis_info"
)
// ProposalType proposal type
type ProposalType int
const (
// ProposalTypeConfig proposal of config type
ProposalTypeConfig ProposalType = iota
// ProposalTypePermission proposal of permission type
ProposalTypePermission
// ProposalTypeNode proposal of node type
ProposalTypeNode
// ProposalTypeCNS proposal of cns type
ProposalTypeCNS
// ProposalTypeContract proposal of contract type
ProposalTypeContract
// ProposalTypeCA proposal of ca type
ProposalTypeCA
)
func (pt ProposalType) String() string {
switch pt {
case ProposalTypeNode:
return "NODE"
case ProposalTypeConfig:
return "CONFIG"
case ProposalTypePermission:
return "PERMISSION"
case ProposalTypeCNS:
return "CNS"
case ProposalTypeContract:
return "CONTRACT"
case ProposalTypeCA:
return "CA"
default:
return "unknown proposal type"
}
}
// Operation define the operation for proposal
type Operation interface {
// Method return method name
Method() ContractMethod
// Args return args for call method
Args() []string
SetMethod(ContractMethod)
SetArgs([]string)
}
type BuiltinOperation interface {
Operation
Address() string
}
type ProposalOperationImpl struct {
operationImpl
}
func (po *ProposalOperationImpl) Address() string {
return proposalAddress
}
type HashOperationImpl struct {
operationImpl
}
func (po *HashOperationImpl) Address() string {
return hashAddress
}
type AccountOperationImpl struct {
operationImpl
}
func (ao *AccountOperationImpl) Address() string {
return accountAddress
}
type CertOperationImpl struct {
operationImpl
}
func (po *CertOperationImpl) Address() string {
return certAddress
}
type HashAlgoOperationImpl struct {
operationImpl
}
func (mo *HashAlgoOperationImpl) Address() string {
return hashAlgoAddress
}
type MPCOperationImpl struct {
operationImpl
}
func (mo *MPCOperationImpl) Address() string {
return mpcAddress
}
type RootCAOperationImpl struct {
operationImpl
}
func (ro *RootCAOperationImpl) Address() string {
return rootCAAddress
}
type ProposalContentOperation interface {
Operation
ProposalType()
}
type PermissionOperation interface {
ProposalContentOperation
PermissionType()
}
type permissionOperationImpl struct {
operationImpl
}
func (po *permissionOperationImpl) ProposalType() {}
func (po *permissionOperationImpl) PermissionType() {}
type CAOperation interface {
ProposalContentOperation
CAType()
}
type caOperationImpl struct {
operationImpl
}
func (co *caOperationImpl) ProposalType() {}
func (co *caOperationImpl) CAType() {}
type NodeOperation interface {
ProposalContentOperation
NodeType()
}
type nodeOperationImpl struct {
operationImpl
}
func (po *nodeOperationImpl) ProposalType() {}
func (po *nodeOperationImpl) NodeType() {}
// CNSOperation cns operation
type CNSOperation interface {
ProposalContentOperation
CNSType()
}
type cnsOperationImpl struct {
operationImpl
}
func (co *cnsOperationImpl) ProposalType() {}
func (co *cnsOperationImpl) CNSType() {}
type ContractOperation interface {
ProposalContentOperation
ContractType()
}
type contractOperationImpl struct {
operationImpl
}
func (co *contractOperationImpl) ProposalType() {}
func (co *contractOperationImpl) ContractType() {}
//DIDOperationImpl used for set chainID
type DIDOperationImpl struct {
operationImpl
}
//Address return did contract address
func (did *DIDOperationImpl) Address() string {
return didAddress
}
type NormalAnchorOperationImpl struct {
operationImpl
}
func (no *NormalAnchorOperationImpl) Address() string {
return NormalAnchorAddress
}
type SystemAnchorOperationImpl struct {
operationImpl
}
func (ao *SystemAnchorOperationImpl) Address() string {
return SystemAnchorAddress
}
// NewProposalCreateOperationForContract new proposal create operation for contract operation
func NewProposalCreateOperationForContract(ops ...ContractOperation) BuiltinOperation {
data := encodeProposalContentOperation(convertToProposalContentOperations(ops))
return newProposalCreateOperation(data, ProposalTypeContract)
}
// NewProposalCreateOperationForCNS new proposal create operation for cns operation
func NewProposalCreateOperationForCNS(ops ...CNSOperation) BuiltinOperation {
data := encodeProposalContentOperation(convertToProposalContentOperations(ops))
return newProposalCreateOperation(data, ProposalTypeCNS)
}
// NewProposalCreateOperationForNode new proposal create operation for node operation
func NewProposalCreateOperationForNode(ops ...NodeOperation) BuiltinOperation {
data := encodeProposalContentOperation(convertToProposalContentOperations(ops))
return newProposalCreateOperation(data, ProposalTypeNode)
}
// NewProposalCreateOperationForCA new proposal create operation for ca mode operation
func NewProposalCreateOperationForCA(ops ...CAOperation) BuiltinOperation {
data := encodeProposalContentOperation(convertToProposalContentOperations(ops))
return newProposalCreateOperation(data, ProposalTypeCA)
}
// NewProposalDirectOperationForNode new proposal direct operation for node operation
func NewProposalDirectOperationForNode(ops ...NodeOperation) BuiltinOperation {
data := encodeProposalContentOperation(convertToProposalContentOperations(ops))
return newProposalDirectOperation(data, ProposalTypeNode)
}
// NewProposalDirectOperationForCA new proposal direct operation for ca mode operation
func NewProposalDirectOperationForCA(ops ...CAOperation) BuiltinOperation {
data := encodeProposalContentOperation(convertToProposalContentOperations(ops))
return newProposalDirectOperation(data, ProposalTypeCA)
}
// convertToProposalContentOperations convert the slice of ProposalContentOperations's drive interface to ProposalContentOperations
func convertToProposalContentOperations(item interface{}) []ProposalContentOperation {
var operations []ProposalContentOperation
switch ops := item.(type) {
case []PermissionOperation:
for _, op := range ops {
operations = append(operations, op)
}
case []NodeOperation:
for _, op := range ops {
operations = append(operations, op)
}
case []ConfigOperation:
for _, op := range ops {
operations = append(operations, op)
}
case []CNSOperation:
for _, op := range ops {
operations = append(operations, op)
}
case []ContractOperation:
for _, op := range ops {
operations = append(operations, op)
}
case []CAOperation:
for _, op := range ops {
operations = append(operations, op)
}
}
return operations
}
// NewProposalCreateOperationForPermission new proposal create operation for permission operation
func NewProposalCreateOperationForPermission(ops ...PermissionOperation) BuiltinOperation {
data := encodeProposalContentOperation(convertToProposalContentOperations(ops))
return newProposalCreateOperation(data, ProposalTypePermission)
}
// NewProposalCreateOperation create a new ProposalCreate operation and return
func newProposalCreateOperation(data []byte, pType ProposalType) *ProposalOperationImpl {
return newProposalOperation(ProposalCreate, string(data), pType.String())
}
// NewProposalCreateOperation create a new ProposalCreate operation and return
func newProposalDirectOperation(data []byte, pType ProposalType) *ProposalOperationImpl {
return newProposalOperation(ProposalDirect, string(data), pType.String())
}
func newProposalOperation(method ContractMethod, args ...string) *ProposalOperationImpl {
return &ProposalOperationImpl{operationImpl{method: method, args: args}}
}
// NewProposalVoteOperation create a new ProposalVote operation and return
func NewProposalVoteOperation(proposalID int, vote bool) BuiltinOperation {
return newProposalOperation(ProposalVote, strconv.Itoa(proposalID), strconv.FormatBool(vote))
}
// NewProposalCancelOperation create a new ProposalCancel operation and return
func NewProposalCancelOperation(proposalID int) BuiltinOperation {
return newProposalOperation(ProposalCancel, strconv.Itoa(proposalID))
}
// NewProposalExecuteOperation create a new ProposalExecute operation and return
func NewProposalExecuteOperation(proposalID int) BuiltinOperation {
return newProposalOperation(ProposalExecute, strconv.Itoa(proposalID))
}
func newPermissionOperation(method ContractMethod, args ...string) PermissionOperation {
return &permissionOperationImpl{operationImpl{method: method, args: args}}
}
// NewPermissionCreateRoleOperation create a new PermissionCreateRole operation and return
func NewPermissionCreateRoleOperation(role string) PermissionOperation {
return newPermissionOperation(PermissionCreateRole, role)
}
// NewPermissionDeleteRoleOperation create a new PermissionDeleteRole operation and return
func NewPermissionDeleteRoleOperation(role string) PermissionOperation {
return newPermissionOperation(PermissionDeleteRole, role)
}
// NewPermissionDeleteRoleOperation create a new PermissionGrant operation and return
func NewPermissionGrantOperation(role string, address string) PermissionOperation {
return newPermissionOperation(PermissionGrant, role, address)
}
// NewPermissionRevokeOperation create a new PermissionRevoke operation and return
func NewPermissionRevokeOperation(role string, address string) PermissionOperation {
return newPermissionOperation(PermissionRevoke, role, address)
}
func newCAOperation(method ContractMethod, args ...string) CAOperation {
return &caOperationImpl{operationImpl{method: method, args: args}}
}
func NewCASetCAModeOperation(mode string) (CAOperation, error) {
caMode, err := bvmcom.ConvertCAMode(mode)
if err != nil {
return nil, err
}
return newCAOperation(CASetCAMode, intToString(int(caMode))), nil
}
func NewCAGetCAModeOperation() CAOperation {
return newCAOperation(CAGetCAMode)
}
func newNodeOperation(method ContractMethod, args ...string) NodeOperation {
return &nodeOperationImpl{operationImpl{method: method, args: args}}
}
// NewNodeAddNodeOperation create a new NodeAddNode operation and return
func NewNodeAddNodeOperation(pub []byte, hostname, role, namespace string) NodeOperation {
return newNodeOperation(NodeAddNode, string(pub), hostname, role, namespace)
}
// NewNodeAddVPOperation create a new NodeAddVP operation and return
func NewNodeAddVPOperation(hostname, namespace string) NodeOperation {
return newNodeOperation(NodeAddVP, hostname, namespace)
}
// NewNodeRemoveVPOperation create a new NodeRemoveVP operation and return
func NewNodeRemoveVPOperation(hostname, namespace string) NodeOperation {
return newNodeOperation(NodeRemoveVP, hostname, namespace)
}
func newCNSOperation(methodName ContractMethod, args ...string) CNSOperation {
return &cnsOperationImpl{operationImpl{
method: methodName,
args: args,
}}
}
// NewCNSSetCNameOperation create a new CNSSetCName operation and return
func NewCNSSetCNameOperation(address string, cnsName string) CNSOperation {
return newCNSOperation(CNSSetCName, address, cnsName)
}
func newContractOperation(methodName ContractMethod, opt *ContractManagerOptions) ContractOperation {
bytes, _ := json.Marshal(opt)
return &contractOperationImpl{operationImpl{
method: methodName,
args: []string{string(bytes)},
}}
}
// NewContractDeployContractOperation create a new ContractDeployContract operation and return
func NewContractDeployContractOperation(source, bin []byte, vmType string, compileOpts map[string]string) ContractOperation {
opt := &ContractManagerOptions{
Source: source,
Bin: bin,
VMType: vmType,
CompileOpt: compileOpts,
}
return newContractOperation(ContractDeployContract, opt)
}
// NewContractUpgradeContractOperation create a new ContractUpgradeContract operation and return
func NewContractUpgradeContractOperation(source, bin []byte, vmType, contractAddress string, compileOpts map[string]string) ContractOperation {
opt := &ContractManagerOptions{
Source: source,
Bin: bin,
VMType: vmType,
Addr: contractAddress,
CompileOpt: compileOpts,
}
return newContractOperation(ContractUpgradeContract, opt)
}
// NewContractUpgradeOperationByName create a new ContractUpgradeContract operation by contract name and return
func NewContractUpgradeOperationByName(source, bin []byte, vmType, contractName string, compileOpts map[string]string) ContractOperation {
opt := &ContractManagerOptions{
Source: source,
Bin: bin,
VMType: vmType,
Name: contractName,
CompileOpt: compileOpts,
}
return newContractOperation(ContractUpgradeContract, opt)
}
// NewContractMaintainContractOperation create a new ContractMaintainContract operation and return
func NewContractMaintainContractOperation(contractAddress, vmType string, opcode int) ContractOperation {
opt := &ContractManagerOptions{
Addr: contractAddress,
OpCode: opcode,
VMType: vmType,
}
return newContractOperation(ContractMaintainContract, opt)
}
// NewContractMaintainOperationByName create a new ContractMaintainContract operation by contract name and return
func NewContractMaintainOperationByName(contractName, vmType string, opcode int) ContractOperation {
opt := &ContractManagerOptions{
Name: contractName,
OpCode: opcode,
VMType: vmType,
}
return newContractOperation(ContractMaintainContract, opt)
}
func newHashOperation(method ContractMethod, args ...string) *HashOperationImpl {
return &HashOperationImpl{operationImpl{method: method, args: args}}
}
// NewSetGenesisInfoForHpcOperation create a new HashGenesisInfo operation for hyperchain and return
func NewSetGenesisInfoForHpcOperation(genesisInfo *GenesisInfo) BuiltinOperation {
genesisBytes, _ := json.Marshal(genesisInfo)
return newHashOperation(HashSet, genesisInfoKey, string(genesisBytes))
}
// NewHashSetOperation create a new HashSet operation and return
func NewHashSetOperation(key, value string) BuiltinOperation {
return newHashOperation(HashSet, key, value)
}
// NewHashGetOperation create a new HashGet operation and return
func NewHashGetOperation(key string) BuiltinOperation {
return newHashOperation(HashGet, key)
}
func newAccountOperation(method ContractMethod, args ...string) *AccountOperationImpl {
return &AccountOperationImpl{operationImpl{method: method, args: args}}
}
// NewAccountRegisterOperation create a new AccountRegister operation and return
func NewAccountRegisterOperation(address string, cert []byte) BuiltinOperation {
return newAccountOperation(AccountRegister, address, string(cert))
}
// NewAccountAbandonOperation create a new AccountAbandon operation and return
func NewAccountAbandonOperation(address string, sdkCert []byte) BuiltinOperation {
return newAccountOperation(AccountAbandon, address, string(sdkCert))
}
func newCertOperation(method ContractMethod, args ...string) *CertOperationImpl {
return &CertOperationImpl{operationImpl{method: method, args: args}}
}
func newMPCOperation(method ContractMethod, args ...string) *MPCOperationImpl {
return &MPCOperationImpl{operationImpl{method: method, args: args}}
}
func newRootCAOperation(method ContractMethod, args ...string) *RootCAOperationImpl {
return &RootCAOperationImpl{operationImpl{method: method, args: args}}
}
func newHashAlgoOperation(method ContractMethod, args ...string) *HashAlgoOperationImpl {
return &HashAlgoOperationImpl{operationImpl{method: method, args: args}}
}
func NewMPCInfoOperation(tag, ct string) BuiltinOperation {
return newMPCOperation(SRSInfo, tag, ct)
}
func NewMPCBeaconOperation(ptau []byte) BuiltinOperation {
return newMPCOperation(SRSBeacon, hex.EncodeToString(ptau))
}
func NewMPCHistoryOperation(ct string) BuiltinOperation {
return newMPCOperation(SRSHistory, ct)
}
func NewRootCAAddOperation(rootCA string) BuiltinOperation {
return newRootCAOperation(AddRootCA, rootCA)
}
func NewRootCAGetOperation() BuiltinOperation {
return newRootCAOperation(GetRootCAs)
}
// NewCertRevokeOperation create a new CertRevoke operation and return
func NewCertRevokeOperation(cert, priv []byte) (BuiltinOperation, error) {
if priv == nil {
return newCertOperation(CertRevoke, string(cert), "", ""), nil
}
msg := "revoke"
sign := []byte(msg)
if priv != nil {
key, err := ParsePriv(priv)
if err != nil {
return nil, err
}
sign, err = key.Sign([]byte(msg))
if err != nil {
return nil, err
}
}
return newCertOperation(CertRevoke, string(cert), msg, hex.EncodeToString(sign)), nil
}
// NewCertFreezeOperation create a new CertFreeze operation and return
func NewCertFreezeOperation(cert, priv []byte) (BuiltinOperation, error) {
msg := "freeze"
sign := []byte(msg)
if priv != nil {
key, err := ParsePriv(priv)
if err != nil {
return nil, err
}
sign, err = key.Sign([]byte(msg))
if err != nil {
return nil, err
}
}
return newCertOperation(CertFreeze, string(cert), msg, hex.EncodeToString(sign)), nil
}
// NewCertUnfreezeOperation create a new CertUnfreeze operation and return
func NewCertUnfreezeOperation(cert, priv []byte) (BuiltinOperation, error) {
msg := "unfreeze"
sign := []byte(msg)
if priv != nil {
key, err := ParsePriv(priv)
if err != nil {
return nil, err
}
sign, err = key.Sign([]byte(msg))
if err != nil {
return nil, err
}
}
return newCertOperation(CertUnfreeze, string(cert), msg, hex.EncodeToString(sign)), nil
}
// NewCertCheckOperation create a new CertCheck operation and return
func NewCertCheckOperation(cert []byte) BuiltinOperation {
return newCertOperation(CertCheck, string(cert))
}
//NewDIDSetChainIDOperation create a setChainID operation
func NewDIDSetChainIDOperation(chainID string) BuiltinOperation {
return &DIDOperationImpl{
operationImpl{method: SetchainID, args: []string{chainID}},
}
}
func NewNormalAnchorOperation(method ContractMethod, args []string) BuiltinOperation {
return &NormalAnchorOperationImpl{
operationImpl{method: method, args: args},
}
}
func NewSystemAnchorOperation(method ContractMethod, args ...string) BuiltinOperation {
return &SystemAnchorOperationImpl{
operationImpl{method: method, args: args},
}
}
func NewHashGetSupportOperation() BuiltinOperation {
return newHashAlgoOperation(GetSupportHashAlgo)
}
func NewHashGetAlgoOperation() BuiltinOperation {
return newHashAlgoOperation(GetHashAlgo)
}
func NewHashChangeHashAlgo(data []byte) BuiltinOperation {
return newHashAlgoOperation(ChangeHashAlgo, string(data))
}
// operationImpl the implementation of Operation
type operationImpl struct {
method ContractMethod
args []string
}
func NewOperation() Operation {
return &operationImpl{
method: "",
args: nil,
}
}
func (o *operationImpl) Method() ContractMethod {
return o.method
}
func (o *operationImpl) Args() []string {
return o.args
}
func (o *operationImpl) SetArgs(params []string) {
o.args = params
}
func (o *operationImpl) SetMethod(method ContractMethod) {
o.method = method
}