/
keys_json.go
170 lines (137 loc) · 4.02 KB
/
keys_json.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
package types
import (
"encoding/json"
"github.com/ProtoconNet/mitum-currency/v3/common"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/encoder"
"github.com/ProtoconNet/mitum2/util/hint"
"github.com/ProtoconNet/mitum2/util/valuehash"
)
type KeyJSONMarshaler struct {
hint.BaseHinter
Weight uint `json:"weight"`
Key base.Publickey `json:"key"`
}
func (ky BaseAccountKey) MarshalJSON() ([]byte, error) {
return util.MarshalJSON(KeyJSONMarshaler{
BaseHinter: ky.BaseHinter,
Weight: ky.w,
Key: ky.k,
})
}
type KeyJSONUnmarshaler struct {
Hint hint.Hint `json:"_hint"`
Weight uint `json:"weight"`
Key string `json:"key"`
}
func (ky *BaseAccountKey) DecodeJSON(b []byte, enc encoder.Encoder) error {
e := util.StringError("decode json of BaseAccountKey")
var uk KeyJSONUnmarshaler
if err := enc.Unmarshal(b, &uk); err != nil {
return e.Wrap(err)
}
return ky.unpack(enc, uk.Hint, uk.Weight, uk.Key)
}
type KeysJSONMarshaler struct {
hint.BaseHinter
Hash util.Hash `json:"hash"`
Keys []AccountKey `json:"keys"`
Threshold uint `json:"threshold"`
}
func (ks BaseAccountKeys) MarshalJSON() ([]byte, error) {
return util.MarshalJSON(KeysJSONMarshaler{
BaseHinter: ks.BaseHinter,
Hash: ks.h,
Keys: ks.keys,
Threshold: ks.threshold,
})
}
func (ks EthAccountKeys) MarshalJSON() ([]byte, error) {
return util.MarshalJSON(KeysJSONMarshaler{
BaseHinter: ks.BaseHinter,
Hash: ks.h,
Keys: ks.keys,
Threshold: ks.threshold,
})
}
func (ks NilAccountKeys) MarshalJSON() ([]byte, error) {
return util.MarshalJSON(KeysJSONMarshaler{
BaseHinter: ks.BaseHinter,
Hash: ks.h,
Threshold: ks.threshold,
})
}
func (ks ContractAccountKeys) MarshalJSON() ([]byte, error) {
return util.MarshalJSON(KeysJSONMarshaler{
BaseHinter: ks.BaseHinter,
Hash: ks.h,
Keys: ks.keys,
Threshold: ks.threshold,
})
}
type KeysJSONUnMarshaler struct {
Hint hint.Hint `json:"_hint"`
Keys json.RawMessage `json:"keys"`
Threshold uint `json:"threshold"`
}
type KeysHashJSONUnMarshaler struct {
Hash valuehash.HashDecoder `json:"hash"`
}
type EthKeysHashJSONUnMarshaler struct {
Hash common.HashDecoder `json:"hash"`
}
func (ks *BaseAccountKeys) DecodeJSON(b []byte, enc encoder.Encoder) error {
e := util.StringError("decode json of BaseAccountKeys")
var uks KeysJSONUnMarshaler
if err := enc.Unmarshal(b, &uks); err != nil {
return e.Wrap(err)
}
var hash util.Hash
var uhs KeysHashJSONUnMarshaler
if err := enc.Unmarshal(b, &uhs); err != nil {
return e.Wrap(err)
}
hash = uhs.Hash.Hash()
return ks.unpack(enc, uks.Hint, hash, uks.Keys, uks.Threshold)
}
func (ks *EthAccountKeys) DecodeJSON(b []byte, enc encoder.Encoder) error {
e := util.StringError("decode json of EthAccountKeys")
var uks KeysJSONUnMarshaler
if err := enc.Unmarshal(b, &uks); err != nil {
return e.Wrap(err)
}
var hash util.Hash
var uhs EthKeysHashJSONUnMarshaler
if err := enc.Unmarshal(b, &uhs); err != nil {
return e.Wrap(err)
}
hash = uhs.Hash.Hash()
return ks.unpack(enc, uks.Hint, hash, uks.Keys, uks.Threshold)
}
func (ks *NilAccountKeys) DecodeJSON(b []byte, enc encoder.Encoder) error {
e := util.StringError("decode json of NilAccountKeys")
var uks KeysJSONUnMarshaler
if err := enc.Unmarshal(b, &uks); err != nil {
return e.Wrap(err)
}
var hash util.Hash
var uhs KeysHashJSONUnMarshaler
if err := enc.Unmarshal(b, &uhs); err != nil {
return e.Wrap(err)
}
hash = uhs.Hash.Hash()
return ks.unpack(enc, uks.Hint, hash, uks.Threshold)
}
func (ks *ContractAccountKeys) DecodeJSON(b []byte, enc encoder.Encoder) error {
e := util.StringError("decode json of BaseAccountKeys")
var uks KeysJSONUnMarshaler
if err := enc.Unmarshal(b, &uks); err != nil {
return e.Wrap(err)
}
var uhs KeysHashJSONUnMarshaler
if err := enc.Unmarshal(b, &uhs); err != nil {
return e.Wrap(err)
}
return ks.unpack(enc, uks.Hint, uhs.Hash, uks.Keys, uks.Threshold)
}