/
structures.go
180 lines (151 loc) · 4.51 KB
/
structures.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
package metaservice
import (
encodingJson "encoding/json"
"fmt"
"math/big"
"reflect"
"strconv"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/metadium/go-delegator/log"
"gopkg.in/validator.v2"
)
const (
// addressSize = 20
// hashSize = 32
// signatureSize = 65
// fileHashSize = 46
// timestampSize = 14
// minimumUserHashSize = 3
// minimumUserDataSize = 10
addressHashIdx = 2
)
type metaIDCreateParams struct {
Address common.Address `json:"address" validate:"len=20"`
Signature hexutil.Bytes `json:"signature" validate:"len=65"` // Sign(Address)
Recaptcha string `json:"recaptcha"`
}
type metaIDExecuteParams struct {
MetaID common.Address `json:"meta_id" validate:"len=20"`
From common.Address `json:"from" validate:"len=20"`
To common.Address `json:"to" validate:"len=20"`
Value *big.Int `json:"value"`
Data hexutil.Bytes `json:"data"`
Nonce *big.Int `json:"nonce"`
Signature hexutil.Bytes `json:"signature" validate:"len=65"`
}
type metaIDApproveParams struct {
MetaID common.Address `json:"meta_id" validate:"len=20"`
From common.Address `json:"from" validate:"len=20"`
// To common.Address `json:"to" validate:"len=20"`
Id hexutil.Bytes `json:"id" validate:"len=32"`
Approve bool `json:"approve"`
Nonce *big.Int `json:"nonce"`
Signature hexutil.Bytes `json:"signature" validate:"len=65"`
}
type metaIDBackupParams struct {
Address common.Address `json:"address" validate:"len=20"`
MetaID hexutil.Bytes `json:"meta_id" validate:"len=20"`
EncData hexutil.Bytes `json:"enc_data" validate:"min=66"`
Signature hexutil.Bytes `json:"signature" validate:"len=65"` // Sign(MetaId)
}
type metaIDGetUserDataParams struct {
//TODO: 테스트로 인해 address/signature 필드 제거 추후 결정 필요
// NewAddress common.Address `json:"address_new"`
FileID string `json:"file_id" validate:"len=46"`
// Signature hexutil.Bytes `json:"signature" validate:"len=65"` // Sign(FileId)
}
func init() {
validator.SetValidationFunc("itemlen", checkBytesLength)
}
func fillParam(p interface{}, obj interface{}) Error {
jsonBytes, err1 := encodingJson.Marshal(obj)
err1 = encodingJson.Unmarshal(jsonBytes, p)
if err1 != nil {
log.Errorf("fillParam err : %v", err1)
return &invalidMessageError{err1.Error()}
}
if errs := validator.Validate(p); errs != nil {
// values not valid, deal with errors here
return &invalidParamsError{errs.Error()}
}
// err := p.validate()
// if err != nil {
// return err
// }
return nil
}
func getParameter(method string, params []interface{}) (interface{}, Error) {
if len(params) != 1 {
return nil, &invalidParamsError{"Invalid params."}
}
obj := params[0]
log.Debug("param", obj)
switch method {
case "create_meta_id":
var reqParam metaIDCreateParams
err := fillParam(&reqParam, obj)
if err != nil {
return nil, err
}
return reqParam, nil
case "delegated_execute":
var reqParam metaIDExecuteParams
err := fillParam(&reqParam, obj)
if err != nil {
return nil, err
}
return reqParam, nil
case "delegated_approve":
var reqParam metaIDApproveParams
err := fillParam(&reqParam, obj)
if err != nil {
return nil, err
}
return reqParam, nil
case "backup_user_data":
var reqParam metaIDBackupParams
err := fillParam(&reqParam, obj)
if err != nil {
return nil, err
}
return reqParam, nil
case "get_user_data":
var reqParam metaIDGetUserDataParams
err := fillParam(&reqParam, obj)
if err != nil {
return nil, err
}
return reqParam, nil
case "get_identity_manager_address", "get_topic_registry_address", "get_attestation_agency_registry_address", "get_achievement_manager_address", "get_achievement_address":
return nil, nil
default:
return nil, &methodNotFoundError{method}
}
}
func checkBytesLength(v interface{}, param string) error {
st := reflect.ValueOf(v)
if st.Kind() != reflect.Slice {
return fmt.Errorf("user_hash_list: invalid parameter")
}
p, err := asInt(param)
if err != nil {
return validator.ErrBadParameter
}
list := v.([]hexutil.Bytes)
for _, hash := range list {
if int64(len(hash)) != p {
return fmt.Errorf("user_hash_list: invalid element")
}
}
return nil
}
// asInt returns the parameter as a int64
// or panics if it can't convert
func asInt(param string) (int64, error) {
i, err := strconv.ParseInt(param, 0, 32)
if err != nil {
return 0, validator.ErrBadParameter
}
return i, nil
}