-
Notifications
You must be signed in to change notification settings - Fork 14
/
ptmkey.go
150 lines (134 loc) · 3.95 KB
/
ptmkey.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
package contract
import (
_ "errors"
"fmt"
_ "net"
_ "strconv"
_ "strings"
"github.com/qlcchain/go-qlc/common"
_ "github.com/qlcchain/go-qlc/common/statedb"
_ "github.com/qlcchain/go-qlc/common/topic"
"github.com/qlcchain/go-qlc/common/types"
"github.com/qlcchain/go-qlc/common/util"
"github.com/qlcchain/go-qlc/common/vmcontract/contractaddress"
cfg "github.com/qlcchain/go-qlc/config"
"github.com/qlcchain/go-qlc/vm/contract/abi"
"github.com/qlcchain/go-qlc/vm/vmstore"
)
// PtmkeyContract contract define
var PtmkeyContract = NewChainContract(
map[string]Contract{
abi.MethodNamePtmKeyDelete: &PtmKeyDelete{
BaseContract: BaseContract{
Describe: Describe{
specVer: SpecVer2,
signature: true,
work: true,
},
},
},
abi.MethodNamePtmKeyUpdate: &PtmKeyUpdate{
BaseContract: BaseContract{
Describe: Describe{
specVer: SpecVer2,
signature: true,
work: true,
},
},
},
},
abi.PtmKeyABI,
abi.JsonPtmKey,
)
const PtmkeyDefaultLength = 44
type PtmKeyUpdate struct {
BaseContract
}
func (pku *PtmKeyUpdate) ProcessSend(ctx *vmstore.VMContext, block *types.StateBlock) (*types.PendingKey, *types.PendingInfo, error) {
if block.GetToken() != cfg.ChainToken() {
return nil, nil, ErrToken
}
pki := new(abi.PtmKeyInfo)
err := abi.PtmKeyABI.UnpackMethod(pki, abi.MethodNamePtmKeyUpdate, block.GetData())
if err != nil {
return nil, nil, ErrUnpackMethod
}
err = abi.PtmKeyInfoCheck(ctx, pki.Btype, []byte(pki.Pubkey))
if err != nil {
return nil, nil, ErrCheckParam
}
//fmt.Printf("ProcessSend:get pki Btype(%d) Pubkey(%s)\n", pki.Btype, pki.Pubkey)
err = pku.SetStorage(ctx, block.Address, pki.Btype, []byte(pki.Pubkey))
if err != nil {
return nil, nil, ErrSetStorage
}
return nil, nil, nil
}
func (pku *PtmKeyUpdate) SetStorage(ctx *vmstore.VMContext, account types.Address, vBtype uint16, vKey []byte) error {
data, err := abi.PtmKeyABI.PackVariable(abi.VariableNamePtmKeyStorageVar, string(vKey[:]), true)
if err != nil {
//fmt.Printf("SetStorage:PackVariable err(%s)\n", err)
return err
}
if vBtype < common.PtmKeyVBtypeDefault || vBtype >= common.PtmKeyVBtypeInvaild {
return ErrCheckParam
}
if len(vKey) != PtmkeyDefaultLength {
return ErrCheckParam
}
var key []byte
key = append(key, account[:]...)
key = append(key, util.BE_Uint16ToBytes(vBtype)...)
//fmt.Printf("SetStorage:get key(%s) data(%s)\n", string(key[:]), data)
err = ctx.SetStorage(contractaddress.PtmKeyKVAddress[:], key, data)
if err != nil {
//fmt.Printf("SetStorage:err(%s)\n", err)
return err
}
return nil
}
type PtmKeyDelete struct {
BaseContract
}
func (pkd *PtmKeyDelete) ProcessSend(ctx *vmstore.VMContext, block *types.StateBlock) (*types.PendingKey, *types.PendingInfo, error) {
if block.GetToken() != cfg.ChainToken() {
return nil, nil, ErrToken
}
pki := new(abi.PtmKeyDeleteInfo)
err := abi.PtmKeyABI.UnpackMethod(pki, abi.MethodNamePtmKeyDelete, block.GetData())
if err != nil {
return nil, nil, ErrUnpackMethod
}
pks, _ := abi.GetPtmKeyByAccountAndBtype(ctx, block.Address, pki.Btype)
if pks != nil {
for _, pk := range pks {
err = pkd.SetStorage(ctx, block.Address, pk.Btype, []byte(pk.Pubkey))
if err != nil {
return nil, nil, ErrSetStorage
}
}
} else {
return nil, nil, fmt.Errorf("PtmKeyDelete:get nil")
}
return nil, nil, nil
}
func (pkd *PtmKeyDelete) SetStorage(ctx *vmstore.VMContext, account types.Address, vBtype uint16, vKey []byte) error {
data, err := abi.PtmKeyABI.PackVariable(abi.VariableNamePtmKeyStorageVar, string(vKey), false)
if err != nil {
return err
}
if vBtype < common.PtmKeyVBtypeDefault || vBtype >= common.PtmKeyVBtypeInvaild {
return ErrCheckParam
}
if len(vKey) != PtmkeyDefaultLength {
return ErrCheckParam
}
var key []byte
key = append(key, account[:]...)
key = append(key, util.BE_Uint16ToBytes(vBtype)...)
err = ctx.SetStorage(contractaddress.PtmKeyKVAddress[:], key, data)
if err != nil {
return err
}
return nil
}